rtc-bq32k.c 7.7 KB

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