khugepaged: ignore pmd tables with THP mapped with ptes
[linux/fpc-iii.git] / drivers / rtc / rtc-ds1343.c
blob3d389bd8a289590561d45a0dc3a82697fd659bf4
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 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 },
78 { }
80 MODULE_DEVICE_TABLE(spi, ds1343_id);
82 struct ds1343_priv {
83 struct spi_device *spi;
84 struct rtc_device *rtc;
85 struct regmap *map;
86 struct mutex mutex;
87 unsigned int irqen;
88 int irq;
89 int alarm_sec;
90 int alarm_min;
91 int alarm_hour;
92 int alarm_mday;
95 static int ds1343_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
97 switch (cmd) {
98 #ifdef RTC_SET_CHARGE
99 case RTC_SET_CHARGE:
101 int val;
103 if (copy_from_user(&val, (int __user *)arg, sizeof(int)))
104 return -EFAULT;
106 return regmap_write(priv->map, DS1343_TRICKLE_REG, val);
108 break;
109 #endif
112 return -ENOIOCTLCMD;
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");
127 else
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);
136 int data;
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);
146 else
147 return -EINVAL;
149 regmap_write(priv->map, DS1343_CONTROL_REG, data);
151 return count;
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)
161 int ret;
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);
169 if (ret < 0)
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)
180 int ret;
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);
188 if (ret < 0)
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);
213 if (alarmstatus)
214 return sprintf(buf, "enabled\n");
215 else
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;
226 char *alarm_str;
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) {
241 case 15:
242 alarm_str = "each second";
243 break;
245 case 7:
246 alarm_str = "seconds match";
247 break;
249 case 3:
250 alarm_str = "minutes and seconds match";
251 break;
253 case 1:
254 alarm_str = "hours, minutes and seconds match";
255 break;
257 case 0:
258 alarm_str = "day, hours, minutes and seconds match";
259 break;
261 default:
262 alarm_str = "invalid";
263 break;
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);
275 int data;
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,";
284 break;
286 default:
287 diodes = "no diode,";
288 break;
291 switch (data & 0x03) {
292 case DS1343_TRICKLE_1K:
293 resistors = "1k Ohm";
294 break;
296 case DS1343_TRICKLE_2K:
297 resistors = "2k Ohm";
298 break;
300 case DS1343_TRICKLE_4K:
301 resistors = "4k Ohm";
302 break;
304 default:
305 diodes = "disabled";
306 break;
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);
318 int err;
320 err = device_create_file(dev, &dev_attr_glitch_filter);
321 if (err)
322 return err;
324 err = device_create_file(dev, &dev_attr_trickle_charger);
325 if (err)
326 goto error1;
328 err = device_create_bin_file(dev, &nvram_attr);
329 if (err)
330 goto error2;
332 if (priv->irq <= 0)
333 return err;
335 err = device_create_file(dev, &dev_attr_alarm_mode);
336 if (err)
337 goto error3;
339 err = device_create_file(dev, &dev_attr_alarm_status);
340 if (!err)
341 return err;
343 device_remove_file(dev, &dev_attr_alarm_mode);
345 error3:
346 device_remove_bin_file(dev, &nvram_attr);
348 error2:
349 device_remove_file(dev, &dev_attr_trickle_charger);
351 error1:
352 device_remove_file(dev, &dev_attr_glitch_filter);
354 return err;
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);
365 if (priv->irq <= 0)
366 return;
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];
376 int res;
378 res = regmap_bulk_read(priv->map, DS1343_SECONDS_REG, buf, 7);
379 if (res)
380 return res;
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);
396 int res;
398 res = regmap_write(priv->map, DS1343_SECONDS_REG,
399 bin2bcd(dt->tm_sec));
400 if (res)
401 return res;
403 res = regmap_write(priv->map, DS1343_MINUTES_REG,
404 bin2bcd(dt->tm_min));
405 if (res)
406 return res;
408 res = regmap_write(priv->map, DS1343_HOURS_REG,
409 bin2bcd(dt->tm_hour) & 0x3F);
410 if (res)
411 return res;
413 res = regmap_write(priv->map, DS1343_DAY_REG,
414 bin2bcd(dt->tm_wday + 1));
415 if (res)
416 return res;
418 res = regmap_write(priv->map, DS1343_DATE_REG,
419 bin2bcd(dt->tm_mday));
420 if (res)
421 return res;
423 res = regmap_write(priv->map, DS1343_MONTH_REG,
424 bin2bcd(dt->tm_mon + 1));
425 if (res)
426 return res;
428 dt->tm_year %= 100;
430 res = regmap_write(priv->map, DS1343_YEAR_REG,
431 bin2bcd(dt->tm_year));
432 if (res)
433 return res;
435 return 0;
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];
443 int res = 0;
445 res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
446 if (res)
447 return res;
449 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
450 if (res)
451 return res;
453 control &= ~(DS1343_A0IE);
454 stat &= ~(DS1343_IRQF0);
456 res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
457 if (res)
458 return res;
460 res = regmap_write(priv->map, DS1343_STATUS_REG, stat);
461 if (res)
462 return res;
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);
474 if (res)
475 return res;
477 if (priv->irqen) {
478 control |= DS1343_A0IE;
479 res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
482 return res;
485 static int ds1343_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
487 struct ds1343_priv *priv = dev_get_drvdata(dev);
488 int res = 0;
489 unsigned int stat;
491 if (priv->irq <= 0)
492 return -EINVAL;
494 mutex_lock(&priv->mutex);
496 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
497 if (res)
498 goto out;
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;
514 out:
515 mutex_unlock(&priv->mutex);
516 return res;
519 static int ds1343_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
521 struct ds1343_priv *priv = dev_get_drvdata(dev);
522 int res = 0;
524 if (priv->irq <= 0)
525 return -EINVAL;
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;
534 if (alarm->enabled)
535 priv->irqen |= RTC_AF;
537 res = ds1343_update_alarm(dev);
539 mutex_unlock(&priv->mutex);
541 return res;
544 static int ds1343_alarm_irq_enable(struct device *dev, unsigned int enabled)
546 struct ds1343_priv *priv = dev_get_drvdata(dev);
547 int res = 0;
549 if (priv->irq <= 0)
550 return -EINVAL;
552 mutex_lock(&priv->mutex);
554 if (enabled)
555 priv->irqen |= RTC_AF;
556 else
557 priv->irqen &= ~RTC_AF;
559 res = ds1343_update_alarm(dev);
561 mutex_unlock(&priv->mutex);
563 return res;
566 static irqreturn_t ds1343_thread(int irq, void *dev_id)
568 struct ds1343_priv *priv = dev_id;
569 unsigned int stat, control;
570 int res = 0;
572 mutex_lock(&priv->mutex);
574 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
575 if (res)
576 goto out;
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);
583 if (res)
584 goto out;
586 control &= ~DS1343_A0IE;
587 regmap_write(priv->map, DS1343_CONTROL_REG, control);
589 rtc_update_irq(priv->rtc, 1, RTC_AF | RTC_IRQF);
592 out:
593 mutex_unlock(&priv->mutex);
594 return IRQ_HANDLED;
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;
610 unsigned int data;
611 int res;
613 memset(&config, 0, sizeof(config));
614 config.reg_bits = 8;
615 config.val_bits = 8;
616 config.write_flag_mask = 0x80;
618 priv = devm_kzalloc(&spi->dev, sizeof(struct ds1343_priv), GFP_KERNEL);
619 if (!priv)
620 return -ENOMEM;
622 priv->spi = spi;
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);
631 if (res)
632 return res;
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);
644 if (res)
645 return res;
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,
668 "ds1343", priv);
669 if (res) {
670 priv->irq = -1;
671 dev_err(&spi->dev,
672 "unable to request irq for rtc ds1343\n");
673 } else {
674 device_init_wakeup(&spi->dev, true);
675 dev_pm_set_wake_irq(&spi->dev, spi->irq);
679 res = ds1343_sysfs_register(&spi->dev);
680 if (res)
681 dev_err(&spi->dev,
682 "unable to create sysfs entries for rtc ds1343\n");
684 return 0;
687 static int ds1343_remove(struct spi_device *spi)
689 struct ds1343_priv *priv = spi_get_drvdata(spi);
691 if (spi->irq) {
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);
705 return 0;
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);
717 return 0;
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);
727 return 0;
730 #endif
732 static SIMPLE_DEV_PM_OPS(ds1343_pm, ds1343_suspend, ds1343_resume);
734 static struct spi_driver ds1343_driver = {
735 .driver = {
736 .name = "ds1343",
737 .pm = &ds1343_pm,
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);