3 * Driver for Dallas Semiconductor DS1343 Low Current, SPI Compatible
6 * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
7 * Ankur Srivastava <sankurece@gmail.com> : DS1343 Nvram Support
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/device.h>
19 #include <linux/spi/spi.h>
20 #include <linux/regmap.h>
21 #include <linux/rtc.h>
22 #include <linux/bcd.h>
24 #include <linux/pm_wakeirq.h>
25 #include <linux/slab.h>
27 #define DS1343_DRV_VERSION "01.00"
28 #define DALLAS_MAXIM_DS1343 0
29 #define DALLAS_MAXIM_DS1344 1
31 /* RTC DS1343 Registers */
32 #define DS1343_SECONDS_REG 0x00
33 #define DS1343_MINUTES_REG 0x01
34 #define DS1343_HOURS_REG 0x02
35 #define DS1343_DAY_REG 0x03
36 #define DS1343_DATE_REG 0x04
37 #define DS1343_MONTH_REG 0x05
38 #define DS1343_YEAR_REG 0x06
39 #define DS1343_ALM0_SEC_REG 0x07
40 #define DS1343_ALM0_MIN_REG 0x08
41 #define DS1343_ALM0_HOUR_REG 0x09
42 #define DS1343_ALM0_DAY_REG 0x0A
43 #define DS1343_ALM1_SEC_REG 0x0B
44 #define DS1343_ALM1_MIN_REG 0x0C
45 #define DS1343_ALM1_HOUR_REG 0x0D
46 #define DS1343_ALM1_DAY_REG 0x0E
47 #define DS1343_CONTROL_REG 0x0F
48 #define DS1343_STATUS_REG 0x10
49 #define DS1343_TRICKLE_REG 0x11
50 #define DS1343_NVRAM 0x20
52 #define DS1343_NVRAM_LEN 96
54 /* DS1343 Control Registers bits */
55 #define DS1343_EOSC 0x80
56 #define DS1343_DOSF 0x20
57 #define DS1343_EGFIL 0x10
58 #define DS1343_SQW 0x08
59 #define DS1343_INTCN 0x04
60 #define DS1343_A1IE 0x02
61 #define DS1343_A0IE 0x01
63 /* DS1343 Status Registers bits */
64 #define DS1343_OSF 0x80
65 #define DS1343_IRQF1 0x02
66 #define DS1343_IRQF0 0x01
68 /* DS1343 Trickle Charger Registers bits */
69 #define DS1343_TRICKLE_MAGIC 0xa0
70 #define DS1343_TRICKLE_DS1 0x08
71 #define DS1343_TRICKLE_1K 0x01
72 #define DS1343_TRICKLE_2K 0x02
73 #define DS1343_TRICKLE_4K 0x03
75 static const struct spi_device_id ds1343_id[] = {
76 { "ds1343", DALLAS_MAXIM_DS1343 },
77 { "ds1344", DALLAS_MAXIM_DS1344 },
80 MODULE_DEVICE_TABLE(spi, ds1343_id);
83 struct spi_device *spi;
84 struct rtc_device *rtc;
95 static int ds1343_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
103 if (copy_from_user(&val, (int __user *)arg, sizeof(int)))
106 return regmap_write(priv->map, DS1343_TRICKLE_REG, val);
115 static ssize_t ds1343_show_glitchfilter(struct device *dev,
116 struct device_attribute *attr, char *buf)
118 struct ds1343_priv *priv = dev_get_drvdata(dev);
119 int glitch_filt_status, data;
121 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
123 glitch_filt_status = !!(data & DS1343_EGFIL);
125 if (glitch_filt_status)
126 return sprintf(buf, "enabled\n");
128 return sprintf(buf, "disabled\n");
131 static ssize_t ds1343_store_glitchfilter(struct device *dev,
132 struct device_attribute *attr,
133 const char *buf, size_t count)
135 struct ds1343_priv *priv = dev_get_drvdata(dev);
138 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
140 if (strncmp(buf, "enabled", 7) == 0)
141 data |= DS1343_EGFIL;
143 else if (strncmp(buf, "disabled", 8) == 0)
144 data &= ~(DS1343_EGFIL);
149 regmap_write(priv->map, DS1343_CONTROL_REG, data);
154 static DEVICE_ATTR(glitch_filter, S_IRUGO | S_IWUSR, ds1343_show_glitchfilter,
155 ds1343_store_glitchfilter);
157 static ssize_t ds1343_nvram_write(struct file *filp, struct kobject *kobj,
158 struct bin_attribute *attr,
159 char *buf, loff_t off, size_t count)
162 unsigned char address;
163 struct device *dev = kobj_to_dev(kobj);
164 struct ds1343_priv *priv = dev_get_drvdata(dev);
166 address = DS1343_NVRAM + off;
168 ret = regmap_bulk_write(priv->map, address, buf, count);
170 dev_err(&priv->spi->dev, "Error in nvram write %d", ret);
172 return (ret < 0) ? ret : count;
176 static ssize_t ds1343_nvram_read(struct file *filp, struct kobject *kobj,
177 struct bin_attribute *attr,
178 char *buf, loff_t off, size_t count)
181 unsigned char address;
182 struct device *dev = kobj_to_dev(kobj);
183 struct ds1343_priv *priv = dev_get_drvdata(dev);
185 address = DS1343_NVRAM + off;
187 ret = regmap_bulk_read(priv->map, address, buf, count);
189 dev_err(&priv->spi->dev, "Error in nvram read %d\n", ret);
191 return (ret < 0) ? ret : count;
195 static struct bin_attribute nvram_attr = {
196 .attr.name = "nvram",
197 .attr.mode = S_IRUGO | S_IWUSR,
198 .read = ds1343_nvram_read,
199 .write = ds1343_nvram_write,
200 .size = DS1343_NVRAM_LEN,
203 static ssize_t ds1343_show_alarmstatus(struct device *dev,
204 struct device_attribute *attr, char *buf)
206 struct ds1343_priv *priv = dev_get_drvdata(dev);
207 int alarmstatus, data;
209 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
211 alarmstatus = !!(data & DS1343_A0IE);
214 return sprintf(buf, "enabled\n");
216 return sprintf(buf, "disabled\n");
219 static DEVICE_ATTR(alarm_status, S_IRUGO, ds1343_show_alarmstatus, NULL);
221 static ssize_t ds1343_show_alarmmode(struct device *dev,
222 struct device_attribute *attr, char *buf)
224 struct ds1343_priv *priv = dev_get_drvdata(dev);
225 int alarm_mode, data;
228 regmap_read(priv->map, DS1343_ALM0_SEC_REG, &data);
229 alarm_mode = (data & 0x80) >> 4;
231 regmap_read(priv->map, DS1343_ALM0_MIN_REG, &data);
232 alarm_mode |= (data & 0x80) >> 5;
234 regmap_read(priv->map, DS1343_ALM0_HOUR_REG, &data);
235 alarm_mode |= (data & 0x80) >> 6;
237 regmap_read(priv->map, DS1343_ALM0_DAY_REG, &data);
238 alarm_mode |= (data & 0x80) >> 7;
240 switch (alarm_mode) {
242 alarm_str = "each second";
246 alarm_str = "seconds match";
250 alarm_str = "minutes and seconds match";
254 alarm_str = "hours, minutes and seconds match";
258 alarm_str = "day, hours, minutes and seconds match";
262 alarm_str = "invalid";
266 return sprintf(buf, "%s\n", alarm_str);
269 static DEVICE_ATTR(alarm_mode, S_IRUGO, ds1343_show_alarmmode, NULL);
271 static ssize_t ds1343_show_tricklecharger(struct device *dev,
272 struct device_attribute *attr, char *buf)
274 struct ds1343_priv *priv = dev_get_drvdata(dev);
276 char *diodes = "disabled", *resistors = " ";
278 regmap_read(priv->map, DS1343_TRICKLE_REG, &data);
280 if ((data & 0xf0) == DS1343_TRICKLE_MAGIC) {
281 switch (data & 0x0c) {
282 case DS1343_TRICKLE_DS1:
283 diodes = "one diode,";
287 diodes = "no diode,";
291 switch (data & 0x03) {
292 case DS1343_TRICKLE_1K:
293 resistors = "1k Ohm";
296 case DS1343_TRICKLE_2K:
297 resistors = "2k Ohm";
300 case DS1343_TRICKLE_4K:
301 resistors = "4k Ohm";
310 return sprintf(buf, "%s %s\n", diodes, resistors);
313 static DEVICE_ATTR(trickle_charger, S_IRUGO, ds1343_show_tricklecharger, NULL);
315 static int ds1343_sysfs_register(struct device *dev)
317 struct ds1343_priv *priv = dev_get_drvdata(dev);
320 err = device_create_file(dev, &dev_attr_glitch_filter);
324 err = device_create_file(dev, &dev_attr_trickle_charger);
328 err = device_create_bin_file(dev, &nvram_attr);
335 err = device_create_file(dev, &dev_attr_alarm_mode);
339 err = device_create_file(dev, &dev_attr_alarm_status);
343 device_remove_file(dev, &dev_attr_alarm_mode);
346 device_remove_bin_file(dev, &nvram_attr);
349 device_remove_file(dev, &dev_attr_trickle_charger);
352 device_remove_file(dev, &dev_attr_glitch_filter);
357 static void ds1343_sysfs_unregister(struct device *dev)
359 struct ds1343_priv *priv = dev_get_drvdata(dev);
361 device_remove_file(dev, &dev_attr_glitch_filter);
362 device_remove_file(dev, &dev_attr_trickle_charger);
363 device_remove_bin_file(dev, &nvram_attr);
368 device_remove_file(dev, &dev_attr_alarm_status);
369 device_remove_file(dev, &dev_attr_alarm_mode);
372 static int ds1343_read_time(struct device *dev, struct rtc_time *dt)
374 struct ds1343_priv *priv = dev_get_drvdata(dev);
375 unsigned char buf[7];
378 res = regmap_bulk_read(priv->map, DS1343_SECONDS_REG, buf, 7);
382 dt->tm_sec = bcd2bin(buf[0]);
383 dt->tm_min = bcd2bin(buf[1]);
384 dt->tm_hour = bcd2bin(buf[2] & 0x3F);
385 dt->tm_wday = bcd2bin(buf[3]) - 1;
386 dt->tm_mday = bcd2bin(buf[4]);
387 dt->tm_mon = bcd2bin(buf[5] & 0x1F) - 1;
388 dt->tm_year = bcd2bin(buf[6]) + 100; /* year offset from 1900 */
390 return rtc_valid_tm(dt);
393 static int ds1343_set_time(struct device *dev, struct rtc_time *dt)
395 struct ds1343_priv *priv = dev_get_drvdata(dev);
398 res = regmap_write(priv->map, DS1343_SECONDS_REG,
399 bin2bcd(dt->tm_sec));
403 res = regmap_write(priv->map, DS1343_MINUTES_REG,
404 bin2bcd(dt->tm_min));
408 res = regmap_write(priv->map, DS1343_HOURS_REG,
409 bin2bcd(dt->tm_hour) & 0x3F);
413 res = regmap_write(priv->map, DS1343_DAY_REG,
414 bin2bcd(dt->tm_wday + 1));
418 res = regmap_write(priv->map, DS1343_DATE_REG,
419 bin2bcd(dt->tm_mday));
423 res = regmap_write(priv->map, DS1343_MONTH_REG,
424 bin2bcd(dt->tm_mon + 1));
430 res = regmap_write(priv->map, DS1343_YEAR_REG,
431 bin2bcd(dt->tm_year));
438 static int ds1343_update_alarm(struct device *dev)
440 struct ds1343_priv *priv = dev_get_drvdata(dev);
441 unsigned int control, stat;
442 unsigned char buf[4];
445 res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
449 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
453 control &= ~(DS1343_A0IE);
454 stat &= ~(DS1343_IRQF0);
456 res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
460 res = regmap_write(priv->map, DS1343_STATUS_REG, stat);
464 buf[0] = priv->alarm_sec < 0 || (priv->irqen & RTC_UF) ?
465 0x80 : bin2bcd(priv->alarm_sec) & 0x7F;
466 buf[1] = priv->alarm_min < 0 || (priv->irqen & RTC_UF) ?
467 0x80 : bin2bcd(priv->alarm_min) & 0x7F;
468 buf[2] = priv->alarm_hour < 0 || (priv->irqen & RTC_UF) ?
469 0x80 : bin2bcd(priv->alarm_hour) & 0x3F;
470 buf[3] = priv->alarm_mday < 0 || (priv->irqen & RTC_UF) ?
471 0x80 : bin2bcd(priv->alarm_mday) & 0x7F;
473 res = regmap_bulk_write(priv->map, DS1343_ALM0_SEC_REG, buf, 4);
478 control |= DS1343_A0IE;
479 res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
485 static int ds1343_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
487 struct ds1343_priv *priv = dev_get_drvdata(dev);
494 mutex_lock(&priv->mutex);
496 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
500 alarm->enabled = !!(priv->irqen & RTC_AF);
501 alarm->pending = !!(stat & DS1343_IRQF0);
503 alarm->time.tm_sec = priv->alarm_sec < 0 ? 0 : priv->alarm_sec;
504 alarm->time.tm_min = priv->alarm_min < 0 ? 0 : priv->alarm_min;
505 alarm->time.tm_hour = priv->alarm_hour < 0 ? 0 : priv->alarm_hour;
506 alarm->time.tm_mday = priv->alarm_mday < 0 ? 0 : priv->alarm_mday;
508 alarm->time.tm_mon = -1;
509 alarm->time.tm_year = -1;
510 alarm->time.tm_wday = -1;
511 alarm->time.tm_yday = -1;
512 alarm->time.tm_isdst = -1;
515 mutex_unlock(&priv->mutex);
519 static int ds1343_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
521 struct ds1343_priv *priv = dev_get_drvdata(dev);
527 mutex_lock(&priv->mutex);
529 priv->alarm_sec = alarm->time.tm_sec;
530 priv->alarm_min = alarm->time.tm_min;
531 priv->alarm_hour = alarm->time.tm_hour;
532 priv->alarm_mday = alarm->time.tm_mday;
535 priv->irqen |= RTC_AF;
537 res = ds1343_update_alarm(dev);
539 mutex_unlock(&priv->mutex);
544 static int ds1343_alarm_irq_enable(struct device *dev, unsigned int enabled)
546 struct ds1343_priv *priv = dev_get_drvdata(dev);
552 mutex_lock(&priv->mutex);
555 priv->irqen |= RTC_AF;
557 priv->irqen &= ~RTC_AF;
559 res = ds1343_update_alarm(dev);
561 mutex_unlock(&priv->mutex);
566 static irqreturn_t ds1343_thread(int irq, void *dev_id)
568 struct ds1343_priv *priv = dev_id;
569 unsigned int stat, control;
572 mutex_lock(&priv->mutex);
574 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
578 if (stat & DS1343_IRQF0) {
579 stat &= ~DS1343_IRQF0;
580 regmap_write(priv->map, DS1343_STATUS_REG, stat);
582 res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
586 control &= ~DS1343_A0IE;
587 regmap_write(priv->map, DS1343_CONTROL_REG, control);
589 rtc_update_irq(priv->rtc, 1, RTC_AF | RTC_IRQF);
593 mutex_unlock(&priv->mutex);
597 static const struct rtc_class_ops ds1343_rtc_ops = {
598 .ioctl = ds1343_ioctl,
599 .read_time = ds1343_read_time,
600 .set_time = ds1343_set_time,
601 .read_alarm = ds1343_read_alarm,
602 .set_alarm = ds1343_set_alarm,
603 .alarm_irq_enable = ds1343_alarm_irq_enable,
606 static int ds1343_probe(struct spi_device *spi)
608 struct ds1343_priv *priv;
609 struct regmap_config config;
613 memset(&config, 0, sizeof(config));
616 config.write_flag_mask = 0x80;
618 priv = devm_kzalloc(&spi->dev, sizeof(struct ds1343_priv), GFP_KERNEL);
623 mutex_init(&priv->mutex);
625 /* RTC DS1347 works in spi mode 3 and
626 * its chip select is active high
628 spi->mode = SPI_MODE_3 | SPI_CS_HIGH;
629 spi->bits_per_word = 8;
630 res = spi_setup(spi);
634 spi_set_drvdata(spi, priv);
636 priv->map = devm_regmap_init_spi(spi, &config);
638 if (IS_ERR(priv->map)) {
639 dev_err(&spi->dev, "spi regmap init failed for rtc ds1343\n");
640 return PTR_ERR(priv->map);
643 res = regmap_read(priv->map, DS1343_SECONDS_REG, &data);
647 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
648 data |= DS1343_INTCN;
649 data &= ~(DS1343_EOSC | DS1343_A1IE | DS1343_A0IE);
650 regmap_write(priv->map, DS1343_CONTROL_REG, data);
652 regmap_read(priv->map, DS1343_STATUS_REG, &data);
653 data &= ~(DS1343_OSF | DS1343_IRQF1 | DS1343_IRQF0);
654 regmap_write(priv->map, DS1343_STATUS_REG, data);
656 priv->rtc = devm_rtc_device_register(&spi->dev, "ds1343",
657 &ds1343_rtc_ops, THIS_MODULE);
658 if (IS_ERR(priv->rtc)) {
659 dev_err(&spi->dev, "unable to register rtc ds1343\n");
660 return PTR_ERR(priv->rtc);
663 priv->irq = spi->irq;
665 if (priv->irq >= 0) {
666 res = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,
667 ds1343_thread, IRQF_ONESHOT,
672 "unable to request irq for rtc ds1343\n");
674 device_init_wakeup(&spi->dev, true);
675 dev_pm_set_wake_irq(&spi->dev, spi->irq);
679 res = ds1343_sysfs_register(&spi->dev);
682 "unable to create sysfs entries for rtc ds1343\n");
687 static int ds1343_remove(struct spi_device *spi)
689 struct ds1343_priv *priv = spi_get_drvdata(spi);
692 mutex_lock(&priv->mutex);
693 priv->irqen &= ~RTC_AF;
694 mutex_unlock(&priv->mutex);
696 dev_pm_clear_wake_irq(&spi->dev);
697 device_init_wakeup(&spi->dev, false);
698 devm_free_irq(&spi->dev, spi->irq, priv);
701 spi_set_drvdata(spi, NULL);
703 ds1343_sysfs_unregister(&spi->dev);
708 #ifdef CONFIG_PM_SLEEP
710 static int ds1343_suspend(struct device *dev)
712 struct spi_device *spi = to_spi_device(dev);
714 if (spi->irq >= 0 && device_may_wakeup(dev))
715 enable_irq_wake(spi->irq);
720 static int ds1343_resume(struct device *dev)
722 struct spi_device *spi = to_spi_device(dev);
724 if (spi->irq >= 0 && device_may_wakeup(dev))
725 disable_irq_wake(spi->irq);
732 static SIMPLE_DEV_PM_OPS(ds1343_pm, ds1343_suspend, ds1343_resume);
734 static struct spi_driver ds1343_driver = {
739 .probe = ds1343_probe,
740 .remove = ds1343_remove,
741 .id_table = ds1343_id,
744 module_spi_driver(ds1343_driver);
746 MODULE_DESCRIPTION("DS1343 RTC SPI Driver");
747 MODULE_AUTHOR("Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>,"
748 "Ankur Srivastava <sankurece@gmail.com>");
749 MODULE_LICENSE("GPL v2");
750 MODULE_VERSION(DS1343_DRV_VERSION);