Linux 6.10
[linux-block.git] / drivers / rtc / rtc-ep93xx.c
CommitLineData
4fdf4d23 1// SPDX-License-Identifier: GPL-2.0
fd507e2f
AZ
2/*
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>
fd507e2f
AZ
7 */
8
9#include <linux/module.h>
1d70f9fe 10#include <linux/mod_devicetable.h>
fd507e2f
AZ
11#include <linux/rtc.h>
12#include <linux/platform_device.h>
38f7b009 13#include <linux/io.h>
5a0e3ad6 14#include <linux/gfp.h>
38f7b009
HS
15
16#define EP93XX_RTC_DATA 0x000
17#define EP93XX_RTC_MATCH 0x004
18#define EP93XX_RTC_STATUS 0x008
d71c7715 19#define EP93XX_RTC_STATUS_INTR BIT(0)
38f7b009
HS
20#define EP93XX_RTC_LOAD 0x00C
21#define EP93XX_RTC_CONTROL 0x010
d71c7715 22#define EP93XX_RTC_CONTROL_MIE BIT(0)
38f7b009
HS
23#define EP93XX_RTC_SWCOMP 0x108
24#define EP93XX_RTC_SWCOMP_DEL_MASK 0x001f0000
25#define EP93XX_RTC_SWCOMP_DEL_SHIFT 16
26#define EP93XX_RTC_SWCOMP_INT_MASK 0x0000ffff
27#define EP93XX_RTC_SWCOMP_INT_SHIFT 0
28
38f7b009
HS
29struct ep93xx_rtc {
30 void __iomem *mmio_base;
bf6ed027 31 struct rtc_device *rtc;
38f7b009 32};
fd507e2f 33
38f7b009 34static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload,
d71c7715 35 unsigned short *delete)
fd507e2f 36{
00c33482 37 struct ep93xx_rtc *ep93xx_rtc = dev_get_drvdata(dev);
38f7b009
HS
38 unsigned long comp;
39
ff32ff17 40 comp = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP);
fd507e2f
AZ
41
42 if (preload)
38f7b009
HS
43 *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK)
44 >> EP93XX_RTC_SWCOMP_INT_SHIFT;
fd507e2f
AZ
45
46 if (delete)
38f7b009
HS
47 *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK)
48 >> EP93XX_RTC_SWCOMP_DEL_SHIFT;
fd507e2f
AZ
49
50 return 0;
51}
52
53static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm)
54{
00c33482 55 struct ep93xx_rtc *ep93xx_rtc = dev_get_drvdata(dev);
38f7b009
HS
56 unsigned long time;
57
725412d9 58 time = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA);
fd507e2f 59
886a77e7 60 rtc_time64_to_tm(time, tm);
fd507e2f
AZ
61 return 0;
62}
63
ef9440a2 64static int ep93xx_rtc_set_time(struct device *dev, struct rtc_time *tm)
fd507e2f 65{
00c33482 66 struct ep93xx_rtc *ep93xx_rtc = dev_get_drvdata(dev);
ef9440a2 67 unsigned long secs = rtc_tm_to_time64(tm);
38f7b009 68
ff32ff17 69 writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD);
fd507e2f
AZ
70 return 0;
71}
72
fd507e2f
AZ
73static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq)
74{
75 unsigned short preload, delete;
76
38f7b009 77 ep93xx_rtc_get_swcomp(dev, &preload, &delete);
fd507e2f 78
fd507e2f
AZ
79 seq_printf(seq, "preload\t\t: %d\n", preload);
80 seq_printf(seq, "delete\t\t: %d\n", delete);
81
82 return 0;
83}
84
ff8371ac 85static const struct rtc_class_ops ep93xx_rtc_ops = {
fd507e2f 86 .read_time = ep93xx_rtc_read_time,
ef9440a2 87 .set_time = ep93xx_rtc_set_time,
fd507e2f
AZ
88 .proc = ep93xx_rtc_proc,
89};
90
d71c7715
AB
91static ssize_t comp_preload_show(struct device *dev,
92 struct device_attribute *attr, char *buf)
fd507e2f
AZ
93{
94 unsigned short preload;
95
09cd030b 96 ep93xx_rtc_get_swcomp(dev->parent, &preload, NULL);
fd507e2f
AZ
97
98 return sprintf(buf, "%d\n", preload);
99}
d71c7715 100static DEVICE_ATTR_RO(comp_preload);
fd507e2f 101
d71c7715
AB
102static ssize_t comp_delete_show(struct device *dev,
103 struct device_attribute *attr, char *buf)
fd507e2f
AZ
104{
105 unsigned short delete;
106
09cd030b 107 ep93xx_rtc_get_swcomp(dev->parent, NULL, &delete);
fd507e2f
AZ
108
109 return sprintf(buf, "%d\n", delete);
110}
d71c7715 111static DEVICE_ATTR_RO(comp_delete);
fd507e2f 112
b4877d2b
HS
113static struct attribute *ep93xx_rtc_attrs[] = {
114 &dev_attr_comp_preload.attr,
115 &dev_attr_comp_delete.attr,
116 NULL
117};
118
119static const struct attribute_group ep93xx_rtc_sysfs_files = {
120 .attrs = ep93xx_rtc_attrs,
121};
fd507e2f 122
5a167f45 123static int ep93xx_rtc_probe(struct platform_device *pdev)
fd507e2f 124{
38f7b009 125 struct ep93xx_rtc *ep93xx_rtc;
38f7b009
HS
126 int err;
127
b4877d2b
HS
128 ep93xx_rtc = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_rtc), GFP_KERNEL);
129 if (!ep93xx_rtc)
38f7b009
HS
130 return -ENOMEM;
131
09ef18bc 132 ep93xx_rtc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
7c1d69ee
JL
133 if (IS_ERR(ep93xx_rtc->mmio_base))
134 return PTR_ERR(ep93xx_rtc->mmio_base);
fd507e2f 135
bf6ed027 136 platform_set_drvdata(pdev, ep93xx_rtc);
38f7b009 137
bac68b30 138 ep93xx_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
b809d192
AB
139 if (IS_ERR(ep93xx_rtc->rtc))
140 return PTR_ERR(ep93xx_rtc->rtc);
fd507e2f 141
bac68b30 142 ep93xx_rtc->rtc->ops = &ep93xx_rtc_ops;
2d4fc6df 143 ep93xx_rtc->rtc->range_max = U32_MAX;
bac68b30 144
09cd030b 145 err = rtc_add_group(ep93xx_rtc->rtc, &ep93xx_rtc_sysfs_files);
bac68b30
AB
146 if (err)
147 return err;
148
fdcfd854 149 return devm_rtc_register_device(ep93xx_rtc->rtc);
fd507e2f
AZ
150}
151
1d70f9fe
NS
152static const struct of_device_id ep93xx_rtc_of_ids[] = {
153 { .compatible = "cirrus,ep9301-rtc" },
154 { /* sentinel */ }
155};
156MODULE_DEVICE_TABLE(of, ep93xx_rtc_of_ids);
157
38f7b009 158static struct platform_driver ep93xx_rtc_driver = {
fd507e2f
AZ
159 .driver = {
160 .name = "ep93xx-rtc",
1d70f9fe 161 .of_match_table = ep93xx_rtc_of_ids,
fd507e2f 162 },
84d56b38 163 .probe = ep93xx_rtc_probe,
fd507e2f
AZ
164};
165
84d56b38 166module_platform_driver(ep93xx_rtc_driver);
fd507e2f
AZ
167
168MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
169MODULE_DESCRIPTION("EP93XX RTC driver");
170MODULE_LICENSE("GPL");
84d56b38 171MODULE_ALIAS("platform:ep93xx-rtc");