Commit | Line | Data |
---|---|---|
4d61ff6b PDM |
1 | /* |
2 | * A driver for the I2C members of the Abracon AB x8xx RTC family, | |
3 | * and compatible: AB 1805 and AB 0805 | |
4 | * | |
5 | * Copyright 2014-2015 Macq S.A. | |
6 | * | |
7 | * Author: Philippe De Muyter <phdm@macqel.be> | |
8 | * Author: Alexandre Belloni <alexandre.belloni@free-electrons.com> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | * | |
14 | */ | |
15 | ||
16 | #include <linux/bcd.h> | |
17 | #include <linux/i2c.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/rtc.h> | |
20 | ||
21 | #define ABX8XX_REG_HTH 0x00 | |
22 | #define ABX8XX_REG_SC 0x01 | |
23 | #define ABX8XX_REG_MN 0x02 | |
24 | #define ABX8XX_REG_HR 0x03 | |
25 | #define ABX8XX_REG_DA 0x04 | |
26 | #define ABX8XX_REG_MO 0x05 | |
27 | #define ABX8XX_REG_YR 0x06 | |
28 | #define ABX8XX_REG_WD 0x07 | |
29 | ||
718a820a AB |
30 | #define ABX8XX_REG_AHTH 0x08 |
31 | #define ABX8XX_REG_ASC 0x09 | |
32 | #define ABX8XX_REG_AMN 0x0a | |
33 | #define ABX8XX_REG_AHR 0x0b | |
34 | #define ABX8XX_REG_ADA 0x0c | |
35 | #define ABX8XX_REG_AMO 0x0d | |
36 | #define ABX8XX_REG_AWD 0x0e | |
37 | ||
38 | #define ABX8XX_REG_STATUS 0x0f | |
39 | #define ABX8XX_STATUS_AF BIT(2) | |
40 | ||
4d61ff6b | 41 | #define ABX8XX_REG_CTRL1 0x10 |
5f1b2f77 | 42 | #define ABX8XX_CTRL_WRITE BIT(0) |
718a820a | 43 | #define ABX8XX_CTRL_ARST BIT(2) |
4d61ff6b PDM |
44 | #define ABX8XX_CTRL_12_24 BIT(6) |
45 | ||
718a820a AB |
46 | #define ABX8XX_REG_IRQ 0x12 |
47 | #define ABX8XX_IRQ_AIE BIT(2) | |
48 | #define ABX8XX_IRQ_IM_1_4 (0x3 << 5) | |
49 | ||
50 | #define ABX8XX_REG_CD_TIMER_CTL 0x18 | |
51 | ||
59a8383a MJ |
52 | #define ABX8XX_REG_OSC 0x1c |
53 | #define ABX8XX_OSC_FOS BIT(3) | |
54 | #define ABX8XX_OSC_BOS BIT(4) | |
55 | #define ABX8XX_OSC_ACAL_512 BIT(5) | |
56 | #define ABX8XX_OSC_ACAL_1024 BIT(6) | |
57 | ||
58 | #define ABX8XX_OSC_OSEL BIT(7) | |
59 | ||
60 | #define ABX8XX_REG_OSS 0x1d | |
61 | #define ABX8XX_OSS_OMODE BIT(4) | |
62 | ||
4d61ff6b | 63 | #define ABX8XX_REG_CFG_KEY 0x1f |
59a8383a | 64 | #define ABX8XX_CFG_KEY_OSC 0xa1 |
4d61ff6b PDM |
65 | #define ABX8XX_CFG_KEY_MISC 0x9d |
66 | ||
67 | #define ABX8XX_REG_ID0 0x28 | |
68 | ||
69 | #define ABX8XX_REG_TRICKLE 0x20 | |
70 | #define ABX8XX_TRICKLE_CHARGE_ENABLE 0xa0 | |
71 | #define ABX8XX_TRICKLE_STANDARD_DIODE 0x8 | |
72 | #define ABX8XX_TRICKLE_SCHOTTKY_DIODE 0x4 | |
73 | ||
74 | static u8 trickle_resistors[] = {0, 3, 6, 11}; | |
75 | ||
76 | enum abx80x_chip {AB0801, AB0803, AB0804, AB0805, | |
77 | AB1801, AB1803, AB1804, AB1805, ABX80X}; | |
78 | ||
79 | struct abx80x_cap { | |
80 | u16 pn; | |
81 | bool has_tc; | |
82 | }; | |
83 | ||
84 | static struct abx80x_cap abx80x_caps[] = { | |
85 | [AB0801] = {.pn = 0x0801}, | |
86 | [AB0803] = {.pn = 0x0803}, | |
87 | [AB0804] = {.pn = 0x0804, .has_tc = true}, | |
88 | [AB0805] = {.pn = 0x0805, .has_tc = true}, | |
89 | [AB1801] = {.pn = 0x1801}, | |
90 | [AB1803] = {.pn = 0x1803}, | |
91 | [AB1804] = {.pn = 0x1804, .has_tc = true}, | |
92 | [AB1805] = {.pn = 0x1805, .has_tc = true}, | |
93 | [ABX80X] = {.pn = 0} | |
94 | }; | |
95 | ||
59a8383a MJ |
96 | static int abx80x_is_rc_mode(struct i2c_client *client) |
97 | { | |
98 | int flags = 0; | |
99 | ||
100 | flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS); | |
101 | if (flags < 0) { | |
102 | dev_err(&client->dev, | |
103 | "Failed to read autocalibration attribute\n"); | |
104 | return flags; | |
105 | } | |
106 | ||
107 | return (flags & ABX8XX_OSS_OMODE) ? 1 : 0; | |
108 | } | |
109 | ||
4d61ff6b PDM |
110 | static int abx80x_enable_trickle_charger(struct i2c_client *client, |
111 | u8 trickle_cfg) | |
112 | { | |
113 | int err; | |
114 | ||
115 | /* | |
116 | * Write the configuration key register to enable access to the Trickle | |
117 | * register | |
118 | */ | |
119 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, | |
120 | ABX8XX_CFG_KEY_MISC); | |
121 | if (err < 0) { | |
122 | dev_err(&client->dev, "Unable to write configuration key\n"); | |
123 | return -EIO; | |
124 | } | |
125 | ||
126 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE, | |
127 | ABX8XX_TRICKLE_CHARGE_ENABLE | | |
128 | trickle_cfg); | |
129 | if (err < 0) { | |
130 | dev_err(&client->dev, "Unable to write trickle register\n"); | |
131 | return -EIO; | |
132 | } | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm) | |
138 | { | |
139 | struct i2c_client *client = to_i2c_client(dev); | |
140 | unsigned char buf[8]; | |
141 | int err; | |
142 | ||
143 | err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH, | |
144 | sizeof(buf), buf); | |
145 | if (err < 0) { | |
146 | dev_err(&client->dev, "Unable to read date\n"); | |
147 | return -EIO; | |
148 | } | |
149 | ||
150 | tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F); | |
151 | tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F); | |
152 | tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F); | |
153 | tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7; | |
154 | tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F); | |
155 | tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1; | |
156 | tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100; | |
157 | ||
158 | err = rtc_valid_tm(tm); | |
159 | if (err < 0) | |
160 | dev_err(&client->dev, "retrieved date/time is not valid.\n"); | |
161 | ||
162 | return err; | |
163 | } | |
164 | ||
165 | static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm) | |
166 | { | |
167 | struct i2c_client *client = to_i2c_client(dev); | |
168 | unsigned char buf[8]; | |
169 | int err; | |
170 | ||
171 | if (tm->tm_year < 100) | |
172 | return -EINVAL; | |
173 | ||
174 | buf[ABX8XX_REG_HTH] = 0; | |
175 | buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec); | |
176 | buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min); | |
177 | buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour); | |
178 | buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday); | |
179 | buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1); | |
180 | buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100); | |
181 | buf[ABX8XX_REG_WD] = tm->tm_wday; | |
182 | ||
183 | err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH, | |
184 | sizeof(buf), buf); | |
185 | if (err < 0) { | |
186 | dev_err(&client->dev, "Unable to write to date registers\n"); | |
187 | return -EIO; | |
188 | } | |
189 | ||
190 | return 0; | |
191 | } | |
192 | ||
718a820a AB |
193 | static irqreturn_t abx80x_handle_irq(int irq, void *dev_id) |
194 | { | |
195 | struct i2c_client *client = dev_id; | |
196 | struct rtc_device *rtc = i2c_get_clientdata(client); | |
197 | int status; | |
198 | ||
199 | status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS); | |
200 | if (status < 0) | |
201 | return IRQ_NONE; | |
202 | ||
203 | if (status & ABX8XX_STATUS_AF) | |
204 | rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF); | |
205 | ||
206 | i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0); | |
207 | ||
208 | return IRQ_HANDLED; | |
209 | } | |
210 | ||
211 | static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t) | |
212 | { | |
213 | struct i2c_client *client = to_i2c_client(dev); | |
214 | unsigned char buf[7]; | |
215 | ||
216 | int irq_mask, err; | |
217 | ||
218 | if (client->irq <= 0) | |
219 | return -EINVAL; | |
220 | ||
221 | err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC, | |
222 | sizeof(buf), buf); | |
223 | if (err) | |
224 | return err; | |
225 | ||
226 | irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ); | |
227 | if (irq_mask < 0) | |
228 | return irq_mask; | |
229 | ||
230 | t->time.tm_sec = bcd2bin(buf[0] & 0x7F); | |
231 | t->time.tm_min = bcd2bin(buf[1] & 0x7F); | |
232 | t->time.tm_hour = bcd2bin(buf[2] & 0x3F); | |
233 | t->time.tm_mday = bcd2bin(buf[3] & 0x3F); | |
234 | t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1; | |
235 | t->time.tm_wday = buf[5] & 0x7; | |
236 | ||
237 | t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE); | |
238 | t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled; | |
239 | ||
240 | return err; | |
241 | } | |
242 | ||
243 | static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t) | |
244 | { | |
245 | struct i2c_client *client = to_i2c_client(dev); | |
246 | u8 alarm[6]; | |
247 | int err; | |
248 | ||
249 | if (client->irq <= 0) | |
250 | return -EINVAL; | |
251 | ||
252 | alarm[0] = 0x0; | |
253 | alarm[1] = bin2bcd(t->time.tm_sec); | |
254 | alarm[2] = bin2bcd(t->time.tm_min); | |
255 | alarm[3] = bin2bcd(t->time.tm_hour); | |
256 | alarm[4] = bin2bcd(t->time.tm_mday); | |
257 | alarm[5] = bin2bcd(t->time.tm_mon + 1); | |
258 | ||
259 | err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH, | |
260 | sizeof(alarm), alarm); | |
261 | if (err < 0) { | |
262 | dev_err(&client->dev, "Unable to write alarm registers\n"); | |
263 | return -EIO; | |
264 | } | |
265 | ||
266 | if (t->enabled) { | |
267 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ, | |
268 | (ABX8XX_IRQ_IM_1_4 | | |
269 | ABX8XX_IRQ_AIE)); | |
270 | if (err) | |
271 | return err; | |
272 | } | |
273 | ||
274 | return 0; | |
275 | } | |
276 | ||
59a8383a MJ |
277 | static int abx80x_rtc_set_autocalibration(struct device *dev, |
278 | int autocalibration) | |
279 | { | |
280 | struct i2c_client *client = to_i2c_client(dev); | |
281 | int retval, flags = 0; | |
282 | ||
283 | if ((autocalibration != 0) && (autocalibration != 1024) && | |
284 | (autocalibration != 512)) { | |
285 | dev_err(dev, "autocalibration value outside permitted range\n"); | |
286 | return -EINVAL; | |
287 | } | |
288 | ||
289 | flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC); | |
290 | if (flags < 0) | |
291 | return flags; | |
292 | ||
293 | if (autocalibration == 0) { | |
294 | flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024); | |
295 | } else if (autocalibration == 1024) { | |
296 | /* 1024 autocalibration is 0x10 */ | |
297 | flags |= ABX8XX_OSC_ACAL_1024; | |
298 | flags &= ~(ABX8XX_OSC_ACAL_512); | |
299 | } else { | |
300 | /* 512 autocalibration is 0x11 */ | |
301 | flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512); | |
302 | } | |
303 | ||
304 | /* Unlock write access to Oscillator Control Register */ | |
305 | retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, | |
306 | ABX8XX_CFG_KEY_OSC); | |
307 | if (retval < 0) { | |
308 | dev_err(dev, "Failed to write CONFIG_KEY register\n"); | |
309 | return retval; | |
310 | } | |
311 | ||
312 | retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags); | |
313 | ||
314 | return retval; | |
315 | } | |
316 | ||
317 | static int abx80x_rtc_get_autocalibration(struct device *dev) | |
318 | { | |
319 | struct i2c_client *client = to_i2c_client(dev); | |
320 | int flags = 0, autocalibration; | |
321 | ||
322 | flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC); | |
323 | if (flags < 0) | |
324 | return flags; | |
325 | ||
326 | if (flags & ABX8XX_OSC_ACAL_512) | |
327 | autocalibration = 512; | |
328 | else if (flags & ABX8XX_OSC_ACAL_1024) | |
329 | autocalibration = 1024; | |
330 | else | |
331 | autocalibration = 0; | |
332 | ||
333 | return autocalibration; | |
334 | } | |
335 | ||
336 | static ssize_t autocalibration_store(struct device *dev, | |
337 | struct device_attribute *attr, | |
338 | const char *buf, size_t count) | |
339 | { | |
340 | int retval; | |
341 | unsigned long autocalibration = 0; | |
342 | ||
343 | retval = kstrtoul(buf, 10, &autocalibration); | |
344 | if (retval < 0) { | |
345 | dev_err(dev, "Failed to store RTC autocalibration attribute\n"); | |
346 | return -EINVAL; | |
347 | } | |
348 | ||
349 | retval = abx80x_rtc_set_autocalibration(dev, autocalibration); | |
350 | ||
351 | return retval ? retval : count; | |
352 | } | |
353 | ||
354 | static ssize_t autocalibration_show(struct device *dev, | |
355 | struct device_attribute *attr, char *buf) | |
356 | { | |
357 | int autocalibration = 0; | |
358 | ||
359 | autocalibration = abx80x_rtc_get_autocalibration(dev); | |
360 | if (autocalibration < 0) { | |
361 | dev_err(dev, "Failed to read RTC autocalibration\n"); | |
362 | sprintf(buf, "0\n"); | |
363 | return autocalibration; | |
364 | } | |
365 | ||
366 | return sprintf(buf, "%d\n", autocalibration); | |
367 | } | |
368 | ||
369 | static DEVICE_ATTR_RW(autocalibration); | |
370 | ||
371 | static ssize_t oscillator_store(struct device *dev, | |
372 | struct device_attribute *attr, | |
373 | const char *buf, size_t count) | |
374 | { | |
375 | struct i2c_client *client = to_i2c_client(dev); | |
376 | int retval, flags, rc_mode = 0; | |
377 | ||
378 | if (strncmp(buf, "rc", 2) == 0) { | |
379 | rc_mode = 1; | |
380 | } else if (strncmp(buf, "xtal", 4) == 0) { | |
381 | rc_mode = 0; | |
382 | } else { | |
383 | dev_err(dev, "Oscillator selection value outside permitted ones\n"); | |
384 | return -EINVAL; | |
385 | } | |
386 | ||
387 | flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC); | |
388 | if (flags < 0) | |
389 | return flags; | |
390 | ||
391 | if (rc_mode == 0) | |
392 | flags &= ~(ABX8XX_OSC_OSEL); | |
393 | else | |
394 | flags |= (ABX8XX_OSC_OSEL); | |
395 | ||
396 | /* Unlock write access on Oscillator Control register */ | |
397 | retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, | |
398 | ABX8XX_CFG_KEY_OSC); | |
399 | if (retval < 0) { | |
400 | dev_err(dev, "Failed to write CONFIG_KEY register\n"); | |
401 | return retval; | |
402 | } | |
403 | ||
404 | retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags); | |
405 | if (retval < 0) { | |
406 | dev_err(dev, "Failed to write Oscillator Control register\n"); | |
407 | return retval; | |
408 | } | |
409 | ||
410 | return retval ? retval : count; | |
411 | } | |
412 | ||
413 | static ssize_t oscillator_show(struct device *dev, | |
414 | struct device_attribute *attr, char *buf) | |
415 | { | |
416 | int rc_mode = 0; | |
417 | struct i2c_client *client = to_i2c_client(dev); | |
418 | ||
419 | rc_mode = abx80x_is_rc_mode(client); | |
420 | ||
421 | if (rc_mode < 0) { | |
422 | dev_err(dev, "Failed to read RTC oscillator selection\n"); | |
423 | sprintf(buf, "\n"); | |
424 | return rc_mode; | |
425 | } | |
426 | ||
427 | if (rc_mode) | |
428 | return sprintf(buf, "rc\n"); | |
429 | else | |
430 | return sprintf(buf, "xtal\n"); | |
431 | } | |
432 | ||
433 | static DEVICE_ATTR_RW(oscillator); | |
434 | ||
435 | static struct attribute *rtc_calib_attrs[] = { | |
436 | &dev_attr_autocalibration.attr, | |
437 | &dev_attr_oscillator.attr, | |
438 | NULL, | |
439 | }; | |
440 | ||
441 | static const struct attribute_group rtc_calib_attr_group = { | |
442 | .attrs = rtc_calib_attrs, | |
443 | }; | |
444 | ||
718a820a AB |
445 | static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled) |
446 | { | |
447 | struct i2c_client *client = to_i2c_client(dev); | |
448 | int err; | |
449 | ||
450 | if (enabled) | |
451 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ, | |
452 | (ABX8XX_IRQ_IM_1_4 | | |
453 | ABX8XX_IRQ_AIE)); | |
454 | else | |
455 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ, | |
456 | ABX8XX_IRQ_IM_1_4); | |
457 | return err; | |
458 | } | |
459 | ||
4d61ff6b PDM |
460 | static const struct rtc_class_ops abx80x_rtc_ops = { |
461 | .read_time = abx80x_rtc_read_time, | |
462 | .set_time = abx80x_rtc_set_time, | |
718a820a AB |
463 | .read_alarm = abx80x_read_alarm, |
464 | .set_alarm = abx80x_set_alarm, | |
465 | .alarm_irq_enable = abx80x_alarm_irq_enable, | |
4d61ff6b PDM |
466 | }; |
467 | ||
468 | static int abx80x_dt_trickle_cfg(struct device_node *np) | |
469 | { | |
470 | const char *diode; | |
471 | int trickle_cfg = 0; | |
472 | int i, ret; | |
473 | u32 tmp; | |
474 | ||
475 | ret = of_property_read_string(np, "abracon,tc-diode", &diode); | |
476 | if (ret) | |
477 | return ret; | |
478 | ||
479 | if (!strcmp(diode, "standard")) | |
480 | trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE; | |
481 | else if (!strcmp(diode, "schottky")) | |
482 | trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE; | |
483 | else | |
484 | return -EINVAL; | |
485 | ||
486 | ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp); | |
487 | if (ret) | |
488 | return ret; | |
489 | ||
490 | for (i = 0; i < sizeof(trickle_resistors); i++) | |
491 | if (trickle_resistors[i] == tmp) | |
492 | break; | |
493 | ||
494 | if (i == sizeof(trickle_resistors)) | |
495 | return -EINVAL; | |
496 | ||
497 | return (trickle_cfg | i); | |
498 | } | |
499 | ||
59a8383a MJ |
500 | static void rtc_calib_remove_sysfs_group(void *_dev) |
501 | { | |
502 | struct device *dev = _dev; | |
503 | ||
504 | sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group); | |
505 | } | |
506 | ||
4d61ff6b PDM |
507 | static int abx80x_probe(struct i2c_client *client, |
508 | const struct i2c_device_id *id) | |
509 | { | |
510 | struct device_node *np = client->dev.of_node; | |
511 | struct rtc_device *rtc; | |
512 | int i, data, err, trickle_cfg = -EINVAL; | |
513 | char buf[7]; | |
514 | unsigned int part = id->driver_data; | |
515 | unsigned int partnumber; | |
516 | unsigned int majrev, minrev; | |
517 | unsigned int lot; | |
518 | unsigned int wafer; | |
519 | unsigned int uid; | |
520 | ||
521 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) | |
522 | return -ENODEV; | |
523 | ||
524 | err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0, | |
525 | sizeof(buf), buf); | |
526 | if (err < 0) { | |
527 | dev_err(&client->dev, "Unable to read partnumber\n"); | |
528 | return -EIO; | |
529 | } | |
530 | ||
531 | partnumber = (buf[0] << 8) | buf[1]; | |
532 | majrev = buf[2] >> 3; | |
533 | minrev = buf[2] & 0x7; | |
534 | lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3]; | |
535 | uid = ((buf[4] & 0x7f) << 8) | buf[5]; | |
536 | wafer = (buf[6] & 0x7c) >> 2; | |
537 | dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n", | |
538 | partnumber, majrev, minrev, lot, wafer, uid); | |
539 | ||
540 | data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1); | |
541 | if (data < 0) { | |
542 | dev_err(&client->dev, "Unable to read control register\n"); | |
543 | return -EIO; | |
544 | } | |
545 | ||
546 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1, | |
718a820a AB |
547 | ((data & ~(ABX8XX_CTRL_12_24 | |
548 | ABX8XX_CTRL_ARST)) | | |
4d61ff6b PDM |
549 | ABX8XX_CTRL_WRITE)); |
550 | if (err < 0) { | |
551 | dev_err(&client->dev, "Unable to write control register\n"); | |
552 | return -EIO; | |
553 | } | |
554 | ||
555 | /* part autodetection */ | |
556 | if (part == ABX80X) { | |
557 | for (i = 0; abx80x_caps[i].pn; i++) | |
558 | if (partnumber == abx80x_caps[i].pn) | |
559 | break; | |
560 | if (abx80x_caps[i].pn == 0) { | |
561 | dev_err(&client->dev, "Unknown part: %04x\n", | |
562 | partnumber); | |
563 | return -EINVAL; | |
564 | } | |
565 | part = i; | |
566 | } | |
567 | ||
568 | if (partnumber != abx80x_caps[part].pn) { | |
569 | dev_err(&client->dev, "partnumber mismatch %04x != %04x\n", | |
570 | partnumber, abx80x_caps[part].pn); | |
571 | return -EINVAL; | |
572 | } | |
573 | ||
574 | if (np && abx80x_caps[part].has_tc) | |
575 | trickle_cfg = abx80x_dt_trickle_cfg(np); | |
576 | ||
577 | if (trickle_cfg > 0) { | |
578 | dev_info(&client->dev, "Enabling trickle charger: %02x\n", | |
579 | trickle_cfg); | |
580 | abx80x_enable_trickle_charger(client, trickle_cfg); | |
581 | } | |
582 | ||
718a820a AB |
583 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL, |
584 | BIT(2)); | |
585 | if (err) | |
586 | return err; | |
587 | ||
588 | rtc = devm_rtc_device_register(&client->dev, "abx8xx", | |
4d61ff6b PDM |
589 | &abx80x_rtc_ops, THIS_MODULE); |
590 | ||
591 | if (IS_ERR(rtc)) | |
592 | return PTR_ERR(rtc); | |
593 | ||
594 | i2c_set_clientdata(client, rtc); | |
595 | ||
718a820a AB |
596 | if (client->irq > 0) { |
597 | dev_info(&client->dev, "IRQ %d supplied\n", client->irq); | |
598 | err = devm_request_threaded_irq(&client->dev, client->irq, NULL, | |
599 | abx80x_handle_irq, | |
600 | IRQF_SHARED | IRQF_ONESHOT, | |
601 | "abx8xx", | |
602 | client); | |
603 | if (err) { | |
604 | dev_err(&client->dev, "unable to request IRQ, alarms disabled\n"); | |
605 | client->irq = 0; | |
606 | } | |
607 | } | |
608 | ||
59a8383a MJ |
609 | /* Export sysfs entries */ |
610 | err = sysfs_create_group(&(&client->dev)->kobj, &rtc_calib_attr_group); | |
611 | if (err) { | |
612 | dev_err(&client->dev, "Failed to create sysfs group: %d\n", | |
613 | err); | |
614 | return err; | |
615 | } | |
616 | ||
617 | err = devm_add_action(&client->dev, rtc_calib_remove_sysfs_group, | |
618 | &client->dev); | |
619 | if (err) { | |
620 | rtc_calib_remove_sysfs_group(&client->dev); | |
621 | dev_err(&client->dev, | |
622 | "Failed to add sysfs cleanup action: %d\n", | |
623 | err); | |
624 | return err; | |
625 | } | |
626 | ||
4d61ff6b PDM |
627 | return 0; |
628 | } | |
629 | ||
630 | static int abx80x_remove(struct i2c_client *client) | |
631 | { | |
632 | return 0; | |
633 | } | |
634 | ||
635 | static const struct i2c_device_id abx80x_id[] = { | |
636 | { "abx80x", ABX80X }, | |
637 | { "ab0801", AB0801 }, | |
638 | { "ab0803", AB0803 }, | |
639 | { "ab0804", AB0804 }, | |
640 | { "ab0805", AB0805 }, | |
641 | { "ab1801", AB1801 }, | |
642 | { "ab1803", AB1803 }, | |
643 | { "ab1804", AB1804 }, | |
644 | { "ab1805", AB1805 }, | |
fca733a1 | 645 | { "rv1805", AB1805 }, |
4d61ff6b PDM |
646 | { } |
647 | }; | |
648 | MODULE_DEVICE_TABLE(i2c, abx80x_id); | |
649 | ||
650 | static struct i2c_driver abx80x_driver = { | |
651 | .driver = { | |
652 | .name = "rtc-abx80x", | |
653 | }, | |
654 | .probe = abx80x_probe, | |
655 | .remove = abx80x_remove, | |
656 | .id_table = abx80x_id, | |
657 | }; | |
658 | ||
659 | module_i2c_driver(abx80x_driver); | |
660 | ||
661 | MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>"); | |
662 | MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>"); | |
663 | MODULE_DESCRIPTION("Abracon ABX80X RTC driver"); | |
664 | MODULE_LICENSE("GPL v2"); |