rtc-rv3029c2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Micro Crystal RV-3029C2 rtc class driver
  3. *
  4. * Author: Gregory Hermant <gregory.hermant@calao-systems.com>
  5. *
  6. * based on previously existing rtc class drivers
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * NOTE: Currently this driver only supports the bare minimum for read
  13. * and write the RTC and alarms. The extra features provided by this chip
  14. * (trickle charger, eeprom, T° compensation) are unavailable.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/i2c.h>
  18. #include <linux/bcd.h>
  19. #include <linux/rtc.h>
  20. /* Register map */
  21. /* control section */
  22. #define RV3029C2_ONOFF_CTRL 0x00
  23. #define RV3029C2_IRQ_CTRL 0x01
  24. #define RV3029C2_IRQ_CTRL_AIE (1 << 0)
  25. #define RV3029C2_IRQ_FLAGS 0x02
  26. #define RV3029C2_IRQ_FLAGS_AF (1 << 0)
  27. #define RV3029C2_STATUS 0x03
  28. #define RV3029C2_STATUS_VLOW1 (1 << 2)
  29. #define RV3029C2_STATUS_VLOW2 (1 << 3)
  30. #define RV3029C2_STATUS_SR (1 << 4)
  31. #define RV3029C2_STATUS_PON (1 << 5)
  32. #define RV3029C2_STATUS_EEBUSY (1 << 7)
  33. #define RV3029C2_RST_CTRL 0x04
  34. #define RV3029C2_CONTROL_SECTION_LEN 0x05
  35. /* watch section */
  36. #define RV3029C2_W_SEC 0x08
  37. #define RV3029C2_W_MINUTES 0x09
  38. #define RV3029C2_W_HOURS 0x0A
  39. #define RV3029C2_REG_HR_12_24 (1<<6) /* 24h/12h mode */
  40. #define RV3029C2_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */
  41. #define RV3029C2_W_DATE 0x0B
  42. #define RV3029C2_W_DAYS 0x0C
  43. #define RV3029C2_W_MONTHS 0x0D
  44. #define RV3029C2_W_YEARS 0x0E
  45. #define RV3029C2_WATCH_SECTION_LEN 0x07
  46. /* alarm section */
  47. #define RV3029C2_A_SC 0x10
  48. #define RV3029C2_A_MN 0x11
  49. #define RV3029C2_A_HR 0x12
  50. #define RV3029C2_A_DT 0x13
  51. #define RV3029C2_A_DW 0x14
  52. #define RV3029C2_A_MO 0x15
  53. #define RV3029C2_A_YR 0x16
  54. #define RV3029C2_ALARM_SECTION_LEN 0x07
  55. /* timer section */
  56. #define RV3029C2_TIMER_LOW 0x18
  57. #define RV3029C2_TIMER_HIGH 0x19
  58. /* temperature section */
  59. #define RV3029C2_TEMP_PAGE 0x20
  60. /* eeprom data section */
  61. #define RV3029C2_E2P_EEDATA1 0x28
  62. #define RV3029C2_E2P_EEDATA2 0x29
  63. /* eeprom control section */
  64. #define RV3029C2_CONTROL_E2P_EECTRL 0x30
  65. #define RV3029C2_TRICKLE_1K (1<<0) /* 1K resistance */
  66. #define RV3029C2_TRICKLE_5K (1<<1) /* 5K resistance */
  67. #define RV3029C2_TRICKLE_20K (1<<2) /* 20K resistance */
  68. #define RV3029C2_TRICKLE_80K (1<<3) /* 80K resistance */
  69. #define RV3029C2_CONTROL_E2P_XTALOFFSET 0x31
  70. #define RV3029C2_CONTROL_E2P_QCOEF 0x32
  71. #define RV3029C2_CONTROL_E2P_TURNOVER 0x33
  72. /* user ram section */
  73. #define RV3029C2_USR1_RAM_PAGE 0x38
  74. #define RV3029C2_USR1_SECTION_LEN 0x04
  75. #define RV3029C2_USR2_RAM_PAGE 0x3C
  76. #define RV3029C2_USR2_SECTION_LEN 0x04
  77. static int
  78. rv3029c2_i2c_read_regs(struct i2c_client *client, u8 reg, u8 *buf,
  79. unsigned len)
  80. {
  81. int ret;
  82. if ((reg > RV3029C2_USR1_RAM_PAGE + 7) ||
  83. (reg + len > RV3029C2_USR1_RAM_PAGE + 8))
  84. return -EINVAL;
  85. ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf);
  86. if (ret < 0)
  87. return ret;
  88. if (ret < len)
  89. return -EIO;
  90. return 0;
  91. }
  92. static int
  93. rv3029c2_i2c_write_regs(struct i2c_client *client, u8 reg, u8 const buf[],
  94. unsigned len)
  95. {
  96. if ((reg > RV3029C2_USR1_RAM_PAGE + 7) ||
  97. (reg + len > RV3029C2_USR1_RAM_PAGE + 8))
  98. return -EINVAL;
  99. return i2c_smbus_write_i2c_block_data(client, reg, len, buf);
  100. }
  101. static int
  102. rv3029c2_i2c_get_sr(struct i2c_client *client, u8 *buf)
  103. {
  104. int ret = rv3029c2_i2c_read_regs(client, RV3029C2_STATUS, buf, 1);
  105. if (ret < 0)
  106. return -EIO;
  107. dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
  108. return 0;
  109. }
  110. static int
  111. rv3029c2_i2c_set_sr(struct i2c_client *client, u8 val)
  112. {
  113. u8 buf[1];
  114. int sr;
  115. buf[0] = val;
  116. sr = rv3029c2_i2c_write_regs(client, RV3029C2_STATUS, buf, 1);
  117. dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
  118. if (sr < 0)
  119. return -EIO;
  120. return 0;
  121. }
  122. static int
  123. rv3029c2_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
  124. {
  125. u8 buf[1];
  126. int ret;
  127. u8 regs[RV3029C2_WATCH_SECTION_LEN] = { 0, };
  128. ret = rv3029c2_i2c_get_sr(client, buf);
  129. if (ret < 0) {
  130. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  131. return -EIO;
  132. }
  133. ret = rv3029c2_i2c_read_regs(client, RV3029C2_W_SEC , regs,
  134. RV3029C2_WATCH_SECTION_LEN);
  135. if (ret < 0) {
  136. dev_err(&client->dev, "%s: reading RTC section failed\n",
  137. __func__);
  138. return ret;
  139. }
  140. tm->tm_sec = bcd2bin(regs[RV3029C2_W_SEC-RV3029C2_W_SEC]);
  141. tm->tm_min = bcd2bin(regs[RV3029C2_W_MINUTES-RV3029C2_W_SEC]);
  142. /* HR field has a more complex interpretation */
  143. {
  144. const u8 _hr = regs[RV3029C2_W_HOURS-RV3029C2_W_SEC];
  145. if (_hr & RV3029C2_REG_HR_12_24) {
  146. /* 12h format */
  147. tm->tm_hour = bcd2bin(_hr & 0x1f);
  148. if (_hr & RV3029C2_REG_HR_PM) /* PM flag set */
  149. tm->tm_hour += 12;
  150. } else /* 24h format */
  151. tm->tm_hour = bcd2bin(_hr & 0x3f);
  152. }
  153. tm->tm_mday = bcd2bin(regs[RV3029C2_W_DATE-RV3029C2_W_SEC]);
  154. tm->tm_mon = bcd2bin(regs[RV3029C2_W_MONTHS-RV3029C2_W_SEC]) - 1;
  155. tm->tm_year = bcd2bin(regs[RV3029C2_W_YEARS-RV3029C2_W_SEC]) + 100;
  156. tm->tm_wday = bcd2bin(regs[RV3029C2_W_DAYS-RV3029C2_W_SEC]) - 1;
  157. return 0;
  158. }
  159. static int rv3029c2_rtc_read_time(struct device *dev, struct rtc_time *tm)
  160. {
  161. return rv3029c2_i2c_read_time(to_i2c_client(dev), tm);
  162. }
  163. static int
  164. rv3029c2_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
  165. {
  166. struct rtc_time *const tm = &alarm->time;
  167. int ret;
  168. u8 regs[8];
  169. ret = rv3029c2_i2c_get_sr(client, regs);
  170. if (ret < 0) {
  171. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  172. return -EIO;
  173. }
  174. ret = rv3029c2_i2c_read_regs(client, RV3029C2_A_SC, regs,
  175. RV3029C2_ALARM_SECTION_LEN);
  176. if (ret < 0) {
  177. dev_err(&client->dev, "%s: reading alarm section failed\n",
  178. __func__);
  179. return ret;
  180. }
  181. tm->tm_sec = bcd2bin(regs[RV3029C2_A_SC-RV3029C2_A_SC] & 0x7f);
  182. tm->tm_min = bcd2bin(regs[RV3029C2_A_MN-RV3029C2_A_SC] & 0x7f);
  183. tm->tm_hour = bcd2bin(regs[RV3029C2_A_HR-RV3029C2_A_SC] & 0x3f);
  184. tm->tm_mday = bcd2bin(regs[RV3029C2_A_DT-RV3029C2_A_SC] & 0x3f);
  185. tm->tm_mon = bcd2bin(regs[RV3029C2_A_MO-RV3029C2_A_SC] & 0x1f) - 1;
  186. tm->tm_year = bcd2bin(regs[RV3029C2_A_YR-RV3029C2_A_SC] & 0x7f) + 100;
  187. tm->tm_wday = bcd2bin(regs[RV3029C2_A_DW-RV3029C2_A_SC] & 0x07) - 1;
  188. return 0;
  189. }
  190. static int
  191. rv3029c2_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  192. {
  193. return rv3029c2_i2c_read_alarm(to_i2c_client(dev), alarm);
  194. }
  195. static int rv3029c2_rtc_i2c_alarm_set_irq(struct i2c_client *client,
  196. int enable)
  197. {
  198. int ret;
  199. u8 buf[1];
  200. /* enable AIE irq */
  201. ret = rv3029c2_i2c_read_regs(client, RV3029C2_IRQ_CTRL, buf, 1);
  202. if (ret < 0) {
  203. dev_err(&client->dev, "can't read INT reg\n");
  204. return ret;
  205. }
  206. if (enable)
  207. buf[0] |= RV3029C2_IRQ_CTRL_AIE;
  208. else
  209. buf[0] &= ~RV3029C2_IRQ_CTRL_AIE;
  210. ret = rv3029c2_i2c_write_regs(client, RV3029C2_IRQ_CTRL, buf, 1);
  211. if (ret < 0) {
  212. dev_err(&client->dev, "can't set INT reg\n");
  213. return ret;
  214. }
  215. return 0;
  216. }
  217. static int rv3029c2_rtc_i2c_set_alarm(struct i2c_client *client,
  218. struct rtc_wkalrm *alarm)
  219. {
  220. struct rtc_time *const tm = &alarm->time;
  221. int ret;
  222. u8 regs[8];
  223. /*
  224. * The clock has an 8 bit wide bcd-coded register (they never learn)
  225. * for the year. tm_year is an offset from 1900 and we are interested
  226. * in the 2000-2099 range, so any value less than 100 is invalid.
  227. */
  228. if (tm->tm_year < 100)
  229. return -EINVAL;
  230. ret = rv3029c2_i2c_get_sr(client, regs);
  231. if (ret < 0) {
  232. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  233. return -EIO;
  234. }
  235. regs[RV3029C2_A_SC-RV3029C2_A_SC] = bin2bcd(tm->tm_sec & 0x7f);
  236. regs[RV3029C2_A_MN-RV3029C2_A_SC] = bin2bcd(tm->tm_min & 0x7f);
  237. regs[RV3029C2_A_HR-RV3029C2_A_SC] = bin2bcd(tm->tm_hour & 0x3f);
  238. regs[RV3029C2_A_DT-RV3029C2_A_SC] = bin2bcd(tm->tm_mday & 0x3f);
  239. regs[RV3029C2_A_MO-RV3029C2_A_SC] = bin2bcd((tm->tm_mon & 0x1f) - 1);
  240. regs[RV3029C2_A_DW-RV3029C2_A_SC] = bin2bcd((tm->tm_wday & 7) - 1);
  241. regs[RV3029C2_A_YR-RV3029C2_A_SC] = bin2bcd((tm->tm_year & 0x7f) - 100);
  242. ret = rv3029c2_i2c_write_regs(client, RV3029C2_A_SC, regs,
  243. RV3029C2_ALARM_SECTION_LEN);
  244. if (ret < 0)
  245. return ret;
  246. if (alarm->enabled) {
  247. u8 buf[1];
  248. /* clear AF flag */
  249. ret = rv3029c2_i2c_read_regs(client, RV3029C2_IRQ_FLAGS,
  250. buf, 1);
  251. if (ret < 0) {
  252. dev_err(&client->dev, "can't read alarm flag\n");
  253. return ret;
  254. }
  255. buf[0] &= ~RV3029C2_IRQ_FLAGS_AF;
  256. ret = rv3029c2_i2c_write_regs(client, RV3029C2_IRQ_FLAGS,
  257. buf, 1);
  258. if (ret < 0) {
  259. dev_err(&client->dev, "can't set alarm flag\n");
  260. return ret;
  261. }
  262. /* enable AIE irq */
  263. ret = rv3029c2_rtc_i2c_alarm_set_irq(client, 1);
  264. if (ret)
  265. return ret;
  266. dev_dbg(&client->dev, "alarm IRQ armed\n");
  267. } else {
  268. /* disable AIE irq */
  269. ret = rv3029c2_rtc_i2c_alarm_set_irq(client, 0);
  270. if (ret)
  271. return ret;
  272. dev_dbg(&client->dev, "alarm IRQ disabled\n");
  273. }
  274. return 0;
  275. }
  276. static int rv3029c2_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  277. {
  278. return rv3029c2_rtc_i2c_set_alarm(to_i2c_client(dev), alarm);
  279. }
  280. static int
  281. rv3029c2_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
  282. {
  283. u8 regs[8];
  284. int ret;
  285. /*
  286. * The clock has an 8 bit wide bcd-coded register (they never learn)
  287. * for the year. tm_year is an offset from 1900 and we are interested
  288. * in the 2000-2099 range, so any value less than 100 is invalid.
  289. */
  290. if (tm->tm_year < 100)
  291. return -EINVAL;
  292. regs[RV3029C2_W_SEC-RV3029C2_W_SEC] = bin2bcd(tm->tm_sec);
  293. regs[RV3029C2_W_MINUTES-RV3029C2_W_SEC] = bin2bcd(tm->tm_min);
  294. regs[RV3029C2_W_HOURS-RV3029C2_W_SEC] = bin2bcd(tm->tm_hour);
  295. regs[RV3029C2_W_DATE-RV3029C2_W_SEC] = bin2bcd(tm->tm_mday);
  296. regs[RV3029C2_W_MONTHS-RV3029C2_W_SEC] = bin2bcd(tm->tm_mon+1);
  297. regs[RV3029C2_W_DAYS-RV3029C2_W_SEC] = bin2bcd((tm->tm_wday & 7)+1);
  298. regs[RV3029C2_W_YEARS-RV3029C2_W_SEC] = bin2bcd(tm->tm_year - 100);
  299. ret = rv3029c2_i2c_write_regs(client, RV3029C2_W_SEC, regs,
  300. RV3029C2_WATCH_SECTION_LEN);
  301. if (ret < 0)
  302. return ret;
  303. ret = rv3029c2_i2c_get_sr(client, regs);
  304. if (ret < 0) {
  305. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  306. return ret;
  307. }
  308. /* clear PON bit */
  309. ret = rv3029c2_i2c_set_sr(client, (regs[0] & ~RV3029C2_STATUS_PON));
  310. if (ret < 0) {
  311. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  312. return ret;
  313. }
  314. return 0;
  315. }
  316. static int rv3029c2_rtc_set_time(struct device *dev, struct rtc_time *tm)
  317. {
  318. return rv3029c2_i2c_set_time(to_i2c_client(dev), tm);
  319. }
  320. static const struct rtc_class_ops rv3029c2_rtc_ops = {
  321. .read_time = rv3029c2_rtc_read_time,
  322. .set_time = rv3029c2_rtc_set_time,
  323. .read_alarm = rv3029c2_rtc_read_alarm,
  324. .set_alarm = rv3029c2_rtc_set_alarm,
  325. };
  326. static struct i2c_device_id rv3029c2_id[] = {
  327. { "rv3029c2", 0 },
  328. { }
  329. };
  330. MODULE_DEVICE_TABLE(i2c, rv3029c2_id);
  331. static int rv3029c2_probe(struct i2c_client *client,
  332. const struct i2c_device_id *id)
  333. {
  334. struct rtc_device *rtc;
  335. int rc = 0;
  336. u8 buf[1];
  337. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_EMUL))
  338. return -ENODEV;
  339. rc = rv3029c2_i2c_get_sr(client, buf);
  340. if (rc < 0) {
  341. dev_err(&client->dev, "reading status failed\n");
  342. return rc;
  343. }
  344. rtc = devm_rtc_device_register(&client->dev, client->name,
  345. &rv3029c2_rtc_ops, THIS_MODULE);
  346. if (IS_ERR(rtc))
  347. return PTR_ERR(rtc);
  348. i2c_set_clientdata(client, rtc);
  349. return 0;
  350. }
  351. static struct i2c_driver rv3029c2_driver = {
  352. .driver = {
  353. .name = "rtc-rv3029c2",
  354. },
  355. .probe = rv3029c2_probe,
  356. .id_table = rv3029c2_id,
  357. };
  358. module_i2c_driver(rv3029c2_driver);
  359. MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
  360. MODULE_DESCRIPTION("Micro Crystal RV3029C2 RTC driver");
  361. MODULE_LICENSE("GPL");