rtc-isl1208.c 17 KB

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