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