rtc-isl1208.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. * Intersil ISL1208 rtc class driver
  3. *
  4. * Copyright 2005,2006 Hebert Valerio Riedel <hvr@gnu.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/i2c.h>
  14. #include <linux/bcd.h>
  15. #include <linux/rtc.h>
  16. #include "rtc-core.h"
  17. #include <linux/of_irq.h>
  18. /* Register map */
  19. /* rtc section */
  20. #define ISL1208_REG_SC 0x00
  21. #define ISL1208_REG_MN 0x01
  22. #define ISL1208_REG_HR 0x02
  23. #define ISL1208_REG_HR_MIL (1<<7) /* 24h/12h mode */
  24. #define ISL1208_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */
  25. #define ISL1208_REG_DT 0x03
  26. #define ISL1208_REG_MO 0x04
  27. #define ISL1208_REG_YR 0x05
  28. #define ISL1208_REG_DW 0x06
  29. #define ISL1208_RTC_SECTION_LEN 7
  30. /* control/status section */
  31. #define ISL1208_REG_SR 0x07
  32. #define ISL1208_REG_SR_ARST (1<<7) /* auto reset */
  33. #define ISL1208_REG_SR_XTOSCB (1<<6) /* crystal oscillator */
  34. #define ISL1208_REG_SR_WRTC (1<<4) /* write rtc */
  35. #define ISL1208_REG_SR_EVT (1<<3) /* event */
  36. #define ISL1208_REG_SR_ALM (1<<2) /* alarm */
  37. #define ISL1208_REG_SR_BAT (1<<1) /* battery */
  38. #define ISL1208_REG_SR_RTCF (1<<0) /* rtc fail */
  39. #define ISL1208_REG_INT 0x08
  40. #define ISL1208_REG_INT_ALME (1<<6) /* alarm enable */
  41. #define ISL1208_REG_INT_IM (1<<7) /* interrupt/alarm mode */
  42. #define ISL1219_REG_EV 0x09
  43. #define ISL1219_REG_EV_EVEN (1<<4) /* event detection enable */
  44. #define ISL1219_REG_EV_EVIENB (1<<7) /* event in pull-up disable */
  45. #define ISL1208_REG_ATR 0x0a
  46. #define ISL1208_REG_DTR 0x0b
  47. /* alarm section */
  48. #define ISL1208_REG_SCA 0x0c
  49. #define ISL1208_REG_MNA 0x0d
  50. #define ISL1208_REG_HRA 0x0e
  51. #define ISL1208_REG_DTA 0x0f
  52. #define ISL1208_REG_MOA 0x10
  53. #define ISL1208_REG_DWA 0x11
  54. #define ISL1208_ALARM_SECTION_LEN 6
  55. /* user section */
  56. #define ISL1208_REG_USR1 0x12
  57. #define ISL1208_REG_USR2 0x13
  58. #define ISL1208_USR_SECTION_LEN 2
  59. /* event section */
  60. #define ISL1219_REG_SCT 0x14
  61. #define ISL1219_REG_MNT 0x15
  62. #define ISL1219_REG_HRT 0x16
  63. #define ISL1219_REG_DTT 0x17
  64. #define ISL1219_REG_MOT 0x18
  65. #define ISL1219_REG_YRT 0x19
  66. #define ISL1219_EVT_SECTION_LEN 6
  67. static struct i2c_driver isl1208_driver;
  68. /* ISL1208 various variants */
  69. enum {
  70. TYPE_ISL1208 = 0,
  71. TYPE_ISL1218,
  72. TYPE_ISL1219,
  73. };
  74. /* block read */
  75. static int
  76. isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[],
  77. unsigned len)
  78. {
  79. u8 reg_addr[1] = { reg };
  80. struct i2c_msg msgs[2] = {
  81. {
  82. .addr = client->addr,
  83. .len = sizeof(reg_addr),
  84. .buf = reg_addr
  85. },
  86. {
  87. .addr = client->addr,
  88. .flags = I2C_M_RD,
  89. .len = len,
  90. .buf = buf
  91. }
  92. };
  93. int ret;
  94. WARN_ON(reg > ISL1219_REG_YRT);
  95. WARN_ON(reg + len > ISL1219_REG_YRT + 1);
  96. ret = i2c_transfer(client->adapter, msgs, 2);
  97. if (ret > 0)
  98. ret = 0;
  99. return ret;
  100. }
  101. /* block write */
  102. static int
  103. isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[],
  104. unsigned len)
  105. {
  106. u8 i2c_buf[ISL1208_REG_USR2 + 2];
  107. struct i2c_msg msgs[1] = {
  108. {
  109. .addr = client->addr,
  110. .len = len + 1,
  111. .buf = i2c_buf
  112. }
  113. };
  114. int ret;
  115. WARN_ON(reg > ISL1219_REG_YRT);
  116. WARN_ON(reg + len > ISL1219_REG_YRT + 1);
  117. i2c_buf[0] = reg;
  118. memcpy(&i2c_buf[1], &buf[0], len);
  119. ret = i2c_transfer(client->adapter, msgs, 1);
  120. if (ret > 0)
  121. ret = 0;
  122. return ret;
  123. }
  124. /* simple check to see whether we have a isl1208 */
  125. static int
  126. isl1208_i2c_validate_client(struct i2c_client *client)
  127. {
  128. u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
  129. u8 zero_mask[ISL1208_RTC_SECTION_LEN] = {
  130. 0x80, 0x80, 0x40, 0xc0, 0xe0, 0x00, 0xf8
  131. };
  132. int i;
  133. int ret;
  134. ret = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
  135. if (ret < 0)
  136. return ret;
  137. for (i = 0; i < ISL1208_RTC_SECTION_LEN; ++i) {
  138. if (regs[i] & zero_mask[i]) /* check if bits are cleared */
  139. return -ENODEV;
  140. }
  141. return 0;
  142. }
  143. static int
  144. isl1208_i2c_get_sr(struct i2c_client *client)
  145. {
  146. return i2c_smbus_read_byte_data(client, ISL1208_REG_SR);
  147. }
  148. static int
  149. isl1208_i2c_get_atr(struct i2c_client *client)
  150. {
  151. int atr = i2c_smbus_read_byte_data(client, ISL1208_REG_ATR);
  152. if (atr < 0)
  153. return atr;
  154. /* The 6bit value in the ATR register controls the load
  155. * capacitance C_load * in steps of 0.25pF
  156. *
  157. * bit (1<<5) of the ATR register is inverted
  158. *
  159. * C_load(ATR=0x20) = 4.50pF
  160. * C_load(ATR=0x00) = 12.50pF
  161. * C_load(ATR=0x1f) = 20.25pF
  162. *
  163. */
  164. atr &= 0x3f; /* mask out lsb */
  165. atr ^= 1 << 5; /* invert 6th bit */
  166. atr += 2 * 9; /* add offset of 4.5pF; unit[atr] = 0.25pF */
  167. return atr;
  168. }
  169. static int
  170. isl1208_i2c_get_dtr(struct i2c_client *client)
  171. {
  172. int dtr = i2c_smbus_read_byte_data(client, ISL1208_REG_DTR);
  173. if (dtr < 0)
  174. return -EIO;
  175. /* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */
  176. dtr = ((dtr & 0x3) * 20) * (dtr & (1 << 2) ? -1 : 1);
  177. return dtr;
  178. }
  179. static int
  180. isl1208_i2c_get_usr(struct i2c_client *client)
  181. {
  182. u8 buf[ISL1208_USR_SECTION_LEN] = { 0, };
  183. int ret;
  184. ret = isl1208_i2c_read_regs(client, ISL1208_REG_USR1, buf,
  185. ISL1208_USR_SECTION_LEN);
  186. if (ret < 0)
  187. return ret;
  188. return (buf[1] << 8) | buf[0];
  189. }
  190. static int
  191. isl1208_i2c_set_usr(struct i2c_client *client, u16 usr)
  192. {
  193. u8 buf[ISL1208_USR_SECTION_LEN];
  194. buf[0] = usr & 0xff;
  195. buf[1] = (usr >> 8) & 0xff;
  196. return isl1208_i2c_set_regs(client, ISL1208_REG_USR1, buf,
  197. ISL1208_USR_SECTION_LEN);
  198. }
  199. static int
  200. isl1208_rtc_toggle_alarm(struct i2c_client *client, int enable)
  201. {
  202. int icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
  203. if (icr < 0) {
  204. dev_err(&client->dev, "%s: reading INT failed\n", __func__);
  205. return icr;
  206. }
  207. if (enable)
  208. icr |= ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM;
  209. else
  210. icr &= ~(ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM);
  211. icr = i2c_smbus_write_byte_data(client, ISL1208_REG_INT, icr);
  212. if (icr < 0) {
  213. dev_err(&client->dev, "%s: writing INT failed\n", __func__);
  214. return icr;
  215. }
  216. return 0;
  217. }
  218. static int
  219. isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
  220. {
  221. struct i2c_client *const client = to_i2c_client(dev);
  222. int sr, dtr, atr, usr;
  223. sr = isl1208_i2c_get_sr(client);
  224. if (sr < 0) {
  225. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  226. return sr;
  227. }
  228. seq_printf(seq, "status_reg\t:%s%s%s%s%s%s (0x%.2x)\n",
  229. (sr & ISL1208_REG_SR_RTCF) ? " RTCF" : "",
  230. (sr & ISL1208_REG_SR_BAT) ? " BAT" : "",
  231. (sr & ISL1208_REG_SR_ALM) ? " ALM" : "",
  232. (sr & ISL1208_REG_SR_WRTC) ? " WRTC" : "",
  233. (sr & ISL1208_REG_SR_XTOSCB) ? " XTOSCB" : "",
  234. (sr & ISL1208_REG_SR_ARST) ? " ARST" : "", sr);
  235. seq_printf(seq, "batt_status\t: %s\n",
  236. (sr & ISL1208_REG_SR_RTCF) ? "bad" : "okay");
  237. dtr = isl1208_i2c_get_dtr(client);
  238. if (dtr >= 0 - 1)
  239. seq_printf(seq, "digital_trim\t: %d ppm\n", dtr);
  240. atr = isl1208_i2c_get_atr(client);
  241. if (atr >= 0)
  242. seq_printf(seq, "analog_trim\t: %d.%.2d pF\n",
  243. atr >> 2, (atr & 0x3) * 25);
  244. usr = isl1208_i2c_get_usr(client);
  245. if (usr >= 0)
  246. seq_printf(seq, "user_data\t: 0x%.4x\n", usr);
  247. return 0;
  248. }
  249. static int
  250. isl1208_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
  251. {
  252. int sr;
  253. u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
  254. sr = isl1208_i2c_get_sr(client);
  255. if (sr < 0) {
  256. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  257. return -EIO;
  258. }
  259. sr = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
  260. if (sr < 0) {
  261. dev_err(&client->dev, "%s: reading RTC section failed\n",
  262. __func__);
  263. return sr;
  264. }
  265. tm->tm_sec = bcd2bin(regs[ISL1208_REG_SC]);
  266. tm->tm_min = bcd2bin(regs[ISL1208_REG_MN]);
  267. /* HR field has a more complex interpretation */
  268. {
  269. const u8 _hr = regs[ISL1208_REG_HR];
  270. if (_hr & ISL1208_REG_HR_MIL) /* 24h format */
  271. tm->tm_hour = bcd2bin(_hr & 0x3f);
  272. else {
  273. /* 12h format */
  274. tm->tm_hour = bcd2bin(_hr & 0x1f);
  275. if (_hr & ISL1208_REG_HR_PM) /* PM flag set */
  276. tm->tm_hour += 12;
  277. }
  278. }
  279. tm->tm_mday = bcd2bin(regs[ISL1208_REG_DT]);
  280. tm->tm_mon = bcd2bin(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */
  281. tm->tm_year = bcd2bin(regs[ISL1208_REG_YR]) + 100;
  282. tm->tm_wday = bcd2bin(regs[ISL1208_REG_DW]);
  283. return 0;
  284. }
  285. static int
  286. isl1208_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
  287. {
  288. struct rtc_time *const tm = &alarm->time;
  289. u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
  290. int icr, yr, sr = isl1208_i2c_get_sr(client);
  291. if (sr < 0) {
  292. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  293. return sr;
  294. }
  295. sr = isl1208_i2c_read_regs(client, ISL1208_REG_SCA, regs,
  296. ISL1208_ALARM_SECTION_LEN);
  297. if (sr < 0) {
  298. dev_err(&client->dev, "%s: reading alarm section failed\n",
  299. __func__);
  300. return sr;
  301. }
  302. /* MSB of each alarm register is an enable bit */
  303. tm->tm_sec = bcd2bin(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f);
  304. tm->tm_min = bcd2bin(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f);
  305. tm->tm_hour = bcd2bin(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f);
  306. tm->tm_mday = bcd2bin(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f);
  307. tm->tm_mon =
  308. bcd2bin(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1;
  309. tm->tm_wday = bcd2bin(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03);
  310. /* The alarm doesn't store the year so get it from the rtc section */
  311. yr = i2c_smbus_read_byte_data(client, ISL1208_REG_YR);
  312. if (yr < 0) {
  313. dev_err(&client->dev, "%s: reading RTC YR failed\n", __func__);
  314. return yr;
  315. }
  316. tm->tm_year = bcd2bin(yr) + 100;
  317. icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
  318. if (icr < 0) {
  319. dev_err(&client->dev, "%s: reading INT failed\n", __func__);
  320. return icr;
  321. }
  322. alarm->enabled = !!(icr & ISL1208_REG_INT_ALME);
  323. return 0;
  324. }
  325. static int
  326. isl1208_i2c_set_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
  327. {
  328. struct rtc_time *alarm_tm = &alarm->time;
  329. u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
  330. const int offs = ISL1208_REG_SCA;
  331. struct rtc_time rtc_tm;
  332. int err, enable;
  333. err = isl1208_i2c_read_time(client, &rtc_tm);
  334. if (err)
  335. return err;
  336. /* If the alarm time is before the current time disable the alarm */
  337. if (!alarm->enabled || rtc_tm_sub(alarm_tm, &rtc_tm) <= 0)
  338. enable = 0x00;
  339. else
  340. enable = 0x80;
  341. /* Program the alarm and enable it for each setting */
  342. regs[ISL1208_REG_SCA - offs] = bin2bcd(alarm_tm->tm_sec) | enable;
  343. regs[ISL1208_REG_MNA - offs] = bin2bcd(alarm_tm->tm_min) | enable;
  344. regs[ISL1208_REG_HRA - offs] = bin2bcd(alarm_tm->tm_hour) |
  345. ISL1208_REG_HR_MIL | enable;
  346. regs[ISL1208_REG_DTA - offs] = bin2bcd(alarm_tm->tm_mday) | enable;
  347. regs[ISL1208_REG_MOA - offs] = bin2bcd(alarm_tm->tm_mon + 1) | enable;
  348. regs[ISL1208_REG_DWA - offs] = bin2bcd(alarm_tm->tm_wday & 7) | enable;
  349. /* write ALARM registers */
  350. err = isl1208_i2c_set_regs(client, offs, regs,
  351. ISL1208_ALARM_SECTION_LEN);
  352. if (err < 0) {
  353. dev_err(&client->dev, "%s: writing ALARM section failed\n",
  354. __func__);
  355. return err;
  356. }
  357. err = isl1208_rtc_toggle_alarm(client, enable);
  358. if (err)
  359. return err;
  360. return 0;
  361. }
  362. static int
  363. isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm)
  364. {
  365. return isl1208_i2c_read_time(to_i2c_client(dev), tm);
  366. }
  367. static int
  368. isl1208_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
  369. {
  370. int sr;
  371. u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
  372. /* The clock has an 8 bit wide bcd-coded register (they never learn)
  373. * for the year. tm_year is an offset from 1900 and we are interested
  374. * in the 2000-2099 range, so any value less than 100 is invalid.
  375. */
  376. if (tm->tm_year < 100)
  377. return -EINVAL;
  378. regs[ISL1208_REG_SC] = bin2bcd(tm->tm_sec);
  379. regs[ISL1208_REG_MN] = bin2bcd(tm->tm_min);
  380. regs[ISL1208_REG_HR] = bin2bcd(tm->tm_hour) | ISL1208_REG_HR_MIL;
  381. regs[ISL1208_REG_DT] = bin2bcd(tm->tm_mday);
  382. regs[ISL1208_REG_MO] = bin2bcd(tm->tm_mon + 1);
  383. regs[ISL1208_REG_YR] = bin2bcd(tm->tm_year - 100);
  384. regs[ISL1208_REG_DW] = bin2bcd(tm->tm_wday & 7);
  385. sr = isl1208_i2c_get_sr(client);
  386. if (sr < 0) {
  387. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  388. return sr;
  389. }
  390. /* set WRTC */
  391. sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
  392. sr | ISL1208_REG_SR_WRTC);
  393. if (sr < 0) {
  394. dev_err(&client->dev, "%s: writing SR failed\n", __func__);
  395. return sr;
  396. }
  397. /* write RTC registers */
  398. sr = isl1208_i2c_set_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
  399. if (sr < 0) {
  400. dev_err(&client->dev, "%s: writing RTC section failed\n",
  401. __func__);
  402. return sr;
  403. }
  404. /* clear WRTC again */
  405. sr = isl1208_i2c_get_sr(client);
  406. if (sr < 0) {
  407. dev_err(&client->dev, "%s: reading SR failed\n", __func__);
  408. return sr;
  409. }
  410. sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
  411. sr & ~ISL1208_REG_SR_WRTC);
  412. if (sr < 0) {
  413. dev_err(&client->dev, "%s: writing SR failed\n", __func__);
  414. return sr;
  415. }
  416. return 0;
  417. }
  418. static int
  419. isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm)
  420. {
  421. return isl1208_i2c_set_time(to_i2c_client(dev), tm);
  422. }
  423. static int
  424. isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  425. {
  426. return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm);
  427. }
  428. static int
  429. isl1208_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  430. {
  431. return isl1208_i2c_set_alarm(to_i2c_client(dev), alarm);
  432. }
  433. static ssize_t timestamp0_store(struct device *dev,
  434. struct device_attribute *attr,
  435. const char *buf, size_t count)
  436. {
  437. struct i2c_client *client = to_i2c_client(dev->parent);
  438. int sr;
  439. sr = isl1208_i2c_get_sr(client);
  440. if (sr < 0) {
  441. dev_err(dev, "%s: reading SR failed\n", __func__);
  442. return sr;
  443. }
  444. sr &= ~ISL1208_REG_SR_EVT;
  445. sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
  446. if (sr < 0)
  447. dev_err(dev, "%s: writing SR failed\n",
  448. __func__);
  449. return count;
  450. };
  451. static ssize_t timestamp0_show(struct device *dev,
  452. struct device_attribute *attr, char *buf)
  453. {
  454. struct i2c_client *client = to_i2c_client(dev->parent);
  455. u8 regs[ISL1219_EVT_SECTION_LEN] = { 0, };
  456. struct rtc_time tm;
  457. int sr;
  458. sr = isl1208_i2c_get_sr(client);
  459. if (sr < 0) {
  460. dev_err(dev, "%s: reading SR failed\n", __func__);
  461. return sr;
  462. }
  463. if (!(sr & ISL1208_REG_SR_EVT))
  464. return 0;
  465. sr = isl1208_i2c_read_regs(client, ISL1219_REG_SCT, regs,
  466. ISL1219_EVT_SECTION_LEN);
  467. if (sr < 0) {
  468. dev_err(dev, "%s: reading event section failed\n",
  469. __func__);
  470. return 0;
  471. }
  472. /* MSB of each alarm register is an enable bit */
  473. tm.tm_sec = bcd2bin(regs[ISL1219_REG_SCT - ISL1219_REG_SCT] & 0x7f);
  474. tm.tm_min = bcd2bin(regs[ISL1219_REG_MNT - ISL1219_REG_SCT] & 0x7f);
  475. tm.tm_hour = bcd2bin(regs[ISL1219_REG_HRT - ISL1219_REG_SCT] & 0x3f);
  476. tm.tm_mday = bcd2bin(regs[ISL1219_REG_DTT - ISL1219_REG_SCT] & 0x3f);
  477. tm.tm_mon =
  478. bcd2bin(regs[ISL1219_REG_MOT - ISL1219_REG_SCT] & 0x1f) - 1;
  479. tm.tm_year = bcd2bin(regs[ISL1219_REG_YRT - ISL1219_REG_SCT]) + 100;
  480. sr = rtc_valid_tm(&tm);
  481. if (sr)
  482. return sr;
  483. return sprintf(buf, "%llu\n",
  484. (unsigned long long)rtc_tm_to_time64(&tm));
  485. };
  486. static DEVICE_ATTR_RW(timestamp0);
  487. static irqreturn_t
  488. isl1208_rtc_interrupt(int irq, void *data)
  489. {
  490. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  491. struct i2c_client *client = data;
  492. struct rtc_device *rtc = i2c_get_clientdata(client);
  493. int handled = 0, sr, err;
  494. /*
  495. * I2C reads get NAK'ed if we read straight away after an interrupt?
  496. * Using a mdelay/msleep didn't seem to help either, so we work around
  497. * this by continually trying to read the register for a short time.
  498. */
  499. while (1) {
  500. sr = isl1208_i2c_get_sr(client);
  501. if (sr >= 0)
  502. break;
  503. if (time_after(jiffies, timeout)) {
  504. dev_err(&client->dev, "%s: reading SR failed\n",
  505. __func__);
  506. return sr;
  507. }
  508. }
  509. if (sr & ISL1208_REG_SR_ALM) {
  510. dev_dbg(&client->dev, "alarm!\n");
  511. rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
  512. /* Clear the alarm */
  513. sr &= ~ISL1208_REG_SR_ALM;
  514. sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
  515. if (sr < 0)
  516. dev_err(&client->dev, "%s: writing SR failed\n",
  517. __func__);
  518. else
  519. handled = 1;
  520. /* Disable the alarm */
  521. err = isl1208_rtc_toggle_alarm(client, 0);
  522. if (err)
  523. return err;
  524. }
  525. if (sr & ISL1208_REG_SR_EVT) {
  526. sysfs_notify(&rtc->dev.kobj, NULL,
  527. dev_attr_timestamp0.attr.name);
  528. dev_warn(&client->dev, "event detected");
  529. handled = 1;
  530. }
  531. return handled ? IRQ_HANDLED : IRQ_NONE;
  532. }
  533. static const struct rtc_class_ops isl1208_rtc_ops = {
  534. .proc = isl1208_rtc_proc,
  535. .read_time = isl1208_rtc_read_time,
  536. .set_time = isl1208_rtc_set_time,
  537. .read_alarm = isl1208_rtc_read_alarm,
  538. .set_alarm = isl1208_rtc_set_alarm,
  539. };
  540. /* sysfs interface */
  541. static ssize_t
  542. isl1208_sysfs_show_atrim(struct device *dev,
  543. struct device_attribute *attr, char *buf)
  544. {
  545. int atr = isl1208_i2c_get_atr(to_i2c_client(dev->parent));
  546. if (atr < 0)
  547. return atr;
  548. return sprintf(buf, "%d.%.2d pF\n", atr >> 2, (atr & 0x3) * 25);
  549. }
  550. static DEVICE_ATTR(atrim, S_IRUGO, isl1208_sysfs_show_atrim, NULL);
  551. static ssize_t
  552. isl1208_sysfs_show_dtrim(struct device *dev,
  553. struct device_attribute *attr, char *buf)
  554. {
  555. int dtr = isl1208_i2c_get_dtr(to_i2c_client(dev->parent));
  556. if (dtr < 0)
  557. return dtr;
  558. return sprintf(buf, "%d ppm\n", dtr);
  559. }
  560. static DEVICE_ATTR(dtrim, S_IRUGO, isl1208_sysfs_show_dtrim, NULL);
  561. static ssize_t
  562. isl1208_sysfs_show_usr(struct device *dev,
  563. struct device_attribute *attr, char *buf)
  564. {
  565. int usr = isl1208_i2c_get_usr(to_i2c_client(dev->parent));
  566. if (usr < 0)
  567. return usr;
  568. return sprintf(buf, "0x%.4x\n", usr);
  569. }
  570. static ssize_t
  571. isl1208_sysfs_store_usr(struct device *dev,
  572. struct device_attribute *attr,
  573. const char *buf, size_t count)
  574. {
  575. int usr = -1;
  576. if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) {
  577. if (sscanf(buf, "%x", &usr) != 1)
  578. return -EINVAL;
  579. } else {
  580. if (sscanf(buf, "%d", &usr) != 1)
  581. return -EINVAL;
  582. }
  583. if (usr < 0 || usr > 0xffff)
  584. return -EINVAL;
  585. if (isl1208_i2c_set_usr(to_i2c_client(dev->parent), usr))
  586. return -EIO;
  587. return count;
  588. }
  589. static DEVICE_ATTR(usr, S_IRUGO | S_IWUSR, isl1208_sysfs_show_usr,
  590. isl1208_sysfs_store_usr);
  591. static struct attribute *isl1208_rtc_attrs[] = {
  592. &dev_attr_atrim.attr,
  593. &dev_attr_dtrim.attr,
  594. &dev_attr_usr.attr,
  595. NULL
  596. };
  597. static const struct attribute_group isl1208_rtc_sysfs_files = {
  598. .attrs = isl1208_rtc_attrs,
  599. };
  600. static struct attribute *isl1219_rtc_attrs[] = {
  601. &dev_attr_timestamp0.attr,
  602. NULL
  603. };
  604. static const struct attribute_group isl1219_rtc_sysfs_files = {
  605. .attrs = isl1219_rtc_attrs,
  606. };
  607. static int isl1208_setup_irq(struct i2c_client *client, int irq)
  608. {
  609. int rc = devm_request_threaded_irq(&client->dev, irq, NULL,
  610. isl1208_rtc_interrupt,
  611. IRQF_SHARED | IRQF_ONESHOT,
  612. isl1208_driver.driver.name,
  613. client);
  614. if (!rc) {
  615. device_init_wakeup(&client->dev, 1);
  616. enable_irq_wake(irq);
  617. } else {
  618. dev_err(&client->dev,
  619. "Unable to request irq %d, no alarm support\n",
  620. irq);
  621. }
  622. return rc;
  623. }
  624. static int
  625. isl1208_probe(struct i2c_client *client, const struct i2c_device_id *id)
  626. {
  627. int rc = 0;
  628. struct rtc_device *rtc;
  629. int evdet_irq = -1;
  630. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  631. return -ENODEV;
  632. if (isl1208_i2c_validate_client(client) < 0)
  633. return -ENODEV;
  634. rtc = devm_rtc_allocate_device(&client->dev);
  635. if (IS_ERR(rtc))
  636. return PTR_ERR(rtc);
  637. rtc->ops = &isl1208_rtc_ops;
  638. i2c_set_clientdata(client, rtc);
  639. rc = isl1208_i2c_get_sr(client);
  640. if (rc < 0) {
  641. dev_err(&client->dev, "reading status failed\n");
  642. return rc;
  643. }
  644. if (rc & ISL1208_REG_SR_RTCF)
  645. dev_warn(&client->dev, "rtc power failure detected, "
  646. "please set clock.\n");
  647. if (id->driver_data == TYPE_ISL1219) {
  648. struct device_node *np = client->dev.of_node;
  649. u32 evienb;
  650. rc = i2c_smbus_read_byte_data(client, ISL1219_REG_EV);
  651. if (rc < 0) {
  652. dev_err(&client->dev, "failed to read EV reg\n");
  653. return rc;
  654. }
  655. rc |= ISL1219_REG_EV_EVEN;
  656. if (!of_property_read_u32(np, "isil,ev-evienb", &evienb)) {
  657. if (evienb)
  658. rc |= ISL1219_REG_EV_EVIENB;
  659. else
  660. rc &= ~ISL1219_REG_EV_EVIENB;
  661. }
  662. rc = i2c_smbus_write_byte_data(client, ISL1219_REG_EV, rc);
  663. if (rc < 0) {
  664. dev_err(&client->dev, "could not enable tamper detection\n");
  665. return rc;
  666. }
  667. rc = rtc_add_group(rtc, &isl1219_rtc_sysfs_files);
  668. if (rc)
  669. return rc;
  670. evdet_irq = of_irq_get_byname(np, "evdet");
  671. }
  672. rc = rtc_add_group(rtc, &isl1208_rtc_sysfs_files);
  673. if (rc)
  674. return rc;
  675. if (client->irq > 0)
  676. rc = isl1208_setup_irq(client, client->irq);
  677. if (rc)
  678. return rc;
  679. if (evdet_irq > 0 && evdet_irq != client->irq)
  680. rc = isl1208_setup_irq(client, evdet_irq);
  681. if (rc)
  682. return rc;
  683. return rtc_register_device(rtc);
  684. }
  685. static const struct i2c_device_id isl1208_id[] = {
  686. { "isl1208", TYPE_ISL1208 },
  687. { "isl1218", TYPE_ISL1218 },
  688. { "isl1219", TYPE_ISL1219 },
  689. { }
  690. };
  691. MODULE_DEVICE_TABLE(i2c, isl1208_id);
  692. static const struct of_device_id isl1208_of_match[] = {
  693. { .compatible = "isil,isl1208" },
  694. { .compatible = "isil,isl1218" },
  695. { .compatible = "isil,isl1219" },
  696. { }
  697. };
  698. MODULE_DEVICE_TABLE(of, isl1208_of_match);
  699. static struct i2c_driver isl1208_driver = {
  700. .driver = {
  701. .name = "rtc-isl1208",
  702. .of_match_table = of_match_ptr(isl1208_of_match),
  703. },
  704. .probe = isl1208_probe,
  705. .id_table = isl1208_id,
  706. };
  707. module_i2c_driver(isl1208_driver);
  708. MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
  709. MODULE_DESCRIPTION("Intersil ISL1208 RTC driver");
  710. MODULE_LICENSE("GPL");