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 
pcf8523_read(struct i2c_client * client,u8 reg,u8 * valuep)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 
pcf8523_write(struct i2c_client * client,u8 reg,u8 value)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 
pcf8523_voltage_low(struct i2c_client * client)88 static int pcf8523_voltage_low(struct i2c_client *client)
89 {
90 	u8 value;
91 	int err;
92 
93 	err = pcf8523_read(client, REG_CONTROL3, &value);
94 	if (err < 0)
95 		return err;
96 
97 	return !!(value & REG_CONTROL3_BLF);
98 }
99 
pcf8523_load_capacitance(struct i2c_client * client)100 static int pcf8523_load_capacitance(struct i2c_client *client)
101 {
102 	u32 load;
103 	u8 value;
104 	int err;
105 
106 	err = pcf8523_read(client, REG_CONTROL1, &value);
107 	if (err < 0)
108 		return err;
109 
110 	load = 12500;
111 	of_property_read_u32(client->dev.of_node, "quartz-load-femtofarads",
112 			     &load);
113 
114 	switch (load) {
115 	default:
116 		dev_warn(&client->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 12500",
117 			 load);
118 		/* fall through */
119 	case 12500:
120 		value |= REG_CONTROL1_CAP_SEL;
121 		break;
122 	case 7000:
123 		value &= ~REG_CONTROL1_CAP_SEL;
124 		break;
125 	}
126 
127 	err = pcf8523_write(client, REG_CONTROL1, value);
128 
129 	return err;
130 }
131 
pcf8523_set_pm(struct i2c_client * client,u8 pm)132 static int pcf8523_set_pm(struct i2c_client *client, u8 pm)
133 {
134 	u8 value;
135 	int err;
136 
137 	err = pcf8523_read(client, REG_CONTROL3, &value);
138 	if (err < 0)
139 		return err;
140 
141 	value = (value & ~REG_CONTROL3_PM_MASK) | pm;
142 
143 	err = pcf8523_write(client, REG_CONTROL3, value);
144 	if (err < 0)
145 		return err;
146 
147 	return 0;
148 }
149 
pcf8523_stop_rtc(struct i2c_client * client)150 static int pcf8523_stop_rtc(struct i2c_client *client)
151 {
152 	u8 value;
153 	int err;
154 
155 	err = pcf8523_read(client, REG_CONTROL1, &value);
156 	if (err < 0)
157 		return err;
158 
159 	value |= REG_CONTROL1_STOP;
160 
161 	err = pcf8523_write(client, REG_CONTROL1, value);
162 	if (err < 0)
163 		return err;
164 
165 	return 0;
166 }
167 
pcf8523_start_rtc(struct i2c_client * client)168 static int pcf8523_start_rtc(struct i2c_client *client)
169 {
170 	u8 value;
171 	int err;
172 
173 	err = pcf8523_read(client, REG_CONTROL1, &value);
174 	if (err < 0)
175 		return err;
176 
177 	value &= ~REG_CONTROL1_STOP;
178 
179 	err = pcf8523_write(client, REG_CONTROL1, value);
180 	if (err < 0)
181 		return err;
182 
183 	return 0;
184 }
185 
pcf8523_rtc_read_time(struct device * dev,struct rtc_time * tm)186 static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm)
187 {
188 	struct i2c_client *client = to_i2c_client(dev);
189 	u8 start = REG_SECONDS, regs[7];
190 	struct i2c_msg msgs[2];
191 	int err;
192 
193 	err = pcf8523_voltage_low(client);
194 	if (err < 0) {
195 		return err;
196 	} else if (err > 0) {
197 		dev_err(dev, "low voltage detected, time is unreliable\n");
198 		return -EINVAL;
199 	}
200 
201 	msgs[0].addr = client->addr;
202 	msgs[0].flags = 0;
203 	msgs[0].len = 1;
204 	msgs[0].buf = &start;
205 
206 	msgs[1].addr = client->addr;
207 	msgs[1].flags = I2C_M_RD;
208 	msgs[1].len = sizeof(regs);
209 	msgs[1].buf = regs;
210 
211 	err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
212 	if (err < 0)
213 		return err;
214 
215 	if (regs[0] & REG_SECONDS_OS)
216 		return -EINVAL;
217 
218 	tm->tm_sec = bcd2bin(regs[0] & 0x7f);
219 	tm->tm_min = bcd2bin(regs[1] & 0x7f);
220 	tm->tm_hour = bcd2bin(regs[2] & 0x3f);
221 	tm->tm_mday = bcd2bin(regs[3] & 0x3f);
222 	tm->tm_wday = regs[4] & 0x7;
223 	tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1;
224 	tm->tm_year = bcd2bin(regs[6]) + 100;
225 
226 	return 0;
227 }
228 
pcf8523_rtc_set_time(struct device * dev,struct rtc_time * tm)229 static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
230 {
231 	struct i2c_client *client = to_i2c_client(dev);
232 	struct i2c_msg msg;
233 	u8 regs[8];
234 	int err;
235 
236 	/*
237 	 * The hardware can only store values between 0 and 99 in it's YEAR
238 	 * register (with 99 overflowing to 0 on increment).
239 	 * After 2100-02-28 we could start interpreting the year to be in the
240 	 * interval [2100, 2199], but there is no path to switch in a smooth way
241 	 * because the chip handles YEAR=0x00 (and the out-of-spec
242 	 * YEAR=0xa0) as a leap year, but 2100 isn't.
243 	 */
244 	if (tm->tm_year < 100 || tm->tm_year >= 200)
245 		return -EINVAL;
246 
247 	err = pcf8523_stop_rtc(client);
248 	if (err < 0)
249 		return err;
250 
251 	regs[0] = REG_SECONDS;
252 	/* This will purposely overwrite REG_SECONDS_OS */
253 	regs[1] = bin2bcd(tm->tm_sec);
254 	regs[2] = bin2bcd(tm->tm_min);
255 	regs[3] = bin2bcd(tm->tm_hour);
256 	regs[4] = bin2bcd(tm->tm_mday);
257 	regs[5] = tm->tm_wday;
258 	regs[6] = bin2bcd(tm->tm_mon + 1);
259 	regs[7] = bin2bcd(tm->tm_year - 100);
260 
261 	msg.addr = client->addr;
262 	msg.flags = 0;
263 	msg.len = sizeof(regs);
264 	msg.buf = regs;
265 
266 	err = i2c_transfer(client->adapter, &msg, 1);
267 	if (err < 0) {
268 		/*
269 		 * If the time cannot be set, restart the RTC anyway. Note
270 		 * that errors are ignored if the RTC cannot be started so
271 		 * that we have a chance to propagate the original error.
272 		 */
273 		pcf8523_start_rtc(client);
274 		return err;
275 	}
276 
277 	return pcf8523_start_rtc(client);
278 }
279 
280 #ifdef CONFIG_RTC_INTF_DEV
pcf8523_rtc_ioctl(struct device * dev,unsigned int cmd,unsigned long arg)281 static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd,
282 			     unsigned long arg)
283 {
284 	struct i2c_client *client = to_i2c_client(dev);
285 	int ret;
286 
287 	switch (cmd) {
288 	case RTC_VL_READ:
289 		ret = pcf8523_voltage_low(client);
290 		if (ret < 0)
291 			return ret;
292 
293 		if (copy_to_user((void __user *)arg, &ret, sizeof(int)))
294 			return -EFAULT;
295 
296 		return 0;
297 	default:
298 		return -ENOIOCTLCMD;
299 	}
300 }
301 #else
302 #define pcf8523_rtc_ioctl NULL
303 #endif
304 
pcf8523_rtc_read_offset(struct device * dev,long * offset)305 static int pcf8523_rtc_read_offset(struct device *dev, long *offset)
306 {
307 	struct i2c_client *client = to_i2c_client(dev);
308 	int err;
309 	u8 value;
310 	s8 val;
311 
312 	err = pcf8523_read(client, REG_OFFSET, &value);
313 	if (err < 0)
314 		return err;
315 
316 	/* sign extend the 7-bit offset value */
317 	val = value << 1;
318 	*offset = (value & REG_OFFSET_MODE ? 4069 : 4340) * (val >> 1);
319 
320 	return 0;
321 }
322 
pcf8523_rtc_set_offset(struct device * dev,long offset)323 static int pcf8523_rtc_set_offset(struct device *dev, long offset)
324 {
325 	struct i2c_client *client = to_i2c_client(dev);
326 	long reg_m0, reg_m1;
327 	u8 value;
328 
329 	reg_m0 = clamp(DIV_ROUND_CLOSEST(offset, 4340), -64L, 63L);
330 	reg_m1 = clamp(DIV_ROUND_CLOSEST(offset, 4069), -64L, 63L);
331 
332 	if (abs(reg_m0 * 4340 - offset) < abs(reg_m1 * 4069 - offset))
333 		value = reg_m0 & 0x7f;
334 	else
335 		value = (reg_m1 & 0x7f) | REG_OFFSET_MODE;
336 
337 	return pcf8523_write(client, REG_OFFSET, value);
338 }
339 
340 static const struct rtc_class_ops pcf8523_rtc_ops = {
341 	.read_time = pcf8523_rtc_read_time,
342 	.set_time = pcf8523_rtc_set_time,
343 	.ioctl = pcf8523_rtc_ioctl,
344 	.read_offset = pcf8523_rtc_read_offset,
345 	.set_offset = pcf8523_rtc_set_offset,
346 };
347 
pcf8523_probe(struct i2c_client * client,const struct i2c_device_id * id)348 static int pcf8523_probe(struct i2c_client *client,
349 			 const struct i2c_device_id *id)
350 {
351 	struct pcf8523 *pcf;
352 	int err;
353 
354 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
355 		return -ENODEV;
356 
357 	pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
358 	if (!pcf)
359 		return -ENOMEM;
360 
361 	err = pcf8523_load_capacitance(client);
362 	if (err < 0)
363 		dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
364 			 err);
365 
366 	err = pcf8523_set_pm(client, 0);
367 	if (err < 0)
368 		return err;
369 
370 	pcf->rtc = devm_rtc_device_register(&client->dev, DRIVER_NAME,
371 				       &pcf8523_rtc_ops, THIS_MODULE);
372 	if (IS_ERR(pcf->rtc))
373 		return PTR_ERR(pcf->rtc);
374 
375 	i2c_set_clientdata(client, pcf);
376 
377 	return 0;
378 }
379 
380 static const struct i2c_device_id pcf8523_id[] = {
381 	{ "pcf8523", 0 },
382 	{ }
383 };
384 MODULE_DEVICE_TABLE(i2c, pcf8523_id);
385 
386 #ifdef CONFIG_OF
387 static const struct of_device_id pcf8523_of_match[] = {
388 	{ .compatible = "nxp,pcf8523" },
389 	{ }
390 };
391 MODULE_DEVICE_TABLE(of, pcf8523_of_match);
392 #endif
393 
394 static struct i2c_driver pcf8523_driver = {
395 	.driver = {
396 		.name = DRIVER_NAME,
397 		.of_match_table = of_match_ptr(pcf8523_of_match),
398 	},
399 	.probe = pcf8523_probe,
400 	.id_table = pcf8523_id,
401 };
402 module_i2c_driver(pcf8523_driver);
403 
404 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
405 MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
406 MODULE_LICENSE("GPL v2");
407