xref: /linux-6.15/drivers/rtc/rtc-ds1343.c (revision 8c9a88fa)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* rtc-ds1343.c
3  *
4  * Driver for Dallas Semiconductor DS1343 Low Current, SPI Compatible
5  * Real Time Clock
6  *
7  * Author : Raghavendra Chandra Ganiga <[email protected]>
8  *	    Ankur Srivastava <[email protected]> : DS1343 Nvram Support
9  */
10 
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/device.h>
15 #include <linux/spi/spi.h>
16 #include <linux/regmap.h>
17 #include <linux/rtc.h>
18 #include <linux/bcd.h>
19 #include <linux/pm.h>
20 #include <linux/pm_wakeirq.h>
21 #include <linux/slab.h>
22 
23 #define DALLAS_MAXIM_DS1343	0
24 #define DALLAS_MAXIM_DS1344	1
25 
26 /* RTC DS1343 Registers */
27 #define DS1343_SECONDS_REG	0x00
28 #define DS1343_MINUTES_REG	0x01
29 #define DS1343_HOURS_REG	0x02
30 #define DS1343_DAY_REG		0x03
31 #define DS1343_DATE_REG		0x04
32 #define DS1343_MONTH_REG	0x05
33 #define DS1343_YEAR_REG		0x06
34 #define DS1343_ALM0_SEC_REG	0x07
35 #define DS1343_ALM0_MIN_REG	0x08
36 #define DS1343_ALM0_HOUR_REG	0x09
37 #define DS1343_ALM0_DAY_REG	0x0A
38 #define DS1343_ALM1_SEC_REG	0x0B
39 #define DS1343_ALM1_MIN_REG	0x0C
40 #define DS1343_ALM1_HOUR_REG	0x0D
41 #define DS1343_ALM1_DAY_REG	0x0E
42 #define DS1343_CONTROL_REG	0x0F
43 #define DS1343_STATUS_REG	0x10
44 #define DS1343_TRICKLE_REG	0x11
45 #define DS1343_NVRAM		0x20
46 
47 #define DS1343_NVRAM_LEN	96
48 
49 /* DS1343 Control Registers bits */
50 #define DS1343_EOSC		0x80
51 #define DS1343_DOSF		0x20
52 #define DS1343_EGFIL		0x10
53 #define DS1343_SQW		0x08
54 #define DS1343_INTCN		0x04
55 #define DS1343_A1IE		0x02
56 #define DS1343_A0IE		0x01
57 
58 /* DS1343 Status Registers bits */
59 #define DS1343_OSF		0x80
60 #define DS1343_IRQF1		0x02
61 #define DS1343_IRQF0		0x01
62 
63 /* DS1343 Trickle Charger Registers bits */
64 #define DS1343_TRICKLE_MAGIC	0xa0
65 #define DS1343_TRICKLE_DS1	0x08
66 #define DS1343_TRICKLE_1K	0x01
67 #define DS1343_TRICKLE_2K	0x02
68 #define DS1343_TRICKLE_4K	0x03
69 
70 static const struct spi_device_id ds1343_id[] = {
71 	{ "ds1343", DALLAS_MAXIM_DS1343 },
72 	{ "ds1344", DALLAS_MAXIM_DS1344 },
73 	{ }
74 };
75 MODULE_DEVICE_TABLE(spi, ds1343_id);
76 
77 struct ds1343_priv {
78 	struct spi_device *spi;
79 	struct rtc_device *rtc;
80 	struct regmap *map;
81 	struct mutex mutex;
82 	unsigned int irqen;
83 	int irq;
84 	int alarm_sec;
85 	int alarm_min;
86 	int alarm_hour;
87 	int alarm_mday;
88 };
89 
90 static ssize_t ds1343_show_glitchfilter(struct device *dev,
91 				struct device_attribute *attr, char *buf)
92 {
93 	struct ds1343_priv *priv = dev_get_drvdata(dev);
94 	int glitch_filt_status, data;
95 
96 	regmap_read(priv->map, DS1343_CONTROL_REG, &data);
97 
98 	glitch_filt_status = !!(data & DS1343_EGFIL);
99 
100 	if (glitch_filt_status)
101 		return sprintf(buf, "enabled\n");
102 	else
103 		return sprintf(buf, "disabled\n");
104 }
105 
106 static ssize_t ds1343_store_glitchfilter(struct device *dev,
107 					struct device_attribute *attr,
108 					const char *buf, size_t count)
109 {
110 	struct ds1343_priv *priv = dev_get_drvdata(dev);
111 	int data;
112 
113 	regmap_read(priv->map, DS1343_CONTROL_REG, &data);
114 
115 	if (strncmp(buf, "enabled", 7) == 0)
116 		data |= DS1343_EGFIL;
117 
118 	else if (strncmp(buf, "disabled", 8) == 0)
119 		data &= ~(DS1343_EGFIL);
120 
121 	else
122 		return -EINVAL;
123 
124 	regmap_write(priv->map, DS1343_CONTROL_REG, data);
125 
126 	return count;
127 }
128 
129 static DEVICE_ATTR(glitch_filter, S_IRUGO | S_IWUSR, ds1343_show_glitchfilter,
130 			ds1343_store_glitchfilter);
131 
132 static int ds1343_nvram_write(void *priv, unsigned int off, void *val,
133 			      size_t bytes)
134 {
135 	struct ds1343_priv *ds1343 = priv;
136 
137 	return regmap_bulk_write(ds1343->map, DS1343_NVRAM + off, val, bytes);
138 }
139 
140 static int ds1343_nvram_read(void *priv, unsigned int off, void *val,
141 			     size_t bytes)
142 {
143 	struct ds1343_priv *ds1343 = priv;
144 
145 	return regmap_bulk_read(ds1343->map, DS1343_NVRAM + off, val, bytes);
146 }
147 
148 static ssize_t ds1343_show_tricklecharger(struct device *dev,
149 				struct device_attribute *attr, char *buf)
150 {
151 	struct ds1343_priv *priv = dev_get_drvdata(dev);
152 	int data;
153 	char *diodes = "disabled", *resistors = " ";
154 
155 	regmap_read(priv->map, DS1343_TRICKLE_REG, &data);
156 
157 	if ((data & 0xf0) == DS1343_TRICKLE_MAGIC) {
158 		switch (data & 0x0c) {
159 		case DS1343_TRICKLE_DS1:
160 			diodes = "one diode,";
161 			break;
162 
163 		default:
164 			diodes = "no diode,";
165 			break;
166 		}
167 
168 		switch (data & 0x03) {
169 		case DS1343_TRICKLE_1K:
170 			resistors = "1k Ohm";
171 			break;
172 
173 		case DS1343_TRICKLE_2K:
174 			resistors = "2k Ohm";
175 			break;
176 
177 		case DS1343_TRICKLE_4K:
178 			resistors = "4k Ohm";
179 			break;
180 
181 		default:
182 			diodes = "disabled";
183 			break;
184 		}
185 	}
186 
187 	return sprintf(buf, "%s %s\n", diodes, resistors);
188 }
189 
190 static DEVICE_ATTR(trickle_charger, S_IRUGO, ds1343_show_tricklecharger, NULL);
191 
192 static int ds1343_sysfs_register(struct device *dev)
193 {
194 	int err;
195 
196 	err = device_create_file(dev, &dev_attr_glitch_filter);
197 	if (err)
198 		return err;
199 
200 	err = device_create_file(dev, &dev_attr_trickle_charger);
201 	if (!err)
202 		return 0;
203 
204 	device_remove_file(dev, &dev_attr_glitch_filter);
205 
206 	return err;
207 }
208 
209 static void ds1343_sysfs_unregister(struct device *dev)
210 {
211 	device_remove_file(dev, &dev_attr_glitch_filter);
212 	device_remove_file(dev, &dev_attr_trickle_charger);
213 }
214 
215 static int ds1343_read_time(struct device *dev, struct rtc_time *dt)
216 {
217 	struct ds1343_priv *priv = dev_get_drvdata(dev);
218 	unsigned char buf[7];
219 	int res;
220 
221 	res = regmap_bulk_read(priv->map, DS1343_SECONDS_REG, buf, 7);
222 	if (res)
223 		return res;
224 
225 	dt->tm_sec	= bcd2bin(buf[0]);
226 	dt->tm_min	= bcd2bin(buf[1]);
227 	dt->tm_hour	= bcd2bin(buf[2] & 0x3F);
228 	dt->tm_wday	= bcd2bin(buf[3]) - 1;
229 	dt->tm_mday	= bcd2bin(buf[4]);
230 	dt->tm_mon	= bcd2bin(buf[5] & 0x1F) - 1;
231 	dt->tm_year	= bcd2bin(buf[6]) + 100; /* year offset from 1900 */
232 
233 	return 0;
234 }
235 
236 static int ds1343_set_time(struct device *dev, struct rtc_time *dt)
237 {
238 	struct ds1343_priv *priv = dev_get_drvdata(dev);
239 	int res;
240 
241 	res = regmap_write(priv->map, DS1343_SECONDS_REG,
242 				bin2bcd(dt->tm_sec));
243 	if (res)
244 		return res;
245 
246 	res = regmap_write(priv->map, DS1343_MINUTES_REG,
247 				bin2bcd(dt->tm_min));
248 	if (res)
249 		return res;
250 
251 	res = regmap_write(priv->map, DS1343_HOURS_REG,
252 				bin2bcd(dt->tm_hour) & 0x3F);
253 	if (res)
254 		return res;
255 
256 	res = regmap_write(priv->map, DS1343_DAY_REG,
257 				bin2bcd(dt->tm_wday + 1));
258 	if (res)
259 		return res;
260 
261 	res = regmap_write(priv->map, DS1343_DATE_REG,
262 				bin2bcd(dt->tm_mday));
263 	if (res)
264 		return res;
265 
266 	res = regmap_write(priv->map, DS1343_MONTH_REG,
267 				bin2bcd(dt->tm_mon + 1));
268 	if (res)
269 		return res;
270 
271 	dt->tm_year %= 100;
272 
273 	res = regmap_write(priv->map, DS1343_YEAR_REG,
274 				bin2bcd(dt->tm_year));
275 	if (res)
276 		return res;
277 
278 	return 0;
279 }
280 
281 static int ds1343_update_alarm(struct device *dev)
282 {
283 	struct ds1343_priv *priv = dev_get_drvdata(dev);
284 	unsigned int control, stat;
285 	unsigned char buf[4];
286 	int res = 0;
287 
288 	res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
289 	if (res)
290 		return res;
291 
292 	res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
293 	if (res)
294 		return res;
295 
296 	control &= ~(DS1343_A0IE);
297 	stat &= ~(DS1343_IRQF0);
298 
299 	res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
300 	if (res)
301 		return res;
302 
303 	res = regmap_write(priv->map, DS1343_STATUS_REG, stat);
304 	if (res)
305 		return res;
306 
307 	buf[0] = priv->alarm_sec < 0 || (priv->irqen & RTC_UF) ?
308 		0x80 : bin2bcd(priv->alarm_sec) & 0x7F;
309 	buf[1] = priv->alarm_min < 0 || (priv->irqen & RTC_UF) ?
310 		0x80 : bin2bcd(priv->alarm_min) & 0x7F;
311 	buf[2] = priv->alarm_hour < 0 || (priv->irqen & RTC_UF) ?
312 		0x80 : bin2bcd(priv->alarm_hour) & 0x3F;
313 	buf[3] = priv->alarm_mday < 0 || (priv->irqen & RTC_UF) ?
314 		0x80 : bin2bcd(priv->alarm_mday) & 0x7F;
315 
316 	res = regmap_bulk_write(priv->map, DS1343_ALM0_SEC_REG, buf, 4);
317 	if (res)
318 		return res;
319 
320 	if (priv->irqen) {
321 		control |= DS1343_A0IE;
322 		res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
323 	}
324 
325 	return res;
326 }
327 
328 static int ds1343_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
329 {
330 	struct ds1343_priv *priv = dev_get_drvdata(dev);
331 	int res = 0;
332 	unsigned int stat;
333 
334 	if (priv->irq <= 0)
335 		return -EINVAL;
336 
337 	mutex_lock(&priv->mutex);
338 
339 	res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
340 	if (res)
341 		goto out;
342 
343 	alarm->enabled = !!(priv->irqen & RTC_AF);
344 	alarm->pending = !!(stat & DS1343_IRQF0);
345 
346 	alarm->time.tm_sec = priv->alarm_sec < 0 ? 0 : priv->alarm_sec;
347 	alarm->time.tm_min = priv->alarm_min < 0 ? 0 : priv->alarm_min;
348 	alarm->time.tm_hour = priv->alarm_hour < 0 ? 0 : priv->alarm_hour;
349 	alarm->time.tm_mday = priv->alarm_mday < 0 ? 0 : priv->alarm_mday;
350 
351 out:
352 	mutex_unlock(&priv->mutex);
353 	return res;
354 }
355 
356 static int ds1343_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
357 {
358 	struct ds1343_priv *priv = dev_get_drvdata(dev);
359 	int res = 0;
360 
361 	if (priv->irq <= 0)
362 		return -EINVAL;
363 
364 	mutex_lock(&priv->mutex);
365 
366 	priv->alarm_sec = alarm->time.tm_sec;
367 	priv->alarm_min = alarm->time.tm_min;
368 	priv->alarm_hour = alarm->time.tm_hour;
369 	priv->alarm_mday = alarm->time.tm_mday;
370 
371 	if (alarm->enabled)
372 		priv->irqen |= RTC_AF;
373 
374 	res = ds1343_update_alarm(dev);
375 
376 	mutex_unlock(&priv->mutex);
377 
378 	return res;
379 }
380 
381 static int ds1343_alarm_irq_enable(struct device *dev, unsigned int enabled)
382 {
383 	struct ds1343_priv *priv = dev_get_drvdata(dev);
384 	int res = 0;
385 
386 	if (priv->irq <= 0)
387 		return -EINVAL;
388 
389 	mutex_lock(&priv->mutex);
390 
391 	if (enabled)
392 		priv->irqen |= RTC_AF;
393 	else
394 		priv->irqen &= ~RTC_AF;
395 
396 	res = ds1343_update_alarm(dev);
397 
398 	mutex_unlock(&priv->mutex);
399 
400 	return res;
401 }
402 
403 static irqreturn_t ds1343_thread(int irq, void *dev_id)
404 {
405 	struct ds1343_priv *priv = dev_id;
406 	unsigned int stat, control;
407 	int res = 0;
408 
409 	mutex_lock(&priv->mutex);
410 
411 	res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
412 	if (res)
413 		goto out;
414 
415 	if (stat & DS1343_IRQF0) {
416 		stat &= ~DS1343_IRQF0;
417 		regmap_write(priv->map, DS1343_STATUS_REG, stat);
418 
419 		res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
420 		if (res)
421 			goto out;
422 
423 		control &= ~DS1343_A0IE;
424 		regmap_write(priv->map, DS1343_CONTROL_REG, control);
425 
426 		rtc_update_irq(priv->rtc, 1, RTC_AF | RTC_IRQF);
427 	}
428 
429 out:
430 	mutex_unlock(&priv->mutex);
431 	return IRQ_HANDLED;
432 }
433 
434 static const struct rtc_class_ops ds1343_rtc_ops = {
435 	.read_time	= ds1343_read_time,
436 	.set_time	= ds1343_set_time,
437 	.read_alarm	= ds1343_read_alarm,
438 	.set_alarm	= ds1343_set_alarm,
439 	.alarm_irq_enable = ds1343_alarm_irq_enable,
440 };
441 
442 static int ds1343_probe(struct spi_device *spi)
443 {
444 	struct ds1343_priv *priv;
445 	struct regmap_config config = { .reg_bits = 8, .val_bits = 8,
446 					.write_flag_mask = 0x80, };
447 	unsigned int data;
448 	int res;
449 	struct nvmem_config nvmem_cfg = {
450 		.name = "ds1343-",
451 		.word_size = 1,
452 		.stride = 1,
453 		.size = DS1343_NVRAM_LEN,
454 		.reg_read = ds1343_nvram_read,
455 		.reg_write = ds1343_nvram_write,
456 	};
457 
458 	priv = devm_kzalloc(&spi->dev, sizeof(struct ds1343_priv), GFP_KERNEL);
459 	if (!priv)
460 		return -ENOMEM;
461 
462 	priv->spi = spi;
463 	mutex_init(&priv->mutex);
464 
465 	/* RTC DS1347 works in spi mode 3 and
466 	 * its chip select is active high
467 	 */
468 	spi->mode = SPI_MODE_3 | SPI_CS_HIGH;
469 	spi->bits_per_word = 8;
470 	res = spi_setup(spi);
471 	if (res)
472 		return res;
473 
474 	spi_set_drvdata(spi, priv);
475 
476 	priv->map = devm_regmap_init_spi(spi, &config);
477 
478 	if (IS_ERR(priv->map)) {
479 		dev_err(&spi->dev, "spi regmap init failed for rtc ds1343\n");
480 		return PTR_ERR(priv->map);
481 	}
482 
483 	res = regmap_read(priv->map, DS1343_SECONDS_REG, &data);
484 	if (res)
485 		return res;
486 
487 	regmap_read(priv->map, DS1343_CONTROL_REG, &data);
488 	data |= DS1343_INTCN;
489 	data &= ~(DS1343_EOSC | DS1343_A1IE | DS1343_A0IE);
490 	regmap_write(priv->map, DS1343_CONTROL_REG, data);
491 
492 	regmap_read(priv->map, DS1343_STATUS_REG, &data);
493 	data &= ~(DS1343_OSF | DS1343_IRQF1 | DS1343_IRQF0);
494 	regmap_write(priv->map, DS1343_STATUS_REG, data);
495 
496 	priv->rtc = devm_rtc_allocate_device(&spi->dev);
497 	if (IS_ERR(priv->rtc))
498 		return PTR_ERR(priv->rtc);
499 
500 	priv->rtc->nvram_old_abi = true;
501 	priv->rtc->ops = &ds1343_rtc_ops;
502 	priv->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
503 	priv->rtc->range_max = RTC_TIMESTAMP_END_2099;
504 
505 	res = rtc_register_device(priv->rtc);
506 	if (res)
507 		return res;
508 
509 	nvmem_cfg.priv = priv;
510 	rtc_nvmem_register(priv->rtc, &nvmem_cfg);
511 
512 	priv->irq = spi->irq;
513 
514 	if (priv->irq >= 0) {
515 		res = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,
516 						ds1343_thread, IRQF_ONESHOT,
517 						"ds1343", priv);
518 		if (res) {
519 			priv->irq = -1;
520 			dev_err(&spi->dev,
521 				"unable to request irq for rtc ds1343\n");
522 		} else {
523 			device_init_wakeup(&spi->dev, true);
524 			dev_pm_set_wake_irq(&spi->dev, spi->irq);
525 		}
526 	}
527 
528 	res = ds1343_sysfs_register(&spi->dev);
529 	if (res)
530 		dev_err(&spi->dev,
531 			"unable to create sysfs entries for rtc ds1343\n");
532 
533 	return 0;
534 }
535 
536 static int ds1343_remove(struct spi_device *spi)
537 {
538 	struct ds1343_priv *priv = spi_get_drvdata(spi);
539 
540 	if (spi->irq) {
541 		mutex_lock(&priv->mutex);
542 		priv->irqen &= ~RTC_AF;
543 		mutex_unlock(&priv->mutex);
544 
545 		dev_pm_clear_wake_irq(&spi->dev);
546 		device_init_wakeup(&spi->dev, false);
547 		devm_free_irq(&spi->dev, spi->irq, priv);
548 	}
549 
550 	spi_set_drvdata(spi, NULL);
551 
552 	ds1343_sysfs_unregister(&spi->dev);
553 
554 	return 0;
555 }
556 
557 #ifdef CONFIG_PM_SLEEP
558 
559 static int ds1343_suspend(struct device *dev)
560 {
561 	struct spi_device *spi = to_spi_device(dev);
562 
563 	if (spi->irq >= 0 && device_may_wakeup(dev))
564 		enable_irq_wake(spi->irq);
565 
566 	return 0;
567 }
568 
569 static int ds1343_resume(struct device *dev)
570 {
571 	struct spi_device *spi = to_spi_device(dev);
572 
573 	if (spi->irq >= 0 && device_may_wakeup(dev))
574 		disable_irq_wake(spi->irq);
575 
576 	return 0;
577 }
578 
579 #endif
580 
581 static SIMPLE_DEV_PM_OPS(ds1343_pm, ds1343_suspend, ds1343_resume);
582 
583 static struct spi_driver ds1343_driver = {
584 	.driver = {
585 		.name = "ds1343",
586 		.pm = &ds1343_pm,
587 	},
588 	.probe = ds1343_probe,
589 	.remove = ds1343_remove,
590 	.id_table = ds1343_id,
591 };
592 
593 module_spi_driver(ds1343_driver);
594 
595 MODULE_DESCRIPTION("DS1343 RTC SPI Driver");
596 MODULE_AUTHOR("Raghavendra Chandra Ganiga <[email protected]>,"
597 		"Ankur Srivastava <[email protected]>");
598 MODULE_LICENSE("GPL v2");
599