powerpc/powernv: Rename alloc_m64_pe() to reserve_m64_pe()
[linux/fpc-iii.git] / drivers / rtc / rtc-da9052.c
blobe5c9486cf452b7dfc0ed467a367700a0fe773b4f
1 /*
2 * Real time clock driver for DA9052
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
6 * Author: Dajun Dajun Chen <dajun.chen@diasemi.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/rtc.h>
18 #include <linux/err.h>
20 #include <linux/mfd/da9052/da9052.h>
21 #include <linux/mfd/da9052/reg.h>
23 #define rtc_err(rtc, fmt, ...) \
24 dev_err(rtc->da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
26 struct da9052_rtc {
27 struct rtc_device *rtc;
28 struct da9052 *da9052;
31 static int da9052_rtc_enable_alarm(struct da9052_rtc *rtc, bool enable)
33 int ret;
34 if (enable) {
35 ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
36 DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON,
37 DA9052_ALARM_Y_ALARM_ON);
38 if (ret != 0)
39 rtc_err(rtc, "Failed to enable ALM: %d\n", ret);
40 } else {
41 ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
42 DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON, 0);
43 if (ret != 0)
44 rtc_err(rtc, "Write error: %d\n", ret);
46 return ret;
49 static irqreturn_t da9052_rtc_irq(int irq, void *data)
51 struct da9052_rtc *rtc = data;
53 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
55 return IRQ_HANDLED;
58 static int da9052_read_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
60 int ret;
61 uint8_t v[5];
63 ret = da9052_group_read(rtc->da9052, DA9052_ALARM_MI_REG, 5, v);
64 if (ret != 0) {
65 rtc_err(rtc, "Failed to group read ALM: %d\n", ret);
66 return ret;
69 rtc_tm->tm_year = (v[4] & DA9052_RTC_YEAR) + 100;
70 rtc_tm->tm_mon = (v[3] & DA9052_RTC_MONTH) - 1;
71 rtc_tm->tm_mday = v[2] & DA9052_RTC_DAY;
72 rtc_tm->tm_hour = v[1] & DA9052_RTC_HOUR;
73 rtc_tm->tm_min = v[0] & DA9052_RTC_MIN;
75 ret = rtc_valid_tm(rtc_tm);
76 return ret;
79 static int da9052_set_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
81 struct da9052 *da9052 = rtc->da9052;
82 unsigned long alm_time;
83 int ret;
84 uint8_t v[3];
86 ret = rtc_tm_to_time(rtc_tm, &alm_time);
87 if (ret != 0)
88 return ret;
90 if (rtc_tm->tm_sec > 0) {
91 alm_time += 60 - rtc_tm->tm_sec;
92 rtc_time_to_tm(alm_time, rtc_tm);
94 BUG_ON(rtc_tm->tm_sec); /* it will cause repeated irqs if not zero */
96 rtc_tm->tm_year -= 100;
97 rtc_tm->tm_mon += 1;
99 ret = da9052_reg_update(da9052, DA9052_ALARM_MI_REG,
100 DA9052_RTC_MIN, rtc_tm->tm_min);
101 if (ret != 0) {
102 rtc_err(rtc, "Failed to write ALRM MIN: %d\n", ret);
103 return ret;
106 v[0] = rtc_tm->tm_hour;
107 v[1] = rtc_tm->tm_mday;
108 v[2] = rtc_tm->tm_mon;
110 ret = da9052_group_write(da9052, DA9052_ALARM_H_REG, 3, v);
111 if (ret < 0)
112 return ret;
114 ret = da9052_reg_update(da9052, DA9052_ALARM_Y_REG,
115 DA9052_RTC_YEAR, rtc_tm->tm_year);
116 if (ret != 0)
117 rtc_err(rtc, "Failed to write ALRM YEAR: %d\n", ret);
119 return ret;
122 static int da9052_rtc_get_alarm_status(struct da9052_rtc *rtc)
124 int ret;
126 ret = da9052_reg_read(rtc->da9052, DA9052_ALARM_Y_REG);
127 if (ret < 0) {
128 rtc_err(rtc, "Failed to read ALM: %d\n", ret);
129 return ret;
132 return !!(ret&DA9052_ALARM_Y_ALARM_ON);
135 static int da9052_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
137 struct da9052_rtc *rtc = dev_get_drvdata(dev);
138 uint8_t v[6];
139 int ret;
141 ret = da9052_group_read(rtc->da9052, DA9052_COUNT_S_REG, 6, v);
142 if (ret < 0) {
143 rtc_err(rtc, "Failed to read RTC time : %d\n", ret);
144 return ret;
147 rtc_tm->tm_year = (v[5] & DA9052_RTC_YEAR) + 100;
148 rtc_tm->tm_mon = (v[4] & DA9052_RTC_MONTH) - 1;
149 rtc_tm->tm_mday = v[3] & DA9052_RTC_DAY;
150 rtc_tm->tm_hour = v[2] & DA9052_RTC_HOUR;
151 rtc_tm->tm_min = v[1] & DA9052_RTC_MIN;
152 rtc_tm->tm_sec = v[0] & DA9052_RTC_SEC;
154 ret = rtc_valid_tm(rtc_tm);
155 return ret;
158 static int da9052_rtc_set_time(struct device *dev, struct rtc_time *tm)
160 struct da9052_rtc *rtc;
161 uint8_t v[6];
162 int ret;
164 rtc = dev_get_drvdata(dev);
166 v[0] = tm->tm_sec;
167 v[1] = tm->tm_min;
168 v[2] = tm->tm_hour;
169 v[3] = tm->tm_mday;
170 v[4] = tm->tm_mon + 1;
171 v[5] = tm->tm_year - 100;
173 ret = da9052_group_write(rtc->da9052, DA9052_COUNT_S_REG, 6, v);
174 if (ret < 0)
175 rtc_err(rtc, "failed to set RTC time: %d\n", ret);
176 return ret;
179 static int da9052_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
181 int ret;
182 struct rtc_time *tm = &alrm->time;
183 struct da9052_rtc *rtc = dev_get_drvdata(dev);
185 ret = da9052_read_alarm(rtc, tm);
186 if (ret < 0) {
187 rtc_err(rtc, "failed to read RTC alarm: %d\n", ret);
188 return ret;
191 alrm->enabled = da9052_rtc_get_alarm_status(rtc);
192 return 0;
195 static int da9052_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
197 int ret;
198 struct rtc_time *tm = &alrm->time;
199 struct da9052_rtc *rtc = dev_get_drvdata(dev);
201 ret = da9052_rtc_enable_alarm(rtc, 0);
202 if (ret < 0)
203 return ret;
205 ret = da9052_set_alarm(rtc, tm);
206 if (ret < 0)
207 return ret;
209 ret = da9052_rtc_enable_alarm(rtc, 1);
210 return ret;
213 static int da9052_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
215 struct da9052_rtc *rtc = dev_get_drvdata(dev);
217 return da9052_rtc_enable_alarm(rtc, enabled);
220 static const struct rtc_class_ops da9052_rtc_ops = {
221 .read_time = da9052_rtc_read_time,
222 .set_time = da9052_rtc_set_time,
223 .read_alarm = da9052_rtc_read_alarm,
224 .set_alarm = da9052_rtc_set_alarm,
225 .alarm_irq_enable = da9052_rtc_alarm_irq_enable,
228 static int da9052_rtc_probe(struct platform_device *pdev)
230 struct da9052_rtc *rtc;
231 int ret;
233 rtc = devm_kzalloc(&pdev->dev, sizeof(struct da9052_rtc), GFP_KERNEL);
234 if (!rtc)
235 return -ENOMEM;
237 rtc->da9052 = dev_get_drvdata(pdev->dev.parent);
238 platform_set_drvdata(pdev, rtc);
240 ret = da9052_reg_write(rtc->da9052, DA9052_BBAT_CONT_REG, 0xFE);
241 if (ret < 0) {
242 rtc_err(rtc,
243 "Failed to setup RTC battery charging: %d\n", ret);
244 return ret;
247 ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
248 DA9052_ALARM_Y_TICK_ON, 0);
249 if (ret != 0)
250 rtc_err(rtc, "Failed to disable TICKS: %d\n", ret);
252 ret = da9052_request_irq(rtc->da9052, DA9052_IRQ_ALARM, "ALM",
253 da9052_rtc_irq, rtc);
254 if (ret != 0) {
255 rtc_err(rtc, "irq registration failed: %d\n", ret);
256 return ret;
259 rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
260 &da9052_rtc_ops, THIS_MODULE);
261 return PTR_ERR_OR_ZERO(rtc->rtc);
264 static struct platform_driver da9052_rtc_driver = {
265 .probe = da9052_rtc_probe,
266 .driver = {
267 .name = "da9052-rtc",
268 .owner = THIS_MODULE,
272 module_platform_driver(da9052_rtc_driver);
274 MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
275 MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
276 MODULE_LICENSE("GPL");
277 MODULE_ALIAS("platform:da9052-rtc");