blob: aae2576741a61585a9624217fa2a6cd1d26510e1 [file] [log] [blame]
Alexandre Belloni1e3929e2015-11-02 23:48:32 +01001/*
2 * RTC driver for the Micro Crystal RV8803
3 *
4 * Copyright (C) 2015 Micro Crystal SA
5 *
6 * Alexandre Belloni <alexandre.belloni@free-electrons.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14#include <linux/bcd.h>
15#include <linux/bitops.h>
Benoît Thébaudeaua1e98e02016-07-21 12:41:29 +020016#include <linux/log2.h>
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010017#include <linux/i2c.h>
18#include <linux/interrupt.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -030021#include <linux/of_device.h>
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010022#include <linux/rtc.h>
23
Benoît Thébaudeaud5226492016-07-21 12:41:30 +020024#define RV8803_I2C_TRY_COUNT 4
25
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010026#define RV8803_SEC 0x00
27#define RV8803_MIN 0x01
28#define RV8803_HOUR 0x02
29#define RV8803_WEEK 0x03
30#define RV8803_DAY 0x04
31#define RV8803_MONTH 0x05
32#define RV8803_YEAR 0x06
33#define RV8803_RAM 0x07
34#define RV8803_ALARM_MIN 0x08
35#define RV8803_ALARM_HOUR 0x09
36#define RV8803_ALARM_WEEK_OR_DAY 0x0A
37#define RV8803_EXT 0x0D
38#define RV8803_FLAG 0x0E
39#define RV8803_CTRL 0x0F
40
41#define RV8803_EXT_WADA BIT(6)
42
43#define RV8803_FLAG_V1F BIT(0)
44#define RV8803_FLAG_V2F BIT(1)
45#define RV8803_FLAG_AF BIT(3)
46#define RV8803_FLAG_TF BIT(4)
47#define RV8803_FLAG_UF BIT(5)
48
49#define RV8803_CTRL_RESET BIT(0)
50
51#define RV8803_CTRL_EIE BIT(2)
52#define RV8803_CTRL_AIE BIT(3)
53#define RV8803_CTRL_TIE BIT(4)
54#define RV8803_CTRL_UIE BIT(5)
55
Oleksij Rempel1cd71372016-06-29 16:40:01 +020056#define RX8900_BACKUP_CTRL 0x18
57#define RX8900_FLAG_SWOFF BIT(2)
58#define RX8900_FLAG_VDETOFF BIT(3)
59
60enum rv8803_type {
61 rv_8803,
62 rx_8900
63};
64
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010065struct rv8803_data {
66 struct i2c_client *client;
67 struct rtc_device *rtc;
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +010068 struct mutex flags_lock;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010069 u8 ctrl;
Oleksij Rempel1cd71372016-06-29 16:40:01 +020070 enum rv8803_type type;
Alexandre Belloni16d70a72017-07-06 11:42:04 +020071 struct nvmem_config nvmem_cfg;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010072};
73
Benoît Thébaudeaud5226492016-07-21 12:41:30 +020074static int rv8803_read_reg(const struct i2c_client *client, u8 reg)
75{
76 int try = RV8803_I2C_TRY_COUNT;
77 s32 ret;
78
79 /*
80 * There is a 61µs window during which the RTC does not acknowledge I2C
81 * transfers. In that case, ensure that there are multiple attempts.
82 */
83 do
84 ret = i2c_smbus_read_byte_data(client, reg);
85 while ((ret == -ENXIO || ret == -EIO) && --try);
86 if (ret < 0)
87 dev_err(&client->dev, "Unable to read register 0x%02x\n", reg);
88
89 return ret;
90}
91
92static int rv8803_read_regs(const struct i2c_client *client,
93 u8 reg, u8 count, u8 *values)
94{
95 int try = RV8803_I2C_TRY_COUNT;
96 s32 ret;
97
98 do
99 ret = i2c_smbus_read_i2c_block_data(client, reg, count, values);
100 while ((ret == -ENXIO || ret == -EIO) && --try);
101 if (ret != count) {
102 dev_err(&client->dev,
103 "Unable to read registers 0x%02x..0x%02x\n",
104 reg, reg + count - 1);
105 return ret < 0 ? ret : -EIO;
106 }
107
108 return 0;
109}
110
111static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value)
112{
113 int try = RV8803_I2C_TRY_COUNT;
114 s32 ret;
115
116 do
117 ret = i2c_smbus_write_byte_data(client, reg, value);
118 while ((ret == -ENXIO || ret == -EIO) && --try);
119 if (ret)
120 dev_err(&client->dev, "Unable to write register 0x%02x\n", reg);
121
122 return ret;
123}
124
125static int rv8803_write_regs(const struct i2c_client *client,
126 u8 reg, u8 count, const u8 *values)
127{
128 int try = RV8803_I2C_TRY_COUNT;
129 s32 ret;
130
131 do
132 ret = i2c_smbus_write_i2c_block_data(client, reg, count,
133 values);
134 while ((ret == -ENXIO || ret == -EIO) && --try);
135 if (ret)
136 dev_err(&client->dev,
137 "Unable to write registers 0x%02x..0x%02x\n",
138 reg, reg + count - 1);
139
140 return ret;
141}
142
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100143static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
144{
145 struct i2c_client *client = dev_id;
146 struct rv8803_data *rv8803 = i2c_get_clientdata(client);
147 unsigned long events = 0;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200148 int flags;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100149
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100150 mutex_lock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100151
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200152 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100153 if (flags <= 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100154 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100155 return IRQ_NONE;
156 }
157
158 if (flags & RV8803_FLAG_V1F)
159 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
160
161 if (flags & RV8803_FLAG_V2F)
162 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
163
164 if (flags & RV8803_FLAG_TF) {
165 flags &= ~RV8803_FLAG_TF;
166 rv8803->ctrl &= ~RV8803_CTRL_TIE;
167 events |= RTC_PF;
168 }
169
170 if (flags & RV8803_FLAG_AF) {
171 flags &= ~RV8803_FLAG_AF;
172 rv8803->ctrl &= ~RV8803_CTRL_AIE;
173 events |= RTC_AF;
174 }
175
176 if (flags & RV8803_FLAG_UF) {
177 flags &= ~RV8803_FLAG_UF;
178 rv8803->ctrl &= ~RV8803_CTRL_UIE;
179 events |= RTC_UF;
180 }
181
182 if (events) {
183 rtc_update_irq(rv8803->rtc, 1, events);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200184 rv8803_write_reg(client, RV8803_FLAG, flags);
185 rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100186 }
187
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100188 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100189
190 return IRQ_HANDLED;
191}
192
193static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
194{
195 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
196 u8 date1[7];
197 u8 date2[7];
198 u8 *date = date1;
199 int ret, flags;
200
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200201 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100202 if (flags < 0)
203 return flags;
204
205 if (flags & RV8803_FLAG_V2F) {
206 dev_warn(dev, "Voltage low, data is invalid.\n");
207 return -EINVAL;
208 }
209
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200210 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date);
211 if (ret)
212 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100213
214 if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200215 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2);
216 if (ret)
217 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100218
219 if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
220 date = date2;
221 }
222
223 tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f);
224 tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f);
225 tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
Benoît Thébaudeaua1e98e02016-07-21 12:41:29 +0200226 tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100227 tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
228 tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
229 tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
230
Benoît Thébaudeau96acb252016-07-21 12:41:28 +0200231 return 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100232}
233
234static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
235{
236 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
237 u8 date[7];
Benoît Thébaudeaud3700b62016-07-21 12:41:31 +0200238 int ctrl, flags, ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100239
240 if ((tm->tm_year < 100) || (tm->tm_year > 199))
241 return -EINVAL;
242
Benoît Thébaudeaud3700b62016-07-21 12:41:31 +0200243 ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL);
244 if (ctrl < 0)
245 return ctrl;
246
247 /* Stop the clock */
248 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
249 ctrl | RV8803_CTRL_RESET);
250 if (ret)
251 return ret;
252
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100253 date[RV8803_SEC] = bin2bcd(tm->tm_sec);
254 date[RV8803_MIN] = bin2bcd(tm->tm_min);
255 date[RV8803_HOUR] = bin2bcd(tm->tm_hour);
256 date[RV8803_WEEK] = 1 << (tm->tm_wday);
257 date[RV8803_DAY] = bin2bcd(tm->tm_mday);
258 date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
259 date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100);
260
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200261 ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date);
262 if (ret)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100263 return ret;
264
Benoît Thébaudeaud3700b62016-07-21 12:41:31 +0200265 /* Restart the clock */
266 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
267 ctrl & ~RV8803_CTRL_RESET);
268 if (ret)
269 return ret;
270
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100271 mutex_lock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100272
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200273 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100274 if (flags < 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100275 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100276 return flags;
277 }
278
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200279 ret = rv8803_write_reg(rv8803->client, RV8803_FLAG,
Benoît Thébaudeau6f367782016-07-21 12:41:32 +0200280 flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F));
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100281
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100282 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100283
284 return ret;
285}
286
287static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
288{
289 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
290 struct i2c_client *client = rv8803->client;
291 u8 alarmvals[3];
292 int flags, ret;
293
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200294 ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals);
295 if (ret)
296 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100297
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200298 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100299 if (flags < 0)
300 return flags;
301
302 alrm->time.tm_sec = 0;
303 alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
304 alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100305 alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100306
307 alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
308 alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
309
310 return 0;
311}
312
313static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
314{
315 struct i2c_client *client = to_i2c_client(dev);
316 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
317 u8 alarmvals[3];
318 u8 ctrl[2];
319 int ret, err;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100320
321 /* The alarm has no seconds, round up to nearest minute */
322 if (alrm->time.tm_sec) {
323 time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
324
325 alarm_time += 60 - alrm->time.tm_sec;
326 rtc_time64_to_tm(alarm_time, &alrm->time);
327 }
328
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100329 mutex_lock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100330
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200331 ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl);
332 if (ret) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100333 mutex_unlock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200334 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100335 }
336
337 alarmvals[0] = bin2bcd(alrm->time.tm_min);
338 alarmvals[1] = bin2bcd(alrm->time.tm_hour);
339 alarmvals[2] = bin2bcd(alrm->time.tm_mday);
340
341 if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
342 rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200343 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
344 rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100345 if (err) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100346 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100347 return err;
348 }
349 }
350
351 ctrl[1] &= ~RV8803_FLAG_AF;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200352 err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[1]);
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100353 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100354 if (err)
355 return err;
356
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200357 err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100358 if (err)
359 return err;
360
361 if (alrm->enabled) {
362 if (rv8803->rtc->uie_rtctimer.enabled)
363 rv8803->ctrl |= RV8803_CTRL_UIE;
364 if (rv8803->rtc->aie_timer.enabled)
365 rv8803->ctrl |= RV8803_CTRL_AIE;
366
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200367 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
368 rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100369 if (err)
370 return err;
371 }
372
373 return 0;
374}
375
376static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
377{
378 struct i2c_client *client = to_i2c_client(dev);
379 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
380 int ctrl, flags, err;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100381
382 ctrl = rv8803->ctrl;
383
384 if (enabled) {
385 if (rv8803->rtc->uie_rtctimer.enabled)
386 ctrl |= RV8803_CTRL_UIE;
387 if (rv8803->rtc->aie_timer.enabled)
388 ctrl |= RV8803_CTRL_AIE;
389 } else {
390 if (!rv8803->rtc->uie_rtctimer.enabled)
391 ctrl &= ~RV8803_CTRL_UIE;
392 if (!rv8803->rtc->aie_timer.enabled)
393 ctrl &= ~RV8803_CTRL_AIE;
394 }
395
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100396 mutex_lock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200397 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100398 if (flags < 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100399 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100400 return flags;
401 }
402 flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200403 err = rv8803_write_reg(client, RV8803_FLAG, flags);
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100404 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100405 if (err)
406 return err;
407
408 if (ctrl != rv8803->ctrl) {
409 rv8803->ctrl = ctrl;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200410 err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100411 if (err)
412 return err;
413 }
414
415 return 0;
416}
417
418static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
419{
420 struct i2c_client *client = to_i2c_client(dev);
421 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
422 int flags, ret = 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100423
424 switch (cmd) {
425 case RTC_VL_READ:
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200426 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100427 if (flags < 0)
428 return flags;
429
430 if (flags & RV8803_FLAG_V1F)
431 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
432
433 if (flags & RV8803_FLAG_V2F)
434 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
435
436 flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F;
437
438 if (copy_to_user((void __user *)arg, &flags, sizeof(int)))
439 return -EFAULT;
440
441 return 0;
442
443 case RTC_VL_CLR:
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100444 mutex_lock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200445 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100446 if (flags < 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100447 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100448 return flags;
449 }
450
451 flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200452 ret = rv8803_write_reg(client, RV8803_FLAG, flags);
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100453 mutex_unlock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200454 if (ret)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100455 return ret;
456
457 return 0;
458
459 default:
460 return -ENOIOCTLCMD;
461 }
462}
463
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200464static int rv8803_nvram_write(void *priv, unsigned int offset, void *val,
465 size_t bytes)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100466{
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100467 int ret;
468
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200469 ret = rv8803_write_reg(priv, RV8803_RAM, *(u8 *)val);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200470 if (ret)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100471 return ret;
472
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200473 return 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100474}
475
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200476static int rv8803_nvram_read(void *priv, unsigned int offset,
477 void *val, size_t bytes)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100478{
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100479 int ret;
480
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200481 ret = rv8803_read_reg(priv, RV8803_RAM);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100482 if (ret < 0)
483 return ret;
484
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200485 *(u8 *)val = ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100486
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200487 return 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100488}
489
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100490static struct rtc_class_ops rv8803_rtc_ops = {
491 .read_time = rv8803_get_time,
492 .set_time = rv8803_set_time,
493 .ioctl = rv8803_ioctl,
494};
495
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200496static int rx8900_trickle_charger_init(struct rv8803_data *rv8803)
497{
498 struct i2c_client *client = rv8803->client;
499 struct device_node *node = client->dev.of_node;
500 int err;
501 u8 flags;
502
503 if (!node)
504 return 0;
505
506 if (rv8803->type != rx_8900)
507 return 0;
508
509 err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL);
510 if (err < 0)
511 return err;
512
513 flags = ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF) & (u8)err;
514
515 if (of_property_read_bool(node, "epson,vdet-disable"))
516 flags |= RX8900_FLAG_VDETOFF;
517
518 if (of_property_read_bool(node, "trickle-diode-disable"))
519 flags |= RX8900_FLAG_SWOFF;
520
521 return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL,
522 flags);
523}
524
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100525static int rv8803_probe(struct i2c_client *client,
526 const struct i2c_device_id *id)
527{
528 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
529 struct rv8803_data *rv8803;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200530 int err, flags;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100531
532 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
533 I2C_FUNC_SMBUS_I2C_BLOCK)) {
534 dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
535 return -EIO;
536 }
537
538 rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
539 GFP_KERNEL);
540 if (!rv8803)
541 return -ENOMEM;
542
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100543 mutex_init(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100544 rv8803->client = client;
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300545 if (client->dev.of_node)
546 rv8803->type = (enum rv8803_type)
547 of_device_get_match_data(&client->dev);
548 else
549 rv8803->type = id->driver_data;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100550 i2c_set_clientdata(client, rv8803);
551
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200552 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100553 if (flags < 0)
554 return flags;
555
556 if (flags & RV8803_FLAG_V1F)
557 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
558
559 if (flags & RV8803_FLAG_V2F)
560 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
561
562 if (flags & RV8803_FLAG_AF)
563 dev_warn(&client->dev, "An alarm maybe have been missed.\n");
564
Alexandre Belloni7133eca2017-07-06 11:42:03 +0200565 rv8803->rtc = devm_rtc_allocate_device(&client->dev);
566 if (IS_ERR(rv8803->rtc)) {
567 return PTR_ERR(rv8803->rtc);
568 }
569
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100570 if (client->irq > 0) {
571 err = devm_request_threaded_irq(&client->dev, client->irq,
572 NULL, rv8803_handle_irq,
573 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
574 "rv8803", client);
575 if (err) {
576 dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
577 client->irq = 0;
578 } else {
579 rv8803_rtc_ops.read_alarm = rv8803_get_alarm;
580 rv8803_rtc_ops.set_alarm = rv8803_set_alarm;
581 rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable;
582 }
583 }
584
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200585 rv8803->nvmem_cfg.name = "rv8803_nvram",
586 rv8803->nvmem_cfg.word_size = 1,
587 rv8803->nvmem_cfg.stride = 1,
588 rv8803->nvmem_cfg.size = 1,
589 rv8803->nvmem_cfg.reg_read = rv8803_nvram_read,
590 rv8803->nvmem_cfg.reg_write = rv8803_nvram_write,
591 rv8803->nvmem_cfg.priv = client;
592
Alexandre Belloni7133eca2017-07-06 11:42:03 +0200593 rv8803->rtc->ops = &rv8803_rtc_ops;
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200594 rv8803->rtc->nvmem_config = &rv8803->nvmem_cfg;
595 rv8803->rtc->nvram_old_abi = true;
Alexandre Belloni7133eca2017-07-06 11:42:03 +0200596 err = rtc_register_device(rv8803->rtc);
597 if (err)
598 return err;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100599
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200600 err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100601 if (err)
602 return err;
603
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200604 err = rx8900_trickle_charger_init(rv8803);
605 if (err) {
606 dev_err(&client->dev, "failed to init charger\n");
607 return err;
608 }
609
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100610 rv8803->rtc->max_user_freq = 1;
611
612 return 0;
613}
614
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100615static const struct i2c_device_id rv8803_id[] = {
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200616 { "rv8803", rv_8803 },
617 { "rx8900", rx_8900 },
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100618 { }
619};
620MODULE_DEVICE_TABLE(i2c, rv8803_id);
621
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300622static const struct of_device_id rv8803_of_match[] = {
623 {
624 .compatible = "microcrystal,rv8803",
625 .data = (void *)rx_8900
626 },
627 {
628 .compatible = "epson,rx8900",
629 .data = (void *)rx_8900
630 },
631 { }
632};
633MODULE_DEVICE_TABLE(of, rv8803_of_match);
634
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100635static struct i2c_driver rv8803_driver = {
636 .driver = {
637 .name = "rtc-rv8803",
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300638 .of_match_table = of_match_ptr(rv8803_of_match),
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100639 },
640 .probe = rv8803_probe,
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100641 .id_table = rv8803_id,
642};
643module_i2c_driver(rv8803_driver);
644
645MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
646MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
647MODULE_LICENSE("GPL v2");