14fdf4d23SAlexandre Belloni // SPDX-License-Identifier: GPL-2.0
2fd507e2fSAlessandro Zummo /*
3fd507e2fSAlessandro Zummo * A driver for the RTC embedded in the Cirrus Logic EP93XX processors
4fd507e2fSAlessandro Zummo * Copyright (c) 2006 Tower Technologies
5fd507e2fSAlessandro Zummo *
6fd507e2fSAlessandro Zummo * Author: Alessandro Zummo <a.zummo@towertech.it>
7fd507e2fSAlessandro Zummo */
8fd507e2fSAlessandro Zummo
9fd507e2fSAlessandro Zummo #include <linux/module.h>
10*1d70f9feSNikita Shubin #include <linux/mod_devicetable.h>
11fd507e2fSAlessandro Zummo #include <linux/rtc.h>
12fd507e2fSAlessandro Zummo #include <linux/platform_device.h>
1338f7b009SHartley Sweeten #include <linux/io.h>
145a0e3ad6STejun Heo #include <linux/gfp.h>
15fd507e2fSAlessandro Zummo
1638f7b009SHartley Sweeten #define EP93XX_RTC_DATA 0x000
1738f7b009SHartley Sweeten #define EP93XX_RTC_MATCH 0x004
1838f7b009SHartley Sweeten #define EP93XX_RTC_STATUS 0x008
19d71c7715SAlexandre Belloni #define EP93XX_RTC_STATUS_INTR BIT(0)
2038f7b009SHartley Sweeten #define EP93XX_RTC_LOAD 0x00C
2138f7b009SHartley Sweeten #define EP93XX_RTC_CONTROL 0x010
22d71c7715SAlexandre Belloni #define EP93XX_RTC_CONTROL_MIE BIT(0)
2338f7b009SHartley Sweeten #define EP93XX_RTC_SWCOMP 0x108
2438f7b009SHartley Sweeten #define EP93XX_RTC_SWCOMP_DEL_MASK 0x001f0000
2538f7b009SHartley Sweeten #define EP93XX_RTC_SWCOMP_DEL_SHIFT 16
2638f7b009SHartley Sweeten #define EP93XX_RTC_SWCOMP_INT_MASK 0x0000ffff
2738f7b009SHartley Sweeten #define EP93XX_RTC_SWCOMP_INT_SHIFT 0
28fd507e2fSAlessandro Zummo
2938f7b009SHartley Sweeten struct ep93xx_rtc {
3038f7b009SHartley Sweeten void __iomem *mmio_base;
31bf6ed027SAxel Lin struct rtc_device *rtc;
3238f7b009SHartley Sweeten };
3338f7b009SHartley Sweeten
ep93xx_rtc_get_swcomp(struct device * dev,unsigned short * preload,unsigned short * delete)3438f7b009SHartley Sweeten static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload,
35fd507e2fSAlessandro Zummo unsigned short *delete)
36fd507e2fSAlessandro Zummo {
3700c33482SNikita Shubin struct ep93xx_rtc *ep93xx_rtc = dev_get_drvdata(dev);
3838f7b009SHartley Sweeten unsigned long comp;
3938f7b009SHartley Sweeten
40ff32ff17SH Hartley Sweeten comp = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP);
41fd507e2fSAlessandro Zummo
42fd507e2fSAlessandro Zummo if (preload)
4338f7b009SHartley Sweeten *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK)
4438f7b009SHartley Sweeten >> EP93XX_RTC_SWCOMP_INT_SHIFT;
45fd507e2fSAlessandro Zummo
46fd507e2fSAlessandro Zummo if (delete)
4738f7b009SHartley Sweeten *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK)
4838f7b009SHartley Sweeten >> EP93XX_RTC_SWCOMP_DEL_SHIFT;
49fd507e2fSAlessandro Zummo
50fd507e2fSAlessandro Zummo return 0;
51fd507e2fSAlessandro Zummo }
52fd507e2fSAlessandro Zummo
ep93xx_rtc_read_time(struct device * dev,struct rtc_time * tm)53fd507e2fSAlessandro Zummo static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm)
54fd507e2fSAlessandro Zummo {
5500c33482SNikita Shubin struct ep93xx_rtc *ep93xx_rtc = dev_get_drvdata(dev);
5638f7b009SHartley Sweeten unsigned long time;
5738f7b009SHartley Sweeten
58ff32ff17SH Hartley Sweeten time = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA);
59fd507e2fSAlessandro Zummo
60886a77e7SAlexandre Belloni rtc_time64_to_tm(time, tm);
61fd507e2fSAlessandro Zummo return 0;
62fd507e2fSAlessandro Zummo }
63fd507e2fSAlessandro Zummo
ep93xx_rtc_set_time(struct device * dev,struct rtc_time * tm)64ef9440a2SAlexandre Belloni static int ep93xx_rtc_set_time(struct device *dev, struct rtc_time *tm)
65fd507e2fSAlessandro Zummo {
6600c33482SNikita Shubin struct ep93xx_rtc *ep93xx_rtc = dev_get_drvdata(dev);
67ef9440a2SAlexandre Belloni unsigned long secs = rtc_tm_to_time64(tm);
6838f7b009SHartley Sweeten
69ff32ff17SH Hartley Sweeten writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD);
70fd507e2fSAlessandro Zummo return 0;
71fd507e2fSAlessandro Zummo }
72fd507e2fSAlessandro Zummo
ep93xx_rtc_proc(struct device * dev,struct seq_file * seq)73fd507e2fSAlessandro Zummo static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq)
74fd507e2fSAlessandro Zummo {
75fd507e2fSAlessandro Zummo unsigned short preload, delete;
76fd507e2fSAlessandro Zummo
7738f7b009SHartley Sweeten ep93xx_rtc_get_swcomp(dev, &preload, &delete);
78fd507e2fSAlessandro Zummo
79fd507e2fSAlessandro Zummo seq_printf(seq, "preload\t\t: %d\n", preload);
80fd507e2fSAlessandro Zummo seq_printf(seq, "delete\t\t: %d\n", delete);
81fd507e2fSAlessandro Zummo
82fd507e2fSAlessandro Zummo return 0;
83fd507e2fSAlessandro Zummo }
84fd507e2fSAlessandro Zummo
85ff8371acSDavid Brownell static const struct rtc_class_ops ep93xx_rtc_ops = {
86fd507e2fSAlessandro Zummo .read_time = ep93xx_rtc_read_time,
87ef9440a2SAlexandre Belloni .set_time = ep93xx_rtc_set_time,
88fd507e2fSAlessandro Zummo .proc = ep93xx_rtc_proc,
89fd507e2fSAlessandro Zummo };
90fd507e2fSAlessandro Zummo
comp_preload_show(struct device * dev,struct device_attribute * attr,char * buf)91d71c7715SAlexandre Belloni static ssize_t comp_preload_show(struct device *dev,
92fd507e2fSAlessandro Zummo struct device_attribute *attr, char *buf)
93fd507e2fSAlessandro Zummo {
94fd507e2fSAlessandro Zummo unsigned short preload;
95fd507e2fSAlessandro Zummo
9609cd030bSAlexandre Belloni ep93xx_rtc_get_swcomp(dev->parent, &preload, NULL);
97fd507e2fSAlessandro Zummo
98fd507e2fSAlessandro Zummo return sprintf(buf, "%d\n", preload);
99fd507e2fSAlessandro Zummo }
100d71c7715SAlexandre Belloni static DEVICE_ATTR_RO(comp_preload);
101fd507e2fSAlessandro Zummo
comp_delete_show(struct device * dev,struct device_attribute * attr,char * buf)102d71c7715SAlexandre Belloni static ssize_t comp_delete_show(struct device *dev,
103fd507e2fSAlessandro Zummo struct device_attribute *attr, char *buf)
104fd507e2fSAlessandro Zummo {
105fd507e2fSAlessandro Zummo unsigned short delete;
106fd507e2fSAlessandro Zummo
10709cd030bSAlexandre Belloni ep93xx_rtc_get_swcomp(dev->parent, NULL, &delete);
108fd507e2fSAlessandro Zummo
109fd507e2fSAlessandro Zummo return sprintf(buf, "%d\n", delete);
110fd507e2fSAlessandro Zummo }
111d71c7715SAlexandre Belloni static DEVICE_ATTR_RO(comp_delete);
112fd507e2fSAlessandro Zummo
113b4877d2bSH Hartley Sweeten static struct attribute *ep93xx_rtc_attrs[] = {
114b4877d2bSH Hartley Sweeten &dev_attr_comp_preload.attr,
115b4877d2bSH Hartley Sweeten &dev_attr_comp_delete.attr,
116b4877d2bSH Hartley Sweeten NULL
117b4877d2bSH Hartley Sweeten };
118b4877d2bSH Hartley Sweeten
119b4877d2bSH Hartley Sweeten static const struct attribute_group ep93xx_rtc_sysfs_files = {
120b4877d2bSH Hartley Sweeten .attrs = ep93xx_rtc_attrs,
121b4877d2bSH Hartley Sweeten };
122fd507e2fSAlessandro Zummo
ep93xx_rtc_probe(struct platform_device * pdev)1235a167f45SGreg Kroah-Hartman static int ep93xx_rtc_probe(struct platform_device *pdev)
124fd507e2fSAlessandro Zummo {
12538f7b009SHartley Sweeten struct ep93xx_rtc *ep93xx_rtc;
12638f7b009SHartley Sweeten int err;
127fd507e2fSAlessandro Zummo
128b4877d2bSH Hartley Sweeten ep93xx_rtc = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_rtc), GFP_KERNEL);
129b4877d2bSH Hartley Sweeten if (!ep93xx_rtc)
13038f7b009SHartley Sweeten return -ENOMEM;
13138f7b009SHartley Sweeten
13209ef18bcSYueHaibing ep93xx_rtc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
1337c1d69eeSJulia Lawall if (IS_ERR(ep93xx_rtc->mmio_base))
1347c1d69eeSJulia Lawall return PTR_ERR(ep93xx_rtc->mmio_base);
135fd507e2fSAlessandro Zummo
136bf6ed027SAxel Lin platform_set_drvdata(pdev, ep93xx_rtc);
137fd507e2fSAlessandro Zummo
138bac68b30SAlexandre Belloni ep93xx_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
139b809d192SAlexandre Belloni if (IS_ERR(ep93xx_rtc->rtc))
140b809d192SAlexandre Belloni return PTR_ERR(ep93xx_rtc->rtc);
14138f7b009SHartley Sweeten
142bac68b30SAlexandre Belloni ep93xx_rtc->rtc->ops = &ep93xx_rtc_ops;
1432d4fc6dfSAlexandre Belloni ep93xx_rtc->rtc->range_max = U32_MAX;
144bac68b30SAlexandre Belloni
14509cd030bSAlexandre Belloni err = rtc_add_group(ep93xx_rtc->rtc, &ep93xx_rtc_sysfs_files);
146bac68b30SAlexandre Belloni if (err)
147bac68b30SAlexandre Belloni return err;
148bac68b30SAlexandre Belloni
149fdcfd854SBartosz Golaszewski return devm_rtc_register_device(ep93xx_rtc->rtc);
150fd507e2fSAlessandro Zummo }
151fd507e2fSAlessandro Zummo
152*1d70f9feSNikita Shubin static const struct of_device_id ep93xx_rtc_of_ids[] = {
153*1d70f9feSNikita Shubin { .compatible = "cirrus,ep9301-rtc" },
154*1d70f9feSNikita Shubin { /* sentinel */ }
155*1d70f9feSNikita Shubin };
156*1d70f9feSNikita Shubin MODULE_DEVICE_TABLE(of, ep93xx_rtc_of_ids);
157*1d70f9feSNikita Shubin
15838f7b009SHartley Sweeten static struct platform_driver ep93xx_rtc_driver = {
159fd507e2fSAlessandro Zummo .driver = {
160fd507e2fSAlessandro Zummo .name = "ep93xx-rtc",
161*1d70f9feSNikita Shubin .of_match_table = ep93xx_rtc_of_ids,
162fd507e2fSAlessandro Zummo },
16384d56b38SH Hartley Sweeten .probe = ep93xx_rtc_probe,
164fd507e2fSAlessandro Zummo };
165fd507e2fSAlessandro Zummo
16684d56b38SH Hartley Sweeten module_platform_driver(ep93xx_rtc_driver);
167fd507e2fSAlessandro Zummo
168fd507e2fSAlessandro Zummo MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
169fd507e2fSAlessandro Zummo MODULE_DESCRIPTION("EP93XX RTC driver");
170fd507e2fSAlessandro Zummo MODULE_LICENSE("GPL");
17184d56b38SH Hartley Sweeten MODULE_ALIAS("platform:ep93xx-rtc");
172