GNU Linux-libre 4.4.289-gnu1
[releases.git] / drivers / rtc / rtc-88pm80x.c
1 /*
2  * Real Time Clock driver for Marvell 88PM80x PMIC
3  *
4  * Copyright (c) 2012 Marvell International Ltd.
5  *  Wenzeng Chen<wzch@marvell.com>
6  *  Qiao Zhou <zhouqiao@marvell.com>
7  *
8  * This file is subject to the terms and conditions of the GNU General
9  * Public License. See the file "COPYING" in the main directory of this
10  * archive for more details.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/regmap.h>
26 #include <linux/mfd/core.h>
27 #include <linux/mfd/88pm80x.h>
28 #include <linux/rtc.h>
29
30 #define PM800_RTC_COUNTER1              (0xD1)
31 #define PM800_RTC_COUNTER2              (0xD2)
32 #define PM800_RTC_COUNTER3              (0xD3)
33 #define PM800_RTC_COUNTER4              (0xD4)
34 #define PM800_RTC_EXPIRE1_1             (0xD5)
35 #define PM800_RTC_EXPIRE1_2             (0xD6)
36 #define PM800_RTC_EXPIRE1_3             (0xD7)
37 #define PM800_RTC_EXPIRE1_4             (0xD8)
38 #define PM800_RTC_TRIM1                 (0xD9)
39 #define PM800_RTC_TRIM2                 (0xDA)
40 #define PM800_RTC_TRIM3                 (0xDB)
41 #define PM800_RTC_TRIM4                 (0xDC)
42 #define PM800_RTC_EXPIRE2_1             (0xDD)
43 #define PM800_RTC_EXPIRE2_2             (0xDE)
44 #define PM800_RTC_EXPIRE2_3             (0xDF)
45 #define PM800_RTC_EXPIRE2_4             (0xE0)
46
47 #define PM800_POWER_DOWN_LOG1   (0xE5)
48 #define PM800_POWER_DOWN_LOG2   (0xE6)
49
50 struct pm80x_rtc_info {
51         struct pm80x_chip *chip;
52         struct regmap *map;
53         struct rtc_device *rtc_dev;
54         struct device *dev;
55         struct delayed_work calib_work;
56
57         int irq;
58         int vrtc;
59 };
60
61 static irqreturn_t rtc_update_handler(int irq, void *data)
62 {
63         struct pm80x_rtc_info *info = (struct pm80x_rtc_info *)data;
64         int mask;
65
66         mask = PM800_ALARM | PM800_ALARM_WAKEUP;
67         regmap_update_bits(info->map, PM800_RTC_CONTROL, mask | PM800_ALARM1_EN,
68                            mask);
69         rtc_update_irq(info->rtc_dev, 1, RTC_AF);
70         return IRQ_HANDLED;
71 }
72
73 static int pm80x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
74 {
75         struct pm80x_rtc_info *info = dev_get_drvdata(dev);
76
77         if (enabled)
78                 regmap_update_bits(info->map, PM800_RTC_CONTROL,
79                                    PM800_ALARM1_EN, PM800_ALARM1_EN);
80         else
81                 regmap_update_bits(info->map, PM800_RTC_CONTROL,
82                                    PM800_ALARM1_EN, 0);
83         return 0;
84 }
85
86 /*
87  * Calculate the next alarm time given the requested alarm time mask
88  * and the current time.
89  */
90 static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
91                                 struct rtc_time *alrm)
92 {
93         unsigned long next_time;
94         unsigned long now_time;
95
96         next->tm_year = now->tm_year;
97         next->tm_mon = now->tm_mon;
98         next->tm_mday = now->tm_mday;
99         next->tm_hour = alrm->tm_hour;
100         next->tm_min = alrm->tm_min;
101         next->tm_sec = alrm->tm_sec;
102
103         rtc_tm_to_time(now, &now_time);
104         rtc_tm_to_time(next, &next_time);
105
106         if (next_time < now_time) {
107                 /* Advance one day */
108                 next_time += 60 * 60 * 24;
109                 rtc_time_to_tm(next_time, next);
110         }
111 }
112
113 static int pm80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
114 {
115         struct pm80x_rtc_info *info = dev_get_drvdata(dev);
116         unsigned char buf[4];
117         unsigned long ticks, base, data;
118         regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
119         base = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
120                 (buf[1] << 8) | buf[0];
121         dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
122
123         /* load 32-bit read-only counter */
124         regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
125         data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
126                 (buf[1] << 8) | buf[0];
127         ticks = base + data;
128         dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
129                 base, data, ticks);
130         rtc_time_to_tm(ticks, tm);
131         return 0;
132 }
133
134 static int pm80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
135 {
136         struct pm80x_rtc_info *info = dev_get_drvdata(dev);
137         unsigned char buf[4];
138         unsigned long ticks, base, data;
139         if ((tm->tm_year < 70) || (tm->tm_year > 138)) {
140                 dev_dbg(info->dev,
141                         "Set time %d out of range. Please set time between 1970 to 2038.\n",
142                         1900 + tm->tm_year);
143                 return -EINVAL;
144         }
145         rtc_tm_to_time(tm, &ticks);
146
147         /* load 32-bit read-only counter */
148         regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
149         data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
150                 (buf[1] << 8) | buf[0];
151         base = ticks - data;
152         dev_dbg(info->dev, "set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
153                 base, data, ticks);
154         buf[0] = base & 0xFF;
155         buf[1] = (base >> 8) & 0xFF;
156         buf[2] = (base >> 16) & 0xFF;
157         buf[3] = (base >> 24) & 0xFF;
158         regmap_raw_write(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
159
160         return 0;
161 }
162
163 static int pm80x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
164 {
165         struct pm80x_rtc_info *info = dev_get_drvdata(dev);
166         unsigned char buf[4];
167         unsigned long ticks, base, data;
168         int ret;
169
170         regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
171         base = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
172                 (buf[1] << 8) | buf[0];
173         dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
174
175         regmap_raw_read(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
176         data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
177                 (buf[1] << 8) | buf[0];
178         ticks = base + data;
179         dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
180                 base, data, ticks);
181
182         rtc_time_to_tm(ticks, &alrm->time);
183         regmap_read(info->map, PM800_RTC_CONTROL, &ret);
184         alrm->enabled = (ret & PM800_ALARM1_EN) ? 1 : 0;
185         alrm->pending = (ret & (PM800_ALARM | PM800_ALARM_WAKEUP)) ? 1 : 0;
186         return 0;
187 }
188
189 static int pm80x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
190 {
191         struct pm80x_rtc_info *info = dev_get_drvdata(dev);
192         struct rtc_time now_tm, alarm_tm;
193         unsigned long ticks, base, data;
194         unsigned char buf[4];
195         int mask;
196
197         regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_ALARM1_EN, 0);
198
199         regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
200         base = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
201                 (buf[1] << 8) | buf[0];
202         dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
203
204         /* load 32-bit read-only counter */
205         regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
206         data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
207                 (buf[1] << 8) | buf[0];
208         ticks = base + data;
209         dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
210                 base, data, ticks);
211
212         rtc_time_to_tm(ticks, &now_tm);
213         dev_dbg(info->dev, "%s, now time : %lu\n", __func__, ticks);
214         rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->time);
215         /* get new ticks for alarm in 24 hours */
216         rtc_tm_to_time(&alarm_tm, &ticks);
217         dev_dbg(info->dev, "%s, alarm time: %lu\n", __func__, ticks);
218         data = ticks - base;
219
220         buf[0] = data & 0xff;
221         buf[1] = (data >> 8) & 0xff;
222         buf[2] = (data >> 16) & 0xff;
223         buf[3] = (data >> 24) & 0xff;
224         regmap_raw_write(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
225         if (alrm->enabled) {
226                 mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
227                 regmap_update_bits(info->map, PM800_RTC_CONTROL, mask, mask);
228         } else {
229                 mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
230                 regmap_update_bits(info->map, PM800_RTC_CONTROL, mask,
231                                    PM800_ALARM | PM800_ALARM_WAKEUP);
232         }
233         return 0;
234 }
235
236 static const struct rtc_class_ops pm80x_rtc_ops = {
237         .read_time = pm80x_rtc_read_time,
238         .set_time = pm80x_rtc_set_time,
239         .read_alarm = pm80x_rtc_read_alarm,
240         .set_alarm = pm80x_rtc_set_alarm,
241         .alarm_irq_enable = pm80x_rtc_alarm_irq_enable,
242 };
243
244 #ifdef CONFIG_PM_SLEEP
245 static int pm80x_rtc_suspend(struct device *dev)
246 {
247         return pm80x_dev_suspend(dev);
248 }
249
250 static int pm80x_rtc_resume(struct device *dev)
251 {
252         return pm80x_dev_resume(dev);
253 }
254 #endif
255
256 static SIMPLE_DEV_PM_OPS(pm80x_rtc_pm_ops, pm80x_rtc_suspend, pm80x_rtc_resume);
257
258 static int pm80x_rtc_probe(struct platform_device *pdev)
259 {
260         struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
261         struct pm80x_rtc_pdata *pdata = dev_get_platdata(&pdev->dev);
262         struct pm80x_rtc_info *info;
263         struct device_node *node = pdev->dev.of_node;
264         struct rtc_time tm;
265         unsigned long ticks = 0;
266         int ret;
267
268         if (!pdata && !node) {
269                 dev_err(&pdev->dev,
270                         "pm80x-rtc requires platform data or of_node\n");
271                 return -EINVAL;
272         }
273
274         if (!pdata) {
275                 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
276                 if (!pdata) {
277                         dev_err(&pdev->dev, "failed to allocate memory\n");
278                         return -ENOMEM;
279                 }
280         }
281
282         info =
283             devm_kzalloc(&pdev->dev, sizeof(struct pm80x_rtc_info), GFP_KERNEL);
284         if (!info)
285                 return -ENOMEM;
286         info->irq = platform_get_irq(pdev, 0);
287         if (info->irq < 0) {
288                 dev_err(&pdev->dev, "No IRQ resource!\n");
289                 ret = -EINVAL;
290                 goto out;
291         }
292
293         info->chip = chip;
294         info->map = chip->regmap;
295         if (!info->map) {
296                 dev_err(&pdev->dev, "no regmap!\n");
297                 ret = -EINVAL;
298                 goto out;
299         }
300
301         info->dev = &pdev->dev;
302         dev_set_drvdata(&pdev->dev, info);
303
304         ret = pm80x_request_irq(chip, info->irq, rtc_update_handler,
305                                 IRQF_ONESHOT, "rtc", info);
306         if (ret < 0) {
307                 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
308                         info->irq, ret);
309                 goto out;
310         }
311
312         ret = pm80x_rtc_read_time(&pdev->dev, &tm);
313         if (ret < 0) {
314                 dev_err(&pdev->dev, "Failed to read initial time.\n");
315                 goto out_rtc;
316         }
317         if ((tm.tm_year < 70) || (tm.tm_year > 138)) {
318                 tm.tm_year = 70;
319                 tm.tm_mon = 0;
320                 tm.tm_mday = 1;
321                 tm.tm_hour = 0;
322                 tm.tm_min = 0;
323                 tm.tm_sec = 0;
324                 ret = pm80x_rtc_set_time(&pdev->dev, &tm);
325                 if (ret < 0) {
326                         dev_err(&pdev->dev, "Failed to set initial time.\n");
327                         goto out_rtc;
328                 }
329         }
330         rtc_tm_to_time(&tm, &ticks);
331
332         info->rtc_dev = devm_rtc_device_register(&pdev->dev, "88pm80x-rtc",
333                                             &pm80x_rtc_ops, THIS_MODULE);
334         if (IS_ERR(info->rtc_dev)) {
335                 ret = PTR_ERR(info->rtc_dev);
336                 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
337                 goto out_rtc;
338         }
339         /*
340          * enable internal XO instead of internal 3.25MHz clock since it can
341          * free running in PMIC power-down state.
342          */
343         regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_RTC1_USE_XO,
344                            PM800_RTC1_USE_XO);
345
346         /* remember whether this power up is caused by PMIC RTC or not */
347         info->rtc_dev->dev.platform_data = &pdata->rtc_wakeup;
348
349         device_init_wakeup(&pdev->dev, 1);
350
351         return 0;
352 out_rtc:
353         pm80x_free_irq(chip, info->irq, info);
354 out:
355         return ret;
356 }
357
358 static int pm80x_rtc_remove(struct platform_device *pdev)
359 {
360         struct pm80x_rtc_info *info = platform_get_drvdata(pdev);
361         pm80x_free_irq(info->chip, info->irq, info);
362         return 0;
363 }
364
365 static struct platform_driver pm80x_rtc_driver = {
366         .driver = {
367                    .name = "88pm80x-rtc",
368                    .pm = &pm80x_rtc_pm_ops,
369                    },
370         .probe = pm80x_rtc_probe,
371         .remove = pm80x_rtc_remove,
372 };
373
374 module_platform_driver(pm80x_rtc_driver);
375
376 MODULE_LICENSE("GPL");
377 MODULE_DESCRIPTION("Marvell 88PM80x RTC driver");
378 MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
379 MODULE_ALIAS("platform:88pm80x-rtc");