rtc-isl1208.c 25 KB

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