Lines Matching +full:rtc +full:-
1 // SPDX-License-Identifier: GPL-2.0-or-later
12 #include <linux/rtc.h>
19 #define rtc_err(rtc, fmt, ...) \ argument
20 dev_err(rtc->da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
25 struct rtc_device *rtc; member
29 static int da9052_rtc_enable_alarm(struct da9052_rtc *rtc, bool enable) in da9052_rtc_enable_alarm() argument
33 ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG, in da9052_rtc_enable_alarm()
37 rtc_err(rtc, "Failed to enable ALM: %d\n", ret); in da9052_rtc_enable_alarm()
39 ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG, in da9052_rtc_enable_alarm()
42 rtc_err(rtc, "Write error: %d\n", ret); in da9052_rtc_enable_alarm()
49 struct da9052_rtc *rtc = data; in da9052_rtc_irq() local
51 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF); in da9052_rtc_irq()
56 static int da9052_read_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm) in da9052_read_alarm() argument
63 ret = da9052_group_read(rtc->da9052, DA9052_ALARM_MI_REG, 5, &v[0][0]); in da9052_read_alarm()
65 rtc_err(rtc, "Failed to group read ALM: %d\n", ret); in da9052_read_alarm()
70 ret = da9052_group_read(rtc->da9052, in da9052_read_alarm()
73 rtc_err(rtc, "Failed to group read ALM: %d\n", ret); in da9052_read_alarm()
78 rtc_tm->tm_year = (v[0][4] & DA9052_RTC_YEAR) + 100; in da9052_read_alarm()
79 rtc_tm->tm_mon = (v[0][3] & DA9052_RTC_MONTH) - 1; in da9052_read_alarm()
80 rtc_tm->tm_mday = v[0][2] & DA9052_RTC_DAY; in da9052_read_alarm()
81 rtc_tm->tm_hour = v[0][1] & DA9052_RTC_HOUR; in da9052_read_alarm()
82 rtc_tm->tm_min = v[0][0] & DA9052_RTC_MIN; in da9052_read_alarm()
83 rtc_tm->tm_sec = 0; in da9052_read_alarm()
89 idx = (1-idx); in da9052_read_alarm()
92 } while (timeout--); in da9052_read_alarm()
94 rtc_err(rtc, "Timed out reading alarm time\n"); in da9052_read_alarm()
96 return -EIO; in da9052_read_alarm()
99 static int da9052_set_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm) in da9052_set_alarm() argument
101 struct da9052 *da9052 = rtc->da9052; in da9052_set_alarm()
108 if (rtc_tm->tm_sec > 0) { in da9052_set_alarm()
109 alm_time += 60 - rtc_tm->tm_sec; in da9052_set_alarm()
112 BUG_ON(rtc_tm->tm_sec); /* it will cause repeated irqs if not zero */ in da9052_set_alarm()
114 rtc_tm->tm_year -= 100; in da9052_set_alarm()
115 rtc_tm->tm_mon += 1; in da9052_set_alarm()
118 DA9052_RTC_MIN, rtc_tm->tm_min); in da9052_set_alarm()
120 rtc_err(rtc, "Failed to write ALRM MIN: %d\n", ret); in da9052_set_alarm()
124 v[0] = rtc_tm->tm_hour; in da9052_set_alarm()
125 v[1] = rtc_tm->tm_mday; in da9052_set_alarm()
126 v[2] = rtc_tm->tm_mon; in da9052_set_alarm()
133 DA9052_RTC_YEAR, rtc_tm->tm_year); in da9052_set_alarm()
135 rtc_err(rtc, "Failed to write ALRM YEAR: %d\n", ret); in da9052_set_alarm()
140 static int da9052_rtc_get_alarm_status(struct da9052_rtc *rtc) in da9052_rtc_get_alarm_status() argument
144 ret = da9052_reg_read(rtc->da9052, DA9052_ALARM_Y_REG); in da9052_rtc_get_alarm_status()
146 rtc_err(rtc, "Failed to read ALM: %d\n", ret); in da9052_rtc_get_alarm_status()
155 struct da9052_rtc *rtc = dev_get_drvdata(dev); in da9052_rtc_read_time() local
161 ret = da9052_group_read(rtc->da9052, DA9052_COUNT_S_REG, 6, &v[0][0]); in da9052_rtc_read_time()
163 rtc_err(rtc, "Failed to read RTC time : %d\n", ret); in da9052_rtc_read_time()
168 ret = da9052_group_read(rtc->da9052, in da9052_rtc_read_time()
171 rtc_err(rtc, "Failed to read RTC time : %d\n", ret); in da9052_rtc_read_time()
176 rtc_tm->tm_year = (v[0][5] & DA9052_RTC_YEAR) + 100; in da9052_rtc_read_time()
177 rtc_tm->tm_mon = (v[0][4] & DA9052_RTC_MONTH) - 1; in da9052_rtc_read_time()
178 rtc_tm->tm_mday = v[0][3] & DA9052_RTC_DAY; in da9052_rtc_read_time()
179 rtc_tm->tm_hour = v[0][2] & DA9052_RTC_HOUR; in da9052_rtc_read_time()
180 rtc_tm->tm_min = v[0][1] & DA9052_RTC_MIN; in da9052_rtc_read_time()
181 rtc_tm->tm_sec = v[0][0] & DA9052_RTC_SEC; in da9052_rtc_read_time()
186 idx = (1-idx); in da9052_rtc_read_time()
189 } while (timeout--); in da9052_rtc_read_time()
191 rtc_err(rtc, "Timed out reading time\n"); in da9052_rtc_read_time()
193 return -EIO; in da9052_rtc_read_time()
198 struct da9052_rtc *rtc; in da9052_rtc_set_time() local
202 /* DA9052 only has 6 bits for year - to represent 2000-2063 */ in da9052_rtc_set_time()
203 if ((tm->tm_year < 100) || (tm->tm_year > 163)) in da9052_rtc_set_time()
204 return -EINVAL; in da9052_rtc_set_time()
206 rtc = dev_get_drvdata(dev); in da9052_rtc_set_time()
208 v[0] = tm->tm_sec; in da9052_rtc_set_time()
209 v[1] = tm->tm_min; in da9052_rtc_set_time()
210 v[2] = tm->tm_hour; in da9052_rtc_set_time()
211 v[3] = tm->tm_mday; in da9052_rtc_set_time()
212 v[4] = tm->tm_mon + 1; in da9052_rtc_set_time()
213 v[5] = tm->tm_year - 100; in da9052_rtc_set_time()
215 ret = da9052_group_write(rtc->da9052, DA9052_COUNT_S_REG, 6, v); in da9052_rtc_set_time()
217 rtc_err(rtc, "failed to set RTC time: %d\n", ret); in da9052_rtc_set_time()
224 struct rtc_time *tm = &alrm->time; in da9052_rtc_read_alarm()
225 struct da9052_rtc *rtc = dev_get_drvdata(dev); in da9052_rtc_read_alarm() local
227 ret = da9052_read_alarm(rtc, tm); in da9052_rtc_read_alarm()
229 rtc_err(rtc, "failed to read RTC alarm: %d\n", ret); in da9052_rtc_read_alarm()
233 alrm->enabled = da9052_rtc_get_alarm_status(rtc); in da9052_rtc_read_alarm()
240 struct rtc_time *tm = &alrm->time; in da9052_rtc_set_alarm()
241 struct da9052_rtc *rtc = dev_get_drvdata(dev); in da9052_rtc_set_alarm() local
243 /* DA9052 only has 6 bits for year - to represent 2000-2063 */ in da9052_rtc_set_alarm()
244 if ((tm->tm_year < 100) || (tm->tm_year > 163)) in da9052_rtc_set_alarm()
245 return -EINVAL; in da9052_rtc_set_alarm()
247 ret = da9052_rtc_enable_alarm(rtc, 0); in da9052_rtc_set_alarm()
251 ret = da9052_set_alarm(rtc, tm); in da9052_rtc_set_alarm()
255 ret = da9052_rtc_enable_alarm(rtc, 1); in da9052_rtc_set_alarm()
261 struct da9052_rtc *rtc = dev_get_drvdata(dev); in da9052_rtc_alarm_irq_enable() local
263 return da9052_rtc_enable_alarm(rtc, enabled); in da9052_rtc_alarm_irq_enable()
276 struct da9052_rtc *rtc; in da9052_rtc_probe() local
279 rtc = devm_kzalloc(&pdev->dev, sizeof(struct da9052_rtc), GFP_KERNEL); in da9052_rtc_probe()
280 if (!rtc) in da9052_rtc_probe()
281 return -ENOMEM; in da9052_rtc_probe()
283 rtc->da9052 = dev_get_drvdata(pdev->dev.parent); in da9052_rtc_probe()
284 platform_set_drvdata(pdev, rtc); in da9052_rtc_probe()
286 ret = da9052_reg_write(rtc->da9052, DA9052_BBAT_CONT_REG, 0xFE); in da9052_rtc_probe()
288 rtc_err(rtc, in da9052_rtc_probe()
289 "Failed to setup RTC battery charging: %d\n", ret); in da9052_rtc_probe()
293 ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG, in da9052_rtc_probe()
296 rtc_err(rtc, "Failed to disable TICKS: %d\n", ret); in da9052_rtc_probe()
298 device_init_wakeup(&pdev->dev, true); in da9052_rtc_probe()
299 rtc->rtc = devm_rtc_allocate_device(&pdev->dev); in da9052_rtc_probe()
300 if (IS_ERR(rtc->rtc)) in da9052_rtc_probe()
301 return PTR_ERR(rtc->rtc); in da9052_rtc_probe()
303 rtc->rtc->ops = &da9052_rtc_ops; in da9052_rtc_probe()
304 rtc->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; in da9052_rtc_probe()
305 rtc->rtc->range_max = RTC_TIMESTAMP_END_2063; in da9052_rtc_probe()
307 ret = devm_rtc_register_device(rtc->rtc); in da9052_rtc_probe()
311 ret = da9052_request_irq(rtc->da9052, DA9052_IRQ_ALARM, "ALM", in da9052_rtc_probe()
312 da9052_rtc_irq, rtc); in da9052_rtc_probe()
314 rtc_err(rtc, "irq registration failed: %d\n", ret); in da9052_rtc_probe()
324 .name = "da9052-rtc",
331 MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
333 MODULE_ALIAS("platform:da9052-rtc");