Merge branch 'r6040-next'
[linux/fpc-iii.git] / drivers / rtc / rtc-ds1343.c
blob23fa9f0cb5e3113f398a468d7c3a941f0bf45a98
1 /* rtc-ds1343.c
3 * Driver for Dallas Semiconductor DS1343 Low Current, SPI Compatible
4 * Real Time Clock
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>
23 #include <linux/pm.h>
24 #include <linux/pm_wakeirq.h>
25 #include <linux/slab.h>
27 #define DALLAS_MAXIM_DS1343 0
28 #define DALLAS_MAXIM_DS1344 1
30 /* RTC DS1343 Registers */
31 #define DS1343_SECONDS_REG 0x00
32 #define DS1343_MINUTES_REG 0x01
33 #define DS1343_HOURS_REG 0x02
34 #define DS1343_DAY_REG 0x03
35 #define DS1343_DATE_REG 0x04
36 #define DS1343_MONTH_REG 0x05
37 #define DS1343_YEAR_REG 0x06
38 #define DS1343_ALM0_SEC_REG 0x07
39 #define DS1343_ALM0_MIN_REG 0x08
40 #define DS1343_ALM0_HOUR_REG 0x09
41 #define DS1343_ALM0_DAY_REG 0x0A
42 #define DS1343_ALM1_SEC_REG 0x0B
43 #define DS1343_ALM1_MIN_REG 0x0C
44 #define DS1343_ALM1_HOUR_REG 0x0D
45 #define DS1343_ALM1_DAY_REG 0x0E
46 #define DS1343_CONTROL_REG 0x0F
47 #define DS1343_STATUS_REG 0x10
48 #define DS1343_TRICKLE_REG 0x11
49 #define DS1343_NVRAM 0x20
51 #define DS1343_NVRAM_LEN 96
53 /* DS1343 Control Registers bits */
54 #define DS1343_EOSC 0x80
55 #define DS1343_DOSF 0x20
56 #define DS1343_EGFIL 0x10
57 #define DS1343_SQW 0x08
58 #define DS1343_INTCN 0x04
59 #define DS1343_A1IE 0x02
60 #define DS1343_A0IE 0x01
62 /* DS1343 Status Registers bits */
63 #define DS1343_OSF 0x80
64 #define DS1343_IRQF1 0x02
65 #define DS1343_IRQF0 0x01
67 /* DS1343 Trickle Charger Registers bits */
68 #define DS1343_TRICKLE_MAGIC 0xa0
69 #define DS1343_TRICKLE_DS1 0x08
70 #define DS1343_TRICKLE_1K 0x01
71 #define DS1343_TRICKLE_2K 0x02
72 #define DS1343_TRICKLE_4K 0x03
74 static const struct spi_device_id ds1343_id[] = {
75 { "ds1343", DALLAS_MAXIM_DS1343 },
76 { "ds1344", DALLAS_MAXIM_DS1344 },
77 { }
79 MODULE_DEVICE_TABLE(spi, ds1343_id);
81 struct ds1343_priv {
82 struct spi_device *spi;
83 struct rtc_device *rtc;
84 struct regmap *map;
85 struct mutex mutex;
86 unsigned int irqen;
87 int irq;
88 int alarm_sec;
89 int alarm_min;
90 int alarm_hour;
91 int alarm_mday;
94 static int ds1343_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
96 switch (cmd) {
97 #ifdef RTC_SET_CHARGE
98 case RTC_SET_CHARGE:
100 int val;
102 if (copy_from_user(&val, (int __user *)arg, sizeof(int)))
103 return -EFAULT;
105 return regmap_write(priv->map, DS1343_TRICKLE_REG, val);
107 break;
108 #endif
111 return -ENOIOCTLCMD;
114 static ssize_t ds1343_show_glitchfilter(struct device *dev,
115 struct device_attribute *attr, char *buf)
117 struct ds1343_priv *priv = dev_get_drvdata(dev);
118 int glitch_filt_status, data;
120 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
122 glitch_filt_status = !!(data & DS1343_EGFIL);
124 if (glitch_filt_status)
125 return sprintf(buf, "enabled\n");
126 else
127 return sprintf(buf, "disabled\n");
130 static ssize_t ds1343_store_glitchfilter(struct device *dev,
131 struct device_attribute *attr,
132 const char *buf, size_t count)
134 struct ds1343_priv *priv = dev_get_drvdata(dev);
135 int data;
137 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
139 if (strncmp(buf, "enabled", 7) == 0)
140 data |= DS1343_EGFIL;
142 else if (strncmp(buf, "disabled", 8) == 0)
143 data &= ~(DS1343_EGFIL);
145 else
146 return -EINVAL;
148 regmap_write(priv->map, DS1343_CONTROL_REG, data);
150 return count;
153 static DEVICE_ATTR(glitch_filter, S_IRUGO | S_IWUSR, ds1343_show_glitchfilter,
154 ds1343_store_glitchfilter);
156 static ssize_t ds1343_nvram_write(struct file *filp, struct kobject *kobj,
157 struct bin_attribute *attr,
158 char *buf, loff_t off, size_t count)
160 int ret;
161 unsigned char address;
162 struct device *dev = kobj_to_dev(kobj);
163 struct ds1343_priv *priv = dev_get_drvdata(dev);
165 address = DS1343_NVRAM + off;
167 ret = regmap_bulk_write(priv->map, address, buf, count);
168 if (ret < 0)
169 dev_err(&priv->spi->dev, "Error in nvram write %d", ret);
171 return (ret < 0) ? ret : count;
175 static ssize_t ds1343_nvram_read(struct file *filp, struct kobject *kobj,
176 struct bin_attribute *attr,
177 char *buf, loff_t off, size_t count)
179 int ret;
180 unsigned char address;
181 struct device *dev = kobj_to_dev(kobj);
182 struct ds1343_priv *priv = dev_get_drvdata(dev);
184 address = DS1343_NVRAM + off;
186 ret = regmap_bulk_read(priv->map, address, buf, count);
187 if (ret < 0)
188 dev_err(&priv->spi->dev, "Error in nvram read %d\n", ret);
190 return (ret < 0) ? ret : count;
194 static struct bin_attribute nvram_attr = {
195 .attr.name = "nvram",
196 .attr.mode = S_IRUGO | S_IWUSR,
197 .read = ds1343_nvram_read,
198 .write = ds1343_nvram_write,
199 .size = DS1343_NVRAM_LEN,
202 static ssize_t ds1343_show_alarmstatus(struct device *dev,
203 struct device_attribute *attr, char *buf)
205 struct ds1343_priv *priv = dev_get_drvdata(dev);
206 int alarmstatus, data;
208 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
210 alarmstatus = !!(data & DS1343_A0IE);
212 if (alarmstatus)
213 return sprintf(buf, "enabled\n");
214 else
215 return sprintf(buf, "disabled\n");
218 static DEVICE_ATTR(alarm_status, S_IRUGO, ds1343_show_alarmstatus, NULL);
220 static ssize_t ds1343_show_alarmmode(struct device *dev,
221 struct device_attribute *attr, char *buf)
223 struct ds1343_priv *priv = dev_get_drvdata(dev);
224 int alarm_mode, data;
225 char *alarm_str;
227 regmap_read(priv->map, DS1343_ALM0_SEC_REG, &data);
228 alarm_mode = (data & 0x80) >> 4;
230 regmap_read(priv->map, DS1343_ALM0_MIN_REG, &data);
231 alarm_mode |= (data & 0x80) >> 5;
233 regmap_read(priv->map, DS1343_ALM0_HOUR_REG, &data);
234 alarm_mode |= (data & 0x80) >> 6;
236 regmap_read(priv->map, DS1343_ALM0_DAY_REG, &data);
237 alarm_mode |= (data & 0x80) >> 7;
239 switch (alarm_mode) {
240 case 15:
241 alarm_str = "each second";
242 break;
244 case 7:
245 alarm_str = "seconds match";
246 break;
248 case 3:
249 alarm_str = "minutes and seconds match";
250 break;
252 case 1:
253 alarm_str = "hours, minutes and seconds match";
254 break;
256 case 0:
257 alarm_str = "day, hours, minutes and seconds match";
258 break;
260 default:
261 alarm_str = "invalid";
262 break;
265 return sprintf(buf, "%s\n", alarm_str);
268 static DEVICE_ATTR(alarm_mode, S_IRUGO, ds1343_show_alarmmode, NULL);
270 static ssize_t ds1343_show_tricklecharger(struct device *dev,
271 struct device_attribute *attr, char *buf)
273 struct ds1343_priv *priv = dev_get_drvdata(dev);
274 int data;
275 char *diodes = "disabled", *resistors = " ";
277 regmap_read(priv->map, DS1343_TRICKLE_REG, &data);
279 if ((data & 0xf0) == DS1343_TRICKLE_MAGIC) {
280 switch (data & 0x0c) {
281 case DS1343_TRICKLE_DS1:
282 diodes = "one diode,";
283 break;
285 default:
286 diodes = "no diode,";
287 break;
290 switch (data & 0x03) {
291 case DS1343_TRICKLE_1K:
292 resistors = "1k Ohm";
293 break;
295 case DS1343_TRICKLE_2K:
296 resistors = "2k Ohm";
297 break;
299 case DS1343_TRICKLE_4K:
300 resistors = "4k Ohm";
301 break;
303 default:
304 diodes = "disabled";
305 break;
309 return sprintf(buf, "%s %s\n", diodes, resistors);
312 static DEVICE_ATTR(trickle_charger, S_IRUGO, ds1343_show_tricklecharger, NULL);
314 static int ds1343_sysfs_register(struct device *dev)
316 struct ds1343_priv *priv = dev_get_drvdata(dev);
317 int err;
319 err = device_create_file(dev, &dev_attr_glitch_filter);
320 if (err)
321 return err;
323 err = device_create_file(dev, &dev_attr_trickle_charger);
324 if (err)
325 goto error1;
327 err = device_create_bin_file(dev, &nvram_attr);
328 if (err)
329 goto error2;
331 if (priv->irq <= 0)
332 return err;
334 err = device_create_file(dev, &dev_attr_alarm_mode);
335 if (err)
336 goto error3;
338 err = device_create_file(dev, &dev_attr_alarm_status);
339 if (!err)
340 return err;
342 device_remove_file(dev, &dev_attr_alarm_mode);
344 error3:
345 device_remove_bin_file(dev, &nvram_attr);
347 error2:
348 device_remove_file(dev, &dev_attr_trickle_charger);
350 error1:
351 device_remove_file(dev, &dev_attr_glitch_filter);
353 return err;
356 static void ds1343_sysfs_unregister(struct device *dev)
358 struct ds1343_priv *priv = dev_get_drvdata(dev);
360 device_remove_file(dev, &dev_attr_glitch_filter);
361 device_remove_file(dev, &dev_attr_trickle_charger);
362 device_remove_bin_file(dev, &nvram_attr);
364 if (priv->irq <= 0)
365 return;
367 device_remove_file(dev, &dev_attr_alarm_status);
368 device_remove_file(dev, &dev_attr_alarm_mode);
371 static int ds1343_read_time(struct device *dev, struct rtc_time *dt)
373 struct ds1343_priv *priv = dev_get_drvdata(dev);
374 unsigned char buf[7];
375 int res;
377 res = regmap_bulk_read(priv->map, DS1343_SECONDS_REG, buf, 7);
378 if (res)
379 return res;
381 dt->tm_sec = bcd2bin(buf[0]);
382 dt->tm_min = bcd2bin(buf[1]);
383 dt->tm_hour = bcd2bin(buf[2] & 0x3F);
384 dt->tm_wday = bcd2bin(buf[3]) - 1;
385 dt->tm_mday = bcd2bin(buf[4]);
386 dt->tm_mon = bcd2bin(buf[5] & 0x1F) - 1;
387 dt->tm_year = bcd2bin(buf[6]) + 100; /* year offset from 1900 */
389 return rtc_valid_tm(dt);
392 static int ds1343_set_time(struct device *dev, struct rtc_time *dt)
394 struct ds1343_priv *priv = dev_get_drvdata(dev);
395 int res;
397 res = regmap_write(priv->map, DS1343_SECONDS_REG,
398 bin2bcd(dt->tm_sec));
399 if (res)
400 return res;
402 res = regmap_write(priv->map, DS1343_MINUTES_REG,
403 bin2bcd(dt->tm_min));
404 if (res)
405 return res;
407 res = regmap_write(priv->map, DS1343_HOURS_REG,
408 bin2bcd(dt->tm_hour) & 0x3F);
409 if (res)
410 return res;
412 res = regmap_write(priv->map, DS1343_DAY_REG,
413 bin2bcd(dt->tm_wday + 1));
414 if (res)
415 return res;
417 res = regmap_write(priv->map, DS1343_DATE_REG,
418 bin2bcd(dt->tm_mday));
419 if (res)
420 return res;
422 res = regmap_write(priv->map, DS1343_MONTH_REG,
423 bin2bcd(dt->tm_mon + 1));
424 if (res)
425 return res;
427 dt->tm_year %= 100;
429 res = regmap_write(priv->map, DS1343_YEAR_REG,
430 bin2bcd(dt->tm_year));
431 if (res)
432 return res;
434 return 0;
437 static int ds1343_update_alarm(struct device *dev)
439 struct ds1343_priv *priv = dev_get_drvdata(dev);
440 unsigned int control, stat;
441 unsigned char buf[4];
442 int res = 0;
444 res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
445 if (res)
446 return res;
448 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
449 if (res)
450 return res;
452 control &= ~(DS1343_A0IE);
453 stat &= ~(DS1343_IRQF0);
455 res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
456 if (res)
457 return res;
459 res = regmap_write(priv->map, DS1343_STATUS_REG, stat);
460 if (res)
461 return res;
463 buf[0] = priv->alarm_sec < 0 || (priv->irqen & RTC_UF) ?
464 0x80 : bin2bcd(priv->alarm_sec) & 0x7F;
465 buf[1] = priv->alarm_min < 0 || (priv->irqen & RTC_UF) ?
466 0x80 : bin2bcd(priv->alarm_min) & 0x7F;
467 buf[2] = priv->alarm_hour < 0 || (priv->irqen & RTC_UF) ?
468 0x80 : bin2bcd(priv->alarm_hour) & 0x3F;
469 buf[3] = priv->alarm_mday < 0 || (priv->irqen & RTC_UF) ?
470 0x80 : bin2bcd(priv->alarm_mday) & 0x7F;
472 res = regmap_bulk_write(priv->map, DS1343_ALM0_SEC_REG, buf, 4);
473 if (res)
474 return res;
476 if (priv->irqen) {
477 control |= DS1343_A0IE;
478 res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
481 return res;
484 static int ds1343_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
486 struct ds1343_priv *priv = dev_get_drvdata(dev);
487 int res = 0;
488 unsigned int stat;
490 if (priv->irq <= 0)
491 return -EINVAL;
493 mutex_lock(&priv->mutex);
495 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
496 if (res)
497 goto out;
499 alarm->enabled = !!(priv->irqen & RTC_AF);
500 alarm->pending = !!(stat & DS1343_IRQF0);
502 alarm->time.tm_sec = priv->alarm_sec < 0 ? 0 : priv->alarm_sec;
503 alarm->time.tm_min = priv->alarm_min < 0 ? 0 : priv->alarm_min;
504 alarm->time.tm_hour = priv->alarm_hour < 0 ? 0 : priv->alarm_hour;
505 alarm->time.tm_mday = priv->alarm_mday < 0 ? 0 : priv->alarm_mday;
507 alarm->time.tm_mon = -1;
508 alarm->time.tm_year = -1;
509 alarm->time.tm_wday = -1;
510 alarm->time.tm_yday = -1;
511 alarm->time.tm_isdst = -1;
513 out:
514 mutex_unlock(&priv->mutex);
515 return res;
518 static int ds1343_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
520 struct ds1343_priv *priv = dev_get_drvdata(dev);
521 int res = 0;
523 if (priv->irq <= 0)
524 return -EINVAL;
526 mutex_lock(&priv->mutex);
528 priv->alarm_sec = alarm->time.tm_sec;
529 priv->alarm_min = alarm->time.tm_min;
530 priv->alarm_hour = alarm->time.tm_hour;
531 priv->alarm_mday = alarm->time.tm_mday;
533 if (alarm->enabled)
534 priv->irqen |= RTC_AF;
536 res = ds1343_update_alarm(dev);
538 mutex_unlock(&priv->mutex);
540 return res;
543 static int ds1343_alarm_irq_enable(struct device *dev, unsigned int enabled)
545 struct ds1343_priv *priv = dev_get_drvdata(dev);
546 int res = 0;
548 if (priv->irq <= 0)
549 return -EINVAL;
551 mutex_lock(&priv->mutex);
553 if (enabled)
554 priv->irqen |= RTC_AF;
555 else
556 priv->irqen &= ~RTC_AF;
558 res = ds1343_update_alarm(dev);
560 mutex_unlock(&priv->mutex);
562 return res;
565 static irqreturn_t ds1343_thread(int irq, void *dev_id)
567 struct ds1343_priv *priv = dev_id;
568 unsigned int stat, control;
569 int res = 0;
571 mutex_lock(&priv->mutex);
573 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
574 if (res)
575 goto out;
577 if (stat & DS1343_IRQF0) {
578 stat &= ~DS1343_IRQF0;
579 regmap_write(priv->map, DS1343_STATUS_REG, stat);
581 res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
582 if (res)
583 goto out;
585 control &= ~DS1343_A0IE;
586 regmap_write(priv->map, DS1343_CONTROL_REG, control);
588 rtc_update_irq(priv->rtc, 1, RTC_AF | RTC_IRQF);
591 out:
592 mutex_unlock(&priv->mutex);
593 return IRQ_HANDLED;
596 static const struct rtc_class_ops ds1343_rtc_ops = {
597 .ioctl = ds1343_ioctl,
598 .read_time = ds1343_read_time,
599 .set_time = ds1343_set_time,
600 .read_alarm = ds1343_read_alarm,
601 .set_alarm = ds1343_set_alarm,
602 .alarm_irq_enable = ds1343_alarm_irq_enable,
605 static int ds1343_probe(struct spi_device *spi)
607 struct ds1343_priv *priv;
608 struct regmap_config config;
609 unsigned int data;
610 int res;
612 memset(&config, 0, sizeof(config));
613 config.reg_bits = 8;
614 config.val_bits = 8;
615 config.write_flag_mask = 0x80;
617 priv = devm_kzalloc(&spi->dev, sizeof(struct ds1343_priv), GFP_KERNEL);
618 if (!priv)
619 return -ENOMEM;
621 priv->spi = spi;
622 mutex_init(&priv->mutex);
624 /* RTC DS1347 works in spi mode 3 and
625 * its chip select is active high
627 spi->mode = SPI_MODE_3 | SPI_CS_HIGH;
628 spi->bits_per_word = 8;
629 res = spi_setup(spi);
630 if (res)
631 return res;
633 spi_set_drvdata(spi, priv);
635 priv->map = devm_regmap_init_spi(spi, &config);
637 if (IS_ERR(priv->map)) {
638 dev_err(&spi->dev, "spi regmap init failed for rtc ds1343\n");
639 return PTR_ERR(priv->map);
642 res = regmap_read(priv->map, DS1343_SECONDS_REG, &data);
643 if (res)
644 return res;
646 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
647 data |= DS1343_INTCN;
648 data &= ~(DS1343_EOSC | DS1343_A1IE | DS1343_A0IE);
649 regmap_write(priv->map, DS1343_CONTROL_REG, data);
651 regmap_read(priv->map, DS1343_STATUS_REG, &data);
652 data &= ~(DS1343_OSF | DS1343_IRQF1 | DS1343_IRQF0);
653 regmap_write(priv->map, DS1343_STATUS_REG, data);
655 priv->rtc = devm_rtc_device_register(&spi->dev, "ds1343",
656 &ds1343_rtc_ops, THIS_MODULE);
657 if (IS_ERR(priv->rtc)) {
658 dev_err(&spi->dev, "unable to register rtc ds1343\n");
659 return PTR_ERR(priv->rtc);
662 priv->irq = spi->irq;
664 if (priv->irq >= 0) {
665 res = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,
666 ds1343_thread, IRQF_ONESHOT,
667 "ds1343", priv);
668 if (res) {
669 priv->irq = -1;
670 dev_err(&spi->dev,
671 "unable to request irq for rtc ds1343\n");
672 } else {
673 device_init_wakeup(&spi->dev, true);
674 dev_pm_set_wake_irq(&spi->dev, spi->irq);
678 res = ds1343_sysfs_register(&spi->dev);
679 if (res)
680 dev_err(&spi->dev,
681 "unable to create sysfs entries for rtc ds1343\n");
683 return 0;
686 static int ds1343_remove(struct spi_device *spi)
688 struct ds1343_priv *priv = spi_get_drvdata(spi);
690 if (spi->irq) {
691 mutex_lock(&priv->mutex);
692 priv->irqen &= ~RTC_AF;
693 mutex_unlock(&priv->mutex);
695 dev_pm_clear_wake_irq(&spi->dev);
696 device_init_wakeup(&spi->dev, false);
697 devm_free_irq(&spi->dev, spi->irq, priv);
700 spi_set_drvdata(spi, NULL);
702 ds1343_sysfs_unregister(&spi->dev);
704 return 0;
707 #ifdef CONFIG_PM_SLEEP
709 static int ds1343_suspend(struct device *dev)
711 struct spi_device *spi = to_spi_device(dev);
713 if (spi->irq >= 0 && device_may_wakeup(dev))
714 enable_irq_wake(spi->irq);
716 return 0;
719 static int ds1343_resume(struct device *dev)
721 struct spi_device *spi = to_spi_device(dev);
723 if (spi->irq >= 0 && device_may_wakeup(dev))
724 disable_irq_wake(spi->irq);
726 return 0;
729 #endif
731 static SIMPLE_DEV_PM_OPS(ds1343_pm, ds1343_suspend, ds1343_resume);
733 static struct spi_driver ds1343_driver = {
734 .driver = {
735 .name = "ds1343",
736 .pm = &ds1343_pm,
738 .probe = ds1343_probe,
739 .remove = ds1343_remove,
740 .id_table = ds1343_id,
743 module_spi_driver(ds1343_driver);
745 MODULE_DESCRIPTION("DS1343 RTC SPI Driver");
746 MODULE_AUTHOR("Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>,"
747 "Ankur Srivastava <sankurece@gmail.com>");
748 MODULE_LICENSE("GPL v2");