rtc-bq32k.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Driver for TI BQ32000 RTC.
  3. *
  4. * Copyright (C) 2009 Semihalf.
  5. * Copyright (C) 2014 Pavel Machek <pavel@denx.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * You can get hardware description at
  12. * http://www.ti.com/lit/ds/symlink/bq32000.pdf
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/rtc.h>
  17. #include <linux/init.h>
  18. #include <linux/errno.h>
  19. #include <linux/bcd.h>
  20. #define BQ32K_SECONDS 0x00 /* Seconds register address */
  21. #define BQ32K_SECONDS_MASK 0x7F /* Mask over seconds value */
  22. #define BQ32K_STOP 0x80 /* Oscillator Stop flat */
  23. #define BQ32K_MINUTES 0x01 /* Minutes register address */
  24. #define BQ32K_MINUTES_MASK 0x7F /* Mask over minutes value */
  25. #define BQ32K_OF 0x80 /* Oscillator Failure flag */
  26. #define BQ32K_HOURS_MASK 0x3F /* Mask over hours value */
  27. #define BQ32K_CENT 0x40 /* Century flag */
  28. #define BQ32K_CENT_EN 0x80 /* Century flag enable bit */
  29. #define BQ32K_CALIBRATION 0x07 /* CAL_CFG1, calibration and control */
  30. #define BQ32K_TCH2 0x08 /* Trickle charge enable */
  31. #define BQ32K_CFG2 0x09 /* Trickle charger control */
  32. struct bq32k_regs {
  33. uint8_t seconds;
  34. uint8_t minutes;
  35. uint8_t cent_hours;
  36. uint8_t day;
  37. uint8_t date;
  38. uint8_t month;
  39. uint8_t years;
  40. };
  41. static struct i2c_driver bq32k_driver;
  42. static int bq32k_read(struct device *dev, void *data, uint8_t off, uint8_t len)
  43. {
  44. struct i2c_client *client = to_i2c_client(dev);
  45. struct i2c_msg msgs[] = {
  46. {
  47. .addr = client->addr,
  48. .flags = 0,
  49. .len = 1,
  50. .buf = &off,
  51. }, {
  52. .addr = client->addr,
  53. .flags = I2C_M_RD,
  54. .len = len,
  55. .buf = data,
  56. }
  57. };
  58. if (i2c_transfer(client->adapter, msgs, 2) == 2)
  59. return 0;
  60. return -EIO;
  61. }
  62. static int bq32k_write(struct device *dev, void *data, uint8_t off, uint8_t len)
  63. {
  64. struct i2c_client *client = to_i2c_client(dev);
  65. uint8_t buffer[len + 1];
  66. buffer[0] = off;
  67. memcpy(&buffer[1], data, len);
  68. if (i2c_master_send(client, buffer, len + 1) == len + 1)
  69. return 0;
  70. return -EIO;
  71. }
  72. static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm)
  73. {
  74. struct bq32k_regs regs;
  75. int error;
  76. error = bq32k_read(dev, &regs, 0, sizeof(regs));
  77. if (error)
  78. return error;
  79. /*
  80. * In case of oscillator failure, the register contents should be
  81. * considered invalid. The flag is cleared the next time the RTC is set.
  82. */
  83. if (regs.minutes & BQ32K_OF)
  84. return -EINVAL;
  85. tm->tm_sec = bcd2bin(regs.seconds & BQ32K_SECONDS_MASK);
  86. tm->tm_min = bcd2bin(regs.minutes & BQ32K_MINUTES_MASK);
  87. tm->tm_hour = bcd2bin(regs.cent_hours & BQ32K_HOURS_MASK);
  88. tm->tm_mday = bcd2bin(regs.date);
  89. tm->tm_wday = bcd2bin(regs.day) - 1;
  90. tm->tm_mon = bcd2bin(regs.month) - 1;
  91. tm->tm_year = bcd2bin(regs.years) +
  92. ((regs.cent_hours & BQ32K_CENT) ? 100 : 0);
  93. return rtc_valid_tm(tm);
  94. }
  95. static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm)
  96. {
  97. struct bq32k_regs regs;
  98. regs.seconds = bin2bcd(tm->tm_sec);
  99. regs.minutes = bin2bcd(tm->tm_min);
  100. regs.cent_hours = bin2bcd(tm->tm_hour) | BQ32K_CENT_EN;
  101. regs.day = bin2bcd(tm->tm_wday + 1);
  102. regs.date = bin2bcd(tm->tm_mday);
  103. regs.month = bin2bcd(tm->tm_mon + 1);
  104. if (tm->tm_year >= 100) {
  105. regs.cent_hours |= BQ32K_CENT;
  106. regs.years = bin2bcd(tm->tm_year - 100);
  107. } else
  108. regs.years = bin2bcd(tm->tm_year);
  109. return bq32k_write(dev, &regs, 0, sizeof(regs));
  110. }
  111. static const struct rtc_class_ops bq32k_rtc_ops = {
  112. .read_time = bq32k_rtc_read_time,
  113. .set_time = bq32k_rtc_set_time,
  114. };
  115. static int trickle_charger_of_init(struct device *dev, struct device_node *node)
  116. {
  117. unsigned char reg;
  118. int error;
  119. u32 ohms = 0;
  120. if (of_property_read_u32(node, "trickle-resistor-ohms" , &ohms))
  121. return 0;
  122. switch (ohms) {
  123. case 180+940:
  124. /*
  125. * TCHE[3:0] == 0x05, TCH2 == 1, TCFE == 0 (charging
  126. * over diode and 940ohm resistor)
  127. */
  128. if (of_property_read_bool(node, "trickle-diode-disable")) {
  129. dev_err(dev, "diode and resistor mismatch\n");
  130. return -EINVAL;
  131. }
  132. reg = 0x05;
  133. break;
  134. case 180+20000:
  135. /* diode disabled */
  136. if (!of_property_read_bool(node, "trickle-diode-disable")) {
  137. dev_err(dev, "bq32k: diode and resistor mismatch\n");
  138. return -EINVAL;
  139. }
  140. reg = 0x45;
  141. break;
  142. default:
  143. dev_err(dev, "invalid resistor value (%d)\n", ohms);
  144. return -EINVAL;
  145. }
  146. error = bq32k_write(dev, &reg, BQ32K_CFG2, 1);
  147. if (error)
  148. return error;
  149. reg = 0x20;
  150. error = bq32k_write(dev, &reg, BQ32K_TCH2, 1);
  151. if (error)
  152. return error;
  153. dev_info(dev, "Enabled trickle RTC battery charge.\n");
  154. return 0;
  155. }
  156. static int bq32k_probe(struct i2c_client *client,
  157. const struct i2c_device_id *id)
  158. {
  159. struct device *dev = &client->dev;
  160. struct rtc_device *rtc;
  161. uint8_t reg;
  162. int error;
  163. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  164. return -ENODEV;
  165. /* Check Oscillator Stop flag */
  166. error = bq32k_read(dev, &reg, BQ32K_SECONDS, 1);
  167. if (!error && (reg & BQ32K_STOP)) {
  168. dev_warn(dev, "Oscillator was halted. Restarting...\n");
  169. reg &= ~BQ32K_STOP;
  170. error = bq32k_write(dev, &reg, BQ32K_SECONDS, 1);
  171. }
  172. if (error)
  173. return error;
  174. /* Check Oscillator Failure flag */
  175. error = bq32k_read(dev, &reg, BQ32K_MINUTES, 1);
  176. if (error)
  177. return error;
  178. if (reg & BQ32K_OF)
  179. dev_warn(dev, "Oscillator Failure. Check RTC battery.\n");
  180. if (client->dev.of_node)
  181. trickle_charger_of_init(dev, client->dev.of_node);
  182. rtc = devm_rtc_device_register(&client->dev, bq32k_driver.driver.name,
  183. &bq32k_rtc_ops, THIS_MODULE);
  184. if (IS_ERR(rtc))
  185. return PTR_ERR(rtc);
  186. i2c_set_clientdata(client, rtc);
  187. return 0;
  188. }
  189. static const struct i2c_device_id bq32k_id[] = {
  190. { "bq32000", 0 },
  191. { }
  192. };
  193. MODULE_DEVICE_TABLE(i2c, bq32k_id);
  194. static struct i2c_driver bq32k_driver = {
  195. .driver = {
  196. .name = "bq32k",
  197. },
  198. .probe = bq32k_probe,
  199. .id_table = bq32k_id,
  200. };
  201. module_i2c_driver(bq32k_driver);
  202. MODULE_AUTHOR("Semihalf, Piotr Ziecik <kosmo@semihalf.com>");
  203. MODULE_DESCRIPTION("TI BQ32000 I2C RTC driver");
  204. MODULE_LICENSE("GPL");