]>
Commit | Line | Data |
---|---|---|
18cb6368 RC |
1 | /* |
2 | * An I2C driver for the NXP PCF2127 RTC | |
3 | * Copyright 2013 Til-Technologies | |
4 | * | |
5 | * Author: Renaud Cerrato <r.cerrato@til-technologies.fr> | |
6 | * | |
7 | * based on the other drivers in this same directory. | |
8 | * | |
9 | * http://www.nxp.com/documents/data_sheet/PCF2127AT.pdf | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License version 2 as | |
13 | * published by the Free Software Foundation. | |
14 | */ | |
15 | ||
16 | #include <linux/i2c.h> | |
17 | #include <linux/bcd.h> | |
18 | #include <linux/rtc.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/of.h> | |
907b3262 | 22 | #include <linux/regmap.h> |
18cb6368 | 23 | |
18cb6368 RC |
24 | #define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */ |
25 | #define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */ | |
f97cfddc | 26 | |
18cb6368 | 27 | #define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */ |
f97cfddc UKK |
28 | #define PCF2127_REG_CTRL3_BLF BIT(2) |
29 | ||
18cb6368 RC |
30 | #define PCF2127_REG_SC (0x03) /* datetime */ |
31 | #define PCF2127_REG_MN (0x04) | |
32 | #define PCF2127_REG_HR (0x05) | |
33 | #define PCF2127_REG_DM (0x06) | |
34 | #define PCF2127_REG_DW (0x07) | |
35 | #define PCF2127_REG_MO (0x08) | |
36 | #define PCF2127_REG_YR (0x09) | |
37 | ||
653ebd75 AS |
38 | #define PCF2127_OSF BIT(7) /* Oscillator Fail flag */ |
39 | ||
18cb6368 RC |
40 | struct pcf2127 { |
41 | struct rtc_device *rtc; | |
907b3262 | 42 | struct regmap *regmap; |
18cb6368 RC |
43 | }; |
44 | ||
45 | /* | |
46 | * In the routines that deal directly with the pcf2127 hardware, we use | |
47 | * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. | |
48 | */ | |
907b3262 | 49 | static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) |
18cb6368 | 50 | { |
907b3262 AM |
51 | struct pcf2127 *pcf2127 = dev_get_drvdata(dev); |
52 | unsigned char buf[10]; | |
53 | int ret; | |
18cb6368 | 54 | |
907b3262 AM |
55 | ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL1, buf, |
56 | sizeof(buf)); | |
57 | if (ret) { | |
58 | dev_err(dev, "%s: read error\n", __func__); | |
59 | return ret; | |
18cb6368 RC |
60 | } |
61 | ||
f97cfddc | 62 | if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF) |
907b3262 | 63 | dev_info(dev, |
653ebd75 | 64 | "low voltage detected, check/replace RTC battery.\n"); |
653ebd75 AS |
65 | |
66 | if (buf[PCF2127_REG_SC] & PCF2127_OSF) { | |
67 | /* | |
68 | * no need clear the flag here, | |
69 | * it will be cleared once the new date is saved | |
70 | */ | |
907b3262 | 71 | dev_warn(dev, |
653ebd75 AS |
72 | "oscillator stop detected, date/time is not reliable\n"); |
73 | return -EINVAL; | |
18cb6368 RC |
74 | } |
75 | ||
907b3262 | 76 | dev_dbg(dev, |
18cb6368 RC |
77 | "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, " |
78 | "sec=%02x, min=%02x, hr=%02x, " | |
79 | "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", | |
80 | __func__, | |
81 | buf[0], buf[1], buf[2], | |
82 | buf[3], buf[4], buf[5], | |
83 | buf[6], buf[7], buf[8], buf[9]); | |
84 | ||
85 | ||
86 | tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F); | |
87 | tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F); | |
88 | tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */ | |
89 | tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F); | |
90 | tm->tm_wday = buf[PCF2127_REG_DW] & 0x07; | |
91 | tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ | |
92 | tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]); | |
93 | if (tm->tm_year < 70) | |
94 | tm->tm_year += 100; /* assume we are in 1970...2069 */ | |
95 | ||
907b3262 | 96 | dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " |
18cb6368 RC |
97 | "mday=%d, mon=%d, year=%d, wday=%d\n", |
98 | __func__, | |
99 | tm->tm_sec, tm->tm_min, tm->tm_hour, | |
100 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | |
101 | ||
821f51c4 | 102 | return rtc_valid_tm(tm); |
18cb6368 RC |
103 | } |
104 | ||
907b3262 | 105 | static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm) |
18cb6368 | 106 | { |
907b3262 AM |
107 | struct pcf2127 *pcf2127 = dev_get_drvdata(dev); |
108 | unsigned char buf[7]; | |
18cb6368 RC |
109 | int i = 0, err; |
110 | ||
907b3262 | 111 | dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, " |
18cb6368 RC |
112 | "mday=%d, mon=%d, year=%d, wday=%d\n", |
113 | __func__, | |
114 | tm->tm_sec, tm->tm_min, tm->tm_hour, | |
115 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | |
116 | ||
18cb6368 | 117 | /* hours, minutes and seconds */ |
653ebd75 | 118 | buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */ |
18cb6368 RC |
119 | buf[i++] = bin2bcd(tm->tm_min); |
120 | buf[i++] = bin2bcd(tm->tm_hour); | |
121 | buf[i++] = bin2bcd(tm->tm_mday); | |
122 | buf[i++] = tm->tm_wday & 0x07; | |
123 | ||
124 | /* month, 1 - 12 */ | |
125 | buf[i++] = bin2bcd(tm->tm_mon + 1); | |
126 | ||
127 | /* year */ | |
128 | buf[i++] = bin2bcd(tm->tm_year % 100); | |
129 | ||
130 | /* write register's data */ | |
907b3262 AM |
131 | err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i); |
132 | if (err) { | |
133 | dev_err(dev, | |
18cb6368 | 134 | "%s: err=%d", __func__, err); |
907b3262 | 135 | return err; |
18cb6368 RC |
136 | } |
137 | ||
138 | return 0; | |
139 | } | |
140 | ||
141 | #ifdef CONFIG_RTC_INTF_DEV | |
142 | static int pcf2127_rtc_ioctl(struct device *dev, | |
143 | unsigned int cmd, unsigned long arg) | |
144 | { | |
907b3262 | 145 | struct pcf2127 *pcf2127 = dev_get_drvdata(dev); |
f97cfddc UKK |
146 | int touser; |
147 | int ret; | |
18cb6368 RC |
148 | |
149 | switch (cmd) { | |
150 | case RTC_VL_READ: | |
907b3262 AM |
151 | ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser); |
152 | if (ret) | |
f97cfddc UKK |
153 | return ret; |
154 | ||
907b3262 | 155 | touser = touser & PCF2127_REG_CTRL3_BLF ? 1 : 0; |
18cb6368 | 156 | |
f97cfddc | 157 | if (copy_to_user((void __user *)arg, &touser, sizeof(int))) |
18cb6368 RC |
158 | return -EFAULT; |
159 | return 0; | |
160 | default: | |
161 | return -ENOIOCTLCMD; | |
162 | } | |
163 | } | |
164 | #else | |
165 | #define pcf2127_rtc_ioctl NULL | |
166 | #endif | |
167 | ||
18cb6368 RC |
168 | static const struct rtc_class_ops pcf2127_rtc_ops = { |
169 | .ioctl = pcf2127_rtc_ioctl, | |
170 | .read_time = pcf2127_rtc_read_time, | |
171 | .set_time = pcf2127_rtc_set_time, | |
172 | }; | |
173 | ||
907b3262 AM |
174 | static int pcf2127_probe(struct device *dev, struct regmap *regmap, |
175 | const char *name) | |
18cb6368 RC |
176 | { |
177 | struct pcf2127 *pcf2127; | |
178 | ||
907b3262 | 179 | dev_dbg(dev, "%s\n", __func__); |
18cb6368 | 180 | |
907b3262 | 181 | pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL); |
18cb6368 RC |
182 | if (!pcf2127) |
183 | return -ENOMEM; | |
184 | ||
907b3262 | 185 | pcf2127->regmap = regmap; |
18cb6368 | 186 | |
907b3262 AM |
187 | dev_set_drvdata(dev, pcf2127); |
188 | ||
189 | pcf2127->rtc = devm_rtc_device_register(dev, name, &pcf2127_rtc_ops, | |
190 | THIS_MODULE); | |
18cb6368 | 191 | |
7ab26cd1 | 192 | return PTR_ERR_OR_ZERO(pcf2127->rtc); |
18cb6368 RC |
193 | } |
194 | ||
18cb6368 RC |
195 | #ifdef CONFIG_OF |
196 | static const struct of_device_id pcf2127_of_match[] = { | |
197 | { .compatible = "nxp,pcf2127" }, | |
198 | {} | |
199 | }; | |
200 | MODULE_DEVICE_TABLE(of, pcf2127_of_match); | |
201 | #endif | |
202 | ||
907b3262 AM |
203 | static int pcf2127_i2c_write(void *context, const void *data, size_t count) |
204 | { | |
205 | struct device *dev = context; | |
206 | struct i2c_client *client = to_i2c_client(dev); | |
207 | int ret; | |
208 | ||
209 | ret = i2c_master_send(client, data, count); | |
210 | if (ret != count) | |
211 | return ret < 0 ? ret : -EIO; | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
216 | static int pcf2127_i2c_gather_write(void *context, | |
217 | const void *reg, size_t reg_size, | |
218 | const void *val, size_t val_size) | |
219 | { | |
220 | struct device *dev = context; | |
221 | struct i2c_client *client = to_i2c_client(dev); | |
222 | int ret; | |
223 | void *buf; | |
224 | ||
225 | if (WARN_ON(reg_size != 1)) | |
226 | return -EINVAL; | |
227 | ||
228 | buf = kmalloc(val_size + 1, GFP_KERNEL); | |
229 | if (!buf) | |
230 | return -ENOMEM; | |
231 | ||
232 | memcpy(buf, reg, 1); | |
233 | memcpy(buf + 1, val, val_size); | |
234 | ||
235 | ret = i2c_master_send(client, buf, val_size + 1); | |
236 | if (ret != val_size + 1) | |
237 | return ret < 0 ? ret : -EIO; | |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
242 | static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size, | |
243 | void *val, size_t val_size) | |
244 | { | |
245 | struct device *dev = context; | |
246 | struct i2c_client *client = to_i2c_client(dev); | |
247 | int ret; | |
248 | ||
249 | if (WARN_ON(reg_size != 1)) | |
250 | return -EINVAL; | |
251 | ||
252 | ret = i2c_master_send(client, reg, 1); | |
253 | if (ret != 1) | |
254 | return ret < 0 ? ret : -EIO; | |
255 | ||
256 | ret = i2c_master_recv(client, val, val_size); | |
257 | if (ret != val_size) | |
258 | return ret < 0 ? ret : -EIO; | |
259 | ||
260 | return 0; | |
261 | } | |
262 | ||
263 | /* | |
264 | * The reason we need this custom regmap_bus instead of using regmap_init_i2c() | |
265 | * is that the STOP condition is required between set register address and | |
266 | * read register data when reading from registers. | |
267 | */ | |
268 | static const struct regmap_bus pcf2127_i2c_regmap = { | |
269 | .write = pcf2127_i2c_write, | |
270 | .gather_write = pcf2127_i2c_gather_write, | |
271 | .read = pcf2127_i2c_read, | |
272 | }; | |
273 | ||
274 | static struct i2c_driver pcf2127_i2c_driver; | |
275 | ||
276 | static int pcf2127_i2c_probe(struct i2c_client *client, | |
277 | const struct i2c_device_id *id) | |
278 | { | |
279 | struct regmap *regmap; | |
280 | static const struct regmap_config config = { | |
281 | .reg_bits = 8, | |
282 | .val_bits = 8, | |
283 | }; | |
284 | ||
285 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) | |
286 | return -ENODEV; | |
287 | ||
288 | regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap, | |
289 | &client->dev, &config); | |
290 | if (IS_ERR(regmap)) { | |
291 | dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", | |
292 | __func__, PTR_ERR(regmap)); | |
293 | return PTR_ERR(regmap); | |
294 | } | |
295 | ||
296 | return pcf2127_probe(&client->dev, regmap, | |
297 | pcf2127_i2c_driver.driver.name); | |
298 | } | |
299 | ||
300 | static const struct i2c_device_id pcf2127_i2c_id[] = { | |
301 | { "pcf2127", 0 }, | |
302 | { } | |
303 | }; | |
304 | MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id); | |
305 | ||
306 | static struct i2c_driver pcf2127_i2c_driver = { | |
18cb6368 | 307 | .driver = { |
907b3262 | 308 | .name = "rtc-pcf2127-i2c", |
18cb6368 RC |
309 | .of_match_table = of_match_ptr(pcf2127_of_match), |
310 | }, | |
907b3262 AM |
311 | .probe = pcf2127_i2c_probe, |
312 | .id_table = pcf2127_i2c_id, | |
18cb6368 | 313 | }; |
907b3262 | 314 | module_i2c_driver(pcf2127_i2c_driver); |
18cb6368 RC |
315 | |
316 | MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>"); | |
317 | MODULE_DESCRIPTION("NXP PCF2127 RTC driver"); | |
4d8318bc | 318 | MODULE_LICENSE("GPL v2"); |