2 * rtc-isl12057 - Driver for Intersil ISL12057 I2C Real Time Clock
4 * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org>
6 * This work is largely based on Intersil ISL1208 driver developed by
7 * Hebert Valerio Riedel <hvr@gnu.org>.
9 * Detailed datasheet on which this development is based is available here:
11 * http://natisbad.org/NAS2/refs/ISL12057.pdf
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.
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.
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>
30 #include <linux/of_device.h>
31 #include <linux/regmap.h>
33 #define DRV_NAME "rtc-isl12057"
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 */
44 #define ISL12057_REG_RTC_MO_CEN BIT(7) /* Century bit */
45 #define ISL12057_REG_RTC_YR 0x06 /* Year */
46 #define ISL12057_RTC_SEC_LEN 7
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
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
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 */
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 */
78 /* Register memory map length */
79 #define ISL12057_MEM_MAP_LEN 0x10
81 struct isl12057_rtc_data
{
82 struct rtc_device
*rtc
;
83 struct regmap
*regmap
;
88 static void isl12057_rtc_regs_to_tm(struct rtc_time
*tm
, u8
*regs
)
90 tm
->tm_sec
= bcd2bin(regs
[ISL12057_REG_RTC_SC
]);
91 tm
->tm_min
= bcd2bin(regs
[ISL12057_REG_RTC_MN
]);
93 if (regs
[ISL12057_REG_RTC_HR
] & ISL12057_REG_RTC_HR_MIL
) { /* AM/PM */
94 tm
->tm_hour
= bcd2bin(regs
[ISL12057_REG_RTC_HR
] & 0x1f);
95 if (regs
[ISL12057_REG_RTC_HR
] & ISL12057_REG_RTC_HR_PM
)
97 } else { /* 24 hour mode */
98 tm
->tm_hour
= bcd2bin(regs
[ISL12057_REG_RTC_HR
] & 0x3f);
101 tm
->tm_mday
= bcd2bin(regs
[ISL12057_REG_RTC_DT
]);
102 tm
->tm_wday
= bcd2bin(regs
[ISL12057_REG_RTC_DW
]) - 1; /* starts at 1 */
103 tm
->tm_mon
= bcd2bin(regs
[ISL12057_REG_RTC_MO
] & 0x1f) - 1; /* ditto */
104 tm
->tm_year
= bcd2bin(regs
[ISL12057_REG_RTC_YR
]) + 100;
106 /* Check if years register has overflown from 99 to 00 */
107 if (regs
[ISL12057_REG_RTC_MO
] & ISL12057_REG_RTC_MO_CEN
)
111 static int isl12057_rtc_tm_to_regs(u8
*regs
, struct rtc_time
*tm
)
116 * The clock has an 8 bit wide bcd-coded register for the year.
117 * It also has a century bit encoded in MO flag which provides
118 * information about overflow of year register from 99 to 00.
119 * tm_year is an offset from 1900 and we are interested in the
120 * 2000-2199 range, so any value less than 100 or larger than
123 if (tm
->tm_year
< 100 || tm
->tm_year
> 299)
126 century_bit
= (tm
->tm_year
> 199) ? ISL12057_REG_RTC_MO_CEN
: 0;
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
);
132 regs
[ISL12057_REG_RTC_MO
] = bin2bcd(tm
->tm_mon
+ 1) | century_bit
;
133 regs
[ISL12057_REG_RTC_YR
] = bin2bcd(tm
->tm_year
% 100);
134 regs
[ISL12057_REG_RTC_DW
] = bin2bcd(tm
->tm_wday
+ 1);
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.
144 static int isl12057_i2c_validate_chip(struct regmap
*regmap
)
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 };
153 ret
= regmap_bulk_read(regmap
, 0, regs
, ISL12057_MEM_MAP_LEN
);
157 for (i
= 0; i
< ISL12057_MEM_MAP_LEN
; ++i
) {
158 if (regs
[i
] & mask
[i
]) /* check if bits are cleared */
165 static int _isl12057_rtc_clear_alarm(struct device
*dev
)
167 struct isl12057_rtc_data
*data
= dev_get_drvdata(dev
);
170 ret
= regmap_update_bits(data
->regmap
, ISL12057_REG_SR
,
171 ISL12057_REG_SR_A1F
, 0);
173 dev_err(dev
, "%s: clearing alarm failed (%d)\n", __func__
, ret
);
178 static int _isl12057_rtc_update_alarm(struct device
*dev
, int enable
)
180 struct isl12057_rtc_data
*data
= dev_get_drvdata(dev
);
183 ret
= regmap_update_bits(data
->regmap
, ISL12057_REG_INT
,
184 ISL12057_REG_INT_A1IE
,
185 enable
? ISL12057_REG_INT_A1IE
: 0);
187 dev_err(dev
, "%s: changing alarm interrupt flag failed (%d)\n",
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.
199 static int _isl12057_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
201 struct isl12057_rtc_data
*data
= dev_get_drvdata(dev
);
202 u8 regs
[ISL12057_RTC_SEC_LEN
];
206 ret
= regmap_read(data
->regmap
, ISL12057_REG_SR
, &sr
);
208 dev_err(dev
, "%s: unable to read oscillator status flag (%d)\n",
212 if (sr
& ISL12057_REG_SR_OSF
) {
218 ret
= regmap_bulk_read(data
->regmap
, ISL12057_REG_RTC_SC
, regs
,
219 ISL12057_RTC_SEC_LEN
);
221 dev_err(dev
, "%s: unable to read RTC time section (%d)\n",
228 isl12057_rtc_regs_to_tm(tm
, regs
);
230 return rtc_valid_tm(tm
);
233 static int isl12057_rtc_update_alarm(struct device
*dev
, int enable
)
235 struct isl12057_rtc_data
*data
= dev_get_drvdata(dev
);
238 mutex_lock(&data
->lock
);
239 ret
= _isl12057_rtc_update_alarm(dev
, enable
);
240 mutex_unlock(&data
->lock
);
245 static int isl12057_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
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
];
254 mutex_lock(&data
->lock
);
255 ret
= regmap_bulk_read(data
->regmap
, ISL12057_REG_A1_SC
, regs
,
256 ISL12057_A1_SEC_LEN
);
258 dev_err(dev
, "%s: reading alarm section failed (%d)\n",
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;
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.
274 ret
= _isl12057_rtc_read_time(dev
, &rtc_tm
);
278 alarm_tm
->tm_year
= rtc_tm
.tm_year
;
279 alarm_tm
->tm_mon
= rtc_tm
.tm_mon
;
281 ret
= rtc_tm_to_time(&rtc_tm
, &rtc_secs
);
285 ret
= rtc_tm_to_time(alarm_tm
, &alarm_secs
);
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;
294 alarm_tm
->tm_mon
+= 1;
298 ret
= regmap_read(data
->regmap
, ISL12057_REG_INT
, &ir
);
300 dev_err(dev
, "%s: reading alarm interrupt flag failed (%d)\n",
305 alarm
->enabled
= !!(ir
& ISL12057_REG_INT_A1IE
);
308 mutex_unlock(&data
->lock
);
313 static int isl12057_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
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
;
322 mutex_lock(&data
->lock
);
323 ret
= _isl12057_rtc_read_time(dev
, &rtc_tm
);
327 ret
= rtc_tm_to_time(&rtc_tm
, &rtc_secs
);
331 ret
= rtc_tm_to_time(alarm_tm
, &alarm_secs
);
335 /* If alarm time is before current time, disable the alarm */
336 if (!alarm
->enabled
|| alarm_secs
<= rtc_secs
) {
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.
345 if (rtc_tm
.tm_mon
== 11) { /* handle year wrapping */
352 ret
= rtc_tm_to_time(&rtc_tm
, &rtc_secs
);
356 if (alarm_secs
> rtc_secs
) {
357 dev_err(dev
, "%s: max for alarm is one month (%d)\n",
364 /* Disable the alarm before modifying it */
365 ret
= _isl12057_rtc_update_alarm(dev
, 0);
367 dev_err(dev
, "%s: unable to disable the alarm (%d)\n",
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;
378 ret
= regmap_bulk_write(data
->regmap
, ISL12057_REG_A1_SC
, regs
,
379 ISL12057_A1_SEC_LEN
);
381 dev_err(dev
, "%s: writing alarm section failed (%d)\n",
386 /* Enable or disable alarm */
387 ret
= _isl12057_rtc_update_alarm(dev
, enable
);
390 mutex_unlock(&data
->lock
);
395 static int isl12057_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
397 struct isl12057_rtc_data
*data
= dev_get_drvdata(dev
);
398 u8 regs
[ISL12057_RTC_SEC_LEN
];
401 ret
= isl12057_rtc_tm_to_regs(regs
, tm
);
405 mutex_lock(&data
->lock
);
406 ret
= regmap_bulk_write(data
->regmap
, ISL12057_REG_RTC_SC
, regs
,
407 ISL12057_RTC_SEC_LEN
);
409 dev_err(dev
, "%s: unable to write RTC time section (%d)\n",
415 * Now that RTC time has been updated, let's clear oscillator
416 * failure flag, if needed.
418 ret
= regmap_update_bits(data
->regmap
, ISL12057_REG_SR
,
419 ISL12057_REG_SR_OSF
, 0);
421 dev_err(dev
, "%s: unable to clear osc. failure bit (%d)\n",
425 mutex_unlock(&data
->lock
);
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.
435 static int isl12057_check_rtc_status(struct device
*dev
, struct regmap
*regmap
)
439 /* Enable oscillator if not already running */
440 ret
= regmap_update_bits(regmap
, ISL12057_REG_INT
,
441 ISL12057_REG_INT_EOSC
, 0);
443 dev_err(dev
, "%s: unable to enable oscillator (%d)\n",
448 /* Clear alarm bit if needed */
449 ret
= regmap_update_bits(regmap
, ISL12057_REG_SR
,
450 ISL12057_REG_SR_A1F
, 0);
452 dev_err(dev
, "%s: unable to clear alarm bit (%d)\n",
462 * One would expect the device to be marked as a wakeup source only
463 * when an IRQ pin of the RTC is routed to an interrupt line of the
464 * CPU. In practice, such an IRQ pin can be connected to a PMIC and
465 * this allows the device to be powered up when RTC alarm rings. This
466 * is for instance the case on ReadyNAS 102, 104 and 2120. On those
467 * devices with no IRQ driectly connected to the SoC, the RTC chip
468 * can be forced as a wakeup source by stating that explicitly in
469 * the device's .dts file using the "wakeup-source" boolean property.
470 * This will guarantee 'wakealarm' sysfs entry is available on the device.
472 * The function below returns 1, i.e. the capability of the chip to
473 * wakeup the device, based on IRQ availability or if the boolean
474 * property has been set in the .dts file. Otherwise, it returns 0.
477 static bool isl12057_can_wakeup_machine(struct device
*dev
)
479 struct isl12057_rtc_data
*data
= dev_get_drvdata(dev
);
481 return data
->irq
|| of_property_read_bool(dev
->of_node
, "wakeup-source")
482 || of_property_read_bool(dev
->of_node
, /* legacy */
483 "isil,irq2-can-wakeup-machine");
486 static bool isl12057_can_wakeup_machine(struct device
*dev
)
488 struct isl12057_rtc_data
*data
= dev_get_drvdata(dev
);
494 static int isl12057_rtc_alarm_irq_enable(struct device
*dev
,
497 struct isl12057_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
501 ret
= isl12057_rtc_update_alarm(dev
, enable
);
506 static irqreturn_t
isl12057_rtc_interrupt(int irq
, void *data
)
508 struct i2c_client
*client
= data
;
509 struct isl12057_rtc_data
*rtc_data
= dev_get_drvdata(&client
->dev
);
510 struct rtc_device
*rtc
= rtc_data
->rtc
;
511 int ret
, handled
= IRQ_NONE
;
514 ret
= regmap_read(rtc_data
->regmap
, ISL12057_REG_SR
, &sr
);
515 if (!ret
&& (sr
& ISL12057_REG_SR_A1F
)) {
516 dev_dbg(&client
->dev
, "RTC alarm!\n");
518 rtc_update_irq(rtc
, 1, RTC_IRQF
| RTC_AF
);
520 /* Acknowledge and disable the alarm */
521 _isl12057_rtc_clear_alarm(&client
->dev
);
522 _isl12057_rtc_update_alarm(&client
->dev
, 0);
524 handled
= IRQ_HANDLED
;
530 static const struct rtc_class_ops rtc_ops
= {
531 .read_time
= _isl12057_rtc_read_time
,
532 .set_time
= isl12057_rtc_set_time
,
533 .read_alarm
= isl12057_rtc_read_alarm
,
534 .set_alarm
= isl12057_rtc_set_alarm
,
535 .alarm_irq_enable
= isl12057_rtc_alarm_irq_enable
,
538 static const struct regmap_config isl12057_rtc_regmap_config
= {
543 static int isl12057_probe(struct i2c_client
*client
,
544 const struct i2c_device_id
*id
)
546 struct device
*dev
= &client
->dev
;
547 struct isl12057_rtc_data
*data
;
548 struct regmap
*regmap
;
551 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
|
552 I2C_FUNC_SMBUS_BYTE_DATA
|
553 I2C_FUNC_SMBUS_I2C_BLOCK
))
556 regmap
= devm_regmap_init_i2c(client
, &isl12057_rtc_regmap_config
);
557 if (IS_ERR(regmap
)) {
558 ret
= PTR_ERR(regmap
);
559 dev_err(dev
, "%s: regmap allocation failed (%d)\n",
564 ret
= isl12057_i2c_validate_chip(regmap
);
568 ret
= isl12057_check_rtc_status(dev
, regmap
);
572 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
576 mutex_init(&data
->lock
);
577 data
->regmap
= regmap
;
578 dev_set_drvdata(dev
, data
);
580 if (client
->irq
> 0) {
581 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
582 isl12057_rtc_interrupt
,
583 IRQF_SHARED
|IRQF_ONESHOT
,
586 data
->irq
= client
->irq
;
588 dev_err(dev
, "%s: irq %d unavailable (%d)\n", __func__
,
592 if (isl12057_can_wakeup_machine(dev
))
593 device_init_wakeup(dev
, true);
595 data
->rtc
= devm_rtc_device_register(dev
, DRV_NAME
, &rtc_ops
,
597 ret
= PTR_ERR_OR_ZERO(data
->rtc
);
599 dev_err(dev
, "%s: unable to register RTC device (%d)\n",
604 /* We cannot support UIE mode if we do not have an IRQ line */
606 data
->rtc
->uie_unsupported
= 1;
612 static int isl12057_remove(struct i2c_client
*client
)
614 if (isl12057_can_wakeup_machine(&client
->dev
))
615 device_init_wakeup(&client
->dev
, false);
620 #ifdef CONFIG_PM_SLEEP
621 static int isl12057_rtc_suspend(struct device
*dev
)
623 struct isl12057_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
625 if (rtc_data
->irq
&& device_may_wakeup(dev
))
626 return enable_irq_wake(rtc_data
->irq
);
631 static int isl12057_rtc_resume(struct device
*dev
)
633 struct isl12057_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
635 if (rtc_data
->irq
&& device_may_wakeup(dev
))
636 return disable_irq_wake(rtc_data
->irq
);
642 static SIMPLE_DEV_PM_OPS(isl12057_rtc_pm_ops
, isl12057_rtc_suspend
,
643 isl12057_rtc_resume
);
646 static const struct of_device_id isl12057_dt_match
[] = {
647 { .compatible
= "isl,isl12057" }, /* for backward compat., don't use */
648 { .compatible
= "isil,isl12057" },
651 MODULE_DEVICE_TABLE(of
, isl12057_dt_match
);
654 static const struct i2c_device_id isl12057_id
[] = {
658 MODULE_DEVICE_TABLE(i2c
, isl12057_id
);
660 static struct i2c_driver isl12057_driver
= {
663 .pm
= &isl12057_rtc_pm_ops
,
664 .of_match_table
= of_match_ptr(isl12057_dt_match
),
666 .probe
= isl12057_probe
,
667 .remove
= isl12057_remove
,
668 .id_table
= isl12057_id
,
670 module_i2c_driver(isl12057_driver
);
672 MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
673 MODULE_DESCRIPTION("Intersil ISL12057 RTC driver");
674 MODULE_LICENSE("GPL");