xref: /openbmc/linux/drivers/rtc/rtc-pcf8523.c (revision 65417d9f)
1 /*
2  * Copyright (C) 2012 Avionic Design GmbH
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 
9 #include <linux/bcd.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/rtc.h>
13 #include <linux/of.h>
14 
15 #define DRIVER_NAME "rtc-pcf8523"
16 
17 #define REG_CONTROL1 0x00
18 #define REG_CONTROL1_CAP_SEL (1 << 7)
19 #define REG_CONTROL1_STOP    (1 << 5)
20 
21 #define REG_CONTROL3 0x02
22 #define REG_CONTROL3_PM_BLD (1 << 7) /* battery low detection disabled */
23 #define REG_CONTROL3_PM_VDD (1 << 6) /* switch-over disabled */
24 #define REG_CONTROL3_PM_DSM (1 << 5) /* direct switching mode */
25 #define REG_CONTROL3_PM_MASK 0xe0
26 #define REG_CONTROL3_BLF (1 << 2) /* battery low bit, read-only */
27 
28 #define REG_SECONDS  0x03
29 #define REG_SECONDS_OS (1 << 7)
30 
31 #define REG_MINUTES  0x04
32 #define REG_HOURS    0x05
33 #define REG_DAYS     0x06
34 #define REG_WEEKDAYS 0x07
35 #define REG_MONTHS   0x08
36 #define REG_YEARS    0x09
37 
38 #define REG_OFFSET   0x0e
39 #define REG_OFFSET_MODE BIT(7)
40 
41 struct pcf8523 {
42 	struct rtc_device *rtc;
43 };
44 
45 static int pcf8523_read(struct i2c_client *client, u8 reg, u8 *valuep)
46 {
47 	struct i2c_msg msgs[2];
48 	u8 value = 0;
49 	int err;
50 
51 	msgs[0].addr = client->addr;
52 	msgs[0].flags = 0;
53 	msgs[0].len = sizeof(reg);
54 	msgs[0].buf = &reg;
55 
56 	msgs[1].addr = client->addr;
57 	msgs[1].flags = I2C_M_RD;
58 	msgs[1].len = sizeof(value);
59 	msgs[1].buf = &value;
60 
61 	err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
62 	if (err < 0)
63 		return err;
64 
65 	*valuep = value;
66 
67 	return 0;
68 }
69 
70 static int pcf8523_write(struct i2c_client *client, u8 reg, u8 value)
71 {
72 	u8 buffer[2] = { reg, value };
73 	struct i2c_msg msg;
74 	int err;
75 
76 	msg.addr = client->addr;
77 	msg.flags = 0;
78 	msg.len = sizeof(buffer);
79 	msg.buf = buffer;
80 
81 	err = i2c_transfer(client->adapter, &msg, 1);
82 	if (err < 0)
83 		return err;
84 
85 	return 0;
86 }
87 
88 static int pcf8523_select_capacitance(struct i2c_client *client, bool high)
89 {
90 	u8 value;
91 	int err;
92 
93 	err = pcf8523_read(client, REG_CONTROL1, &value);
94 	if (err < 0)
95 		return err;
96 
97 	if (!high)
98 		value &= ~REG_CONTROL1_CAP_SEL;
99 	else
100 		value |= REG_CONTROL1_CAP_SEL;
101 
102 	err = pcf8523_write(client, REG_CONTROL1, value);
103 	if (err < 0)
104 		return err;
105 
106 	return err;
107 }
108 
109 static int pcf8523_set_pm(struct i2c_client *client, u8 pm)
110 {
111 	u8 value;
112 	int err;
113 
114 	err = pcf8523_read(client, REG_CONTROL3, &value);
115 	if (err < 0)
116 		return err;
117 
118 	value = (value & ~REG_CONTROL3_PM_MASK) | pm;
119 
120 	err = pcf8523_write(client, REG_CONTROL3, value);
121 	if (err < 0)
122 		return err;
123 
124 	return 0;
125 }
126 
127 static int pcf8523_stop_rtc(struct i2c_client *client)
128 {
129 	u8 value;
130 	int err;
131 
132 	err = pcf8523_read(client, REG_CONTROL1, &value);
133 	if (err < 0)
134 		return err;
135 
136 	value |= REG_CONTROL1_STOP;
137 
138 	err = pcf8523_write(client, REG_CONTROL1, value);
139 	if (err < 0)
140 		return err;
141 
142 	return 0;
143 }
144 
145 static int pcf8523_start_rtc(struct i2c_client *client)
146 {
147 	u8 value;
148 	int err;
149 
150 	err = pcf8523_read(client, REG_CONTROL1, &value);
151 	if (err < 0)
152 		return err;
153 
154 	value &= ~REG_CONTROL1_STOP;
155 
156 	err = pcf8523_write(client, REG_CONTROL1, value);
157 	if (err < 0)
158 		return err;
159 
160 	return 0;
161 }
162 
163 static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm)
164 {
165 	struct i2c_client *client = to_i2c_client(dev);
166 	u8 start = REG_SECONDS, regs[7];
167 	struct i2c_msg msgs[2];
168 	int err;
169 
170 	msgs[0].addr = client->addr;
171 	msgs[0].flags = 0;
172 	msgs[0].len = 1;
173 	msgs[0].buf = &start;
174 
175 	msgs[1].addr = client->addr;
176 	msgs[1].flags = I2C_M_RD;
177 	msgs[1].len = sizeof(regs);
178 	msgs[1].buf = regs;
179 
180 	err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
181 	if (err < 0)
182 		return err;
183 
184 	if (regs[0] & REG_SECONDS_OS)
185 		return -EINVAL;
186 
187 	tm->tm_sec = bcd2bin(regs[0] & 0x7f);
188 	tm->tm_min = bcd2bin(regs[1] & 0x7f);
189 	tm->tm_hour = bcd2bin(regs[2] & 0x3f);
190 	tm->tm_mday = bcd2bin(regs[3] & 0x3f);
191 	tm->tm_wday = regs[4] & 0x7;
192 	tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1;
193 	tm->tm_year = bcd2bin(regs[6]) + 100;
194 
195 	return rtc_valid_tm(tm);
196 }
197 
198 static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
199 {
200 	struct i2c_client *client = to_i2c_client(dev);
201 	struct i2c_msg msg;
202 	u8 regs[8];
203 	int err;
204 
205 	/*
206 	 * The hardware can only store values between 0 and 99 in it's YEAR
207 	 * register (with 99 overflowing to 0 on increment).
208 	 * After 2100-02-28 we could start interpreting the year to be in the
209 	 * interval [2100, 2199], but there is no path to switch in a smooth way
210 	 * because the chip handles YEAR=0x00 (and the out-of-spec
211 	 * YEAR=0xa0) as a leap year, but 2100 isn't.
212 	 */
213 	if (tm->tm_year < 100 || tm->tm_year >= 200)
214 		return -EINVAL;
215 
216 	err = pcf8523_stop_rtc(client);
217 	if (err < 0)
218 		return err;
219 
220 	regs[0] = REG_SECONDS;
221 	/* This will purposely overwrite REG_SECONDS_OS */
222 	regs[1] = bin2bcd(tm->tm_sec);
223 	regs[2] = bin2bcd(tm->tm_min);
224 	regs[3] = bin2bcd(tm->tm_hour);
225 	regs[4] = bin2bcd(tm->tm_mday);
226 	regs[5] = tm->tm_wday;
227 	regs[6] = bin2bcd(tm->tm_mon + 1);
228 	regs[7] = bin2bcd(tm->tm_year - 100);
229 
230 	msg.addr = client->addr;
231 	msg.flags = 0;
232 	msg.len = sizeof(regs);
233 	msg.buf = regs;
234 
235 	err = i2c_transfer(client->adapter, &msg, 1);
236 	if (err < 0) {
237 		/*
238 		 * If the time cannot be set, restart the RTC anyway. Note
239 		 * that errors are ignored if the RTC cannot be started so
240 		 * that we have a chance to propagate the original error.
241 		 */
242 		pcf8523_start_rtc(client);
243 		return err;
244 	}
245 
246 	return pcf8523_start_rtc(client);
247 }
248 
249 #ifdef CONFIG_RTC_INTF_DEV
250 static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd,
251 			     unsigned long arg)
252 {
253 	struct i2c_client *client = to_i2c_client(dev);
254 	u8 value;
255 	int ret = 0, err;
256 
257 	switch (cmd) {
258 	case RTC_VL_READ:
259 		err = pcf8523_read(client, REG_CONTROL3, &value);
260 		if (err < 0)
261 			return err;
262 
263 		if (value & REG_CONTROL3_BLF)
264 			ret = 1;
265 
266 		if (copy_to_user((void __user *)arg, &ret, sizeof(int)))
267 			return -EFAULT;
268 
269 		return 0;
270 	default:
271 		return -ENOIOCTLCMD;
272 	}
273 }
274 #else
275 #define pcf8523_rtc_ioctl NULL
276 #endif
277 
278 static int pcf8523_rtc_read_offset(struct device *dev, long *offset)
279 {
280 	struct i2c_client *client = to_i2c_client(dev);
281 	int err;
282 	u8 value;
283 	s8 val;
284 
285 	err = pcf8523_read(client, REG_OFFSET, &value);
286 	if (err < 0)
287 		return err;
288 
289 	/* sign extend the 7-bit offset value */
290 	val = value << 1;
291 	*offset = (value & REG_OFFSET_MODE ? 4069 : 4340) * (val >> 1);
292 
293 	return 0;
294 }
295 
296 static int pcf8523_rtc_set_offset(struct device *dev, long offset)
297 {
298 	struct i2c_client *client = to_i2c_client(dev);
299 	long reg_m0, reg_m1;
300 	u8 value;
301 
302 	reg_m0 = clamp(DIV_ROUND_CLOSEST(offset, 4340), -64L, 63L);
303 	reg_m1 = clamp(DIV_ROUND_CLOSEST(offset, 4069), -64L, 63L);
304 
305 	if (abs(reg_m0 * 4340 - offset) < abs(reg_m1 * 4069 - offset))
306 		value = reg_m0 & 0x7f;
307 	else
308 		value = (reg_m1 & 0x7f) | REG_OFFSET_MODE;
309 
310 	return pcf8523_write(client, REG_OFFSET, value);
311 }
312 
313 static const struct rtc_class_ops pcf8523_rtc_ops = {
314 	.read_time = pcf8523_rtc_read_time,
315 	.set_time = pcf8523_rtc_set_time,
316 	.ioctl = pcf8523_rtc_ioctl,
317 	.read_offset = pcf8523_rtc_read_offset,
318 	.set_offset = pcf8523_rtc_set_offset,
319 };
320 
321 static int pcf8523_probe(struct i2c_client *client,
322 			 const struct i2c_device_id *id)
323 {
324 	struct pcf8523 *pcf;
325 	int err;
326 
327 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
328 		return -ENODEV;
329 
330 	pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
331 	if (!pcf)
332 		return -ENOMEM;
333 
334 	err = pcf8523_select_capacitance(client, true);
335 	if (err < 0)
336 		return err;
337 
338 	err = pcf8523_set_pm(client, 0);
339 	if (err < 0)
340 		return err;
341 
342 	pcf->rtc = devm_rtc_device_register(&client->dev, DRIVER_NAME,
343 				       &pcf8523_rtc_ops, THIS_MODULE);
344 	if (IS_ERR(pcf->rtc))
345 		return PTR_ERR(pcf->rtc);
346 
347 	i2c_set_clientdata(client, pcf);
348 
349 	return 0;
350 }
351 
352 static const struct i2c_device_id pcf8523_id[] = {
353 	{ "pcf8523", 0 },
354 	{ }
355 };
356 MODULE_DEVICE_TABLE(i2c, pcf8523_id);
357 
358 #ifdef CONFIG_OF
359 static const struct of_device_id pcf8523_of_match[] = {
360 	{ .compatible = "nxp,pcf8523" },
361 	{ }
362 };
363 MODULE_DEVICE_TABLE(of, pcf8523_of_match);
364 #endif
365 
366 static struct i2c_driver pcf8523_driver = {
367 	.driver = {
368 		.name = DRIVER_NAME,
369 		.of_match_table = of_match_ptr(pcf8523_of_match),
370 	},
371 	.probe = pcf8523_probe,
372 	.id_table = pcf8523_id,
373 };
374 module_i2c_driver(pcf8523_driver);
375 
376 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
377 MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
378 MODULE_LICENSE("GPL v2");
379