1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* |
3 | * RTC driver for the Micro Crystal RV8803 |
4 | * |
5 | * Copyright (C) 2015 Micro Crystal SA |
6 | * Alexandre Belloni <alexandre.belloni@bootlin.com> |
7 | * |
8 | */ |
9 | |
10 | #include <linux/bcd.h> |
11 | #include <linux/bitops.h> |
12 | #include <linux/bitfield.h> |
13 | #include <linux/log2.h> |
14 | #include <linux/i2c.h> |
15 | #include <linux/interrupt.h> |
16 | #include <linux/kernel.h> |
17 | #include <linux/module.h> |
18 | #include <linux/of.h> |
19 | #include <linux/rtc.h> |
20 | #include <linux/pm_wakeirq.h> |
21 | |
22 | #define RV8803_I2C_TRY_COUNT 4 |
23 | |
24 | #define RV8803_SEC 0x00 |
25 | #define RV8803_MIN 0x01 |
26 | #define RV8803_HOUR 0x02 |
27 | #define RV8803_WEEK 0x03 |
28 | #define RV8803_DAY 0x04 |
29 | #define RV8803_MONTH 0x05 |
30 | #define RV8803_YEAR 0x06 |
31 | #define RV8803_RAM 0x07 |
32 | #define RV8803_ALARM_MIN 0x08 |
33 | #define RV8803_ALARM_HOUR 0x09 |
34 | #define RV8803_ALARM_WEEK_OR_DAY 0x0A |
35 | #define RV8803_EXT 0x0D |
36 | #define RV8803_FLAG 0x0E |
37 | #define RV8803_CTRL 0x0F |
38 | #define RV8803_OSC_OFFSET 0x2C |
39 | |
40 | #define RV8803_EXT_WADA BIT(6) |
41 | |
42 | #define RV8803_FLAG_V1F BIT(0) |
43 | #define RV8803_FLAG_V2F BIT(1) |
44 | #define RV8803_FLAG_AF BIT(3) |
45 | #define RV8803_FLAG_TF BIT(4) |
46 | #define RV8803_FLAG_UF BIT(5) |
47 | |
48 | #define RV8803_CTRL_RESET BIT(0) |
49 | |
50 | #define RV8803_CTRL_EIE BIT(2) |
51 | #define RV8803_CTRL_AIE BIT(3) |
52 | #define RV8803_CTRL_TIE BIT(4) |
53 | #define RV8803_CTRL_UIE BIT(5) |
54 | |
55 | #define RX8803_CTRL_CSEL GENMASK(7, 6) |
56 | |
57 | #define RX8900_BACKUP_CTRL 0x18 |
58 | #define RX8900_FLAG_SWOFF BIT(2) |
59 | #define RX8900_FLAG_VDETOFF BIT(3) |
60 | |
61 | enum rv8803_type { |
62 | rv_8803, |
63 | rx_8803, |
64 | rx_8804, |
65 | rx_8900 |
66 | }; |
67 | |
68 | struct rv8803_data { |
69 | struct i2c_client *client; |
70 | struct rtc_device *rtc; |
71 | struct mutex flags_lock; |
72 | u8 ctrl; |
73 | u8 backup; |
74 | u8 alarm_invalid:1; |
75 | enum rv8803_type type; |
76 | }; |
77 | |
78 | static int rv8803_read_reg(const struct i2c_client *client, u8 reg) |
79 | { |
80 | int try = RV8803_I2C_TRY_COUNT; |
81 | s32 ret; |
82 | |
83 | /* |
84 | * There is a 61µs window during which the RTC does not acknowledge I2C |
85 | * transfers. In that case, ensure that there are multiple attempts. |
86 | */ |
87 | do |
88 | ret = i2c_smbus_read_byte_data(client, command: reg); |
89 | while ((ret == -ENXIO || ret == -EIO) && --try); |
90 | if (ret < 0) |
91 | dev_err(&client->dev, "Unable to read register 0x%02x\n" , reg); |
92 | |
93 | return ret; |
94 | } |
95 | |
96 | static int rv8803_read_regs(const struct i2c_client *client, |
97 | u8 reg, u8 count, u8 *values) |
98 | { |
99 | int try = RV8803_I2C_TRY_COUNT; |
100 | s32 ret; |
101 | |
102 | do |
103 | ret = i2c_smbus_read_i2c_block_data(client, command: reg, length: count, values); |
104 | while ((ret == -ENXIO || ret == -EIO) && --try); |
105 | if (ret != count) { |
106 | dev_err(&client->dev, |
107 | "Unable to read registers 0x%02x..0x%02x\n" , |
108 | reg, reg + count - 1); |
109 | return ret < 0 ? ret : -EIO; |
110 | } |
111 | |
112 | return 0; |
113 | } |
114 | |
115 | static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value) |
116 | { |
117 | int try = RV8803_I2C_TRY_COUNT; |
118 | s32 ret; |
119 | |
120 | do |
121 | ret = i2c_smbus_write_byte_data(client, command: reg, value); |
122 | while ((ret == -ENXIO || ret == -EIO) && --try); |
123 | if (ret) |
124 | dev_err(&client->dev, "Unable to write register 0x%02x\n" , reg); |
125 | |
126 | return ret; |
127 | } |
128 | |
129 | static int rv8803_write_regs(const struct i2c_client *client, |
130 | u8 reg, u8 count, const u8 *values) |
131 | { |
132 | int try = RV8803_I2C_TRY_COUNT; |
133 | s32 ret; |
134 | |
135 | do |
136 | ret = i2c_smbus_write_i2c_block_data(client, command: reg, length: count, |
137 | values); |
138 | while ((ret == -ENXIO || ret == -EIO) && --try); |
139 | if (ret) |
140 | dev_err(&client->dev, |
141 | "Unable to write registers 0x%02x..0x%02x\n" , |
142 | reg, reg + count - 1); |
143 | |
144 | return ret; |
145 | } |
146 | |
147 | static int rv8803_regs_init(struct rv8803_data *rv8803) |
148 | { |
149 | int ret; |
150 | |
151 | ret = rv8803_write_reg(client: rv8803->client, RV8803_OSC_OFFSET, value: 0x00); |
152 | if (ret) |
153 | return ret; |
154 | |
155 | ret = rv8803_write_reg(client: rv8803->client, RV8803_CTRL, |
156 | FIELD_PREP(RX8803_CTRL_CSEL, 1)); /* 2s */ |
157 | if (ret) |
158 | return ret; |
159 | |
160 | ret = rv8803_write_regs(client: rv8803->client, RV8803_ALARM_MIN, count: 3, |
161 | values: (u8[]){ 0, 0, 0 }); |
162 | if (ret) |
163 | return ret; |
164 | |
165 | return rv8803_write_reg(client: rv8803->client, RV8803_RAM, value: 0x00); |
166 | } |
167 | |
168 | static int rv8803_regs_configure(struct rv8803_data *rv8803); |
169 | |
170 | static int rv8803_regs_reset(struct rv8803_data *rv8803, bool full) |
171 | { |
172 | /* |
173 | * The RV-8803 resets all registers to POR defaults after voltage-loss, |
174 | * the Epson RTCs don't, so we manually reset the remainder here. |
175 | */ |
176 | if (full || rv8803->type == rx_8803 || rv8803->type == rx_8900) { |
177 | int ret = rv8803_regs_init(rv8803); |
178 | if (ret) |
179 | return ret; |
180 | } |
181 | |
182 | return rv8803_regs_configure(rv8803); |
183 | } |
184 | |
185 | static irqreturn_t rv8803_handle_irq(int irq, void *dev_id) |
186 | { |
187 | struct i2c_client *client = dev_id; |
188 | struct rv8803_data *rv8803 = i2c_get_clientdata(client); |
189 | unsigned long events = 0; |
190 | int flags; |
191 | |
192 | mutex_lock(&rv8803->flags_lock); |
193 | |
194 | flags = rv8803_read_reg(client, RV8803_FLAG); |
195 | if (flags <= 0) { |
196 | mutex_unlock(lock: &rv8803->flags_lock); |
197 | return IRQ_NONE; |
198 | } |
199 | |
200 | if (flags & RV8803_FLAG_V1F) |
201 | dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n" ); |
202 | |
203 | if (flags & RV8803_FLAG_V2F) |
204 | dev_warn(&client->dev, "Voltage low, data loss detected.\n" ); |
205 | |
206 | if (flags & RV8803_FLAG_TF) { |
207 | flags &= ~RV8803_FLAG_TF; |
208 | rv8803->ctrl &= ~RV8803_CTRL_TIE; |
209 | events |= RTC_PF; |
210 | } |
211 | |
212 | if (flags & RV8803_FLAG_AF) { |
213 | flags &= ~RV8803_FLAG_AF; |
214 | rv8803->ctrl &= ~RV8803_CTRL_AIE; |
215 | events |= RTC_AF; |
216 | } |
217 | |
218 | if (flags & RV8803_FLAG_UF) { |
219 | flags &= ~RV8803_FLAG_UF; |
220 | rv8803->ctrl &= ~RV8803_CTRL_UIE; |
221 | events |= RTC_UF; |
222 | } |
223 | |
224 | if (events) { |
225 | rtc_update_irq(rtc: rv8803->rtc, num: 1, events); |
226 | rv8803_write_reg(client, RV8803_FLAG, value: flags); |
227 | rv8803_write_reg(client: rv8803->client, RV8803_CTRL, value: rv8803->ctrl); |
228 | } |
229 | |
230 | mutex_unlock(lock: &rv8803->flags_lock); |
231 | |
232 | return IRQ_HANDLED; |
233 | } |
234 | |
235 | static int rv8803_get_time(struct device *dev, struct rtc_time *tm) |
236 | { |
237 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
238 | u8 date1[7]; |
239 | u8 date2[7]; |
240 | u8 *date = date1; |
241 | int ret, flags; |
242 | |
243 | if (rv8803->alarm_invalid) { |
244 | dev_warn(dev, "Corruption detected, data may be invalid.\n" ); |
245 | return -EINVAL; |
246 | } |
247 | |
248 | flags = rv8803_read_reg(client: rv8803->client, RV8803_FLAG); |
249 | if (flags < 0) |
250 | return flags; |
251 | |
252 | if (flags & RV8803_FLAG_V2F) { |
253 | dev_warn(dev, "Voltage low, data is invalid.\n" ); |
254 | return -EINVAL; |
255 | } |
256 | |
257 | ret = rv8803_read_regs(client: rv8803->client, RV8803_SEC, count: 7, values: date); |
258 | if (ret) |
259 | return ret; |
260 | |
261 | if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) { |
262 | ret = rv8803_read_regs(client: rv8803->client, RV8803_SEC, count: 7, values: date2); |
263 | if (ret) |
264 | return ret; |
265 | |
266 | if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59)) |
267 | date = date2; |
268 | } |
269 | |
270 | tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f); |
271 | tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f); |
272 | tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f); |
273 | tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f); |
274 | tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f); |
275 | tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1; |
276 | tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100; |
277 | |
278 | return 0; |
279 | } |
280 | |
281 | static int rv8803_set_time(struct device *dev, struct rtc_time *tm) |
282 | { |
283 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
284 | u8 date[7]; |
285 | int ctrl, flags, ret; |
286 | |
287 | ctrl = rv8803_read_reg(client: rv8803->client, RV8803_CTRL); |
288 | if (ctrl < 0) |
289 | return ctrl; |
290 | |
291 | /* Stop the clock */ |
292 | ret = rv8803_write_reg(client: rv8803->client, RV8803_CTRL, |
293 | value: ctrl | RV8803_CTRL_RESET); |
294 | if (ret) |
295 | return ret; |
296 | |
297 | date[RV8803_SEC] = bin2bcd(tm->tm_sec); |
298 | date[RV8803_MIN] = bin2bcd(tm->tm_min); |
299 | date[RV8803_HOUR] = bin2bcd(tm->tm_hour); |
300 | date[RV8803_WEEK] = 1 << (tm->tm_wday); |
301 | date[RV8803_DAY] = bin2bcd(tm->tm_mday); |
302 | date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1); |
303 | date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100); |
304 | |
305 | ret = rv8803_write_regs(client: rv8803->client, RV8803_SEC, count: 7, values: date); |
306 | if (ret) |
307 | return ret; |
308 | |
309 | /* Restart the clock */ |
310 | ret = rv8803_write_reg(client: rv8803->client, RV8803_CTRL, |
311 | value: ctrl & ~RV8803_CTRL_RESET); |
312 | if (ret) |
313 | return ret; |
314 | |
315 | mutex_lock(&rv8803->flags_lock); |
316 | |
317 | flags = rv8803_read_reg(client: rv8803->client, RV8803_FLAG); |
318 | if (flags < 0) { |
319 | mutex_unlock(lock: &rv8803->flags_lock); |
320 | return flags; |
321 | } |
322 | |
323 | if ((flags & RV8803_FLAG_V2F) || rv8803->alarm_invalid) { |
324 | /* |
325 | * If we sense corruption in the alarm registers, but see no |
326 | * voltage loss flag, we can't rely on other registers having |
327 | * sensible values. Reset them fully. |
328 | */ |
329 | ret = rv8803_regs_reset(rv8803, full: rv8803->alarm_invalid); |
330 | if (ret) { |
331 | mutex_unlock(lock: &rv8803->flags_lock); |
332 | return ret; |
333 | } |
334 | |
335 | rv8803->alarm_invalid = false; |
336 | } |
337 | |
338 | ret = rv8803_write_reg(client: rv8803->client, RV8803_FLAG, |
339 | value: flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F)); |
340 | |
341 | mutex_unlock(lock: &rv8803->flags_lock); |
342 | |
343 | return ret; |
344 | } |
345 | |
346 | static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
347 | { |
348 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
349 | struct i2c_client *client = rv8803->client; |
350 | u8 alarmvals[3]; |
351 | int flags, ret; |
352 | |
353 | ret = rv8803_read_regs(client, RV8803_ALARM_MIN, count: 3, values: alarmvals); |
354 | if (ret) |
355 | return ret; |
356 | |
357 | flags = rv8803_read_reg(client, RV8803_FLAG); |
358 | if (flags < 0) |
359 | return flags; |
360 | |
361 | alarmvals[0] &= 0x7f; |
362 | alarmvals[1] &= 0x3f; |
363 | alarmvals[2] &= 0x3f; |
364 | |
365 | if (!bcd_is_valid(alarmvals[0]) || |
366 | !bcd_is_valid(alarmvals[1]) || |
367 | !bcd_is_valid(alarmvals[2])) |
368 | goto err_invalid; |
369 | |
370 | alrm->time.tm_sec = 0; |
371 | alrm->time.tm_min = bcd2bin(alarmvals[0]); |
372 | alrm->time.tm_hour = bcd2bin(alarmvals[1]); |
373 | alrm->time.tm_mday = bcd2bin(alarmvals[2]); |
374 | |
375 | alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE); |
376 | alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled; |
377 | |
378 | if ((unsigned int)alrm->time.tm_mday > 31 || |
379 | (unsigned int)alrm->time.tm_hour >= 24 || |
380 | (unsigned int)alrm->time.tm_min >= 60) |
381 | goto err_invalid; |
382 | |
383 | return 0; |
384 | |
385 | err_invalid: |
386 | rv8803->alarm_invalid = true; |
387 | return -EINVAL; |
388 | } |
389 | |
390 | static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
391 | { |
392 | struct i2c_client *client = to_i2c_client(dev); |
393 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
394 | u8 alarmvals[3]; |
395 | u8 ctrl[2]; |
396 | int ret, err; |
397 | |
398 | /* The alarm has no seconds, round up to nearest minute */ |
399 | if (alrm->time.tm_sec) { |
400 | time64_t alarm_time = rtc_tm_to_time64(tm: &alrm->time); |
401 | |
402 | alarm_time += 60 - alrm->time.tm_sec; |
403 | rtc_time64_to_tm(time: alarm_time, tm: &alrm->time); |
404 | } |
405 | |
406 | mutex_lock(&rv8803->flags_lock); |
407 | |
408 | ret = rv8803_read_regs(client, RV8803_FLAG, count: 2, values: ctrl); |
409 | if (ret) { |
410 | mutex_unlock(lock: &rv8803->flags_lock); |
411 | return ret; |
412 | } |
413 | |
414 | alarmvals[0] = bin2bcd(alrm->time.tm_min); |
415 | alarmvals[1] = bin2bcd(alrm->time.tm_hour); |
416 | alarmvals[2] = bin2bcd(alrm->time.tm_mday); |
417 | |
418 | if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) { |
419 | rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE); |
420 | err = rv8803_write_reg(client: rv8803->client, RV8803_CTRL, |
421 | value: rv8803->ctrl); |
422 | if (err) { |
423 | mutex_unlock(lock: &rv8803->flags_lock); |
424 | return err; |
425 | } |
426 | } |
427 | |
428 | ctrl[0] &= ~RV8803_FLAG_AF; |
429 | err = rv8803_write_reg(client: rv8803->client, RV8803_FLAG, value: ctrl[0]); |
430 | mutex_unlock(lock: &rv8803->flags_lock); |
431 | if (err) |
432 | return err; |
433 | |
434 | err = rv8803_write_regs(client: rv8803->client, RV8803_ALARM_MIN, count: 3, values: alarmvals); |
435 | if (err) |
436 | return err; |
437 | |
438 | if (alrm->enabled) { |
439 | if (rv8803->rtc->uie_rtctimer.enabled) |
440 | rv8803->ctrl |= RV8803_CTRL_UIE; |
441 | if (rv8803->rtc->aie_timer.enabled) |
442 | rv8803->ctrl |= RV8803_CTRL_AIE; |
443 | |
444 | err = rv8803_write_reg(client: rv8803->client, RV8803_CTRL, |
445 | value: rv8803->ctrl); |
446 | if (err) |
447 | return err; |
448 | } |
449 | |
450 | return 0; |
451 | } |
452 | |
453 | static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled) |
454 | { |
455 | struct i2c_client *client = to_i2c_client(dev); |
456 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
457 | int ctrl, flags, err; |
458 | |
459 | ctrl = rv8803->ctrl; |
460 | |
461 | if (enabled) { |
462 | if (rv8803->rtc->uie_rtctimer.enabled) |
463 | ctrl |= RV8803_CTRL_UIE; |
464 | if (rv8803->rtc->aie_timer.enabled) |
465 | ctrl |= RV8803_CTRL_AIE; |
466 | } else { |
467 | if (!rv8803->rtc->uie_rtctimer.enabled) |
468 | ctrl &= ~RV8803_CTRL_UIE; |
469 | if (!rv8803->rtc->aie_timer.enabled) |
470 | ctrl &= ~RV8803_CTRL_AIE; |
471 | } |
472 | |
473 | mutex_lock(&rv8803->flags_lock); |
474 | flags = rv8803_read_reg(client, RV8803_FLAG); |
475 | if (flags < 0) { |
476 | mutex_unlock(lock: &rv8803->flags_lock); |
477 | return flags; |
478 | } |
479 | flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF); |
480 | err = rv8803_write_reg(client, RV8803_FLAG, value: flags); |
481 | mutex_unlock(lock: &rv8803->flags_lock); |
482 | if (err) |
483 | return err; |
484 | |
485 | if (ctrl != rv8803->ctrl) { |
486 | rv8803->ctrl = ctrl; |
487 | err = rv8803_write_reg(client, RV8803_CTRL, value: rv8803->ctrl); |
488 | if (err) |
489 | return err; |
490 | } |
491 | |
492 | return 0; |
493 | } |
494 | |
495 | static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) |
496 | { |
497 | struct i2c_client *client = to_i2c_client(dev); |
498 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
499 | unsigned int vl = 0; |
500 | int flags, ret = 0; |
501 | |
502 | switch (cmd) { |
503 | case RTC_VL_READ: |
504 | flags = rv8803_read_reg(client, RV8803_FLAG); |
505 | if (flags < 0) |
506 | return flags; |
507 | |
508 | if (flags & RV8803_FLAG_V1F) { |
509 | dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n" ); |
510 | vl = RTC_VL_ACCURACY_LOW; |
511 | } |
512 | |
513 | if (flags & RV8803_FLAG_V2F) |
514 | vl |= RTC_VL_DATA_INVALID; |
515 | |
516 | return put_user(vl, (unsigned int __user *)arg); |
517 | |
518 | case RTC_VL_CLR: |
519 | mutex_lock(&rv8803->flags_lock); |
520 | flags = rv8803_read_reg(client, RV8803_FLAG); |
521 | if (flags < 0) { |
522 | mutex_unlock(lock: &rv8803->flags_lock); |
523 | return flags; |
524 | } |
525 | |
526 | flags &= ~RV8803_FLAG_V1F; |
527 | ret = rv8803_write_reg(client, RV8803_FLAG, value: flags); |
528 | mutex_unlock(lock: &rv8803->flags_lock); |
529 | if (ret) |
530 | return ret; |
531 | |
532 | return 0; |
533 | |
534 | default: |
535 | return -ENOIOCTLCMD; |
536 | } |
537 | } |
538 | |
539 | static int rv8803_nvram_write(void *priv, unsigned int offset, void *val, |
540 | size_t bytes) |
541 | { |
542 | return rv8803_write_reg(client: priv, RV8803_RAM, value: *(u8 *)val); |
543 | } |
544 | |
545 | static int rv8803_nvram_read(void *priv, unsigned int offset, |
546 | void *val, size_t bytes) |
547 | { |
548 | int ret; |
549 | |
550 | ret = rv8803_read_reg(client: priv, RV8803_RAM); |
551 | if (ret < 0) |
552 | return ret; |
553 | |
554 | *(u8 *)val = ret; |
555 | |
556 | return 0; |
557 | } |
558 | |
559 | static const struct rtc_class_ops rv8803_rtc_ops = { |
560 | .read_time = rv8803_get_time, |
561 | .set_time = rv8803_set_time, |
562 | .ioctl = rv8803_ioctl, |
563 | .read_alarm = rv8803_get_alarm, |
564 | .set_alarm = rv8803_set_alarm, |
565 | .alarm_irq_enable = rv8803_alarm_irq_enable, |
566 | }; |
567 | |
568 | static int rx8900_trickle_charger_init(struct rv8803_data *rv8803) |
569 | { |
570 | struct i2c_client *client = rv8803->client; |
571 | struct device_node *node = client->dev.of_node; |
572 | int err; |
573 | u8 flags; |
574 | |
575 | if (!node) |
576 | return 0; |
577 | |
578 | if (rv8803->type != rx_8900) |
579 | return 0; |
580 | |
581 | err = i2c_smbus_read_byte_data(client: rv8803->client, RX8900_BACKUP_CTRL); |
582 | if (err < 0) |
583 | return err; |
584 | |
585 | flags = (u8)err; |
586 | flags &= ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF); |
587 | flags |= rv8803->backup; |
588 | |
589 | return i2c_smbus_write_byte_data(client: rv8803->client, RX8900_BACKUP_CTRL, |
590 | value: flags); |
591 | } |
592 | |
593 | /* configure registers with values different than the Power-On reset defaults */ |
594 | static int rv8803_regs_configure(struct rv8803_data *rv8803) |
595 | { |
596 | int err; |
597 | |
598 | err = rv8803_write_reg(client: rv8803->client, RV8803_EXT, RV8803_EXT_WADA); |
599 | if (err) |
600 | return err; |
601 | |
602 | err = rx8900_trickle_charger_init(rv8803); |
603 | if (err) { |
604 | dev_err(&rv8803->client->dev, "failed to init charger\n" ); |
605 | return err; |
606 | } |
607 | |
608 | return 0; |
609 | } |
610 | |
611 | static int rv8803_resume(struct device *dev) |
612 | { |
613 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
614 | |
615 | if (rv8803->client->irq > 0 && device_may_wakeup(dev)) |
616 | disable_irq_wake(irq: rv8803->client->irq); |
617 | |
618 | return 0; |
619 | } |
620 | |
621 | static int rv8803_suspend(struct device *dev) |
622 | { |
623 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
624 | |
625 | if (rv8803->client->irq > 0 && device_may_wakeup(dev)) |
626 | enable_irq_wake(irq: rv8803->client->irq); |
627 | |
628 | return 0; |
629 | } |
630 | |
631 | static DEFINE_SIMPLE_DEV_PM_OPS(rv8803_pm_ops, rv8803_suspend, rv8803_resume); |
632 | |
633 | static const struct i2c_device_id rv8803_id[] = { |
634 | { "rv8803" , rv_8803 }, |
635 | { "rv8804" , rx_8804 }, |
636 | { "rx8803" , rx_8803 }, |
637 | { "rx8900" , rx_8900 }, |
638 | { } |
639 | }; |
640 | MODULE_DEVICE_TABLE(i2c, rv8803_id); |
641 | |
642 | static int rv8803_probe(struct i2c_client *client) |
643 | { |
644 | struct i2c_adapter *adapter = client->adapter; |
645 | struct rv8803_data *rv8803; |
646 | int err, flags; |
647 | struct nvmem_config nvmem_cfg = { |
648 | .name = "rv8803_nvram" , |
649 | .word_size = 1, |
650 | .stride = 1, |
651 | .size = 1, |
652 | .reg_read = rv8803_nvram_read, |
653 | .reg_write = rv8803_nvram_write, |
654 | .priv = client, |
655 | }; |
656 | |
657 | if (!i2c_check_functionality(adap: adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
658 | I2C_FUNC_SMBUS_I2C_BLOCK)) { |
659 | dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n" ); |
660 | return -EIO; |
661 | } |
662 | |
663 | rv8803 = devm_kzalloc(dev: &client->dev, size: sizeof(struct rv8803_data), |
664 | GFP_KERNEL); |
665 | if (!rv8803) |
666 | return -ENOMEM; |
667 | |
668 | mutex_init(&rv8803->flags_lock); |
669 | rv8803->client = client; |
670 | if (client->dev.of_node) { |
671 | rv8803->type = (uintptr_t)of_device_get_match_data(dev: &client->dev); |
672 | } else { |
673 | const struct i2c_device_id *id = i2c_match_id(id: rv8803_id, client); |
674 | |
675 | rv8803->type = id->driver_data; |
676 | } |
677 | i2c_set_clientdata(client, data: rv8803); |
678 | |
679 | flags = rv8803_read_reg(client, RV8803_FLAG); |
680 | if (flags < 0) |
681 | return flags; |
682 | |
683 | if (flags & RV8803_FLAG_V1F) |
684 | dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n" ); |
685 | |
686 | if (flags & RV8803_FLAG_V2F) |
687 | dev_warn(&client->dev, "Voltage low, data loss detected.\n" ); |
688 | |
689 | if (flags & RV8803_FLAG_AF) |
690 | dev_warn(&client->dev, "An alarm maybe have been missed.\n" ); |
691 | |
692 | rv8803->rtc = devm_rtc_allocate_device(dev: &client->dev); |
693 | if (IS_ERR(ptr: rv8803->rtc)) |
694 | return PTR_ERR(ptr: rv8803->rtc); |
695 | |
696 | if (client->irq > 0) { |
697 | unsigned long irqflags = IRQF_TRIGGER_LOW; |
698 | |
699 | if (dev_fwnode(&client->dev)) |
700 | irqflags = 0; |
701 | |
702 | err = devm_request_threaded_irq(dev: &client->dev, irq: client->irq, |
703 | NULL, thread_fn: rv8803_handle_irq, |
704 | irqflags: irqflags | IRQF_ONESHOT, |
705 | devname: "rv8803" , dev_id: client); |
706 | if (err) { |
707 | dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n" ); |
708 | client->irq = 0; |
709 | } else { |
710 | device_init_wakeup(dev: &client->dev, enable: true); |
711 | err = dev_pm_set_wake_irq(dev: &client->dev, irq: client->irq); |
712 | if (err) |
713 | dev_err(&client->dev, "failed to set wake IRQ\n" ); |
714 | } |
715 | } else { |
716 | if (device_property_read_bool(dev: &client->dev, propname: "wakeup-source" )) |
717 | device_init_wakeup(dev: &client->dev, enable: true); |
718 | else |
719 | clear_bit(RTC_FEATURE_ALARM, addr: rv8803->rtc->features); |
720 | } |
721 | |
722 | if (of_property_read_bool(np: client->dev.of_node, propname: "epson,vdet-disable" )) |
723 | rv8803->backup |= RX8900_FLAG_VDETOFF; |
724 | |
725 | if (of_property_read_bool(np: client->dev.of_node, propname: "trickle-diode-disable" )) |
726 | rv8803->backup |= RX8900_FLAG_SWOFF; |
727 | |
728 | err = rv8803_regs_configure(rv8803); |
729 | if (err) |
730 | return err; |
731 | |
732 | rv8803->rtc->ops = &rv8803_rtc_ops; |
733 | rv8803->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; |
734 | rv8803->rtc->range_max = RTC_TIMESTAMP_END_2099; |
735 | err = devm_rtc_register_device(rv8803->rtc); |
736 | if (err) |
737 | return err; |
738 | |
739 | devm_rtc_nvmem_register(rtc: rv8803->rtc, nvmem_config: &nvmem_cfg); |
740 | |
741 | rv8803->rtc->max_user_freq = 1; |
742 | |
743 | return 0; |
744 | } |
745 | |
746 | static const __maybe_unused struct of_device_id rv8803_of_match[] = { |
747 | { |
748 | .compatible = "microcrystal,rv8803" , |
749 | .data = (void *)rv_8803 |
750 | }, |
751 | { |
752 | .compatible = "epson,rx8803" , |
753 | .data = (void *)rx_8803 |
754 | }, |
755 | { |
756 | .compatible = "epson,rx8804" , |
757 | .data = (void *)rx_8804 |
758 | }, |
759 | { |
760 | .compatible = "epson,rx8900" , |
761 | .data = (void *)rx_8900 |
762 | }, |
763 | { } |
764 | }; |
765 | MODULE_DEVICE_TABLE(of, rv8803_of_match); |
766 | |
767 | static struct i2c_driver rv8803_driver = { |
768 | .driver = { |
769 | .name = "rtc-rv8803" , |
770 | .of_match_table = of_match_ptr(rv8803_of_match), |
771 | .pm = &rv8803_pm_ops, |
772 | }, |
773 | .probe = rv8803_probe, |
774 | .id_table = rv8803_id, |
775 | }; |
776 | module_i2c_driver(rv8803_driver); |
777 | |
778 | MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>" ); |
779 | MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver" ); |
780 | MODULE_LICENSE("GPL v2" ); |
781 | |