rtc-isl12057.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * rtc-isl12057 - Driver for Intersil ISL12057 I2C Real Time Clock
  3. *
  4. * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org>
  5. *
  6. * This work is largely based on Intersil ISL1208 driver developed by
  7. * Hebert Valerio Riedel <hvr@gnu.org>.
  8. *
  9. * Detailed datasheet on which this development is based is available here:
  10. *
  11. * http://natisbad.org/NAS2/refs/ISL12057.pdf
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/rtc.h>
  26. #include <linux/i2c.h>
  27. #include <linux/bcd.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/regmap.h>
  31. #define DRV_NAME "rtc-isl12057"
  32. /* RTC section */
  33. #define ISL12057_REG_RTC_SC 0x00 /* Seconds */
  34. #define ISL12057_REG_RTC_MN 0x01 /* Minutes */
  35. #define ISL12057_REG_RTC_HR 0x02 /* Hours */
  36. #define ISL12057_REG_RTC_HR_PM BIT(5) /* AM/PM bit in 12h format */
  37. #define ISL12057_REG_RTC_HR_MIL BIT(6) /* 24h/12h format */
  38. #define ISL12057_REG_RTC_DW 0x03 /* Day of the Week */
  39. #define ISL12057_REG_RTC_DT 0x04 /* Date */
  40. #define ISL12057_REG_RTC_MO 0x05 /* Month */
  41. #define ISL12057_REG_RTC_MO_CEN BIT(7) /* Century bit */
  42. #define ISL12057_REG_RTC_YR 0x06 /* Year */
  43. #define ISL12057_RTC_SEC_LEN 7
  44. /* Alarm 1 section */
  45. #define ISL12057_REG_A1_SC 0x07 /* Alarm 1 Seconds */
  46. #define ISL12057_REG_A1_MN 0x08 /* Alarm 1 Minutes */
  47. #define ISL12057_REG_A1_HR 0x09 /* Alarm 1 Hours */
  48. #define ISL12057_REG_A1_HR_PM BIT(5) /* AM/PM bit in 12h format */
  49. #define ISL12057_REG_A1_HR_MIL BIT(6) /* 24h/12h format */
  50. #define ISL12057_REG_A1_DWDT 0x0A /* Alarm 1 Date / Day of the week */
  51. #define ISL12057_REG_A1_DWDT_B BIT(6) /* DW / DT selection bit */
  52. #define ISL12057_A1_SEC_LEN 4
  53. /* Alarm 2 section */
  54. #define ISL12057_REG_A2_MN 0x0B /* Alarm 2 Minutes */
  55. #define ISL12057_REG_A2_HR 0x0C /* Alarm 2 Hours */
  56. #define ISL12057_REG_A2_DWDT 0x0D /* Alarm 2 Date / Day of the week */
  57. #define ISL12057_A2_SEC_LEN 3
  58. /* Control/Status registers */
  59. #define ISL12057_REG_INT 0x0E
  60. #define ISL12057_REG_INT_A1IE BIT(0) /* Alarm 1 interrupt enable bit */
  61. #define ISL12057_REG_INT_A2IE BIT(1) /* Alarm 2 interrupt enable bit */
  62. #define ISL12057_REG_INT_INTCN BIT(2) /* Interrupt control enable bit */
  63. #define ISL12057_REG_INT_RS1 BIT(3) /* Freq out control bit 1 */
  64. #define ISL12057_REG_INT_RS2 BIT(4) /* Freq out control bit 2 */
  65. #define ISL12057_REG_INT_EOSC BIT(7) /* Oscillator enable bit */
  66. #define ISL12057_REG_SR 0x0F
  67. #define ISL12057_REG_SR_A1F BIT(0) /* Alarm 1 interrupt bit */
  68. #define ISL12057_REG_SR_A2F BIT(1) /* Alarm 2 interrupt bit */
  69. #define ISL12057_REG_SR_OSF BIT(7) /* Oscillator failure bit */
  70. /* Register memory map length */
  71. #define ISL12057_MEM_MAP_LEN 0x10
  72. struct isl12057_rtc_data {
  73. struct rtc_device *rtc;
  74. struct regmap *regmap;
  75. struct mutex lock;
  76. int irq;
  77. };
  78. static void isl12057_rtc_regs_to_tm(struct rtc_time *tm, u8 *regs)
  79. {
  80. tm->tm_sec = bcd2bin(regs[ISL12057_REG_RTC_SC]);
  81. tm->tm_min = bcd2bin(regs[ISL12057_REG_RTC_MN]);
  82. if (regs[ISL12057_REG_RTC_HR] & ISL12057_REG_RTC_HR_MIL) { /* AM/PM */
  83. tm->tm_hour = bcd2bin(regs[ISL12057_REG_RTC_HR] & 0x1f);
  84. if (regs[ISL12057_REG_RTC_HR] & ISL12057_REG_RTC_HR_PM)
  85. tm->tm_hour += 12;
  86. } else { /* 24 hour mode */
  87. tm->tm_hour = bcd2bin(regs[ISL12057_REG_RTC_HR] & 0x3f);
  88. }
  89. tm->tm_mday = bcd2bin(regs[ISL12057_REG_RTC_DT]);
  90. tm->tm_wday = bcd2bin(regs[ISL12057_REG_RTC_DW]) - 1; /* starts at 1 */
  91. tm->tm_mon = bcd2bin(regs[ISL12057_REG_RTC_MO] & 0x1f) - 1; /* ditto */
  92. tm->tm_year = bcd2bin(regs[ISL12057_REG_RTC_YR]) + 100;
  93. /* Check if years register has overflown from 99 to 00 */
  94. if (regs[ISL12057_REG_RTC_MO] & ISL12057_REG_RTC_MO_CEN)
  95. tm->tm_year += 100;
  96. }
  97. static int isl12057_rtc_tm_to_regs(u8 *regs, struct rtc_time *tm)
  98. {
  99. u8 century_bit;
  100. /*
  101. * The clock has an 8 bit wide bcd-coded register for the year.
  102. * It also has a century bit encoded in MO flag which provides
  103. * information about overflow of year register from 99 to 00.
  104. * tm_year is an offset from 1900 and we are interested in the
  105. * 2000-2199 range, so any value less than 100 or larger than
  106. * 299 is invalid.
  107. */
  108. if (tm->tm_year < 100 || tm->tm_year > 299)
  109. return -EINVAL;
  110. century_bit = (tm->tm_year > 199) ? ISL12057_REG_RTC_MO_CEN : 0;
  111. regs[ISL12057_REG_RTC_SC] = bin2bcd(tm->tm_sec);
  112. regs[ISL12057_REG_RTC_MN] = bin2bcd(tm->tm_min);
  113. regs[ISL12057_REG_RTC_HR] = bin2bcd(tm->tm_hour); /* 24-hour format */
  114. regs[ISL12057_REG_RTC_DT] = bin2bcd(tm->tm_mday);
  115. regs[ISL12057_REG_RTC_MO] = bin2bcd(tm->tm_mon + 1) | century_bit;
  116. regs[ISL12057_REG_RTC_YR] = bin2bcd(tm->tm_year % 100);
  117. regs[ISL12057_REG_RTC_DW] = bin2bcd(tm->tm_wday + 1);
  118. return 0;
  119. }
  120. /*
  121. * Try and match register bits w/ fixed null values to see whether we
  122. * are dealing with an ISL12057. Note: this function is called early
  123. * during init and hence does need mutex protection.
  124. */
  125. static int isl12057_i2c_validate_chip(struct regmap *regmap)
  126. {
  127. u8 regs[ISL12057_MEM_MAP_LEN];
  128. static const u8 mask[ISL12057_MEM_MAP_LEN] = { 0x80, 0x80, 0x80, 0xf8,
  129. 0xc0, 0x60, 0x00, 0x00,
  130. 0x00, 0x00, 0x00, 0x00,
  131. 0x00, 0x00, 0x60, 0x7c };
  132. int ret, i;
  133. ret = regmap_bulk_read(regmap, 0, regs, ISL12057_MEM_MAP_LEN);
  134. if (ret)
  135. return ret;
  136. for (i = 0; i < ISL12057_MEM_MAP_LEN; ++i) {
  137. if (regs[i] & mask[i]) /* check if bits are cleared */
  138. return -ENODEV;
  139. }
  140. return 0;
  141. }
  142. static int _isl12057_rtc_clear_alarm(struct device *dev)
  143. {
  144. struct isl12057_rtc_data *data = dev_get_drvdata(dev);
  145. int ret;
  146. ret = regmap_update_bits(data->regmap, ISL12057_REG_SR,
  147. ISL12057_REG_SR_A1F, 0);
  148. if (ret)
  149. dev_err(dev, "%s: clearing alarm failed (%d)\n", __func__, ret);
  150. return ret;
  151. }
  152. static int _isl12057_rtc_update_alarm(struct device *dev, int enable)
  153. {
  154. struct isl12057_rtc_data *data = dev_get_drvdata(dev);
  155. int ret;
  156. ret = regmap_update_bits(data->regmap, ISL12057_REG_INT,
  157. ISL12057_REG_INT_A1IE,
  158. enable ? ISL12057_REG_INT_A1IE : 0);
  159. if (ret)
  160. dev_err(dev, "%s: changing alarm interrupt flag failed (%d)\n",
  161. __func__, ret);
  162. return ret;
  163. }
  164. /*
  165. * Note: as we only read from device and do not perform any update, there is
  166. * no need for an equivalent function which would try and get driver's main
  167. * lock. Here, it is safe for everyone if we just use regmap internal lock
  168. * on the device when reading.
  169. */
  170. static int _isl12057_rtc_read_time(struct device *dev, struct rtc_time *tm)
  171. {
  172. struct isl12057_rtc_data *data = dev_get_drvdata(dev);
  173. u8 regs[ISL12057_RTC_SEC_LEN];
  174. unsigned int sr;
  175. int ret;
  176. ret = regmap_read(data->regmap, ISL12057_REG_SR, &sr);
  177. if (ret) {
  178. dev_err(dev, "%s: unable to read oscillator status flag (%d)\n",
  179. __func__, ret);
  180. goto out;
  181. } else {
  182. if (sr & ISL12057_REG_SR_OSF) {
  183. ret = -ENODATA;
  184. goto out;
  185. }
  186. }
  187. ret = regmap_bulk_read(data->regmap, ISL12057_REG_RTC_SC, regs,
  188. ISL12057_RTC_SEC_LEN);
  189. if (ret)
  190. dev_err(dev, "%s: unable to read RTC time section (%d)\n",
  191. __func__, ret);
  192. out:
  193. if (ret)
  194. return ret;
  195. isl12057_rtc_regs_to_tm(tm, regs);
  196. return rtc_valid_tm(tm);
  197. }
  198. static int isl12057_rtc_update_alarm(struct device *dev, int enable)
  199. {
  200. struct isl12057_rtc_data *data = dev_get_drvdata(dev);
  201. int ret;
  202. mutex_lock(&data->lock);
  203. ret = _isl12057_rtc_update_alarm(dev, enable);
  204. mutex_unlock(&data->lock);
  205. return ret;
  206. }
  207. static int isl12057_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  208. {
  209. struct isl12057_rtc_data *data = dev_get_drvdata(dev);
  210. struct rtc_time rtc_tm, *alarm_tm = &alarm->time;
  211. unsigned long rtc_secs, alarm_secs;
  212. u8 regs[ISL12057_A1_SEC_LEN];
  213. unsigned int ir;
  214. int ret;
  215. mutex_lock(&data->lock);
  216. ret = regmap_bulk_read(data->regmap, ISL12057_REG_A1_SC, regs,
  217. ISL12057_A1_SEC_LEN);
  218. if (ret) {
  219. dev_err(dev, "%s: reading alarm section failed (%d)\n",
  220. __func__, ret);
  221. goto err_unlock;
  222. }
  223. alarm_tm->tm_sec = bcd2bin(regs[0] & 0x7f);
  224. alarm_tm->tm_min = bcd2bin(regs[1] & 0x7f);
  225. alarm_tm->tm_hour = bcd2bin(regs[2] & 0x3f);
  226. alarm_tm->tm_mday = bcd2bin(regs[3] & 0x3f);
  227. alarm_tm->tm_wday = -1;
  228. /*
  229. * The alarm section does not store year/month. We use the ones in rtc
  230. * section as a basis and increment month and then year if needed to get
  231. * alarm after current time.
  232. */
  233. ret = _isl12057_rtc_read_time(dev, &rtc_tm);
  234. if (ret)
  235. goto err_unlock;
  236. alarm_tm->tm_year = rtc_tm.tm_year;
  237. alarm_tm->tm_mon = rtc_tm.tm_mon;
  238. ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
  239. if (ret)
  240. goto err_unlock;
  241. ret = rtc_tm_to_time(alarm_tm, &alarm_secs);
  242. if (ret)
  243. goto err_unlock;
  244. if (alarm_secs < rtc_secs) {
  245. if (alarm_tm->tm_mon == 11) {
  246. alarm_tm->tm_mon = 0;
  247. alarm_tm->tm_year += 1;
  248. } else {
  249. alarm_tm->tm_mon += 1;
  250. }
  251. }
  252. ret = regmap_read(data->regmap, ISL12057_REG_INT, &ir);
  253. if (ret) {
  254. dev_err(dev, "%s: reading alarm interrupt flag failed (%d)\n",
  255. __func__, ret);
  256. goto err_unlock;
  257. }
  258. alarm->enabled = !!(ir & ISL12057_REG_INT_A1IE);
  259. err_unlock:
  260. mutex_unlock(&data->lock);
  261. return ret;
  262. }
  263. static int isl12057_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  264. {
  265. struct isl12057_rtc_data *data = dev_get_drvdata(dev);
  266. struct rtc_time *alarm_tm = &alarm->time;
  267. unsigned long rtc_secs, alarm_secs;
  268. u8 regs[ISL12057_A1_SEC_LEN];
  269. struct rtc_time rtc_tm;
  270. int ret, enable = 1;
  271. mutex_lock(&data->lock);
  272. ret = _isl12057_rtc_read_time(dev, &rtc_tm);
  273. if (ret)
  274. goto err_unlock;
  275. ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
  276. if (ret)
  277. goto err_unlock;
  278. ret = rtc_tm_to_time(alarm_tm, &alarm_secs);
  279. if (ret)
  280. goto err_unlock;
  281. /* If alarm time is before current time, disable the alarm */
  282. if (!alarm->enabled || alarm_secs <= rtc_secs) {
  283. enable = 0;
  284. } else {
  285. /*
  286. * Chip only support alarms up to one month in the future. Let's
  287. * return an error if we get something after that limit.
  288. * Comparison is done by incrementing rtc_tm month field by one
  289. * and checking alarm value is still below.
  290. */
  291. if (rtc_tm.tm_mon == 11) { /* handle year wrapping */
  292. rtc_tm.tm_mon = 0;
  293. rtc_tm.tm_year += 1;
  294. } else {
  295. rtc_tm.tm_mon += 1;
  296. }
  297. ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
  298. if (ret)
  299. goto err_unlock;
  300. if (alarm_secs > rtc_secs) {
  301. dev_err(dev, "%s: max for alarm is one month (%d)\n",
  302. __func__, ret);
  303. ret = -EINVAL;
  304. goto err_unlock;
  305. }
  306. }
  307. /* Disable the alarm before modifying it */
  308. ret = _isl12057_rtc_update_alarm(dev, 0);
  309. if (ret < 0) {
  310. dev_err(dev, "%s: unable to disable the alarm (%d)\n",
  311. __func__, ret);
  312. goto err_unlock;
  313. }
  314. /* Program alarm registers */
  315. regs[0] = bin2bcd(alarm_tm->tm_sec) & 0x7f;
  316. regs[1] = bin2bcd(alarm_tm->tm_min) & 0x7f;
  317. regs[2] = bin2bcd(alarm_tm->tm_hour) & 0x3f;
  318. regs[3] = bin2bcd(alarm_tm->tm_mday) & 0x3f;
  319. ret = regmap_bulk_write(data->regmap, ISL12057_REG_A1_SC, regs,
  320. ISL12057_A1_SEC_LEN);
  321. if (ret < 0) {
  322. dev_err(dev, "%s: writing alarm section failed (%d)\n",
  323. __func__, ret);
  324. goto err_unlock;
  325. }
  326. /* Enable or disable alarm */
  327. ret = _isl12057_rtc_update_alarm(dev, enable);
  328. err_unlock:
  329. mutex_unlock(&data->lock);
  330. return ret;
  331. }
  332. static int isl12057_rtc_set_time(struct device *dev, struct rtc_time *tm)
  333. {
  334. struct isl12057_rtc_data *data = dev_get_drvdata(dev);
  335. u8 regs[ISL12057_RTC_SEC_LEN];
  336. int ret;
  337. ret = isl12057_rtc_tm_to_regs(regs, tm);
  338. if (ret)
  339. return ret;
  340. mutex_lock(&data->lock);
  341. ret = regmap_bulk_write(data->regmap, ISL12057_REG_RTC_SC, regs,
  342. ISL12057_RTC_SEC_LEN);
  343. if (ret) {
  344. dev_err(dev, "%s: unable to write RTC time section (%d)\n",
  345. __func__, ret);
  346. goto out;
  347. }
  348. /*
  349. * Now that RTC time has been updated, let's clear oscillator
  350. * failure flag, if needed.
  351. */
  352. ret = regmap_update_bits(data->regmap, ISL12057_REG_SR,
  353. ISL12057_REG_SR_OSF, 0);
  354. if (ret < 0)
  355. dev_err(dev, "%s: unable to clear osc. failure bit (%d)\n",
  356. __func__, ret);
  357. out:
  358. mutex_unlock(&data->lock);
  359. return ret;
  360. }
  361. /*
  362. * Check current RTC status and enable/disable what needs to be. Return 0 if
  363. * everything went ok and a negative value upon error. Note: this function
  364. * is called early during init and hence does need mutex protection.
  365. */
  366. static int isl12057_check_rtc_status(struct device *dev, struct regmap *regmap)
  367. {
  368. int ret;
  369. /* Enable oscillator if not already running */
  370. ret = regmap_update_bits(regmap, ISL12057_REG_INT,
  371. ISL12057_REG_INT_EOSC, 0);
  372. if (ret < 0) {
  373. dev_err(dev, "%s: unable to enable oscillator (%d)\n",
  374. __func__, ret);
  375. return ret;
  376. }
  377. /* Clear alarm bit if needed */
  378. ret = regmap_update_bits(regmap, ISL12057_REG_SR,
  379. ISL12057_REG_SR_A1F, 0);
  380. if (ret < 0) {
  381. dev_err(dev, "%s: unable to clear alarm bit (%d)\n",
  382. __func__, ret);
  383. return ret;
  384. }
  385. return 0;
  386. }
  387. #ifdef CONFIG_OF
  388. /*
  389. * One would expect the device to be marked as a wakeup source only
  390. * when an IRQ pin of the RTC is routed to an interrupt line of the
  391. * CPU. In practice, such an IRQ pin can be connected to a PMIC and
  392. * this allows the device to be powered up when RTC alarm rings. This
  393. * is for instance the case on ReadyNAS 102, 104 and 2120. On those
  394. * devices with no IRQ driectly connected to the SoC, the RTC chip
  395. * can be forced as a wakeup source by stating that explicitly in
  396. * the device's .dts file using the "wakeup-source" boolean property.
  397. * This will guarantee 'wakealarm' sysfs entry is available on the device.
  398. *
  399. * The function below returns 1, i.e. the capability of the chip to
  400. * wakeup the device, based on IRQ availability or if the boolean
  401. * property has been set in the .dts file. Otherwise, it returns 0.
  402. */
  403. static bool isl12057_can_wakeup_machine(struct device *dev)
  404. {
  405. struct isl12057_rtc_data *data = dev_get_drvdata(dev);
  406. return data->irq || of_property_read_bool(dev->of_node, "wakeup-source")
  407. || of_property_read_bool(dev->of_node, /* legacy */
  408. "isil,irq2-can-wakeup-machine");
  409. }
  410. #else
  411. static bool isl12057_can_wakeup_machine(struct device *dev)
  412. {
  413. struct isl12057_rtc_data *data = dev_get_drvdata(dev);
  414. return !!data->irq;
  415. }
  416. #endif
  417. static int isl12057_rtc_alarm_irq_enable(struct device *dev,
  418. unsigned int enable)
  419. {
  420. struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev);
  421. int ret = -ENOTTY;
  422. if (rtc_data->irq)
  423. ret = isl12057_rtc_update_alarm(dev, enable);
  424. return ret;
  425. }
  426. static irqreturn_t isl12057_rtc_interrupt(int irq, void *data)
  427. {
  428. struct i2c_client *client = data;
  429. struct isl12057_rtc_data *rtc_data = dev_get_drvdata(&client->dev);
  430. struct rtc_device *rtc = rtc_data->rtc;
  431. int ret, handled = IRQ_NONE;
  432. unsigned int sr;
  433. ret = regmap_read(rtc_data->regmap, ISL12057_REG_SR, &sr);
  434. if (!ret && (sr & ISL12057_REG_SR_A1F)) {
  435. dev_dbg(&client->dev, "RTC alarm!\n");
  436. rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
  437. /* Acknowledge and disable the alarm */
  438. _isl12057_rtc_clear_alarm(&client->dev);
  439. _isl12057_rtc_update_alarm(&client->dev, 0);
  440. handled = IRQ_HANDLED;
  441. }
  442. return handled;
  443. }
  444. static const struct rtc_class_ops rtc_ops = {
  445. .read_time = _isl12057_rtc_read_time,
  446. .set_time = isl12057_rtc_set_time,
  447. .read_alarm = isl12057_rtc_read_alarm,
  448. .set_alarm = isl12057_rtc_set_alarm,
  449. .alarm_irq_enable = isl12057_rtc_alarm_irq_enable,
  450. };
  451. static const struct regmap_config isl12057_rtc_regmap_config = {
  452. .reg_bits = 8,
  453. .val_bits = 8,
  454. };
  455. static int isl12057_probe(struct i2c_client *client,
  456. const struct i2c_device_id *id)
  457. {
  458. struct device *dev = &client->dev;
  459. struct isl12057_rtc_data *data;
  460. struct regmap *regmap;
  461. int ret;
  462. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
  463. I2C_FUNC_SMBUS_BYTE_DATA |
  464. I2C_FUNC_SMBUS_I2C_BLOCK))
  465. return -ENODEV;
  466. regmap = devm_regmap_init_i2c(client, &isl12057_rtc_regmap_config);
  467. if (IS_ERR(regmap)) {
  468. ret = PTR_ERR(regmap);
  469. dev_err(dev, "%s: regmap allocation failed (%d)\n",
  470. __func__, ret);
  471. return ret;
  472. }
  473. ret = isl12057_i2c_validate_chip(regmap);
  474. if (ret)
  475. return ret;
  476. ret = isl12057_check_rtc_status(dev, regmap);
  477. if (ret)
  478. return ret;
  479. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  480. if (!data)
  481. return -ENOMEM;
  482. mutex_init(&data->lock);
  483. data->regmap = regmap;
  484. dev_set_drvdata(dev, data);
  485. if (client->irq > 0) {
  486. ret = devm_request_threaded_irq(dev, client->irq, NULL,
  487. isl12057_rtc_interrupt,
  488. IRQF_SHARED|IRQF_ONESHOT,
  489. DRV_NAME, client);
  490. if (!ret)
  491. data->irq = client->irq;
  492. else
  493. dev_err(dev, "%s: irq %d unavailable (%d)\n", __func__,
  494. client->irq, ret);
  495. }
  496. if (isl12057_can_wakeup_machine(dev))
  497. device_init_wakeup(dev, true);
  498. data->rtc = devm_rtc_device_register(dev, DRV_NAME, &rtc_ops,
  499. THIS_MODULE);
  500. ret = PTR_ERR_OR_ZERO(data->rtc);
  501. if (ret) {
  502. dev_err(dev, "%s: unable to register RTC device (%d)\n",
  503. __func__, ret);
  504. goto err;
  505. }
  506. /* We cannot support UIE mode if we do not have an IRQ line */
  507. if (!data->irq)
  508. data->rtc->uie_unsupported = 1;
  509. err:
  510. return ret;
  511. }
  512. static int isl12057_remove(struct i2c_client *client)
  513. {
  514. if (isl12057_can_wakeup_machine(&client->dev))
  515. device_init_wakeup(&client->dev, false);
  516. return 0;
  517. }
  518. #ifdef CONFIG_PM_SLEEP
  519. static int isl12057_rtc_suspend(struct device *dev)
  520. {
  521. struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev);
  522. if (rtc_data->irq && device_may_wakeup(dev))
  523. return enable_irq_wake(rtc_data->irq);
  524. return 0;
  525. }
  526. static int isl12057_rtc_resume(struct device *dev)
  527. {
  528. struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev);
  529. if (rtc_data->irq && device_may_wakeup(dev))
  530. return disable_irq_wake(rtc_data->irq);
  531. return 0;
  532. }
  533. #endif
  534. static SIMPLE_DEV_PM_OPS(isl12057_rtc_pm_ops, isl12057_rtc_suspend,
  535. isl12057_rtc_resume);
  536. #ifdef CONFIG_OF
  537. static const struct of_device_id isl12057_dt_match[] = {
  538. { .compatible = "isl,isl12057" }, /* for backward compat., don't use */
  539. { .compatible = "isil,isl12057" },
  540. { },
  541. };
  542. MODULE_DEVICE_TABLE(of, isl12057_dt_match);
  543. #endif
  544. static const struct i2c_device_id isl12057_id[] = {
  545. { "isl12057", 0 },
  546. { }
  547. };
  548. MODULE_DEVICE_TABLE(i2c, isl12057_id);
  549. static struct i2c_driver isl12057_driver = {
  550. .driver = {
  551. .name = DRV_NAME,
  552. .pm = &isl12057_rtc_pm_ops,
  553. .of_match_table = of_match_ptr(isl12057_dt_match),
  554. },
  555. .probe = isl12057_probe,
  556. .remove = isl12057_remove,
  557. .id_table = isl12057_id,
  558. };
  559. module_i2c_driver(isl12057_driver);
  560. MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
  561. MODULE_DESCRIPTION("Intersil ISL12057 RTC driver");
  562. MODULE_LICENSE("GPL");