blob: 694038208745a3f8ca0a1548b6fa28c89bfe284f [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/*
32 * struct device dev.platform_data is used to store our private data
33 * because struct rtc_device does not have a variable to hold it.
34 */
35struct ep93xx_rtc {
36 void __iomem *mmio_base;
Axel Linbf6ed022011-08-10 21:11:26 +080037 struct rtc_device *rtc;
Hartley Sweeten38f7b002009-04-15 23:18:26 +010038};
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{
Jingoo Han28dc5f82013-11-12 15:10:43 -080043 struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
Hartley Sweeten38f7b002009-04-15 23:18:26 +010044 unsigned long comp;
45
H Hartley Sweetenff32ff12012-03-21 10:55:31 -070046 comp = 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{
Jingoo Han28dc5f82013-11-12 15:10:43 -080061 struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
Hartley Sweeten38f7b002009-04-15 23:18:26 +010062 unsigned long time;
63
H Hartley Sweetenff32ff12012-03-21 10:55:31 -070064 time = 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{
Jingoo Han28dc5f82013-11-12 15:10:43 -080072 struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
Hartley Sweeten38f7b002009-04-15 23:18:26 +010073
H Hartley Sweetenff32ff12012-03-21 10:55:31 -070074 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
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800118static struct attribute *ep93xx_rtc_attrs[] = {
119 &dev_attr_comp_preload.attr,
120 &dev_attr_comp_delete.attr,
121 NULL
122};
123
124static const struct attribute_group ep93xx_rtc_sysfs_files = {
125 .attrs = ep93xx_rtc_attrs,
126};
Alessandro Zummofd507e22006-03-27 01:16:45 -0800127
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800128static int ep93xx_rtc_probe(struct platform_device *pdev)
Alessandro Zummofd507e22006-03-27 01:16:45 -0800129{
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100130 struct ep93xx_rtc *ep93xx_rtc;
131 struct resource *res;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100132 int err;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800133
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800134 ep93xx_rtc = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_rtc), GFP_KERNEL);
135 if (!ep93xx_rtc)
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100136 return -ENOMEM;
137
138 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700139 ep93xx_rtc->mmio_base = devm_ioremap_resource(&pdev->dev, res);
140 if (IS_ERR(ep93xx_rtc->mmio_base))
141 return PTR_ERR(ep93xx_rtc->mmio_base);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800142
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100143 pdev->dev.platform_data = ep93xx_rtc;
Axel Linbf6ed022011-08-10 21:11:26 +0800144 platform_set_drvdata(pdev, ep93xx_rtc);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800145
Jingoo Han5d65cbf2013-04-29 16:19:02 -0700146 ep93xx_rtc->rtc = devm_rtc_device_register(&pdev->dev,
147 pdev->name, &ep93xx_rtc_ops, THIS_MODULE);
Axel Linbf6ed022011-08-10 21:11:26 +0800148 if (IS_ERR(ep93xx_rtc->rtc)) {
149 err = PTR_ERR(ep93xx_rtc->rtc);
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800150 goto exit;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100151 }
152
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800153 err = sysfs_create_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100154 if (err)
Jingoo Han5d65cbf2013-04-29 16:19:02 -0700155 goto exit;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800156
157 return 0;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100158
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800159exit:
160 pdev->dev.platform_data = NULL;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100161 return err;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800162}
163
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800164static int ep93xx_rtc_remove(struct platform_device *pdev)
Alessandro Zummofd507e22006-03-27 01:16:45 -0800165{
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800166 sysfs_remove_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800167 pdev->dev.platform_data = NULL;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800168
169 return 0;
170}
171
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100172static struct platform_driver ep93xx_rtc_driver = {
Alessandro Zummofd507e22006-03-27 01:16:45 -0800173 .driver = {
174 .name = "ep93xx-rtc",
Alessandro Zummofd507e22006-03-27 01:16:45 -0800175 },
H Hartley Sweeten84d56b32012-05-29 15:07:36 -0700176 .probe = ep93xx_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800177 .remove = ep93xx_rtc_remove,
Alessandro Zummofd507e22006-03-27 01:16:45 -0800178};
179
H Hartley Sweeten84d56b32012-05-29 15:07:36 -0700180module_platform_driver(ep93xx_rtc_driver);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800181
182MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
183MODULE_DESCRIPTION("EP93XX RTC driver");
184MODULE_LICENSE("GPL");
H Hartley Sweeten84d56b32012-05-29 15:07:36 -0700185MODULE_ALIAS("platform:ep93xx-rtc");