blob: acae7f16808f76f0493c330d893d61d8bbb95270 [file] [log] [blame]
Alexandre Belloni4fdf4d22019-04-19 10:00:04 +02001// SPDX-License-Identifier: GPL-2.0
Alessandro Zummofd507e22006-03-27 01:16:45 -08002/*
3 * A driver for the RTC embedded in the Cirrus Logic EP93XX processors
4 * Copyright (c) 2006 Tower Technologies
5 *
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
Alessandro Zummofd507e22006-03-27 01:16:45 -08007 */
8
9#include <linux/module.h>
10#include <linux/rtc.h>
11#include <linux/platform_device.h>
Hartley Sweeten38f7b002009-04-15 23:18:26 +010012#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Alessandro Zummofd507e22006-03-27 01:16:45 -080014
Hartley Sweeten38f7b002009-04-15 23:18:26 +010015#define EP93XX_RTC_DATA 0x000
16#define EP93XX_RTC_MATCH 0x004
17#define EP93XX_RTC_STATUS 0x008
Alexandre Bellonid71c7712019-04-19 10:00:05 +020018#define EP93XX_RTC_STATUS_INTR BIT(0)
Hartley Sweeten38f7b002009-04-15 23:18:26 +010019#define EP93XX_RTC_LOAD 0x00C
20#define EP93XX_RTC_CONTROL 0x010
Alexandre Bellonid71c7712019-04-19 10:00:05 +020021#define EP93XX_RTC_CONTROL_MIE BIT(0)
Hartley Sweeten38f7b002009-04-15 23:18:26 +010022#define EP93XX_RTC_SWCOMP 0x108
23#define EP93XX_RTC_SWCOMP_DEL_MASK 0x001f0000
24#define EP93XX_RTC_SWCOMP_DEL_SHIFT 16
25#define EP93XX_RTC_SWCOMP_INT_MASK 0x0000ffff
26#define EP93XX_RTC_SWCOMP_INT_SHIFT 0
Alessandro Zummofd507e22006-03-27 01:16:45 -080027
Hartley Sweeten38f7b002009-04-15 23:18:26 +010028struct ep93xx_rtc {
29 void __iomem *mmio_base;
Axel Linbf6ed022011-08-10 21:11:26 +080030 struct rtc_device *rtc;
Hartley Sweeten38f7b002009-04-15 23:18:26 +010031};
32
33static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload,
Alexandre Bellonid71c7712019-04-19 10:00:05 +020034 unsigned short *delete)
Alessandro Zummofd507e22006-03-27 01:16:45 -080035{
Nikita Shubin00c33482020-12-01 12:55:07 +030036 struct ep93xx_rtc *ep93xx_rtc = dev_get_drvdata(dev);
Hartley Sweeten38f7b002009-04-15 23:18:26 +010037 unsigned long comp;
38
H Hartley Sweetenff32ff12012-03-21 10:55:31 -070039 comp = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP);
Alessandro Zummofd507e22006-03-27 01:16:45 -080040
41 if (preload)
Hartley Sweeten38f7b002009-04-15 23:18:26 +010042 *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK)
43 >> EP93XX_RTC_SWCOMP_INT_SHIFT;
Alessandro Zummofd507e22006-03-27 01:16:45 -080044
45 if (delete)
Hartley Sweeten38f7b002009-04-15 23:18:26 +010046 *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK)
47 >> EP93XX_RTC_SWCOMP_DEL_SHIFT;
Alessandro Zummofd507e22006-03-27 01:16:45 -080048
49 return 0;
50}
51
52static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm)
53{
Nikita Shubin00c33482020-12-01 12:55:07 +030054 struct ep93xx_rtc *ep93xx_rtc = dev_get_drvdata(dev);
Hartley Sweeten38f7b002009-04-15 23:18:26 +010055 unsigned long time;
56
Colin Ian King725412d92018-11-01 14:53:52 +000057 time = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA);
Alessandro Zummofd507e22006-03-27 01:16:45 -080058
Alexandre Belloni886a77e2019-04-19 10:00:02 +020059 rtc_time64_to_tm(time, tm);
Alessandro Zummofd507e22006-03-27 01:16:45 -080060 return 0;
61}
62
Alexandre Bellonief9440a2019-04-19 10:00:03 +020063static int ep93xx_rtc_set_time(struct device *dev, struct rtc_time *tm)
Alessandro Zummofd507e22006-03-27 01:16:45 -080064{
Nikita Shubin00c33482020-12-01 12:55:07 +030065 struct ep93xx_rtc *ep93xx_rtc = dev_get_drvdata(dev);
Alexandre Bellonief9440a2019-04-19 10:00:03 +020066 unsigned long secs = rtc_tm_to_time64(tm);
Hartley Sweeten38f7b002009-04-15 23:18:26 +010067
H Hartley Sweetenff32ff12012-03-21 10:55:31 -070068 writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD);
Alessandro Zummofd507e22006-03-27 01:16:45 -080069 return 0;
70}
71
Alessandro Zummofd507e22006-03-27 01:16:45 -080072static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq)
73{
74 unsigned short preload, delete;
75
Hartley Sweeten38f7b002009-04-15 23:18:26 +010076 ep93xx_rtc_get_swcomp(dev, &preload, &delete);
Alessandro Zummofd507e22006-03-27 01:16:45 -080077
Alessandro Zummofd507e22006-03-27 01:16:45 -080078 seq_printf(seq, "preload\t\t: %d\n", preload);
79 seq_printf(seq, "delete\t\t: %d\n", delete);
80
81 return 0;
82}
83
David Brownellff8371a2006-09-30 23:28:17 -070084static const struct rtc_class_ops ep93xx_rtc_ops = {
Alessandro Zummofd507e22006-03-27 01:16:45 -080085 .read_time = ep93xx_rtc_read_time,
Alexandre Bellonief9440a2019-04-19 10:00:03 +020086 .set_time = ep93xx_rtc_set_time,
Alessandro Zummofd507e22006-03-27 01:16:45 -080087 .proc = ep93xx_rtc_proc,
88};
89
Alexandre Bellonid71c7712019-04-19 10:00:05 +020090static ssize_t comp_preload_show(struct device *dev,
91 struct device_attribute *attr, char *buf)
Alessandro Zummofd507e22006-03-27 01:16:45 -080092{
93 unsigned short preload;
94
Alexandre Belloni09cd0302019-04-19 10:00:00 +020095 ep93xx_rtc_get_swcomp(dev->parent, &preload, NULL);
Alessandro Zummofd507e22006-03-27 01:16:45 -080096
97 return sprintf(buf, "%d\n", preload);
98}
Alexandre Bellonid71c7712019-04-19 10:00:05 +020099static DEVICE_ATTR_RO(comp_preload);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800100
Alexandre Bellonid71c7712019-04-19 10:00:05 +0200101static ssize_t comp_delete_show(struct device *dev,
102 struct device_attribute *attr, char *buf)
Alessandro Zummofd507e22006-03-27 01:16:45 -0800103{
104 unsigned short delete;
105
Alexandre Belloni09cd0302019-04-19 10:00:00 +0200106 ep93xx_rtc_get_swcomp(dev->parent, NULL, &delete);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800107
108 return sprintf(buf, "%d\n", delete);
109}
Alexandre Bellonid71c7712019-04-19 10:00:05 +0200110static DEVICE_ATTR_RO(comp_delete);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800111
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800112static struct attribute *ep93xx_rtc_attrs[] = {
113 &dev_attr_comp_preload.attr,
114 &dev_attr_comp_delete.attr,
115 NULL
116};
117
118static const struct attribute_group ep93xx_rtc_sysfs_files = {
119 .attrs = ep93xx_rtc_attrs,
120};
Alessandro Zummofd507e22006-03-27 01:16:45 -0800121
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800122static int ep93xx_rtc_probe(struct platform_device *pdev)
Alessandro Zummofd507e22006-03-27 01:16:45 -0800123{
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100124 struct ep93xx_rtc *ep93xx_rtc;
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100125 int err;
Alessandro Zummofd507e22006-03-27 01:16:45 -0800126
H Hartley Sweetenb4877d22010-03-05 13:44:20 -0800127 ep93xx_rtc = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_rtc), GFP_KERNEL);
128 if (!ep93xx_rtc)
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100129 return -ENOMEM;
130
YueHaibing09ef18b2019-10-06 18:29:20 +0800131 ep93xx_rtc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700132 if (IS_ERR(ep93xx_rtc->mmio_base))
133 return PTR_ERR(ep93xx_rtc->mmio_base);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800134
Axel Linbf6ed022011-08-10 21:11:26 +0800135 platform_set_drvdata(pdev, ep93xx_rtc);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800136
Alexandre Bellonibac68b32019-04-19 09:59:59 +0200137 ep93xx_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
Alexandre Bellonib809d192019-04-19 09:59:58 +0200138 if (IS_ERR(ep93xx_rtc->rtc))
139 return PTR_ERR(ep93xx_rtc->rtc);
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100140
Alexandre Bellonibac68b32019-04-19 09:59:59 +0200141 ep93xx_rtc->rtc->ops = &ep93xx_rtc_ops;
Alexandre Belloni2d4fc6d2019-04-19 10:00:01 +0200142 ep93xx_rtc->rtc->range_max = U32_MAX;
Alexandre Bellonibac68b32019-04-19 09:59:59 +0200143
Alexandre Belloni09cd0302019-04-19 10:00:00 +0200144 err = rtc_add_group(ep93xx_rtc->rtc, &ep93xx_rtc_sysfs_files);
Alexandre Bellonibac68b32019-04-19 09:59:59 +0200145 if (err)
146 return err;
147
Bartosz Golaszewskifdcfd852020-11-09 17:34:08 +0100148 return devm_rtc_register_device(ep93xx_rtc->rtc);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800149}
150
Hartley Sweeten38f7b002009-04-15 23:18:26 +0100151static struct platform_driver ep93xx_rtc_driver = {
Alessandro Zummofd507e22006-03-27 01:16:45 -0800152 .driver = {
153 .name = "ep93xx-rtc",
Alessandro Zummofd507e22006-03-27 01:16:45 -0800154 },
H Hartley Sweeten84d56b32012-05-29 15:07:36 -0700155 .probe = ep93xx_rtc_probe,
Alessandro Zummofd507e22006-03-27 01:16:45 -0800156};
157
H Hartley Sweeten84d56b32012-05-29 15:07:36 -0700158module_platform_driver(ep93xx_rtc_driver);
Alessandro Zummofd507e22006-03-27 01:16:45 -0800159
160MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
161MODULE_DESCRIPTION("EP93XX RTC driver");
162MODULE_LICENSE("GPL");
H Hartley Sweeten84d56b32012-05-29 15:07:36 -0700163MODULE_ALIAS("platform:ep93xx-rtc");