2 * Copyright (c) 2014-2015 MediaTek Inc.
3 * Author: Tianping.Fang <tianping.fang@mediatek.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program 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
12 * GNU General Public License for more details.
15 #include <linux/delay.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/regmap.h>
19 #include <linux/rtc.h>
20 #include <linux/irqdomain.h>
21 #include <linux/platform_device.h>
22 #include <linux/of_address.h>
23 #include <linux/of_irq.h>
25 #include <linux/mfd/mt6397/core.h>
27 #define RTC_BBPU 0x0000
28 #define RTC_BBPU_CBUSY BIT(6)
30 #define RTC_WRTGR 0x003c
32 #define RTC_IRQ_STA 0x0002
33 #define RTC_IRQ_STA_AL BIT(0)
34 #define RTC_IRQ_STA_LP BIT(3)
36 #define RTC_IRQ_EN 0x0004
37 #define RTC_IRQ_EN_AL BIT(0)
38 #define RTC_IRQ_EN_ONESHOT BIT(2)
39 #define RTC_IRQ_EN_LP BIT(3)
40 #define RTC_IRQ_EN_ONESHOT_AL (RTC_IRQ_EN_ONESHOT | RTC_IRQ_EN_AL)
42 #define RTC_AL_MASK 0x0008
43 #define RTC_AL_MASK_DOW BIT(4)
45 #define RTC_TC_SEC 0x000a
46 /* Min, Hour, Dom... register offset to RTC_TC_SEC */
47 #define RTC_OFFSET_SEC 0
48 #define RTC_OFFSET_MIN 1
49 #define RTC_OFFSET_HOUR 2
50 #define RTC_OFFSET_DOM 3
51 #define RTC_OFFSET_DOW 4
52 #define RTC_OFFSET_MTH 5
53 #define RTC_OFFSET_YEAR 6
54 #define RTC_OFFSET_COUNT 7
56 #define RTC_AL_SEC 0x0018
58 #define RTC_AL_SEC_MASK 0x003f
59 #define RTC_AL_MIN_MASK 0x003f
60 #define RTC_AL_HOU_MASK 0x001f
61 #define RTC_AL_DOM_MASK 0x001f
62 #define RTC_AL_DOW_MASK 0x0007
63 #define RTC_AL_MTH_MASK 0x000f
64 #define RTC_AL_YEA_MASK 0x007f
66 #define RTC_PDN2 0x002e
67 #define RTC_PDN2_PWRON_ALARM BIT(4)
69 #define RTC_MIN_YEAR 1968
70 #define RTC_BASE_YEAR 1900
71 #define RTC_NUM_YEARS 128
72 #define RTC_MIN_YEAR_OFFSET (RTC_MIN_YEAR - RTC_BASE_YEAR)
76 struct rtc_device
*rtc_dev
;
78 struct regmap
*regmap
;
83 static int mtk_rtc_write_trigger(struct mt6397_rtc
*rtc
)
85 unsigned long timeout
= jiffies
+ HZ
;
89 ret
= regmap_write(rtc
->regmap
, rtc
->addr_base
+ RTC_WRTGR
, 1);
94 ret
= regmap_read(rtc
->regmap
, rtc
->addr_base
+ RTC_BBPU
,
98 if (!(data
& RTC_BBPU_CBUSY
))
100 if (time_after(jiffies
, timeout
)) {
110 static irqreturn_t
mtk_rtc_irq_handler_thread(int irq
, void *data
)
112 struct mt6397_rtc
*rtc
= data
;
116 ret
= regmap_read(rtc
->regmap
, rtc
->addr_base
+ RTC_IRQ_STA
, &irqsta
);
117 if ((ret
>= 0) && (irqsta
& RTC_IRQ_STA_AL
)) {
118 rtc_update_irq(rtc
->rtc_dev
, 1, RTC_IRQF
| RTC_AF
);
119 irqen
= irqsta
& ~RTC_IRQ_EN_AL
;
120 mutex_lock(&rtc
->lock
);
121 if (regmap_write(rtc
->regmap
, rtc
->addr_base
+ RTC_IRQ_EN
,
123 mtk_rtc_write_trigger(rtc
);
124 mutex_unlock(&rtc
->lock
);
132 static int __mtk_rtc_read_time(struct mt6397_rtc
*rtc
,
133 struct rtc_time
*tm
, int *sec
)
136 u16 data
[RTC_OFFSET_COUNT
];
138 mutex_lock(&rtc
->lock
);
139 ret
= regmap_bulk_read(rtc
->regmap
, rtc
->addr_base
+ RTC_TC_SEC
,
140 data
, RTC_OFFSET_COUNT
);
144 tm
->tm_sec
= data
[RTC_OFFSET_SEC
];
145 tm
->tm_min
= data
[RTC_OFFSET_MIN
];
146 tm
->tm_hour
= data
[RTC_OFFSET_HOUR
];
147 tm
->tm_mday
= data
[RTC_OFFSET_DOM
];
148 tm
->tm_mon
= data
[RTC_OFFSET_MTH
];
149 tm
->tm_year
= data
[RTC_OFFSET_YEAR
];
151 ret
= regmap_read(rtc
->regmap
, rtc
->addr_base
+ RTC_TC_SEC
, sec
);
153 mutex_unlock(&rtc
->lock
);
157 static int mtk_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
160 struct mt6397_rtc
*rtc
= dev_get_drvdata(dev
);
164 ret
= __mtk_rtc_read_time(rtc
, tm
, &sec
);
167 } while (sec
< tm
->tm_sec
);
169 /* HW register use 7 bits to store year data, minus
170 * RTC_MIN_YEAR_OFFSET before write year data to register, and plus
171 * RTC_MIN_YEAR_OFFSET back after read year from register
173 tm
->tm_year
+= RTC_MIN_YEAR_OFFSET
;
175 /* HW register start mon from one, but tm_mon start from zero. */
177 time
= rtc_tm_to_time64(tm
);
179 /* rtc_tm_to_time64 covert Gregorian date to seconds since
180 * 01-01-1970 00:00:00, and this date is Thursday.
182 days
= div_s64(time
, 86400);
183 tm
->tm_wday
= (days
+ 4) % 7;
189 static int mtk_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
191 struct mt6397_rtc
*rtc
= dev_get_drvdata(dev
);
193 u16 data
[RTC_OFFSET_COUNT
];
195 tm
->tm_year
-= RTC_MIN_YEAR_OFFSET
;
198 data
[RTC_OFFSET_SEC
] = tm
->tm_sec
;
199 data
[RTC_OFFSET_MIN
] = tm
->tm_min
;
200 data
[RTC_OFFSET_HOUR
] = tm
->tm_hour
;
201 data
[RTC_OFFSET_DOM
] = tm
->tm_mday
;
202 data
[RTC_OFFSET_MTH
] = tm
->tm_mon
;
203 data
[RTC_OFFSET_YEAR
] = tm
->tm_year
;
205 mutex_lock(&rtc
->lock
);
206 ret
= regmap_bulk_write(rtc
->regmap
, rtc
->addr_base
+ RTC_TC_SEC
,
207 data
, RTC_OFFSET_COUNT
);
211 /* Time register write to hardware after call trigger function */
212 ret
= mtk_rtc_write_trigger(rtc
);
215 mutex_unlock(&rtc
->lock
);
219 static int mtk_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
221 struct rtc_time
*tm
= &alm
->time
;
222 struct mt6397_rtc
*rtc
= dev_get_drvdata(dev
);
225 u16 data
[RTC_OFFSET_COUNT
];
227 mutex_lock(&rtc
->lock
);
228 ret
= regmap_read(rtc
->regmap
, rtc
->addr_base
+ RTC_IRQ_EN
, &irqen
);
231 ret
= regmap_read(rtc
->regmap
, rtc
->addr_base
+ RTC_PDN2
, &pdn2
);
235 ret
= regmap_bulk_read(rtc
->regmap
, rtc
->addr_base
+ RTC_AL_SEC
,
236 data
, RTC_OFFSET_COUNT
);
240 alm
->enabled
= !!(irqen
& RTC_IRQ_EN_AL
);
241 alm
->pending
= !!(pdn2
& RTC_PDN2_PWRON_ALARM
);
242 mutex_unlock(&rtc
->lock
);
244 tm
->tm_sec
= data
[RTC_OFFSET_SEC
] & RTC_AL_SEC_MASK
;
245 tm
->tm_min
= data
[RTC_OFFSET_MIN
] & RTC_AL_MIN_MASK
;
246 tm
->tm_hour
= data
[RTC_OFFSET_HOUR
] & RTC_AL_HOU_MASK
;
247 tm
->tm_mday
= data
[RTC_OFFSET_DOM
] & RTC_AL_DOM_MASK
;
248 tm
->tm_mon
= data
[RTC_OFFSET_MTH
] & RTC_AL_MTH_MASK
;
249 tm
->tm_year
= data
[RTC_OFFSET_YEAR
] & RTC_AL_YEA_MASK
;
251 tm
->tm_year
+= RTC_MIN_YEAR_OFFSET
;
256 mutex_unlock(&rtc
->lock
);
260 static int mtk_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
262 struct rtc_time
*tm
= &alm
->time
;
263 struct mt6397_rtc
*rtc
= dev_get_drvdata(dev
);
265 u16 data
[RTC_OFFSET_COUNT
];
267 tm
->tm_year
-= RTC_MIN_YEAR_OFFSET
;
270 mutex_lock(&rtc
->lock
);
271 ret
= regmap_bulk_read(rtc
->regmap
, rtc
->addr_base
+ RTC_AL_SEC
,
272 data
, RTC_OFFSET_COUNT
);
276 data
[RTC_OFFSET_SEC
] = ((data
[RTC_OFFSET_SEC
] & ~(RTC_AL_SEC_MASK
)) |
277 (tm
->tm_sec
& RTC_AL_SEC_MASK
));
278 data
[RTC_OFFSET_MIN
] = ((data
[RTC_OFFSET_MIN
] & ~(RTC_AL_MIN_MASK
)) |
279 (tm
->tm_min
& RTC_AL_MIN_MASK
));
280 data
[RTC_OFFSET_HOUR
] = ((data
[RTC_OFFSET_HOUR
] & ~(RTC_AL_HOU_MASK
)) |
281 (tm
->tm_hour
& RTC_AL_HOU_MASK
));
282 data
[RTC_OFFSET_DOM
] = ((data
[RTC_OFFSET_DOM
] & ~(RTC_AL_DOM_MASK
)) |
283 (tm
->tm_mday
& RTC_AL_DOM_MASK
));
284 data
[RTC_OFFSET_MTH
] = ((data
[RTC_OFFSET_MTH
] & ~(RTC_AL_MTH_MASK
)) |
285 (tm
->tm_mon
& RTC_AL_MTH_MASK
));
286 data
[RTC_OFFSET_YEAR
] = ((data
[RTC_OFFSET_YEAR
] & ~(RTC_AL_YEA_MASK
)) |
287 (tm
->tm_year
& RTC_AL_YEA_MASK
));
290 ret
= regmap_bulk_write(rtc
->regmap
,
291 rtc
->addr_base
+ RTC_AL_SEC
,
292 data
, RTC_OFFSET_COUNT
);
295 ret
= regmap_write(rtc
->regmap
, rtc
->addr_base
+ RTC_AL_MASK
,
299 ret
= regmap_update_bits(rtc
->regmap
,
300 rtc
->addr_base
+ RTC_IRQ_EN
,
301 RTC_IRQ_EN_ONESHOT_AL
,
302 RTC_IRQ_EN_ONESHOT_AL
);
306 ret
= regmap_update_bits(rtc
->regmap
,
307 rtc
->addr_base
+ RTC_IRQ_EN
,
308 RTC_IRQ_EN_ONESHOT_AL
, 0);
313 /* All alarm time register write to hardware after calling
314 * mtk_rtc_write_trigger. This can avoid race condition if alarm
315 * occur happen during writing alarm time register.
317 ret
= mtk_rtc_write_trigger(rtc
);
319 mutex_unlock(&rtc
->lock
);
323 static const struct rtc_class_ops mtk_rtc_ops
= {
324 .read_time
= mtk_rtc_read_time
,
325 .set_time
= mtk_rtc_set_time
,
326 .read_alarm
= mtk_rtc_read_alarm
,
327 .set_alarm
= mtk_rtc_set_alarm
,
330 static int mtk_rtc_probe(struct platform_device
*pdev
)
332 struct resource
*res
;
333 struct mt6397_chip
*mt6397_chip
= dev_get_drvdata(pdev
->dev
.parent
);
334 struct mt6397_rtc
*rtc
;
337 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(struct mt6397_rtc
), GFP_KERNEL
);
341 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
342 rtc
->addr_base
= res
->start
;
344 rtc
->irq
= platform_get_irq(pdev
, 0);
348 rtc
->regmap
= mt6397_chip
->regmap
;
349 rtc
->dev
= &pdev
->dev
;
350 mutex_init(&rtc
->lock
);
352 platform_set_drvdata(pdev
, rtc
);
354 rtc
->rtc_dev
= devm_rtc_allocate_device(rtc
->dev
);
355 if (IS_ERR(rtc
->rtc_dev
))
356 return PTR_ERR(rtc
->rtc_dev
);
358 ret
= request_threaded_irq(rtc
->irq
, NULL
,
359 mtk_rtc_irq_handler_thread
,
360 IRQF_ONESHOT
| IRQF_TRIGGER_HIGH
,
363 dev_err(&pdev
->dev
, "Failed to request alarm IRQ: %d: %d\n",
368 device_init_wakeup(&pdev
->dev
, 1);
370 rtc
->rtc_dev
->ops
= &mtk_rtc_ops
;
372 ret
= rtc_register_device(rtc
->rtc_dev
);
374 dev_err(&pdev
->dev
, "register rtc device failed\n");
381 free_irq(rtc
->irq
, rtc
);
385 static int mtk_rtc_remove(struct platform_device
*pdev
)
387 struct mt6397_rtc
*rtc
= platform_get_drvdata(pdev
);
389 free_irq(rtc
->irq
, rtc
);
394 #ifdef CONFIG_PM_SLEEP
395 static int mt6397_rtc_suspend(struct device
*dev
)
397 struct mt6397_rtc
*rtc
= dev_get_drvdata(dev
);
399 if (device_may_wakeup(dev
))
400 enable_irq_wake(rtc
->irq
);
405 static int mt6397_rtc_resume(struct device
*dev
)
407 struct mt6397_rtc
*rtc
= dev_get_drvdata(dev
);
409 if (device_may_wakeup(dev
))
410 disable_irq_wake(rtc
->irq
);
416 static SIMPLE_DEV_PM_OPS(mt6397_pm_ops
, mt6397_rtc_suspend
,
419 static const struct of_device_id mt6397_rtc_of_match
[] = {
420 { .compatible
= "mediatek,mt6397-rtc", },
423 MODULE_DEVICE_TABLE(of
, mt6397_rtc_of_match
);
425 static struct platform_driver mtk_rtc_driver
= {
427 .name
= "mt6397-rtc",
428 .of_match_table
= mt6397_rtc_of_match
,
429 .pm
= &mt6397_pm_ops
,
431 .probe
= mtk_rtc_probe
,
432 .remove
= mtk_rtc_remove
,
435 module_platform_driver(mtk_rtc_driver
);
437 MODULE_LICENSE("GPL v2");
438 MODULE_AUTHOR("Tianping Fang <tianping.fang@mediatek.com>");
439 MODULE_DESCRIPTION("RTC Driver for MediaTek MT6397 PMIC");