rtc-isl1208.c 17 KB

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