xref: /linux/drivers/rtc/rtc-pcf8523.c (revision 87c9c16317882dd6dbbc07e349bc3223e14f3244)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Avionic Design GmbH
4  */
5 
6 #include <linux/bcd.h>
7 #include <linux/i2c.h>
8 #include <linux/module.h>
9 #include <linux/rtc.h>
10 #include <linux/of.h>
11 #include <linux/pm_wakeirq.h>
12 
13 #define REG_CONTROL1 0x00
14 #define REG_CONTROL1_CAP_SEL BIT(7)
15 #define REG_CONTROL1_STOP    BIT(5)
16 #define REG_CONTROL1_AIE    BIT(1)
17 
18 #define REG_CONTROL2 0x01
19 #define REG_CONTROL2_AF BIT(3)
20 
21 #define REG_CONTROL3 0x02
22 #define REG_CONTROL3_PM_BLD BIT(7) /* battery low detection disabled */
23 #define REG_CONTROL3_PM_VDD BIT(6) /* switch-over disabled */
24 #define REG_CONTROL3_PM_DSM BIT(5) /* direct switching mode */
25 #define REG_CONTROL3_PM_MASK 0xe0
26 #define REG_CONTROL3_BLF BIT(2) /* battery low bit, read-only */
27 
28 #define REG_SECONDS  0x03
29 #define REG_SECONDS_OS BIT(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_MINUTE_ALARM	0x0a
39 #define REG_HOUR_ALARM		0x0b
40 #define REG_DAY_ALARM		0x0c
41 #define REG_WEEKDAY_ALARM	0x0d
42 #define ALARM_DIS BIT(7)
43 
44 #define REG_OFFSET   0x0e
45 #define REG_OFFSET_MODE BIT(7)
46 
47 #define REG_TMR_CLKOUT_CTRL 0x0f
48 
49 struct pcf8523 {
50 	struct rtc_device *rtc;
51 	struct i2c_client *client;
52 };
53 
54 static int pcf8523_read(struct i2c_client *client, u8 reg, u8 *valuep)
55 {
56 	struct i2c_msg msgs[2];
57 	u8 value = 0;
58 	int err;
59 
60 	msgs[0].addr = client->addr;
61 	msgs[0].flags = 0;
62 	msgs[0].len = sizeof(reg);
63 	msgs[0].buf = &reg;
64 
65 	msgs[1].addr = client->addr;
66 	msgs[1].flags = I2C_M_RD;
67 	msgs[1].len = sizeof(value);
68 	msgs[1].buf = &value;
69 
70 	err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
71 	if (err < 0)
72 		return err;
73 
74 	*valuep = value;
75 
76 	return 0;
77 }
78 
79 static int pcf8523_write(struct i2c_client *client, u8 reg, u8 value)
80 {
81 	u8 buffer[2] = { reg, value };
82 	struct i2c_msg msg;
83 	int err;
84 
85 	msg.addr = client->addr;
86 	msg.flags = 0;
87 	msg.len = sizeof(buffer);
88 	msg.buf = buffer;
89 
90 	err = i2c_transfer(client->adapter, &msg, 1);
91 	if (err < 0)
92 		return err;
93 
94 	return 0;
95 }
96 
97 static int pcf8523_voltage_low(struct i2c_client *client)
98 {
99 	u8 value;
100 	int err;
101 
102 	err = pcf8523_read(client, REG_CONTROL3, &value);
103 	if (err < 0)
104 		return err;
105 
106 	return !!(value & REG_CONTROL3_BLF);
107 }
108 
109 static int pcf8523_load_capacitance(struct i2c_client *client)
110 {
111 	u32 load;
112 	u8 value;
113 	int err;
114 
115 	err = pcf8523_read(client, REG_CONTROL1, &value);
116 	if (err < 0)
117 		return err;
118 
119 	load = 12500;
120 	of_property_read_u32(client->dev.of_node, "quartz-load-femtofarads",
121 			     &load);
122 
123 	switch (load) {
124 	default:
125 		dev_warn(&client->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 12500",
126 			 load);
127 		fallthrough;
128 	case 12500:
129 		value |= REG_CONTROL1_CAP_SEL;
130 		break;
131 	case 7000:
132 		value &= ~REG_CONTROL1_CAP_SEL;
133 		break;
134 	}
135 
136 	err = pcf8523_write(client, REG_CONTROL1, value);
137 
138 	return err;
139 }
140 
141 static int pcf8523_set_pm(struct i2c_client *client, u8 pm)
142 {
143 	u8 value;
144 	int err;
145 
146 	err = pcf8523_read(client, REG_CONTROL3, &value);
147 	if (err < 0)
148 		return err;
149 
150 	value = (value & ~REG_CONTROL3_PM_MASK) | pm;
151 
152 	err = pcf8523_write(client, REG_CONTROL3, value);
153 	if (err < 0)
154 		return err;
155 
156 	return 0;
157 }
158 
159 static irqreturn_t pcf8523_irq(int irq, void *dev_id)
160 {
161 	struct pcf8523 *pcf8523 = i2c_get_clientdata(dev_id);
162 	u8 value;
163 	int err;
164 
165 	err = pcf8523_read(pcf8523->client, REG_CONTROL2, &value);
166 	if (err < 0)
167 		return IRQ_HANDLED;
168 
169 	if (value & REG_CONTROL2_AF) {
170 		value &= ~REG_CONTROL2_AF;
171 		pcf8523_write(pcf8523->client, REG_CONTROL2, value);
172 		rtc_update_irq(pcf8523->rtc, 1, RTC_IRQF | RTC_AF);
173 
174 		return IRQ_HANDLED;
175 	}
176 
177 	return IRQ_NONE;
178 }
179 
180 static int pcf8523_stop_rtc(struct i2c_client *client)
181 {
182 	u8 value;
183 	int err;
184 
185 	err = pcf8523_read(client, REG_CONTROL1, &value);
186 	if (err < 0)
187 		return err;
188 
189 	value |= REG_CONTROL1_STOP;
190 
191 	err = pcf8523_write(client, REG_CONTROL1, value);
192 	if (err < 0)
193 		return err;
194 
195 	return 0;
196 }
197 
198 static int pcf8523_start_rtc(struct i2c_client *client)
199 {
200 	u8 value;
201 	int err;
202 
203 	err = pcf8523_read(client, REG_CONTROL1, &value);
204 	if (err < 0)
205 		return err;
206 
207 	value &= ~REG_CONTROL1_STOP;
208 
209 	err = pcf8523_write(client, REG_CONTROL1, value);
210 	if (err < 0)
211 		return err;
212 
213 	return 0;
214 }
215 
216 static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm)
217 {
218 	struct i2c_client *client = to_i2c_client(dev);
219 	u8 start = REG_SECONDS, regs[7];
220 	struct i2c_msg msgs[2];
221 	int err;
222 
223 	err = pcf8523_voltage_low(client);
224 	if (err < 0) {
225 		return err;
226 	} else if (err > 0) {
227 		dev_err(dev, "low voltage detected, time is unreliable\n");
228 		return -EINVAL;
229 	}
230 
231 	msgs[0].addr = client->addr;
232 	msgs[0].flags = 0;
233 	msgs[0].len = 1;
234 	msgs[0].buf = &start;
235 
236 	msgs[1].addr = client->addr;
237 	msgs[1].flags = I2C_M_RD;
238 	msgs[1].len = sizeof(regs);
239 	msgs[1].buf = regs;
240 
241 	err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
242 	if (err < 0)
243 		return err;
244 
245 	if (regs[0] & REG_SECONDS_OS)
246 		return -EINVAL;
247 
248 	tm->tm_sec = bcd2bin(regs[0] & 0x7f);
249 	tm->tm_min = bcd2bin(regs[1] & 0x7f);
250 	tm->tm_hour = bcd2bin(regs[2] & 0x3f);
251 	tm->tm_mday = bcd2bin(regs[3] & 0x3f);
252 	tm->tm_wday = regs[4] & 0x7;
253 	tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1;
254 	tm->tm_year = bcd2bin(regs[6]) + 100;
255 
256 	return 0;
257 }
258 
259 static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
260 {
261 	struct i2c_client *client = to_i2c_client(dev);
262 	struct i2c_msg msg;
263 	u8 regs[8];
264 	int err;
265 
266 	err = pcf8523_stop_rtc(client);
267 	if (err < 0)
268 		return err;
269 
270 	regs[0] = REG_SECONDS;
271 	/* This will purposely overwrite REG_SECONDS_OS */
272 	regs[1] = bin2bcd(tm->tm_sec);
273 	regs[2] = bin2bcd(tm->tm_min);
274 	regs[3] = bin2bcd(tm->tm_hour);
275 	regs[4] = bin2bcd(tm->tm_mday);
276 	regs[5] = tm->tm_wday;
277 	regs[6] = bin2bcd(tm->tm_mon + 1);
278 	regs[7] = bin2bcd(tm->tm_year - 100);
279 
280 	msg.addr = client->addr;
281 	msg.flags = 0;
282 	msg.len = sizeof(regs);
283 	msg.buf = regs;
284 
285 	err = i2c_transfer(client->adapter, &msg, 1);
286 	if (err < 0) {
287 		/*
288 		 * If the time cannot be set, restart the RTC anyway. Note
289 		 * that errors are ignored if the RTC cannot be started so
290 		 * that we have a chance to propagate the original error.
291 		 */
292 		pcf8523_start_rtc(client);
293 		return err;
294 	}
295 
296 	return pcf8523_start_rtc(client);
297 }
298 
299 static int pcf8523_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
300 {
301 	struct i2c_client *client = to_i2c_client(dev);
302 	u8 start = REG_MINUTE_ALARM, regs[4];
303 	struct i2c_msg msgs[2];
304 	u8 value;
305 	int err;
306 
307 	msgs[0].addr = client->addr;
308 	msgs[0].flags = 0;
309 	msgs[0].len = 1;
310 	msgs[0].buf = &start;
311 
312 	msgs[1].addr = client->addr;
313 	msgs[1].flags = I2C_M_RD;
314 	msgs[1].len = sizeof(regs);
315 	msgs[1].buf = regs;
316 
317 	err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
318 	if (err < 0)
319 		return err;
320 
321 	tm->time.tm_sec = 0;
322 	tm->time.tm_min = bcd2bin(regs[0] & 0x7F);
323 	tm->time.tm_hour = bcd2bin(regs[1] & 0x3F);
324 	tm->time.tm_mday = bcd2bin(regs[2] & 0x3F);
325 	tm->time.tm_wday = bcd2bin(regs[3] & 0x7);
326 
327 	err = pcf8523_read(client, REG_CONTROL1, &value);
328 	if (err < 0)
329 		return err;
330 	tm->enabled = !!(value & REG_CONTROL1_AIE);
331 
332 	err = pcf8523_read(client, REG_CONTROL2, &value);
333 	if (err < 0)
334 		return err;
335 	tm->pending = !!(value & REG_CONTROL2_AF);
336 
337 	return 0;
338 }
339 
340 static int pcf8523_irq_enable(struct device *dev, unsigned int enabled)
341 {
342 	struct i2c_client *client = to_i2c_client(dev);
343 	u8 value;
344 	int err;
345 
346 	err = pcf8523_read(client, REG_CONTROL1, &value);
347 	if (err < 0)
348 		return err;
349 
350 	value &= REG_CONTROL1_AIE;
351 
352 	if (enabled)
353 		value |= REG_CONTROL1_AIE;
354 
355 	err = pcf8523_write(client, REG_CONTROL1, value);
356 	if (err < 0)
357 		return err;
358 
359 	return 0;
360 }
361 
362 static int pcf8523_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
363 {
364 	struct i2c_client *client = to_i2c_client(dev);
365 	struct i2c_msg msg;
366 	u8 regs[5];
367 	int err;
368 
369 	err = pcf8523_irq_enable(dev, 0);
370 	if (err)
371 		return err;
372 
373 	err = pcf8523_write(client, REG_CONTROL2, 0);
374 	if (err < 0)
375 		return err;
376 
377 	/* The alarm has no seconds, round up to nearest minute */
378 	if (tm->time.tm_sec) {
379 		time64_t alarm_time = rtc_tm_to_time64(&tm->time);
380 
381 		alarm_time += 60 - tm->time.tm_sec;
382 		rtc_time64_to_tm(alarm_time, &tm->time);
383 	}
384 
385 	regs[0] = REG_MINUTE_ALARM;
386 	regs[1] = bin2bcd(tm->time.tm_min);
387 	regs[2] = bin2bcd(tm->time.tm_hour);
388 	regs[3] = bin2bcd(tm->time.tm_mday);
389 	regs[4] = ALARM_DIS;
390 	msg.addr = client->addr;
391 	msg.flags = 0;
392 	msg.len = sizeof(regs);
393 	msg.buf = regs;
394 	err = i2c_transfer(client->adapter, &msg, 1);
395 	if (err < 0)
396 		return err;
397 
398 	if (tm->enabled)
399 		return pcf8523_irq_enable(dev, tm->enabled);
400 
401 	return 0;
402 }
403 
404 #ifdef CONFIG_RTC_INTF_DEV
405 static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd,
406 			     unsigned long arg)
407 {
408 	struct i2c_client *client = to_i2c_client(dev);
409 	unsigned int flags = 0;
410 	u8 value;
411 	int ret;
412 
413 	switch (cmd) {
414 	case RTC_VL_READ:
415 		ret = pcf8523_voltage_low(client);
416 		if (ret < 0)
417 			return ret;
418 		if (ret)
419 			flags |= RTC_VL_BACKUP_LOW;
420 
421 		ret = pcf8523_read(client, REG_SECONDS, &value);
422 		if (ret < 0)
423 			return ret;
424 
425 		if (value & REG_SECONDS_OS)
426 			flags |= RTC_VL_DATA_INVALID;
427 
428 		return put_user(flags, (unsigned int __user *)arg);
429 
430 	default:
431 		return -ENOIOCTLCMD;
432 	}
433 }
434 #else
435 #define pcf8523_rtc_ioctl NULL
436 #endif
437 
438 static int pcf8523_rtc_read_offset(struct device *dev, long *offset)
439 {
440 	struct i2c_client *client = to_i2c_client(dev);
441 	int err;
442 	u8 value;
443 	s8 val;
444 
445 	err = pcf8523_read(client, REG_OFFSET, &value);
446 	if (err < 0)
447 		return err;
448 
449 	/* sign extend the 7-bit offset value */
450 	val = value << 1;
451 	*offset = (value & REG_OFFSET_MODE ? 4069 : 4340) * (val >> 1);
452 
453 	return 0;
454 }
455 
456 static int pcf8523_rtc_set_offset(struct device *dev, long offset)
457 {
458 	struct i2c_client *client = to_i2c_client(dev);
459 	long reg_m0, reg_m1;
460 	u8 value;
461 
462 	reg_m0 = clamp(DIV_ROUND_CLOSEST(offset, 4340), -64L, 63L);
463 	reg_m1 = clamp(DIV_ROUND_CLOSEST(offset, 4069), -64L, 63L);
464 
465 	if (abs(reg_m0 * 4340 - offset) < abs(reg_m1 * 4069 - offset))
466 		value = reg_m0 & 0x7f;
467 	else
468 		value = (reg_m1 & 0x7f) | REG_OFFSET_MODE;
469 
470 	return pcf8523_write(client, REG_OFFSET, value);
471 }
472 
473 static const struct rtc_class_ops pcf8523_rtc_ops = {
474 	.read_time = pcf8523_rtc_read_time,
475 	.set_time = pcf8523_rtc_set_time,
476 	.read_alarm = pcf8523_rtc_read_alarm,
477 	.set_alarm = pcf8523_rtc_set_alarm,
478 	.alarm_irq_enable = pcf8523_irq_enable,
479 	.ioctl = pcf8523_rtc_ioctl,
480 	.read_offset = pcf8523_rtc_read_offset,
481 	.set_offset = pcf8523_rtc_set_offset,
482 };
483 
484 static int pcf8523_probe(struct i2c_client *client,
485 			 const struct i2c_device_id *id)
486 {
487 	struct pcf8523 *pcf8523;
488 	struct rtc_device *rtc;
489 	bool wakeup_source = false;
490 	int err;
491 
492 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
493 		return -ENODEV;
494 
495 	pcf8523 = devm_kzalloc(&client->dev, sizeof(struct pcf8523), GFP_KERNEL);
496 	if (!pcf8523)
497 		return -ENOMEM;
498 
499 	i2c_set_clientdata(client, pcf8523);
500 	pcf8523->client = client;
501 
502 	err = pcf8523_load_capacitance(client);
503 	if (err < 0)
504 		dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
505 			 err);
506 
507 	err = pcf8523_set_pm(client, 0);
508 	if (err < 0)
509 		return err;
510 
511 	rtc = devm_rtc_allocate_device(&client->dev);
512 	if (IS_ERR(rtc))
513 		return PTR_ERR(rtc);
514 
515 	pcf8523->rtc = rtc;
516 	rtc->ops = &pcf8523_rtc_ops;
517 	rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
518 	rtc->range_max = RTC_TIMESTAMP_END_2099;
519 	rtc->uie_unsupported = 1;
520 
521 	if (client->irq > 0) {
522 		err = pcf8523_write(client, REG_TMR_CLKOUT_CTRL, 0x38);
523 		if (err < 0)
524 			return err;
525 
526 		err = devm_request_threaded_irq(&client->dev, client->irq,
527 						NULL, pcf8523_irq,
528 						IRQF_SHARED | IRQF_ONESHOT | IRQF_TRIGGER_LOW,
529 						dev_name(&rtc->dev), client);
530 		if (err)
531 			return err;
532 
533 		dev_pm_set_wake_irq(&client->dev, client->irq);
534 	}
535 
536 #ifdef CONFIG_OF
537 	wakeup_source = of_property_read_bool(client->dev.of_node, "wakeup-source");
538 #endif
539 	if (client->irq > 0 || wakeup_source)
540 		device_init_wakeup(&client->dev, true);
541 
542 	return devm_rtc_register_device(rtc);
543 }
544 
545 static const struct i2c_device_id pcf8523_id[] = {
546 	{ "pcf8523", 0 },
547 	{ }
548 };
549 MODULE_DEVICE_TABLE(i2c, pcf8523_id);
550 
551 #ifdef CONFIG_OF
552 static const struct of_device_id pcf8523_of_match[] = {
553 	{ .compatible = "nxp,pcf8523" },
554 	{ .compatible = "microcrystal,rv8523" },
555 	{ }
556 };
557 MODULE_DEVICE_TABLE(of, pcf8523_of_match);
558 #endif
559 
560 static struct i2c_driver pcf8523_driver = {
561 	.driver = {
562 		.name = "rtc-pcf8523",
563 		.of_match_table = of_match_ptr(pcf8523_of_match),
564 	},
565 	.probe = pcf8523_probe,
566 	.id_table = pcf8523_id,
567 };
568 module_i2c_driver(pcf8523_driver);
569 
570 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
571 MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
572 MODULE_LICENSE("GPL v2");
573