blob: 9da02d108b73370d79f45dea06f016143b93ac90 [file] [log] [blame]
Alessandro Zummofd507e22006-03-27 01:16:45 -08001/*
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>
Hartley Sweeten38f7b002009-04-15 23:18:26 +010015#include <linux/io.h>
Alessandro Zummofd507e22006-03-27 01:16:45 -080016
Hartley Sweeten38f7b002009-04-15 23:18:26 +010017#define EP93XX_RTC_DATA 0x000
18#define EP93XX_RTC_MATCH 0x004
19#define EP93XX_RTC_STATUS 0x008
20#define EP93XX_RTC_STATUS_INTR (1<<0)
21#define EP93XX_RTC_LOAD 0x00C
22#define EP93XX_RTC_CONTROL 0x010
23#define EP93XX_RTC_CONTROL_MIE (1<<0)
24#define EP93XX_RTC_SWCOMP 0x108
25#define EP93XX_RTC_SWCOMP_DEL_MASK 0x001f0000
26#define EP93XX_RTC_SWCOMP_DEL_SHIFT 16
27#define EP93XX_RTC_SWCOMP_INT_MASK 0x0000ffff
28#define EP93XX_RTC_SWCOMP_INT_SHIFT 0
Alessandro Zummofd507e22006-03-27 01:16:45 -080029
Hartley Sweeten38f7b002009-04-15 23:18:26 +010030#define DRV_VERSION "0.3"
Alessandro Zummofd507e22006-03-27 01:16:45 -080031
Hartley Sweeten38f7b002009-04-15 23:18:26 +010032/*
33 * struct device dev.platform_data is used to store our private data
34 * because struct rtc_device does not have a variable to hold it.
35 */
36struct ep93xx_rtc {
37 void __iomem *mmio_base;
38};
39
40static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload,
Alessandro Zummofd507e22006-03-27 01:16:45 -080041 unsigned short *delete)
42{
Hartley Sweeten38f7b002009-04-15 23:18:26 +010043 struct ep93xx_rtc *ep93xx_rtc = dev->platform_data;
44 unsigned long comp;
45
46 comp = __raw_readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP);
Alessandro Zummofd507e22006-03-27 01:16:45 -080047
48 if (preload)
Hartley Sweeten38f7b002009-04-15 23:18:26 +010049 *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK)
50 >> EP93XX_RTC_SWCOMP_INT_SHIFT;
Alessandro Zummofd507e22006-03-27 01:16:45 -080051
52 if (delete)
Hartley Sweeten38f7b002009-04-15 23:18:26 +010053 *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK)
54 >> EP93XX_RTC_SWCOMP_DEL_SHIFT;
Alessandro Zummofd507e22006-03-27 01:16:45 -080055
56 return 0;
57}
58
59static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm)
60{
Hartley Sweeten38f7b002009-04-15 23:18:26 +010061 struct ep93xx_rtc *ep93xx_rtc = dev->platform_data;
62 unsigned long time;
63
64 time = __raw_readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA);
Alessandro Zummofd507e22006-03-27 01:16:45 -080065
66 rtc_time_to_tm(time, tm);
67 return 0;
68}
69
70static int ep93xx_rtc_set_mmss(struct device *dev, unsigned long secs)
71{
Hartley Sweeten38f7b002009-04-15 23:18:26 +010072 struct ep93xx_rtc *ep93xx_rtc = dev->platform_data;
73
74 __raw_writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD);
Alessandro Zummofd507e22006-03-27 01:16:45 -080075 return 0;
76}
77
Alessandro Zummofd507e22006-03-27 01:16:45 -080078static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq)
79{
80 unsigned short preload, delete;
81
Hartley Sweeten38f7b002009-04-15 23:18:26 +010082 ep93xx_rtc_get_swcomp(dev, &preload, &delete);
Alessandro Zummofd507e22006-03-27 01:16:45 -080083
Alessandro Zummofd507e22006-03-27 01:16:45 -080084 seq_printf(seq, "preload\t\t: %d\n", preload);
85 seq_printf(seq, "delete\t\t: %d\n", delete);
86
87 return 0;
88}
89
David Brownellff8371a2006-09-30 23:28:17 -070090static const struct rtc_class_ops ep93xx_rtc_ops = {
Alessandro Zummofd507e22006-03-27 01:16:45 -080091 .read_time = ep93xx_rtc_read_time,
Alessandro Zummofd507e22006-03-27 01:16:45 -080092 .set_mmss = ep93xx_rtc_set_mmss,
93 .proc = ep93xx_rtc_proc,
94};
95
Hartley Sweeten38f7b002009-04-15 23:18:26 +010096static ssize_t ep93xx_rtc_show_comp_preload(struct device *dev,
Alessandro Zummofd507e22006-03-27 01:16:45 -080097 struct device_attribute *attr, char *buf)
98{
99 unsigned short preload;
100
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100101 ep93xx_rtc_get_swcomp(dev, &preload, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800102
103 return sprintf(buf, "%d\n", preload);
104}
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100105static DEVICE_ATTR(comp_preload, S_IRUGO, ep93xx_rtc_show_comp_preload, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800106
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100107static ssize_t ep93xx_rtc_show_comp_delete(struct device *dev,
Alessandro Zummofd507e22006-03-27 01:16:45 -0800108 struct device_attribute *attr, char *buf)
109{
110 unsigned short delete;
111
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100112 ep93xx_rtc_get_swcomp(dev, NULL, &delete);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800113
114 return sprintf(buf, "%d\n", delete);
115}
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100116static DEVICE_ATTR(comp_delete, S_IRUGO, ep93xx_rtc_show_comp_delete, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800117
118
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100119static int __init ep93xx_rtc_probe(struct platform_device *pdev)
Alessandro Zummofd507e22006-03-27 01:16:45 -0800120{
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100121 struct ep93xx_rtc *ep93xx_rtc;
122 struct resource *res;
123 struct rtc_device *rtc;
124 int err;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800125
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100126 ep93xx_rtc = kzalloc(sizeof(struct ep93xx_rtc), GFP_KERNEL);
127 if (ep93xx_rtc == NULL)
128 return -ENOMEM;
129
130 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall971370c2009-09-22 16:46:30 -0700131 if (res == NULL) {
132 err = -ENXIO;
133 goto fail_free;
134 }
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100135
136 res = request_mem_region(res->start, resource_size(res), pdev->name);
Julia Lawall971370c2009-09-22 16:46:30 -0700137 if (res == NULL) {
138 err = -EBUSY;
139 goto fail_free;
140 }
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100141
142 ep93xx_rtc->mmio_base = ioremap(res->start, resource_size(res));
143 if (ep93xx_rtc->mmio_base == NULL) {
144 err = -ENXIO;
145 goto fail;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800146 }
147
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100148 pdev->dev.platform_data = ep93xx_rtc;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800149
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100150 rtc = rtc_device_register(pdev->name,
151 &pdev->dev, &ep93xx_rtc_ops, THIS_MODULE);
152 if (IS_ERR(rtc)) {
153 err = PTR_ERR(rtc);
154 goto fail;
155 }
156
157 platform_set_drvdata(pdev, rtc);
158
159 err = device_create_file(&pdev->dev, &dev_attr_comp_preload);
160 if (err)
161 goto fail;
162 err = device_create_file(&pdev->dev, &dev_attr_comp_delete);
163 if (err) {
164 device_remove_file(&pdev->dev, &dev_attr_comp_preload);
165 goto fail;
166 }
Alessandro Zummofd507e22006-03-27 01:16:45 -0800167
168 return 0;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100169
170fail:
171 if (ep93xx_rtc->mmio_base) {
172 iounmap(ep93xx_rtc->mmio_base);
173 pdev->dev.platform_data = NULL;
174 }
175 release_mem_region(res->start, resource_size(res));
Julia Lawall971370c2009-09-22 16:46:30 -0700176fail_free:
177 kfree(ep93xx_rtc);
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100178 return err;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800179}
180
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100181static int __exit ep93xx_rtc_remove(struct platform_device *pdev)
Alessandro Zummofd507e22006-03-27 01:16:45 -0800182{
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100183 struct rtc_device *rtc = platform_get_drvdata(pdev);
184 struct ep93xx_rtc *ep93xx_rtc = pdev->dev.platform_data;
185 struct resource *res;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800186
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100187 /* cleanup sysfs */
188 device_remove_file(&pdev->dev, &dev_attr_comp_delete);
189 device_remove_file(&pdev->dev, &dev_attr_comp_preload);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800190
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100191 rtc_device_unregister(rtc);
192
193 iounmap(ep93xx_rtc->mmio_base);
194 pdev->dev.platform_data = NULL;
195
196 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
197 release_mem_region(res->start, resource_size(res));
198
199 platform_set_drvdata(pdev, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800200
201 return 0;
202}
203
Kay Sieversad28a072008-04-10 21:29:25 -0700204/* work with hotplug and coldplug */
205MODULE_ALIAS("platform:ep93xx-rtc");
206
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100207static struct platform_driver ep93xx_rtc_driver = {
Alessandro Zummofd507e22006-03-27 01:16:45 -0800208 .driver = {
209 .name = "ep93xx-rtc",
210 .owner = THIS_MODULE,
211 },
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100212 .remove = __exit_p(ep93xx_rtc_remove),
Alessandro Zummofd507e22006-03-27 01:16:45 -0800213};
214
215static int __init ep93xx_rtc_init(void)
216{
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100217 return platform_driver_probe(&ep93xx_rtc_driver, ep93xx_rtc_probe);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800218}
219
220static void __exit ep93xx_rtc_exit(void)
221{
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100222 platform_driver_unregister(&ep93xx_rtc_driver);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800223}
224
225MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
226MODULE_DESCRIPTION("EP93XX RTC driver");
227MODULE_LICENSE("GPL");
228MODULE_VERSION(DRV_VERSION);
229
230module_init(ep93xx_rtc_init);
231module_exit(ep93xx_rtc_exit);