1 /* rtc-da9063.c - Real time clock device driver for DA9063
2 * Copyright (C) 2013-14 Dialog Semiconductor Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/rtc.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/regmap.h>
24 #include <linux/mfd/da9063/registers.h>
25 #include <linux/mfd/da9063/core.h>
27 #define YEARS_TO_DA9063(year) ((year) - 100)
28 #define MONTHS_TO_DA9063(month) ((month) + 1)
29 #define YEARS_FROM_DA9063(year) ((year) + 100)
30 #define MONTHS_FROM_DA9063(month) ((month) - 1)
32 #define RTC_ALARM_DATA_LEN (DA9063_AD_REG_ALARM_Y - DA9063_AD_REG_ALARM_MI + 1)
34 #define RTC_DATA_LEN (DA9063_REG_COUNT_Y - DA9063_REG_COUNT_S + 1)
43 struct rtc_device
*rtc_dev
;
45 struct rtc_time alarm_time
;
53 static void da9063_data_to_tm(u8
*data
, struct rtc_time
*tm
)
55 tm
->tm_sec
= data
[RTC_SEC
] & DA9063_COUNT_SEC_MASK
;
56 tm
->tm_min
= data
[RTC_MIN
] & DA9063_COUNT_MIN_MASK
;
57 tm
->tm_hour
= data
[RTC_HOUR
] & DA9063_COUNT_HOUR_MASK
;
58 tm
->tm_mday
= data
[RTC_DAY
] & DA9063_COUNT_DAY_MASK
;
59 tm
->tm_mon
= MONTHS_FROM_DA9063(data
[RTC_MONTH
] &
60 DA9063_COUNT_MONTH_MASK
);
61 tm
->tm_year
= YEARS_FROM_DA9063(data
[RTC_YEAR
] &
62 DA9063_COUNT_YEAR_MASK
);
65 static void da9063_tm_to_data(struct rtc_time
*tm
, u8
*data
)
67 data
[RTC_SEC
] &= ~DA9063_COUNT_SEC_MASK
;
68 data
[RTC_SEC
] |= tm
->tm_sec
& DA9063_COUNT_SEC_MASK
;
70 data
[RTC_MIN
] &= ~DA9063_COUNT_MIN_MASK
;
71 data
[RTC_MIN
] |= tm
->tm_min
& DA9063_COUNT_MIN_MASK
;
73 data
[RTC_HOUR
] &= ~DA9063_COUNT_HOUR_MASK
;
74 data
[RTC_HOUR
] |= tm
->tm_hour
& DA9063_COUNT_HOUR_MASK
;
76 data
[RTC_DAY
] &= ~DA9063_COUNT_DAY_MASK
;
77 data
[RTC_DAY
] |= tm
->tm_mday
& DA9063_COUNT_DAY_MASK
;
79 data
[RTC_MONTH
] &= ~DA9063_COUNT_MONTH_MASK
;
80 data
[RTC_MONTH
] |= MONTHS_TO_DA9063(tm
->tm_mon
) &
81 DA9063_COUNT_MONTH_MASK
;
83 data
[RTC_YEAR
] &= ~DA9063_COUNT_YEAR_MASK
;
84 data
[RTC_YEAR
] |= YEARS_TO_DA9063(tm
->tm_year
) &
85 DA9063_COUNT_YEAR_MASK
;
88 static int da9063_rtc_stop_alarm(struct device
*dev
)
90 struct da9063_rtc
*rtc
= dev_get_drvdata(dev
);
92 return regmap_update_bits(rtc
->hw
->regmap
, rtc
->alarm_year
,
96 static int da9063_rtc_start_alarm(struct device
*dev
)
98 struct da9063_rtc
*rtc
= dev_get_drvdata(dev
);
100 return regmap_update_bits(rtc
->hw
->regmap
, rtc
->alarm_year
,
101 DA9063_ALARM_ON
, DA9063_ALARM_ON
);
104 static int da9063_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
106 struct da9063_rtc
*rtc
= dev_get_drvdata(dev
);
107 unsigned long tm_secs
;
108 unsigned long al_secs
;
109 u8 data
[RTC_DATA_LEN
];
112 ret
= regmap_bulk_read(rtc
->hw
->regmap
, DA9063_REG_COUNT_S
,
115 dev_err(dev
, "Failed to read RTC time data: %d\n", ret
);
119 if (!(data
[RTC_SEC
] & DA9063_RTC_READ
)) {
120 dev_dbg(dev
, "RTC not yet ready to be read by the host\n");
124 da9063_data_to_tm(data
, tm
);
126 rtc_tm_to_time(tm
, &tm_secs
);
127 rtc_tm_to_time(&rtc
->alarm_time
, &al_secs
);
129 /* handle the rtc synchronisation delay */
130 if (rtc
->rtc_sync
== true && al_secs
- tm_secs
== 1)
131 memcpy(tm
, &rtc
->alarm_time
, sizeof(struct rtc_time
));
133 rtc
->rtc_sync
= false;
135 return rtc_valid_tm(tm
);
138 static int da9063_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
140 struct da9063_rtc
*rtc
= dev_get_drvdata(dev
);
141 u8 data
[RTC_DATA_LEN
];
144 da9063_tm_to_data(tm
, data
);
145 ret
= regmap_bulk_write(rtc
->hw
->regmap
, DA9063_REG_COUNT_S
,
148 dev_err(dev
, "Failed to set RTC time data: %d\n", ret
);
153 static int da9063_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
155 struct da9063_rtc
*rtc
= dev_get_drvdata(dev
);
156 u8 data
[RTC_DATA_LEN
];
161 ret
= regmap_bulk_read(rtc
->hw
->regmap
, rtc
->alarm_start
,
162 &data
[rtc
->data_start
], rtc
->alarm_len
);
166 da9063_data_to_tm(data
, &alrm
->time
);
168 alrm
->enabled
= !!(data
[RTC_YEAR
] & DA9063_ALARM_ON
);
170 ret
= regmap_read(rtc
->hw
->regmap
, DA9063_REG_EVENT_A
, &val
);
174 if (val
& (DA9063_E_ALARM
))
182 static int da9063_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
184 struct da9063_rtc
*rtc
= dev_get_drvdata(dev
);
185 u8 data
[RTC_DATA_LEN
];
188 da9063_tm_to_data(&alrm
->time
, data
);
190 ret
= da9063_rtc_stop_alarm(dev
);
192 dev_err(dev
, "Failed to stop alarm: %d\n", ret
);
196 ret
= regmap_bulk_write(rtc
->hw
->regmap
, rtc
->alarm_start
,
197 &data
[rtc
->data_start
], rtc
->alarm_len
);
199 dev_err(dev
, "Failed to write alarm: %d\n", ret
);
203 da9063_data_to_tm(data
, &rtc
->alarm_time
);
206 ret
= da9063_rtc_start_alarm(dev
);
208 dev_err(dev
, "Failed to start alarm: %d\n", ret
);
216 static int da9063_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
219 return da9063_rtc_start_alarm(dev
);
221 return da9063_rtc_stop_alarm(dev
);
224 static irqreturn_t
da9063_alarm_event(int irq
, void *data
)
226 struct da9063_rtc
*rtc
= data
;
228 regmap_update_bits(rtc
->hw
->regmap
, rtc
->alarm_year
,
231 rtc
->rtc_sync
= true;
232 rtc_update_irq(rtc
->rtc_dev
, 1, RTC_IRQF
| RTC_AF
);
237 static const struct rtc_class_ops da9063_rtc_ops
= {
238 .read_time
= da9063_rtc_read_time
,
239 .set_time
= da9063_rtc_set_time
,
240 .read_alarm
= da9063_rtc_read_alarm
,
241 .set_alarm
= da9063_rtc_set_alarm
,
242 .alarm_irq_enable
= da9063_rtc_alarm_irq_enable
,
245 static int da9063_rtc_probe(struct platform_device
*pdev
)
247 struct da9063
*da9063
= dev_get_drvdata(pdev
->dev
.parent
);
248 struct da9063_rtc
*rtc
;
250 u8 data
[RTC_DATA_LEN
];
253 ret
= regmap_update_bits(da9063
->regmap
, DA9063_REG_CONTROL_E
,
254 DA9063_RTC_EN
, DA9063_RTC_EN
);
256 dev_err(&pdev
->dev
, "Failed to enable RTC\n");
260 ret
= regmap_update_bits(da9063
->regmap
, DA9063_REG_EN_32K
,
261 DA9063_CRYSTAL
, DA9063_CRYSTAL
);
263 dev_err(&pdev
->dev
, "Failed to run 32kHz oscillator\n");
267 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*rtc
), GFP_KERNEL
);
271 if (da9063
->variant_code
== PMIC_DA9063_AD
) {
272 rtc
->alarm_year
= DA9063_AD_REG_ALARM_Y
;
273 rtc
->alarm_start
= DA9063_AD_REG_ALARM_MI
;
274 rtc
->alarm_len
= RTC_ALARM_DATA_LEN
;
275 rtc
->data_start
= RTC_MIN
;
277 rtc
->alarm_year
= DA9063_BB_REG_ALARM_Y
;
278 rtc
->alarm_start
= DA9063_BB_REG_ALARM_S
;
279 rtc
->alarm_len
= RTC_DATA_LEN
;
280 rtc
->data_start
= RTC_SEC
;
283 ret
= regmap_update_bits(da9063
->regmap
, rtc
->alarm_start
,
284 DA9063_ALARM_STATUS_TICK
| DA9063_ALARM_STATUS_ALARM
,
287 dev_err(&pdev
->dev
, "Failed to access RTC alarm register\n");
291 ret
= regmap_update_bits(da9063
->regmap
, rtc
->alarm_start
,
292 DA9063_ALARM_STATUS_ALARM
,
293 DA9063_ALARM_STATUS_ALARM
);
295 dev_err(&pdev
->dev
, "Failed to access RTC alarm register\n");
299 ret
= regmap_update_bits(da9063
->regmap
, rtc
->alarm_year
,
302 dev_err(&pdev
->dev
, "Failed to disable TICKs\n");
307 ret
= regmap_bulk_read(da9063
->regmap
, rtc
->alarm_start
,
308 &data
[rtc
->data_start
], rtc
->alarm_len
);
310 dev_err(&pdev
->dev
, "Failed to read initial alarm data: %d\n",
315 platform_set_drvdata(pdev
, rtc
);
317 irq_alarm
= platform_get_irq_byname(pdev
, "ALARM");
318 ret
= devm_request_threaded_irq(&pdev
->dev
, irq_alarm
, NULL
,
320 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
323 dev_err(&pdev
->dev
, "Failed to request ALARM IRQ %d: %d\n",
329 rtc
->rtc_dev
= devm_rtc_device_register(&pdev
->dev
, DA9063_DRVNAME_RTC
,
330 &da9063_rtc_ops
, THIS_MODULE
);
331 if (IS_ERR(rtc
->rtc_dev
))
332 return PTR_ERR(rtc
->rtc_dev
);
334 da9063_data_to_tm(data
, &rtc
->alarm_time
);
335 rtc
->rtc_sync
= false;
340 static struct platform_driver da9063_rtc_driver
= {
341 .probe
= da9063_rtc_probe
,
343 .name
= DA9063_DRVNAME_RTC
,
347 module_platform_driver(da9063_rtc_driver
);
349 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
350 MODULE_DESCRIPTION("Real time clock device driver for Dialog DA9063");
351 MODULE_LICENSE("GPL v2");
352 MODULE_ALIAS("platform:" DA9063_DRVNAME_RTC
);