3 * Driver for Dallas Semiconductor DS1343 Low Current, SPI Compatible
6 * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/device.h>
18 #include <linux/spi/spi.h>
19 #include <linux/regmap.h>
20 #include <linux/rtc.h>
21 #include <linux/bcd.h>
23 #include <linux/slab.h>
25 #define DS1343_DRV_VERSION "01.00"
26 #define DALLAS_MAXIM_DS1343 0
27 #define DALLAS_MAXIM_DS1344 1
29 /* RTC DS1343 Registers */
30 #define DS1343_SECONDS_REG 0x00
31 #define DS1343_MINUTES_REG 0x01
32 #define DS1343_HOURS_REG 0x02
33 #define DS1343_DAY_REG 0x03
34 #define DS1343_DATE_REG 0x04
35 #define DS1343_MONTH_REG 0x05
36 #define DS1343_YEAR_REG 0x06
37 #define DS1343_ALM0_SEC_REG 0x07
38 #define DS1343_ALM0_MIN_REG 0x08
39 #define DS1343_ALM0_HOUR_REG 0x09
40 #define DS1343_ALM0_DAY_REG 0x0A
41 #define DS1343_ALM1_SEC_REG 0x0B
42 #define DS1343_ALM1_MIN_REG 0x0C
43 #define DS1343_ALM1_HOUR_REG 0x0D
44 #define DS1343_ALM1_DAY_REG 0x0E
45 #define DS1343_CONTROL_REG 0x0F
46 #define DS1343_STATUS_REG 0x10
47 #define DS1343_TRICKLE_REG 0x11
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
58 /* DS1343 Status Registers bits */
59 #define DS1343_OSF 0x80
60 #define DS1343_IRQF1 0x02
61 #define DS1343_IRQF0 0x01
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
70 static const struct spi_device_id ds1343_id
[] = {
71 { "ds1343", DALLAS_MAXIM_DS1343
},
72 { "ds1344", DALLAS_MAXIM_DS1344
},
75 MODULE_DEVICE_TABLE(spi
, ds1343_id
);
78 struct spi_device
*spi
;
79 struct rtc_device
*rtc
;
90 static int ds1343_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
98 if (copy_from_user(&val
, (int __user
*)arg
, sizeof(int)))
101 return regmap_write(priv
->map
, DS1343_TRICKLE_REG
, val
);
110 static ssize_t
ds1343_show_glitchfilter(struct device
*dev
,
111 struct device_attribute
*attr
, char *buf
)
113 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
114 int glitch_filt_status
, data
;
116 regmap_read(priv
->map
, DS1343_CONTROL_REG
, &data
);
118 glitch_filt_status
= !!(data
& DS1343_EGFIL
);
120 if (glitch_filt_status
)
121 return sprintf(buf
, "enabled\n");
123 return sprintf(buf
, "disabled\n");
126 static ssize_t
ds1343_store_glitchfilter(struct device
*dev
,
127 struct device_attribute
*attr
,
128 const char *buf
, size_t count
)
130 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
133 regmap_read(priv
->map
, DS1343_CONTROL_REG
, &data
);
135 if (strncmp(buf
, "enabled", 7) == 0)
136 data
|= DS1343_EGFIL
;
138 else if (strncmp(buf
, "disabled", 8) == 0)
139 data
&= ~(DS1343_EGFIL
);
144 regmap_write(priv
->map
, DS1343_CONTROL_REG
, data
);
149 static DEVICE_ATTR(glitch_filter
, S_IRUGO
| S_IWUSR
, ds1343_show_glitchfilter
,
150 ds1343_store_glitchfilter
);
152 static ssize_t
ds1343_show_alarmstatus(struct device
*dev
,
153 struct device_attribute
*attr
, char *buf
)
155 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
156 int alarmstatus
, data
;
158 regmap_read(priv
->map
, DS1343_CONTROL_REG
, &data
);
160 alarmstatus
= !!(data
& DS1343_A0IE
);
163 return sprintf(buf
, "enabled\n");
165 return sprintf(buf
, "disabled\n");
168 static DEVICE_ATTR(alarm_status
, S_IRUGO
, ds1343_show_alarmstatus
, NULL
);
170 static ssize_t
ds1343_show_alarmmode(struct device
*dev
,
171 struct device_attribute
*attr
, char *buf
)
173 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
174 int alarm_mode
, data
;
177 regmap_read(priv
->map
, DS1343_ALM0_SEC_REG
, &data
);
178 alarm_mode
= (data
& 0x80) >> 4;
180 regmap_read(priv
->map
, DS1343_ALM0_MIN_REG
, &data
);
181 alarm_mode
|= (data
& 0x80) >> 5;
183 regmap_read(priv
->map
, DS1343_ALM0_HOUR_REG
, &data
);
184 alarm_mode
|= (data
& 0x80) >> 6;
186 regmap_read(priv
->map
, DS1343_ALM0_DAY_REG
, &data
);
187 alarm_mode
|= (data
& 0x80) >> 7;
189 switch (alarm_mode
) {
191 alarm_str
= "each second";
195 alarm_str
= "seconds match";
199 alarm_str
= "minutes and seconds match";
203 alarm_str
= "hours, minutes and seconds match";
207 alarm_str
= "day, hours, minutes and seconds match";
211 alarm_str
= "invalid";
215 return sprintf(buf
, "%s\n", alarm_str
);
218 static DEVICE_ATTR(alarm_mode
, S_IRUGO
, ds1343_show_alarmmode
, NULL
);
220 static ssize_t
ds1343_show_tricklecharger(struct device
*dev
,
221 struct device_attribute
*attr
, char *buf
)
223 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
225 char *diodes
= "disabled", *resistors
= " ";
227 regmap_read(priv
->map
, DS1343_TRICKLE_REG
, &data
);
229 if ((data
& 0xf0) == DS1343_TRICKLE_MAGIC
) {
230 switch (data
& 0x0c) {
231 case DS1343_TRICKLE_DS1
:
232 diodes
= "one diode,";
236 diodes
= "no diode,";
240 switch (data
& 0x03) {
241 case DS1343_TRICKLE_1K
:
242 resistors
= "1k Ohm";
245 case DS1343_TRICKLE_2K
:
246 resistors
= "2k Ohm";
249 case DS1343_TRICKLE_4K
:
250 resistors
= "4k Ohm";
259 return sprintf(buf
, "%s %s\n", diodes
, resistors
);
262 static DEVICE_ATTR(trickle_charger
, S_IRUGO
, ds1343_show_tricklecharger
, NULL
);
264 static int ds1343_sysfs_register(struct device
*dev
)
266 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
269 err
= device_create_file(dev
, &dev_attr_glitch_filter
);
273 err
= device_create_file(dev
, &dev_attr_trickle_charger
);
280 err
= device_create_file(dev
, &dev_attr_alarm_mode
);
284 err
= device_create_file(dev
, &dev_attr_alarm_status
);
288 device_remove_file(dev
, &dev_attr_alarm_mode
);
291 device_remove_file(dev
, &dev_attr_trickle_charger
);
294 device_remove_file(dev
, &dev_attr_glitch_filter
);
299 static void ds1343_sysfs_unregister(struct device
*dev
)
301 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
303 device_remove_file(dev
, &dev_attr_glitch_filter
);
304 device_remove_file(dev
, &dev_attr_trickle_charger
);
309 device_remove_file(dev
, &dev_attr_alarm_status
);
310 device_remove_file(dev
, &dev_attr_alarm_mode
);
313 static int ds1343_read_time(struct device
*dev
, struct rtc_time
*dt
)
315 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
316 unsigned char buf
[7];
319 res
= regmap_bulk_read(priv
->map
, DS1343_SECONDS_REG
, buf
, 7);
323 dt
->tm_sec
= bcd2bin(buf
[0]);
324 dt
->tm_min
= bcd2bin(buf
[1]);
325 dt
->tm_hour
= bcd2bin(buf
[2] & 0x3F);
326 dt
->tm_wday
= bcd2bin(buf
[3]) - 1;
327 dt
->tm_mday
= bcd2bin(buf
[4]);
328 dt
->tm_mon
= bcd2bin(buf
[5] & 0x1F) - 1;
329 dt
->tm_year
= bcd2bin(buf
[6]) + 100; /* year offset from 1900 */
331 return rtc_valid_tm(dt
);
334 static int ds1343_set_time(struct device
*dev
, struct rtc_time
*dt
)
336 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
339 res
= regmap_write(priv
->map
, DS1343_SECONDS_REG
,
340 bin2bcd(dt
->tm_sec
));
344 res
= regmap_write(priv
->map
, DS1343_MINUTES_REG
,
345 bin2bcd(dt
->tm_min
));
349 res
= regmap_write(priv
->map
, DS1343_HOURS_REG
,
350 bin2bcd(dt
->tm_hour
) & 0x3F);
354 res
= regmap_write(priv
->map
, DS1343_DAY_REG
,
355 bin2bcd(dt
->tm_wday
+ 1));
359 res
= regmap_write(priv
->map
, DS1343_DATE_REG
,
360 bin2bcd(dt
->tm_mday
));
364 res
= regmap_write(priv
->map
, DS1343_MONTH_REG
,
365 bin2bcd(dt
->tm_mon
+ 1));
371 res
= regmap_write(priv
->map
, DS1343_YEAR_REG
,
372 bin2bcd(dt
->tm_year
));
379 static int ds1343_update_alarm(struct device
*dev
)
381 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
382 unsigned int control
, stat
;
383 unsigned char buf
[4];
386 res
= regmap_read(priv
->map
, DS1343_CONTROL_REG
, &control
);
390 res
= regmap_read(priv
->map
, DS1343_STATUS_REG
, &stat
);
394 control
&= ~(DS1343_A0IE
);
395 stat
&= ~(DS1343_IRQF0
);
397 res
= regmap_write(priv
->map
, DS1343_CONTROL_REG
, control
);
401 res
= regmap_write(priv
->map
, DS1343_STATUS_REG
, stat
);
405 buf
[0] = priv
->alarm_sec
< 0 || (priv
->irqen
& RTC_UF
) ?
406 0x80 : bin2bcd(priv
->alarm_sec
) & 0x7F;
407 buf
[1] = priv
->alarm_min
< 0 || (priv
->irqen
& RTC_UF
) ?
408 0x80 : bin2bcd(priv
->alarm_min
) & 0x7F;
409 buf
[2] = priv
->alarm_hour
< 0 || (priv
->irqen
& RTC_UF
) ?
410 0x80 : bin2bcd(priv
->alarm_hour
) & 0x3F;
411 buf
[3] = priv
->alarm_mday
< 0 || (priv
->irqen
& RTC_UF
) ?
412 0x80 : bin2bcd(priv
->alarm_mday
) & 0x7F;
414 res
= regmap_bulk_write(priv
->map
, DS1343_ALM0_SEC_REG
, buf
, 4);
419 control
|= DS1343_A0IE
;
420 res
= regmap_write(priv
->map
, DS1343_CONTROL_REG
, control
);
426 static int ds1343_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
428 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
435 mutex_lock(&priv
->mutex
);
437 res
= regmap_read(priv
->map
, DS1343_STATUS_REG
, &stat
);
441 alarm
->enabled
= !!(priv
->irqen
& RTC_AF
);
442 alarm
->pending
= !!(stat
& DS1343_IRQF0
);
444 alarm
->time
.tm_sec
= priv
->alarm_sec
< 0 ? 0 : priv
->alarm_sec
;
445 alarm
->time
.tm_min
= priv
->alarm_min
< 0 ? 0 : priv
->alarm_min
;
446 alarm
->time
.tm_hour
= priv
->alarm_hour
< 0 ? 0 : priv
->alarm_hour
;
447 alarm
->time
.tm_mday
= priv
->alarm_mday
< 0 ? 0 : priv
->alarm_mday
;
449 alarm
->time
.tm_mon
= -1;
450 alarm
->time
.tm_year
= -1;
451 alarm
->time
.tm_wday
= -1;
452 alarm
->time
.tm_yday
= -1;
453 alarm
->time
.tm_isdst
= -1;
456 mutex_unlock(&priv
->mutex
);
460 static int ds1343_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
462 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
468 mutex_lock(&priv
->mutex
);
470 priv
->alarm_sec
= alarm
->time
.tm_sec
;
471 priv
->alarm_min
= alarm
->time
.tm_min
;
472 priv
->alarm_hour
= alarm
->time
.tm_hour
;
473 priv
->alarm_mday
= alarm
->time
.tm_mday
;
476 priv
->irqen
|= RTC_AF
;
478 res
= ds1343_update_alarm(dev
);
480 mutex_unlock(&priv
->mutex
);
485 static int ds1343_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
487 struct ds1343_priv
*priv
= dev_get_drvdata(dev
);
493 mutex_lock(&priv
->mutex
);
496 priv
->irqen
|= RTC_AF
;
498 priv
->irqen
&= ~RTC_AF
;
500 res
= ds1343_update_alarm(dev
);
502 mutex_unlock(&priv
->mutex
);
507 static irqreturn_t
ds1343_thread(int irq
, void *dev_id
)
509 struct ds1343_priv
*priv
= dev_id
;
510 unsigned int stat
, control
;
513 mutex_lock(&priv
->mutex
);
515 res
= regmap_read(priv
->map
, DS1343_STATUS_REG
, &stat
);
519 if (stat
& DS1343_IRQF0
) {
520 stat
&= ~DS1343_IRQF0
;
521 regmap_write(priv
->map
, DS1343_STATUS_REG
, stat
);
523 res
= regmap_read(priv
->map
, DS1343_CONTROL_REG
, &control
);
527 control
&= ~DS1343_A0IE
;
528 regmap_write(priv
->map
, DS1343_CONTROL_REG
, control
);
530 rtc_update_irq(priv
->rtc
, 1, RTC_AF
| RTC_IRQF
);
534 mutex_unlock(&priv
->mutex
);
538 static const struct rtc_class_ops ds1343_rtc_ops
= {
539 .ioctl
= ds1343_ioctl
,
540 .read_time
= ds1343_read_time
,
541 .set_time
= ds1343_set_time
,
542 .read_alarm
= ds1343_read_alarm
,
543 .set_alarm
= ds1343_set_alarm
,
544 .alarm_irq_enable
= ds1343_alarm_irq_enable
,
547 static int ds1343_probe(struct spi_device
*spi
)
549 struct ds1343_priv
*priv
;
550 struct regmap_config config
;
554 memset(&config
, 0, sizeof(config
));
557 config
.write_flag_mask
= 0x80;
559 priv
= devm_kzalloc(&spi
->dev
, sizeof(struct ds1343_priv
), GFP_KERNEL
);
564 mutex_init(&priv
->mutex
);
566 /* RTC DS1347 works in spi mode 3 and
567 * its chip select is active high
569 spi
->mode
= SPI_MODE_3
| SPI_CS_HIGH
;
570 spi
->bits_per_word
= 8;
571 res
= spi_setup(spi
);
575 spi_set_drvdata(spi
, priv
);
577 priv
->map
= devm_regmap_init_spi(spi
, &config
);
579 if (IS_ERR(priv
->map
)) {
580 dev_err(&spi
->dev
, "spi regmap init failed for rtc ds1343\n");
581 return PTR_ERR(priv
->map
);
584 res
= regmap_read(priv
->map
, DS1343_SECONDS_REG
, &data
);
588 regmap_read(priv
->map
, DS1343_CONTROL_REG
, &data
);
589 data
|= DS1343_INTCN
;
590 data
&= ~(DS1343_EOSC
| DS1343_A1IE
| DS1343_A0IE
);
591 regmap_write(priv
->map
, DS1343_CONTROL_REG
, data
);
593 regmap_read(priv
->map
, DS1343_STATUS_REG
, &data
);
594 data
&= ~(DS1343_OSF
| DS1343_IRQF1
| DS1343_IRQF0
);
595 regmap_write(priv
->map
, DS1343_STATUS_REG
, data
);
597 priv
->rtc
= devm_rtc_device_register(&spi
->dev
, "ds1343",
598 &ds1343_rtc_ops
, THIS_MODULE
);
599 if (IS_ERR(priv
->rtc
)) {
600 dev_err(&spi
->dev
, "unable to register rtc ds1343\n");
601 return PTR_ERR(priv
->rtc
);
604 priv
->irq
= spi
->irq
;
606 if (priv
->irq
>= 0) {
607 res
= devm_request_threaded_irq(&spi
->dev
, spi
->irq
, NULL
,
609 IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
614 "unable to request irq for rtc ds1343\n");
616 device_set_wakeup_capable(&spi
->dev
, 1);
620 res
= ds1343_sysfs_register(&spi
->dev
);
623 "unable to create sysfs entries for rtc ds1343\n");
628 static int ds1343_remove(struct spi_device
*spi
)
630 struct ds1343_priv
*priv
= spi_get_drvdata(spi
);
633 mutex_lock(&priv
->mutex
);
634 priv
->irqen
&= ~RTC_AF
;
635 mutex_unlock(&priv
->mutex
);
637 devm_free_irq(&spi
->dev
, spi
->irq
, priv
);
640 spi_set_drvdata(spi
, NULL
);
642 ds1343_sysfs_unregister(&spi
->dev
);
647 #ifdef CONFIG_PM_SLEEP
649 static int ds1343_suspend(struct device
*dev
)
651 struct spi_device
*spi
= to_spi_device(dev
);
653 if (spi
->irq
>= 0 && device_may_wakeup(dev
))
654 enable_irq_wake(spi
->irq
);
659 static int ds1343_resume(struct device
*dev
)
661 struct spi_device
*spi
= to_spi_device(dev
);
663 if (spi
->irq
>= 0 && device_may_wakeup(dev
))
664 disable_irq_wake(spi
->irq
);
671 static SIMPLE_DEV_PM_OPS(ds1343_pm
, ds1343_suspend
, ds1343_resume
);
673 static struct spi_driver ds1343_driver
= {
676 .owner
= THIS_MODULE
,
679 .probe
= ds1343_probe
,
680 .remove
= ds1343_remove
,
681 .id_table
= ds1343_id
,
684 module_spi_driver(ds1343_driver
);
686 MODULE_DESCRIPTION("DS1343 RTC SPI Driver");
687 MODULE_AUTHOR("Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>");
688 MODULE_LICENSE("GPL v2");
689 MODULE_VERSION(DS1343_DRV_VERSION
);