blob: f69e0b1137cd02659ae07d22f4b3f8eda856cd85 [file] [log] [blame]
Alexandre Belloni182ae2b2019-03-04 20:12:17 +01001// SPDX-License-Identifier: GPL-2.0
Alexandre Belloni1e3929e2015-11-02 23:48:32 +01002/*
3 * RTC driver for the Micro Crystal RV8803
4 *
5 * Copyright (C) 2015 Micro Crystal SA
Alexandre Belloni7d1e5bf2019-03-04 20:17:38 +01006 * Alexandre Belloni <alexandre.belloni@bootlin.com>
Alexandre Belloni1e3929e2015-11-02 23:48:32 +01007 *
Alexandre Belloni1e3929e2015-11-02 23:48:32 +01008 */
9
10#include <linux/bcd.h>
11#include <linux/bitops.h>
Benoît Thébaudeaua1e98e02016-07-21 12:41:29 +020012#include <linux/log2.h>
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010013#include <linux/i2c.h>
14#include <linux/interrupt.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -030017#include <linux/of_device.h>
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010018#include <linux/rtc.h>
19
Benoît Thébaudeaud5226492016-07-21 12:41:30 +020020#define RV8803_I2C_TRY_COUNT 4
21
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010022#define RV8803_SEC 0x00
23#define RV8803_MIN 0x01
24#define RV8803_HOUR 0x02
25#define RV8803_WEEK 0x03
26#define RV8803_DAY 0x04
27#define RV8803_MONTH 0x05
28#define RV8803_YEAR 0x06
29#define RV8803_RAM 0x07
30#define RV8803_ALARM_MIN 0x08
31#define RV8803_ALARM_HOUR 0x09
32#define RV8803_ALARM_WEEK_OR_DAY 0x0A
33#define RV8803_EXT 0x0D
34#define RV8803_FLAG 0x0E
35#define RV8803_CTRL 0x0F
36
37#define RV8803_EXT_WADA BIT(6)
38
39#define RV8803_FLAG_V1F BIT(0)
40#define RV8803_FLAG_V2F BIT(1)
41#define RV8803_FLAG_AF BIT(3)
42#define RV8803_FLAG_TF BIT(4)
43#define RV8803_FLAG_UF BIT(5)
44
45#define RV8803_CTRL_RESET BIT(0)
46
47#define RV8803_CTRL_EIE BIT(2)
48#define RV8803_CTRL_AIE BIT(3)
49#define RV8803_CTRL_TIE BIT(4)
50#define RV8803_CTRL_UIE BIT(5)
51
Oleksij Rempel1cd71372016-06-29 16:40:01 +020052#define RX8900_BACKUP_CTRL 0x18
53#define RX8900_FLAG_SWOFF BIT(2)
54#define RX8900_FLAG_VDETOFF BIT(3)
55
56enum rv8803_type {
57 rv_8803,
Fabio Estevam5c0189a2021-11-30 09:58:30 -030058 rx_8804,
Oleksij Rempel1cd71372016-06-29 16:40:01 +020059 rx_8900
60};
61
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010062struct rv8803_data {
63 struct i2c_client *client;
64 struct rtc_device *rtc;
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +010065 struct mutex flags_lock;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010066 u8 ctrl;
Oleksij Rempel1cd71372016-06-29 16:40:01 +020067 enum rv8803_type type;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +010068};
69
Benoît Thébaudeaud5226492016-07-21 12:41:30 +020070static int rv8803_read_reg(const struct i2c_client *client, u8 reg)
71{
72 int try = RV8803_I2C_TRY_COUNT;
73 s32 ret;
74
75 /*
76 * There is a 61µs window during which the RTC does not acknowledge I2C
77 * transfers. In that case, ensure that there are multiple attempts.
78 */
79 do
80 ret = i2c_smbus_read_byte_data(client, reg);
81 while ((ret == -ENXIO || ret == -EIO) && --try);
82 if (ret < 0)
83 dev_err(&client->dev, "Unable to read register 0x%02x\n", reg);
84
85 return ret;
86}
87
88static int rv8803_read_regs(const struct i2c_client *client,
89 u8 reg, u8 count, u8 *values)
90{
91 int try = RV8803_I2C_TRY_COUNT;
92 s32 ret;
93
94 do
95 ret = i2c_smbus_read_i2c_block_data(client, reg, count, values);
96 while ((ret == -ENXIO || ret == -EIO) && --try);
97 if (ret != count) {
98 dev_err(&client->dev,
99 "Unable to read registers 0x%02x..0x%02x\n",
100 reg, reg + count - 1);
101 return ret < 0 ? ret : -EIO;
102 }
103
104 return 0;
105}
106
107static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value)
108{
109 int try = RV8803_I2C_TRY_COUNT;
110 s32 ret;
111
112 do
113 ret = i2c_smbus_write_byte_data(client, reg, value);
114 while ((ret == -ENXIO || ret == -EIO) && --try);
115 if (ret)
116 dev_err(&client->dev, "Unable to write register 0x%02x\n", reg);
117
118 return ret;
119}
120
121static int rv8803_write_regs(const struct i2c_client *client,
122 u8 reg, u8 count, const u8 *values)
123{
124 int try = RV8803_I2C_TRY_COUNT;
125 s32 ret;
126
127 do
128 ret = i2c_smbus_write_i2c_block_data(client, reg, count,
129 values);
130 while ((ret == -ENXIO || ret == -EIO) && --try);
131 if (ret)
132 dev_err(&client->dev,
133 "Unable to write registers 0x%02x..0x%02x\n",
134 reg, reg + count - 1);
135
136 return ret;
137}
138
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100139static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
140{
141 struct i2c_client *client = dev_id;
142 struct rv8803_data *rv8803 = i2c_get_clientdata(client);
143 unsigned long events = 0;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200144 int flags;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100145
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100146 mutex_lock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100147
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200148 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100149 if (flags <= 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100150 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100151 return IRQ_NONE;
152 }
153
154 if (flags & RV8803_FLAG_V1F)
155 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
156
157 if (flags & RV8803_FLAG_V2F)
158 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
159
160 if (flags & RV8803_FLAG_TF) {
161 flags &= ~RV8803_FLAG_TF;
162 rv8803->ctrl &= ~RV8803_CTRL_TIE;
163 events |= RTC_PF;
164 }
165
166 if (flags & RV8803_FLAG_AF) {
167 flags &= ~RV8803_FLAG_AF;
168 rv8803->ctrl &= ~RV8803_CTRL_AIE;
169 events |= RTC_AF;
170 }
171
172 if (flags & RV8803_FLAG_UF) {
173 flags &= ~RV8803_FLAG_UF;
174 rv8803->ctrl &= ~RV8803_CTRL_UIE;
175 events |= RTC_UF;
176 }
177
178 if (events) {
179 rtc_update_irq(rv8803->rtc, 1, events);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200180 rv8803_write_reg(client, RV8803_FLAG, flags);
181 rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100182 }
183
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100184 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100185
186 return IRQ_HANDLED;
187}
188
189static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
190{
191 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
192 u8 date1[7];
193 u8 date2[7];
194 u8 *date = date1;
195 int ret, flags;
196
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200197 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100198 if (flags < 0)
199 return flags;
200
201 if (flags & RV8803_FLAG_V2F) {
202 dev_warn(dev, "Voltage low, data is invalid.\n");
203 return -EINVAL;
204 }
205
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200206 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date);
207 if (ret)
208 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100209
210 if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200211 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2);
212 if (ret)
213 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100214
215 if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
216 date = date2;
217 }
218
219 tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f);
220 tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f);
221 tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
Benoît Thébaudeaua1e98e02016-07-21 12:41:29 +0200222 tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100223 tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
224 tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
225 tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
226
Benoît Thébaudeau96acb252016-07-21 12:41:28 +0200227 return 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100228}
229
230static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
231{
232 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
233 u8 date[7];
Benoît Thébaudeaud3700b62016-07-21 12:41:31 +0200234 int ctrl, flags, ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100235
Benoît Thébaudeaud3700b62016-07-21 12:41:31 +0200236 ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL);
237 if (ctrl < 0)
238 return ctrl;
239
240 /* Stop the clock */
241 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
242 ctrl | RV8803_CTRL_RESET);
243 if (ret)
244 return ret;
245
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100246 date[RV8803_SEC] = bin2bcd(tm->tm_sec);
247 date[RV8803_MIN] = bin2bcd(tm->tm_min);
248 date[RV8803_HOUR] = bin2bcd(tm->tm_hour);
249 date[RV8803_WEEK] = 1 << (tm->tm_wday);
250 date[RV8803_DAY] = bin2bcd(tm->tm_mday);
251 date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
252 date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100);
253
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200254 ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date);
255 if (ret)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100256 return ret;
257
Benoît Thébaudeaud3700b62016-07-21 12:41:31 +0200258 /* Restart the clock */
259 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
260 ctrl & ~RV8803_CTRL_RESET);
261 if (ret)
262 return ret;
263
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100264 mutex_lock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100265
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200266 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100267 if (flags < 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100268 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100269 return flags;
270 }
271
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200272 ret = rv8803_write_reg(rv8803->client, RV8803_FLAG,
Benoît Thébaudeau6f367782016-07-21 12:41:32 +0200273 flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F));
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100274
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100275 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100276
277 return ret;
278}
279
280static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
281{
282 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
283 struct i2c_client *client = rv8803->client;
284 u8 alarmvals[3];
285 int flags, ret;
286
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200287 ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals);
288 if (ret)
289 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100290
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200291 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100292 if (flags < 0)
293 return flags;
294
295 alrm->time.tm_sec = 0;
296 alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
297 alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100298 alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100299
300 alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
301 alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
302
303 return 0;
304}
305
306static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
307{
308 struct i2c_client *client = to_i2c_client(dev);
309 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
310 u8 alarmvals[3];
311 u8 ctrl[2];
312 int ret, err;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100313
314 /* The alarm has no seconds, round up to nearest minute */
315 if (alrm->time.tm_sec) {
316 time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
317
318 alarm_time += 60 - alrm->time.tm_sec;
319 rtc_time64_to_tm(alarm_time, &alrm->time);
320 }
321
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100322 mutex_lock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100323
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200324 ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl);
325 if (ret) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100326 mutex_unlock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200327 return ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100328 }
329
330 alarmvals[0] = bin2bcd(alrm->time.tm_min);
331 alarmvals[1] = bin2bcd(alrm->time.tm_hour);
332 alarmvals[2] = bin2bcd(alrm->time.tm_mday);
333
334 if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
335 rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200336 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
337 rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100338 if (err) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100339 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100340 return err;
341 }
342 }
343
Dominique Martinet03a86cd2021-11-01 10:33:59 +0900344 ctrl[0] &= ~RV8803_FLAG_AF;
345 err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[0]);
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100346 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100347 if (err)
348 return err;
349
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200350 err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100351 if (err)
352 return err;
353
354 if (alrm->enabled) {
355 if (rv8803->rtc->uie_rtctimer.enabled)
356 rv8803->ctrl |= RV8803_CTRL_UIE;
357 if (rv8803->rtc->aie_timer.enabled)
358 rv8803->ctrl |= RV8803_CTRL_AIE;
359
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200360 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
361 rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100362 if (err)
363 return err;
364 }
365
366 return 0;
367}
368
369static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
370{
371 struct i2c_client *client = to_i2c_client(dev);
372 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
373 int ctrl, flags, err;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100374
375 ctrl = rv8803->ctrl;
376
377 if (enabled) {
378 if (rv8803->rtc->uie_rtctimer.enabled)
379 ctrl |= RV8803_CTRL_UIE;
380 if (rv8803->rtc->aie_timer.enabled)
381 ctrl |= RV8803_CTRL_AIE;
382 } else {
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 }
388
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100389 mutex_lock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200390 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100391 if (flags < 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100392 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100393 return flags;
394 }
395 flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200396 err = rv8803_write_reg(client, RV8803_FLAG, flags);
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100397 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100398 if (err)
399 return err;
400
401 if (ctrl != rv8803->ctrl) {
402 rv8803->ctrl = ctrl;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200403 err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100404 if (err)
405 return err;
406 }
407
408 return 0;
409}
410
411static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
412{
413 struct i2c_client *client = to_i2c_client(dev);
414 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
Alexandre Bellonibcd17c52019-12-14 23:02:57 +0100415 unsigned int vl = 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100416 int flags, ret = 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100417
418 switch (cmd) {
419 case RTC_VL_READ:
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200420 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100421 if (flags < 0)
422 return flags;
423
Alexandre Bellonibcd17c52019-12-14 23:02:57 +0100424 if (flags & RV8803_FLAG_V1F) {
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100425 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
Alexandre Bellonibcd17c52019-12-14 23:02:57 +0100426 vl = RTC_VL_ACCURACY_LOW;
427 }
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100428
429 if (flags & RV8803_FLAG_V2F)
Alexandre Bellonibcd17c52019-12-14 23:02:57 +0100430 vl |= RTC_VL_DATA_INVALID;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100431
Alexandre Bellonibcd17c52019-12-14 23:02:57 +0100432 return put_user(vl, (unsigned int __user *)arg);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100433
434 case RTC_VL_CLR:
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100435 mutex_lock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200436 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100437 if (flags < 0) {
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100438 mutex_unlock(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100439 return flags;
440 }
441
Alexandre Belloni7e890a02019-12-14 23:02:56 +0100442 flags &= ~RV8803_FLAG_V1F;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200443 ret = rv8803_write_reg(client, RV8803_FLAG, flags);
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100444 mutex_unlock(&rv8803->flags_lock);
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200445 if (ret)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100446 return ret;
447
448 return 0;
449
450 default:
451 return -ENOIOCTLCMD;
452 }
453}
454
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200455static int rv8803_nvram_write(void *priv, unsigned int offset, void *val,
456 size_t bytes)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100457{
Liu Shixin179b4bc2020-09-21 16:24:49 +0800458 return rv8803_write_reg(priv, RV8803_RAM, *(u8 *)val);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100459}
460
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200461static int rv8803_nvram_read(void *priv, unsigned int offset,
462 void *val, size_t bytes)
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100463{
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100464 int ret;
465
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200466 ret = rv8803_read_reg(priv, RV8803_RAM);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100467 if (ret < 0)
468 return ret;
469
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200470 *(u8 *)val = ret;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100471
Alexandre Belloni16d70a72017-07-06 11:42:04 +0200472 return 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100473}
474
Alexandre Belloni45909e52021-01-11 00:17:51 +0100475static const struct rtc_class_ops rv8803_rtc_ops = {
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100476 .read_time = rv8803_get_time,
477 .set_time = rv8803_set_time,
478 .ioctl = rv8803_ioctl,
Alexandre Belloni45909e52021-01-11 00:17:51 +0100479 .read_alarm = rv8803_get_alarm,
480 .set_alarm = rv8803_set_alarm,
481 .alarm_irq_enable = rv8803_alarm_irq_enable,
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100482};
483
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200484static int rx8900_trickle_charger_init(struct rv8803_data *rv8803)
485{
486 struct i2c_client *client = rv8803->client;
487 struct device_node *node = client->dev.of_node;
488 int err;
489 u8 flags;
490
491 if (!node)
492 return 0;
493
494 if (rv8803->type != rx_8900)
495 return 0;
496
497 err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL);
498 if (err < 0)
499 return err;
500
501 flags = ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF) & (u8)err;
502
503 if (of_property_read_bool(node, "epson,vdet-disable"))
504 flags |= RX8900_FLAG_VDETOFF;
505
506 if (of_property_read_bool(node, "trickle-diode-disable"))
507 flags |= RX8900_FLAG_SWOFF;
508
509 return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL,
510 flags);
511}
512
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100513static int rv8803_probe(struct i2c_client *client,
514 const struct i2c_device_id *id)
515{
Wolfram Sang5cb17262019-06-08 12:56:07 +0200516 struct i2c_adapter *adapter = client->adapter;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100517 struct rv8803_data *rv8803;
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200518 int err, flags;
Alexandre Bellonic07fd9de2018-02-12 23:47:32 +0100519 struct nvmem_config nvmem_cfg = {
520 .name = "rv8803_nvram",
521 .word_size = 1,
522 .stride = 1,
523 .size = 1,
524 .reg_read = rv8803_nvram_read,
525 .reg_write = rv8803_nvram_write,
526 .priv = client,
527 };
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100528
529 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
530 I2C_FUNC_SMBUS_I2C_BLOCK)) {
531 dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
532 return -EIO;
533 }
534
535 rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
536 GFP_KERNEL);
537 if (!rv8803)
538 return -ENOMEM;
539
Oleksij Rempel9d1fa4c2016-02-04 13:45:20 +0100540 mutex_init(&rv8803->flags_lock);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100541 rv8803->client = client;
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300542 if (client->dev.of_node)
543 rv8803->type = (enum rv8803_type)
544 of_device_get_match_data(&client->dev);
545 else
546 rv8803->type = id->driver_data;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100547 i2c_set_clientdata(client, rv8803);
548
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200549 flags = rv8803_read_reg(client, RV8803_FLAG);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100550 if (flags < 0)
551 return flags;
552
553 if (flags & RV8803_FLAG_V1F)
554 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
555
556 if (flags & RV8803_FLAG_V2F)
557 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
558
559 if (flags & RV8803_FLAG_AF)
560 dev_warn(&client->dev, "An alarm maybe have been missed.\n");
561
Alexandre Belloni7133eca2017-07-06 11:42:03 +0200562 rv8803->rtc = devm_rtc_allocate_device(&client->dev);
Alexandre Belloni44c638c2019-08-19 00:00:41 +0200563 if (IS_ERR(rv8803->rtc))
Alexandre Belloni7133eca2017-07-06 11:42:03 +0200564 return PTR_ERR(rv8803->rtc);
Alexandre Belloni7133eca2017-07-06 11:42:03 +0200565
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100566 if (client->irq > 0) {
567 err = devm_request_threaded_irq(&client->dev, client->irq,
568 NULL, rv8803_handle_irq,
569 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
570 "rv8803", client);
571 if (err) {
572 dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
573 client->irq = 0;
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100574 }
575 }
Alexandre Belloni45909e52021-01-11 00:17:51 +0100576 if (!client->irq)
577 clear_bit(RTC_FEATURE_ALARM, rv8803->rtc->features);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100578
Benoît Thébaudeaud5226492016-07-21 12:41:30 +0200579 err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA);
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100580 if (err)
581 return err;
582
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200583 err = rx8900_trickle_charger_init(rv8803);
584 if (err) {
585 dev_err(&client->dev, "failed to init charger\n");
586 return err;
587 }
588
Alexandre Bellonice1ae8e2018-02-12 23:47:33 +0100589 rv8803->rtc->ops = &rv8803_rtc_ops;
Alexandre Belloni2e17f8b2019-03-04 11:03:46 +0100590 rv8803->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
591 rv8803->rtc->range_max = RTC_TIMESTAMP_END_2099;
Bartosz Golaszewskifdcfd852020-11-09 17:34:08 +0100592 err = devm_rtc_register_device(rv8803->rtc);
Alexandre Bellonice1ae8e2018-02-12 23:47:33 +0100593 if (err)
594 return err;
595
Bartosz Golaszewski3a905c2d2020-11-09 17:34:06 +0100596 devm_rtc_nvmem_register(rv8803->rtc, &nvmem_cfg);
Alexandre Bellonice1ae8e2018-02-12 23:47:33 +0100597
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100598 rv8803->rtc->max_user_freq = 1;
599
600 return 0;
601}
602
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100603static const struct i2c_device_id rv8803_id[] = {
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200604 { "rv8803", rv_8803 },
Fabio Estevam5c0189a2021-11-30 09:58:30 -0300605 { "rv8804", rx_8804 },
Alexandre Belloniac771ed2018-08-27 23:23:44 +0200606 { "rx8803", rv_8803 },
Oleksij Rempel1cd71372016-06-29 16:40:01 +0200607 { "rx8900", rx_8900 },
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100608 { }
609};
610MODULE_DEVICE_TABLE(i2c, rv8803_id);
611
Alexandre Bellonida826322021-02-02 12:22:14 +0100612static const __maybe_unused struct of_device_id rv8803_of_match[] = {
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300613 {
614 .compatible = "microcrystal,rv8803",
Alexandre Bellonic8566182018-08-27 23:23:43 +0200615 .data = (void *)rv_8803
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300616 },
617 {
Alexandre Belloniac771ed2018-08-27 23:23:44 +0200618 .compatible = "epson,rx8803",
619 .data = (void *)rv_8803
620 },
621 {
Fabio Estevam5c0189a2021-11-30 09:58:30 -0300622 .compatible = "epson,rx8804",
623 .data = (void *)rx_8804
624 },
625 {
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300626 .compatible = "epson,rx8900",
627 .data = (void *)rx_8900
628 },
629 { }
630};
631MODULE_DEVICE_TABLE(of, rv8803_of_match);
632
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100633static struct i2c_driver rv8803_driver = {
634 .driver = {
635 .name = "rtc-rv8803",
Javier Martinez Canillas740ad8f2017-03-03 11:29:12 -0300636 .of_match_table = of_match_ptr(rv8803_of_match),
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100637 },
638 .probe = rv8803_probe,
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100639 .id_table = rv8803_id,
640};
641module_i2c_driver(rv8803_driver);
642
Alexandre Belloni7d1e5bf2019-03-04 20:17:38 +0100643MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
Alexandre Belloni1e3929e2015-11-02 23:48:32 +0100644MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
645MODULE_LICENSE("GPL v2");