]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/rtc/rtc-mxc.c
rtc: simplify getting .drvdata
[linux.git] / drivers / rtc / rtc-mxc.c
1 /*
2  * Copyright 2004-2008 Freescale Semiconductor, Inc. All Rights Reserved.
3  *
4  * The code contained herein is licensed under the GNU General Public
5  * License. You may obtain a copy of the GNU General Public License
6  * Version 2 or later at the following locations:
7  *
8  * http://www.opensource.org/licenses/gpl-license.html
9  * http://www.gnu.org/copyleft/gpl.html
10  */
11
12 #include <linux/io.h>
13 #include <linux/rtc.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/platform_device.h>
18 #include <linux/clk.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21
22 #define RTC_INPUT_CLK_32768HZ   (0x00 << 5)
23 #define RTC_INPUT_CLK_32000HZ   (0x01 << 5)
24 #define RTC_INPUT_CLK_38400HZ   (0x02 << 5)
25
26 #define RTC_SW_BIT      (1 << 0)
27 #define RTC_ALM_BIT     (1 << 2)
28 #define RTC_1HZ_BIT     (1 << 4)
29 #define RTC_2HZ_BIT     (1 << 7)
30 #define RTC_SAM0_BIT    (1 << 8)
31 #define RTC_SAM1_BIT    (1 << 9)
32 #define RTC_SAM2_BIT    (1 << 10)
33 #define RTC_SAM3_BIT    (1 << 11)
34 #define RTC_SAM4_BIT    (1 << 12)
35 #define RTC_SAM5_BIT    (1 << 13)
36 #define RTC_SAM6_BIT    (1 << 14)
37 #define RTC_SAM7_BIT    (1 << 15)
38 #define PIT_ALL_ON      (RTC_2HZ_BIT | RTC_SAM0_BIT | RTC_SAM1_BIT | \
39                          RTC_SAM2_BIT | RTC_SAM3_BIT | RTC_SAM4_BIT | \
40                          RTC_SAM5_BIT | RTC_SAM6_BIT | RTC_SAM7_BIT)
41
42 #define RTC_ENABLE_BIT  (1 << 7)
43
44 #define MAX_PIE_NUM     9
45 #define MAX_PIE_FREQ    512
46
47 #define MXC_RTC_TIME    0
48 #define MXC_RTC_ALARM   1
49
50 #define RTC_HOURMIN     0x00    /*  32bit rtc hour/min counter reg */
51 #define RTC_SECOND      0x04    /*  32bit rtc seconds counter reg */
52 #define RTC_ALRM_HM     0x08    /*  32bit rtc alarm hour/min reg */
53 #define RTC_ALRM_SEC    0x0C    /*  32bit rtc alarm seconds reg */
54 #define RTC_RTCCTL      0x10    /*  32bit rtc control reg */
55 #define RTC_RTCISR      0x14    /*  32bit rtc interrupt status reg */
56 #define RTC_RTCIENR     0x18    /*  32bit rtc interrupt enable reg */
57 #define RTC_STPWCH      0x1C    /*  32bit rtc stopwatch min reg */
58 #define RTC_DAYR        0x20    /*  32bit rtc days counter reg */
59 #define RTC_DAYALARM    0x24    /*  32bit rtc day alarm reg */
60 #define RTC_TEST1       0x28    /*  32bit rtc test reg 1 */
61 #define RTC_TEST2       0x2C    /*  32bit rtc test reg 2 */
62 #define RTC_TEST3       0x30    /*  32bit rtc test reg 3 */
63
64 enum imx_rtc_type {
65         IMX1_RTC,
66         IMX21_RTC,
67 };
68
69 struct rtc_plat_data {
70         struct rtc_device *rtc;
71         void __iomem *ioaddr;
72         int irq;
73         struct clk *clk_ref;
74         struct clk *clk_ipg;
75         struct rtc_time g_rtc_alarm;
76         enum imx_rtc_type devtype;
77 };
78
79 static const struct platform_device_id imx_rtc_devtype[] = {
80         {
81                 .name = "imx1-rtc",
82                 .driver_data = IMX1_RTC,
83         }, {
84                 .name = "imx21-rtc",
85                 .driver_data = IMX21_RTC,
86         }, {
87                 /* sentinel */
88         }
89 };
90 MODULE_DEVICE_TABLE(platform, imx_rtc_devtype);
91
92 #ifdef CONFIG_OF
93 static const struct of_device_id imx_rtc_dt_ids[] = {
94         { .compatible = "fsl,imx1-rtc", .data = (const void *)IMX1_RTC },
95         { .compatible = "fsl,imx21-rtc", .data = (const void *)IMX21_RTC },
96         {}
97 };
98 MODULE_DEVICE_TABLE(of, imx_rtc_dt_ids);
99 #endif
100
101 static inline int is_imx1_rtc(struct rtc_plat_data *data)
102 {
103         return data->devtype == IMX1_RTC;
104 }
105
106 /*
107  * This function is used to obtain the RTC time or the alarm value in
108  * second.
109  */
110 static time64_t get_alarm_or_time(struct device *dev, int time_alarm)
111 {
112         struct rtc_plat_data *pdata = dev_get_drvdata(dev);
113         void __iomem *ioaddr = pdata->ioaddr;
114         u32 day = 0, hr = 0, min = 0, sec = 0, hr_min = 0;
115
116         switch (time_alarm) {
117         case MXC_RTC_TIME:
118                 day = readw(ioaddr + RTC_DAYR);
119                 hr_min = readw(ioaddr + RTC_HOURMIN);
120                 sec = readw(ioaddr + RTC_SECOND);
121                 break;
122         case MXC_RTC_ALARM:
123                 day = readw(ioaddr + RTC_DAYALARM);
124                 hr_min = readw(ioaddr + RTC_ALRM_HM) & 0xffff;
125                 sec = readw(ioaddr + RTC_ALRM_SEC);
126                 break;
127         }
128
129         hr = hr_min >> 8;
130         min = hr_min & 0xff;
131
132         return ((((time64_t)day * 24 + hr) * 60) + min) * 60 + sec;
133 }
134
135 /*
136  * This function sets the RTC alarm value or the time value.
137  */
138 static void set_alarm_or_time(struct device *dev, int time_alarm, time64_t time)
139 {
140         u32 tod, day, hr, min, sec, temp;
141         struct rtc_plat_data *pdata = dev_get_drvdata(dev);
142         void __iomem *ioaddr = pdata->ioaddr;
143
144         day = div_s64_rem(time, 86400, &tod);
145
146         /* time is within a day now */
147         hr = tod / 3600;
148         tod -= hr * 3600;
149
150         /* time is within an hour now */
151         min = tod / 60;
152         sec = tod - min * 60;
153
154         temp = (hr << 8) + min;
155
156         switch (time_alarm) {
157         case MXC_RTC_TIME:
158                 writew(day, ioaddr + RTC_DAYR);
159                 writew(sec, ioaddr + RTC_SECOND);
160                 writew(temp, ioaddr + RTC_HOURMIN);
161                 break;
162         case MXC_RTC_ALARM:
163                 writew(day, ioaddr + RTC_DAYALARM);
164                 writew(sec, ioaddr + RTC_ALRM_SEC);
165                 writew(temp, ioaddr + RTC_ALRM_HM);
166                 break;
167         }
168 }
169
170 /*
171  * This function updates the RTC alarm registers and then clears all the
172  * interrupt status bits.
173  */
174 static void rtc_update_alarm(struct device *dev, struct rtc_time *alrm)
175 {
176         time64_t time;
177         struct rtc_plat_data *pdata = dev_get_drvdata(dev);
178         void __iomem *ioaddr = pdata->ioaddr;
179
180         time = rtc_tm_to_time64(alrm);
181
182         /* clear all the interrupt status bits */
183         writew(readw(ioaddr + RTC_RTCISR), ioaddr + RTC_RTCISR);
184         set_alarm_or_time(dev, MXC_RTC_ALARM, time);
185 }
186
187 static void mxc_rtc_irq_enable(struct device *dev, unsigned int bit,
188                                 unsigned int enabled)
189 {
190         struct rtc_plat_data *pdata = dev_get_drvdata(dev);
191         void __iomem *ioaddr = pdata->ioaddr;
192         u32 reg;
193
194         spin_lock_irq(&pdata->rtc->irq_lock);
195         reg = readw(ioaddr + RTC_RTCIENR);
196
197         if (enabled)
198                 reg |= bit;
199         else
200                 reg &= ~bit;
201
202         writew(reg, ioaddr + RTC_RTCIENR);
203         spin_unlock_irq(&pdata->rtc->irq_lock);
204 }
205
206 /* This function is the RTC interrupt service routine. */
207 static irqreturn_t mxc_rtc_interrupt(int irq, void *dev_id)
208 {
209         struct platform_device *pdev = dev_id;
210         struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
211         void __iomem *ioaddr = pdata->ioaddr;
212         unsigned long flags;
213         u32 status;
214         u32 events = 0;
215
216         spin_lock_irqsave(&pdata->rtc->irq_lock, flags);
217         status = readw(ioaddr + RTC_RTCISR) & readw(ioaddr + RTC_RTCIENR);
218         /* clear interrupt sources */
219         writew(status, ioaddr + RTC_RTCISR);
220
221         /* update irq data & counter */
222         if (status & RTC_ALM_BIT) {
223                 events |= (RTC_AF | RTC_IRQF);
224                 /* RTC alarm should be one-shot */
225                 mxc_rtc_irq_enable(&pdev->dev, RTC_ALM_BIT, 0);
226         }
227
228         if (status & PIT_ALL_ON)
229                 events |= (RTC_PF | RTC_IRQF);
230
231         rtc_update_irq(pdata->rtc, 1, events);
232         spin_unlock_irqrestore(&pdata->rtc->irq_lock, flags);
233
234         return IRQ_HANDLED;
235 }
236
237 static int mxc_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
238 {
239         mxc_rtc_irq_enable(dev, RTC_ALM_BIT, enabled);
240         return 0;
241 }
242
243 /*
244  * This function reads the current RTC time into tm in Gregorian date.
245  */
246 static int mxc_rtc_read_time(struct device *dev, struct rtc_time *tm)
247 {
248         time64_t val;
249
250         /* Avoid roll-over from reading the different registers */
251         do {
252                 val = get_alarm_or_time(dev, MXC_RTC_TIME);
253         } while (val != get_alarm_or_time(dev, MXC_RTC_TIME));
254
255         rtc_time64_to_tm(val, tm);
256
257         return 0;
258 }
259
260 /*
261  * This function sets the internal RTC time based on tm in Gregorian date.
262  */
263 static int mxc_rtc_set_mmss(struct device *dev, time64_t time)
264 {
265         struct rtc_plat_data *pdata = dev_get_drvdata(dev);
266
267         /*
268          * TTC_DAYR register is 9-bit in MX1 SoC, save time and day of year only
269          */
270         if (is_imx1_rtc(pdata)) {
271                 struct rtc_time tm;
272
273                 rtc_time64_to_tm(time, &tm);
274                 tm.tm_year = 70;
275                 time = rtc_tm_to_time64(&tm);
276         }
277
278         /* Avoid roll-over from reading the different registers */
279         do {
280                 set_alarm_or_time(dev, MXC_RTC_TIME, time);
281         } while (time != get_alarm_or_time(dev, MXC_RTC_TIME));
282
283         return 0;
284 }
285
286 /*
287  * This function reads the current alarm value into the passed in 'alrm'
288  * argument. It updates the alrm's pending field value based on the whether
289  * an alarm interrupt occurs or not.
290  */
291 static int mxc_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
292 {
293         struct rtc_plat_data *pdata = dev_get_drvdata(dev);
294         void __iomem *ioaddr = pdata->ioaddr;
295
296         rtc_time64_to_tm(get_alarm_or_time(dev, MXC_RTC_ALARM), &alrm->time);
297         alrm->pending = ((readw(ioaddr + RTC_RTCISR) & RTC_ALM_BIT)) ? 1 : 0;
298
299         return 0;
300 }
301
302 /*
303  * This function sets the RTC alarm based on passed in alrm.
304  */
305 static int mxc_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
306 {
307         struct rtc_plat_data *pdata = dev_get_drvdata(dev);
308
309         rtc_update_alarm(dev, &alrm->time);
310
311         memcpy(&pdata->g_rtc_alarm, &alrm->time, sizeof(struct rtc_time));
312         mxc_rtc_irq_enable(dev, RTC_ALM_BIT, alrm->enabled);
313
314         return 0;
315 }
316
317 /* RTC layer */
318 static const struct rtc_class_ops mxc_rtc_ops = {
319         .read_time              = mxc_rtc_read_time,
320         .set_mmss64             = mxc_rtc_set_mmss,
321         .read_alarm             = mxc_rtc_read_alarm,
322         .set_alarm              = mxc_rtc_set_alarm,
323         .alarm_irq_enable       = mxc_rtc_alarm_irq_enable,
324 };
325
326 static int mxc_rtc_probe(struct platform_device *pdev)
327 {
328         struct resource *res;
329         struct rtc_device *rtc;
330         struct rtc_plat_data *pdata = NULL;
331         u32 reg;
332         unsigned long rate;
333         int ret;
334         const struct of_device_id *of_id;
335
336         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
337         if (!pdata)
338                 return -ENOMEM;
339
340         of_id = of_match_device(imx_rtc_dt_ids, &pdev->dev);
341         if (of_id)
342                 pdata->devtype = (enum imx_rtc_type)of_id->data;
343         else
344                 pdata->devtype = pdev->id_entry->driver_data;
345
346         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
347         pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res);
348         if (IS_ERR(pdata->ioaddr))
349                 return PTR_ERR(pdata->ioaddr);
350
351         pdata->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
352         if (IS_ERR(pdata->clk_ipg)) {
353                 dev_err(&pdev->dev, "unable to get ipg clock!\n");
354                 return PTR_ERR(pdata->clk_ipg);
355         }
356
357         ret = clk_prepare_enable(pdata->clk_ipg);
358         if (ret)
359                 return ret;
360
361         pdata->clk_ref = devm_clk_get(&pdev->dev, "ref");
362         if (IS_ERR(pdata->clk_ref)) {
363                 dev_err(&pdev->dev, "unable to get ref clock!\n");
364                 ret = PTR_ERR(pdata->clk_ref);
365                 goto exit_put_clk_ipg;
366         }
367
368         ret = clk_prepare_enable(pdata->clk_ref);
369         if (ret)
370                 goto exit_put_clk_ipg;
371
372         rate = clk_get_rate(pdata->clk_ref);
373
374         if (rate == 32768)
375                 reg = RTC_INPUT_CLK_32768HZ;
376         else if (rate == 32000)
377                 reg = RTC_INPUT_CLK_32000HZ;
378         else if (rate == 38400)
379                 reg = RTC_INPUT_CLK_38400HZ;
380         else {
381                 dev_err(&pdev->dev, "rtc clock is not valid (%lu)\n", rate);
382                 ret = -EINVAL;
383                 goto exit_put_clk_ref;
384         }
385
386         reg |= RTC_ENABLE_BIT;
387         writew(reg, (pdata->ioaddr + RTC_RTCCTL));
388         if (((readw(pdata->ioaddr + RTC_RTCCTL)) & RTC_ENABLE_BIT) == 0) {
389                 dev_err(&pdev->dev, "hardware module can't be enabled!\n");
390                 ret = -EIO;
391                 goto exit_put_clk_ref;
392         }
393
394         platform_set_drvdata(pdev, pdata);
395
396         /* Configure and enable the RTC */
397         pdata->irq = platform_get_irq(pdev, 0);
398
399         if (pdata->irq >= 0 &&
400             devm_request_irq(&pdev->dev, pdata->irq, mxc_rtc_interrupt,
401                              IRQF_SHARED, pdev->name, pdev) < 0) {
402                 dev_warn(&pdev->dev, "interrupt not available.\n");
403                 pdata->irq = -1;
404         }
405
406         if (pdata->irq >= 0)
407                 device_init_wakeup(&pdev->dev, 1);
408
409         rtc = devm_rtc_device_register(&pdev->dev, pdev->name, &mxc_rtc_ops,
410                                   THIS_MODULE);
411         if (IS_ERR(rtc)) {
412                 ret = PTR_ERR(rtc);
413                 goto exit_put_clk_ref;
414         }
415
416         pdata->rtc = rtc;
417
418         return 0;
419
420 exit_put_clk_ref:
421         clk_disable_unprepare(pdata->clk_ref);
422 exit_put_clk_ipg:
423         clk_disable_unprepare(pdata->clk_ipg);
424
425         return ret;
426 }
427
428 static int mxc_rtc_remove(struct platform_device *pdev)
429 {
430         struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
431
432         clk_disable_unprepare(pdata->clk_ref);
433         clk_disable_unprepare(pdata->clk_ipg);
434
435         return 0;
436 }
437
438 #ifdef CONFIG_PM_SLEEP
439 static int mxc_rtc_suspend(struct device *dev)
440 {
441         struct rtc_plat_data *pdata = dev_get_drvdata(dev);
442
443         if (device_may_wakeup(dev))
444                 enable_irq_wake(pdata->irq);
445
446         return 0;
447 }
448
449 static int mxc_rtc_resume(struct device *dev)
450 {
451         struct rtc_plat_data *pdata = dev_get_drvdata(dev);
452
453         if (device_may_wakeup(dev))
454                 disable_irq_wake(pdata->irq);
455
456         return 0;
457 }
458 #endif
459
460 static SIMPLE_DEV_PM_OPS(mxc_rtc_pm_ops, mxc_rtc_suspend, mxc_rtc_resume);
461
462 static struct platform_driver mxc_rtc_driver = {
463         .driver = {
464                    .name        = "mxc_rtc",
465                    .of_match_table = of_match_ptr(imx_rtc_dt_ids),
466                    .pm          = &mxc_rtc_pm_ops,
467         },
468         .id_table = imx_rtc_devtype,
469         .probe = mxc_rtc_probe,
470         .remove = mxc_rtc_remove,
471 };
472
473 module_platform_driver(mxc_rtc_driver)
474
475 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
476 MODULE_DESCRIPTION("RTC driver for Freescale MXC");
477 MODULE_LICENSE("GPL");
478