blob: de325d68c7e4a61acc0b2c22ac34b1096ea02fb2 [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;
Axel Linbf6ed022011-08-10 21:11:26 +080039 struct rtc_device *rtc;
Hartley Sweeten38f7b002009-04-15 23:18:26 +010040};
41
42static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload,
Alessandro Zummofd507e22006-03-27 01:16:45 -080043 unsigned short *delete)
44{
Jingoo Han28dc5f82013-11-12 15:10:43 -080045 struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
Hartley Sweeten38f7b002009-04-15 23:18:26 +010046 unsigned long comp;
47
48 comp = __raw_readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP);
Alessandro Zummofd507e22006-03-27 01:16:45 -080049
50 if (preload)
Hartley Sweeten38f7b002009-04-15 23:18:26 +010051 *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK)
52 >> EP93XX_RTC_SWCOMP_INT_SHIFT;
Alessandro Zummofd507e22006-03-27 01:16:45 -080053
54 if (delete)
Hartley Sweeten38f7b002009-04-15 23:18:26 +010055 *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK)
56 >> EP93XX_RTC_SWCOMP_DEL_SHIFT;
Alessandro Zummofd507e22006-03-27 01:16:45 -080057
58 return 0;
59}
60
61static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm)
62{
Jingoo Han28dc5f82013-11-12 15:10:43 -080063 struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
Hartley Sweeten38f7b002009-04-15 23:18:26 +010064 unsigned long time;
65
66 time = __raw_readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA);
Alessandro Zummofd507e22006-03-27 01:16:45 -080067
68 rtc_time_to_tm(time, tm);
69 return 0;
70}
71
72static int ep93xx_rtc_set_mmss(struct device *dev, unsigned long secs)
73{
Jingoo Han28dc5f82013-11-12 15:10:43 -080074 struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
Hartley Sweeten38f7b002009-04-15 23:18:26 +010075
76 __raw_writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD);
Alessandro Zummofd507e22006-03-27 01:16:45 -080077 return 0;
78}
79
Alessandro Zummofd507e22006-03-27 01:16:45 -080080static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq)
81{
82 unsigned short preload, delete;
83
Hartley Sweeten38f7b002009-04-15 23:18:26 +010084 ep93xx_rtc_get_swcomp(dev, &preload, &delete);
Alessandro Zummofd507e22006-03-27 01:16:45 -080085
Alessandro Zummofd507e22006-03-27 01:16:45 -080086 seq_printf(seq, "preload\t\t: %d\n", preload);
87 seq_printf(seq, "delete\t\t: %d\n", delete);
88
89 return 0;
90}
91
David Brownellff8371a2006-09-30 23:28:17 -070092static const struct rtc_class_ops ep93xx_rtc_ops = {
Alessandro Zummofd507e22006-03-27 01:16:45 -080093 .read_time = ep93xx_rtc_read_time,
Alessandro Zummofd507e22006-03-27 01:16:45 -080094 .set_mmss = ep93xx_rtc_set_mmss,
95 .proc = ep93xx_rtc_proc,
96};
97
Hartley Sweeten38f7b002009-04-15 23:18:26 +010098static ssize_t ep93xx_rtc_show_comp_preload(struct device *dev,
Alessandro Zummofd507e22006-03-27 01:16:45 -080099 struct device_attribute *attr, char *buf)
100{
101 unsigned short preload;
102
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100103 ep93xx_rtc_get_swcomp(dev, &preload, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800104
105 return sprintf(buf, "%d\n", preload);
106}
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100107static DEVICE_ATTR(comp_preload, S_IRUGO, ep93xx_rtc_show_comp_preload, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800108
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100109static ssize_t ep93xx_rtc_show_comp_delete(struct device *dev,
Alessandro Zummofd507e22006-03-27 01:16:45 -0800110 struct device_attribute *attr, char *buf)
111{
112 unsigned short delete;
113
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100114 ep93xx_rtc_get_swcomp(dev, NULL, &delete);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800115
116 return sprintf(buf, "%d\n", delete);
117}
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100118static DEVICE_ATTR(comp_delete, S_IRUGO, ep93xx_rtc_show_comp_delete, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800119
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800120static struct attribute *ep93xx_rtc_attrs[] = {
121 &dev_attr_comp_preload.attr,
122 &dev_attr_comp_delete.attr,
123 NULL
124};
125
126static const struct attribute_group ep93xx_rtc_sysfs_files = {
127 .attrs = ep93xx_rtc_attrs,
128};
Alessandro Zummofd507e22006-03-27 01:16:45 -0800129
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800130static int ep93xx_rtc_probe(struct platform_device *pdev)
Alessandro Zummofd507e22006-03-27 01:16:45 -0800131{
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100132 struct ep93xx_rtc *ep93xx_rtc;
133 struct resource *res;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100134 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);
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700141 ep93xx_rtc->mmio_base = devm_ioremap_resource(&pdev->dev, res);
142 if (IS_ERR(ep93xx_rtc->mmio_base))
143 return PTR_ERR(ep93xx_rtc->mmio_base);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800144
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100145 pdev->dev.platform_data = ep93xx_rtc;
Axel Linbf6ed022011-08-10 21:11:26 +0800146 platform_set_drvdata(pdev, ep93xx_rtc);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800147
Jingoo Han5d65cbf2013-04-29 16:19:02 -0700148 ep93xx_rtc->rtc = devm_rtc_device_register(&pdev->dev,
149 pdev->name, &ep93xx_rtc_ops, THIS_MODULE);
Axel Linbf6ed022011-08-10 21:11:26 +0800150 if (IS_ERR(ep93xx_rtc->rtc)) {
151 err = PTR_ERR(ep93xx_rtc->rtc);
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800152 goto exit;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100153 }
154
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800155 err = sysfs_create_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100156 if (err)
Jingoo Han5d65cbf2013-04-29 16:19:02 -0700157 goto exit;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800158
159 return 0;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100160
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800161exit:
162 pdev->dev.platform_data = NULL;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100163 return err;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800164}
165
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800166static int ep93xx_rtc_remove(struct platform_device *pdev)
Alessandro Zummofd507e22006-03-27 01:16:45 -0800167{
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800168 sysfs_remove_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800169 pdev->dev.platform_data = NULL;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800170
171 return 0;
172}
173
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100174static struct platform_driver ep93xx_rtc_driver = {
Alessandro Zummofd507e22006-03-27 01:16:45 -0800175 .driver = {
176 .name = "ep93xx-rtc",
Alessandro Zummofd507e22006-03-27 01:16:45 -0800177 },
H Hartley Sweeten84d56b32012-05-29 15:07:36 -0700178 .probe = ep93xx_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800179 .remove = ep93xx_rtc_remove,
Alessandro Zummofd507e22006-03-27 01:16:45 -0800180};
181
H Hartley Sweeten84d56b32012-05-29 15:07:36 -0700182module_platform_driver(ep93xx_rtc_driver);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800183
184MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
185MODULE_DESCRIPTION("EP93XX RTC driver");
186MODULE_LICENSE("GPL");
187MODULE_VERSION(DRV_VERSION);
H Hartley Sweeten84d56b32012-05-29 15:07:36 -0700188MODULE_ALIAS("platform:ep93xx-rtc");