rtc-rv3029c2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * Micro Crystal RV-3029 rtc class driver
  3. *
  4. * Author: Gregory Hermant <gregory.hermant@calao-systems.com>
  5. * Michael Buesch <m@bues.ch>
  6. *
  7. * based on previously existing rtc class drivers
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/bcd.h>
  17. #include <linux/rtc.h>
  18. #include <linux/delay.h>
  19. #include <linux/of.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. /* Register map */
  23. /* control section */
  24. #define RV3029_ONOFF_CTRL 0x00
  25. #define RV3029_ONOFF_CTRL_WE BIT(0)
  26. #define RV3029_ONOFF_CTRL_TE BIT(1)
  27. #define RV3029_ONOFF_CTRL_TAR BIT(2)
  28. #define RV3029_ONOFF_CTRL_EERE BIT(3)
  29. #define RV3029_ONOFF_CTRL_SRON BIT(4)
  30. #define RV3029_ONOFF_CTRL_TD0 BIT(5)
  31. #define RV3029_ONOFF_CTRL_TD1 BIT(6)
  32. #define RV3029_ONOFF_CTRL_CLKINT BIT(7)
  33. #define RV3029_IRQ_CTRL 0x01
  34. #define RV3029_IRQ_CTRL_AIE BIT(0)
  35. #define RV3029_IRQ_CTRL_TIE BIT(1)
  36. #define RV3029_IRQ_CTRL_V1IE BIT(2)
  37. #define RV3029_IRQ_CTRL_V2IE BIT(3)
  38. #define RV3029_IRQ_CTRL_SRIE BIT(4)
  39. #define RV3029_IRQ_FLAGS 0x02
  40. #define RV3029_IRQ_FLAGS_AF BIT(0)
  41. #define RV3029_IRQ_FLAGS_TF BIT(1)
  42. #define RV3029_IRQ_FLAGS_V1IF BIT(2)
  43. #define RV3029_IRQ_FLAGS_V2IF BIT(3)
  44. #define RV3029_IRQ_FLAGS_SRF BIT(4)
  45. #define RV3029_STATUS 0x03
  46. #define RV3029_STATUS_VLOW1 BIT(2)
  47. #define RV3029_STATUS_VLOW2 BIT(3)
  48. #define RV3029_STATUS_SR BIT(4)
  49. #define RV3029_STATUS_PON BIT(5)
  50. #define RV3029_STATUS_EEBUSY BIT(7)
  51. #define RV3029_RST_CTRL 0x04
  52. #define RV3029_RST_CTRL_SYSR BIT(4)
  53. #define RV3029_CONTROL_SECTION_LEN 0x05
  54. /* watch section */
  55. #define RV3029_W_SEC 0x08
  56. #define RV3029_W_MINUTES 0x09
  57. #define RV3029_W_HOURS 0x0A
  58. #define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */
  59. #define RV3029_REG_HR_PM BIT(5) /* PM/AM bit in 12h mode */
  60. #define RV3029_W_DATE 0x0B
  61. #define RV3029_W_DAYS 0x0C
  62. #define RV3029_W_MONTHS 0x0D
  63. #define RV3029_W_YEARS 0x0E
  64. #define RV3029_WATCH_SECTION_LEN 0x07
  65. /* alarm section */
  66. #define RV3029_A_SC 0x10
  67. #define RV3029_A_MN 0x11
  68. #define RV3029_A_HR 0x12
  69. #define RV3029_A_DT 0x13
  70. #define RV3029_A_DW 0x14
  71. #define RV3029_A_MO 0x15
  72. #define RV3029_A_YR 0x16
  73. #define RV3029_ALARM_SECTION_LEN 0x07
  74. /* timer section */
  75. #define RV3029_TIMER_LOW 0x18
  76. #define RV3029_TIMER_HIGH 0x19
  77. /* temperature section */
  78. #define RV3029_TEMP_PAGE 0x20
  79. /* eeprom data section */
  80. #define RV3029_E2P_EEDATA1 0x28
  81. #define RV3029_E2P_EEDATA2 0x29
  82. #define RV3029_E2PDATA_SECTION_LEN 0x02
  83. /* eeprom control section */
  84. #define RV3029_CONTROL_E2P_EECTRL 0x30
  85. #define RV3029_EECTRL_THP BIT(0) /* temp scan interval */
  86. #define RV3029_EECTRL_THE BIT(1) /* thermometer enable */
  87. #define RV3029_EECTRL_FD0 BIT(2) /* CLKOUT */
  88. #define RV3029_EECTRL_FD1 BIT(3) /* CLKOUT */
  89. #define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */
  90. #define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */
  91. #define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */
  92. #define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */
  93. #define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\
  94. RV3029_TRICKLE_5K |\
  95. RV3029_TRICKLE_20K |\
  96. RV3029_TRICKLE_80K)
  97. #define RV3029_TRICKLE_SHIFT 4
  98. #define RV3029_CONTROL_E2P_XOFFS 0x31 /* XTAL offset */
  99. #define RV3029_CONTROL_E2P_XOFFS_SIGN BIT(7) /* Sign: 1->pos, 0->neg */
  100. #define RV3029_CONTROL_E2P_QCOEF 0x32 /* XTAL temp drift coef */
  101. #define RV3029_CONTROL_E2P_TURNOVER 0x33 /* XTAL turnover temp (in *C) */
  102. #define RV3029_CONTROL_E2P_TOV_MASK 0x3F /* XTAL turnover temp mask */
  103. /* user ram section */
  104. #define RV3029_USR1_RAM_PAGE 0x38
  105. #define RV3029_USR1_SECTION_LEN 0x04
  106. #define RV3029_USR2_RAM_PAGE 0x3C
  107. #define RV3029_USR2_SECTION_LEN 0x04
  108. static int
  109. rv3029_i2c_read_regs(struct i2c_client *client, u8 reg, u8 *buf,
  110. unsigned len)
  111. {
  112. int ret;
  113. if ((reg > RV3029_USR1_RAM_PAGE + 7) ||
  114. (reg + len > RV3029_USR1_RAM_PAGE + 8))
  115. return -EINVAL;
  116. ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf);
  117. if (ret < 0)
  118. return ret;
  119. if (ret < len)
  120. return -EIO;
  121. return 0;
  122. }
  123. static int
  124. rv3029_i2c_write_regs(struct i2c_client *client, u8 reg, u8 const buf[],
  125. unsigned len)
  126. {
  127. if ((reg > RV3029_USR1_RAM_PAGE + 7) ||
  128. (reg + len > RV3029_USR1_RAM_PAGE + 8))
  129. return -EINVAL;
  130. return i2c_smbus_write_i2c_block_data(client, reg, len, buf);
  131. }
  132. static int
  133. rv3029_i2c_update_bits(struct i2c_client *client, u8 reg, u8 mask, u8 set)
  134. {
  135. u8 buf;
  136. int ret;
  137. ret = rv3029_i2c_read_regs(client, reg, &buf, 1);
  138. if (ret < 0)
  139. return ret;
  140. buf &= ~mask;
  141. buf |= set & mask;
  142. ret = rv3029_i2c_write_regs(client, reg, &buf, 1);
  143. if (ret < 0)
  144. return ret;
  145. return 0;
  146. }
  147. static int
  148. rv3029_i2c_get_sr(struct i2c_client *client, u8 *buf)
  149. {
  150. int ret = rv3029_i2c_read_regs(client, RV3029_STATUS, buf, 1);
  151. if (ret < 0)
  152. return -EIO;
  153. dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
  154. return 0;
  155. }
  156. static int
  157. rv3029_i2c_set_sr(struct i2c_client *client, u8 val)
  158. {
  159. u8 buf[1];
  160. int sr;
  161. buf[0] = val;
  162. sr = rv3029_i2c_write_regs(client, RV3029_STATUS, buf, 1);
  163. dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
  164. if (sr < 0)
  165. return -EIO;
  166. return 0;
  167. }
  168. static int rv3029_eeprom_busywait(struct i2c_client *client)
  169. {
  170. int i, ret;
  171. u8 sr;
  172. for (i = 100; i > 0; i--) {
  173. ret = rv3029_i2c_get_sr(client, &sr);
  174. if (ret < 0)
  175. break;
  176. if (!(sr & RV3029_STATUS_EEBUSY))
  177. break;
  178. usleep_range(1000, 10000);
  179. }
  180. if (i <= 0) {
  181. dev_err(&client->dev, "EEPROM busy wait timeout.\n");
  182. return -ETIMEDOUT;
  183. }
  184. return ret;
  185. }
  186. static int rv3029_eeprom_exit(struct i2c_client *client)
  187. {
  188. /* Re-enable eeprom refresh */
  189. return rv3029_i2c_update_bits(client, RV3029_ONOFF_CTRL,
  190. RV3029_ONOFF_CTRL_EERE,
  191. RV3029_ONOFF_CTRL_EERE);
  192. }
  193. static int rv3029_eeprom_enter(struct i2c_client *client)
  194. {
  195. int ret;
  196. u8 sr;
  197. /* Check whether we are in the allowed voltage range. */
  198. ret = rv3029_i2c_get_sr(client, &sr);
  199. if (ret < 0)
  200. return ret;
  201. if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
  202. /* We clear the bits and retry once just in case
  203. * we had a brown out in early startup.
  204. */
  205. sr &= ~RV3029_STATUS_VLOW1;
  206. sr &= ~RV3029_STATUS_VLOW2;
  207. ret = rv3029_i2c_set_sr(client, sr);
  208. if (ret < 0)
  209. return ret;
  210. usleep_range(1000, 10000);
  211. ret = rv3029_i2c_get_sr(client, &sr);
  212. if (ret < 0)
  213. return ret;
  214. if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
  215. dev_err(&client->dev,
  216. "Supply voltage is too low to safely access the EEPROM.\n");
  217. return -ENODEV;
  218. }
  219. }
  220. /* Disable eeprom refresh. */
  221. ret = rv3029_i2c_update_bits(client, RV3029_ONOFF_CTRL,
  222. RV3029_ONOFF_CTRL_EERE, 0);
  223. if (ret < 0)
  224. return ret;
  225. /* Wait for any previous eeprom accesses to finish. */
  226. ret = rv3029_eeprom_busywait(client);
  227. if (ret < 0)
  228. rv3029_eeprom_exit(client);
  229. return ret;
  230. }
  231. static int rv3029_eeprom_read(struct i2c_client *client, u8 reg,
  232. u8 buf[], size_t len)
  233. {
  234. int ret, err;
  235. err = rv3029_eeprom_enter(client);
  236. if (err < 0)
  237. return err;
  238. ret = rv3029_i2c_read_regs(client, reg, buf, len);
  239. err = rv3029_eeprom_exit(client);
  240. if (err < 0)
  241. return err;
  242. return ret;
  243. }
  244. static int rv3029_eeprom_write(struct i2c_client *client, u8 reg,
  245. u8 const buf[], size_t len)
  246. {
  247. int ret, err;
  248. size_t i;
  249. u8 tmp;
  250. err = rv3029_eeprom_enter(client);
  251. if (err < 0)
  252. return err;
  253. for (i = 0; i < len; i++, reg++) {
  254. ret = rv3029_i2c_read_regs(client, reg, &tmp, 1);
  255. if (ret < 0)
  256. break;
  257. if (tmp != buf[i]) {
  258. ret = rv3029_i2c_write_regs(client, reg, &buf[i], 1);
  259. if (ret < 0)
  260. break;
  261. }
  262. ret = rv3029_eeprom_busywait(client);
  263. if (ret < 0)
  264. break;
  265. }
  266. err = rv3029_eeprom_exit(client);
  267. if (err < 0)
  268. return err;
  269. return ret;
  270. }
  271. static int rv3029_eeprom_update_bits(struct i2c_client *client,
  272. u8 reg, u8 mask, u8 set)
  273. {
  274. u8 buf;
  275. int ret;
  276. ret = rv3029_eeprom_read(client, reg, &buf, 1);
  277. if (ret < 0)
  278. return ret;
  279. buf &= ~mask;
  280. buf |= set & mask;
  281. ret = rv3029_eeprom_write(client, reg, &buf, 1);
  282. if (ret < 0)
  283. return ret;
  284. return 0;
  285. }
  286. static int
  287. rv3029_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
  288. {
  289. u8 buf[1];
  290. int ret;
  291. u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, };
  292. ret = rv3029_i2c_get_sr(client, buf);
  293. if (ret < 0) {
  294. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  295. return -EIO;
  296. }
  297. ret = rv3029_i2c_read_regs(client, RV3029_W_SEC, regs,
  298. RV3029_WATCH_SECTION_LEN);
  299. if (ret < 0) {
  300. dev_err(&client->dev, "%s: reading RTC section failed\n",
  301. __func__);
  302. return ret;
  303. }
  304. tm->tm_sec = bcd2bin(regs[RV3029_W_SEC-RV3029_W_SEC]);
  305. tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES-RV3029_W_SEC]);
  306. /* HR field has a more complex interpretation */
  307. {
  308. const u8 _hr = regs[RV3029_W_HOURS-RV3029_W_SEC];
  309. if (_hr & RV3029_REG_HR_12_24) {
  310. /* 12h format */
  311. tm->tm_hour = bcd2bin(_hr & 0x1f);
  312. if (_hr & RV3029_REG_HR_PM) /* PM flag set */
  313. tm->tm_hour += 12;
  314. } else /* 24h format */
  315. tm->tm_hour = bcd2bin(_hr & 0x3f);
  316. }
  317. tm->tm_mday = bcd2bin(regs[RV3029_W_DATE-RV3029_W_SEC]);
  318. tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS-RV3029_W_SEC]) - 1;
  319. tm->tm_year = bcd2bin(regs[RV3029_W_YEARS-RV3029_W_SEC]) + 100;
  320. tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS-RV3029_W_SEC]) - 1;
  321. return 0;
  322. }
  323. static int rv3029_rtc_read_time(struct device *dev, struct rtc_time *tm)
  324. {
  325. return rv3029_i2c_read_time(to_i2c_client(dev), tm);
  326. }
  327. static int
  328. rv3029_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
  329. {
  330. struct rtc_time *const tm = &alarm->time;
  331. int ret;
  332. u8 regs[8];
  333. ret = rv3029_i2c_get_sr(client, regs);
  334. if (ret < 0) {
  335. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  336. return -EIO;
  337. }
  338. ret = rv3029_i2c_read_regs(client, RV3029_A_SC, regs,
  339. RV3029_ALARM_SECTION_LEN);
  340. if (ret < 0) {
  341. dev_err(&client->dev, "%s: reading alarm section failed\n",
  342. __func__);
  343. return ret;
  344. }
  345. tm->tm_sec = bcd2bin(regs[RV3029_A_SC-RV3029_A_SC] & 0x7f);
  346. tm->tm_min = bcd2bin(regs[RV3029_A_MN-RV3029_A_SC] & 0x7f);
  347. tm->tm_hour = bcd2bin(regs[RV3029_A_HR-RV3029_A_SC] & 0x3f);
  348. tm->tm_mday = bcd2bin(regs[RV3029_A_DT-RV3029_A_SC] & 0x3f);
  349. tm->tm_mon = bcd2bin(regs[RV3029_A_MO-RV3029_A_SC] & 0x1f) - 1;
  350. tm->tm_year = bcd2bin(regs[RV3029_A_YR-RV3029_A_SC] & 0x7f) + 100;
  351. tm->tm_wday = bcd2bin(regs[RV3029_A_DW-RV3029_A_SC] & 0x07) - 1;
  352. return 0;
  353. }
  354. static int
  355. rv3029_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  356. {
  357. return rv3029_i2c_read_alarm(to_i2c_client(dev), alarm);
  358. }
  359. static int rv3029_rtc_i2c_alarm_set_irq(struct i2c_client *client,
  360. int enable)
  361. {
  362. int ret;
  363. /* enable/disable AIE irq */
  364. ret = rv3029_i2c_update_bits(client, RV3029_IRQ_CTRL,
  365. RV3029_IRQ_CTRL_AIE,
  366. (enable ? RV3029_IRQ_CTRL_AIE : 0));
  367. if (ret < 0) {
  368. dev_err(&client->dev, "can't update INT reg\n");
  369. return ret;
  370. }
  371. return 0;
  372. }
  373. static int rv3029_rtc_i2c_set_alarm(struct i2c_client *client,
  374. struct rtc_wkalrm *alarm)
  375. {
  376. struct rtc_time *const tm = &alarm->time;
  377. int ret;
  378. u8 regs[8];
  379. /*
  380. * The clock has an 8 bit wide bcd-coded register (they never learn)
  381. * for the year. tm_year is an offset from 1900 and we are interested
  382. * in the 2000-2099 range, so any value less than 100 is invalid.
  383. */
  384. if (tm->tm_year < 100)
  385. return -EINVAL;
  386. ret = rv3029_i2c_get_sr(client, regs);
  387. if (ret < 0) {
  388. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  389. return -EIO;
  390. }
  391. regs[RV3029_A_SC-RV3029_A_SC] = bin2bcd(tm->tm_sec & 0x7f);
  392. regs[RV3029_A_MN-RV3029_A_SC] = bin2bcd(tm->tm_min & 0x7f);
  393. regs[RV3029_A_HR-RV3029_A_SC] = bin2bcd(tm->tm_hour & 0x3f);
  394. regs[RV3029_A_DT-RV3029_A_SC] = bin2bcd(tm->tm_mday & 0x3f);
  395. regs[RV3029_A_MO-RV3029_A_SC] = bin2bcd((tm->tm_mon & 0x1f) - 1);
  396. regs[RV3029_A_DW-RV3029_A_SC] = bin2bcd((tm->tm_wday & 7) - 1);
  397. regs[RV3029_A_YR-RV3029_A_SC] = bin2bcd((tm->tm_year & 0x7f) - 100);
  398. ret = rv3029_i2c_write_regs(client, RV3029_A_SC, regs,
  399. RV3029_ALARM_SECTION_LEN);
  400. if (ret < 0)
  401. return ret;
  402. if (alarm->enabled) {
  403. /* clear AF flag */
  404. ret = rv3029_i2c_update_bits(client, RV3029_IRQ_FLAGS,
  405. RV3029_IRQ_FLAGS_AF, 0);
  406. if (ret < 0) {
  407. dev_err(&client->dev, "can't clear alarm flag\n");
  408. return ret;
  409. }
  410. /* enable AIE irq */
  411. ret = rv3029_rtc_i2c_alarm_set_irq(client, 1);
  412. if (ret)
  413. return ret;
  414. dev_dbg(&client->dev, "alarm IRQ armed\n");
  415. } else {
  416. /* disable AIE irq */
  417. ret = rv3029_rtc_i2c_alarm_set_irq(client, 0);
  418. if (ret)
  419. return ret;
  420. dev_dbg(&client->dev, "alarm IRQ disabled\n");
  421. }
  422. return 0;
  423. }
  424. static int rv3029_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  425. {
  426. return rv3029_rtc_i2c_set_alarm(to_i2c_client(dev), alarm);
  427. }
  428. static int
  429. rv3029_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
  430. {
  431. u8 regs[8];
  432. int ret;
  433. /*
  434. * The clock has an 8 bit wide bcd-coded register (they never learn)
  435. * for the year. tm_year is an offset from 1900 and we are interested
  436. * in the 2000-2099 range, so any value less than 100 is invalid.
  437. */
  438. if (tm->tm_year < 100)
  439. return -EINVAL;
  440. regs[RV3029_W_SEC-RV3029_W_SEC] = bin2bcd(tm->tm_sec);
  441. regs[RV3029_W_MINUTES-RV3029_W_SEC] = bin2bcd(tm->tm_min);
  442. regs[RV3029_W_HOURS-RV3029_W_SEC] = bin2bcd(tm->tm_hour);
  443. regs[RV3029_W_DATE-RV3029_W_SEC] = bin2bcd(tm->tm_mday);
  444. regs[RV3029_W_MONTHS-RV3029_W_SEC] = bin2bcd(tm->tm_mon+1);
  445. regs[RV3029_W_DAYS-RV3029_W_SEC] = bin2bcd((tm->tm_wday & 7)+1);
  446. regs[RV3029_W_YEARS-RV3029_W_SEC] = bin2bcd(tm->tm_year - 100);
  447. ret = rv3029_i2c_write_regs(client, RV3029_W_SEC, regs,
  448. RV3029_WATCH_SECTION_LEN);
  449. if (ret < 0)
  450. return ret;
  451. ret = rv3029_i2c_get_sr(client, regs);
  452. if (ret < 0) {
  453. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  454. return ret;
  455. }
  456. /* clear PON bit */
  457. ret = rv3029_i2c_set_sr(client, (regs[0] & ~RV3029_STATUS_PON));
  458. if (ret < 0) {
  459. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  460. return ret;
  461. }
  462. return 0;
  463. }
  464. static int rv3029_rtc_set_time(struct device *dev, struct rtc_time *tm)
  465. {
  466. return rv3029_i2c_set_time(to_i2c_client(dev), tm);
  467. }
  468. static const struct rv3029_trickle_tab_elem {
  469. u32 r; /* resistance in ohms */
  470. u8 conf; /* trickle config bits */
  471. } rv3029_trickle_tab[] = {
  472. {
  473. .r = 1076,
  474. .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
  475. RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
  476. }, {
  477. .r = 1091,
  478. .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
  479. RV3029_TRICKLE_20K,
  480. }, {
  481. .r = 1137,
  482. .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
  483. RV3029_TRICKLE_80K,
  484. }, {
  485. .r = 1154,
  486. .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
  487. }, {
  488. .r = 1371,
  489. .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
  490. RV3029_TRICKLE_80K,
  491. }, {
  492. .r = 1395,
  493. .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
  494. }, {
  495. .r = 1472,
  496. .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
  497. }, {
  498. .r = 1500,
  499. .conf = RV3029_TRICKLE_1K,
  500. }, {
  501. .r = 3810,
  502. .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
  503. RV3029_TRICKLE_80K,
  504. }, {
  505. .r = 4000,
  506. .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
  507. }, {
  508. .r = 4706,
  509. .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
  510. }, {
  511. .r = 5000,
  512. .conf = RV3029_TRICKLE_5K,
  513. }, {
  514. .r = 16000,
  515. .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
  516. }, {
  517. .r = 20000,
  518. .conf = RV3029_TRICKLE_20K,
  519. }, {
  520. .r = 80000,
  521. .conf = RV3029_TRICKLE_80K,
  522. },
  523. };
  524. static void rv3029_trickle_config(struct i2c_client *client)
  525. {
  526. struct device_node *of_node = client->dev.of_node;
  527. const struct rv3029_trickle_tab_elem *elem;
  528. int i, err;
  529. u32 ohms;
  530. u8 trickle_set_bits;
  531. if (!of_node)
  532. return;
  533. /* Configure the trickle charger. */
  534. err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms);
  535. if (err) {
  536. /* Disable trickle charger. */
  537. trickle_set_bits = 0;
  538. } else {
  539. /* Enable trickle charger. */
  540. for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) {
  541. elem = &rv3029_trickle_tab[i];
  542. if (elem->r >= ohms)
  543. break;
  544. }
  545. trickle_set_bits = elem->conf;
  546. dev_info(&client->dev,
  547. "Trickle charger enabled at %d ohms resistance.\n",
  548. elem->r);
  549. }
  550. err = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL,
  551. RV3029_TRICKLE_MASK,
  552. trickle_set_bits);
  553. if (err < 0) {
  554. dev_err(&client->dev,
  555. "Failed to update trickle charger config\n");
  556. }
  557. }
  558. #ifdef CONFIG_RTC_DRV_RV3029_HWMON
  559. static int rv3029_read_temp(struct i2c_client *client, int *temp_mC)
  560. {
  561. int ret;
  562. u8 temp;
  563. ret = rv3029_i2c_read_regs(client, RV3029_TEMP_PAGE, &temp, 1);
  564. if (ret < 0)
  565. return ret;
  566. *temp_mC = ((int)temp - 60) * 1000;
  567. return 0;
  568. }
  569. static ssize_t rv3029_hwmon_show_temp(struct device *dev,
  570. struct device_attribute *attr,
  571. char *buf)
  572. {
  573. struct i2c_client *client = dev_get_drvdata(dev);
  574. int ret, temp_mC;
  575. ret = rv3029_read_temp(client, &temp_mC);
  576. if (ret < 0)
  577. return ret;
  578. return sprintf(buf, "%d\n", temp_mC);
  579. }
  580. static ssize_t rv3029_hwmon_set_update_interval(struct device *dev,
  581. struct device_attribute *attr,
  582. const char *buf,
  583. size_t count)
  584. {
  585. struct i2c_client *client = dev_get_drvdata(dev);
  586. unsigned long interval_ms;
  587. int ret;
  588. u8 th_set_bits = 0;
  589. ret = kstrtoul(buf, 10, &interval_ms);
  590. if (ret < 0)
  591. return ret;
  592. if (interval_ms != 0) {
  593. th_set_bits |= RV3029_EECTRL_THE;
  594. if (interval_ms >= 16000)
  595. th_set_bits |= RV3029_EECTRL_THP;
  596. }
  597. ret = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL,
  598. RV3029_EECTRL_THE | RV3029_EECTRL_THP,
  599. th_set_bits);
  600. if (ret < 0)
  601. return ret;
  602. return count;
  603. }
  604. static ssize_t rv3029_hwmon_show_update_interval(struct device *dev,
  605. struct device_attribute *attr,
  606. char *buf)
  607. {
  608. struct i2c_client *client = dev_get_drvdata(dev);
  609. int ret, interval_ms;
  610. u8 eectrl;
  611. ret = rv3029_eeprom_read(client, RV3029_CONTROL_E2P_EECTRL,
  612. &eectrl, 1);
  613. if (ret < 0)
  614. return ret;
  615. if (eectrl & RV3029_EECTRL_THE) {
  616. if (eectrl & RV3029_EECTRL_THP)
  617. interval_ms = 16000;
  618. else
  619. interval_ms = 1000;
  620. } else {
  621. interval_ms = 0;
  622. }
  623. return sprintf(buf, "%d\n", interval_ms);
  624. }
  625. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, rv3029_hwmon_show_temp,
  626. NULL, 0);
  627. static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,
  628. rv3029_hwmon_show_update_interval,
  629. rv3029_hwmon_set_update_interval, 0);
  630. static struct attribute *rv3029_hwmon_attrs[] = {
  631. &sensor_dev_attr_temp1_input.dev_attr.attr,
  632. &sensor_dev_attr_update_interval.dev_attr.attr,
  633. NULL,
  634. };
  635. ATTRIBUTE_GROUPS(rv3029_hwmon);
  636. static void rv3029_hwmon_register(struct i2c_client *client)
  637. {
  638. struct device *hwmon_dev;
  639. hwmon_dev = devm_hwmon_device_register_with_groups(
  640. &client->dev, client->name, client, rv3029_hwmon_groups);
  641. if (IS_ERR(hwmon_dev)) {
  642. dev_warn(&client->dev,
  643. "unable to register hwmon device %ld\n",
  644. PTR_ERR(hwmon_dev));
  645. }
  646. }
  647. #else /* CONFIG_RTC_DRV_RV3029_HWMON */
  648. static void rv3029_hwmon_register(struct i2c_client *client)
  649. {
  650. }
  651. #endif /* CONFIG_RTC_DRV_RV3029_HWMON */
  652. static const struct rtc_class_ops rv3029_rtc_ops = {
  653. .read_time = rv3029_rtc_read_time,
  654. .set_time = rv3029_rtc_set_time,
  655. .read_alarm = rv3029_rtc_read_alarm,
  656. .set_alarm = rv3029_rtc_set_alarm,
  657. };
  658. static struct i2c_device_id rv3029_id[] = {
  659. { "rv3029", 0 },
  660. { "rv3029c2", 0 },
  661. { }
  662. };
  663. MODULE_DEVICE_TABLE(i2c, rv3029_id);
  664. static int rv3029_probe(struct i2c_client *client,
  665. const struct i2c_device_id *id)
  666. {
  667. struct rtc_device *rtc;
  668. int rc = 0;
  669. u8 buf[1];
  670. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_EMUL))
  671. return -ENODEV;
  672. rc = rv3029_i2c_get_sr(client, buf);
  673. if (rc < 0) {
  674. dev_err(&client->dev, "reading status failed\n");
  675. return rc;
  676. }
  677. rv3029_trickle_config(client);
  678. rv3029_hwmon_register(client);
  679. rtc = devm_rtc_device_register(&client->dev, client->name,
  680. &rv3029_rtc_ops, THIS_MODULE);
  681. if (IS_ERR(rtc))
  682. return PTR_ERR(rtc);
  683. i2c_set_clientdata(client, rtc);
  684. return 0;
  685. }
  686. static struct i2c_driver rv3029_driver = {
  687. .driver = {
  688. .name = "rtc-rv3029c2",
  689. },
  690. .probe = rv3029_probe,
  691. .id_table = rv3029_id,
  692. };
  693. module_i2c_driver(rv3029_driver);
  694. MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
  695. MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
  696. MODULE_DESCRIPTION("Micro Crystal RV3029 RTC driver");
  697. MODULE_LICENSE("GPL");