blob: f9277e536f7e8754120da35b17581b9f1492c632 [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>
21#include <linux/rtc.h>
22
Benoît Thébaudeaud5226492016-07-21 12:41:30 +020023#define RV8803_I2C_TRY_COUNT 4
24
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010025#define RV8803_SEC 0x00
26#define RV8803_MIN 0x01
27#define RV8803_HOUR 0x02
28#define RV8803_WEEK 0x03
29#define RV8803_DAY 0x04
30#define RV8803_MONTH 0x05
31#define RV8803_YEAR 0x06
32#define RV8803_RAM 0x07
33#define RV8803_ALARM_MIN 0x08
34#define RV8803_ALARM_HOUR 0x09
35#define RV8803_ALARM_WEEK_OR_DAY 0x0A
36#define RV8803_EXT 0x0D
37#define RV8803_FLAG 0x0E
38#define RV8803_CTRL 0x0F
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
Oleksij Rempel1cd71372016-06-29 16:40:01 +020055#define RX8900_BACKUP_CTRL 0x18
56#define RX8900_FLAG_SWOFF BIT(2)
57#define RX8900_FLAG_VDETOFF BIT(3)
58
59enum rv8803_type {
60 rv_8803,
61 rx_8900
62};
63
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010064struct rv8803_data {
65 struct i2c_client *client;
66 struct rtc_device *rtc;
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +010067 struct mutex flags_lock;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010068 u8 ctrl;
Oleksij Rempel1cd71372016-06-29 16:40:01 +020069 enum rv8803_type type;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010070};
71
Benoît Thébaudeaud5226492016-07-21 12:41:30 +020072static int rv8803_read_reg(const struct i2c_client *client, u8 reg)
73{
74 int try = RV8803_I2C_TRY_COUNT;
75 s32 ret;
76
77 /*
78 * There is a 61µs window during which the RTC does not acknowledge I2C
79 * transfers. In that case, ensure that there are multiple attempts.
80 */
81 do
82 ret = i2c_smbus_read_byte_data(client, reg);
83 while ((ret == -ENXIO || ret == -EIO) && --try);
84 if (ret < 0)
85 dev_err(&client->dev, "Unable to read register 0x%02x\n", reg);
86
87 return ret;
88}
89
90static int rv8803_read_regs(const struct i2c_client *client,
91 u8 reg, u8 count, u8 *values)
92{
93 int try = RV8803_I2C_TRY_COUNT;
94 s32 ret;
95
96 do
97 ret = i2c_smbus_read_i2c_block_data(client, reg, count, values);
98 while ((ret == -ENXIO || ret == -EIO) && --try);
99 if (ret != count) {
100 dev_err(&client->dev,
101 "Unable to read registers 0x%02x..0x%02x\n",
102 reg, reg + count - 1);
103 return ret < 0 ? ret : -EIO;
104 }
105
106 return 0;
107}
108
109static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value)
110{
111 int try = RV8803_I2C_TRY_COUNT;
112 s32 ret;
113
114 do
115 ret = i2c_smbus_write_byte_data(client, reg, value);
116 while ((ret == -ENXIO || ret == -EIO) && --try);
117 if (ret)
118 dev_err(&client->dev, "Unable to write register 0x%02x\n", reg);
119
120 return ret;
121}
122
123static int rv8803_write_regs(const struct i2c_client *client,
124 u8 reg, u8 count, const u8 *values)
125{
126 int try = RV8803_I2C_TRY_COUNT;
127 s32 ret;
128
129 do
130 ret = i2c_smbus_write_i2c_block_data(client, reg, count,
131 values);
132 while ((ret == -ENXIO || ret == -EIO) && --try);
133 if (ret)
134 dev_err(&client->dev,
135 "Unable to write registers 0x%02x..0x%02x\n",
136 reg, reg + count - 1);
137
138 return ret;
139}
140
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100141static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
142{
143 struct i2c_client *client = dev_id;
144 struct rv8803_data *rv8803 = i2c_get_clientdata(client);
145 unsigned long events = 0;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200146 int flags;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100147
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100148 mutex_lock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100149
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200150 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100151 if (flags <= 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100152 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100153 return IRQ_NONE;
154 }
155
156 if (flags & RV8803_FLAG_V1F)
157 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
158
159 if (flags & RV8803_FLAG_V2F)
160 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
161
162 if (flags & RV8803_FLAG_TF) {
163 flags &= ~RV8803_FLAG_TF;
164 rv8803->ctrl &= ~RV8803_CTRL_TIE;
165 events |= RTC_PF;
166 }
167
168 if (flags & RV8803_FLAG_AF) {
169 flags &= ~RV8803_FLAG_AF;
170 rv8803->ctrl &= ~RV8803_CTRL_AIE;
171 events |= RTC_AF;
172 }
173
174 if (flags & RV8803_FLAG_UF) {
175 flags &= ~RV8803_FLAG_UF;
176 rv8803->ctrl &= ~RV8803_CTRL_UIE;
177 events |= RTC_UF;
178 }
179
180 if (events) {
181 rtc_update_irq(rv8803->rtc, 1, events);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200182 rv8803_write_reg(client, RV8803_FLAG, flags);
183 rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100184 }
185
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100186 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100187
188 return IRQ_HANDLED;
189}
190
191static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
192{
193 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
194 u8 date1[7];
195 u8 date2[7];
196 u8 *date = date1;
197 int ret, flags;
198
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200199 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100200 if (flags < 0)
201 return flags;
202
203 if (flags & RV8803_FLAG_V2F) {
204 dev_warn(dev, "Voltage low, data is invalid.\n");
205 return -EINVAL;
206 }
207
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200208 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date);
209 if (ret)
210 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100211
212 if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200213 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2);
214 if (ret)
215 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100216
217 if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
218 date = date2;
219 }
220
221 tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f);
222 tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f);
223 tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
Benoît Thébaudeaua1e98e02016-07-21 12:41:29 +0200224 tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100225 tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
226 tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
227 tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
228
Benoît Thébaudeau96acb252016-07-21 12:41:28 +0200229 return 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100230}
231
232static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
233{
234 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
235 u8 date[7];
Benoît Thébaudeaud3700b62016-07-21 12:41:31 +0200236 int ctrl, flags, ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100237
238 if ((tm->tm_year < 100) || (tm->tm_year > 199))
239 return -EINVAL;
240
Benoît Thébaudeaud3700b62016-07-21 12:41:31 +0200241 ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL);
242 if (ctrl < 0)
243 return ctrl;
244
245 /* Stop the clock */
246 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
247 ctrl | RV8803_CTRL_RESET);
248 if (ret)
249 return ret;
250
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100251 date[RV8803_SEC] = bin2bcd(tm->tm_sec);
252 date[RV8803_MIN] = bin2bcd(tm->tm_min);
253 date[RV8803_HOUR] = bin2bcd(tm->tm_hour);
254 date[RV8803_WEEK] = 1 << (tm->tm_wday);
255 date[RV8803_DAY] = bin2bcd(tm->tm_mday);
256 date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
257 date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100);
258
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200259 ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date);
260 if (ret)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100261 return ret;
262
Benoît Thébaudeaud3700b62016-07-21 12:41:31 +0200263 /* Restart the clock */
264 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
265 ctrl & ~RV8803_CTRL_RESET);
266 if (ret)
267 return ret;
268
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100269 mutex_lock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100270
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200271 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100272 if (flags < 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100273 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100274 return flags;
275 }
276
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200277 ret = rv8803_write_reg(rv8803->client, RV8803_FLAG,
Benoît Thébaudeau6f367782016-07-21 12:41:32 +0200278 flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F));
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100279
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100280 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100281
282 return ret;
283}
284
285static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
286{
287 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
288 struct i2c_client *client = rv8803->client;
289 u8 alarmvals[3];
290 int flags, ret;
291
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200292 ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals);
293 if (ret)
294 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100295
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200296 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100297 if (flags < 0)
298 return flags;
299
300 alrm->time.tm_sec = 0;
301 alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
302 alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100303 alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100304
305 alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
306 alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
307
308 return 0;
309}
310
311static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
312{
313 struct i2c_client *client = to_i2c_client(dev);
314 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
315 u8 alarmvals[3];
316 u8 ctrl[2];
317 int ret, err;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100318
319 /* The alarm has no seconds, round up to nearest minute */
320 if (alrm->time.tm_sec) {
321 time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
322
323 alarm_time += 60 - alrm->time.tm_sec;
324 rtc_time64_to_tm(alarm_time, &alrm->time);
325 }
326
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100327 mutex_lock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100328
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200329 ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl);
330 if (ret) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100331 mutex_unlock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200332 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100333 }
334
335 alarmvals[0] = bin2bcd(alrm->time.tm_min);
336 alarmvals[1] = bin2bcd(alrm->time.tm_hour);
337 alarmvals[2] = bin2bcd(alrm->time.tm_mday);
338
339 if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
340 rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200341 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
342 rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100343 if (err) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100344 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100345 return err;
346 }
347 }
348
349 ctrl[1] &= ~RV8803_FLAG_AF;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200350 err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[1]);
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100351 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100352 if (err)
353 return err;
354
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200355 err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100356 if (err)
357 return err;
358
359 if (alrm->enabled) {
360 if (rv8803->rtc->uie_rtctimer.enabled)
361 rv8803->ctrl |= RV8803_CTRL_UIE;
362 if (rv8803->rtc->aie_timer.enabled)
363 rv8803->ctrl |= RV8803_CTRL_AIE;
364
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200365 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
366 rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100367 if (err)
368 return err;
369 }
370
371 return 0;
372}
373
374static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
375{
376 struct i2c_client *client = to_i2c_client(dev);
377 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
378 int ctrl, flags, err;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100379
380 ctrl = rv8803->ctrl;
381
382 if (enabled) {
383 if (rv8803->rtc->uie_rtctimer.enabled)
384 ctrl |= RV8803_CTRL_UIE;
385 if (rv8803->rtc->aie_timer.enabled)
386 ctrl |= RV8803_CTRL_AIE;
387 } else {
388 if (!rv8803->rtc->uie_rtctimer.enabled)
389 ctrl &= ~RV8803_CTRL_UIE;
390 if (!rv8803->rtc->aie_timer.enabled)
391 ctrl &= ~RV8803_CTRL_AIE;
392 }
393
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100394 mutex_lock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200395 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100396 if (flags < 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100397 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100398 return flags;
399 }
400 flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200401 err = rv8803_write_reg(client, RV8803_FLAG, flags);
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100402 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100403 if (err)
404 return err;
405
406 if (ctrl != rv8803->ctrl) {
407 rv8803->ctrl = ctrl;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200408 err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100409 if (err)
410 return err;
411 }
412
413 return 0;
414}
415
416static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
417{
418 struct i2c_client *client = to_i2c_client(dev);
419 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
420 int flags, ret = 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100421
422 switch (cmd) {
423 case RTC_VL_READ:
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200424 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100425 if (flags < 0)
426 return flags;
427
428 if (flags & RV8803_FLAG_V1F)
429 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
430
431 if (flags & RV8803_FLAG_V2F)
432 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
433
434 flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F;
435
436 if (copy_to_user((void __user *)arg, &flags, sizeof(int)))
437 return -EFAULT;
438
439 return 0;
440
441 case RTC_VL_CLR:
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100442 mutex_lock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200443 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100444 if (flags < 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100445 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100446 return flags;
447 }
448
449 flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200450 ret = rv8803_write_reg(client, RV8803_FLAG, flags);
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100451 mutex_unlock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200452 if (ret)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100453 return ret;
454
455 return 0;
456
457 default:
458 return -ENOIOCTLCMD;
459 }
460}
461
462static ssize_t rv8803_nvram_write(struct file *filp, struct kobject *kobj,
463 struct bin_attribute *attr,
464 char *buf, loff_t off, size_t count)
465{
466 struct device *dev = kobj_to_dev(kobj);
467 struct i2c_client *client = to_i2c_client(dev);
468 int ret;
469
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200470 ret = rv8803_write_reg(client, RV8803_RAM, buf[0]);
471 if (ret)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100472 return ret;
473
474 return 1;
475}
476
477static ssize_t rv8803_nvram_read(struct file *filp, struct kobject *kobj,
478 struct bin_attribute *attr,
479 char *buf, loff_t off, size_t count)
480{
481 struct device *dev = kobj_to_dev(kobj);
482 struct i2c_client *client = to_i2c_client(dev);
483 int ret;
484
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200485 ret = rv8803_read_reg(client, RV8803_RAM);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100486 if (ret < 0)
487 return ret;
488
489 buf[0] = ret;
490
491 return 1;
492}
493
494static struct bin_attribute rv8803_nvram_attr = {
495 .attr = {
496 .name = "nvram",
497 .mode = S_IRUGO | S_IWUSR,
498 },
499 .size = 1,
500 .read = rv8803_nvram_read,
501 .write = rv8803_nvram_write,
502};
503
504static struct rtc_class_ops rv8803_rtc_ops = {
505 .read_time = rv8803_get_time,
506 .set_time = rv8803_set_time,
507 .ioctl = rv8803_ioctl,
508};
509
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200510static int rx8900_trickle_charger_init(struct rv8803_data *rv8803)
511{
512 struct i2c_client *client = rv8803->client;
513 struct device_node *node = client->dev.of_node;
514 int err;
515 u8 flags;
516
517 if (!node)
518 return 0;
519
520 if (rv8803->type != rx_8900)
521 return 0;
522
523 err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL);
524 if (err < 0)
525 return err;
526
527 flags = ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF) & (u8)err;
528
529 if (of_property_read_bool(node, "epson,vdet-disable"))
530 flags |= RX8900_FLAG_VDETOFF;
531
532 if (of_property_read_bool(node, "trickle-diode-disable"))
533 flags |= RX8900_FLAG_SWOFF;
534
535 return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL,
536 flags);
537}
538
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100539static int rv8803_probe(struct i2c_client *client,
540 const struct i2c_device_id *id)
541{
542 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
543 struct rv8803_data *rv8803;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200544 int err, flags;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100545
546 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
547 I2C_FUNC_SMBUS_I2C_BLOCK)) {
548 dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
549 return -EIO;
550 }
551
552 rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
553 GFP_KERNEL);
554 if (!rv8803)
555 return -ENOMEM;
556
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100557 mutex_init(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100558 rv8803->client = client;
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200559 rv8803->type = id->driver_data;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100560 i2c_set_clientdata(client, rv8803);
561
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200562 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100563 if (flags < 0)
564 return flags;
565
566 if (flags & RV8803_FLAG_V1F)
567 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
568
569 if (flags & RV8803_FLAG_V2F)
570 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
571
572 if (flags & RV8803_FLAG_AF)
573 dev_warn(&client->dev, "An alarm maybe have been missed.\n");
574
575 if (client->irq > 0) {
576 err = devm_request_threaded_irq(&client->dev, client->irq,
577 NULL, rv8803_handle_irq,
578 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
579 "rv8803", client);
580 if (err) {
581 dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
582 client->irq = 0;
583 } else {
584 rv8803_rtc_ops.read_alarm = rv8803_get_alarm;
585 rv8803_rtc_ops.set_alarm = rv8803_set_alarm;
586 rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable;
587 }
588 }
589
590 rv8803->rtc = devm_rtc_device_register(&client->dev, client->name,
591 &rv8803_rtc_ops, THIS_MODULE);
592 if (IS_ERR(rv8803->rtc)) {
593 dev_err(&client->dev, "unable to register the class device\n");
594 return PTR_ERR(rv8803->rtc);
595 }
596
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200597 err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100598 if (err)
599 return err;
600
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200601 err = rx8900_trickle_charger_init(rv8803);
602 if (err) {
603 dev_err(&client->dev, "failed to init charger\n");
604 return err;
605 }
606
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100607 err = device_create_bin_file(&client->dev, &rv8803_nvram_attr);
608 if (err)
609 return err;
610
611 rv8803->rtc->max_user_freq = 1;
612
613 return 0;
614}
615
616static int rv8803_remove(struct i2c_client *client)
617{
618 device_remove_bin_file(&client->dev, &rv8803_nvram_attr);
619
620 return 0;
621}
622
623static const struct i2c_device_id rv8803_id[] = {
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200624 { "rv8803", rv_8803 },
625 { "rx8900", rx_8900 },
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100626 { }
627};
628MODULE_DEVICE_TABLE(i2c, rv8803_id);
629
630static struct i2c_driver rv8803_driver = {
631 .driver = {
632 .name = "rtc-rv8803",
633 },
634 .probe = rv8803_probe,
635 .remove = rv8803_remove,
636 .id_table = rv8803_id,
637};
638module_i2c_driver(rv8803_driver);
639
640MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
641MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
642MODULE_LICENSE("GPL v2");