of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / rtc / rtc-rk808.c
blob35c9aada07c8ef3a19f44cf8d4a3e8811f484e3f
1 /*
2 * RTC driver for Rockchip RK808
4 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
6 * Author: Chris Zhong <zyw@rock-chips.com>
7 * Author: Zhang Qing <zhangqing@rock-chips.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/rtc.h>
22 #include <linux/bcd.h>
23 #include <linux/mfd/rk808.h>
24 #include <linux/platform_device.h>
25 #include <linux/i2c.h>
27 /* RTC_CTRL_REG bitfields */
28 #define BIT_RTC_CTRL_REG_STOP_RTC_M BIT(0)
30 /* RK808 has a shadowed register for saving a "frozen" RTC time.
31 * When user setting "GET_TIME" to 1, the time will save in this shadowed
32 * register. If set "READSEL" to 1, user read rtc time register, actually
33 * get the time of that moment. If we need the real time, clr this bit.
35 #define BIT_RTC_CTRL_REG_RTC_GET_TIME BIT(6)
36 #define BIT_RTC_CTRL_REG_RTC_READSEL_M BIT(7)
37 #define BIT_RTC_INTERRUPTS_REG_IT_ALARM_M BIT(3)
38 #define RTC_STATUS_MASK 0xFE
40 #define SECONDS_REG_MSK 0x7F
41 #define MINUTES_REG_MAK 0x7F
42 #define HOURS_REG_MSK 0x3F
43 #define DAYS_REG_MSK 0x3F
44 #define MONTHS_REG_MSK 0x1F
45 #define YEARS_REG_MSK 0xFF
46 #define WEEKS_REG_MSK 0x7
48 /* REG_SECONDS_REG through REG_YEARS_REG is how many registers? */
50 #define NUM_TIME_REGS (RK808_WEEKS_REG - RK808_SECONDS_REG + 1)
51 #define NUM_ALARM_REGS (RK808_ALARM_YEARS_REG - RK808_ALARM_SECONDS_REG + 1)
53 struct rk808_rtc {
54 struct rk808 *rk808;
55 struct rtc_device *rtc;
56 int irq;
60 * The Rockchip calendar used by the RK808 counts November with 31 days. We use
61 * these translation functions to convert its dates to/from the Gregorian
62 * calendar used by the rest of the world. We arbitrarily define Jan 1st, 2016
63 * as the day when both calendars were in sync, and treat all other dates
64 * relative to that.
65 * NOTE: Other system software (e.g. firmware) that reads the same hardware must
66 * implement this exact same conversion algorithm, with the same anchor date.
68 static time64_t nov2dec_transitions(struct rtc_time *tm)
70 return (tm->tm_year + 1900) - 2016 + (tm->tm_mon + 1 > 11 ? 1 : 0);
73 static void rockchip_to_gregorian(struct rtc_time *tm)
75 /* If it's Nov 31st, rtc_tm_to_time64() will count that like Dec 1st */
76 time64_t time = rtc_tm_to_time64(tm);
77 rtc_time64_to_tm(time + nov2dec_transitions(tm) * 86400, tm);
80 static void gregorian_to_rockchip(struct rtc_time *tm)
82 time64_t extra_days = nov2dec_transitions(tm);
83 time64_t time = rtc_tm_to_time64(tm);
84 rtc_time64_to_tm(time - extra_days * 86400, tm);
86 /* Compensate if we went back over Nov 31st (will work up to 2381) */
87 if (nov2dec_transitions(tm) < extra_days) {
88 if (tm->tm_mon + 1 == 11)
89 tm->tm_mday++; /* This may result in 31! */
90 else
91 rtc_time64_to_tm(time - (extra_days - 1) * 86400, tm);
95 /* Read current time and date in RTC */
96 static int rk808_rtc_readtime(struct device *dev, struct rtc_time *tm)
98 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
99 struct rk808 *rk808 = rk808_rtc->rk808;
100 u8 rtc_data[NUM_TIME_REGS];
101 int ret;
103 /* Force an update of the shadowed registers right now */
104 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
105 BIT_RTC_CTRL_REG_RTC_GET_TIME,
106 BIT_RTC_CTRL_REG_RTC_GET_TIME);
107 if (ret) {
108 dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret);
109 return ret;
113 * After we set the GET_TIME bit, the rtc time can't be read
114 * immediately. So we should wait up to 31.25 us, about one cycle of
115 * 32khz. If we clear the GET_TIME bit here, the time of i2c transfer
116 * certainly more than 31.25us: 16 * 2.5us at 400kHz bus frequency.
118 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
119 BIT_RTC_CTRL_REG_RTC_GET_TIME,
121 if (ret) {
122 dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret);
123 return ret;
126 ret = regmap_bulk_read(rk808->regmap, RK808_SECONDS_REG,
127 rtc_data, NUM_TIME_REGS);
128 if (ret) {
129 dev_err(dev, "Failed to bulk read rtc_data: %d\n", ret);
130 return ret;
133 tm->tm_sec = bcd2bin(rtc_data[0] & SECONDS_REG_MSK);
134 tm->tm_min = bcd2bin(rtc_data[1] & MINUTES_REG_MAK);
135 tm->tm_hour = bcd2bin(rtc_data[2] & HOURS_REG_MSK);
136 tm->tm_mday = bcd2bin(rtc_data[3] & DAYS_REG_MSK);
137 tm->tm_mon = (bcd2bin(rtc_data[4] & MONTHS_REG_MSK)) - 1;
138 tm->tm_year = (bcd2bin(rtc_data[5] & YEARS_REG_MSK)) + 100;
139 tm->tm_wday = bcd2bin(rtc_data[6] & WEEKS_REG_MSK);
140 rockchip_to_gregorian(tm);
141 dev_dbg(dev, "RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
142 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
143 tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
145 return ret;
148 /* Set current time and date in RTC */
149 static int rk808_rtc_set_time(struct device *dev, struct rtc_time *tm)
151 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
152 struct rk808 *rk808 = rk808_rtc->rk808;
153 u8 rtc_data[NUM_TIME_REGS];
154 int ret;
156 dev_dbg(dev, "set RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
157 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
158 tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
159 gregorian_to_rockchip(tm);
160 rtc_data[0] = bin2bcd(tm->tm_sec);
161 rtc_data[1] = bin2bcd(tm->tm_min);
162 rtc_data[2] = bin2bcd(tm->tm_hour);
163 rtc_data[3] = bin2bcd(tm->tm_mday);
164 rtc_data[4] = bin2bcd(tm->tm_mon + 1);
165 rtc_data[5] = bin2bcd(tm->tm_year - 100);
166 rtc_data[6] = bin2bcd(tm->tm_wday);
168 /* Stop RTC while updating the RTC registers */
169 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
170 BIT_RTC_CTRL_REG_STOP_RTC_M,
171 BIT_RTC_CTRL_REG_STOP_RTC_M);
172 if (ret) {
173 dev_err(dev, "Failed to update RTC control: %d\n", ret);
174 return ret;
177 ret = regmap_bulk_write(rk808->regmap, RK808_SECONDS_REG,
178 rtc_data, NUM_TIME_REGS);
179 if (ret) {
180 dev_err(dev, "Failed to bull write rtc_data: %d\n", ret);
181 return ret;
183 /* Start RTC again */
184 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
185 BIT_RTC_CTRL_REG_STOP_RTC_M, 0);
186 if (ret) {
187 dev_err(dev, "Failed to update RTC control: %d\n", ret);
188 return ret;
190 return 0;
193 /* Read alarm time and date in RTC */
194 static int rk808_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
196 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
197 struct rk808 *rk808 = rk808_rtc->rk808;
198 u8 alrm_data[NUM_ALARM_REGS];
199 uint32_t int_reg;
200 int ret;
202 ret = regmap_bulk_read(rk808->regmap, RK808_ALARM_SECONDS_REG,
203 alrm_data, NUM_ALARM_REGS);
205 alrm->time.tm_sec = bcd2bin(alrm_data[0] & SECONDS_REG_MSK);
206 alrm->time.tm_min = bcd2bin(alrm_data[1] & MINUTES_REG_MAK);
207 alrm->time.tm_hour = bcd2bin(alrm_data[2] & HOURS_REG_MSK);
208 alrm->time.tm_mday = bcd2bin(alrm_data[3] & DAYS_REG_MSK);
209 alrm->time.tm_mon = (bcd2bin(alrm_data[4] & MONTHS_REG_MSK)) - 1;
210 alrm->time.tm_year = (bcd2bin(alrm_data[5] & YEARS_REG_MSK)) + 100;
211 rockchip_to_gregorian(&alrm->time);
213 ret = regmap_read(rk808->regmap, RK808_RTC_INT_REG, &int_reg);
214 if (ret) {
215 dev_err(dev, "Failed to read RTC INT REG: %d\n", ret);
216 return ret;
219 dev_dbg(dev, "alrm read RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
220 1900 + alrm->time.tm_year, alrm->time.tm_mon + 1,
221 alrm->time.tm_mday, alrm->time.tm_wday, alrm->time.tm_hour,
222 alrm->time.tm_min, alrm->time.tm_sec);
224 alrm->enabled = (int_reg & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M) ? 1 : 0;
226 return 0;
229 static int rk808_rtc_stop_alarm(struct rk808_rtc *rk808_rtc)
231 struct rk808 *rk808 = rk808_rtc->rk808;
232 int ret;
234 ret = regmap_update_bits(rk808->regmap, RK808_RTC_INT_REG,
235 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M, 0);
237 return ret;
240 static int rk808_rtc_start_alarm(struct rk808_rtc *rk808_rtc)
242 struct rk808 *rk808 = rk808_rtc->rk808;
243 int ret;
245 ret = regmap_update_bits(rk808->regmap, RK808_RTC_INT_REG,
246 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M,
247 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
249 return ret;
252 static int rk808_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
254 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
255 struct rk808 *rk808 = rk808_rtc->rk808;
256 u8 alrm_data[NUM_ALARM_REGS];
257 int ret;
259 ret = rk808_rtc_stop_alarm(rk808_rtc);
260 if (ret) {
261 dev_err(dev, "Failed to stop alarm: %d\n", ret);
262 return ret;
264 dev_dbg(dev, "alrm set RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
265 1900 + alrm->time.tm_year, alrm->time.tm_mon + 1,
266 alrm->time.tm_mday, alrm->time.tm_wday, alrm->time.tm_hour,
267 alrm->time.tm_min, alrm->time.tm_sec);
269 gregorian_to_rockchip(&alrm->time);
270 alrm_data[0] = bin2bcd(alrm->time.tm_sec);
271 alrm_data[1] = bin2bcd(alrm->time.tm_min);
272 alrm_data[2] = bin2bcd(alrm->time.tm_hour);
273 alrm_data[3] = bin2bcd(alrm->time.tm_mday);
274 alrm_data[4] = bin2bcd(alrm->time.tm_mon + 1);
275 alrm_data[5] = bin2bcd(alrm->time.tm_year - 100);
277 ret = regmap_bulk_write(rk808->regmap, RK808_ALARM_SECONDS_REG,
278 alrm_data, NUM_ALARM_REGS);
279 if (ret) {
280 dev_err(dev, "Failed to bulk write: %d\n", ret);
281 return ret;
283 if (alrm->enabled) {
284 ret = rk808_rtc_start_alarm(rk808_rtc);
285 if (ret) {
286 dev_err(dev, "Failed to start alarm: %d\n", ret);
287 return ret;
290 return 0;
293 static int rk808_rtc_alarm_irq_enable(struct device *dev,
294 unsigned int enabled)
296 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
298 if (enabled)
299 return rk808_rtc_start_alarm(rk808_rtc);
301 return rk808_rtc_stop_alarm(rk808_rtc);
305 * We will just handle setting the frequency and make use the framework for
306 * reading the periodic interupts.
308 * @freq: Current periodic IRQ freq:
309 * bit 0: every second
310 * bit 1: every minute
311 * bit 2: every hour
312 * bit 3: every day
314 static irqreturn_t rk808_alarm_irq(int irq, void *data)
316 struct rk808_rtc *rk808_rtc = data;
317 struct rk808 *rk808 = rk808_rtc->rk808;
318 struct i2c_client *client = rk808->i2c;
319 int ret;
321 ret = regmap_write(rk808->regmap, RK808_RTC_STATUS_REG,
322 RTC_STATUS_MASK);
323 if (ret) {
324 dev_err(&client->dev,
325 "%s:Failed to update RTC status: %d\n", __func__, ret);
326 return ret;
329 rtc_update_irq(rk808_rtc->rtc, 1, RTC_IRQF | RTC_AF);
330 dev_dbg(&client->dev,
331 "%s:irq=%d\n", __func__, irq);
332 return IRQ_HANDLED;
335 static const struct rtc_class_ops rk808_rtc_ops = {
336 .read_time = rk808_rtc_readtime,
337 .set_time = rk808_rtc_set_time,
338 .read_alarm = rk808_rtc_readalarm,
339 .set_alarm = rk808_rtc_setalarm,
340 .alarm_irq_enable = rk808_rtc_alarm_irq_enable,
343 #ifdef CONFIG_PM_SLEEP
344 /* Turn off the alarm if it should not be a wake source. */
345 static int rk808_rtc_suspend(struct device *dev)
347 struct platform_device *pdev = to_platform_device(dev);
348 struct rk808_rtc *rk808_rtc = dev_get_drvdata(&pdev->dev);
350 if (device_may_wakeup(dev))
351 enable_irq_wake(rk808_rtc->irq);
353 return 0;
356 /* Enable the alarm if it should be enabled (in case it was disabled to
357 * prevent use as a wake source).
359 static int rk808_rtc_resume(struct device *dev)
361 struct platform_device *pdev = to_platform_device(dev);
362 struct rk808_rtc *rk808_rtc = dev_get_drvdata(&pdev->dev);
364 if (device_may_wakeup(dev))
365 disable_irq_wake(rk808_rtc->irq);
367 return 0;
369 #endif
371 static SIMPLE_DEV_PM_OPS(rk808_rtc_pm_ops,
372 rk808_rtc_suspend, rk808_rtc_resume);
374 static int rk808_rtc_probe(struct platform_device *pdev)
376 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
377 struct rk808_rtc *rk808_rtc;
378 struct rtc_time tm;
379 int ret;
381 rk808_rtc = devm_kzalloc(&pdev->dev, sizeof(*rk808_rtc), GFP_KERNEL);
382 if (rk808_rtc == NULL)
383 return -ENOMEM;
385 platform_set_drvdata(pdev, rk808_rtc);
386 rk808_rtc->rk808 = rk808;
388 /* start rtc running by default, and use shadowed timer. */
389 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
390 BIT_RTC_CTRL_REG_STOP_RTC_M |
391 BIT_RTC_CTRL_REG_RTC_READSEL_M,
392 BIT_RTC_CTRL_REG_RTC_READSEL_M);
393 if (ret) {
394 dev_err(&pdev->dev,
395 "Failed to update RTC control: %d\n", ret);
396 return ret;
399 ret = regmap_write(rk808->regmap, RK808_RTC_STATUS_REG,
400 RTC_STATUS_MASK);
401 if (ret) {
402 dev_err(&pdev->dev,
403 "Failed to write RTC status: %d\n", ret);
404 return ret;
407 /* set init time */
408 ret = rk808_rtc_readtime(&pdev->dev, &tm);
409 if (ret) {
410 dev_err(&pdev->dev, "Failed to read RTC time\n");
411 return ret;
413 ret = rtc_valid_tm(&tm);
414 if (ret)
415 dev_warn(&pdev->dev, "invalid date/time\n");
417 device_init_wakeup(&pdev->dev, 1);
419 rk808_rtc->rtc = devm_rtc_device_register(&pdev->dev, "rk808-rtc",
420 &rk808_rtc_ops, THIS_MODULE);
421 if (IS_ERR(rk808_rtc->rtc)) {
422 ret = PTR_ERR(rk808_rtc->rtc);
423 return ret;
426 rk808_rtc->irq = platform_get_irq(pdev, 0);
427 if (rk808_rtc->irq < 0) {
428 if (rk808_rtc->irq != -EPROBE_DEFER)
429 dev_err(&pdev->dev, "Wake up is not possible as irq = %d\n",
430 rk808_rtc->irq);
431 return rk808_rtc->irq;
434 /* request alarm irq of rk808 */
435 ret = devm_request_threaded_irq(&pdev->dev, rk808_rtc->irq, NULL,
436 rk808_alarm_irq, 0,
437 "RTC alarm", rk808_rtc);
438 if (ret) {
439 dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n",
440 rk808_rtc->irq, ret);
443 return ret;
446 static struct platform_driver rk808_rtc_driver = {
447 .probe = rk808_rtc_probe,
448 .driver = {
449 .name = "rk808-rtc",
450 .pm = &rk808_rtc_pm_ops,
454 module_platform_driver(rk808_rtc_driver);
456 MODULE_DESCRIPTION("RTC driver for the rk808 series PMICs");
457 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
458 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
459 MODULE_LICENSE("GPL");
460 MODULE_ALIAS("platform:rk808-rtc");