rtc-abx80x.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * A driver for the I2C members of the Abracon AB x8xx RTC family,
  3. * and compatible: AB 1805 and AB 0805
  4. *
  5. * Copyright 2014-2015 Macq S.A.
  6. *
  7. * Author: Philippe De Muyter <phdm@macqel.be>
  8. * Author: Alexandre Belloni <alexandre.belloni@free-electrons.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/bcd.h>
  16. #include <linux/i2c.h>
  17. #include <linux/module.h>
  18. #include <linux/rtc.h>
  19. #define ABX8XX_REG_HTH 0x00
  20. #define ABX8XX_REG_SC 0x01
  21. #define ABX8XX_REG_MN 0x02
  22. #define ABX8XX_REG_HR 0x03
  23. #define ABX8XX_REG_DA 0x04
  24. #define ABX8XX_REG_MO 0x05
  25. #define ABX8XX_REG_YR 0x06
  26. #define ABX8XX_REG_WD 0x07
  27. #define ABX8XX_REG_CTRL1 0x10
  28. #define ABX8XX_CTRL_WRITE BIT(1)
  29. #define ABX8XX_CTRL_12_24 BIT(6)
  30. #define ABX8XX_REG_CFG_KEY 0x1f
  31. #define ABX8XX_CFG_KEY_MISC 0x9d
  32. #define ABX8XX_REG_ID0 0x28
  33. #define ABX8XX_REG_TRICKLE 0x20
  34. #define ABX8XX_TRICKLE_CHARGE_ENABLE 0xa0
  35. #define ABX8XX_TRICKLE_STANDARD_DIODE 0x8
  36. #define ABX8XX_TRICKLE_SCHOTTKY_DIODE 0x4
  37. static u8 trickle_resistors[] = {0, 3, 6, 11};
  38. enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
  39. AB1801, AB1803, AB1804, AB1805, ABX80X};
  40. struct abx80x_cap {
  41. u16 pn;
  42. bool has_tc;
  43. };
  44. static struct abx80x_cap abx80x_caps[] = {
  45. [AB0801] = {.pn = 0x0801},
  46. [AB0803] = {.pn = 0x0803},
  47. [AB0804] = {.pn = 0x0804, .has_tc = true},
  48. [AB0805] = {.pn = 0x0805, .has_tc = true},
  49. [AB1801] = {.pn = 0x1801},
  50. [AB1803] = {.pn = 0x1803},
  51. [AB1804] = {.pn = 0x1804, .has_tc = true},
  52. [AB1805] = {.pn = 0x1805, .has_tc = true},
  53. [ABX80X] = {.pn = 0}
  54. };
  55. static struct i2c_driver abx80x_driver;
  56. static int abx80x_enable_trickle_charger(struct i2c_client *client,
  57. u8 trickle_cfg)
  58. {
  59. int err;
  60. /*
  61. * Write the configuration key register to enable access to the Trickle
  62. * register
  63. */
  64. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
  65. ABX8XX_CFG_KEY_MISC);
  66. if (err < 0) {
  67. dev_err(&client->dev, "Unable to write configuration key\n");
  68. return -EIO;
  69. }
  70. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE,
  71. ABX8XX_TRICKLE_CHARGE_ENABLE |
  72. trickle_cfg);
  73. if (err < 0) {
  74. dev_err(&client->dev, "Unable to write trickle register\n");
  75. return -EIO;
  76. }
  77. return 0;
  78. }
  79. static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
  80. {
  81. struct i2c_client *client = to_i2c_client(dev);
  82. unsigned char buf[8];
  83. int err;
  84. err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH,
  85. sizeof(buf), buf);
  86. if (err < 0) {
  87. dev_err(&client->dev, "Unable to read date\n");
  88. return -EIO;
  89. }
  90. tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F);
  91. tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F);
  92. tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F);
  93. tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7;
  94. tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F);
  95. tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1;
  96. tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100;
  97. err = rtc_valid_tm(tm);
  98. if (err < 0)
  99. dev_err(&client->dev, "retrieved date/time is not valid.\n");
  100. return err;
  101. }
  102. static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  103. {
  104. struct i2c_client *client = to_i2c_client(dev);
  105. unsigned char buf[8];
  106. int err;
  107. if (tm->tm_year < 100)
  108. return -EINVAL;
  109. buf[ABX8XX_REG_HTH] = 0;
  110. buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
  111. buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
  112. buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
  113. buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
  114. buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
  115. buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
  116. buf[ABX8XX_REG_WD] = tm->tm_wday;
  117. err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
  118. sizeof(buf), buf);
  119. if (err < 0) {
  120. dev_err(&client->dev, "Unable to write to date registers\n");
  121. return -EIO;
  122. }
  123. return 0;
  124. }
  125. static const struct rtc_class_ops abx80x_rtc_ops = {
  126. .read_time = abx80x_rtc_read_time,
  127. .set_time = abx80x_rtc_set_time,
  128. };
  129. static int abx80x_dt_trickle_cfg(struct device_node *np)
  130. {
  131. const char *diode;
  132. int trickle_cfg = 0;
  133. int i, ret;
  134. u32 tmp;
  135. ret = of_property_read_string(np, "abracon,tc-diode", &diode);
  136. if (ret)
  137. return ret;
  138. if (!strcmp(diode, "standard"))
  139. trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
  140. else if (!strcmp(diode, "schottky"))
  141. trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
  142. else
  143. return -EINVAL;
  144. ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
  145. if (ret)
  146. return ret;
  147. for (i = 0; i < sizeof(trickle_resistors); i++)
  148. if (trickle_resistors[i] == tmp)
  149. break;
  150. if (i == sizeof(trickle_resistors))
  151. return -EINVAL;
  152. return (trickle_cfg | i);
  153. }
  154. static int abx80x_probe(struct i2c_client *client,
  155. const struct i2c_device_id *id)
  156. {
  157. struct device_node *np = client->dev.of_node;
  158. struct rtc_device *rtc;
  159. int i, data, err, trickle_cfg = -EINVAL;
  160. char buf[7];
  161. unsigned int part = id->driver_data;
  162. unsigned int partnumber;
  163. unsigned int majrev, minrev;
  164. unsigned int lot;
  165. unsigned int wafer;
  166. unsigned int uid;
  167. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  168. return -ENODEV;
  169. err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
  170. sizeof(buf), buf);
  171. if (err < 0) {
  172. dev_err(&client->dev, "Unable to read partnumber\n");
  173. return -EIO;
  174. }
  175. partnumber = (buf[0] << 8) | buf[1];
  176. majrev = buf[2] >> 3;
  177. minrev = buf[2] & 0x7;
  178. lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
  179. uid = ((buf[4] & 0x7f) << 8) | buf[5];
  180. wafer = (buf[6] & 0x7c) >> 2;
  181. dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
  182. partnumber, majrev, minrev, lot, wafer, uid);
  183. data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
  184. if (data < 0) {
  185. dev_err(&client->dev, "Unable to read control register\n");
  186. return -EIO;
  187. }
  188. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
  189. ((data & ~ABX8XX_CTRL_12_24) |
  190. ABX8XX_CTRL_WRITE));
  191. if (err < 0) {
  192. dev_err(&client->dev, "Unable to write control register\n");
  193. return -EIO;
  194. }
  195. /* part autodetection */
  196. if (part == ABX80X) {
  197. for (i = 0; abx80x_caps[i].pn; i++)
  198. if (partnumber == abx80x_caps[i].pn)
  199. break;
  200. if (abx80x_caps[i].pn == 0) {
  201. dev_err(&client->dev, "Unknown part: %04x\n",
  202. partnumber);
  203. return -EINVAL;
  204. }
  205. part = i;
  206. }
  207. if (partnumber != abx80x_caps[part].pn) {
  208. dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
  209. partnumber, abx80x_caps[part].pn);
  210. return -EINVAL;
  211. }
  212. if (np && abx80x_caps[part].has_tc)
  213. trickle_cfg = abx80x_dt_trickle_cfg(np);
  214. if (trickle_cfg > 0) {
  215. dev_info(&client->dev, "Enabling trickle charger: %02x\n",
  216. trickle_cfg);
  217. abx80x_enable_trickle_charger(client, trickle_cfg);
  218. }
  219. rtc = devm_rtc_device_register(&client->dev, abx80x_driver.driver.name,
  220. &abx80x_rtc_ops, THIS_MODULE);
  221. if (IS_ERR(rtc))
  222. return PTR_ERR(rtc);
  223. i2c_set_clientdata(client, rtc);
  224. return 0;
  225. }
  226. static int abx80x_remove(struct i2c_client *client)
  227. {
  228. return 0;
  229. }
  230. static const struct i2c_device_id abx80x_id[] = {
  231. { "abx80x", ABX80X },
  232. { "ab0801", AB0801 },
  233. { "ab0803", AB0803 },
  234. { "ab0804", AB0804 },
  235. { "ab0805", AB0805 },
  236. { "ab1801", AB1801 },
  237. { "ab1803", AB1803 },
  238. { "ab1804", AB1804 },
  239. { "ab1805", AB1805 },
  240. { }
  241. };
  242. MODULE_DEVICE_TABLE(i2c, abx80x_id);
  243. static struct i2c_driver abx80x_driver = {
  244. .driver = {
  245. .name = "rtc-abx80x",
  246. },
  247. .probe = abx80x_probe,
  248. .remove = abx80x_remove,
  249. .id_table = abx80x_id,
  250. };
  251. module_i2c_driver(abx80x_driver);
  252. MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
  253. MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
  254. MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
  255. MODULE_LICENSE("GPL v2");