rtc-isl1208.c 21 KB

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