blob: f15391bc45972700558f545de3ce9fdca395b746 [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 +010031struct ep93xx_rtc {
32 void __iomem *mmio_base;
Axel Linbf6ed022011-08-10 21:11:26 +080033 struct rtc_device *rtc;
Hartley Sweeten38f7b002009-04-15 23:18:26 +010034};
35
36static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload,
Alessandro Zummofd507e22006-03-27 01:16:45 -080037 unsigned short *delete)
38{
Jingoo Han28dc5f82013-11-12 15:10:43 -080039 struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
Hartley Sweeten38f7b002009-04-15 23:18:26 +010040 unsigned long comp;
41
H Hartley Sweetenff32ff12012-03-21 10:55:31 -070042 comp = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP);
Alessandro Zummofd507e22006-03-27 01:16:45 -080043
44 if (preload)
Hartley Sweeten38f7b002009-04-15 23:18:26 +010045 *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK)
46 >> EP93XX_RTC_SWCOMP_INT_SHIFT;
Alessandro Zummofd507e22006-03-27 01:16:45 -080047
48 if (delete)
Hartley Sweeten38f7b002009-04-15 23:18:26 +010049 *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK)
50 >> EP93XX_RTC_SWCOMP_DEL_SHIFT;
Alessandro Zummofd507e22006-03-27 01:16:45 -080051
52 return 0;
53}
54
55static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm)
56{
Jingoo Han28dc5f82013-11-12 15:10:43 -080057 struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
Hartley Sweeten38f7b002009-04-15 23:18:26 +010058 unsigned long time;
59
Colin Ian King725412d92018-11-01 14:53:52 +000060 time = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA);
Alessandro Zummofd507e22006-03-27 01:16:45 -080061
62 rtc_time_to_tm(time, tm);
63 return 0;
64}
65
66static int ep93xx_rtc_set_mmss(struct device *dev, unsigned long secs)
67{
Jingoo Han28dc5f82013-11-12 15:10:43 -080068 struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
Hartley Sweeten38f7b002009-04-15 23:18:26 +010069
H Hartley Sweetenff32ff12012-03-21 10:55:31 -070070 writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD);
Alessandro Zummofd507e22006-03-27 01:16:45 -080071 return 0;
72}
73
Alessandro Zummofd507e22006-03-27 01:16:45 -080074static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq)
75{
76 unsigned short preload, delete;
77
Hartley Sweeten38f7b002009-04-15 23:18:26 +010078 ep93xx_rtc_get_swcomp(dev, &preload, &delete);
Alessandro Zummofd507e22006-03-27 01:16:45 -080079
Alessandro Zummofd507e22006-03-27 01:16:45 -080080 seq_printf(seq, "preload\t\t: %d\n", preload);
81 seq_printf(seq, "delete\t\t: %d\n", delete);
82
83 return 0;
84}
85
David Brownellff8371a2006-09-30 23:28:17 -070086static const struct rtc_class_ops ep93xx_rtc_ops = {
Alessandro Zummofd507e22006-03-27 01:16:45 -080087 .read_time = ep93xx_rtc_read_time,
Alessandro Zummofd507e22006-03-27 01:16:45 -080088 .set_mmss = ep93xx_rtc_set_mmss,
89 .proc = ep93xx_rtc_proc,
90};
91
Hartley Sweeten38f7b002009-04-15 23:18:26 +010092static ssize_t ep93xx_rtc_show_comp_preload(struct device *dev,
Alessandro Zummofd507e22006-03-27 01:16:45 -080093 struct device_attribute *attr, char *buf)
94{
95 unsigned short preload;
96
Hartley Sweeten38f7b002009-04-15 23:18:26 +010097 ep93xx_rtc_get_swcomp(dev, &preload, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -080098
99 return sprintf(buf, "%d\n", preload);
100}
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100101static DEVICE_ATTR(comp_preload, S_IRUGO, ep93xx_rtc_show_comp_preload, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800102
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100103static ssize_t ep93xx_rtc_show_comp_delete(struct device *dev,
Alessandro Zummofd507e22006-03-27 01:16:45 -0800104 struct device_attribute *attr, char *buf)
105{
106 unsigned short delete;
107
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100108 ep93xx_rtc_get_swcomp(dev, NULL, &delete);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800109
110 return sprintf(buf, "%d\n", delete);
111}
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100112static DEVICE_ATTR(comp_delete, S_IRUGO, ep93xx_rtc_show_comp_delete, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800113
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800114static struct attribute *ep93xx_rtc_attrs[] = {
115 &dev_attr_comp_preload.attr,
116 &dev_attr_comp_delete.attr,
117 NULL
118};
119
120static const struct attribute_group ep93xx_rtc_sysfs_files = {
121 .attrs = ep93xx_rtc_attrs,
122};
Alessandro Zummofd507e22006-03-27 01:16:45 -0800123
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800124static int ep93xx_rtc_probe(struct platform_device *pdev)
Alessandro Zummofd507e22006-03-27 01:16:45 -0800125{
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100126 struct ep93xx_rtc *ep93xx_rtc;
127 struct resource *res;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100128 int err;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800129
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800130 ep93xx_rtc = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_rtc), GFP_KERNEL);
131 if (!ep93xx_rtc)
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100132 return -ENOMEM;
133
134 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700135 ep93xx_rtc->mmio_base = devm_ioremap_resource(&pdev->dev, res);
136 if (IS_ERR(ep93xx_rtc->mmio_base))
137 return PTR_ERR(ep93xx_rtc->mmio_base);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800138
Axel Linbf6ed022011-08-10 21:11:26 +0800139 platform_set_drvdata(pdev, ep93xx_rtc);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800140
Alexandre Bellonibac68b32019-04-19 09:59:59 +0200141 ep93xx_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
Alexandre Bellonib809d192019-04-19 09:59:58 +0200142 if (IS_ERR(ep93xx_rtc->rtc))
143 return PTR_ERR(ep93xx_rtc->rtc);
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100144
Alexandre Bellonibac68b32019-04-19 09:59:59 +0200145 ep93xx_rtc->rtc->ops = &ep93xx_rtc_ops;
146
147 err = rtc_register_device(ep93xx_rtc->rtc);
148 if (err)
149 return err;
150
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800151 err = sysfs_create_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100152 if (err)
Alexandre Bellonib809d192019-04-19 09:59:58 +0200153 return err;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800154
155 return 0;
156}
157
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800158static int ep93xx_rtc_remove(struct platform_device *pdev)
Alessandro Zummofd507e22006-03-27 01:16:45 -0800159{
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800160 sysfs_remove_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800161
162 return 0;
163}
164
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100165static struct platform_driver ep93xx_rtc_driver = {
Alessandro Zummofd507e22006-03-27 01:16:45 -0800166 .driver = {
167 .name = "ep93xx-rtc",
Alessandro Zummofd507e22006-03-27 01:16:45 -0800168 },
H Hartley Sweeten84d56b32012-05-29 15:07:36 -0700169 .probe = ep93xx_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800170 .remove = ep93xx_rtc_remove,
Alessandro Zummofd507e22006-03-27 01:16:45 -0800171};
172
H Hartley Sweeten84d56b32012-05-29 15:07:36 -0700173module_platform_driver(ep93xx_rtc_driver);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800174
175MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
176MODULE_DESCRIPTION("EP93XX RTC driver");
177MODULE_LICENSE("GPL");
H Hartley Sweeten84d56b32012-05-29 15:07:36 -0700178MODULE_ALIAS("platform:ep93xx-rtc");