rtc-isl1208.c 23 KB

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