Linux 3.3-rc6
[linux/fpc-iii.git] / drivers / rtc / rtc-ep93xx.c
blob14a42a1edc66d55be04a79c67aa622db7b10b38d
1 /*
2 * A driver for the RTC embedded in the Cirrus Logic EP93XX processors
3 * Copyright (c) 2006 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
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.
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>
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
31 #define DRV_VERSION "0.3"
34 * struct device dev.platform_data is used to store our private data
35 * because struct rtc_device does not have a variable to hold it.
37 struct ep93xx_rtc {
38 void __iomem *mmio_base;
39 struct rtc_device *rtc;
42 static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload,
43 unsigned short *delete)
45 struct ep93xx_rtc *ep93xx_rtc = dev->platform_data;
46 unsigned long comp;
48 comp = __raw_readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP);
50 if (preload)
51 *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK)
52 >> EP93XX_RTC_SWCOMP_INT_SHIFT;
54 if (delete)
55 *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK)
56 >> EP93XX_RTC_SWCOMP_DEL_SHIFT;
58 return 0;
61 static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm)
63 struct ep93xx_rtc *ep93xx_rtc = dev->platform_data;
64 unsigned long time;
66 time = __raw_readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA);
68 rtc_time_to_tm(time, tm);
69 return 0;
72 static int ep93xx_rtc_set_mmss(struct device *dev, unsigned long secs)
74 struct ep93xx_rtc *ep93xx_rtc = dev->platform_data;
76 __raw_writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD);
77 return 0;
80 static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq)
82 unsigned short preload, delete;
84 ep93xx_rtc_get_swcomp(dev, &preload, &delete);
86 seq_printf(seq, "preload\t\t: %d\n", preload);
87 seq_printf(seq, "delete\t\t: %d\n", delete);
89 return 0;
92 static const struct rtc_class_ops ep93xx_rtc_ops = {
93 .read_time = ep93xx_rtc_read_time,
94 .set_mmss = ep93xx_rtc_set_mmss,
95 .proc = ep93xx_rtc_proc,
98 static ssize_t ep93xx_rtc_show_comp_preload(struct device *dev,
99 struct device_attribute *attr, char *buf)
101 unsigned short preload;
103 ep93xx_rtc_get_swcomp(dev, &preload, NULL);
105 return sprintf(buf, "%d\n", preload);
107 static DEVICE_ATTR(comp_preload, S_IRUGO, ep93xx_rtc_show_comp_preload, NULL);
109 static ssize_t ep93xx_rtc_show_comp_delete(struct device *dev,
110 struct device_attribute *attr, char *buf)
112 unsigned short delete;
114 ep93xx_rtc_get_swcomp(dev, NULL, &delete);
116 return sprintf(buf, "%d\n", delete);
118 static DEVICE_ATTR(comp_delete, S_IRUGO, ep93xx_rtc_show_comp_delete, NULL);
120 static struct attribute *ep93xx_rtc_attrs[] = {
121 &dev_attr_comp_preload.attr,
122 &dev_attr_comp_delete.attr,
123 NULL
126 static const struct attribute_group ep93xx_rtc_sysfs_files = {
127 .attrs = ep93xx_rtc_attrs,
130 static int __init ep93xx_rtc_probe(struct platform_device *pdev)
132 struct ep93xx_rtc *ep93xx_rtc;
133 struct resource *res;
134 int err;
136 ep93xx_rtc = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_rtc), GFP_KERNEL);
137 if (!ep93xx_rtc)
138 return -ENOMEM;
140 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
141 if (!res)
142 return -ENXIO;
144 if (!devm_request_mem_region(&pdev->dev, res->start,
145 resource_size(res), pdev->name))
146 return -EBUSY;
148 ep93xx_rtc->mmio_base = devm_ioremap(&pdev->dev, res->start,
149 resource_size(res));
150 if (!ep93xx_rtc->mmio_base)
151 return -ENXIO;
153 pdev->dev.platform_data = ep93xx_rtc;
154 platform_set_drvdata(pdev, ep93xx_rtc);
156 ep93xx_rtc->rtc = rtc_device_register(pdev->name,
157 &pdev->dev, &ep93xx_rtc_ops, THIS_MODULE);
158 if (IS_ERR(ep93xx_rtc->rtc)) {
159 err = PTR_ERR(ep93xx_rtc->rtc);
160 goto exit;
163 err = sysfs_create_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
164 if (err)
165 goto fail;
167 return 0;
169 fail:
170 rtc_device_unregister(ep93xx_rtc->rtc);
171 exit:
172 platform_set_drvdata(pdev, NULL);
173 pdev->dev.platform_data = NULL;
174 return err;
177 static int __exit ep93xx_rtc_remove(struct platform_device *pdev)
179 struct ep93xx_rtc *ep93xx_rtc = platform_get_drvdata(pdev);
181 sysfs_remove_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
182 platform_set_drvdata(pdev, NULL);
183 rtc_device_unregister(ep93xx_rtc->rtc);
184 pdev->dev.platform_data = NULL;
186 return 0;
189 /* work with hotplug and coldplug */
190 MODULE_ALIAS("platform:ep93xx-rtc");
192 static struct platform_driver ep93xx_rtc_driver = {
193 .driver = {
194 .name = "ep93xx-rtc",
195 .owner = THIS_MODULE,
197 .remove = __exit_p(ep93xx_rtc_remove),
200 static int __init ep93xx_rtc_init(void)
202 return platform_driver_probe(&ep93xx_rtc_driver, ep93xx_rtc_probe);
205 static void __exit ep93xx_rtc_exit(void)
207 platform_driver_unregister(&ep93xx_rtc_driver);
210 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
211 MODULE_DESCRIPTION("EP93XX RTC driver");
212 MODULE_LICENSE("GPL");
213 MODULE_VERSION(DRV_VERSION);
215 module_init(ep93xx_rtc_init);
216 module_exit(ep93xx_rtc_exit);