1*82ee16cfSKarel Balej // SPDX-License-Identifier: GPL-2.0-only
2*82ee16cfSKarel Balej #include <linux/limits.h>
3*82ee16cfSKarel Balej #include <linux/mod_devicetable.h>
4*82ee16cfSKarel Balej #include <linux/platform_device.h>
5*82ee16cfSKarel Balej #include <linux/rtc.h>
6*82ee16cfSKarel Balej
7*82ee16cfSKarel Balej #include <linux/mfd/88pm886.h>
8*82ee16cfSKarel Balej
9*82ee16cfSKarel Balej /*
10*82ee16cfSKarel Balej * Time is calculated as the sum of a 32-bit read-only advancing counter and a
11*82ee16cfSKarel Balej * writeable constant offset stored in the chip's spare registers.
12*82ee16cfSKarel Balej */
13*82ee16cfSKarel Balej
pm886_rtc_read_time(struct device * dev,struct rtc_time * tm)14*82ee16cfSKarel Balej static int pm886_rtc_read_time(struct device *dev, struct rtc_time *tm)
15*82ee16cfSKarel Balej {
16*82ee16cfSKarel Balej struct regmap *regmap = dev_get_drvdata(dev);
17*82ee16cfSKarel Balej u32 time;
18*82ee16cfSKarel Balej u32 buf;
19*82ee16cfSKarel Balej int ret;
20*82ee16cfSKarel Balej
21*82ee16cfSKarel Balej ret = regmap_bulk_read(regmap, PM886_REG_RTC_SPARE1, &buf, 4);
22*82ee16cfSKarel Balej if (ret)
23*82ee16cfSKarel Balej return ret;
24*82ee16cfSKarel Balej time = buf;
25*82ee16cfSKarel Balej
26*82ee16cfSKarel Balej ret = regmap_bulk_read(regmap, PM886_REG_RTC_CNT1, &buf, 4);
27*82ee16cfSKarel Balej if (ret)
28*82ee16cfSKarel Balej return ret;
29*82ee16cfSKarel Balej time += buf;
30*82ee16cfSKarel Balej
31*82ee16cfSKarel Balej rtc_time64_to_tm(time, tm);
32*82ee16cfSKarel Balej
33*82ee16cfSKarel Balej return 0;
34*82ee16cfSKarel Balej }
35*82ee16cfSKarel Balej
pm886_rtc_set_time(struct device * dev,struct rtc_time * tm)36*82ee16cfSKarel Balej static int pm886_rtc_set_time(struct device *dev, struct rtc_time *tm)
37*82ee16cfSKarel Balej {
38*82ee16cfSKarel Balej struct regmap *regmap = dev_get_drvdata(dev);
39*82ee16cfSKarel Balej u32 buf;
40*82ee16cfSKarel Balej int ret;
41*82ee16cfSKarel Balej
42*82ee16cfSKarel Balej ret = regmap_bulk_read(regmap, PM886_REG_RTC_CNT1, &buf, 4);
43*82ee16cfSKarel Balej if (ret)
44*82ee16cfSKarel Balej return ret;
45*82ee16cfSKarel Balej
46*82ee16cfSKarel Balej buf = rtc_tm_to_time64(tm) - buf;
47*82ee16cfSKarel Balej
48*82ee16cfSKarel Balej return regmap_bulk_write(regmap, PM886_REG_RTC_SPARE1, &buf, 4);
49*82ee16cfSKarel Balej }
50*82ee16cfSKarel Balej
51*82ee16cfSKarel Balej static const struct rtc_class_ops pm886_rtc_ops = {
52*82ee16cfSKarel Balej .read_time = pm886_rtc_read_time,
53*82ee16cfSKarel Balej .set_time = pm886_rtc_set_time,
54*82ee16cfSKarel Balej };
55*82ee16cfSKarel Balej
pm886_rtc_probe(struct platform_device * pdev)56*82ee16cfSKarel Balej static int pm886_rtc_probe(struct platform_device *pdev)
57*82ee16cfSKarel Balej {
58*82ee16cfSKarel Balej struct pm886_chip *chip = dev_get_drvdata(pdev->dev.parent);
59*82ee16cfSKarel Balej struct device *dev = &pdev->dev;
60*82ee16cfSKarel Balej struct rtc_device *rtc;
61*82ee16cfSKarel Balej int ret;
62*82ee16cfSKarel Balej
63*82ee16cfSKarel Balej platform_set_drvdata(pdev, chip->regmap);
64*82ee16cfSKarel Balej
65*82ee16cfSKarel Balej rtc = devm_rtc_allocate_device(dev);
66*82ee16cfSKarel Balej if (IS_ERR(rtc))
67*82ee16cfSKarel Balej return dev_err_probe(dev, PTR_ERR(rtc),
68*82ee16cfSKarel Balej "Failed to allocate RTC device\n");
69*82ee16cfSKarel Balej
70*82ee16cfSKarel Balej rtc->ops = &pm886_rtc_ops;
71*82ee16cfSKarel Balej rtc->range_max = U32_MAX;
72*82ee16cfSKarel Balej
73*82ee16cfSKarel Balej ret = devm_rtc_register_device(rtc);
74*82ee16cfSKarel Balej if (ret)
75*82ee16cfSKarel Balej return dev_err_probe(dev, ret, "Failed to register RTC device\n");
76*82ee16cfSKarel Balej
77*82ee16cfSKarel Balej return 0;
78*82ee16cfSKarel Balej }
79*82ee16cfSKarel Balej
80*82ee16cfSKarel Balej static const struct platform_device_id pm886_rtc_id_table[] = {
81*82ee16cfSKarel Balej { "88pm886-rtc", },
82*82ee16cfSKarel Balej { }
83*82ee16cfSKarel Balej };
84*82ee16cfSKarel Balej MODULE_DEVICE_TABLE(platform, pm886_rtc_id_table);
85*82ee16cfSKarel Balej
86*82ee16cfSKarel Balej static struct platform_driver pm886_rtc_driver = {
87*82ee16cfSKarel Balej .driver = {
88*82ee16cfSKarel Balej .name = "88pm886-rtc",
89*82ee16cfSKarel Balej },
90*82ee16cfSKarel Balej .probe = pm886_rtc_probe,
91*82ee16cfSKarel Balej .id_table = pm886_rtc_id_table,
92*82ee16cfSKarel Balej };
93*82ee16cfSKarel Balej module_platform_driver(pm886_rtc_driver);
94*82ee16cfSKarel Balej
95*82ee16cfSKarel Balej MODULE_DESCRIPTION("Marvell 88PM886 RTC driver");
96*82ee16cfSKarel Balej MODULE_AUTHOR("Karel Balej <balejk@matfyz.cz>");
97*82ee16cfSKarel Balej MODULE_LICENSE("GPL");
98