xref: /linux/drivers/rtc/rtc-ds1374.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * RTC client/driver for the Maxim/Dallas DS1374 Real-Time Clock over I2C
4  *
5  * Based on code by Randy Vinson <rvinson@mvista.com>,
6  * which was based on the m41t00.c by Mark Greer <mgreer@mvista.com>.
7  *
8  * Copyright (C) 2014 Rose Technology
9  * Copyright (C) 2006-2007 Freescale Semiconductor
10  * Copyright (c) 2005 MontaVista Software, Inc.
11  */
12 /*
13  * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
14  * recommended in .../Documentation/i2c/writing-clients.rst section
15  * "Sending and receiving", using SMBus level communication is preferred.
16  */
17 
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/i2c.h>
24 #include <linux/rtc.h>
25 #include <linux/bcd.h>
26 #include <linux/workqueue.h>
27 #include <linux/slab.h>
28 #include <linux/pm.h>
29 #include <linux/stringify.h>
30 #ifdef CONFIG_RTC_DRV_DS1374_WDT
31 #include <linux/fs.h>
32 #include <linux/ioctl.h>
33 #include <linux/miscdevice.h>
34 #include <linux/reboot.h>
35 #include <linux/watchdog.h>
36 #endif
37 
38 #define DS1374_REG_TOD0		0x00 /* Time of Day */
39 #define DS1374_REG_TOD1		0x01
40 #define DS1374_REG_TOD2		0x02
41 #define DS1374_REG_TOD3		0x03
42 #define DS1374_REG_WDALM0	0x04 /* Watchdog/Alarm */
43 #define DS1374_REG_WDALM1	0x05
44 #define DS1374_REG_WDALM2	0x06
45 #define DS1374_REG_CR		0x07 /* Control */
46 #define DS1374_REG_CR_AIE	0x01 /* Alarm Int. Enable */
47 #define DS1374_REG_CR_WDSTR	0x08 /* 1=INT, 0=RST */
48 #define DS1374_REG_CR_WDALM	0x20 /* 1=Watchdog, 0=Alarm */
49 #define DS1374_REG_CR_WACE	0x40 /* WD/Alarm counter enable */
50 #define DS1374_REG_SR		0x08 /* Status */
51 #define DS1374_REG_SR_OSF	0x80 /* Oscillator Stop Flag */
52 #define DS1374_REG_SR_AF	0x01 /* Alarm Flag */
53 #define DS1374_REG_TCR		0x09 /* Trickle Charge */
54 
55 static const struct i2c_device_id ds1374_id[] = {
56 	{ .name = "ds1374" },
57 	{ }
58 };
59 MODULE_DEVICE_TABLE(i2c, ds1374_id);
60 
61 #ifdef CONFIG_OF
62 static const struct of_device_id ds1374_of_match[] = {
63 	{ .compatible = "dallas,ds1374" },
64 	{ }
65 };
66 MODULE_DEVICE_TABLE(of, ds1374_of_match);
67 #endif
68 
69 struct ds1374 {
70 	struct i2c_client *client;
71 	struct rtc_device *rtc;
72 	struct work_struct work;
73 #ifdef CONFIG_RTC_DRV_DS1374_WDT
74 	struct watchdog_device wdt;
75 #endif
76 	/* The mutex protects alarm operations, and prevents a race
77 	 * between the enable_irq() in the workqueue and the free_irq()
78 	 * in the remove function.
79 	 */
80 	struct mutex mutex;
81 	int exiting;
82 };
83 
84 static struct i2c_driver ds1374_driver;
85 
86 static int ds1374_read_rtc(struct i2c_client *client, u32 *time,
87 			   int reg, int nbytes)
88 {
89 	u8 buf[4];
90 	int ret;
91 	int i;
92 
93 	if (WARN_ON(nbytes > 4))
94 		return -EINVAL;
95 
96 	ret = i2c_smbus_read_i2c_block_data(client, reg, nbytes, buf);
97 
98 	if (ret < 0)
99 		return ret;
100 	if (ret < nbytes)
101 		return -EIO;
102 
103 	for (i = nbytes - 1, *time = 0; i >= 0; i--)
104 		*time = (*time << 8) | buf[i];
105 
106 	return 0;
107 }
108 
109 static int ds1374_write_rtc(struct i2c_client *client, u32 time,
110 			    int reg, int nbytes)
111 {
112 	u8 buf[4];
113 	int i;
114 
115 	if (nbytes > 4) {
116 		WARN_ON(1);
117 		return -EINVAL;
118 	}
119 
120 	for (i = 0; i < nbytes; i++) {
121 		buf[i] = time & 0xff;
122 		time >>= 8;
123 	}
124 
125 	return i2c_smbus_write_i2c_block_data(client, reg, nbytes, buf);
126 }
127 
128 static int ds1374_check_rtc_status(struct i2c_client *client)
129 {
130 	int ret = 0;
131 	int control, stat;
132 
133 	stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
134 	if (stat < 0)
135 		return stat;
136 
137 	if (stat & DS1374_REG_SR_OSF)
138 		dev_warn(&client->dev,
139 			 "oscillator discontinuity flagged, time unreliable\n");
140 
141 	stat &= ~(DS1374_REG_SR_OSF | DS1374_REG_SR_AF);
142 
143 	ret = i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
144 	if (ret < 0)
145 		return ret;
146 
147 	/* If the alarm is pending, clear it before requesting
148 	 * the interrupt, so an interrupt event isn't reported
149 	 * before everything is initialized.
150 	 */
151 
152 	control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
153 	if (control < 0)
154 		return control;
155 
156 	control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
157 	return i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
158 }
159 
160 static int ds1374_read_time(struct device *dev, struct rtc_time *time)
161 {
162 	struct i2c_client *client = to_i2c_client(dev);
163 	u32 itime;
164 	int ret;
165 
166 	ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4);
167 	if (!ret)
168 		rtc_time64_to_tm(itime, time);
169 
170 	return ret;
171 }
172 
173 static int ds1374_set_time(struct device *dev, struct rtc_time *time)
174 {
175 	struct i2c_client *client = to_i2c_client(dev);
176 	unsigned long itime = rtc_tm_to_time64(time);
177 
178 	return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
179 }
180 
181 #ifndef CONFIG_RTC_DRV_DS1374_WDT
182 /* The ds1374 has a decrementer for an alarm, rather than a comparator.
183  * If the time of day is changed, then the alarm will need to be
184  * reset.
185  */
186 static int ds1374_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
187 {
188 	struct i2c_client *client = to_i2c_client(dev);
189 	struct ds1374 *ds1374 = i2c_get_clientdata(client);
190 	u32 now, cur_alarm;
191 	int cr, sr;
192 	int ret = 0;
193 
194 	if (client->irq <= 0)
195 		return -EINVAL;
196 
197 	mutex_lock(&ds1374->mutex);
198 
199 	cr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
200 	if (ret < 0)
201 		goto out;
202 
203 	sr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
204 	if (ret < 0)
205 		goto out;
206 
207 	ret = ds1374_read_rtc(client, &now, DS1374_REG_TOD0, 4);
208 	if (ret)
209 		goto out;
210 
211 	ret = ds1374_read_rtc(client, &cur_alarm, DS1374_REG_WDALM0, 3);
212 	if (ret)
213 		goto out;
214 
215 	rtc_time64_to_tm(now + cur_alarm, &alarm->time);
216 	alarm->enabled = !!(cr & DS1374_REG_CR_WACE);
217 	alarm->pending = !!(sr & DS1374_REG_SR_AF);
218 
219 out:
220 	mutex_unlock(&ds1374->mutex);
221 	return ret;
222 }
223 
224 static int ds1374_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
225 {
226 	struct i2c_client *client = to_i2c_client(dev);
227 	struct ds1374 *ds1374 = i2c_get_clientdata(client);
228 	struct rtc_time now;
229 	unsigned long new_alarm, itime;
230 	int cr;
231 	int ret = 0;
232 
233 	if (client->irq <= 0)
234 		return -EINVAL;
235 
236 	ret = ds1374_read_time(dev, &now);
237 	if (ret < 0)
238 		return ret;
239 
240 	new_alarm = rtc_tm_to_time64(&alarm->time);
241 	itime = rtc_tm_to_time64(&now);
242 
243 	/* This can happen due to races, in addition to dates that are
244 	 * truly in the past.  To avoid requiring the caller to check for
245 	 * races, dates in the past are assumed to be in the recent past
246 	 * (i.e. not something that we'd rather the caller know about via
247 	 * an error), and the alarm is set to go off as soon as possible.
248 	 */
249 	if (time_before_eq(new_alarm, itime))
250 		new_alarm = 1;
251 	else
252 		new_alarm -= itime;
253 
254 	mutex_lock(&ds1374->mutex);
255 
256 	ret = cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
257 	if (ret < 0)
258 		goto out;
259 
260 	/* Disable any existing alarm before setting the new one
261 	 * (or lack thereof). */
262 	cr &= ~DS1374_REG_CR_WACE;
263 
264 	ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
265 	if (ret < 0)
266 		goto out;
267 
268 	ret = ds1374_write_rtc(client, new_alarm, DS1374_REG_WDALM0, 3);
269 	if (ret)
270 		goto out;
271 
272 	if (alarm->enabled) {
273 		cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
274 		cr &= ~DS1374_REG_CR_WDALM;
275 
276 		ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
277 	}
278 
279 out:
280 	mutex_unlock(&ds1374->mutex);
281 	return ret;
282 }
283 #endif
284 
285 static irqreturn_t ds1374_irq(int irq, void *dev_id)
286 {
287 	struct i2c_client *client = dev_id;
288 	struct ds1374 *ds1374 = i2c_get_clientdata(client);
289 
290 	disable_irq_nosync(irq);
291 	schedule_work(&ds1374->work);
292 	return IRQ_HANDLED;
293 }
294 
295 static void ds1374_work(struct work_struct *work)
296 {
297 	struct ds1374 *ds1374 = container_of(work, struct ds1374, work);
298 	struct i2c_client *client = ds1374->client;
299 	int stat, control;
300 
301 	mutex_lock(&ds1374->mutex);
302 
303 	stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
304 	if (stat < 0)
305 		goto unlock;
306 
307 	if (stat & DS1374_REG_SR_AF) {
308 		stat &= ~DS1374_REG_SR_AF;
309 		i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
310 
311 		control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
312 		if (control < 0)
313 			goto out;
314 
315 		control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
316 		i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
317 
318 		rtc_update_irq(ds1374->rtc, 1, RTC_AF | RTC_IRQF);
319 	}
320 
321 out:
322 	if (!ds1374->exiting)
323 		enable_irq(client->irq);
324 unlock:
325 	mutex_unlock(&ds1374->mutex);
326 }
327 
328 #ifndef CONFIG_RTC_DRV_DS1374_WDT
329 static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled)
330 {
331 	struct i2c_client *client = to_i2c_client(dev);
332 	struct ds1374 *ds1374 = i2c_get_clientdata(client);
333 	int ret;
334 
335 	mutex_lock(&ds1374->mutex);
336 
337 	ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
338 	if (ret < 0)
339 		goto out;
340 
341 	if (enabled) {
342 		ret |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
343 		ret &= ~DS1374_REG_CR_WDALM;
344 	} else {
345 		ret &= ~DS1374_REG_CR_WACE;
346 	}
347 	ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret);
348 
349 out:
350 	mutex_unlock(&ds1374->mutex);
351 	return ret;
352 }
353 #endif
354 
355 static const struct rtc_class_ops ds1374_rtc_ops = {
356 	.read_time = ds1374_read_time,
357 	.set_time = ds1374_set_time,
358 #ifndef CONFIG_RTC_DRV_DS1374_WDT
359 	.read_alarm = ds1374_read_alarm,
360 	.set_alarm = ds1374_set_alarm,
361 	.alarm_irq_enable = ds1374_alarm_irq_enable,
362 #endif
363 };
364 
365 #ifdef CONFIG_RTC_DRV_DS1374_WDT
366 /*
367  *****************************************************************************
368  *
369  * Watchdog Driver
370  *
371  *****************************************************************************
372  */
373 /* Default margin */
374 #define TIMER_MARGIN_DEFAULT	32
375 #define TIMER_MARGIN_MIN	1
376 #define TIMER_MARGIN_MAX	4095 /* 24-bit value */
377 
378 static int wdt_margin;
379 module_param(wdt_margin, int, 0);
380 MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)");
381 
382 static bool nowayout = WATCHDOG_NOWAYOUT;
383 module_param(nowayout, bool, 0);
384 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default ="
385 		__stringify(WATCHDOG_NOWAYOUT)")");
386 
387 static const struct watchdog_info ds1374_wdt_info = {
388 	.identity       = "DS1374 Watchdog",
389 	.options        = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
390 						WDIOF_MAGICCLOSE,
391 };
392 
393 static int ds1374_wdt_settimeout(struct watchdog_device *wdt, unsigned int timeout)
394 {
395 	struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
396 	struct i2c_client *client = ds1374->client;
397 	int ret, cr;
398 
399 	wdt->timeout = timeout;
400 
401 	cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
402 	if (cr < 0)
403 		return cr;
404 
405 	/* Disable any existing watchdog/alarm before setting the new one */
406 	cr &= ~DS1374_REG_CR_WACE;
407 
408 	ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
409 	if (ret < 0)
410 		return ret;
411 
412 	/* Set new watchdog time */
413 	timeout = timeout * 4096;
414 	ret = ds1374_write_rtc(client, timeout, DS1374_REG_WDALM0, 3);
415 	if (ret)
416 		return ret;
417 
418 	/* Enable watchdog timer */
419 	cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM;
420 	cr &= ~DS1374_REG_CR_WDSTR;/* for RST PIN */
421 	cr &= ~DS1374_REG_CR_AIE;
422 
423 	ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
424 	if (ret < 0)
425 		return ret;
426 
427 	return 0;
428 }
429 
430 /*
431  * Reload the watchdog timer.  (ie, pat the watchdog)
432  */
433 static int ds1374_wdt_start(struct watchdog_device *wdt)
434 {
435 	struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
436 	u32 val;
437 
438 	return ds1374_read_rtc(ds1374->client, &val, DS1374_REG_WDALM0, 3);
439 }
440 
441 static int ds1374_wdt_stop(struct watchdog_device *wdt)
442 {
443 	struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
444 	struct i2c_client *client = ds1374->client;
445 	int cr;
446 
447 	cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
448 	if (cr < 0)
449 		return cr;
450 
451 	/* Disable watchdog timer */
452 	cr &= ~DS1374_REG_CR_WACE;
453 
454 	return i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
455 }
456 
457 static const struct watchdog_ops ds1374_wdt_ops = {
458 	.owner          = THIS_MODULE,
459 	.start          = ds1374_wdt_start,
460 	.stop           = ds1374_wdt_stop,
461 	.set_timeout    = ds1374_wdt_settimeout,
462 };
463 #endif /*CONFIG_RTC_DRV_DS1374_WDT*/
464 /*
465  *****************************************************************************
466  *
467  *	Driver Interface
468  *
469  *****************************************************************************
470  */
471 static int ds1374_probe(struct i2c_client *client)
472 {
473 	struct ds1374 *ds1374;
474 	int ret;
475 
476 	ds1374 = devm_kzalloc_obj(&client->dev, struct ds1374);
477 	if (!ds1374)
478 		return -ENOMEM;
479 
480 	ds1374->rtc = devm_rtc_allocate_device(&client->dev);
481 	if (IS_ERR(ds1374->rtc))
482 		return PTR_ERR(ds1374->rtc);
483 
484 	ds1374->client = client;
485 	i2c_set_clientdata(client, ds1374);
486 
487 	INIT_WORK(&ds1374->work, ds1374_work);
488 	mutex_init(&ds1374->mutex);
489 
490 	ret = ds1374_check_rtc_status(client);
491 	if (ret)
492 		return ret;
493 
494 	if (client->irq > 0) {
495 		ret = devm_request_irq(&client->dev, client->irq, ds1374_irq, 0,
496 					"ds1374", client);
497 		if (ret) {
498 			dev_err(&client->dev, "unable to request IRQ\n");
499 			return ret;
500 		}
501 
502 		device_set_wakeup_capable(&client->dev, 1);
503 	}
504 
505 	ds1374->rtc->ops = &ds1374_rtc_ops;
506 	ds1374->rtc->range_max = U32_MAX;
507 
508 	ret = devm_rtc_register_device(ds1374->rtc);
509 	if (ret)
510 		return ret;
511 
512 #ifdef CONFIG_RTC_DRV_DS1374_WDT
513 	ds1374->wdt.info = &ds1374_wdt_info;
514 	ds1374->wdt.ops = &ds1374_wdt_ops;
515 	ds1374->wdt.timeout = TIMER_MARGIN_DEFAULT;
516 	ds1374->wdt.min_timeout = TIMER_MARGIN_MIN;
517 	ds1374->wdt.max_timeout = TIMER_MARGIN_MAX;
518 
519 	watchdog_init_timeout(&ds1374->wdt, wdt_margin, &client->dev);
520 	watchdog_set_nowayout(&ds1374->wdt, nowayout);
521 	watchdog_stop_on_reboot(&ds1374->wdt);
522 	watchdog_stop_on_unregister(&ds1374->wdt);
523 	watchdog_set_drvdata(&ds1374->wdt, ds1374);
524 	ds1374_wdt_settimeout(&ds1374->wdt, ds1374->wdt.timeout);
525 
526 	ret = devm_watchdog_register_device(&client->dev, &ds1374->wdt);
527 	if (ret)
528 		return ret;
529 #endif
530 
531 	return 0;
532 }
533 
534 static void ds1374_remove(struct i2c_client *client)
535 {
536 	struct ds1374 *ds1374 = i2c_get_clientdata(client);
537 
538 	if (client->irq > 0) {
539 		mutex_lock(&ds1374->mutex);
540 		ds1374->exiting = 1;
541 		mutex_unlock(&ds1374->mutex);
542 
543 		devm_free_irq(&client->dev, client->irq, client);
544 		cancel_work_sync(&ds1374->work);
545 	}
546 }
547 
548 #ifdef CONFIG_PM_SLEEP
549 static int ds1374_suspend(struct device *dev)
550 {
551 	struct i2c_client *client = to_i2c_client(dev);
552 
553 	if (client->irq > 0 && device_may_wakeup(&client->dev))
554 		enable_irq_wake(client->irq);
555 	return 0;
556 }
557 
558 static int ds1374_resume(struct device *dev)
559 {
560 	struct i2c_client *client = to_i2c_client(dev);
561 
562 	if (client->irq > 0 && device_may_wakeup(&client->dev))
563 		disable_irq_wake(client->irq);
564 	return 0;
565 }
566 #endif
567 
568 static SIMPLE_DEV_PM_OPS(ds1374_pm, ds1374_suspend, ds1374_resume);
569 
570 static struct i2c_driver ds1374_driver = {
571 	.driver = {
572 		.name = "rtc-ds1374",
573 		.of_match_table = of_match_ptr(ds1374_of_match),
574 		.pm = &ds1374_pm,
575 	},
576 	.probe = ds1374_probe,
577 	.remove = ds1374_remove,
578 	.id_table = ds1374_id,
579 };
580 
581 module_i2c_driver(ds1374_driver);
582 
583 MODULE_AUTHOR("Scott Wood <scottwood@freescale.com>");
584 MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC Driver");
585 MODULE_LICENSE("GPL");
586