blob: 11ae64dcbf3c7b1a365853ae16cb095cbb38ea9f [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Alessandro Zummofd507e22006-03-27 01:16:45 -080017
Hartley Sweeten38f7b002009-04-15 23:18:26 +010018#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
Alessandro Zummofd507e22006-03-27 01:16:45 -080030
Hartley Sweeten38f7b002009-04-15 23:18:26 +010031#define DRV_VERSION "0.3"
Alessandro Zummofd507e22006-03-27 01:16:45 -080032
Hartley Sweeten38f7b002009-04-15 23:18:26 +010033/*
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.
36 */
37struct ep93xx_rtc {
38 void __iomem *mmio_base;
39};
40
41static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload,
Alessandro Zummofd507e22006-03-27 01:16:45 -080042 unsigned short *delete)
43{
Hartley Sweeten38f7b002009-04-15 23:18:26 +010044 struct ep93xx_rtc *ep93xx_rtc = dev->platform_data;
45 unsigned long comp;
46
47 comp = __raw_readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP);
Alessandro Zummofd507e22006-03-27 01:16:45 -080048
49 if (preload)
Hartley Sweeten38f7b002009-04-15 23:18:26 +010050 *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK)
51 >> EP93XX_RTC_SWCOMP_INT_SHIFT;
Alessandro Zummofd507e22006-03-27 01:16:45 -080052
53 if (delete)
Hartley Sweeten38f7b002009-04-15 23:18:26 +010054 *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK)
55 >> EP93XX_RTC_SWCOMP_DEL_SHIFT;
Alessandro Zummofd507e22006-03-27 01:16:45 -080056
57 return 0;
58}
59
60static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm)
61{
Hartley Sweeten38f7b002009-04-15 23:18:26 +010062 struct ep93xx_rtc *ep93xx_rtc = dev->platform_data;
63 unsigned long time;
64
65 time = __raw_readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA);
Alessandro Zummofd507e22006-03-27 01:16:45 -080066
67 rtc_time_to_tm(time, tm);
68 return 0;
69}
70
71static int ep93xx_rtc_set_mmss(struct device *dev, unsigned long secs)
72{
Hartley Sweeten38f7b002009-04-15 23:18:26 +010073 struct ep93xx_rtc *ep93xx_rtc = dev->platform_data;
74
75 __raw_writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD);
Alessandro Zummofd507e22006-03-27 01:16:45 -080076 return 0;
77}
78
Alessandro Zummofd507e22006-03-27 01:16:45 -080079static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq)
80{
81 unsigned short preload, delete;
82
Hartley Sweeten38f7b002009-04-15 23:18:26 +010083 ep93xx_rtc_get_swcomp(dev, &preload, &delete);
Alessandro Zummofd507e22006-03-27 01:16:45 -080084
Alessandro Zummofd507e22006-03-27 01:16:45 -080085 seq_printf(seq, "preload\t\t: %d\n", preload);
86 seq_printf(seq, "delete\t\t: %d\n", delete);
87
88 return 0;
89}
90
David Brownellff8371a2006-09-30 23:28:17 -070091static const struct rtc_class_ops ep93xx_rtc_ops = {
Alessandro Zummofd507e22006-03-27 01:16:45 -080092 .read_time = ep93xx_rtc_read_time,
Alessandro Zummofd507e22006-03-27 01:16:45 -080093 .set_mmss = ep93xx_rtc_set_mmss,
94 .proc = ep93xx_rtc_proc,
95};
96
Hartley Sweeten38f7b002009-04-15 23:18:26 +010097static ssize_t ep93xx_rtc_show_comp_preload(struct device *dev,
Alessandro Zummofd507e22006-03-27 01:16:45 -080098 struct device_attribute *attr, char *buf)
99{
100 unsigned short preload;
101
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100102 ep93xx_rtc_get_swcomp(dev, &preload, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800103
104 return sprintf(buf, "%d\n", preload);
105}
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100106static DEVICE_ATTR(comp_preload, S_IRUGO, ep93xx_rtc_show_comp_preload, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800107
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100108static ssize_t ep93xx_rtc_show_comp_delete(struct device *dev,
Alessandro Zummofd507e22006-03-27 01:16:45 -0800109 struct device_attribute *attr, char *buf)
110{
111 unsigned short delete;
112
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100113 ep93xx_rtc_get_swcomp(dev, NULL, &delete);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800114
115 return sprintf(buf, "%d\n", delete);
116}
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100117static DEVICE_ATTR(comp_delete, S_IRUGO, ep93xx_rtc_show_comp_delete, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800118
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800119static struct attribute *ep93xx_rtc_attrs[] = {
120 &dev_attr_comp_preload.attr,
121 &dev_attr_comp_delete.attr,
122 NULL
123};
124
125static const struct attribute_group ep93xx_rtc_sysfs_files = {
126 .attrs = ep93xx_rtc_attrs,
127};
Alessandro Zummofd507e22006-03-27 01:16:45 -0800128
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100129static int __init ep93xx_rtc_probe(struct platform_device *pdev)
Alessandro Zummofd507e22006-03-27 01:16:45 -0800130{
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100131 struct ep93xx_rtc *ep93xx_rtc;
132 struct resource *res;
133 struct rtc_device *rtc;
134 int err;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800135
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800136 ep93xx_rtc = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_rtc), GFP_KERNEL);
137 if (!ep93xx_rtc)
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100138 return -ENOMEM;
139
140 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800141 if (!res)
142 return -ENXIO;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100143
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800144 if (!devm_request_mem_region(&pdev->dev, res->start,
145 resource_size(res), pdev->name))
146 return -EBUSY;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100147
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800148 ep93xx_rtc->mmio_base = devm_ioremap(&pdev->dev, res->start,
149 resource_size(res));
150 if (!ep93xx_rtc->mmio_base)
151 return -ENXIO;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800152
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100153 pdev->dev.platform_data = ep93xx_rtc;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800154
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100155 rtc = rtc_device_register(pdev->name,
156 &pdev->dev, &ep93xx_rtc_ops, THIS_MODULE);
157 if (IS_ERR(rtc)) {
158 err = PTR_ERR(rtc);
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800159 goto exit;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100160 }
161
162 platform_set_drvdata(pdev, rtc);
163
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800164 err = sysfs_create_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100165 if (err)
166 goto fail;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800167
168 return 0;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100169
170fail:
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800171 platform_set_drvdata(pdev, NULL);
172 rtc_device_unregister(rtc);
173exit:
174 pdev->dev.platform_data = NULL;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100175 return err;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800176}
177
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100178static int __exit ep93xx_rtc_remove(struct platform_device *pdev)
Alessandro Zummofd507e22006-03-27 01:16:45 -0800179{
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100180 struct rtc_device *rtc = platform_get_drvdata(pdev);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800181
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800182 sysfs_remove_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100183 platform_set_drvdata(pdev, NULL);
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800184 rtc_device_unregister(rtc);
185 pdev->dev.platform_data = NULL;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800186
187 return 0;
188}
189
Kay Sieversad28a072008-04-10 21:29:25 -0700190/* work with hotplug and coldplug */
191MODULE_ALIAS("platform:ep93xx-rtc");
192
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100193static struct platform_driver ep93xx_rtc_driver = {
Alessandro Zummofd507e22006-03-27 01:16:45 -0800194 .driver = {
195 .name = "ep93xx-rtc",
196 .owner = THIS_MODULE,
197 },
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100198 .remove = __exit_p(ep93xx_rtc_remove),
Alessandro Zummofd507e22006-03-27 01:16:45 -0800199};
200
201static int __init ep93xx_rtc_init(void)
202{
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100203 return platform_driver_probe(&ep93xx_rtc_driver, ep93xx_rtc_probe);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800204}
205
206static void __exit ep93xx_rtc_exit(void)
207{
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100208 platform_driver_unregister(&ep93xx_rtc_driver);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800209}
210
211MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
212MODULE_DESCRIPTION("EP93XX RTC driver");
213MODULE_LICENSE("GPL");
214MODULE_VERSION(DRV_VERSION);
215
216module_init(ep93xx_rtc_init);
217module_exit(ep93xx_rtc_exit);