]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/rtc/rtc-ep93xx.c
32d7b3ec816d2cd2975cbf50c22fade891f0b5ad
[linux.git] / drivers / rtc / rtc-ep93xx.c
1 /*
2  * A driver for the RTC embedded in the Cirrus Logic EP93XX processors
3  * Copyright (c) 2006 Tower Technologies
4  *
5  * Author: Alessandro Zummo <a.zummo@towertech.it>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/module.h>
13 #include <linux/rtc.h>
14 #include <linux/platform_device.h>
15 #include <linux/io.h>
16 #include <linux/gfp.h>
17
18 #define EP93XX_RTC_DATA                 0x000
19 #define EP93XX_RTC_MATCH                0x004
20 #define EP93XX_RTC_STATUS               0x008
21 #define  EP93XX_RTC_STATUS_INTR          (1<<0)
22 #define EP93XX_RTC_LOAD                 0x00C
23 #define EP93XX_RTC_CONTROL              0x010
24 #define  EP93XX_RTC_CONTROL_MIE          (1<<0)
25 #define EP93XX_RTC_SWCOMP               0x108
26 #define  EP93XX_RTC_SWCOMP_DEL_MASK      0x001f0000
27 #define  EP93XX_RTC_SWCOMP_DEL_SHIFT     16
28 #define  EP93XX_RTC_SWCOMP_INT_MASK      0x0000ffff
29 #define  EP93XX_RTC_SWCOMP_INT_SHIFT     0
30
31 struct ep93xx_rtc {
32         void __iomem    *mmio_base;
33         struct rtc_device *rtc;
34 };
35
36 static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload,
37                                 unsigned short *delete)
38 {
39         struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
40         unsigned long comp;
41
42         comp = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP);
43
44         if (preload)
45                 *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK)
46                                 >> EP93XX_RTC_SWCOMP_INT_SHIFT;
47
48         if (delete)
49                 *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK)
50                                 >> EP93XX_RTC_SWCOMP_DEL_SHIFT;
51
52         return 0;
53 }
54
55 static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm)
56 {
57         struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
58         unsigned long time;
59
60         time = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA);
61
62         rtc_time_to_tm(time, tm);
63         return 0;
64 }
65
66 static int ep93xx_rtc_set_mmss(struct device *dev, unsigned long secs)
67 {
68         struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
69
70         writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD);
71         return 0;
72 }
73
74 static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq)
75 {
76         unsigned short preload, delete;
77
78         ep93xx_rtc_get_swcomp(dev, &preload, &delete);
79
80         seq_printf(seq, "preload\t\t: %d\n", preload);
81         seq_printf(seq, "delete\t\t: %d\n", delete);
82
83         return 0;
84 }
85
86 static const struct rtc_class_ops ep93xx_rtc_ops = {
87         .read_time      = ep93xx_rtc_read_time,
88         .set_mmss       = ep93xx_rtc_set_mmss,
89         .proc           = ep93xx_rtc_proc,
90 };
91
92 static ssize_t ep93xx_rtc_show_comp_preload(struct device *dev,
93                         struct device_attribute *attr, char *buf)
94 {
95         unsigned short preload;
96
97         ep93xx_rtc_get_swcomp(dev->parent, &preload, NULL);
98
99         return sprintf(buf, "%d\n", preload);
100 }
101 static DEVICE_ATTR(comp_preload, S_IRUGO, ep93xx_rtc_show_comp_preload, NULL);
102
103 static ssize_t ep93xx_rtc_show_comp_delete(struct device *dev,
104                         struct device_attribute *attr, char *buf)
105 {
106         unsigned short delete;
107
108         ep93xx_rtc_get_swcomp(dev->parent, NULL, &delete);
109
110         return sprintf(buf, "%d\n", delete);
111 }
112 static DEVICE_ATTR(comp_delete, S_IRUGO, ep93xx_rtc_show_comp_delete, NULL);
113
114 static struct attribute *ep93xx_rtc_attrs[] = {
115         &dev_attr_comp_preload.attr,
116         &dev_attr_comp_delete.attr,
117         NULL
118 };
119
120 static const struct attribute_group ep93xx_rtc_sysfs_files = {
121         .attrs  = ep93xx_rtc_attrs,
122 };
123
124 static int ep93xx_rtc_probe(struct platform_device *pdev)
125 {
126         struct ep93xx_rtc *ep93xx_rtc;
127         struct resource *res;
128         int err;
129
130         ep93xx_rtc = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_rtc), GFP_KERNEL);
131         if (!ep93xx_rtc)
132                 return -ENOMEM;
133
134         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
135         ep93xx_rtc->mmio_base = devm_ioremap_resource(&pdev->dev, res);
136         if (IS_ERR(ep93xx_rtc->mmio_base))
137                 return PTR_ERR(ep93xx_rtc->mmio_base);
138
139         platform_set_drvdata(pdev, ep93xx_rtc);
140
141         ep93xx_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
142         if (IS_ERR(ep93xx_rtc->rtc))
143                 return PTR_ERR(ep93xx_rtc->rtc);
144
145         ep93xx_rtc->rtc->ops = &ep93xx_rtc_ops;
146         ep93xx_rtc->rtc->range_max = U32_MAX;
147
148         err = rtc_add_group(ep93xx_rtc->rtc, &ep93xx_rtc_sysfs_files);
149         if (err)
150                 return err;
151
152         return rtc_register_device(ep93xx_rtc->rtc);
153 }
154
155 static struct platform_driver ep93xx_rtc_driver = {
156         .driver         = {
157                 .name   = "ep93xx-rtc",
158         },
159         .probe          = ep93xx_rtc_probe,
160 };
161
162 module_platform_driver(ep93xx_rtc_driver);
163
164 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
165 MODULE_DESCRIPTION("EP93XX RTC driver");
166 MODULE_LICENSE("GPL");
167 MODULE_ALIAS("platform:ep93xx-rtc");