rtc-bq32k.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Driver for TI BQ32000 RTC.
  3. *
  4. * Copyright (C) 2009 Semihalf.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/i2c.h>
  12. #include <linux/rtc.h>
  13. #include <linux/init.h>
  14. #include <linux/errno.h>
  15. #include <linux/bcd.h>
  16. #define BQ32K_SECONDS 0x00 /* Seconds register address */
  17. #define BQ32K_SECONDS_MASK 0x7F /* Mask over seconds value */
  18. #define BQ32K_STOP 0x80 /* Oscillator Stop flat */
  19. #define BQ32K_MINUTES 0x01 /* Minutes register address */
  20. #define BQ32K_MINUTES_MASK 0x7F /* Mask over minutes value */
  21. #define BQ32K_OF 0x80 /* Oscillator Failure flag */
  22. #define BQ32K_HOURS_MASK 0x3F /* Mask over hours value */
  23. #define BQ32K_CENT 0x40 /* Century flag */
  24. #define BQ32K_CENT_EN 0x80 /* Century flag enable bit */
  25. struct bq32k_regs {
  26. uint8_t seconds;
  27. uint8_t minutes;
  28. uint8_t cent_hours;
  29. uint8_t day;
  30. uint8_t date;
  31. uint8_t month;
  32. uint8_t years;
  33. };
  34. static struct i2c_driver bq32k_driver;
  35. static int bq32k_read(struct device *dev, void *data, uint8_t off, uint8_t len)
  36. {
  37. struct i2c_client *client = to_i2c_client(dev);
  38. struct i2c_msg msgs[] = {
  39. {
  40. .addr = client->addr,
  41. .flags = 0,
  42. .len = 1,
  43. .buf = &off,
  44. }, {
  45. .addr = client->addr,
  46. .flags = I2C_M_RD,
  47. .len = len,
  48. .buf = data,
  49. }
  50. };
  51. if (i2c_transfer(client->adapter, msgs, 2) == 2)
  52. return 0;
  53. return -EIO;
  54. }
  55. static int bq32k_write(struct device *dev, void *data, uint8_t off, uint8_t len)
  56. {
  57. struct i2c_client *client = to_i2c_client(dev);
  58. uint8_t buffer[len + 1];
  59. buffer[0] = off;
  60. memcpy(&buffer[1], data, len);
  61. if (i2c_master_send(client, buffer, len + 1) == len + 1)
  62. return 0;
  63. return -EIO;
  64. }
  65. static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm)
  66. {
  67. struct bq32k_regs regs;
  68. int error;
  69. error = bq32k_read(dev, &regs, 0, sizeof(regs));
  70. if (error)
  71. return error;
  72. tm->tm_sec = bcd2bin(regs.seconds & BQ32K_SECONDS_MASK);
  73. tm->tm_min = bcd2bin(regs.minutes & BQ32K_SECONDS_MASK);
  74. tm->tm_hour = bcd2bin(regs.cent_hours & BQ32K_HOURS_MASK);
  75. tm->tm_mday = bcd2bin(regs.date);
  76. tm->tm_wday = bcd2bin(regs.day) - 1;
  77. tm->tm_mon = bcd2bin(regs.month) - 1;
  78. tm->tm_year = bcd2bin(regs.years) +
  79. ((regs.cent_hours & BQ32K_CENT) ? 100 : 0);
  80. return rtc_valid_tm(tm);
  81. }
  82. static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm)
  83. {
  84. struct bq32k_regs regs;
  85. regs.seconds = bin2bcd(tm->tm_sec);
  86. regs.minutes = bin2bcd(tm->tm_min);
  87. regs.cent_hours = bin2bcd(tm->tm_hour) | BQ32K_CENT_EN;
  88. regs.day = bin2bcd(tm->tm_wday + 1);
  89. regs.date = bin2bcd(tm->tm_mday);
  90. regs.month = bin2bcd(tm->tm_mon + 1);
  91. if (tm->tm_year >= 100) {
  92. regs.cent_hours |= BQ32K_CENT;
  93. regs.years = bin2bcd(tm->tm_year - 100);
  94. } else
  95. regs.years = bin2bcd(tm->tm_year);
  96. return bq32k_write(dev, &regs, 0, sizeof(regs));
  97. }
  98. static const struct rtc_class_ops bq32k_rtc_ops = {
  99. .read_time = bq32k_rtc_read_time,
  100. .set_time = bq32k_rtc_set_time,
  101. };
  102. static int bq32k_probe(struct i2c_client *client,
  103. const struct i2c_device_id *id)
  104. {
  105. struct device *dev = &client->dev;
  106. struct rtc_device *rtc;
  107. uint8_t reg;
  108. int error;
  109. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  110. return -ENODEV;
  111. /* Check Oscillator Stop flag */
  112. error = bq32k_read(dev, &reg, BQ32K_SECONDS, 1);
  113. if (!error && (reg & BQ32K_STOP)) {
  114. dev_warn(dev, "Oscillator was halted. Restarting...\n");
  115. reg &= ~BQ32K_STOP;
  116. error = bq32k_write(dev, &reg, BQ32K_SECONDS, 1);
  117. }
  118. if (error)
  119. return error;
  120. /* Check Oscillator Failure flag */
  121. error = bq32k_read(dev, &reg, BQ32K_MINUTES, 1);
  122. if (!error && (reg & BQ32K_OF)) {
  123. dev_warn(dev, "Oscillator Failure. Check RTC battery.\n");
  124. reg &= ~BQ32K_OF;
  125. error = bq32k_write(dev, &reg, BQ32K_MINUTES, 1);
  126. }
  127. if (error)
  128. return error;
  129. rtc = rtc_device_register(bq32k_driver.driver.name, &client->dev,
  130. &bq32k_rtc_ops, THIS_MODULE);
  131. if (IS_ERR(rtc))
  132. return PTR_ERR(rtc);
  133. i2c_set_clientdata(client, rtc);
  134. return 0;
  135. }
  136. static int __devexit bq32k_remove(struct i2c_client *client)
  137. {
  138. struct rtc_device *rtc = i2c_get_clientdata(client);
  139. rtc_device_unregister(rtc);
  140. return 0;
  141. }
  142. static const struct i2c_device_id bq32k_id[] = {
  143. { "bq32000", 0 },
  144. { }
  145. };
  146. MODULE_DEVICE_TABLE(i2c, bq32k_id);
  147. static struct i2c_driver bq32k_driver = {
  148. .driver = {
  149. .name = "bq32k",
  150. .owner = THIS_MODULE,
  151. },
  152. .probe = bq32k_probe,
  153. .remove = __devexit_p(bq32k_remove),
  154. .id_table = bq32k_id,
  155. };
  156. module_i2c_driver(bq32k_driver);
  157. MODULE_AUTHOR("Semihalf, Piotr Ziecik <kosmo@semihalf.com>");
  158. MODULE_DESCRIPTION("TI BQ32000 I2C RTC driver");
  159. MODULE_LICENSE("GPL");