Lines Matching +full:mt6323 +full:- +full:rtc
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014-2015 MediaTek Inc.
15 #include <linux/rtc.h>
16 #include <linux/mfd/mt6397/rtc.h>
19 static int mtk_rtc_write_trigger(struct mt6397_rtc *rtc) in mtk_rtc_write_trigger() argument
24 ret = regmap_write(rtc->regmap, rtc->addr_base + rtc->data->wrtgr, 1); in mtk_rtc_write_trigger()
28 ret = regmap_read_poll_timeout(rtc->regmap, in mtk_rtc_write_trigger()
29 rtc->addr_base + RTC_BBPU, data, in mtk_rtc_write_trigger()
34 dev_err(rtc->rtc_dev->dev.parent, in mtk_rtc_write_trigger()
42 struct mt6397_rtc *rtc = data; in mtk_rtc_irq_handler_thread() local
46 ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_IRQ_STA, &irqsta); in mtk_rtc_irq_handler_thread()
48 rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF); in mtk_rtc_irq_handler_thread()
50 mutex_lock(&rtc->lock); in mtk_rtc_irq_handler_thread()
51 if (regmap_write(rtc->regmap, rtc->addr_base + RTC_IRQ_EN, in mtk_rtc_irq_handler_thread()
53 mtk_rtc_write_trigger(rtc); in mtk_rtc_irq_handler_thread()
54 mutex_unlock(&rtc->lock); in mtk_rtc_irq_handler_thread()
62 static int __mtk_rtc_read_time(struct mt6397_rtc *rtc, in __mtk_rtc_read_time() argument
68 mutex_lock(&rtc->lock); in __mtk_rtc_read_time()
69 ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_TC_SEC, in __mtk_rtc_read_time()
74 tm->tm_sec = data[RTC_OFFSET_SEC]; in __mtk_rtc_read_time()
75 tm->tm_min = data[RTC_OFFSET_MIN]; in __mtk_rtc_read_time()
76 tm->tm_hour = data[RTC_OFFSET_HOUR]; in __mtk_rtc_read_time()
77 tm->tm_mday = data[RTC_OFFSET_DOM]; in __mtk_rtc_read_time()
78 tm->tm_wday = data[RTC_OFFSET_DOW]; in __mtk_rtc_read_time()
79 tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_TC_MTH_MASK; in __mtk_rtc_read_time()
80 tm->tm_year = data[RTC_OFFSET_YEAR]; in __mtk_rtc_read_time()
82 ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_TC_SEC, sec); in __mtk_rtc_read_time()
84 mutex_unlock(&rtc->lock); in __mtk_rtc_read_time()
90 struct mt6397_rtc *rtc = dev_get_drvdata(dev); in mtk_rtc_read_time() local
94 ret = __mtk_rtc_read_time(rtc, tm, &sec); in mtk_rtc_read_time()
97 } while (sec < tm->tm_sec); in mtk_rtc_read_time()
100 tm->tm_mon--; in mtk_rtc_read_time()
101 tm->tm_wday--; in mtk_rtc_read_time()
109 struct mt6397_rtc *rtc = dev_get_drvdata(dev); in mtk_rtc_set_time() local
113 tm->tm_mon++; in mtk_rtc_set_time()
114 tm->tm_wday++; in mtk_rtc_set_time()
116 data[RTC_OFFSET_SEC] = tm->tm_sec; in mtk_rtc_set_time()
117 data[RTC_OFFSET_MIN] = tm->tm_min; in mtk_rtc_set_time()
118 data[RTC_OFFSET_HOUR] = tm->tm_hour; in mtk_rtc_set_time()
119 data[RTC_OFFSET_DOM] = tm->tm_mday; in mtk_rtc_set_time()
120 data[RTC_OFFSET_DOW] = tm->tm_wday; in mtk_rtc_set_time()
121 data[RTC_OFFSET_MTH] = tm->tm_mon; in mtk_rtc_set_time()
122 data[RTC_OFFSET_YEAR] = tm->tm_year; in mtk_rtc_set_time()
124 mutex_lock(&rtc->lock); in mtk_rtc_set_time()
125 ret = regmap_bulk_write(rtc->regmap, rtc->addr_base + RTC_TC_SEC, in mtk_rtc_set_time()
131 ret = mtk_rtc_write_trigger(rtc); in mtk_rtc_set_time()
134 mutex_unlock(&rtc->lock); in mtk_rtc_set_time()
140 struct rtc_time *tm = &alm->time; in mtk_rtc_read_alarm()
141 struct mt6397_rtc *rtc = dev_get_drvdata(dev); in mtk_rtc_read_alarm() local
146 mutex_lock(&rtc->lock); in mtk_rtc_read_alarm()
147 ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_IRQ_EN, &irqen); in mtk_rtc_read_alarm()
150 ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_PDN2, &pdn2); in mtk_rtc_read_alarm()
154 ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_AL_SEC, in mtk_rtc_read_alarm()
159 alm->enabled = !!(irqen & RTC_IRQ_EN_AL); in mtk_rtc_read_alarm()
160 alm->pending = !!(pdn2 & RTC_PDN2_PWRON_ALARM); in mtk_rtc_read_alarm()
161 mutex_unlock(&rtc->lock); in mtk_rtc_read_alarm()
163 tm->tm_sec = data[RTC_OFFSET_SEC] & RTC_AL_SEC_MASK; in mtk_rtc_read_alarm()
164 tm->tm_min = data[RTC_OFFSET_MIN] & RTC_AL_MIN_MASK; in mtk_rtc_read_alarm()
165 tm->tm_hour = data[RTC_OFFSET_HOUR] & RTC_AL_HOU_MASK; in mtk_rtc_read_alarm()
166 tm->tm_mday = data[RTC_OFFSET_DOM] & RTC_AL_DOM_MASK; in mtk_rtc_read_alarm()
167 tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_AL_MTH_MASK; in mtk_rtc_read_alarm()
168 tm->tm_year = data[RTC_OFFSET_YEAR] & RTC_AL_YEA_MASK; in mtk_rtc_read_alarm()
170 tm->tm_mon--; in mtk_rtc_read_alarm()
174 mutex_unlock(&rtc->lock); in mtk_rtc_read_alarm()
180 struct rtc_time *tm = &alm->time; in mtk_rtc_set_alarm()
181 struct mt6397_rtc *rtc = dev_get_drvdata(dev); in mtk_rtc_set_alarm() local
185 tm->tm_mon++; in mtk_rtc_set_alarm()
187 mutex_lock(&rtc->lock); in mtk_rtc_set_alarm()
188 ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_AL_SEC, in mtk_rtc_set_alarm()
194 (tm->tm_sec & RTC_AL_SEC_MASK)); in mtk_rtc_set_alarm()
196 (tm->tm_min & RTC_AL_MIN_MASK)); in mtk_rtc_set_alarm()
198 (tm->tm_hour & RTC_AL_HOU_MASK)); in mtk_rtc_set_alarm()
200 (tm->tm_mday & RTC_AL_DOM_MASK)); in mtk_rtc_set_alarm()
202 (tm->tm_mon & RTC_AL_MTH_MASK)); in mtk_rtc_set_alarm()
204 (tm->tm_year & RTC_AL_YEA_MASK)); in mtk_rtc_set_alarm()
206 if (alm->enabled) { in mtk_rtc_set_alarm()
207 ret = regmap_bulk_write(rtc->regmap, in mtk_rtc_set_alarm()
208 rtc->addr_base + RTC_AL_SEC, in mtk_rtc_set_alarm()
212 ret = regmap_write(rtc->regmap, rtc->addr_base + RTC_AL_MASK, in mtk_rtc_set_alarm()
216 ret = regmap_update_bits(rtc->regmap, in mtk_rtc_set_alarm()
217 rtc->addr_base + RTC_IRQ_EN, in mtk_rtc_set_alarm()
223 ret = regmap_update_bits(rtc->regmap, in mtk_rtc_set_alarm()
224 rtc->addr_base + RTC_IRQ_EN, in mtk_rtc_set_alarm()
234 ret = mtk_rtc_write_trigger(rtc); in mtk_rtc_set_alarm()
236 mutex_unlock(&rtc->lock); in mtk_rtc_set_alarm()
250 struct mt6397_chip *mt6397_chip = dev_get_drvdata(pdev->dev.parent); in mtk_rtc_probe()
251 struct mt6397_rtc *rtc; in mtk_rtc_probe() local
254 rtc = devm_kzalloc(&pdev->dev, sizeof(struct mt6397_rtc), GFP_KERNEL); in mtk_rtc_probe()
255 if (!rtc) in mtk_rtc_probe()
256 return -ENOMEM; in mtk_rtc_probe()
260 return -EINVAL; in mtk_rtc_probe()
261 rtc->addr_base = res->start; in mtk_rtc_probe()
263 rtc->data = of_device_get_match_data(&pdev->dev); in mtk_rtc_probe()
265 rtc->irq = platform_get_irq(pdev, 0); in mtk_rtc_probe()
266 if (rtc->irq < 0) in mtk_rtc_probe()
267 return rtc->irq; in mtk_rtc_probe()
269 rtc->regmap = mt6397_chip->regmap; in mtk_rtc_probe()
270 mutex_init(&rtc->lock); in mtk_rtc_probe()
272 platform_set_drvdata(pdev, rtc); in mtk_rtc_probe()
274 rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev); in mtk_rtc_probe()
275 if (IS_ERR(rtc->rtc_dev)) in mtk_rtc_probe()
276 return PTR_ERR(rtc->rtc_dev); in mtk_rtc_probe()
278 ret = devm_request_threaded_irq(&pdev->dev, rtc->irq, NULL, in mtk_rtc_probe()
281 "mt6397-rtc", rtc); in mtk_rtc_probe()
284 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n", in mtk_rtc_probe()
285 rtc->irq, ret); in mtk_rtc_probe()
289 device_init_wakeup(&pdev->dev, 1); in mtk_rtc_probe()
291 rtc->rtc_dev->ops = &mtk_rtc_ops; in mtk_rtc_probe()
292 rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_1900; in mtk_rtc_probe()
293 rtc->rtc_dev->range_max = mktime64(2027, 12, 31, 23, 59, 59); in mtk_rtc_probe()
294 rtc->rtc_dev->start_secs = mktime64(1968, 1, 2, 0, 0, 0); in mtk_rtc_probe()
295 rtc->rtc_dev->set_start_time = true; in mtk_rtc_probe()
297 return devm_rtc_register_device(rtc->rtc_dev); in mtk_rtc_probe()
303 struct mt6397_rtc *rtc = dev_get_drvdata(dev); in mt6397_rtc_suspend() local
306 enable_irq_wake(rtc->irq); in mt6397_rtc_suspend()
313 struct mt6397_rtc *rtc = dev_get_drvdata(dev); in mt6397_rtc_resume() local
316 disable_irq_wake(rtc->irq); in mt6397_rtc_resume()
334 { .compatible = "mediatek,mt6323-rtc", .data = &mt6397_rtc_data },
335 { .compatible = "mediatek,mt6358-rtc", .data = &mt6358_rtc_data },
336 { .compatible = "mediatek,mt6397-rtc", .data = &mt6397_rtc_data },
343 .name = "mt6397-rtc",
354 MODULE_DESCRIPTION("RTC Driver for MediaTek MT6397 PMIC");