blob: df000a68d6b1b0b92acc3492c38265dc9f0b378f [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 Belloni1e3929e2015-11-02 23:48:32 +010071};
72
Benoît Thébaudeaud5226492016-07-21 12:41:30 +020073static int rv8803_read_reg(const struct i2c_client *client, u8 reg)
74{
75 int try = RV8803_I2C_TRY_COUNT;
76 s32 ret;
77
78 /*
79 * There is a 61µs window during which the RTC does not acknowledge I2C
80 * transfers. In that case, ensure that there are multiple attempts.
81 */
82 do
83 ret = i2c_smbus_read_byte_data(client, reg);
84 while ((ret == -ENXIO || ret == -EIO) && --try);
85 if (ret < 0)
86 dev_err(&client->dev, "Unable to read register 0x%02x\n", reg);
87
88 return ret;
89}
90
91static int rv8803_read_regs(const struct i2c_client *client,
92 u8 reg, u8 count, u8 *values)
93{
94 int try = RV8803_I2C_TRY_COUNT;
95 s32 ret;
96
97 do
98 ret = i2c_smbus_read_i2c_block_data(client, reg, count, values);
99 while ((ret == -ENXIO || ret == -EIO) && --try);
100 if (ret != count) {
101 dev_err(&client->dev,
102 "Unable to read registers 0x%02x..0x%02x\n",
103 reg, reg + count - 1);
104 return ret < 0 ? ret : -EIO;
105 }
106
107 return 0;
108}
109
110static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value)
111{
112 int try = RV8803_I2C_TRY_COUNT;
113 s32 ret;
114
115 do
116 ret = i2c_smbus_write_byte_data(client, reg, value);
117 while ((ret == -ENXIO || ret == -EIO) && --try);
118 if (ret)
119 dev_err(&client->dev, "Unable to write register 0x%02x\n", reg);
120
121 return ret;
122}
123
124static int rv8803_write_regs(const struct i2c_client *client,
125 u8 reg, u8 count, const u8 *values)
126{
127 int try = RV8803_I2C_TRY_COUNT;
128 s32 ret;
129
130 do
131 ret = i2c_smbus_write_i2c_block_data(client, reg, count,
132 values);
133 while ((ret == -ENXIO || ret == -EIO) && --try);
134 if (ret)
135 dev_err(&client->dev,
136 "Unable to write registers 0x%02x..0x%02x\n",
137 reg, reg + count - 1);
138
139 return ret;
140}
141
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100142static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
143{
144 struct i2c_client *client = dev_id;
145 struct rv8803_data *rv8803 = i2c_get_clientdata(client);
146 unsigned long events = 0;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200147 int flags;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100148
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100149 mutex_lock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100150
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200151 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100152 if (flags <= 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100153 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100154 return IRQ_NONE;
155 }
156
157 if (flags & RV8803_FLAG_V1F)
158 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
159
160 if (flags & RV8803_FLAG_V2F)
161 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
162
163 if (flags & RV8803_FLAG_TF) {
164 flags &= ~RV8803_FLAG_TF;
165 rv8803->ctrl &= ~RV8803_CTRL_TIE;
166 events |= RTC_PF;
167 }
168
169 if (flags & RV8803_FLAG_AF) {
170 flags &= ~RV8803_FLAG_AF;
171 rv8803->ctrl &= ~RV8803_CTRL_AIE;
172 events |= RTC_AF;
173 }
174
175 if (flags & RV8803_FLAG_UF) {
176 flags &= ~RV8803_FLAG_UF;
177 rv8803->ctrl &= ~RV8803_CTRL_UIE;
178 events |= RTC_UF;
179 }
180
181 if (events) {
182 rtc_update_irq(rv8803->rtc, 1, events);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200183 rv8803_write_reg(client, RV8803_FLAG, flags);
184 rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100185 }
186
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100187 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100188
189 return IRQ_HANDLED;
190}
191
192static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
193{
194 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
195 u8 date1[7];
196 u8 date2[7];
197 u8 *date = date1;
198 int ret, flags;
199
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200200 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100201 if (flags < 0)
202 return flags;
203
204 if (flags & RV8803_FLAG_V2F) {
205 dev_warn(dev, "Voltage low, data is invalid.\n");
206 return -EINVAL;
207 }
208
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200209 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date);
210 if (ret)
211 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100212
213 if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200214 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2);
215 if (ret)
216 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100217
218 if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
219 date = date2;
220 }
221
222 tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f);
223 tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f);
224 tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
Benoît Thébaudeaua1e98e02016-07-21 12:41:29 +0200225 tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100226 tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
227 tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
228 tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
229
Benoît Thébaudeau96acb252016-07-21 12:41:28 +0200230 return 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100231}
232
233static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
234{
235 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
236 u8 date[7];
Benoît Thébaudeaud3700b62016-07-21 12:41:31 +0200237 int ctrl, flags, ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100238
Benoît Thébaudeaud3700b62016-07-21 12:41:31 +0200239 ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL);
240 if (ctrl < 0)
241 return ctrl;
242
243 /* Stop the clock */
244 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
245 ctrl | RV8803_CTRL_RESET);
246 if (ret)
247 return ret;
248
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100249 date[RV8803_SEC] = bin2bcd(tm->tm_sec);
250 date[RV8803_MIN] = bin2bcd(tm->tm_min);
251 date[RV8803_HOUR] = bin2bcd(tm->tm_hour);
252 date[RV8803_WEEK] = 1 << (tm->tm_wday);
253 date[RV8803_DAY] = bin2bcd(tm->tm_mday);
254 date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
255 date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100);
256
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200257 ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date);
258 if (ret)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100259 return ret;
260
Benoît Thébaudeaud3700b62016-07-21 12:41:31 +0200261 /* Restart the clock */
262 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
263 ctrl & ~RV8803_CTRL_RESET);
264 if (ret)
265 return ret;
266
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100267 mutex_lock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100268
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200269 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100270 if (flags < 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100271 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100272 return flags;
273 }
274
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200275 ret = rv8803_write_reg(rv8803->client, RV8803_FLAG,
Benoît Thébaudeau6f367782016-07-21 12:41:32 +0200276 flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F));
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100277
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100278 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100279
280 return ret;
281}
282
283static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
284{
285 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
286 struct i2c_client *client = rv8803->client;
287 u8 alarmvals[3];
288 int flags, ret;
289
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200290 ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals);
291 if (ret)
292 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100293
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200294 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100295 if (flags < 0)
296 return flags;
297
298 alrm->time.tm_sec = 0;
299 alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
300 alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100301 alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100302
303 alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
304 alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
305
306 return 0;
307}
308
309static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
310{
311 struct i2c_client *client = to_i2c_client(dev);
312 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
313 u8 alarmvals[3];
314 u8 ctrl[2];
315 int ret, err;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100316
317 /* The alarm has no seconds, round up to nearest minute */
318 if (alrm->time.tm_sec) {
319 time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
320
321 alarm_time += 60 - alrm->time.tm_sec;
322 rtc_time64_to_tm(alarm_time, &alrm->time);
323 }
324
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100325 mutex_lock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100326
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200327 ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl);
328 if (ret) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100329 mutex_unlock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200330 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100331 }
332
333 alarmvals[0] = bin2bcd(alrm->time.tm_min);
334 alarmvals[1] = bin2bcd(alrm->time.tm_hour);
335 alarmvals[2] = bin2bcd(alrm->time.tm_mday);
336
337 if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
338 rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200339 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
340 rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100341 if (err) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100342 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100343 return err;
344 }
345 }
346
347 ctrl[1] &= ~RV8803_FLAG_AF;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200348 err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[1]);
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100349 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100350 if (err)
351 return err;
352
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200353 err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100354 if (err)
355 return err;
356
357 if (alrm->enabled) {
358 if (rv8803->rtc->uie_rtctimer.enabled)
359 rv8803->ctrl |= RV8803_CTRL_UIE;
360 if (rv8803->rtc->aie_timer.enabled)
361 rv8803->ctrl |= RV8803_CTRL_AIE;
362
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200363 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
364 rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100365 if (err)
366 return err;
367 }
368
369 return 0;
370}
371
372static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
373{
374 struct i2c_client *client = to_i2c_client(dev);
375 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
376 int ctrl, flags, err;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100377
378 ctrl = rv8803->ctrl;
379
380 if (enabled) {
381 if (rv8803->rtc->uie_rtctimer.enabled)
382 ctrl |= RV8803_CTRL_UIE;
383 if (rv8803->rtc->aie_timer.enabled)
384 ctrl |= RV8803_CTRL_AIE;
385 } else {
386 if (!rv8803->rtc->uie_rtctimer.enabled)
387 ctrl &= ~RV8803_CTRL_UIE;
388 if (!rv8803->rtc->aie_timer.enabled)
389 ctrl &= ~RV8803_CTRL_AIE;
390 }
391
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100392 mutex_lock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200393 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100394 if (flags < 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100395 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100396 return flags;
397 }
398 flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200399 err = rv8803_write_reg(client, RV8803_FLAG, flags);
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100400 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100401 if (err)
402 return err;
403
404 if (ctrl != rv8803->ctrl) {
405 rv8803->ctrl = ctrl;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200406 err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100407 if (err)
408 return err;
409 }
410
411 return 0;
412}
413
414static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
415{
416 struct i2c_client *client = to_i2c_client(dev);
417 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
418 int flags, ret = 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100419
420 switch (cmd) {
421 case RTC_VL_READ:
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200422 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100423 if (flags < 0)
424 return flags;
425
426 if (flags & RV8803_FLAG_V1F)
427 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
428
429 if (flags & RV8803_FLAG_V2F)
430 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
431
432 flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F;
433
434 if (copy_to_user((void __user *)arg, &flags, sizeof(int)))
435 return -EFAULT;
436
437 return 0;
438
439 case RTC_VL_CLR:
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100440 mutex_lock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200441 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100442 if (flags < 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100443 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100444 return flags;
445 }
446
447 flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200448 ret = rv8803_write_reg(client, RV8803_FLAG, flags);
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100449 mutex_unlock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200450 if (ret)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100451 return ret;
452
453 return 0;
454
455 default:
456 return -ENOIOCTLCMD;
457 }
458}
459
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200460static int rv8803_nvram_write(void *priv, unsigned int offset, void *val,
461 size_t bytes)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100462{
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100463 int ret;
464
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200465 ret = rv8803_write_reg(priv, RV8803_RAM, *(u8 *)val);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200466 if (ret)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100467 return ret;
468
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200469 return 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100470}
471
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200472static int rv8803_nvram_read(void *priv, unsigned int offset,
473 void *val, size_t bytes)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100474{
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100475 int ret;
476
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200477 ret = rv8803_read_reg(priv, RV8803_RAM);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100478 if (ret < 0)
479 return ret;
480
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200481 *(u8 *)val = ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100482
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200483 return 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100484}
485
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100486static struct rtc_class_ops rv8803_rtc_ops = {
487 .read_time = rv8803_get_time,
488 .set_time = rv8803_set_time,
489 .ioctl = rv8803_ioctl,
490};
491
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200492static int rx8900_trickle_charger_init(struct rv8803_data *rv8803)
493{
494 struct i2c_client *client = rv8803->client;
495 struct device_node *node = client->dev.of_node;
496 int err;
497 u8 flags;
498
499 if (!node)
500 return 0;
501
502 if (rv8803->type != rx_8900)
503 return 0;
504
505 err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL);
506 if (err < 0)
507 return err;
508
509 flags = ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF) & (u8)err;
510
511 if (of_property_read_bool(node, "epson,vdet-disable"))
512 flags |= RX8900_FLAG_VDETOFF;
513
514 if (of_property_read_bool(node, "trickle-diode-disable"))
515 flags |= RX8900_FLAG_SWOFF;
516
517 return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL,
518 flags);
519}
520
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100521static int rv8803_probe(struct i2c_client *client,
522 const struct i2c_device_id *id)
523{
524 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
525 struct rv8803_data *rv8803;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200526 int err, flags;
Alexandre Bellonic07fd9de2018-02-12 23:47:32 +0100527 struct nvmem_config nvmem_cfg = {
528 .name = "rv8803_nvram",
529 .word_size = 1,
530 .stride = 1,
531 .size = 1,
532 .reg_read = rv8803_nvram_read,
533 .reg_write = rv8803_nvram_write,
534 .priv = client,
535 };
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100536
537 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
538 I2C_FUNC_SMBUS_I2C_BLOCK)) {
539 dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
540 return -EIO;
541 }
542
543 rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
544 GFP_KERNEL);
545 if (!rv8803)
546 return -ENOMEM;
547
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100548 mutex_init(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100549 rv8803->client = client;
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300550 if (client->dev.of_node)
551 rv8803->type = (enum rv8803_type)
552 of_device_get_match_data(&client->dev);
553 else
554 rv8803->type = id->driver_data;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100555 i2c_set_clientdata(client, rv8803);
556
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200557 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100558 if (flags < 0)
559 return flags;
560
561 if (flags & RV8803_FLAG_V1F)
562 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
563
564 if (flags & RV8803_FLAG_V2F)
565 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
566
567 if (flags & RV8803_FLAG_AF)
568 dev_warn(&client->dev, "An alarm maybe have been missed.\n");
569
Alexandre Belloni7133eca2017-07-06 11:42:03 +0200570 rv8803->rtc = devm_rtc_allocate_device(&client->dev);
571 if (IS_ERR(rv8803->rtc)) {
572 return PTR_ERR(rv8803->rtc);
573 }
574
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100575 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
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200590 err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100591 if (err)
592 return err;
593
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200594 err = rx8900_trickle_charger_init(rv8803);
595 if (err) {
596 dev_err(&client->dev, "failed to init charger\n");
597 return err;
598 }
599
Alexandre Bellonice1ae8e2018-02-12 23:47:33 +0100600 rv8803->rtc->ops = &rv8803_rtc_ops;
601 rv8803->rtc->nvram_old_abi = true;
Alexandre Belloni2e17f8b2019-03-04 11:03:46 +0100602 rv8803->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
603 rv8803->rtc->range_max = RTC_TIMESTAMP_END_2099;
Alexandre Bellonice1ae8e2018-02-12 23:47:33 +0100604 err = rtc_register_device(rv8803->rtc);
605 if (err)
606 return err;
607
608 rtc_nvmem_register(rv8803->rtc, &nvmem_cfg);
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 },
Alexandre Belloniac771ed2018-08-27 23:23:44 +0200617 { "rx8803", rv_8803 },
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200618 { "rx8900", rx_8900 },
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100619 { }
620};
621MODULE_DEVICE_TABLE(i2c, rv8803_id);
622
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300623static const struct of_device_id rv8803_of_match[] = {
624 {
625 .compatible = "microcrystal,rv8803",
Alexandre Bellonic8566182018-08-27 23:23:43 +0200626 .data = (void *)rv_8803
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300627 },
628 {
Alexandre Belloniac771ed2018-08-27 23:23:44 +0200629 .compatible = "epson,rx8803",
630 .data = (void *)rv_8803
631 },
632 {
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300633 .compatible = "epson,rx8900",
634 .data = (void *)rx_8900
635 },
636 { }
637};
638MODULE_DEVICE_TABLE(of, rv8803_of_match);
639
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100640static struct i2c_driver rv8803_driver = {
641 .driver = {
642 .name = "rtc-rv8803",
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300643 .of_match_table = of_match_ptr(rv8803_of_match),
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100644 },
645 .probe = rv8803_probe,
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100646 .id_table = rv8803_id,
647};
648module_i2c_driver(rv8803_driver);
649
650MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
651MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
652MODULE_LICENSE("GPL v2");