rtc-bq32k.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for TI BQ32000 RTC.
  4. *
  5. * Copyright (C) 2009 Semihalf.
  6. * Copyright (C) 2014 Pavel Machek <[email protected]>
  7. *
  8. * You can get hardware description at
  9. * https://www.ti.com/lit/ds/symlink/bq32000.pdf
  10. */
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/rtc.h>
  14. #include <linux/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/bcd.h>
  17. #define BQ32K_SECONDS 0x00 /* Seconds register address */
  18. #define BQ32K_SECONDS_MASK 0x7F /* Mask over seconds value */
  19. #define BQ32K_STOP 0x80 /* Oscillator Stop flat */
  20. #define BQ32K_MINUTES 0x01 /* Minutes register address */
  21. #define BQ32K_MINUTES_MASK 0x7F /* Mask over minutes value */
  22. #define BQ32K_OF 0x80 /* Oscillator Failure flag */
  23. #define BQ32K_HOURS_MASK 0x3F /* Mask over hours value */
  24. #define BQ32K_CENT 0x40 /* Century flag */
  25. #define BQ32K_CENT_EN 0x80 /* Century flag enable bit */
  26. #define BQ32K_CALIBRATION 0x07 /* CAL_CFG1, calibration and control */
  27. #define BQ32K_TCH2 0x08 /* Trickle charge enable */
  28. #define BQ32K_CFG2 0x09 /* Trickle charger control */
  29. #define BQ32K_TCFE BIT(6) /* Trickle charge FET bypass */
  30. #define MAX_LEN 10 /* Maximum number of consecutive
  31. * register for this particular RTC.
  32. */
  33. struct bq32k_regs {
  34. uint8_t seconds;
  35. uint8_t minutes;
  36. uint8_t cent_hours;
  37. uint8_t day;
  38. uint8_t date;
  39. uint8_t month;
  40. uint8_t years;
  41. };
  42. static struct i2c_driver bq32k_driver;
  43. static int bq32k_read(struct device *dev, void *data, uint8_t off, uint8_t len)
  44. {
  45. struct i2c_client *client = to_i2c_client(dev);
  46. struct i2c_msg msgs[] = {
  47. {
  48. .addr = client->addr,
  49. .flags = 0,
  50. .len = 1,
  51. .buf = &off,
  52. }, {
  53. .addr = client->addr,
  54. .flags = I2C_M_RD,
  55. .len = len,
  56. .buf = data,
  57. }
  58. };
  59. if (i2c_transfer(client->adapter, msgs, 2) == 2)
  60. return 0;
  61. return -EIO;
  62. }
  63. static int bq32k_write(struct device *dev, void *data, uint8_t off, uint8_t len)
  64. {
  65. struct i2c_client *client = to_i2c_client(dev);
  66. uint8_t buffer[MAX_LEN + 1];
  67. buffer[0] = off;
  68. memcpy(&buffer[1], data, len);
  69. if (i2c_master_send(client, buffer, len + 1) == len + 1)
  70. return 0;
  71. return -EIO;
  72. }
  73. static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm)
  74. {
  75. struct bq32k_regs regs;
  76. int error;
  77. error = bq32k_read(dev, &regs, 0, sizeof(regs));
  78. if (error)
  79. return error;
  80. /*
  81. * In case of oscillator failure, the register contents should be
  82. * considered invalid. The flag is cleared the next time the RTC is set.
  83. */
  84. if (regs.minutes & BQ32K_OF)
  85. return -EINVAL;
  86. tm->tm_sec = bcd2bin(regs.seconds & BQ32K_SECONDS_MASK);
  87. tm->tm_min = bcd2bin(regs.minutes & BQ32K_MINUTES_MASK);
  88. tm->tm_hour = bcd2bin(regs.cent_hours & BQ32K_HOURS_MASK);
  89. tm->tm_mday = bcd2bin(regs.date);
  90. tm->tm_wday = bcd2bin(regs.day) - 1;
  91. tm->tm_mon = bcd2bin(regs.month) - 1;
  92. tm->tm_year = bcd2bin(regs.years) +
  93. ((regs.cent_hours & BQ32K_CENT) ? 100 : 0);
  94. return 0;
  95. }
  96. static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm)
  97. {
  98. struct bq32k_regs regs;
  99. regs.seconds = bin2bcd(tm->tm_sec);
  100. regs.minutes = bin2bcd(tm->tm_min);
  101. regs.cent_hours = bin2bcd(tm->tm_hour) | BQ32K_CENT_EN;
  102. regs.day = bin2bcd(tm->tm_wday + 1);
  103. regs.date = bin2bcd(tm->tm_mday);
  104. regs.month = bin2bcd(tm->tm_mon + 1);
  105. if (tm->tm_year >= 100) {
  106. regs.cent_hours |= BQ32K_CENT;
  107. regs.years = bin2bcd(tm->tm_year - 100);
  108. } else
  109. regs.years = bin2bcd(tm->tm_year);
  110. return bq32k_write(dev, &regs, 0, sizeof(regs));
  111. }
  112. static const struct rtc_class_ops bq32k_rtc_ops = {
  113. .read_time = bq32k_rtc_read_time,
  114. .set_time = bq32k_rtc_set_time,
  115. };
  116. static int trickle_charger_of_init(struct device *dev, struct device_node *node)
  117. {
  118. unsigned char reg;
  119. int error;
  120. u32 ohms = 0;
  121. if (of_property_read_u32(node, "trickle-resistor-ohms" , &ohms))
  122. return 0;
  123. switch (ohms) {
  124. case 180+940:
  125. /*
  126. * TCHE[3:0] == 0x05, TCH2 == 1, TCFE == 0 (charging
  127. * over diode and 940ohm resistor)
  128. */
  129. if (of_property_read_bool(node, "trickle-diode-disable")) {
  130. dev_err(dev, "diode and resistor mismatch\n");
  131. return -EINVAL;
  132. }
  133. reg = 0x05;
  134. break;
  135. case 180+20000:
  136. /* diode disabled */
  137. if (!of_property_read_bool(node, "trickle-diode-disable")) {
  138. dev_err(dev, "bq32k: diode and resistor mismatch\n");
  139. return -EINVAL;
  140. }
  141. reg = 0x45;
  142. break;
  143. default:
  144. dev_err(dev, "invalid resistor value (%d)\n", ohms);
  145. return -EINVAL;
  146. }
  147. error = bq32k_write(dev, &reg, BQ32K_CFG2, 1);
  148. if (error)
  149. return error;
  150. reg = 0x20;
  151. error = bq32k_write(dev, &reg, BQ32K_TCH2, 1);
  152. if (error)
  153. return error;
  154. dev_info(dev, "Enabled trickle RTC battery charge.\n");
  155. return 0;
  156. }
  157. static ssize_t bq32k_sysfs_show_tricklecharge_bypass(struct device *dev,
  158. struct device_attribute *attr,
  159. char *buf)
  160. {
  161. int reg, error;
  162. error = bq32k_read(dev, &reg, BQ32K_CFG2, 1);
  163. if (error)
  164. return error;
  165. return sprintf(buf, "%d\n", (reg & BQ32K_TCFE) ? 1 : 0);
  166. }
  167. static ssize_t bq32k_sysfs_store_tricklecharge_bypass(struct device *dev,
  168. struct device_attribute *attr,
  169. const char *buf, size_t count)
  170. {
  171. int reg, enable, error;
  172. if (kstrtoint(buf, 0, &enable))
  173. return -EINVAL;
  174. error = bq32k_read(dev, &reg, BQ32K_CFG2, 1);
  175. if (error)
  176. return error;
  177. if (enable) {
  178. reg |= BQ32K_TCFE;
  179. error = bq32k_write(dev, &reg, BQ32K_CFG2, 1);
  180. if (error)
  181. return error;
  182. dev_info(dev, "Enabled trickle charge FET bypass.\n");
  183. } else {
  184. reg &= ~BQ32K_TCFE;
  185. error = bq32k_write(dev, &reg, BQ32K_CFG2, 1);
  186. if (error)
  187. return error;
  188. dev_info(dev, "Disabled trickle charge FET bypass.\n");
  189. }
  190. return count;
  191. }
  192. static DEVICE_ATTR(trickle_charge_bypass, 0644,
  193. bq32k_sysfs_show_tricklecharge_bypass,
  194. bq32k_sysfs_store_tricklecharge_bypass);
  195. static int bq32k_sysfs_register(struct device *dev)
  196. {
  197. return device_create_file(dev, &dev_attr_trickle_charge_bypass);
  198. }
  199. static void bq32k_sysfs_unregister(struct device *dev)
  200. {
  201. device_remove_file(dev, &dev_attr_trickle_charge_bypass);
  202. }
  203. static int bq32k_probe(struct i2c_client *client)
  204. {
  205. struct device *dev = &client->dev;
  206. struct rtc_device *rtc;
  207. uint8_t reg;
  208. int error;
  209. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  210. return -ENODEV;
  211. /* Check Oscillator Stop flag */
  212. error = bq32k_read(dev, &reg, BQ32K_SECONDS, 1);
  213. if (!error && (reg & BQ32K_STOP)) {
  214. dev_warn(dev, "Oscillator was halted. Restarting...\n");
  215. reg &= ~BQ32K_STOP;
  216. error = bq32k_write(dev, &reg, BQ32K_SECONDS, 1);
  217. }
  218. if (error)
  219. return error;
  220. /* Check Oscillator Failure flag */
  221. error = bq32k_read(dev, &reg, BQ32K_MINUTES, 1);
  222. if (error)
  223. return error;
  224. if (reg & BQ32K_OF)
  225. dev_warn(dev, "Oscillator Failure. Check RTC battery.\n");
  226. if (client->dev.of_node)
  227. trickle_charger_of_init(dev, client->dev.of_node);
  228. rtc = devm_rtc_device_register(&client->dev, bq32k_driver.driver.name,
  229. &bq32k_rtc_ops, THIS_MODULE);
  230. if (IS_ERR(rtc))
  231. return PTR_ERR(rtc);
  232. error = bq32k_sysfs_register(&client->dev);
  233. if (error) {
  234. dev_err(&client->dev,
  235. "Unable to create sysfs entries for rtc bq32000\n");
  236. return error;
  237. }
  238. i2c_set_clientdata(client, rtc);
  239. return 0;
  240. }
  241. static void bq32k_remove(struct i2c_client *client)
  242. {
  243. bq32k_sysfs_unregister(&client->dev);
  244. }
  245. static const struct i2c_device_id bq32k_id[] = {
  246. { "bq32000", 0 },
  247. { }
  248. };
  249. MODULE_DEVICE_TABLE(i2c, bq32k_id);
  250. static const __maybe_unused struct of_device_id bq32k_of_match[] = {
  251. { .compatible = "ti,bq32000" },
  252. { }
  253. };
  254. MODULE_DEVICE_TABLE(of, bq32k_of_match);
  255. static struct i2c_driver bq32k_driver = {
  256. .driver = {
  257. .name = "bq32k",
  258. .of_match_table = of_match_ptr(bq32k_of_match),
  259. },
  260. .probe_new = bq32k_probe,
  261. .remove = bq32k_remove,
  262. .id_table = bq32k_id,
  263. };
  264. module_i2c_driver(bq32k_driver);
  265. MODULE_AUTHOR("Semihalf, Piotr Ziecik <[email protected]>");
  266. MODULE_DESCRIPTION("TI BQ32000 I2C RTC driver");
  267. MODULE_LICENSE("GPL");