rtc-isl1208.c 17 KB

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