1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014-2015 MediaTek Inc.
4 * Author: Tianping.Fang <tianping.fang@mediatek.com>
7 #include <linux/delay.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/regmap.h>
11 #include <linux/rtc.h>
12 #include <linux/irqdomain.h>
13 #include <linux/platform_device.h>
14 #include <linux/of_address.h>
15 #include <linux/of_irq.h>
17 #include <linux/mfd/mt6397/core.h>
19 #define RTC_BBPU 0x0000
20 #define RTC_BBPU_CBUSY BIT(6)
22 #define RTC_WRTGR 0x003c
24 #define RTC_IRQ_STA 0x0002
25 #define RTC_IRQ_STA_AL BIT(0)
26 #define RTC_IRQ_STA_LP BIT(3)
28 #define RTC_IRQ_EN 0x0004
29 #define RTC_IRQ_EN_AL BIT(0)
30 #define RTC_IRQ_EN_ONESHOT BIT(2)
31 #define RTC_IRQ_EN_LP BIT(3)
32 #define RTC_IRQ_EN_ONESHOT_AL (RTC_IRQ_EN_ONESHOT | RTC_IRQ_EN_AL)
34 #define RTC_AL_MASK 0x0008
35 #define RTC_AL_MASK_DOW BIT(4)
37 #define RTC_TC_SEC 0x000a
38 /* Min, Hour, Dom... register offset to RTC_TC_SEC */
39 #define RTC_OFFSET_SEC 0
40 #define RTC_OFFSET_MIN 1
41 #define RTC_OFFSET_HOUR 2
42 #define RTC_OFFSET_DOM 3
43 #define RTC_OFFSET_DOW 4
44 #define RTC_OFFSET_MTH 5
45 #define RTC_OFFSET_YEAR 6
46 #define RTC_OFFSET_COUNT 7
48 #define RTC_AL_SEC 0x0018
50 #define RTC_AL_SEC_MASK 0x003f
51 #define RTC_AL_MIN_MASK 0x003f
52 #define RTC_AL_HOU_MASK 0x001f
53 #define RTC_AL_DOM_MASK 0x001f
54 #define RTC_AL_DOW_MASK 0x0007
55 #define RTC_AL_MTH_MASK 0x000f
56 #define RTC_AL_YEA_MASK 0x007f
58 #define RTC_PDN2 0x002e
59 #define RTC_PDN2_PWRON_ALARM BIT(4)
61 #define RTC_MIN_YEAR 1968
62 #define RTC_BASE_YEAR 1900
63 #define RTC_NUM_YEARS 128
64 #define RTC_MIN_YEAR_OFFSET (RTC_MIN_YEAR - RTC_BASE_YEAR)
68 struct rtc_device *rtc_dev;
70 struct regmap *regmap;
75 static int mtk_rtc_write_trigger(struct mt6397_rtc *rtc)
77 unsigned long timeout = jiffies + HZ;
81 ret = regmap_write(rtc->regmap, rtc->addr_base + RTC_WRTGR, 1);
86 ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_BBPU,
90 if (!(data & RTC_BBPU_CBUSY))
92 if (time_after(jiffies, timeout)) {
102 static irqreturn_t mtk_rtc_irq_handler_thread(int irq, void *data)
104 struct mt6397_rtc *rtc = data;
108 ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_IRQ_STA, &irqsta);
109 if ((ret >= 0) && (irqsta & RTC_IRQ_STA_AL)) {
110 rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
111 irqen = irqsta & ~RTC_IRQ_EN_AL;
112 mutex_lock(&rtc->lock);
113 if (regmap_write(rtc->regmap, rtc->addr_base + RTC_IRQ_EN,
115 mtk_rtc_write_trigger(rtc);
116 mutex_unlock(&rtc->lock);
124 static int __mtk_rtc_read_time(struct mt6397_rtc *rtc,
125 struct rtc_time *tm, int *sec)
128 u16 data[RTC_OFFSET_COUNT];
130 mutex_lock(&rtc->lock);
131 ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_TC_SEC,
132 data, RTC_OFFSET_COUNT);
136 tm->tm_sec = data[RTC_OFFSET_SEC];
137 tm->tm_min = data[RTC_OFFSET_MIN];
138 tm->tm_hour = data[RTC_OFFSET_HOUR];
139 tm->tm_mday = data[RTC_OFFSET_DOM];
140 tm->tm_mon = data[RTC_OFFSET_MTH];
141 tm->tm_year = data[RTC_OFFSET_YEAR];
143 ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_TC_SEC, sec);
145 mutex_unlock(&rtc->lock);
149 static int mtk_rtc_read_time(struct device *dev, struct rtc_time *tm)
152 struct mt6397_rtc *rtc = dev_get_drvdata(dev);
156 ret = __mtk_rtc_read_time(rtc, tm, &sec);
159 } while (sec < tm->tm_sec);
161 /* HW register use 7 bits to store year data, minus
162 * RTC_MIN_YEAR_OFFSET before write year data to register, and plus
163 * RTC_MIN_YEAR_OFFSET back after read year from register
165 tm->tm_year += RTC_MIN_YEAR_OFFSET;
167 /* HW register start mon from one, but tm_mon start from zero. */
169 time = rtc_tm_to_time64(tm);
171 /* rtc_tm_to_time64 covert Gregorian date to seconds since
172 * 01-01-1970 00:00:00, and this date is Thursday.
174 days = div_s64(time, 86400);
175 tm->tm_wday = (days + 4) % 7;
181 static int mtk_rtc_set_time(struct device *dev, struct rtc_time *tm)
183 struct mt6397_rtc *rtc = dev_get_drvdata(dev);
185 u16 data[RTC_OFFSET_COUNT];
187 tm->tm_year -= RTC_MIN_YEAR_OFFSET;
190 data[RTC_OFFSET_SEC] = tm->tm_sec;
191 data[RTC_OFFSET_MIN] = tm->tm_min;
192 data[RTC_OFFSET_HOUR] = tm->tm_hour;
193 data[RTC_OFFSET_DOM] = tm->tm_mday;
194 data[RTC_OFFSET_MTH] = tm->tm_mon;
195 data[RTC_OFFSET_YEAR] = tm->tm_year;
197 mutex_lock(&rtc->lock);
198 ret = regmap_bulk_write(rtc->regmap, rtc->addr_base + RTC_TC_SEC,
199 data, RTC_OFFSET_COUNT);
203 /* Time register write to hardware after call trigger function */
204 ret = mtk_rtc_write_trigger(rtc);
207 mutex_unlock(&rtc->lock);
211 static int mtk_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
213 struct rtc_time *tm = &alm->time;
214 struct mt6397_rtc *rtc = dev_get_drvdata(dev);
217 u16 data[RTC_OFFSET_COUNT];
219 mutex_lock(&rtc->lock);
220 ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_IRQ_EN, &irqen);
223 ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_PDN2, &pdn2);
227 ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_AL_SEC,
228 data, RTC_OFFSET_COUNT);
232 alm->enabled = !!(irqen & RTC_IRQ_EN_AL);
233 alm->pending = !!(pdn2 & RTC_PDN2_PWRON_ALARM);
234 mutex_unlock(&rtc->lock);
236 tm->tm_sec = data[RTC_OFFSET_SEC] & RTC_AL_SEC_MASK;
237 tm->tm_min = data[RTC_OFFSET_MIN] & RTC_AL_MIN_MASK;
238 tm->tm_hour = data[RTC_OFFSET_HOUR] & RTC_AL_HOU_MASK;
239 tm->tm_mday = data[RTC_OFFSET_DOM] & RTC_AL_DOM_MASK;
240 tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_AL_MTH_MASK;
241 tm->tm_year = data[RTC_OFFSET_YEAR] & RTC_AL_YEA_MASK;
243 tm->tm_year += RTC_MIN_YEAR_OFFSET;
248 mutex_unlock(&rtc->lock);
252 static int mtk_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
254 struct rtc_time *tm = &alm->time;
255 struct mt6397_rtc *rtc = dev_get_drvdata(dev);
257 u16 data[RTC_OFFSET_COUNT];
259 tm->tm_year -= RTC_MIN_YEAR_OFFSET;
262 mutex_lock(&rtc->lock);
263 ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_AL_SEC,
264 data, RTC_OFFSET_COUNT);
268 data[RTC_OFFSET_SEC] = ((data[RTC_OFFSET_SEC] & ~(RTC_AL_SEC_MASK)) |
269 (tm->tm_sec & RTC_AL_SEC_MASK));
270 data[RTC_OFFSET_MIN] = ((data[RTC_OFFSET_MIN] & ~(RTC_AL_MIN_MASK)) |
271 (tm->tm_min & RTC_AL_MIN_MASK));
272 data[RTC_OFFSET_HOUR] = ((data[RTC_OFFSET_HOUR] & ~(RTC_AL_HOU_MASK)) |
273 (tm->tm_hour & RTC_AL_HOU_MASK));
274 data[RTC_OFFSET_DOM] = ((data[RTC_OFFSET_DOM] & ~(RTC_AL_DOM_MASK)) |
275 (tm->tm_mday & RTC_AL_DOM_MASK));
276 data[RTC_OFFSET_MTH] = ((data[RTC_OFFSET_MTH] & ~(RTC_AL_MTH_MASK)) |
277 (tm->tm_mon & RTC_AL_MTH_MASK));
278 data[RTC_OFFSET_YEAR] = ((data[RTC_OFFSET_YEAR] & ~(RTC_AL_YEA_MASK)) |
279 (tm->tm_year & RTC_AL_YEA_MASK));
282 ret = regmap_bulk_write(rtc->regmap,
283 rtc->addr_base + RTC_AL_SEC,
284 data, RTC_OFFSET_COUNT);
287 ret = regmap_write(rtc->regmap, rtc->addr_base + RTC_AL_MASK,
291 ret = regmap_update_bits(rtc->regmap,
292 rtc->addr_base + RTC_IRQ_EN,
293 RTC_IRQ_EN_ONESHOT_AL,
294 RTC_IRQ_EN_ONESHOT_AL);
298 ret = regmap_update_bits(rtc->regmap,
299 rtc->addr_base + RTC_IRQ_EN,
300 RTC_IRQ_EN_ONESHOT_AL, 0);
305 /* All alarm time register write to hardware after calling
306 * mtk_rtc_write_trigger. This can avoid race condition if alarm
307 * occur happen during writing alarm time register.
309 ret = mtk_rtc_write_trigger(rtc);
311 mutex_unlock(&rtc->lock);
315 static const struct rtc_class_ops mtk_rtc_ops = {
316 .read_time = mtk_rtc_read_time,
317 .set_time = mtk_rtc_set_time,
318 .read_alarm = mtk_rtc_read_alarm,
319 .set_alarm = mtk_rtc_set_alarm,
322 static int mtk_rtc_probe(struct platform_device *pdev)
324 struct resource *res;
325 struct mt6397_chip *mt6397_chip = dev_get_drvdata(pdev->dev.parent);
326 struct mt6397_rtc *rtc;
329 rtc = devm_kzalloc(&pdev->dev, sizeof(struct mt6397_rtc), GFP_KERNEL);
333 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
334 rtc->addr_base = res->start;
336 rtc->irq = platform_get_irq(pdev, 0);
340 rtc->regmap = mt6397_chip->regmap;
341 rtc->dev = &pdev->dev;
342 mutex_init(&rtc->lock);
344 platform_set_drvdata(pdev, rtc);
346 rtc->rtc_dev = devm_rtc_allocate_device(rtc->dev);
347 if (IS_ERR(rtc->rtc_dev))
348 return PTR_ERR(rtc->rtc_dev);
350 ret = request_threaded_irq(rtc->irq, NULL,
351 mtk_rtc_irq_handler_thread,
352 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
355 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
360 device_init_wakeup(&pdev->dev, 1);
362 rtc->rtc_dev->ops = &mtk_rtc_ops;
364 ret = rtc_register_device(rtc->rtc_dev);
371 free_irq(rtc->irq, rtc);
375 static int mtk_rtc_remove(struct platform_device *pdev)
377 struct mt6397_rtc *rtc = platform_get_drvdata(pdev);
379 free_irq(rtc->irq, rtc);
384 #ifdef CONFIG_PM_SLEEP
385 static int mt6397_rtc_suspend(struct device *dev)
387 struct mt6397_rtc *rtc = dev_get_drvdata(dev);
389 if (device_may_wakeup(dev))
390 enable_irq_wake(rtc->irq);
395 static int mt6397_rtc_resume(struct device *dev)
397 struct mt6397_rtc *rtc = dev_get_drvdata(dev);
399 if (device_may_wakeup(dev))
400 disable_irq_wake(rtc->irq);
406 static SIMPLE_DEV_PM_OPS(mt6397_pm_ops, mt6397_rtc_suspend,
409 static const struct of_device_id mt6397_rtc_of_match[] = {
410 { .compatible = "mediatek,mt6397-rtc", },
413 MODULE_DEVICE_TABLE(of, mt6397_rtc_of_match);
415 static struct platform_driver mtk_rtc_driver = {
417 .name = "mt6397-rtc",
418 .of_match_table = mt6397_rtc_of_match,
419 .pm = &mt6397_pm_ops,
421 .probe = mtk_rtc_probe,
422 .remove = mtk_rtc_remove,
425 module_platform_driver(mtk_rtc_driver);
427 MODULE_LICENSE("GPL v2");
428 MODULE_AUTHOR("Tianping Fang <tianping.fang@mediatek.com>");
429 MODULE_DESCRIPTION("RTC Driver for MediaTek MT6397 PMIC");