rtc-abx80x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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_AHTH 0x08
  28. #define ABX8XX_REG_ASC 0x09
  29. #define ABX8XX_REG_AMN 0x0a
  30. #define ABX8XX_REG_AHR 0x0b
  31. #define ABX8XX_REG_ADA 0x0c
  32. #define ABX8XX_REG_AMO 0x0d
  33. #define ABX8XX_REG_AWD 0x0e
  34. #define ABX8XX_REG_STATUS 0x0f
  35. #define ABX8XX_STATUS_AF BIT(2)
  36. #define ABX8XX_REG_CTRL1 0x10
  37. #define ABX8XX_CTRL_WRITE BIT(0)
  38. #define ABX8XX_CTRL_ARST BIT(2)
  39. #define ABX8XX_CTRL_12_24 BIT(6)
  40. #define ABX8XX_REG_IRQ 0x12
  41. #define ABX8XX_IRQ_AIE BIT(2)
  42. #define ABX8XX_IRQ_IM_1_4 (0x3 << 5)
  43. #define ABX8XX_REG_CD_TIMER_CTL 0x18
  44. #define ABX8XX_REG_CFG_KEY 0x1f
  45. #define ABX8XX_CFG_KEY_MISC 0x9d
  46. #define ABX8XX_REG_ID0 0x28
  47. #define ABX8XX_REG_TRICKLE 0x20
  48. #define ABX8XX_TRICKLE_CHARGE_ENABLE 0xa0
  49. #define ABX8XX_TRICKLE_STANDARD_DIODE 0x8
  50. #define ABX8XX_TRICKLE_SCHOTTKY_DIODE 0x4
  51. static u8 trickle_resistors[] = {0, 3, 6, 11};
  52. enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
  53. AB1801, AB1803, AB1804, AB1805, ABX80X};
  54. struct abx80x_cap {
  55. u16 pn;
  56. bool has_tc;
  57. };
  58. static struct abx80x_cap abx80x_caps[] = {
  59. [AB0801] = {.pn = 0x0801},
  60. [AB0803] = {.pn = 0x0803},
  61. [AB0804] = {.pn = 0x0804, .has_tc = true},
  62. [AB0805] = {.pn = 0x0805, .has_tc = true},
  63. [AB1801] = {.pn = 0x1801},
  64. [AB1803] = {.pn = 0x1803},
  65. [AB1804] = {.pn = 0x1804, .has_tc = true},
  66. [AB1805] = {.pn = 0x1805, .has_tc = true},
  67. [ABX80X] = {.pn = 0}
  68. };
  69. static int abx80x_enable_trickle_charger(struct i2c_client *client,
  70. u8 trickle_cfg)
  71. {
  72. int err;
  73. /*
  74. * Write the configuration key register to enable access to the Trickle
  75. * register
  76. */
  77. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
  78. ABX8XX_CFG_KEY_MISC);
  79. if (err < 0) {
  80. dev_err(&client->dev, "Unable to write configuration key\n");
  81. return -EIO;
  82. }
  83. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE,
  84. ABX8XX_TRICKLE_CHARGE_ENABLE |
  85. trickle_cfg);
  86. if (err < 0) {
  87. dev_err(&client->dev, "Unable to write trickle register\n");
  88. return -EIO;
  89. }
  90. return 0;
  91. }
  92. static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
  93. {
  94. struct i2c_client *client = to_i2c_client(dev);
  95. unsigned char buf[8];
  96. int err;
  97. err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH,
  98. sizeof(buf), buf);
  99. if (err < 0) {
  100. dev_err(&client->dev, "Unable to read date\n");
  101. return -EIO;
  102. }
  103. tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F);
  104. tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F);
  105. tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F);
  106. tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7;
  107. tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F);
  108. tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1;
  109. tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100;
  110. err = rtc_valid_tm(tm);
  111. if (err < 0)
  112. dev_err(&client->dev, "retrieved date/time is not valid.\n");
  113. return err;
  114. }
  115. static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  116. {
  117. struct i2c_client *client = to_i2c_client(dev);
  118. unsigned char buf[8];
  119. int err;
  120. if (tm->tm_year < 100)
  121. return -EINVAL;
  122. buf[ABX8XX_REG_HTH] = 0;
  123. buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
  124. buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
  125. buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
  126. buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
  127. buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
  128. buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
  129. buf[ABX8XX_REG_WD] = tm->tm_wday;
  130. err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
  131. sizeof(buf), buf);
  132. if (err < 0) {
  133. dev_err(&client->dev, "Unable to write to date registers\n");
  134. return -EIO;
  135. }
  136. return 0;
  137. }
  138. static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
  139. {
  140. struct i2c_client *client = dev_id;
  141. struct rtc_device *rtc = i2c_get_clientdata(client);
  142. int status;
  143. status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
  144. if (status < 0)
  145. return IRQ_NONE;
  146. if (status & ABX8XX_STATUS_AF)
  147. rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
  148. i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
  149. return IRQ_HANDLED;
  150. }
  151. static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  152. {
  153. struct i2c_client *client = to_i2c_client(dev);
  154. unsigned char buf[7];
  155. int irq_mask, err;
  156. if (client->irq <= 0)
  157. return -EINVAL;
  158. err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC,
  159. sizeof(buf), buf);
  160. if (err)
  161. return err;
  162. irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ);
  163. if (irq_mask < 0)
  164. return irq_mask;
  165. t->time.tm_sec = bcd2bin(buf[0] & 0x7F);
  166. t->time.tm_min = bcd2bin(buf[1] & 0x7F);
  167. t->time.tm_hour = bcd2bin(buf[2] & 0x3F);
  168. t->time.tm_mday = bcd2bin(buf[3] & 0x3F);
  169. t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1;
  170. t->time.tm_wday = buf[5] & 0x7;
  171. t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE);
  172. t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled;
  173. return err;
  174. }
  175. static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  176. {
  177. struct i2c_client *client = to_i2c_client(dev);
  178. u8 alarm[6];
  179. int err;
  180. if (client->irq <= 0)
  181. return -EINVAL;
  182. alarm[0] = 0x0;
  183. alarm[1] = bin2bcd(t->time.tm_sec);
  184. alarm[2] = bin2bcd(t->time.tm_min);
  185. alarm[3] = bin2bcd(t->time.tm_hour);
  186. alarm[4] = bin2bcd(t->time.tm_mday);
  187. alarm[5] = bin2bcd(t->time.tm_mon + 1);
  188. err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH,
  189. sizeof(alarm), alarm);
  190. if (err < 0) {
  191. dev_err(&client->dev, "Unable to write alarm registers\n");
  192. return -EIO;
  193. }
  194. if (t->enabled) {
  195. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
  196. (ABX8XX_IRQ_IM_1_4 |
  197. ABX8XX_IRQ_AIE));
  198. if (err)
  199. return err;
  200. }
  201. return 0;
  202. }
  203. static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled)
  204. {
  205. struct i2c_client *client = to_i2c_client(dev);
  206. int err;
  207. if (enabled)
  208. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
  209. (ABX8XX_IRQ_IM_1_4 |
  210. ABX8XX_IRQ_AIE));
  211. else
  212. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
  213. ABX8XX_IRQ_IM_1_4);
  214. return err;
  215. }
  216. static const struct rtc_class_ops abx80x_rtc_ops = {
  217. .read_time = abx80x_rtc_read_time,
  218. .set_time = abx80x_rtc_set_time,
  219. .read_alarm = abx80x_read_alarm,
  220. .set_alarm = abx80x_set_alarm,
  221. .alarm_irq_enable = abx80x_alarm_irq_enable,
  222. };
  223. static int abx80x_dt_trickle_cfg(struct device_node *np)
  224. {
  225. const char *diode;
  226. int trickle_cfg = 0;
  227. int i, ret;
  228. u32 tmp;
  229. ret = of_property_read_string(np, "abracon,tc-diode", &diode);
  230. if (ret)
  231. return ret;
  232. if (!strcmp(diode, "standard"))
  233. trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
  234. else if (!strcmp(diode, "schottky"))
  235. trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
  236. else
  237. return -EINVAL;
  238. ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
  239. if (ret)
  240. return ret;
  241. for (i = 0; i < sizeof(trickle_resistors); i++)
  242. if (trickle_resistors[i] == tmp)
  243. break;
  244. if (i == sizeof(trickle_resistors))
  245. return -EINVAL;
  246. return (trickle_cfg | i);
  247. }
  248. static int abx80x_probe(struct i2c_client *client,
  249. const struct i2c_device_id *id)
  250. {
  251. struct device_node *np = client->dev.of_node;
  252. struct rtc_device *rtc;
  253. int i, data, err, trickle_cfg = -EINVAL;
  254. char buf[7];
  255. unsigned int part = id->driver_data;
  256. unsigned int partnumber;
  257. unsigned int majrev, minrev;
  258. unsigned int lot;
  259. unsigned int wafer;
  260. unsigned int uid;
  261. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  262. return -ENODEV;
  263. err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
  264. sizeof(buf), buf);
  265. if (err < 0) {
  266. dev_err(&client->dev, "Unable to read partnumber\n");
  267. return -EIO;
  268. }
  269. partnumber = (buf[0] << 8) | buf[1];
  270. majrev = buf[2] >> 3;
  271. minrev = buf[2] & 0x7;
  272. lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
  273. uid = ((buf[4] & 0x7f) << 8) | buf[5];
  274. wafer = (buf[6] & 0x7c) >> 2;
  275. dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
  276. partnumber, majrev, minrev, lot, wafer, uid);
  277. data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
  278. if (data < 0) {
  279. dev_err(&client->dev, "Unable to read control register\n");
  280. return -EIO;
  281. }
  282. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
  283. ((data & ~(ABX8XX_CTRL_12_24 |
  284. ABX8XX_CTRL_ARST)) |
  285. ABX8XX_CTRL_WRITE));
  286. if (err < 0) {
  287. dev_err(&client->dev, "Unable to write control register\n");
  288. return -EIO;
  289. }
  290. /* part autodetection */
  291. if (part == ABX80X) {
  292. for (i = 0; abx80x_caps[i].pn; i++)
  293. if (partnumber == abx80x_caps[i].pn)
  294. break;
  295. if (abx80x_caps[i].pn == 0) {
  296. dev_err(&client->dev, "Unknown part: %04x\n",
  297. partnumber);
  298. return -EINVAL;
  299. }
  300. part = i;
  301. }
  302. if (partnumber != abx80x_caps[part].pn) {
  303. dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
  304. partnumber, abx80x_caps[part].pn);
  305. return -EINVAL;
  306. }
  307. if (np && abx80x_caps[part].has_tc)
  308. trickle_cfg = abx80x_dt_trickle_cfg(np);
  309. if (trickle_cfg > 0) {
  310. dev_info(&client->dev, "Enabling trickle charger: %02x\n",
  311. trickle_cfg);
  312. abx80x_enable_trickle_charger(client, trickle_cfg);
  313. }
  314. err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL,
  315. BIT(2));
  316. if (err)
  317. return err;
  318. rtc = devm_rtc_device_register(&client->dev, "abx8xx",
  319. &abx80x_rtc_ops, THIS_MODULE);
  320. if (IS_ERR(rtc))
  321. return PTR_ERR(rtc);
  322. i2c_set_clientdata(client, rtc);
  323. if (client->irq > 0) {
  324. dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
  325. err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  326. abx80x_handle_irq,
  327. IRQF_SHARED | IRQF_ONESHOT,
  328. "abx8xx",
  329. client);
  330. if (err) {
  331. dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
  332. client->irq = 0;
  333. }
  334. }
  335. return 0;
  336. }
  337. static int abx80x_remove(struct i2c_client *client)
  338. {
  339. return 0;
  340. }
  341. static const struct i2c_device_id abx80x_id[] = {
  342. { "abx80x", ABX80X },
  343. { "ab0801", AB0801 },
  344. { "ab0803", AB0803 },
  345. { "ab0804", AB0804 },
  346. { "ab0805", AB0805 },
  347. { "ab1801", AB1801 },
  348. { "ab1803", AB1803 },
  349. { "ab1804", AB1804 },
  350. { "ab1805", AB1805 },
  351. { "rv1805", AB1805 },
  352. { }
  353. };
  354. MODULE_DEVICE_TABLE(i2c, abx80x_id);
  355. static struct i2c_driver abx80x_driver = {
  356. .driver = {
  357. .name = "rtc-abx80x",
  358. },
  359. .probe = abx80x_probe,
  360. .remove = abx80x_remove,
  361. .id_table = abx80x_id,
  362. };
  363. module_i2c_driver(abx80x_driver);
  364. MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
  365. MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
  366. MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
  367. MODULE_LICENSE("GPL v2");