rtc-bq32k.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. tm->tm_sec = bcd2bin(regs.seconds & BQ32K_SECONDS_MASK);
  80. tm->tm_min = bcd2bin(regs.minutes & BQ32K_SECONDS_MASK);
  81. tm->tm_hour = bcd2bin(regs.cent_hours & BQ32K_HOURS_MASK);
  82. tm->tm_mday = bcd2bin(regs.date);
  83. tm->tm_wday = bcd2bin(regs.day) - 1;
  84. tm->tm_mon = bcd2bin(regs.month) - 1;
  85. tm->tm_year = bcd2bin(regs.years) +
  86. ((regs.cent_hours & BQ32K_CENT) ? 100 : 0);
  87. return rtc_valid_tm(tm);
  88. }
  89. static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm)
  90. {
  91. struct bq32k_regs regs;
  92. regs.seconds = bin2bcd(tm->tm_sec);
  93. regs.minutes = bin2bcd(tm->tm_min);
  94. regs.cent_hours = bin2bcd(tm->tm_hour) | BQ32K_CENT_EN;
  95. regs.day = bin2bcd(tm->tm_wday + 1);
  96. regs.date = bin2bcd(tm->tm_mday);
  97. regs.month = bin2bcd(tm->tm_mon + 1);
  98. if (tm->tm_year >= 100) {
  99. regs.cent_hours |= BQ32K_CENT;
  100. regs.years = bin2bcd(tm->tm_year - 100);
  101. } else
  102. regs.years = bin2bcd(tm->tm_year);
  103. return bq32k_write(dev, &regs, 0, sizeof(regs));
  104. }
  105. static const struct rtc_class_ops bq32k_rtc_ops = {
  106. .read_time = bq32k_rtc_read_time,
  107. .set_time = bq32k_rtc_set_time,
  108. };
  109. static int trickle_charger_of_init(struct device *dev, struct device_node *node)
  110. {
  111. unsigned char reg;
  112. int error;
  113. u32 ohms = 0;
  114. if (of_property_read_u32(node, "trickle-resistor-ohms" , &ohms))
  115. return 0;
  116. switch (ohms) {
  117. case 180+940:
  118. /*
  119. * TCHE[3:0] == 0x05, TCH2 == 1, TCFE == 0 (charging
  120. * over diode and 940ohm resistor)
  121. */
  122. if (of_property_read_bool(node, "trickle-diode-disable")) {
  123. dev_err(dev, "diode and resistor mismatch\n");
  124. return -EINVAL;
  125. }
  126. reg = 0x05;
  127. break;
  128. case 180+20000:
  129. /* diode disabled */
  130. if (!of_property_read_bool(node, "trickle-diode-disable")) {
  131. dev_err(dev, "bq32k: diode and resistor mismatch\n");
  132. return -EINVAL;
  133. }
  134. reg = 0x45;
  135. break;
  136. default:
  137. dev_err(dev, "invalid resistor value (%d)\n", ohms);
  138. return -EINVAL;
  139. }
  140. error = bq32k_write(dev, &reg, BQ32K_CFG2, 1);
  141. if (error)
  142. return error;
  143. reg = 0x20;
  144. error = bq32k_write(dev, &reg, BQ32K_TCH2, 1);
  145. if (error)
  146. return error;
  147. dev_info(dev, "Enabled trickle RTC battery charge.\n");
  148. return 0;
  149. }
  150. static int bq32k_probe(struct i2c_client *client,
  151. const struct i2c_device_id *id)
  152. {
  153. struct device *dev = &client->dev;
  154. struct rtc_device *rtc;
  155. uint8_t reg;
  156. int error;
  157. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  158. return -ENODEV;
  159. /* Check Oscillator Stop flag */
  160. error = bq32k_read(dev, &reg, BQ32K_SECONDS, 1);
  161. if (!error && (reg & BQ32K_STOP)) {
  162. dev_warn(dev, "Oscillator was halted. Restarting...\n");
  163. reg &= ~BQ32K_STOP;
  164. error = bq32k_write(dev, &reg, BQ32K_SECONDS, 1);
  165. }
  166. if (error)
  167. return error;
  168. /* Check Oscillator Failure flag */
  169. error = bq32k_read(dev, &reg, BQ32K_MINUTES, 1);
  170. if (!error && (reg & BQ32K_OF)) {
  171. dev_warn(dev, "Oscillator Failure. Check RTC battery.\n");
  172. reg &= ~BQ32K_OF;
  173. error = bq32k_write(dev, &reg, BQ32K_MINUTES, 1);
  174. }
  175. if (error)
  176. return error;
  177. if (client->dev.of_node)
  178. trickle_charger_of_init(dev, client->dev.of_node);
  179. rtc = devm_rtc_device_register(&client->dev, bq32k_driver.driver.name,
  180. &bq32k_rtc_ops, THIS_MODULE);
  181. if (IS_ERR(rtc))
  182. return PTR_ERR(rtc);
  183. i2c_set_clientdata(client, rtc);
  184. return 0;
  185. }
  186. static const struct i2c_device_id bq32k_id[] = {
  187. { "bq32000", 0 },
  188. { }
  189. };
  190. MODULE_DEVICE_TABLE(i2c, bq32k_id);
  191. static struct i2c_driver bq32k_driver = {
  192. .driver = {
  193. .name = "bq32k",
  194. },
  195. .probe = bq32k_probe,
  196. .id_table = bq32k_id,
  197. };
  198. module_i2c_driver(bq32k_driver);
  199. MODULE_AUTHOR("Semihalf, Piotr Ziecik <kosmo@semihalf.com>");
  200. MODULE_DESCRIPTION("TI BQ32000 I2C RTC driver");
  201. MODULE_LICENSE("GPL");