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