diff options
Diffstat (limited to 'drivers/rtc')
-rw-r--r-- | drivers/rtc/Kconfig | 29 | ||||
-rw-r--r-- | drivers/rtc/Makefile | 3 | ||||
-rw-r--r-- | drivers/rtc/interface.c | 2 | ||||
-rw-r--r-- | drivers/rtc/rtc-cmos.c | 2 | ||||
-rw-r--r-- | drivers/rtc/rtc-ds1390.c | 220 | ||||
-rw-r--r-- | drivers/rtc/rtc-rx8581.c | 281 | ||||
-rw-r--r-- | drivers/rtc/rtc-sun4v.c | 69 | ||||
-rw-r--r-- | drivers/rtc/rtc-wm8350.c | 514 |
8 files changed, 1068 insertions, 52 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 8abbb2020af9..123092d8a984 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig | |||
@@ -277,6 +277,14 @@ config RTC_DRV_FM3130 | |||
277 | This driver can also be built as a module. If so the module | 277 | This driver can also be built as a module. If so the module |
278 | will be called rtc-fm3130. | 278 | will be called rtc-fm3130. |
279 | 279 | ||
280 | config RTC_DRV_RX8581 | ||
281 | tristate "Epson RX-8581" | ||
282 | help | ||
283 | If you say yes here you will get support for the Epson RX-8581. | ||
284 | |||
285 | This driver can also be built as a module. If so the module | ||
286 | will be called rtc-rx8581. | ||
287 | |||
280 | endif # I2C | 288 | endif # I2C |
281 | 289 | ||
282 | comment "SPI RTC drivers" | 290 | comment "SPI RTC drivers" |
@@ -302,6 +310,17 @@ config RTC_DRV_DS1305 | |||
302 | This driver can also be built as a module. If so, the module | 310 | This driver can also be built as a module. If so, the module |
303 | will be called rtc-ds1305. | 311 | will be called rtc-ds1305. |
304 | 312 | ||
313 | config RTC_DRV_DS1390 | ||
314 | tristate "Dallas/Maxim DS1390/93/94" | ||
315 | help | ||
316 | If you say yes here you get support for the DS1390/93/94 chips. | ||
317 | |||
318 | This driver only supports the RTC feature, and not other chip | ||
319 | features such as alarms and trickle charging. | ||
320 | |||
321 | This driver can also be built as a module. If so, the module | ||
322 | will be called rtc-ds1390. | ||
323 | |||
305 | config RTC_DRV_MAX6902 | 324 | config RTC_DRV_MAX6902 |
306 | tristate "Maxim MAX6902" | 325 | tristate "Maxim MAX6902" |
307 | help | 326 | help |
@@ -468,6 +487,16 @@ config RTC_DRV_V3020 | |||
468 | This driver can also be built as a module. If so, the module | 487 | This driver can also be built as a module. If so, the module |
469 | will be called rtc-v3020. | 488 | will be called rtc-v3020. |
470 | 489 | ||
490 | config RTC_DRV_WM8350 | ||
491 | tristate "Wolfson Microelectronics WM8350 RTC" | ||
492 | depends on MFD_WM8350 | ||
493 | help | ||
494 | If you say yes here you will get support for the RTC subsystem | ||
495 | of the Wolfson Microelectronics WM8350. | ||
496 | |||
497 | This driver can also be built as a module. If so, the module | ||
498 | will be called "rtc-wm8350". | ||
499 | |||
471 | comment "on-CPU RTC drivers" | 500 | comment "on-CPU RTC drivers" |
472 | 501 | ||
473 | config RTC_DRV_OMAP | 502 | config RTC_DRV_OMAP |
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index e9e8474cc8fe..6e79c912bf9e 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile | |||
@@ -28,6 +28,7 @@ obj-$(CONFIG_RTC_DRV_DS1302) += rtc-ds1302.o | |||
28 | obj-$(CONFIG_RTC_DRV_DS1305) += rtc-ds1305.o | 28 | obj-$(CONFIG_RTC_DRV_DS1305) += rtc-ds1305.o |
29 | obj-$(CONFIG_RTC_DRV_DS1307) += rtc-ds1307.o | 29 | obj-$(CONFIG_RTC_DRV_DS1307) += rtc-ds1307.o |
30 | obj-$(CONFIG_RTC_DRV_DS1374) += rtc-ds1374.o | 30 | obj-$(CONFIG_RTC_DRV_DS1374) += rtc-ds1374.o |
31 | obj-$(CONFIG_RTC_DRV_DS1390) += rtc-ds1390.o | ||
31 | obj-$(CONFIG_RTC_DRV_DS1511) += rtc-ds1511.o | 32 | obj-$(CONFIG_RTC_DRV_DS1511) += rtc-ds1511.o |
32 | obj-$(CONFIG_RTC_DRV_DS1553) += rtc-ds1553.o | 33 | obj-$(CONFIG_RTC_DRV_DS1553) += rtc-ds1553.o |
33 | obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o | 34 | obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o |
@@ -57,6 +58,7 @@ obj-$(CONFIG_RTC_DRV_R9701) += rtc-r9701.o | |||
57 | obj-$(CONFIG_RTC_DRV_RS5C313) += rtc-rs5c313.o | 58 | obj-$(CONFIG_RTC_DRV_RS5C313) += rtc-rs5c313.o |
58 | obj-$(CONFIG_RTC_DRV_RS5C348) += rtc-rs5c348.o | 59 | obj-$(CONFIG_RTC_DRV_RS5C348) += rtc-rs5c348.o |
59 | obj-$(CONFIG_RTC_DRV_RS5C372) += rtc-rs5c372.o | 60 | obj-$(CONFIG_RTC_DRV_RS5C372) += rtc-rs5c372.o |
61 | obj-$(CONFIG_RTC_DRV_RX8581) += rtc-rx8581.o | ||
60 | obj-$(CONFIG_RTC_DRV_S35390A) += rtc-s35390a.o | 62 | obj-$(CONFIG_RTC_DRV_S35390A) += rtc-s35390a.o |
61 | obj-$(CONFIG_RTC_DRV_S3C) += rtc-s3c.o | 63 | obj-$(CONFIG_RTC_DRV_S3C) += rtc-s3c.o |
62 | obj-$(CONFIG_RTC_DRV_SA1100) += rtc-sa1100.o | 64 | obj-$(CONFIG_RTC_DRV_SA1100) += rtc-sa1100.o |
@@ -66,4 +68,5 @@ obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o | |||
66 | obj-$(CONFIG_RTC_DRV_TWL4030) += rtc-twl4030.o | 68 | obj-$(CONFIG_RTC_DRV_TWL4030) += rtc-twl4030.o |
67 | obj-$(CONFIG_RTC_DRV_V3020) += rtc-v3020.o | 69 | obj-$(CONFIG_RTC_DRV_V3020) += rtc-v3020.o |
68 | obj-$(CONFIG_RTC_DRV_VR41XX) += rtc-vr41xx.o | 70 | obj-$(CONFIG_RTC_DRV_VR41XX) += rtc-vr41xx.o |
71 | obj-$(CONFIG_RTC_DRV_WM8350) += rtc-wm8350.o | ||
69 | obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o | 72 | obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o |
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c index 7af60b98d8a4..a04c1b6b1575 100644 --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c | |||
@@ -271,7 +271,7 @@ int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) | |||
271 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year"); | 271 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year"); |
272 | do { | 272 | do { |
273 | alarm->time.tm_year++; | 273 | alarm->time.tm_year++; |
274 | } while (!rtc_valid_tm(&alarm->time)); | 274 | } while (rtc_valid_tm(&alarm->time) != 0); |
275 | break; | 275 | break; |
276 | 276 | ||
277 | default: | 277 | default: |
diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c index 5549231179a2..6cf8e282338f 100644 --- a/drivers/rtc/rtc-cmos.c +++ b/drivers/rtc/rtc-cmos.c | |||
@@ -794,7 +794,7 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq) | |||
794 | goto cleanup2; | 794 | goto cleanup2; |
795 | } | 795 | } |
796 | 796 | ||
797 | pr_info("%s: alarms up to one %s%s, %zd bytes nvram, %s irqs\n", | 797 | pr_info("%s: alarms up to one %s%s, %zd bytes nvram%s\n", |
798 | cmos_rtc.rtc->dev.bus_id, | 798 | cmos_rtc.rtc->dev.bus_id, |
799 | is_valid_irq(rtc_irq) | 799 | is_valid_irq(rtc_irq) |
800 | ? (cmos_rtc.mon_alrm | 800 | ? (cmos_rtc.mon_alrm |
diff --git a/drivers/rtc/rtc-ds1390.c b/drivers/rtc/rtc-ds1390.c new file mode 100644 index 000000000000..599e976bf014 --- /dev/null +++ b/drivers/rtc/rtc-ds1390.c | |||
@@ -0,0 +1,220 @@ | |||
1 | /* | ||
2 | * rtc-ds1390.c -- driver for DS1390/93/94 | ||
3 | * | ||
4 | * Copyright (C) 2008 Mercury IMC Ltd | ||
5 | * Written by Mark Jackson <mpfj@mimc.co.uk> | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License version 2 as | ||
9 | * published by the Free Software Foundation. | ||
10 | * | ||
11 | * NOTE : Currently this driver only supports the bare minimum for read | ||
12 | * and write the RTC. The extra features provided by the chip family | ||
13 | * (alarms, trickle charger, different control registers) are unavailable. | ||
14 | */ | ||
15 | |||
16 | #include <linux/platform_device.h> | ||
17 | #include <linux/rtc.h> | ||
18 | #include <linux/spi/spi.h> | ||
19 | #include <linux/bcd.h> | ||
20 | |||
21 | #define DS1390_REG_100THS 0x00 | ||
22 | #define DS1390_REG_SECONDS 0x01 | ||
23 | #define DS1390_REG_MINUTES 0x02 | ||
24 | #define DS1390_REG_HOURS 0x03 | ||
25 | #define DS1390_REG_DAY 0x04 | ||
26 | #define DS1390_REG_DATE 0x05 | ||
27 | #define DS1390_REG_MONTH_CENT 0x06 | ||
28 | #define DS1390_REG_YEAR 0x07 | ||
29 | |||
30 | #define DS1390_REG_ALARM_100THS 0x08 | ||
31 | #define DS1390_REG_ALARM_SECONDS 0x09 | ||
32 | #define DS1390_REG_ALARM_MINUTES 0x0A | ||
33 | #define DS1390_REG_ALARM_HOURS 0x0B | ||
34 | #define DS1390_REG_ALARM_DAY_DATE 0x0C | ||
35 | |||
36 | #define DS1390_REG_CONTROL 0x0D | ||
37 | #define DS1390_REG_STATUS 0x0E | ||
38 | #define DS1390_REG_TRICKLE 0x0F | ||
39 | |||
40 | struct ds1390 { | ||
41 | struct rtc_device *rtc; | ||
42 | u8 txrx_buf[9]; /* cmd + 8 registers */ | ||
43 | }; | ||
44 | |||
45 | static void ds1390_set_reg(struct device *dev, unsigned char address, | ||
46 | unsigned char data) | ||
47 | { | ||
48 | struct spi_device *spi = to_spi_device(dev); | ||
49 | struct ds1390 *chip = dev_get_drvdata(dev); | ||
50 | |||
51 | /* Set MSB to indicate write */ | ||
52 | chip->txrx_buf[0] = address | 0x80; | ||
53 | chip->txrx_buf[1] = data; | ||
54 | |||
55 | /* do the i/o */ | ||
56 | spi_write_then_read(spi, chip->txrx_buf, 2, NULL, 0); | ||
57 | } | ||
58 | |||
59 | static int ds1390_get_reg(struct device *dev, unsigned char address, | ||
60 | unsigned char *data) | ||
61 | { | ||
62 | struct spi_device *spi = to_spi_device(dev); | ||
63 | struct ds1390 *chip = dev_get_drvdata(dev); | ||
64 | int status; | ||
65 | |||
66 | if (!data) | ||
67 | return -EINVAL; | ||
68 | |||
69 | /* Clear MSB to indicate read */ | ||
70 | chip->txrx_buf[0] = address & 0x7f; | ||
71 | /* do the i/o */ | ||
72 | status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1); | ||
73 | if (status != 0) | ||
74 | return status; | ||
75 | |||
76 | *data = chip->txrx_buf[1]; | ||
77 | |||
78 | return 0; | ||
79 | } | ||
80 | |||
81 | static int ds1390_get_datetime(struct device *dev, struct rtc_time *dt) | ||
82 | { | ||
83 | struct spi_device *spi = to_spi_device(dev); | ||
84 | struct ds1390 *chip = dev_get_drvdata(dev); | ||
85 | int status; | ||
86 | |||
87 | /* build the message */ | ||
88 | chip->txrx_buf[0] = DS1390_REG_SECONDS; | ||
89 | |||
90 | /* do the i/o */ | ||
91 | status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8); | ||
92 | if (status != 0) | ||
93 | return status; | ||
94 | |||
95 | /* The chip sends data in this order: | ||
96 | * Seconds, Minutes, Hours, Day, Date, Month / Century, Year */ | ||
97 | dt->tm_sec = bcd2bin(chip->txrx_buf[0]); | ||
98 | dt->tm_min = bcd2bin(chip->txrx_buf[1]); | ||
99 | dt->tm_hour = bcd2bin(chip->txrx_buf[2]); | ||
100 | dt->tm_wday = bcd2bin(chip->txrx_buf[3]); | ||
101 | dt->tm_mday = bcd2bin(chip->txrx_buf[4]); | ||
102 | /* mask off century bit */ | ||
103 | dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1; | ||
104 | /* adjust for century bit */ | ||
105 | dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0); | ||
106 | |||
107 | return rtc_valid_tm(dt); | ||
108 | } | ||
109 | |||
110 | static int ds1390_set_datetime(struct device *dev, struct rtc_time *dt) | ||
111 | { | ||
112 | struct spi_device *spi = to_spi_device(dev); | ||
113 | struct ds1390 *chip = dev_get_drvdata(dev); | ||
114 | |||
115 | /* build the message */ | ||
116 | chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80; | ||
117 | chip->txrx_buf[1] = bin2bcd(dt->tm_sec); | ||
118 | chip->txrx_buf[2] = bin2bcd(dt->tm_min); | ||
119 | chip->txrx_buf[3] = bin2bcd(dt->tm_hour); | ||
120 | chip->txrx_buf[4] = bin2bcd(dt->tm_wday); | ||
121 | chip->txrx_buf[5] = bin2bcd(dt->tm_mday); | ||
122 | chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) | | ||
123 | ((dt->tm_year > 99) ? 0x80 : 0x00); | ||
124 | chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100); | ||
125 | |||
126 | /* do the i/o */ | ||
127 | return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0); | ||
128 | } | ||
129 | |||
130 | static int ds1390_read_time(struct device *dev, struct rtc_time *tm) | ||
131 | { | ||
132 | return ds1390_get_datetime(dev, tm); | ||
133 | } | ||
134 | |||
135 | static int ds1390_set_time(struct device *dev, struct rtc_time *tm) | ||
136 | { | ||
137 | return ds1390_set_datetime(dev, tm); | ||
138 | } | ||
139 | |||
140 | static const struct rtc_class_ops ds1390_rtc_ops = { | ||
141 | .read_time = ds1390_read_time, | ||
142 | .set_time = ds1390_set_time, | ||
143 | }; | ||
144 | |||
145 | static int __devinit ds1390_probe(struct spi_device *spi) | ||
146 | { | ||
147 | struct rtc_device *rtc; | ||
148 | unsigned char tmp; | ||
149 | struct ds1390 *chip; | ||
150 | int res; | ||
151 | |||
152 | printk(KERN_DEBUG "DS1390 SPI RTC driver\n"); | ||
153 | |||
154 | rtc = rtc_device_register("ds1390", | ||
155 | &spi->dev, &ds1390_rtc_ops, THIS_MODULE); | ||
156 | if (IS_ERR(rtc)) { | ||
157 | printk(KERN_ALERT "RTC : unable to register device\n"); | ||
158 | return PTR_ERR(rtc); | ||
159 | } | ||
160 | |||
161 | spi->mode = SPI_MODE_3; | ||
162 | spi->bits_per_word = 8; | ||
163 | spi_setup(spi); | ||
164 | |||
165 | chip = kzalloc(sizeof *chip, GFP_KERNEL); | ||
166 | if (!chip) { | ||
167 | printk(KERN_ALERT "RTC : unable to allocate device memory\n"); | ||
168 | rtc_device_unregister(rtc); | ||
169 | return -ENOMEM; | ||
170 | } | ||
171 | chip->rtc = rtc; | ||
172 | dev_set_drvdata(&spi->dev, chip); | ||
173 | |||
174 | res = ds1390_get_reg(&spi->dev, DS1390_REG_SECONDS, &tmp); | ||
175 | if (res) { | ||
176 | printk(KERN_ALERT "RTC : unable to read device\n"); | ||
177 | rtc_device_unregister(rtc); | ||
178 | return res; | ||
179 | } | ||
180 | |||
181 | return 0; | ||
182 | } | ||
183 | |||
184 | static int __devexit ds1390_remove(struct spi_device *spi) | ||
185 | { | ||
186 | struct ds1390 *chip = platform_get_drvdata(spi); | ||
187 | struct rtc_device *rtc = chip->rtc; | ||
188 | |||
189 | if (rtc) | ||
190 | rtc_device_unregister(rtc); | ||
191 | |||
192 | kfree(chip); | ||
193 | |||
194 | return 0; | ||
195 | } | ||
196 | |||
197 | static struct spi_driver ds1390_driver = { | ||
198 | .driver = { | ||
199 | .name = "rtc-ds1390", | ||
200 | .owner = THIS_MODULE, | ||
201 | }, | ||
202 | .probe = ds1390_probe, | ||
203 | .remove = __devexit_p(ds1390_remove), | ||
204 | }; | ||
205 | |||
206 | static __init int ds1390_init(void) | ||
207 | { | ||
208 | return spi_register_driver(&ds1390_driver); | ||
209 | } | ||
210 | module_init(ds1390_init); | ||
211 | |||
212 | static __exit void ds1390_exit(void) | ||
213 | { | ||
214 | spi_unregister_driver(&ds1390_driver); | ||
215 | } | ||
216 | module_exit(ds1390_exit); | ||
217 | |||
218 | MODULE_DESCRIPTION("DS1390/93/94 SPI RTC driver"); | ||
219 | MODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>"); | ||
220 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/rtc/rtc-rx8581.c b/drivers/rtc/rtc-rx8581.c new file mode 100644 index 000000000000..c9522f3bc21c --- /dev/null +++ b/drivers/rtc/rtc-rx8581.c | |||
@@ -0,0 +1,281 @@ | |||
1 | /* | ||
2 | * An I2C driver for the Epson RX8581 RTC | ||
3 | * | ||
4 | * Author: Martyn Welch <martyn.welch@gefanuc.com> | ||
5 | * Copyright 2008 GE Fanuc Intelligent Platforms Embedded Systems, Inc. | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License version 2 as | ||
9 | * published by the Free Software Foundation. | ||
10 | * | ||
11 | * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC) | ||
12 | * Copyright 2005-06 Tower Technologies | ||
13 | */ | ||
14 | |||
15 | #include <linux/module.h> | ||
16 | #include <linux/i2c.h> | ||
17 | #include <linux/bcd.h> | ||
18 | #include <linux/rtc.h> | ||
19 | #include <linux/log2.h> | ||
20 | |||
21 | #define DRV_VERSION "0.1" | ||
22 | |||
23 | #define RX8581_REG_SC 0x00 /* Second in BCD */ | ||
24 | #define RX8581_REG_MN 0x01 /* Minute in BCD */ | ||
25 | #define RX8581_REG_HR 0x02 /* Hour in BCD */ | ||
26 | #define RX8581_REG_DW 0x03 /* Day of Week */ | ||
27 | #define RX8581_REG_DM 0x04 /* Day of Month in BCD */ | ||
28 | #define RX8581_REG_MO 0x05 /* Month in BCD */ | ||
29 | #define RX8581_REG_YR 0x06 /* Year in BCD */ | ||
30 | #define RX8581_REG_RAM 0x07 /* RAM */ | ||
31 | #define RX8581_REG_AMN 0x08 /* Alarm Min in BCD*/ | ||
32 | #define RX8581_REG_AHR 0x09 /* Alarm Hour in BCD */ | ||
33 | #define RX8581_REG_ADM 0x0A | ||
34 | #define RX8581_REG_ADW 0x0A | ||
35 | #define RX8581_REG_TMR0 0x0B | ||
36 | #define RX8581_REG_TMR1 0x0C | ||
37 | #define RX8581_REG_EXT 0x0D /* Extension Register */ | ||
38 | #define RX8581_REG_FLAG 0x0E /* Flag Register */ | ||
39 | #define RX8581_REG_CTRL 0x0F /* Control Register */ | ||
40 | |||
41 | |||
42 | /* Flag Register bit definitions */ | ||
43 | #define RX8581_FLAG_UF 0x20 /* Update */ | ||
44 | #define RX8581_FLAG_TF 0x10 /* Timer */ | ||
45 | #define RX8581_FLAG_AF 0x08 /* Alarm */ | ||
46 | #define RX8581_FLAG_VLF 0x02 /* Voltage Low */ | ||
47 | |||
48 | /* Control Register bit definitions */ | ||
49 | #define RX8581_CTRL_UIE 0x20 /* Update Interrupt Enable */ | ||
50 | #define RX8581_CTRL_TIE 0x10 /* Timer Interrupt Enable */ | ||
51 | #define RX8581_CTRL_AIE 0x08 /* Alarm Interrupt Enable */ | ||
52 | #define RX8581_CTRL_STOP 0x02 /* STOP bit */ | ||
53 | #define RX8581_CTRL_RESET 0x01 /* RESET bit */ | ||
54 | |||
55 | static struct i2c_driver rx8581_driver; | ||
56 | |||
57 | /* | ||
58 | * In the routines that deal directly with the rx8581 hardware, we use | ||
59 | * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. | ||
60 | */ | ||
61 | static int rx8581_get_datetime(struct i2c_client *client, struct rtc_time *tm) | ||
62 | { | ||
63 | unsigned char date[7]; | ||
64 | int data, err; | ||
65 | |||
66 | /* First we ensure that the "update flag" is not set, we read the | ||
67 | * time and date then re-read the "update flag". If the update flag | ||
68 | * has been set, we know that the time has changed during the read so | ||
69 | * we repeat the whole process again. | ||
70 | */ | ||
71 | data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG); | ||
72 | if (data < 0) { | ||
73 | dev_err(&client->dev, "Unable to read device flags\n"); | ||
74 | return -EIO; | ||
75 | } | ||
76 | |||
77 | do { | ||
78 | /* If update flag set, clear it */ | ||
79 | if (data & RX8581_FLAG_UF) { | ||
80 | err = i2c_smbus_write_byte_data(client, | ||
81 | RX8581_REG_FLAG, (data & ~RX8581_FLAG_UF)); | ||
82 | if (err != 0) { | ||
83 | dev_err(&client->dev, "Unable to write device " | ||
84 | "flags\n"); | ||
85 | return -EIO; | ||
86 | } | ||
87 | } | ||
88 | |||
89 | /* Now read time and date */ | ||
90 | err = i2c_smbus_read_i2c_block_data(client, RX8581_REG_SC, | ||
91 | 7, date); | ||
92 | if (err < 0) { | ||
93 | dev_err(&client->dev, "Unable to read date\n"); | ||
94 | return -EIO; | ||
95 | } | ||
96 | |||
97 | /* Check flag register */ | ||
98 | data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG); | ||
99 | if (data < 0) { | ||
100 | dev_err(&client->dev, "Unable to read device flags\n"); | ||
101 | return -EIO; | ||
102 | } | ||
103 | } while (data & RX8581_FLAG_UF); | ||
104 | |||
105 | if (data & RX8581_FLAG_VLF) | ||
106 | dev_info(&client->dev, | ||
107 | "low voltage detected, date/time is not reliable.\n"); | ||
108 | |||
109 | dev_dbg(&client->dev, | ||
110 | "%s: raw data is sec=%02x, min=%02x, hr=%02x, " | ||
111 | "wday=%02x, mday=%02x, mon=%02x, year=%02x\n", | ||
112 | __func__, | ||
113 | date[0], date[1], date[2], date[3], date[4], date[5], date[6]); | ||
114 | |||
115 | tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F); | ||
116 | tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F); | ||
117 | tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F); /* rtc hr 0-23 */ | ||
118 | tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F); | ||
119 | tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F); | ||
120 | tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ | ||
121 | tm->tm_year = bcd2bin(date[RX8581_REG_YR]); | ||
122 | if (tm->tm_year < 70) | ||
123 | tm->tm_year += 100; /* assume we are in 1970...2069 */ | ||
124 | |||
125 | |||
126 | dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " | ||
127 | "mday=%d, mon=%d, year=%d, wday=%d\n", | ||
128 | __func__, | ||
129 | tm->tm_sec, tm->tm_min, tm->tm_hour, | ||
130 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | ||
131 | |||
132 | err = rtc_valid_tm(tm); | ||
133 | if (err < 0) | ||
134 | dev_err(&client->dev, "retrieved date/time is not valid.\n"); | ||
135 | |||
136 | return err; | ||
137 | } | ||
138 | |||
139 | static int rx8581_set_datetime(struct i2c_client *client, struct rtc_time *tm) | ||
140 | { | ||
141 | int data, err; | ||
142 | unsigned char buf[7]; | ||
143 | |||
144 | dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, " | ||
145 | "mday=%d, mon=%d, year=%d, wday=%d\n", | ||
146 | __func__, | ||
147 | tm->tm_sec, tm->tm_min, tm->tm_hour, | ||
148 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | ||
149 | |||
150 | /* hours, minutes and seconds */ | ||
151 | buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec); | ||
152 | buf[RX8581_REG_MN] = bin2bcd(tm->tm_min); | ||
153 | buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour); | ||
154 | |||
155 | buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday); | ||
156 | |||
157 | /* month, 1 - 12 */ | ||
158 | buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1); | ||
159 | |||
160 | /* year and century */ | ||
161 | buf[RX8581_REG_YR] = bin2bcd(tm->tm_year % 100); | ||
162 | buf[RX8581_REG_DW] = (0x1 << tm->tm_wday); | ||
163 | |||
164 | /* Stop the clock */ | ||
165 | data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL); | ||
166 | if (data < 0) { | ||
167 | dev_err(&client->dev, "Unable to read control register\n"); | ||
168 | return -EIO; | ||
169 | } | ||
170 | |||
171 | err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG, | ||
172 | (data | RX8581_CTRL_STOP)); | ||
173 | if (err < 0) { | ||
174 | dev_err(&client->dev, "Unable to write control register\n"); | ||
175 | return -EIO; | ||
176 | } | ||
177 | |||
178 | /* write register's data */ | ||
179 | err = i2c_smbus_write_i2c_block_data(client, RX8581_REG_SC, 7, buf); | ||
180 | if (err < 0) { | ||
181 | dev_err(&client->dev, "Unable to write to date registers\n"); | ||
182 | return -EIO; | ||
183 | } | ||
184 | |||
185 | /* Restart the clock */ | ||
186 | data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL); | ||
187 | if (data < 0) { | ||
188 | dev_err(&client->dev, "Unable to read control register\n"); | ||
189 | return -EIO; | ||
190 | } | ||
191 | |||
192 | err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG, | ||
193 | (data | ~(RX8581_CTRL_STOP))); | ||
194 | if (err != 0) { | ||
195 | dev_err(&client->dev, "Unable to write control register\n"); | ||
196 | return -EIO; | ||
197 | } | ||
198 | |||
199 | return 0; | ||
200 | } | ||
201 | |||
202 | static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
203 | { | ||
204 | return rx8581_get_datetime(to_i2c_client(dev), tm); | ||
205 | } | ||
206 | |||
207 | static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
208 | { | ||
209 | return rx8581_set_datetime(to_i2c_client(dev), tm); | ||
210 | } | ||
211 | |||
212 | static const struct rtc_class_ops rx8581_rtc_ops = { | ||
213 | .read_time = rx8581_rtc_read_time, | ||
214 | .set_time = rx8581_rtc_set_time, | ||
215 | }; | ||
216 | |||
217 | static int __devinit rx8581_probe(struct i2c_client *client, | ||
218 | const struct i2c_device_id *id) | ||
219 | { | ||
220 | struct rtc_device *rtc; | ||
221 | |||
222 | dev_dbg(&client->dev, "%s\n", __func__); | ||
223 | |||
224 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) | ||
225 | return -ENODEV; | ||
226 | |||
227 | dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); | ||
228 | |||
229 | rtc = rtc_device_register(rx8581_driver.driver.name, | ||
230 | &client->dev, &rx8581_rtc_ops, THIS_MODULE); | ||
231 | |||
232 | if (IS_ERR(rtc)) | ||
233 | return PTR_ERR(rtc); | ||
234 | |||
235 | i2c_set_clientdata(client, rtc); | ||
236 | |||
237 | return 0; | ||
238 | } | ||
239 | |||
240 | static int __devexit rx8581_remove(struct i2c_client *client) | ||
241 | { | ||
242 | struct rtc_device *rtc = i2c_get_clientdata(client); | ||
243 | |||
244 | rtc_device_unregister(rtc); | ||
245 | |||
246 | return 0; | ||
247 | } | ||
248 | |||
249 | static const struct i2c_device_id rx8581_id[] = { | ||
250 | { "rx8581", 0 }, | ||
251 | { } | ||
252 | }; | ||
253 | MODULE_DEVICE_TABLE(i2c, rx8581_id); | ||
254 | |||
255 | static struct i2c_driver rx8581_driver = { | ||
256 | .driver = { | ||
257 | .name = "rtc-rx8581", | ||
258 | .owner = THIS_MODULE, | ||
259 | }, | ||
260 | .probe = rx8581_probe, | ||
261 | .remove = __devexit_p(rx8581_remove), | ||
262 | .id_table = rx8581_id, | ||
263 | }; | ||
264 | |||
265 | static int __init rx8581_init(void) | ||
266 | { | ||
267 | return i2c_add_driver(&rx8581_driver); | ||
268 | } | ||
269 | |||
270 | static void __exit rx8581_exit(void) | ||
271 | { | ||
272 | i2c_del_driver(&rx8581_driver); | ||
273 | } | ||
274 | |||
275 | MODULE_AUTHOR("Martyn Welch <martyn.welch@gefanuc.com>"); | ||
276 | MODULE_DESCRIPTION("Epson RX-8581 RTC driver"); | ||
277 | MODULE_LICENSE("GPL"); | ||
278 | MODULE_VERSION(DRV_VERSION); | ||
279 | |||
280 | module_init(rx8581_init); | ||
281 | module_exit(rx8581_exit); | ||
diff --git a/drivers/rtc/rtc-sun4v.c b/drivers/rtc/rtc-sun4v.c index 2012ccbb4a53..5b2261052a65 100644 --- a/drivers/rtc/rtc-sun4v.c +++ b/drivers/rtc/rtc-sun4v.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* rtc-sun4c.c: Hypervisor based RTC for SUN4V systems. | 1 | /* rtc-sun4v.c: Hypervisor based RTC for SUN4V systems. |
2 | * | 2 | * |
3 | * Copyright (C) 2008 David S. Miller <davem@davemloft.net> | 3 | * Copyright (C) 2008 David S. Miller <davem@davemloft.net> |
4 | */ | 4 | */ |
@@ -7,21 +7,11 @@ | |||
7 | #include <linux/module.h> | 7 | #include <linux/module.h> |
8 | #include <linux/delay.h> | 8 | #include <linux/delay.h> |
9 | #include <linux/init.h> | 9 | #include <linux/init.h> |
10 | #include <linux/time.h> | ||
11 | #include <linux/rtc.h> | 10 | #include <linux/rtc.h> |
12 | #include <linux/platform_device.h> | 11 | #include <linux/platform_device.h> |
13 | 12 | ||
14 | #include <asm/hypervisor.h> | 13 | #include <asm/hypervisor.h> |
15 | 14 | ||
16 | MODULE_AUTHOR("David S. Miller <davem@davemloft.net>"); | ||
17 | MODULE_DESCRIPTION("SUN4V RTC driver"); | ||
18 | MODULE_LICENSE("GPL"); | ||
19 | |||
20 | struct sun4v_rtc { | ||
21 | struct rtc_device *rtc; | ||
22 | spinlock_t lock; | ||
23 | }; | ||
24 | |||
25 | static unsigned long hypervisor_get_time(void) | 15 | static unsigned long hypervisor_get_time(void) |
26 | { | 16 | { |
27 | unsigned long ret, time; | 17 | unsigned long ret, time; |
@@ -45,15 +35,7 @@ retry: | |||
45 | 35 | ||
46 | static int sun4v_read_time(struct device *dev, struct rtc_time *tm) | 36 | static int sun4v_read_time(struct device *dev, struct rtc_time *tm) |
47 | { | 37 | { |
48 | struct sun4v_rtc *p = dev_get_drvdata(dev); | 38 | rtc_time_to_tm(hypervisor_get_time(), tm); |
49 | unsigned long flags, secs; | ||
50 | |||
51 | spin_lock_irqsave(&p->lock, flags); | ||
52 | secs = hypervisor_get_time(); | ||
53 | spin_unlock_irqrestore(&p->lock, flags); | ||
54 | |||
55 | rtc_time_to_tm(secs, tm); | ||
56 | |||
57 | return 0; | 39 | return 0; |
58 | } | 40 | } |
59 | 41 | ||
@@ -80,19 +62,14 @@ retry: | |||
80 | 62 | ||
81 | static int sun4v_set_time(struct device *dev, struct rtc_time *tm) | 63 | static int sun4v_set_time(struct device *dev, struct rtc_time *tm) |
82 | { | 64 | { |
83 | struct sun4v_rtc *p = dev_get_drvdata(dev); | 65 | unsigned long secs; |
84 | unsigned long flags, secs; | ||
85 | int err; | 66 | int err; |
86 | 67 | ||
87 | err = rtc_tm_to_time(tm, &secs); | 68 | err = rtc_tm_to_time(tm, &secs); |
88 | if (err) | 69 | if (err) |
89 | return err; | 70 | return err; |
90 | 71 | ||
91 | spin_lock_irqsave(&p->lock, flags); | 72 | return hypervisor_set_time(secs); |
92 | err = hypervisor_set_time(secs); | ||
93 | spin_unlock_irqrestore(&p->lock, flags); | ||
94 | |||
95 | return err; | ||
96 | } | 73 | } |
97 | 74 | ||
98 | static const struct rtc_class_ops sun4v_rtc_ops = { | 75 | static const struct rtc_class_ops sun4v_rtc_ops = { |
@@ -100,33 +77,22 @@ static const struct rtc_class_ops sun4v_rtc_ops = { | |||
100 | .set_time = sun4v_set_time, | 77 | .set_time = sun4v_set_time, |
101 | }; | 78 | }; |
102 | 79 | ||
103 | static int __devinit sun4v_rtc_probe(struct platform_device *pdev) | 80 | static int __init sun4v_rtc_probe(struct platform_device *pdev) |
104 | { | 81 | { |
105 | struct sun4v_rtc *p = kzalloc(sizeof(*p), GFP_KERNEL); | 82 | struct rtc_device *rtc = rtc_device_register("sun4v", &pdev->dev, |
106 | |||
107 | if (!p) | ||
108 | return -ENOMEM; | ||
109 | |||
110 | spin_lock_init(&p->lock); | ||
111 | |||
112 | p->rtc = rtc_device_register("sun4v", &pdev->dev, | ||
113 | &sun4v_rtc_ops, THIS_MODULE); | 83 | &sun4v_rtc_ops, THIS_MODULE); |
114 | if (IS_ERR(p->rtc)) { | 84 | if (IS_ERR(rtc)) |
115 | int err = PTR_ERR(p->rtc); | 85 | return PTR_ERR(rtc); |
116 | kfree(p); | 86 | |
117 | return err; | 87 | platform_set_drvdata(pdev, rtc); |
118 | } | ||
119 | platform_set_drvdata(pdev, p); | ||
120 | return 0; | 88 | return 0; |
121 | } | 89 | } |
122 | 90 | ||
123 | static int __devexit sun4v_rtc_remove(struct platform_device *pdev) | 91 | static int __exit sun4v_rtc_remove(struct platform_device *pdev) |
124 | { | 92 | { |
125 | struct sun4v_rtc *p = platform_get_drvdata(pdev); | 93 | struct rtc_device *rtc = platform_get_drvdata(pdev); |
126 | |||
127 | rtc_device_unregister(p->rtc); | ||
128 | kfree(p); | ||
129 | 94 | ||
95 | rtc_device_unregister(rtc); | ||
130 | return 0; | 96 | return 0; |
131 | } | 97 | } |
132 | 98 | ||
@@ -135,13 +101,12 @@ static struct platform_driver sun4v_rtc_driver = { | |||
135 | .name = "rtc-sun4v", | 101 | .name = "rtc-sun4v", |
136 | .owner = THIS_MODULE, | 102 | .owner = THIS_MODULE, |
137 | }, | 103 | }, |
138 | .probe = sun4v_rtc_probe, | 104 | .remove = __exit_p(sun4v_rtc_remove), |
139 | .remove = __devexit_p(sun4v_rtc_remove), | ||
140 | }; | 105 | }; |
141 | 106 | ||
142 | static int __init sun4v_rtc_init(void) | 107 | static int __init sun4v_rtc_init(void) |
143 | { | 108 | { |
144 | return platform_driver_register(&sun4v_rtc_driver); | 109 | return platform_driver_probe(&sun4v_rtc_driver, sun4v_rtc_probe); |
145 | } | 110 | } |
146 | 111 | ||
147 | static void __exit sun4v_rtc_exit(void) | 112 | static void __exit sun4v_rtc_exit(void) |
@@ -151,3 +116,7 @@ static void __exit sun4v_rtc_exit(void) | |||
151 | 116 | ||
152 | module_init(sun4v_rtc_init); | 117 | module_init(sun4v_rtc_init); |
153 | module_exit(sun4v_rtc_exit); | 118 | module_exit(sun4v_rtc_exit); |
119 | |||
120 | MODULE_AUTHOR("David S. Miller <davem@davemloft.net>"); | ||
121 | MODULE_DESCRIPTION("SUN4V RTC driver"); | ||
122 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/rtc/rtc-wm8350.c b/drivers/rtc/rtc-wm8350.c new file mode 100644 index 000000000000..5c5e3aa91385 --- /dev/null +++ b/drivers/rtc/rtc-wm8350.c | |||
@@ -0,0 +1,514 @@ | |||
1 | /* | ||
2 | * Real Time Clock driver for Wolfson Microelectronics WM8350 | ||
3 | * | ||
4 | * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC. | ||
5 | * | ||
6 | * Author: Liam Girdwood | ||
7 | * linux@wolfsonmicro.com | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify it | ||
10 | * under the terms of the GNU General Public License as published by the | ||
11 | * Free Software Foundation; either version 2 of the License, or (at your | ||
12 | * option) any later version. | ||
13 | * | ||
14 | */ | ||
15 | |||
16 | #include <linux/module.h> | ||
17 | #include <linux/kernel.h> | ||
18 | #include <linux/time.h> | ||
19 | #include <linux/rtc.h> | ||
20 | #include <linux/bcd.h> | ||
21 | #include <linux/interrupt.h> | ||
22 | #include <linux/ioctl.h> | ||
23 | #include <linux/completion.h> | ||
24 | #include <linux/mfd/wm8350/rtc.h> | ||
25 | #include <linux/mfd/wm8350/core.h> | ||
26 | #include <linux/delay.h> | ||
27 | #include <linux/platform_device.h> | ||
28 | |||
29 | #define WM8350_SET_ALM_RETRIES 5 | ||
30 | #define WM8350_SET_TIME_RETRIES 5 | ||
31 | #define WM8350_GET_TIME_RETRIES 5 | ||
32 | |||
33 | #define to_wm8350_from_rtc_dev(d) container_of(d, struct wm8350, rtc.pdev.dev) | ||
34 | |||
35 | /* | ||
36 | * Read current time and date in RTC | ||
37 | */ | ||
38 | static int wm8350_rtc_readtime(struct device *dev, struct rtc_time *tm) | ||
39 | { | ||
40 | struct wm8350 *wm8350 = dev_get_drvdata(dev); | ||
41 | u16 time1[4], time2[4]; | ||
42 | int retries = WM8350_GET_TIME_RETRIES, ret; | ||
43 | |||
44 | /* | ||
45 | * Read the time twice and compare. | ||
46 | * If time1 == time2, then time is valid else retry. | ||
47 | */ | ||
48 | do { | ||
49 | ret = wm8350_block_read(wm8350, WM8350_RTC_SECONDS_MINUTES, | ||
50 | 4, time1); | ||
51 | if (ret < 0) | ||
52 | return ret; | ||
53 | ret = wm8350_block_read(wm8350, WM8350_RTC_SECONDS_MINUTES, | ||
54 | 4, time2); | ||
55 | if (ret < 0) | ||
56 | return ret; | ||
57 | |||
58 | if (memcmp(time1, time2, sizeof(time1)) == 0) { | ||
59 | tm->tm_sec = time1[0] & WM8350_RTC_SECS_MASK; | ||
60 | |||
61 | tm->tm_min = (time1[0] & WM8350_RTC_MINS_MASK) | ||
62 | >> WM8350_RTC_MINS_SHIFT; | ||
63 | |||
64 | tm->tm_hour = time1[1] & WM8350_RTC_HRS_MASK; | ||
65 | |||
66 | tm->tm_wday = ((time1[1] >> WM8350_RTC_DAY_SHIFT) | ||
67 | & 0x7) - 1; | ||
68 | |||
69 | tm->tm_mon = ((time1[2] & WM8350_RTC_MTH_MASK) | ||
70 | >> WM8350_RTC_MTH_SHIFT) - 1; | ||
71 | |||
72 | tm->tm_mday = (time1[2] & WM8350_RTC_DATE_MASK); | ||
73 | |||
74 | tm->tm_year = ((time1[3] & WM8350_RTC_YHUNDREDS_MASK) | ||
75 | >> WM8350_RTC_YHUNDREDS_SHIFT) * 100; | ||
76 | tm->tm_year += time1[3] & WM8350_RTC_YUNITS_MASK; | ||
77 | |||
78 | tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, | ||
79 | tm->tm_year); | ||
80 | tm->tm_year -= 1900; | ||
81 | |||
82 | dev_dbg(dev, "Read (%d left): %04x %04x %04x %04x\n", | ||
83 | retries, | ||
84 | time1[0], time1[1], time1[2], time1[3]); | ||
85 | |||
86 | return 0; | ||
87 | } | ||
88 | } while (retries--); | ||
89 | |||
90 | dev_err(dev, "timed out reading RTC time\n"); | ||
91 | return -EIO; | ||
92 | } | ||
93 | |||
94 | /* | ||
95 | * Set current time and date in RTC | ||
96 | */ | ||
97 | static int wm8350_rtc_settime(struct device *dev, struct rtc_time *tm) | ||
98 | { | ||
99 | struct wm8350 *wm8350 = dev_get_drvdata(dev); | ||
100 | u16 time[4]; | ||
101 | u16 rtc_ctrl; | ||
102 | int ret, retries = WM8350_SET_TIME_RETRIES; | ||
103 | |||
104 | time[0] = tm->tm_sec; | ||
105 | time[0] |= tm->tm_min << WM8350_RTC_MINS_SHIFT; | ||
106 | time[1] = tm->tm_hour; | ||
107 | time[1] |= (tm->tm_wday + 1) << WM8350_RTC_DAY_SHIFT; | ||
108 | time[2] = tm->tm_mday; | ||
109 | time[2] |= (tm->tm_mon + 1) << WM8350_RTC_MTH_SHIFT; | ||
110 | time[3] = ((tm->tm_year + 1900) / 100) << WM8350_RTC_YHUNDREDS_SHIFT; | ||
111 | time[3] |= (tm->tm_year + 1900) % 100; | ||
112 | |||
113 | dev_dbg(dev, "Setting: %04x %04x %04x %04x\n", | ||
114 | time[0], time[1], time[2], time[3]); | ||
115 | |||
116 | /* Set RTC_SET to stop the clock */ | ||
117 | ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL, WM8350_RTC_SET); | ||
118 | if (ret < 0) | ||
119 | return ret; | ||
120 | |||
121 | /* Wait until confirmation of stopping */ | ||
122 | do { | ||
123 | rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL); | ||
124 | schedule_timeout_uninterruptible(msecs_to_jiffies(1)); | ||
125 | } while (retries-- && !(rtc_ctrl & WM8350_RTC_STS)); | ||
126 | |||
127 | if (!retries) { | ||
128 | dev_err(dev, "timed out on set confirmation\n"); | ||
129 | return -EIO; | ||
130 | } | ||
131 | |||
132 | /* Write time to RTC */ | ||
133 | ret = wm8350_block_write(wm8350, WM8350_RTC_SECONDS_MINUTES, 4, time); | ||
134 | if (ret < 0) | ||
135 | return ret; | ||
136 | |||
137 | /* Clear RTC_SET to start the clock */ | ||
138 | ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL, | ||
139 | WM8350_RTC_SET); | ||
140 | return ret; | ||
141 | } | ||
142 | |||
143 | /* | ||
144 | * Read alarm time and date in RTC | ||
145 | */ | ||
146 | static int wm8350_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
147 | { | ||
148 | struct wm8350 *wm8350 = dev_get_drvdata(dev); | ||
149 | struct rtc_time *tm = &alrm->time; | ||
150 | u16 time[4]; | ||
151 | int ret; | ||
152 | |||
153 | ret = wm8350_block_read(wm8350, WM8350_ALARM_SECONDS_MINUTES, 4, time); | ||
154 | if (ret < 0) | ||
155 | return ret; | ||
156 | |||
157 | tm->tm_sec = time[0] & WM8350_RTC_ALMSECS_MASK; | ||
158 | if (tm->tm_sec == WM8350_RTC_ALMSECS_MASK) | ||
159 | tm->tm_sec = -1; | ||
160 | |||
161 | tm->tm_min = time[0] & WM8350_RTC_ALMMINS_MASK; | ||
162 | if (tm->tm_min == WM8350_RTC_ALMMINS_MASK) | ||
163 | tm->tm_min = -1; | ||
164 | else | ||
165 | tm->tm_min >>= WM8350_RTC_ALMMINS_SHIFT; | ||
166 | |||
167 | tm->tm_hour = time[1] & WM8350_RTC_ALMHRS_MASK; | ||
168 | if (tm->tm_hour == WM8350_RTC_ALMHRS_MASK) | ||
169 | tm->tm_hour = -1; | ||
170 | |||
171 | tm->tm_wday = ((time[1] >> WM8350_RTC_ALMDAY_SHIFT) & 0x7) - 1; | ||
172 | if (tm->tm_wday > 7) | ||
173 | tm->tm_wday = -1; | ||
174 | |||
175 | tm->tm_mon = time[2] & WM8350_RTC_ALMMTH_MASK; | ||
176 | if (tm->tm_mon == WM8350_RTC_ALMMTH_MASK) | ||
177 | tm->tm_mon = -1; | ||
178 | else | ||
179 | tm->tm_mon = (tm->tm_mon >> WM8350_RTC_ALMMTH_SHIFT) - 1; | ||
180 | |||
181 | tm->tm_mday = (time[2] & WM8350_RTC_ALMDATE_MASK); | ||
182 | if (tm->tm_mday == WM8350_RTC_ALMDATE_MASK) | ||
183 | tm->tm_mday = -1; | ||
184 | |||
185 | tm->tm_year = -1; | ||
186 | |||
187 | alrm->enabled = !(time[3] & WM8350_RTC_ALMSTS); | ||
188 | |||
189 | return 0; | ||
190 | } | ||
191 | |||
192 | static int wm8350_rtc_stop_alarm(struct wm8350 *wm8350) | ||
193 | { | ||
194 | int retries = WM8350_SET_ALM_RETRIES; | ||
195 | u16 rtc_ctrl; | ||
196 | int ret; | ||
197 | |||
198 | /* Set RTC_SET to stop the clock */ | ||
199 | ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL, | ||
200 | WM8350_RTC_ALMSET); | ||
201 | if (ret < 0) | ||
202 | return ret; | ||
203 | |||
204 | /* Wait until confirmation of stopping */ | ||
205 | do { | ||
206 | rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL); | ||
207 | schedule_timeout_uninterruptible(msecs_to_jiffies(1)); | ||
208 | } while (retries-- && !(rtc_ctrl & WM8350_RTC_ALMSTS)); | ||
209 | |||
210 | if (!(rtc_ctrl & WM8350_RTC_ALMSTS)) | ||
211 | return -ETIMEDOUT; | ||
212 | |||
213 | return 0; | ||
214 | } | ||
215 | |||
216 | static int wm8350_rtc_start_alarm(struct wm8350 *wm8350) | ||
217 | { | ||
218 | int ret; | ||
219 | int retries = WM8350_SET_ALM_RETRIES; | ||
220 | u16 rtc_ctrl; | ||
221 | |||
222 | ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL, | ||
223 | WM8350_RTC_ALMSET); | ||
224 | if (ret < 0) | ||
225 | return ret; | ||
226 | |||
227 | /* Wait until confirmation */ | ||
228 | do { | ||
229 | rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL); | ||
230 | schedule_timeout_uninterruptible(msecs_to_jiffies(1)); | ||
231 | } while (retries-- && rtc_ctrl & WM8350_RTC_ALMSTS); | ||
232 | |||
233 | if (rtc_ctrl & WM8350_RTC_ALMSTS) | ||
234 | return -ETIMEDOUT; | ||
235 | |||
236 | return 0; | ||
237 | } | ||
238 | |||
239 | static int wm8350_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
240 | { | ||
241 | struct wm8350 *wm8350 = dev_get_drvdata(dev); | ||
242 | struct rtc_time *tm = &alrm->time; | ||
243 | u16 time[3]; | ||
244 | int ret; | ||
245 | |||
246 | memset(time, 0, sizeof(time)); | ||
247 | |||
248 | if (tm->tm_sec != -1) | ||
249 | time[0] |= tm->tm_sec; | ||
250 | else | ||
251 | time[0] |= WM8350_RTC_ALMSECS_MASK; | ||
252 | |||
253 | if (tm->tm_min != -1) | ||
254 | time[0] |= tm->tm_min << WM8350_RTC_ALMMINS_SHIFT; | ||
255 | else | ||
256 | time[0] |= WM8350_RTC_ALMMINS_MASK; | ||
257 | |||
258 | if (tm->tm_hour != -1) | ||
259 | time[1] |= tm->tm_hour; | ||
260 | else | ||
261 | time[1] |= WM8350_RTC_ALMHRS_MASK; | ||
262 | |||
263 | if (tm->tm_wday != -1) | ||
264 | time[1] |= (tm->tm_wday + 1) << WM8350_RTC_ALMDAY_SHIFT; | ||
265 | else | ||
266 | time[1] |= WM8350_RTC_ALMDAY_MASK; | ||
267 | |||
268 | if (tm->tm_mday != -1) | ||
269 | time[2] |= tm->tm_mday; | ||
270 | else | ||
271 | time[2] |= WM8350_RTC_ALMDATE_MASK; | ||
272 | |||
273 | if (tm->tm_mon != -1) | ||
274 | time[2] |= (tm->tm_mon + 1) << WM8350_RTC_ALMMTH_SHIFT; | ||
275 | else | ||
276 | time[2] |= WM8350_RTC_ALMMTH_MASK; | ||
277 | |||
278 | ret = wm8350_rtc_stop_alarm(wm8350); | ||
279 | if (ret < 0) | ||
280 | return ret; | ||
281 | |||
282 | /* Write time to RTC */ | ||
283 | ret = wm8350_block_write(wm8350, WM8350_ALARM_SECONDS_MINUTES, | ||
284 | 3, time); | ||
285 | if (ret < 0) | ||
286 | return ret; | ||
287 | |||
288 | if (alrm->enabled) | ||
289 | ret = wm8350_rtc_start_alarm(wm8350); | ||
290 | |||
291 | return ret; | ||
292 | } | ||
293 | |||
294 | /* | ||
295 | * Handle commands from user-space | ||
296 | */ | ||
297 | static int wm8350_rtc_ioctl(struct device *dev, unsigned int cmd, | ||
298 | unsigned long arg) | ||
299 | { | ||
300 | struct wm8350 *wm8350 = dev_get_drvdata(dev); | ||
301 | |||
302 | switch (cmd) { | ||
303 | case RTC_AIE_OFF: | ||
304 | return wm8350_rtc_stop_alarm(wm8350); | ||
305 | case RTC_AIE_ON: | ||
306 | return wm8350_rtc_start_alarm(wm8350); | ||
307 | |||
308 | case RTC_UIE_OFF: | ||
309 | wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC); | ||
310 | break; | ||
311 | case RTC_UIE_ON: | ||
312 | wm8350_unmask_irq(wm8350, WM8350_IRQ_RTC_SEC); | ||
313 | break; | ||
314 | |||
315 | default: | ||
316 | return -ENOIOCTLCMD; | ||
317 | } | ||
318 | |||
319 | return 0; | ||
320 | } | ||
321 | |||
322 | static void wm8350_rtc_alarm_handler(struct wm8350 *wm8350, int irq, | ||
323 | void *data) | ||
324 | { | ||
325 | struct rtc_device *rtc = wm8350->rtc.rtc; | ||
326 | int ret; | ||
327 | |||
328 | rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF); | ||
329 | |||
330 | /* Make it one shot */ | ||
331 | ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL, | ||
332 | WM8350_RTC_ALMSET); | ||
333 | if (ret != 0) { | ||
334 | dev_err(&(wm8350->rtc.pdev->dev), | ||
335 | "Failed to disable alarm: %d\n", ret); | ||
336 | } | ||
337 | } | ||
338 | |||
339 | static void wm8350_rtc_update_handler(struct wm8350 *wm8350, int irq, | ||
340 | void *data) | ||
341 | { | ||
342 | struct rtc_device *rtc = wm8350->rtc.rtc; | ||
343 | |||
344 | rtc_update_irq(rtc, 1, RTC_IRQF | RTC_UF); | ||
345 | } | ||
346 | |||
347 | static const struct rtc_class_ops wm8350_rtc_ops = { | ||
348 | .ioctl = wm8350_rtc_ioctl, | ||
349 | .read_time = wm8350_rtc_readtime, | ||
350 | .set_time = wm8350_rtc_settime, | ||
351 | .read_alarm = wm8350_rtc_readalarm, | ||
352 | .set_alarm = wm8350_rtc_setalarm, | ||
353 | }; | ||
354 | |||
355 | #ifdef CONFIG_PM | ||
356 | static int wm8350_rtc_suspend(struct platform_device *pdev, pm_message_t state) | ||
357 | { | ||
358 | struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev); | ||
359 | int ret = 0; | ||
360 | u16 reg; | ||
361 | |||
362 | reg = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL); | ||
363 | |||
364 | if (device_may_wakeup(&wm8350->rtc.pdev->dev) && | ||
365 | reg & WM8350_RTC_ALMSTS) { | ||
366 | ret = wm8350_rtc_stop_alarm(wm8350); | ||
367 | if (ret != 0) | ||
368 | dev_err(&pdev->dev, "Failed to stop RTC alarm: %d\n", | ||
369 | ret); | ||
370 | } | ||
371 | |||
372 | return ret; | ||
373 | } | ||
374 | |||
375 | static int wm8350_rtc_resume(struct platform_device *pdev) | ||
376 | { | ||
377 | struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev); | ||
378 | int ret; | ||
379 | |||
380 | if (wm8350->rtc.alarm_enabled) { | ||
381 | ret = wm8350_rtc_start_alarm(wm8350); | ||
382 | if (ret != 0) | ||
383 | dev_err(&pdev->dev, | ||
384 | "Failed to restart RTC alarm: %d\n", ret); | ||
385 | } | ||
386 | |||
387 | return 0; | ||
388 | } | ||
389 | |||
390 | #else | ||
391 | #define wm8350_rtc_suspend NULL | ||
392 | #define wm8350_rtc_resume NULL | ||
393 | #endif | ||
394 | |||
395 | static int wm8350_rtc_probe(struct platform_device *pdev) | ||
396 | { | ||
397 | struct wm8350 *wm8350 = platform_get_drvdata(pdev); | ||
398 | struct wm8350_rtc *wm_rtc = &wm8350->rtc; | ||
399 | int ret = 0; | ||
400 | u16 timectl, power5; | ||
401 | |||
402 | timectl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL); | ||
403 | if (timectl & WM8350_RTC_BCD) { | ||
404 | dev_err(&pdev->dev, "RTC BCD mode not supported\n"); | ||
405 | return -EINVAL; | ||
406 | } | ||
407 | if (timectl & WM8350_RTC_12HR) { | ||
408 | dev_err(&pdev->dev, "RTC 12 hour mode not supported\n"); | ||
409 | return -EINVAL; | ||
410 | } | ||
411 | |||
412 | /* enable the RTC if it's not already enabled */ | ||
413 | power5 = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5); | ||
414 | if (!(power5 & WM8350_RTC_TICK_ENA)) { | ||
415 | dev_info(wm8350->dev, "Starting RTC\n"); | ||
416 | |||
417 | wm8350_reg_unlock(wm8350); | ||
418 | |||
419 | ret = wm8350_set_bits(wm8350, WM8350_POWER_MGMT_5, | ||
420 | WM8350_RTC_TICK_ENA); | ||
421 | if (ret < 0) { | ||
422 | dev_err(&pdev->dev, "failed to enable RTC: %d\n", ret); | ||
423 | return ret; | ||
424 | } | ||
425 | |||
426 | wm8350_reg_lock(wm8350); | ||
427 | } | ||
428 | |||
429 | if (timectl & WM8350_RTC_STS) { | ||
430 | int retries; | ||
431 | |||
432 | ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL, | ||
433 | WM8350_RTC_SET); | ||
434 | if (ret < 0) { | ||
435 | dev_err(&pdev->dev, "failed to start: %d\n", ret); | ||
436 | return ret; | ||
437 | } | ||
438 | |||
439 | retries = WM8350_SET_TIME_RETRIES; | ||
440 | do { | ||
441 | timectl = wm8350_reg_read(wm8350, | ||
442 | WM8350_RTC_TIME_CONTROL); | ||
443 | } while (timectl & WM8350_RTC_STS && retries--); | ||
444 | |||
445 | if (retries == 0) { | ||
446 | dev_err(&pdev->dev, "failed to start: timeout\n"); | ||
447 | return -ENODEV; | ||
448 | } | ||
449 | } | ||
450 | |||
451 | device_init_wakeup(&pdev->dev, 1); | ||
452 | |||
453 | wm_rtc->rtc = rtc_device_register("wm8350", &pdev->dev, | ||
454 | &wm8350_rtc_ops, THIS_MODULE); | ||
455 | if (IS_ERR(wm_rtc->rtc)) { | ||
456 | ret = PTR_ERR(wm_rtc->rtc); | ||
457 | dev_err(&pdev->dev, "failed to register RTC: %d\n", ret); | ||
458 | return ret; | ||
459 | } | ||
460 | |||
461 | wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC); | ||
462 | wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_PER); | ||
463 | |||
464 | wm8350_register_irq(wm8350, WM8350_IRQ_RTC_SEC, | ||
465 | wm8350_rtc_update_handler, NULL); | ||
466 | |||
467 | wm8350_register_irq(wm8350, WM8350_IRQ_RTC_ALM, | ||
468 | wm8350_rtc_alarm_handler, NULL); | ||
469 | wm8350_unmask_irq(wm8350, WM8350_IRQ_RTC_ALM); | ||
470 | |||
471 | return 0; | ||
472 | } | ||
473 | |||
474 | static int __devexit wm8350_rtc_remove(struct platform_device *pdev) | ||
475 | { | ||
476 | struct wm8350 *wm8350 = platform_get_drvdata(pdev); | ||
477 | struct wm8350_rtc *wm_rtc = &wm8350->rtc; | ||
478 | |||
479 | wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC); | ||
480 | |||
481 | wm8350_free_irq(wm8350, WM8350_IRQ_RTC_SEC); | ||
482 | wm8350_free_irq(wm8350, WM8350_IRQ_RTC_ALM); | ||
483 | |||
484 | rtc_device_unregister(wm_rtc->rtc); | ||
485 | |||
486 | return 0; | ||
487 | } | ||
488 | |||
489 | static struct platform_driver wm8350_rtc_driver = { | ||
490 | .probe = wm8350_rtc_probe, | ||
491 | .remove = __devexit_p(wm8350_rtc_remove), | ||
492 | .suspend = wm8350_rtc_suspend, | ||
493 | .resume = wm8350_rtc_resume, | ||
494 | .driver = { | ||
495 | .name = "wm8350-rtc", | ||
496 | }, | ||
497 | }; | ||
498 | |||
499 | static int __init wm8350_rtc_init(void) | ||
500 | { | ||
501 | return platform_driver_register(&wm8350_rtc_driver); | ||
502 | } | ||
503 | module_init(wm8350_rtc_init); | ||
504 | |||
505 | static void __exit wm8350_rtc_exit(void) | ||
506 | { | ||
507 | platform_driver_unregister(&wm8350_rtc_driver); | ||
508 | } | ||
509 | module_exit(wm8350_rtc_exit); | ||
510 | |||
511 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); | ||
512 | MODULE_DESCRIPTION("RTC driver for the WM8350"); | ||
513 | MODULE_LICENSE("GPL"); | ||
514 | MODULE_ALIAS("platform:wm8350-rtc"); | ||