rtc-isl1208.c 18 KB

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