xref: /openbmc/linux/drivers/rtc/rtc-ds1742.c (revision 3db55767)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * An rtc driver for the Dallas DS1742
4  *
5  * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
6  *
7  * Copyright (C) 2006 Torsten Ertbjerg Rasmussen <tr@newtec.dk>
8  *  - nvram size determined from resource
9  *  - this ds1742 driver now supports ds1743.
10  */
11 
12 #include <linux/bcd.h>
13 #include <linux/kernel.h>
14 #include <linux/gfp.h>
15 #include <linux/delay.h>
16 #include <linux/jiffies.h>
17 #include <linux/rtc.h>
18 #include <linux/of.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/io.h>
22 #include <linux/module.h>
23 
24 #define RTC_SIZE		8
25 
26 #define RTC_CONTROL		0
27 #define RTC_CENTURY		0
28 #define RTC_SECONDS		1
29 #define RTC_MINUTES		2
30 #define RTC_HOURS		3
31 #define RTC_DAY			4
32 #define RTC_DATE		5
33 #define RTC_MONTH		6
34 #define RTC_YEAR		7
35 
36 #define RTC_CENTURY_MASK	0x3f
37 #define RTC_SECONDS_MASK	0x7f
38 #define RTC_DAY_MASK		0x07
39 
40 /* Bits in the Control/Century register */
41 #define RTC_WRITE		0x80
42 #define RTC_READ		0x40
43 
44 /* Bits in the Seconds register */
45 #define RTC_STOP		0x80
46 
47 /* Bits in the Day register */
48 #define RTC_BATT_FLAG		0x80
49 
50 struct rtc_plat_data {
51 	void __iomem *ioaddr_nvram;
52 	void __iomem *ioaddr_rtc;
53 	unsigned long last_jiffies;
54 };
55 
56 static int ds1742_rtc_set_time(struct device *dev, struct rtc_time *tm)
57 {
58 	struct rtc_plat_data *pdata = dev_get_drvdata(dev);
59 	void __iomem *ioaddr = pdata->ioaddr_rtc;
60 	u8 century;
61 
62 	century = bin2bcd((tm->tm_year + 1900) / 100);
63 
64 	writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
65 
66 	writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
67 	writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
68 	writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
69 	writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
70 	writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
71 	writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
72 	writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
73 
74 	/* RTC_CENTURY and RTC_CONTROL share same register */
75 	writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY);
76 	writeb(century & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
77 	return 0;
78 }
79 
80 static int ds1742_rtc_read_time(struct device *dev, struct rtc_time *tm)
81 {
82 	struct rtc_plat_data *pdata = dev_get_drvdata(dev);
83 	void __iomem *ioaddr = pdata->ioaddr_rtc;
84 	unsigned int year, month, day, hour, minute, second, week;
85 	unsigned int century;
86 
87 	/* give enough time to update RTC in case of continuous read */
88 	if (pdata->last_jiffies == jiffies)
89 		msleep(1);
90 	pdata->last_jiffies = jiffies;
91 	writeb(RTC_READ, ioaddr + RTC_CONTROL);
92 	second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
93 	minute = readb(ioaddr + RTC_MINUTES);
94 	hour = readb(ioaddr + RTC_HOURS);
95 	day = readb(ioaddr + RTC_DATE);
96 	week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
97 	month = readb(ioaddr + RTC_MONTH);
98 	year = readb(ioaddr + RTC_YEAR);
99 	century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
100 	writeb(0, ioaddr + RTC_CONTROL);
101 	tm->tm_sec = bcd2bin(second);
102 	tm->tm_min = bcd2bin(minute);
103 	tm->tm_hour = bcd2bin(hour);
104 	tm->tm_mday = bcd2bin(day);
105 	tm->tm_wday = bcd2bin(week);
106 	tm->tm_mon = bcd2bin(month) - 1;
107 	/* year is 1900 + tm->tm_year */
108 	tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
109 
110 	return 0;
111 }
112 
113 static const struct rtc_class_ops ds1742_rtc_ops = {
114 	.read_time	= ds1742_rtc_read_time,
115 	.set_time	= ds1742_rtc_set_time,
116 };
117 
118 static int ds1742_nvram_read(void *priv, unsigned int pos, void *val,
119 			     size_t bytes)
120 {
121 	struct rtc_plat_data *pdata = priv;
122 	void __iomem *ioaddr = pdata->ioaddr_nvram;
123 	u8 *buf = val;
124 
125 	for (; bytes; bytes--)
126 		*buf++ = readb(ioaddr + pos++);
127 	return 0;
128 }
129 
130 static int ds1742_nvram_write(void *priv, unsigned int pos, void *val,
131 			      size_t bytes)
132 {
133 	struct rtc_plat_data *pdata = priv;
134 	void __iomem *ioaddr = pdata->ioaddr_nvram;
135 	u8 *buf = val;
136 
137 	for (; bytes; bytes--)
138 		writeb(*buf++, ioaddr + pos++);
139 	return 0;
140 }
141 
142 static int ds1742_rtc_probe(struct platform_device *pdev)
143 {
144 	struct rtc_device *rtc;
145 	struct resource *res;
146 	unsigned int cen, sec;
147 	struct rtc_plat_data *pdata;
148 	void __iomem *ioaddr;
149 	int ret = 0;
150 	struct nvmem_config nvmem_cfg = {
151 		.name = "ds1742_nvram",
152 		.reg_read = ds1742_nvram_read,
153 		.reg_write = ds1742_nvram_write,
154 	};
155 
156 
157 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
158 	if (!pdata)
159 		return -ENOMEM;
160 
161 	ioaddr = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
162 	if (IS_ERR(ioaddr))
163 		return PTR_ERR(ioaddr);
164 
165 	pdata->ioaddr_nvram = ioaddr;
166 	pdata->ioaddr_rtc = ioaddr + resource_size(res) - RTC_SIZE;
167 
168 	nvmem_cfg.size = resource_size(res) - RTC_SIZE;
169 	nvmem_cfg.priv = pdata;
170 
171 	/* turn RTC on if it was not on */
172 	ioaddr = pdata->ioaddr_rtc;
173 	sec = readb(ioaddr + RTC_SECONDS);
174 	if (sec & RTC_STOP) {
175 		sec &= RTC_SECONDS_MASK;
176 		cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
177 		writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
178 		writeb(sec, ioaddr + RTC_SECONDS);
179 		writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
180 	}
181 	if (!(readb(ioaddr + RTC_DAY) & RTC_BATT_FLAG))
182 		dev_warn(&pdev->dev, "voltage-low detected.\n");
183 
184 	pdata->last_jiffies = jiffies;
185 	platform_set_drvdata(pdev, pdata);
186 
187 	rtc = devm_rtc_allocate_device(&pdev->dev);
188 	if (IS_ERR(rtc))
189 		return PTR_ERR(rtc);
190 
191 	rtc->ops = &ds1742_rtc_ops;
192 
193 	ret = devm_rtc_register_device(rtc);
194 	if (ret)
195 		return ret;
196 
197 	devm_rtc_nvmem_register(rtc, &nvmem_cfg);
198 
199 	return 0;
200 }
201 
202 static const struct of_device_id __maybe_unused ds1742_rtc_of_match[] = {
203 	{ .compatible = "maxim,ds1742", },
204 	{ }
205 };
206 MODULE_DEVICE_TABLE(of, ds1742_rtc_of_match);
207 
208 static struct platform_driver ds1742_rtc_driver = {
209 	.probe		= ds1742_rtc_probe,
210 	.driver		= {
211 		.name	= "rtc-ds1742",
212 		.of_match_table = of_match_ptr(ds1742_rtc_of_match),
213 	},
214 };
215 
216 module_platform_driver(ds1742_rtc_driver);
217 
218 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
219 MODULE_DESCRIPTION("Dallas DS1742 RTC driver");
220 MODULE_LICENSE("GPL");
221 MODULE_ALIAS("platform:rtc-ds1742");
222