blob: 3ec73ad7f2d89686757e38611f665e8a6b4585d8 [file] [log] [blame]
Arnaud Ebalard70e12332013-12-19 23:27:28 +01001/*
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>
Arnaud Ebalard70e12332013-12-19 23:27:28 +010029#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 */
Arnaud Ebalardb5f41842014-12-10 15:54:05 -080044#define ISL12057_REG_RTC_MO_CEN BIT(7) /* Century bit */
Arnaud Ebalard70e12332013-12-19 23:27:28 +010045#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
81struct isl12057_rtc_data {
Arnaud Ebalardfd714932015-02-13 14:40:32 -080082 struct rtc_device *rtc;
Arnaud Ebalard70e12332013-12-19 23:27:28 +010083 struct regmap *regmap;
84 struct mutex lock;
Arnaud Ebalardfd714932015-02-13 14:40:32 -080085 int irq;
Arnaud Ebalard70e12332013-12-19 23:27:28 +010086};
87
88static 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 */
Arnaud Ebalard5945b282014-12-10 15:54:02 -080094 tm->tm_hour = bcd2bin(regs[ISL12057_REG_RTC_HR] & 0x1f);
Arnaud Ebalard70e12332013-12-19 23:27:28 +010095 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 */
Arnaud Ebalard5945b282014-12-10 15:54:02 -0800103 tm->tm_mon = bcd2bin(regs[ISL12057_REG_RTC_MO] & 0x1f) - 1; /* ditto */
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100104 tm->tm_year = bcd2bin(regs[ISL12057_REG_RTC_YR]) + 100;
Arnaud Ebalardb5f41842014-12-10 15:54:05 -0800105
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;
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100109}
110
111static int isl12057_rtc_tm_to_regs(u8 *regs, struct rtc_time *tm)
112{
Arnaud Ebalardb5f41842014-12-10 15:54:05 -0800113 u8 century_bit;
114
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100115 /*
116 * The clock has an 8 bit wide bcd-coded register for the year.
Arnaud Ebalardb5f41842014-12-10 15:54:05 -0800117 * It also has a century bit encoded in MO flag which provides
118 * information about overflow of year register from 99 to 00.
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100119 * tm_year is an offset from 1900 and we are interested in the
Arnaud Ebalardb5f41842014-12-10 15:54:05 -0800120 * 2000-2199 range, so any value less than 100 or larger than
121 * 299 is invalid.
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100122 */
Arnaud Ebalardb5f41842014-12-10 15:54:05 -0800123 if (tm->tm_year < 100 || tm->tm_year > 299)
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100124 return -EINVAL;
125
Arnaud Ebalardb5f41842014-12-10 15:54:05 -0800126 century_bit = (tm->tm_year > 199) ? ISL12057_REG_RTC_MO_CEN : 0;
127
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100128 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);
Arnaud Ebalardb5f41842014-12-10 15:54:05 -0800132 regs[ISL12057_REG_RTC_MO] = bin2bcd(tm->tm_mon + 1) | century_bit;
133 regs[ISL12057_REG_RTC_YR] = bin2bcd(tm->tm_year % 100);
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100134 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 */
144static 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
Arnaud Ebalardfd714932015-02-13 14:40:32 -0800165static 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
178static 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 */
199static int _isl12057_rtc_read_time(struct device *dev, struct rtc_time *tm)
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100200{
201 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
202 u8 regs[ISL12057_RTC_SEC_LEN];
Arnaud Ebalard10df1e62014-12-10 15:54:08 -0800203 unsigned int sr;
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100204 int ret;
205
Arnaud Ebalard10df1e62014-12-10 15:54:08 -0800206 ret = regmap_read(data->regmap, ISL12057_REG_SR, &sr);
207 if (ret) {
Arnaud Ebalardcf67d0b2014-12-10 15:54:11 -0800208 dev_err(dev, "%s: unable to read oscillator status flag (%d)\n",
209 __func__, ret);
Arnaud Ebalard10df1e62014-12-10 15:54:08 -0800210 goto out;
211 } else {
212 if (sr & ISL12057_REG_SR_OSF) {
213 ret = -ENODATA;
214 goto out;
215 }
216 }
217
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100218 ret = regmap_bulk_read(data->regmap, ISL12057_REG_RTC_SC, regs,
219 ISL12057_RTC_SEC_LEN);
Arnaud Ebalard10df1e62014-12-10 15:54:08 -0800220 if (ret)
Arnaud Ebalardcf67d0b2014-12-10 15:54:11 -0800221 dev_err(dev, "%s: unable to read RTC time section (%d)\n",
222 __func__, ret);
Arnaud Ebalard10df1e62014-12-10 15:54:08 -0800223
224out:
Arnaud Ebalard10df1e62014-12-10 15:54:08 -0800225 if (ret)
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100226 return ret;
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100227
228 isl12057_rtc_regs_to_tm(tm, regs);
229
230 return rtc_valid_tm(tm);
231}
232
Arnaud Ebalardfd714932015-02-13 14:40:32 -0800233static 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
245static int isl12057_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
246{
247 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
248 struct rtc_time rtc_tm, *alarm_tm = &alarm->time;
249 unsigned long rtc_secs, alarm_secs;
250 u8 regs[ISL12057_A1_SEC_LEN];
251 unsigned int ir;
252 int ret;
253
254 mutex_lock(&data->lock);
255 ret = regmap_bulk_read(data->regmap, ISL12057_REG_A1_SC, regs,
256 ISL12057_A1_SEC_LEN);
257 if (ret) {
258 dev_err(dev, "%s: reading alarm section failed (%d)\n",
259 __func__, ret);
260 goto err_unlock;
261 }
262
263 alarm_tm->tm_sec = bcd2bin(regs[0] & 0x7f);
264 alarm_tm->tm_min = bcd2bin(regs[1] & 0x7f);
265 alarm_tm->tm_hour = bcd2bin(regs[2] & 0x3f);
266 alarm_tm->tm_mday = bcd2bin(regs[3] & 0x3f);
267 alarm_tm->tm_wday = -1;
268
269 /*
270 * The alarm section does not store year/month. We use the ones in rtc
271 * section as a basis and increment month and then year if needed to get
272 * alarm after current time.
273 */
274 ret = _isl12057_rtc_read_time(dev, &rtc_tm);
275 if (ret)
276 goto err_unlock;
277
278 alarm_tm->tm_year = rtc_tm.tm_year;
279 alarm_tm->tm_mon = rtc_tm.tm_mon;
280
281 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
282 if (ret)
283 goto err_unlock;
284
285 ret = rtc_tm_to_time(alarm_tm, &alarm_secs);
286 if (ret)
287 goto err_unlock;
288
289 if (alarm_secs < rtc_secs) {
290 if (alarm_tm->tm_mon == 11) {
291 alarm_tm->tm_mon = 0;
292 alarm_tm->tm_year += 1;
293 } else {
294 alarm_tm->tm_mon += 1;
295 }
296 }
297
298 ret = regmap_read(data->regmap, ISL12057_REG_INT, &ir);
299 if (ret) {
300 dev_err(dev, "%s: reading alarm interrupt flag failed (%d)\n",
301 __func__, ret);
302 goto err_unlock;
303 }
304
305 alarm->enabled = !!(ir & ISL12057_REG_INT_A1IE);
306
307err_unlock:
308 mutex_unlock(&data->lock);
309
310 return ret;
311}
312
313static int isl12057_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
314{
315 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
316 struct rtc_time *alarm_tm = &alarm->time;
317 unsigned long rtc_secs, alarm_secs;
318 u8 regs[ISL12057_A1_SEC_LEN];
319 struct rtc_time rtc_tm;
320 int ret, enable = 1;
321
322 mutex_lock(&data->lock);
323 ret = _isl12057_rtc_read_time(dev, &rtc_tm);
324 if (ret)
325 goto err_unlock;
326
327 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
328 if (ret)
329 goto err_unlock;
330
331 ret = rtc_tm_to_time(alarm_tm, &alarm_secs);
332 if (ret)
333 goto err_unlock;
334
335 /* If alarm time is before current time, disable the alarm */
336 if (!alarm->enabled || alarm_secs <= rtc_secs) {
337 enable = 0;
338 } else {
339 /*
340 * Chip only support alarms up to one month in the future. Let's
341 * return an error if we get something after that limit.
342 * Comparison is done by incrementing rtc_tm month field by one
343 * and checking alarm value is still below.
344 */
345 if (rtc_tm.tm_mon == 11) { /* handle year wrapping */
346 rtc_tm.tm_mon = 0;
347 rtc_tm.tm_year += 1;
348 } else {
349 rtc_tm.tm_mon += 1;
350 }
351
352 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
353 if (ret)
354 goto err_unlock;
355
356 if (alarm_secs > rtc_secs) {
357 dev_err(dev, "%s: max for alarm is one month (%d)\n",
358 __func__, ret);
359 ret = -EINVAL;
360 goto err_unlock;
361 }
362 }
363
364 /* Disable the alarm before modifying it */
365 ret = _isl12057_rtc_update_alarm(dev, 0);
366 if (ret < 0) {
367 dev_err(dev, "%s: unable to disable the alarm (%d)\n",
368 __func__, ret);
369 goto err_unlock;
370 }
371
372 /* Program alarm registers */
373 regs[0] = bin2bcd(alarm_tm->tm_sec) & 0x7f;
374 regs[1] = bin2bcd(alarm_tm->tm_min) & 0x7f;
375 regs[2] = bin2bcd(alarm_tm->tm_hour) & 0x3f;
376 regs[3] = bin2bcd(alarm_tm->tm_mday) & 0x3f;
377
378 ret = regmap_bulk_write(data->regmap, ISL12057_REG_A1_SC, regs,
379 ISL12057_A1_SEC_LEN);
380 if (ret < 0) {
381 dev_err(dev, "%s: writing alarm section failed (%d)\n",
382 __func__, ret);
383 goto err_unlock;
384 }
385
386 /* Enable or disable alarm */
387 ret = _isl12057_rtc_update_alarm(dev, enable);
388
389err_unlock:
390 mutex_unlock(&data->lock);
391
392 return ret;
393}
394
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100395static int isl12057_rtc_set_time(struct device *dev, struct rtc_time *tm)
396{
397 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
398 u8 regs[ISL12057_RTC_SEC_LEN];
399 int ret;
400
401 ret = isl12057_rtc_tm_to_regs(regs, tm);
402 if (ret)
403 return ret;
404
405 mutex_lock(&data->lock);
406 ret = regmap_bulk_write(data->regmap, ISL12057_REG_RTC_SC, regs,
407 ISL12057_RTC_SEC_LEN);
Arnaud Ebalard10df1e62014-12-10 15:54:08 -0800408 if (ret) {
Arnaud Ebalardcf67d0b2014-12-10 15:54:11 -0800409 dev_err(dev, "%s: unable to write RTC time section (%d)\n",
410 __func__, ret);
Arnaud Ebalard10df1e62014-12-10 15:54:08 -0800411 goto out;
412 }
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100413
Arnaud Ebalard10df1e62014-12-10 15:54:08 -0800414 /*
415 * Now that RTC time has been updated, let's clear oscillator
416 * failure flag, if needed.
417 */
418 ret = regmap_update_bits(data->regmap, ISL12057_REG_SR,
419 ISL12057_REG_SR_OSF, 0);
420 if (ret < 0)
Arnaud Ebalardcf67d0b2014-12-10 15:54:11 -0800421 dev_err(dev, "%s: unable to clear osc. failure bit (%d)\n",
422 __func__, ret);
Arnaud Ebalard10df1e62014-12-10 15:54:08 -0800423
424out:
425 mutex_unlock(&data->lock);
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100426
427 return ret;
428}
429
430/*
431 * Check current RTC status and enable/disable what needs to be. Return 0 if
432 * everything went ok and a negative value upon error. Note: this function
433 * is called early during init and hence does need mutex protection.
434 */
435static int isl12057_check_rtc_status(struct device *dev, struct regmap *regmap)
436{
437 int ret;
438
439 /* Enable oscillator if not already running */
440 ret = regmap_update_bits(regmap, ISL12057_REG_INT,
441 ISL12057_REG_INT_EOSC, 0);
442 if (ret < 0) {
Arnaud Ebalardcf67d0b2014-12-10 15:54:11 -0800443 dev_err(dev, "%s: unable to enable oscillator (%d)\n",
444 __func__, ret);
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100445 return ret;
446 }
447
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100448 /* Clear alarm bit if needed */
449 ret = regmap_update_bits(regmap, ISL12057_REG_SR,
450 ISL12057_REG_SR_A1F, 0);
451 if (ret < 0) {
Arnaud Ebalardcf67d0b2014-12-10 15:54:11 -0800452 dev_err(dev, "%s: unable to clear alarm bit (%d)\n",
453 __func__, ret);
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100454 return ret;
455 }
456
457 return 0;
458}
459
Arnaud Ebalardfd714932015-02-13 14:40:32 -0800460static int isl12057_rtc_alarm_irq_enable(struct device *dev,
461 unsigned int enable)
462{
463 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev);
464 int ret = -ENOTTY;
465
466 if (rtc_data->irq)
467 ret = isl12057_rtc_update_alarm(dev, enable);
468
469 return ret;
470}
471
472static irqreturn_t isl12057_rtc_interrupt(int irq, void *data)
473{
474 struct i2c_client *client = data;
475 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(&client->dev);
476 struct rtc_device *rtc = rtc_data->rtc;
477 int ret, handled = IRQ_NONE;
478 unsigned int sr;
479
480 ret = regmap_read(rtc_data->regmap, ISL12057_REG_SR, &sr);
481 if (!ret && (sr & ISL12057_REG_SR_A1F)) {
482 dev_dbg(&client->dev, "RTC alarm!\n");
483
484 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
485
486 /* Acknowledge and disable the alarm */
487 _isl12057_rtc_clear_alarm(&client->dev);
488 _isl12057_rtc_update_alarm(&client->dev, 0);
489
490 handled = IRQ_HANDLED;
491 }
492
493 return handled;
494}
495
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100496static const struct rtc_class_ops rtc_ops = {
Arnaud Ebalardfd714932015-02-13 14:40:32 -0800497 .read_time = _isl12057_rtc_read_time,
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100498 .set_time = isl12057_rtc_set_time,
Arnaud Ebalardfd714932015-02-13 14:40:32 -0800499 .read_alarm = isl12057_rtc_read_alarm,
500 .set_alarm = isl12057_rtc_set_alarm,
501 .alarm_irq_enable = isl12057_rtc_alarm_irq_enable,
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100502};
503
504static struct regmap_config isl12057_rtc_regmap_config = {
505 .reg_bits = 8,
506 .val_bits = 8,
507};
508
509static int isl12057_probe(struct i2c_client *client,
510 const struct i2c_device_id *id)
511{
512 struct device *dev = &client->dev;
513 struct isl12057_rtc_data *data;
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100514 struct regmap *regmap;
515 int ret;
516
517 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
518 I2C_FUNC_SMBUS_BYTE_DATA |
519 I2C_FUNC_SMBUS_I2C_BLOCK))
520 return -ENODEV;
521
522 regmap = devm_regmap_init_i2c(client, &isl12057_rtc_regmap_config);
523 if (IS_ERR(regmap)) {
524 ret = PTR_ERR(regmap);
Arnaud Ebalardcf67d0b2014-12-10 15:54:11 -0800525 dev_err(dev, "%s: regmap allocation failed (%d)\n",
526 __func__, ret);
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100527 return ret;
528 }
529
530 ret = isl12057_i2c_validate_chip(regmap);
531 if (ret)
532 return ret;
533
534 ret = isl12057_check_rtc_status(dev, regmap);
535 if (ret)
536 return ret;
537
538 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
539 if (!data)
540 return -ENOMEM;
541
542 mutex_init(&data->lock);
543 data->regmap = regmap;
544 dev_set_drvdata(dev, data);
545
Arnaud Ebalardfd714932015-02-13 14:40:32 -0800546 if (client->irq > 0) {
547 ret = devm_request_threaded_irq(dev, client->irq, NULL,
548 isl12057_rtc_interrupt,
549 IRQF_SHARED|IRQF_ONESHOT,
550 DRV_NAME, client);
551 if (!ret)
552 data->irq = client->irq;
553 else
554 dev_err(dev, "%s: irq %d unavailable (%d)\n", __func__,
555 client->irq, ret);
556 }
557
558 device_init_wakeup(dev, !!data->irq);
559
560 data->rtc = devm_rtc_device_register(dev, DRV_NAME, &rtc_ops,
561 THIS_MODULE);
562 ret = PTR_ERR_OR_ZERO(data->rtc);
563 if (ret) {
564 dev_err(dev, "%s: unable to register RTC device (%d)\n",
565 __func__, ret);
566 goto err;
567 }
568
569 /* We cannot support UIE mode if we do not have an IRQ line */
570 if (!data->irq)
571 data->rtc->uie_unsupported = 1;
572
573err:
574 return ret;
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100575}
576
Arnaud Ebalardfd714932015-02-13 14:40:32 -0800577static int isl12057_remove(struct i2c_client *client)
578{
579 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(&client->dev);
580
581 if (rtc_data->irq)
582 device_init_wakeup(&client->dev, false);
583
584 return 0;
585}
586
587#ifdef CONFIG_PM_SLEEP
588static int isl12057_rtc_suspend(struct device *dev)
589{
590 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev);
591
592 if (device_may_wakeup(dev))
593 return enable_irq_wake(rtc_data->irq);
594
595 return 0;
596}
597
598static int isl12057_rtc_resume(struct device *dev)
599{
600 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev);
601
602 if (device_may_wakeup(dev))
603 return disable_irq_wake(rtc_data->irq);
604
605 return 0;
606}
607#endif
608
609static SIMPLE_DEV_PM_OPS(isl12057_rtc_pm_ops, isl12057_rtc_suspend,
610 isl12057_rtc_resume);
611
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100612#ifdef CONFIG_OF
Jingoo Han8c5bfac2014-06-06 14:35:56 -0700613static const struct of_device_id isl12057_dt_match[] = {
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100614 { .compatible = "isl,isl12057" },
615 { },
616};
617#endif
618
619static const struct i2c_device_id isl12057_id[] = {
620 { "isl12057", 0 },
621 { }
622};
623MODULE_DEVICE_TABLE(i2c, isl12057_id);
624
625static struct i2c_driver isl12057_driver = {
626 .driver = {
627 .name = DRV_NAME,
628 .owner = THIS_MODULE,
Arnaud Ebalardfd714932015-02-13 14:40:32 -0800629 .pm = &isl12057_rtc_pm_ops,
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100630 .of_match_table = of_match_ptr(isl12057_dt_match),
631 },
632 .probe = isl12057_probe,
Arnaud Ebalardfd714932015-02-13 14:40:32 -0800633 .remove = isl12057_remove,
Arnaud Ebalard70e12332013-12-19 23:27:28 +0100634 .id_table = isl12057_id,
635};
636module_i2c_driver(isl12057_driver);
637
638MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
639MODULE_DESCRIPTION("Intersil ISL12057 RTC driver");
640MODULE_LICENSE("GPL");