Commit | Line | Data |
---|---|---|
b5a82d62 AZ |
1 | /* |
2 | * An I2C driver for the Philips PCF8563 RTC | |
3 | * Copyright 2005-06 Tower Technologies | |
4 | * | |
5 | * Author: Alessandro Zummo <a.zummo@towertech.it> | |
6 | * Maintainers: http://www.nslu2-linux.org/ | |
7 | * | |
8 | * based on the other drivers in this same directory. | |
9 | * | |
10 | * http://www.semiconductors.philips.com/acrobat/datasheets/PCF8563-04.pdf | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License version 2 as | |
14 | * published by the Free Software Foundation. | |
15 | */ | |
16 | ||
17 | #include <linux/i2c.h> | |
18 | #include <linux/bcd.h> | |
19 | #include <linux/rtc.h> | |
5a0e3ad6 | 20 | #include <linux/slab.h> |
2113852b | 21 | #include <linux/module.h> |
8dccaf06 | 22 | #include <linux/of.h> |
23f809ee | 23 | #include <linux/err.h> |
b5a82d62 | 24 | |
e5fc9cc0 | 25 | #define DRV_VERSION "0.4.3" |
b5a82d62 AZ |
26 | |
27 | #define PCF8563_REG_ST1 0x00 /* status */ | |
28 | #define PCF8563_REG_ST2 0x01 | |
29 | ||
30 | #define PCF8563_REG_SC 0x02 /* datetime */ | |
31 | #define PCF8563_REG_MN 0x03 | |
32 | #define PCF8563_REG_HR 0x04 | |
33 | #define PCF8563_REG_DM 0x05 | |
34 | #define PCF8563_REG_DW 0x06 | |
35 | #define PCF8563_REG_MO 0x07 | |
36 | #define PCF8563_REG_YR 0x08 | |
37 | ||
38 | #define PCF8563_REG_AMN 0x09 /* alarm */ | |
39 | #define PCF8563_REG_AHR 0x0A | |
40 | #define PCF8563_REG_ADM 0x0B | |
41 | #define PCF8563_REG_ADW 0x0C | |
42 | ||
43 | #define PCF8563_REG_CLKO 0x0D /* clock out */ | |
44 | #define PCF8563_REG_TMRC 0x0E /* timer control */ | |
45 | #define PCF8563_REG_TMR 0x0F /* timer */ | |
46 | ||
47 | #define PCF8563_SC_LV 0x80 /* low voltage */ | |
48 | #define PCF8563_MO_C 0x80 /* century */ | |
49 | ||
e5fc9cc0 AZ |
50 | static struct i2c_driver pcf8563_driver; |
51 | ||
cbb94502 | 52 | struct pcf8563 { |
e5fc9cc0 | 53 | struct rtc_device *rtc; |
cbb94502 AN |
54 | /* |
55 | * The meaning of MO_C bit varies by the chip type. | |
56 | * From PCF8563 datasheet: this bit is toggled when the years | |
57 | * register overflows from 99 to 00 | |
58 | * 0 indicates the century is 20xx | |
59 | * 1 indicates the century is 19xx | |
60 | * From RTC8564 datasheet: this bit indicates change of | |
61 | * century. When the year digit data overflows from 99 to 00, | |
62 | * this bit is set. By presetting it to 0 while still in the | |
63 | * 20th century, it will be set in year 2000, ... | |
64 | * There seems no reliable way to know how the system use this | |
65 | * bit. So let's do it heuristically, assuming we are live in | |
66 | * 1970...2069. | |
67 | */ | |
68 | int c_polarity; /* 0: MO_C=1 means 19xx, otherwise MO_C=1 means 20xx */ | |
0f20b767 | 69 | int voltage_low; /* incicates if a low_voltage was detected */ |
cbb94502 AN |
70 | }; |
71 | ||
2784366c VD |
72 | static int pcf8563_read_block_data(struct i2c_client *client, unsigned char reg, |
73 | unsigned char length, unsigned char *buf) | |
b5a82d62 | 74 | { |
b5a82d62 | 75 | struct i2c_msg msgs[] = { |
755e4a4b S |
76 | {/* setup read ptr */ |
77 | .addr = client->addr, | |
78 | .len = 1, | |
2784366c | 79 | .buf = ®, |
755e4a4b | 80 | }, |
2784366c | 81 | { |
755e4a4b S |
82 | .addr = client->addr, |
83 | .flags = I2C_M_RD, | |
2784366c | 84 | .len = length, |
755e4a4b S |
85 | .buf = buf |
86 | }, | |
b5a82d62 AZ |
87 | }; |
88 | ||
b5a82d62 | 89 | if ((i2c_transfer(client->adapter, msgs, 2)) != 2) { |
2a4e2b87 | 90 | dev_err(&client->dev, "%s: read error\n", __func__); |
b5a82d62 AZ |
91 | return -EIO; |
92 | } | |
93 | ||
2784366c VD |
94 | return 0; |
95 | } | |
96 | ||
97 | static int pcf8563_write_block_data(struct i2c_client *client, | |
98 | unsigned char reg, unsigned char length, | |
99 | unsigned char *buf) | |
100 | { | |
101 | int i, err; | |
102 | ||
103 | for (i = 0; i < length; i++) { | |
104 | unsigned char data[2] = { reg + i, buf[i] }; | |
105 | ||
106 | err = i2c_master_send(client, data, sizeof(data)); | |
107 | if (err != sizeof(data)) { | |
108 | dev_err(&client->dev, | |
109 | "%s: err=%d addr=%02x, data=%02x\n", | |
110 | __func__, err, data[0], data[1]); | |
111 | return -EIO; | |
112 | } | |
113 | } | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
118 | /* | |
119 | * In the routines that deal directly with the pcf8563 hardware, we use | |
120 | * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. | |
121 | */ | |
122 | static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm) | |
123 | { | |
124 | struct pcf8563 *pcf8563 = i2c_get_clientdata(client); | |
125 | unsigned char buf[9]; | |
126 | int err; | |
127 | ||
128 | err = pcf8563_read_block_data(client, PCF8563_REG_ST1, 9, buf); | |
129 | if (err) | |
130 | return err; | |
131 | ||
0f20b767 AS |
132 | if (buf[PCF8563_REG_SC] & PCF8563_SC_LV) { |
133 | pcf8563->voltage_low = 1; | |
b5a82d62 AZ |
134 | dev_info(&client->dev, |
135 | "low voltage detected, date/time is not reliable.\n"); | |
0f20b767 | 136 | } |
b5a82d62 AZ |
137 | |
138 | dev_dbg(&client->dev, | |
139 | "%s: raw data is st1=%02x, st2=%02x, sec=%02x, min=%02x, hr=%02x, " | |
140 | "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", | |
2a4e2b87 | 141 | __func__, |
b5a82d62 AZ |
142 | buf[0], buf[1], buf[2], buf[3], |
143 | buf[4], buf[5], buf[6], buf[7], | |
144 | buf[8]); | |
145 | ||
146 | ||
fe20ba70 AB |
147 | tm->tm_sec = bcd2bin(buf[PCF8563_REG_SC] & 0x7F); |
148 | tm->tm_min = bcd2bin(buf[PCF8563_REG_MN] & 0x7F); | |
149 | tm->tm_hour = bcd2bin(buf[PCF8563_REG_HR] & 0x3F); /* rtc hr 0-23 */ | |
150 | tm->tm_mday = bcd2bin(buf[PCF8563_REG_DM] & 0x3F); | |
b5a82d62 | 151 | tm->tm_wday = buf[PCF8563_REG_DW] & 0x07; |
fe20ba70 AB |
152 | tm->tm_mon = bcd2bin(buf[PCF8563_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ |
153 | tm->tm_year = bcd2bin(buf[PCF8563_REG_YR]); | |
cbb94502 AN |
154 | if (tm->tm_year < 70) |
155 | tm->tm_year += 100; /* assume we are in 1970...2069 */ | |
156 | /* detect the polarity heuristically. see note above. */ | |
157 | pcf8563->c_polarity = (buf[PCF8563_REG_MO] & PCF8563_MO_C) ? | |
158 | (tm->tm_year >= 100) : (tm->tm_year < 100); | |
b5a82d62 AZ |
159 | |
160 | dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " | |
161 | "mday=%d, mon=%d, year=%d, wday=%d\n", | |
2a4e2b87 | 162 | __func__, |
b5a82d62 AZ |
163 | tm->tm_sec, tm->tm_min, tm->tm_hour, |
164 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | |
165 | ||
166 | /* the clock can give out invalid datetime, but we cannot return | |
167 | * -EINVAL otherwise hwclock will refuse to set the time on bootup. | |
168 | */ | |
169 | if (rtc_valid_tm(tm) < 0) | |
170 | dev_err(&client->dev, "retrieved date/time is not valid.\n"); | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm) | |
176 | { | |
e5fc9cc0 | 177 | struct pcf8563 *pcf8563 = i2c_get_clientdata(client); |
2784366c | 178 | int err; |
b5a82d62 AZ |
179 | unsigned char buf[9]; |
180 | ||
181 | dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, " | |
182 | "mday=%d, mon=%d, year=%d, wday=%d\n", | |
2a4e2b87 | 183 | __func__, |
b5a82d62 AZ |
184 | tm->tm_sec, tm->tm_min, tm->tm_hour, |
185 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | |
186 | ||
187 | /* hours, minutes and seconds */ | |
fe20ba70 AB |
188 | buf[PCF8563_REG_SC] = bin2bcd(tm->tm_sec); |
189 | buf[PCF8563_REG_MN] = bin2bcd(tm->tm_min); | |
190 | buf[PCF8563_REG_HR] = bin2bcd(tm->tm_hour); | |
b5a82d62 | 191 | |
fe20ba70 | 192 | buf[PCF8563_REG_DM] = bin2bcd(tm->tm_mday); |
b5a82d62 AZ |
193 | |
194 | /* month, 1 - 12 */ | |
fe20ba70 | 195 | buf[PCF8563_REG_MO] = bin2bcd(tm->tm_mon + 1); |
b5a82d62 AZ |
196 | |
197 | /* year and century */ | |
fe20ba70 | 198 | buf[PCF8563_REG_YR] = bin2bcd(tm->tm_year % 100); |
cbb94502 | 199 | if (pcf8563->c_polarity ? (tm->tm_year >= 100) : (tm->tm_year < 100)) |
b5a82d62 AZ |
200 | buf[PCF8563_REG_MO] |= PCF8563_MO_C; |
201 | ||
202 | buf[PCF8563_REG_DW] = tm->tm_wday & 0x07; | |
203 | ||
2784366c VD |
204 | err = pcf8563_write_block_data(client, PCF8563_REG_SC, |
205 | 9 - PCF8563_REG_SC, buf + PCF8563_REG_SC); | |
206 | if (err) | |
207 | return err; | |
b5a82d62 AZ |
208 | |
209 | return 0; | |
210 | } | |
211 | ||
0f20b767 AS |
212 | #ifdef CONFIG_RTC_INTF_DEV |
213 | static int pcf8563_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) | |
214 | { | |
215 | struct pcf8563 *pcf8563 = i2c_get_clientdata(to_i2c_client(dev)); | |
216 | struct rtc_time tm; | |
217 | ||
218 | switch (cmd) { | |
219 | case RTC_VL_READ: | |
220 | if (pcf8563->voltage_low) | |
221 | dev_info(dev, "low voltage detected, date/time is not reliable.\n"); | |
222 | ||
223 | if (copy_to_user((void __user *)arg, &pcf8563->voltage_low, | |
224 | sizeof(int))) | |
225 | return -EFAULT; | |
226 | return 0; | |
227 | case RTC_VL_CLR: | |
228 | /* | |
229 | * Clear the VL bit in the seconds register in case | |
230 | * the time has not been set already (which would | |
231 | * have cleared it). This does not really matter | |
232 | * because of the cached voltage_low value but do it | |
233 | * anyway for consistency. | |
234 | */ | |
235 | if (pcf8563_get_datetime(to_i2c_client(dev), &tm)) | |
236 | pcf8563_set_datetime(to_i2c_client(dev), &tm); | |
237 | ||
238 | /* Clear the cached value. */ | |
239 | pcf8563->voltage_low = 0; | |
240 | ||
241 | return 0; | |
242 | default: | |
243 | return -ENOIOCTLCMD; | |
244 | } | |
245 | } | |
246 | #else | |
247 | #define pcf8563_rtc_ioctl NULL | |
248 | #endif | |
249 | ||
b5a82d62 AZ |
250 | static int pcf8563_rtc_read_time(struct device *dev, struct rtc_time *tm) |
251 | { | |
252 | return pcf8563_get_datetime(to_i2c_client(dev), tm); | |
253 | } | |
254 | ||
255 | static int pcf8563_rtc_set_time(struct device *dev, struct rtc_time *tm) | |
256 | { | |
257 | return pcf8563_set_datetime(to_i2c_client(dev), tm); | |
258 | } | |
259 | ||
ff8371ac | 260 | static const struct rtc_class_ops pcf8563_rtc_ops = { |
0f20b767 | 261 | .ioctl = pcf8563_rtc_ioctl, |
b5a82d62 AZ |
262 | .read_time = pcf8563_rtc_read_time, |
263 | .set_time = pcf8563_rtc_set_time, | |
264 | }; | |
265 | ||
d2653e92 JD |
266 | static int pcf8563_probe(struct i2c_client *client, |
267 | const struct i2c_device_id *id) | |
b5a82d62 | 268 | { |
cbb94502 | 269 | struct pcf8563 *pcf8563; |
b5a82d62 | 270 | |
e5fc9cc0 | 271 | dev_dbg(&client->dev, "%s\n", __func__); |
b5a82d62 | 272 | |
e5fc9cc0 AZ |
273 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
274 | return -ENODEV; | |
b5a82d62 | 275 | |
d6fbdc34 JH |
276 | pcf8563 = devm_kzalloc(&client->dev, sizeof(struct pcf8563), |
277 | GFP_KERNEL); | |
e5fc9cc0 AZ |
278 | if (!pcf8563) |
279 | return -ENOMEM; | |
b5a82d62 | 280 | |
b5a82d62 AZ |
281 | dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); |
282 | ||
b74d2caa AZ |
283 | i2c_set_clientdata(client, pcf8563); |
284 | ||
d6fbdc34 JH |
285 | pcf8563->rtc = devm_rtc_device_register(&client->dev, |
286 | pcf8563_driver.driver.name, | |
287 | &pcf8563_rtc_ops, THIS_MODULE); | |
b5a82d62 | 288 | |
dac30a98 | 289 | return PTR_ERR_OR_ZERO(pcf8563->rtc); |
b5a82d62 AZ |
290 | } |
291 | ||
3760f736 JD |
292 | static const struct i2c_device_id pcf8563_id[] = { |
293 | { "pcf8563", 0 }, | |
8ea9212c | 294 | { "rtc8564", 0 }, |
3760f736 JD |
295 | { } |
296 | }; | |
297 | MODULE_DEVICE_TABLE(i2c, pcf8563_id); | |
298 | ||
8dccaf06 | 299 | #ifdef CONFIG_OF |
5a167f45 | 300 | static const struct of_device_id pcf8563_of_match[] = { |
8dccaf06 NB |
301 | { .compatible = "nxp,pcf8563" }, |
302 | {} | |
303 | }; | |
304 | MODULE_DEVICE_TABLE(of, pcf8563_of_match); | |
305 | #endif | |
306 | ||
e5fc9cc0 AZ |
307 | static struct i2c_driver pcf8563_driver = { |
308 | .driver = { | |
309 | .name = "rtc-pcf8563", | |
0a25bf40 | 310 | .owner = THIS_MODULE, |
8dccaf06 | 311 | .of_match_table = of_match_ptr(pcf8563_of_match), |
e5fc9cc0 AZ |
312 | }, |
313 | .probe = pcf8563_probe, | |
3760f736 | 314 | .id_table = pcf8563_id, |
e5fc9cc0 AZ |
315 | }; |
316 | ||
0abc9201 | 317 | module_i2c_driver(pcf8563_driver); |
b5a82d62 AZ |
318 | |
319 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); | |
320 | MODULE_DESCRIPTION("Philips PCF8563/Epson RTC8564 RTC driver"); | |
321 | MODULE_LICENSE("GPL"); | |
322 | MODULE_VERSION(DRV_VERSION); |