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 <asm/hardware.h>
16
17#define EP93XX_RTC_REG(x)	(EP93XX_RTC_BASE + (x))
18#define EP93XX_RTC_DATA		EP93XX_RTC_REG(0x0000)
19#define EP93XX_RTC_LOAD		EP93XX_RTC_REG(0x000C)
20#define EP93XX_RTC_SWCOMP	EP93XX_RTC_REG(0x0108)
21
22#define DRV_VERSION "0.2"
23
24static int ep93xx_get_swcomp(struct device *dev, unsigned short *preload,
25				unsigned short *delete)
26{
27	unsigned short comp = __raw_readl(EP93XX_RTC_SWCOMP);
28
29	if (preload)
30		*preload = comp & 0xffff;
31
32	if (delete)
33		*delete = (comp >> 16) & 0x1f;
34
35	return 0;
36}
37
38static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm)
39{
40	unsigned long time = __raw_readl(EP93XX_RTC_DATA);
41
42	rtc_time_to_tm(time, tm);
43	return 0;
44}
45
46static int ep93xx_rtc_set_mmss(struct device *dev, unsigned long secs)
47{
48	__raw_writel(secs + 1, EP93XX_RTC_LOAD);
49	return 0;
50}
51
52static int ep93xx_rtc_set_time(struct device *dev, struct rtc_time *tm)
53{
54	int err;
55	unsigned long secs;
56
57	err = rtc_tm_to_time(tm, &secs);
58	if (err != 0)
59		return err;
60
61	return ep93xx_rtc_set_mmss(dev, secs);
62}
63
64static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq)
65{
66	unsigned short preload, delete;
67
68	ep93xx_get_swcomp(dev, &preload, &delete);
69
70	seq_printf(seq, "preload\t\t: %d\n", preload);
71	seq_printf(seq, "delete\t\t: %d\n", delete);
72
73	return 0;
74}
75
76static const struct rtc_class_ops ep93xx_rtc_ops = {
77	.read_time	= ep93xx_rtc_read_time,
78	.set_time	= ep93xx_rtc_set_time,
79	.set_mmss	= ep93xx_rtc_set_mmss,
80	.proc		= ep93xx_rtc_proc,
81};
82
83static ssize_t ep93xx_sysfs_show_comp_preload(struct device *dev,
84			struct device_attribute *attr, char *buf)
85{
86	unsigned short preload;
87
88	ep93xx_get_swcomp(dev, &preload, NULL);
89
90	return sprintf(buf, "%d\n", preload);
91}
92static DEVICE_ATTR(comp_preload, S_IRUGO, ep93xx_sysfs_show_comp_preload, NULL);
93
94static ssize_t ep93xx_sysfs_show_comp_delete(struct device *dev,
95			struct device_attribute *attr, char *buf)
96{
97	unsigned short delete;
98
99	ep93xx_get_swcomp(dev, NULL, &delete);
100
101	return sprintf(buf, "%d\n", delete);
102}
103static DEVICE_ATTR(comp_delete, S_IRUGO, ep93xx_sysfs_show_comp_delete, NULL);
104
105
106static int __devinit ep93xx_rtc_probe(struct platform_device *dev)
107{
108	struct rtc_device *rtc = rtc_device_register("ep93xx",
109				&dev->dev, &ep93xx_rtc_ops, THIS_MODULE);
110
111	if (IS_ERR(rtc)) {
112		return PTR_ERR(rtc);
113	}
114
115	platform_set_drvdata(dev, rtc);
116
117	device_create_file(&dev->dev, &dev_attr_comp_preload);
118	device_create_file(&dev->dev, &dev_attr_comp_delete);
119
120	return 0;
121}
122
123static int __devexit ep93xx_rtc_remove(struct platform_device *dev)
124{
125	struct rtc_device *rtc = platform_get_drvdata(dev);
126
127 	if (rtc)
128		rtc_device_unregister(rtc);
129
130	platform_set_drvdata(dev, NULL);
131
132	return 0;
133}
134
135static struct platform_driver ep93xx_rtc_platform_driver = {
136	.driver		= {
137		.name	= "ep93xx-rtc",
138		.owner	= THIS_MODULE,
139	},
140	.probe		= ep93xx_rtc_probe,
141	.remove		= __devexit_p(ep93xx_rtc_remove),
142};
143
144static int __init ep93xx_rtc_init(void)
145{
146	return platform_driver_register(&ep93xx_rtc_platform_driver);
147}
148
149static void __exit ep93xx_rtc_exit(void)
150{
151	platform_driver_unregister(&ep93xx_rtc_platform_driver);
152}
153
154MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
155MODULE_DESCRIPTION("EP93XX RTC driver");
156MODULE_LICENSE("GPL");
157MODULE_VERSION(DRV_VERSION);
158
159module_init(ep93xx_rtc_init);
160module_exit(ep93xx_rtc_exit);
161