1 // SPDX-License-Identifier: GPL-2.0
3 * A driver for the RTC embedded in the Cirrus Logic EP93XX processors
4 * Copyright (c) 2006 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/rtc.h>
12 #include <linux/platform_device.h>
14 #include <linux/gfp.h>
16 #define EP93XX_RTC_DATA 0x000
17 #define EP93XX_RTC_MATCH 0x004
18 #define EP93XX_RTC_STATUS 0x008
19 #define EP93XX_RTC_STATUS_INTR BIT(0)
20 #define EP93XX_RTC_LOAD 0x00C
21 #define EP93XX_RTC_CONTROL 0x010
22 #define EP93XX_RTC_CONTROL_MIE BIT(0)
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
30 void __iomem
*mmio_base
;
31 struct rtc_device
*rtc
;
34 static int ep93xx_rtc_get_swcomp(struct device
*dev
, unsigned short *preload
,
35 unsigned short *delete)
37 struct ep93xx_rtc
*ep93xx_rtc
= dev_get_drvdata(dev
);
40 comp
= readl(ep93xx_rtc
->mmio_base
+ EP93XX_RTC_SWCOMP
);
43 *preload
= (comp
& EP93XX_RTC_SWCOMP_INT_MASK
)
44 >> EP93XX_RTC_SWCOMP_INT_SHIFT
;
47 *delete = (comp
& EP93XX_RTC_SWCOMP_DEL_MASK
)
48 >> EP93XX_RTC_SWCOMP_DEL_SHIFT
;
53 static int ep93xx_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
55 struct ep93xx_rtc
*ep93xx_rtc
= dev_get_drvdata(dev
);
58 time
= readl(ep93xx_rtc
->mmio_base
+ EP93XX_RTC_DATA
);
60 rtc_time64_to_tm(time
, tm
);
64 static int ep93xx_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
66 struct ep93xx_rtc
*ep93xx_rtc
= dev_get_drvdata(dev
);
67 unsigned long secs
= rtc_tm_to_time64(tm
);
69 writel(secs
+ 1, ep93xx_rtc
->mmio_base
+ EP93XX_RTC_LOAD
);
73 static int ep93xx_rtc_proc(struct device
*dev
, struct seq_file
*seq
)
75 unsigned short preload
, delete;
77 ep93xx_rtc_get_swcomp(dev
, &preload
, &delete);
79 seq_printf(seq
, "preload\t\t: %d\n", preload
);
80 seq_printf(seq
, "delete\t\t: %d\n", delete);
85 static const struct rtc_class_ops ep93xx_rtc_ops
= {
86 .read_time
= ep93xx_rtc_read_time
,
87 .set_time
= ep93xx_rtc_set_time
,
88 .proc
= ep93xx_rtc_proc
,
91 static ssize_t
comp_preload_show(struct device
*dev
,
92 struct device_attribute
*attr
, char *buf
)
94 unsigned short preload
;
96 ep93xx_rtc_get_swcomp(dev
->parent
, &preload
, NULL
);
98 return sprintf(buf
, "%d\n", preload
);
100 static DEVICE_ATTR_RO(comp_preload
);
102 static ssize_t
comp_delete_show(struct device
*dev
,
103 struct device_attribute
*attr
, char *buf
)
105 unsigned short delete;
107 ep93xx_rtc_get_swcomp(dev
->parent
, NULL
, &delete);
109 return sprintf(buf
, "%d\n", delete);
111 static DEVICE_ATTR_RO(comp_delete
);
113 static struct attribute
*ep93xx_rtc_attrs
[] = {
114 &dev_attr_comp_preload
.attr
,
115 &dev_attr_comp_delete
.attr
,
119 static const struct attribute_group ep93xx_rtc_sysfs_files
= {
120 .attrs
= ep93xx_rtc_attrs
,
123 static int ep93xx_rtc_probe(struct platform_device
*pdev
)
125 struct ep93xx_rtc
*ep93xx_rtc
;
128 ep93xx_rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*ep93xx_rtc
), GFP_KERNEL
);
132 ep93xx_rtc
->mmio_base
= devm_platform_ioremap_resource(pdev
, 0);
133 if (IS_ERR(ep93xx_rtc
->mmio_base
))
134 return PTR_ERR(ep93xx_rtc
->mmio_base
);
136 platform_set_drvdata(pdev
, ep93xx_rtc
);
138 ep93xx_rtc
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
139 if (IS_ERR(ep93xx_rtc
->rtc
))
140 return PTR_ERR(ep93xx_rtc
->rtc
);
142 ep93xx_rtc
->rtc
->ops
= &ep93xx_rtc_ops
;
143 ep93xx_rtc
->rtc
->range_max
= U32_MAX
;
145 err
= rtc_add_group(ep93xx_rtc
->rtc
, &ep93xx_rtc_sysfs_files
);
149 return devm_rtc_register_device(ep93xx_rtc
->rtc
);
152 static const struct of_device_id ep93xx_rtc_of_ids
[] = {
153 { .compatible
= "cirrus,ep9301-rtc" },
156 MODULE_DEVICE_TABLE(of
, ep93xx_rtc_of_ids
);
158 static struct platform_driver ep93xx_rtc_driver
= {
160 .name
= "ep93xx-rtc",
161 .of_match_table
= ep93xx_rtc_of_ids
,
163 .probe
= ep93xx_rtc_probe
,
166 module_platform_driver(ep93xx_rtc_driver
);
168 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
169 MODULE_DESCRIPTION("EP93XX RTC driver");
170 MODULE_LICENSE("GPL");
171 MODULE_ALIAS("platform:ep93xx-rtc");