Blame view

drivers/rtc/rtc-rv8803.c 15.1 KB
182ae2bb6   Alexandre Belloni   rtc: rv8803: conv...
1
  // SPDX-License-Identifier: GPL-2.0
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
2
3
4
5
  /*
   * RTC driver for the Micro Crystal RV8803
   *
   * Copyright (C) 2015 Micro Crystal SA
7d1e5bfe1   Alexandre Belloni   rtc: update my em...
6
   * Alexandre Belloni <alexandre.belloni@bootlin.com>
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
7
   *
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
8
9
10
11
   */
  
  #include <linux/bcd.h>
  #include <linux/bitops.h>
a1e98e097   Benoît Thébaudeau   rtc: rv8803: Fix ...
12
  #include <linux/log2.h>
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
13
14
15
16
  #include <linux/i2c.h>
  #include <linux/interrupt.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
740ad8f43   Javier Martinez Canillas   rtc: rv8803: Add ...
17
  #include <linux/of_device.h>
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
18
  #include <linux/rtc.h>
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
19
  #define RV8803_I2C_TRY_COUNT		4
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #define RV8803_SEC			0x00
  #define RV8803_MIN			0x01
  #define RV8803_HOUR			0x02
  #define RV8803_WEEK			0x03
  #define RV8803_DAY			0x04
  #define RV8803_MONTH			0x05
  #define RV8803_YEAR			0x06
  #define RV8803_RAM			0x07
  #define RV8803_ALARM_MIN		0x08
  #define RV8803_ALARM_HOUR		0x09
  #define RV8803_ALARM_WEEK_OR_DAY	0x0A
  #define RV8803_EXT			0x0D
  #define RV8803_FLAG			0x0E
  #define RV8803_CTRL			0x0F
  
  #define RV8803_EXT_WADA			BIT(6)
  
  #define RV8803_FLAG_V1F			BIT(0)
  #define RV8803_FLAG_V2F			BIT(1)
  #define RV8803_FLAG_AF			BIT(3)
  #define RV8803_FLAG_TF			BIT(4)
  #define RV8803_FLAG_UF			BIT(5)
  
  #define RV8803_CTRL_RESET		BIT(0)
  
  #define RV8803_CTRL_EIE			BIT(2)
  #define RV8803_CTRL_AIE			BIT(3)
  #define RV8803_CTRL_TIE			BIT(4)
  #define RV8803_CTRL_UIE			BIT(5)
1cd713762   Oleksij Rempel   rtc: rv8803: set ...
49
50
51
52
53
54
55
56
  #define RX8900_BACKUP_CTRL		0x18
  #define RX8900_FLAG_SWOFF		BIT(2)
  #define RX8900_FLAG_VDETOFF		BIT(3)
  
  enum rv8803_type {
  	rv_8803,
  	rx_8900
  };
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
57
58
59
  struct rv8803_data {
  	struct i2c_client *client;
  	struct rtc_device *rtc;
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
60
  	struct mutex flags_lock;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
61
  	u8 ctrl;
1cd713762   Oleksij Rempel   rtc: rv8803: set ...
62
  	enum rv8803_type type;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
63
  };
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  static int rv8803_read_reg(const struct i2c_client *client, u8 reg)
  {
  	int try = RV8803_I2C_TRY_COUNT;
  	s32 ret;
  
  	/*
  	 * There is a 61µs window during which the RTC does not acknowledge I2C
  	 * transfers. In that case, ensure that there are multiple attempts.
  	 */
  	do
  		ret = i2c_smbus_read_byte_data(client, reg);
  	while ((ret == -ENXIO || ret == -EIO) && --try);
  	if (ret < 0)
  		dev_err(&client->dev, "Unable to read register 0x%02x
  ", reg);
  
  	return ret;
  }
  
  static int rv8803_read_regs(const struct i2c_client *client,
  			    u8 reg, u8 count, u8 *values)
  {
  	int try = RV8803_I2C_TRY_COUNT;
  	s32 ret;
  
  	do
  		ret = i2c_smbus_read_i2c_block_data(client, reg, count, values);
  	while ((ret == -ENXIO || ret == -EIO) && --try);
  	if (ret != count) {
  		dev_err(&client->dev,
  			"Unable to read registers 0x%02x..0x%02x
  ",
  			reg, reg + count - 1);
  		return ret < 0 ? ret : -EIO;
  	}
  
  	return 0;
  }
  
  static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value)
  {
  	int try = RV8803_I2C_TRY_COUNT;
  	s32 ret;
  
  	do
  		ret = i2c_smbus_write_byte_data(client, reg, value);
  	while ((ret == -ENXIO || ret == -EIO) && --try);
  	if (ret)
  		dev_err(&client->dev, "Unable to write register 0x%02x
  ", reg);
  
  	return ret;
  }
  
  static int rv8803_write_regs(const struct i2c_client *client,
  			     u8 reg, u8 count, const u8 *values)
  {
  	int try = RV8803_I2C_TRY_COUNT;
  	s32 ret;
  
  	do
  		ret = i2c_smbus_write_i2c_block_data(client, reg, count,
  						     values);
  	while ((ret == -ENXIO || ret == -EIO) && --try);
  	if (ret)
  		dev_err(&client->dev,
  			"Unable to write registers 0x%02x..0x%02x
  ",
  			reg, reg + count - 1);
  
  	return ret;
  }
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
136
137
138
139
140
  static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
  {
  	struct i2c_client *client = dev_id;
  	struct rv8803_data *rv8803 = i2c_get_clientdata(client);
  	unsigned long events = 0;
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
141
  	int flags;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
142

9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
143
  	mutex_lock(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
144

d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
145
  	flags = rv8803_read_reg(client, RV8803_FLAG);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
146
  	if (flags <= 0) {
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
147
  		mutex_unlock(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  		return IRQ_NONE;
  	}
  
  	if (flags & RV8803_FLAG_V1F)
  		dev_warn(&client->dev, "Voltage low, temperature compensation stopped.
  ");
  
  	if (flags & RV8803_FLAG_V2F)
  		dev_warn(&client->dev, "Voltage low, data loss detected.
  ");
  
  	if (flags & RV8803_FLAG_TF) {
  		flags &= ~RV8803_FLAG_TF;
  		rv8803->ctrl &= ~RV8803_CTRL_TIE;
  		events |= RTC_PF;
  	}
  
  	if (flags & RV8803_FLAG_AF) {
  		flags &= ~RV8803_FLAG_AF;
  		rv8803->ctrl &= ~RV8803_CTRL_AIE;
  		events |= RTC_AF;
  	}
  
  	if (flags & RV8803_FLAG_UF) {
  		flags &= ~RV8803_FLAG_UF;
  		rv8803->ctrl &= ~RV8803_CTRL_UIE;
  		events |= RTC_UF;
  	}
  
  	if (events) {
  		rtc_update_irq(rv8803->rtc, 1, events);
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
179
180
  		rv8803_write_reg(client, RV8803_FLAG, flags);
  		rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
181
  	}
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
182
  	mutex_unlock(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
183
184
185
186
187
188
189
190
191
192
193
  
  	return IRQ_HANDLED;
  }
  
  static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
  {
  	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
  	u8 date1[7];
  	u8 date2[7];
  	u8 *date = date1;
  	int ret, flags;
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
194
  	flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
195
196
197
198
199
200
201
202
  	if (flags < 0)
  		return flags;
  
  	if (flags & RV8803_FLAG_V2F) {
  		dev_warn(dev, "Voltage low, data is invalid.
  ");
  		return -EINVAL;
  	}
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
203
204
205
  	ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date);
  	if (ret)
  		return ret;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
206
207
  
  	if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
208
209
210
  		ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2);
  		if (ret)
  			return ret;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
211
212
213
214
215
216
217
218
  
  		if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
  			date = date2;
  	}
  
  	tm->tm_sec  = bcd2bin(date[RV8803_SEC] & 0x7f);
  	tm->tm_min  = bcd2bin(date[RV8803_MIN] & 0x7f);
  	tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
a1e98e097   Benoît Thébaudeau   rtc: rv8803: Fix ...
219
  	tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
220
221
222
  	tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
  	tm->tm_mon  = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
  	tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
96acb25c5   Benoît Thébaudeau   rtc: rv8803: Remo...
223
  	return 0;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
224
225
226
227
228
229
  }
  
  static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
  {
  	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
  	u8 date[7];
d3700b6b6   Benoît Thébaudeau   rtc: rv8803: Stop...
230
  	int ctrl, flags, ret;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
231

d3700b6b6   Benoît Thébaudeau   rtc: rv8803: Stop...
232
233
234
235
236
237
238
239
240
  	ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL);
  	if (ctrl < 0)
  		return ctrl;
  
  	/* Stop the clock */
  	ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
  			       ctrl | RV8803_CTRL_RESET);
  	if (ret)
  		return ret;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
241
242
243
244
245
246
247
  	date[RV8803_SEC]   = bin2bcd(tm->tm_sec);
  	date[RV8803_MIN]   = bin2bcd(tm->tm_min);
  	date[RV8803_HOUR]  = bin2bcd(tm->tm_hour);
  	date[RV8803_WEEK]  = 1 << (tm->tm_wday);
  	date[RV8803_DAY]   = bin2bcd(tm->tm_mday);
  	date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
  	date[RV8803_YEAR]  = bin2bcd(tm->tm_year - 100);
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
248
249
  	ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date);
  	if (ret)
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
250
  		return ret;
d3700b6b6   Benoît Thébaudeau   rtc: rv8803: Stop...
251
252
253
254
255
  	/* Restart the clock */
  	ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
  			       ctrl & ~RV8803_CTRL_RESET);
  	if (ret)
  		return ret;
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
256
  	mutex_lock(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
257

d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
258
  	flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
259
  	if (flags < 0) {
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
260
  		mutex_unlock(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
261
262
  		return flags;
  	}
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
263
  	ret = rv8803_write_reg(rv8803->client, RV8803_FLAG,
6f367788d   Benoît Thébaudeau   rtc: rv8803: Clea...
264
  			       flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F));
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
265

9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
266
  	mutex_unlock(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
267
268
269
270
271
272
273
274
275
276
  
  	return ret;
  }
  
  static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  {
  	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
  	struct i2c_client *client = rv8803->client;
  	u8 alarmvals[3];
  	int flags, ret;
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
277
278
279
  	ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals);
  	if (ret)
  		return ret;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
280

d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
281
  	flags = rv8803_read_reg(client, RV8803_FLAG);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
282
283
284
285
286
287
  	if (flags < 0)
  		return flags;
  
  	alrm->time.tm_sec  = 0;
  	alrm->time.tm_min  = bcd2bin(alarmvals[0] & 0x7f);
  	alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
288
  	alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
289
290
291
292
293
294
295
296
297
298
299
300
301
302
  
  	alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
  	alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
  
  	return 0;
  }
  
  static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
  	u8 alarmvals[3];
  	u8 ctrl[2];
  	int ret, err;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
303
304
305
306
307
308
309
310
  
  	/* The alarm has no seconds, round up to nearest minute */
  	if (alrm->time.tm_sec) {
  		time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
  
  		alarm_time += 60 - alrm->time.tm_sec;
  		rtc_time64_to_tm(alarm_time, &alrm->time);
  	}
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
311
  	mutex_lock(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
312

d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
313
314
  	ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl);
  	if (ret) {
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
315
  		mutex_unlock(&rv8803->flags_lock);
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
316
  		return ret;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
317
318
319
320
321
322
323
324
  	}
  
  	alarmvals[0] = bin2bcd(alrm->time.tm_min);
  	alarmvals[1] = bin2bcd(alrm->time.tm_hour);
  	alarmvals[2] = bin2bcd(alrm->time.tm_mday);
  
  	if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
  		rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
325
326
  		err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
  				       rv8803->ctrl);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
327
  		if (err) {
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
328
  			mutex_unlock(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
329
330
331
332
333
  			return err;
  		}
  	}
  
  	ctrl[1] &= ~RV8803_FLAG_AF;
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
334
  	err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[1]);
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
335
  	mutex_unlock(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
336
337
  	if (err)
  		return err;
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
338
  	err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
339
340
341
342
343
344
345
346
  	if (err)
  		return err;
  
  	if (alrm->enabled) {
  		if (rv8803->rtc->uie_rtctimer.enabled)
  			rv8803->ctrl |= RV8803_CTRL_UIE;
  		if (rv8803->rtc->aie_timer.enabled)
  			rv8803->ctrl |= RV8803_CTRL_AIE;
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
347
348
  		err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
  				       rv8803->ctrl);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
349
350
351
352
353
354
355
356
357
358
359
360
  		if (err)
  			return err;
  	}
  
  	return 0;
  }
  
  static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
  	int ctrl, flags, err;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  
  	ctrl = rv8803->ctrl;
  
  	if (enabled) {
  		if (rv8803->rtc->uie_rtctimer.enabled)
  			ctrl |= RV8803_CTRL_UIE;
  		if (rv8803->rtc->aie_timer.enabled)
  			ctrl |= RV8803_CTRL_AIE;
  	} else {
  		if (!rv8803->rtc->uie_rtctimer.enabled)
  			ctrl &= ~RV8803_CTRL_UIE;
  		if (!rv8803->rtc->aie_timer.enabled)
  			ctrl &= ~RV8803_CTRL_AIE;
  	}
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
375
  	mutex_lock(&rv8803->flags_lock);
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
376
  	flags = rv8803_read_reg(client, RV8803_FLAG);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
377
  	if (flags < 0) {
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
378
  		mutex_unlock(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
379
380
381
  		return flags;
  	}
  	flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
382
  	err = rv8803_write_reg(client, RV8803_FLAG, flags);
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
383
  	mutex_unlock(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
384
385
386
387
388
  	if (err)
  		return err;
  
  	if (ctrl != rv8803->ctrl) {
  		rv8803->ctrl = ctrl;
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
389
  		err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
390
391
392
393
394
395
396
397
398
399
400
401
  		if (err)
  			return err;
  	}
  
  	return 0;
  }
  
  static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
  	int flags, ret = 0;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
402
403
404
  
  	switch (cmd) {
  	case RTC_VL_READ:
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
405
  		flags = rv8803_read_reg(client, RV8803_FLAG);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
  		if (flags < 0)
  			return flags;
  
  		if (flags & RV8803_FLAG_V1F)
  			dev_warn(&client->dev, "Voltage low, temperature compensation stopped.
  ");
  
  		if (flags & RV8803_FLAG_V2F)
  			dev_warn(&client->dev, "Voltage low, data loss detected.
  ");
  
  		flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F;
  
  		if (copy_to_user((void __user *)arg, &flags, sizeof(int)))
  			return -EFAULT;
  
  		return 0;
  
  	case RTC_VL_CLR:
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
425
  		mutex_lock(&rv8803->flags_lock);
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
426
  		flags = rv8803_read_reg(client, RV8803_FLAG);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
427
  		if (flags < 0) {
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
428
  			mutex_unlock(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
429
430
431
432
  			return flags;
  		}
  
  		flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F);
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
433
  		ret = rv8803_write_reg(client, RV8803_FLAG, flags);
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
434
  		mutex_unlock(&rv8803->flags_lock);
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
435
  		if (ret)
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
436
437
438
439
440
441
442
443
  			return ret;
  
  		return 0;
  
  	default:
  		return -ENOIOCTLCMD;
  	}
  }
16d70a78b   Alexandre Belloni   rtc: rv8803: use ...
444
445
  static int rv8803_nvram_write(void *priv, unsigned int offset, void *val,
  			      size_t bytes)
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
446
  {
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
447
  	int ret;
16d70a78b   Alexandre Belloni   rtc: rv8803: use ...
448
  	ret = rv8803_write_reg(priv, RV8803_RAM, *(u8 *)val);
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
449
  	if (ret)
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
450
  		return ret;
16d70a78b   Alexandre Belloni   rtc: rv8803: use ...
451
  	return 0;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
452
  }
16d70a78b   Alexandre Belloni   rtc: rv8803: use ...
453
454
  static int rv8803_nvram_read(void *priv, unsigned int offset,
  			     void *val, size_t bytes)
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
455
  {
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
456
  	int ret;
16d70a78b   Alexandre Belloni   rtc: rv8803: use ...
457
  	ret = rv8803_read_reg(priv, RV8803_RAM);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
458
459
  	if (ret < 0)
  		return ret;
16d70a78b   Alexandre Belloni   rtc: rv8803: use ...
460
  	*(u8 *)val = ret;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
461

16d70a78b   Alexandre Belloni   rtc: rv8803: use ...
462
  	return 0;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
463
  }
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
464
465
466
467
468
  static struct rtc_class_ops rv8803_rtc_ops = {
  	.read_time = rv8803_get_time,
  	.set_time = rv8803_set_time,
  	.ioctl = rv8803_ioctl,
  };
1cd713762   Oleksij Rempel   rtc: rv8803: set ...
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  static int rx8900_trickle_charger_init(struct rv8803_data *rv8803)
  {
  	struct i2c_client *client = rv8803->client;
  	struct device_node *node = client->dev.of_node;
  	int err;
  	u8 flags;
  
  	if (!node)
  		return 0;
  
  	if (rv8803->type != rx_8900)
  		return 0;
  
  	err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL);
  	if (err < 0)
  		return err;
  
  	flags = ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF) & (u8)err;
  
  	if (of_property_read_bool(node, "epson,vdet-disable"))
  		flags |= RX8900_FLAG_VDETOFF;
  
  	if (of_property_read_bool(node, "trickle-diode-disable"))
  		flags |= RX8900_FLAG_SWOFF;
  
  	return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL,
  					 flags);
  }
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
497
498
499
  static int rv8803_probe(struct i2c_client *client,
  			const struct i2c_device_id *id)
  {
5cb172694   Wolfram Sang   rtc: rv8803: simp...
500
  	struct i2c_adapter *adapter = client->adapter;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
501
  	struct rv8803_data *rv8803;
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
502
  	int err, flags;
c07fd9dea   Alexandre Belloni   rtc: rv8803: put ...
503
504
505
506
507
508
509
510
511
  	struct nvmem_config nvmem_cfg = {
  		.name = "rv8803_nvram",
  		.word_size = 1,
  		.stride = 1,
  		.size = 1,
  		.reg_read = rv8803_nvram_read,
  		.reg_write = rv8803_nvram_write,
  		.priv = client,
  	};
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
512
513
514
515
516
517
518
519
520
521
522
523
  
  	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  				     I2C_FUNC_SMBUS_I2C_BLOCK)) {
  		dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK
  ");
  		return -EIO;
  	}
  
  	rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
  			      GFP_KERNEL);
  	if (!rv8803)
  		return -ENOMEM;
9d1fa4c37   Oleksij Rempel   rtc: rv8803: conv...
524
  	mutex_init(&rv8803->flags_lock);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
525
  	rv8803->client = client;
740ad8f43   Javier Martinez Canillas   rtc: rv8803: Add ...
526
527
528
529
530
  	if (client->dev.of_node)
  		rv8803->type = (enum rv8803_type)
  			of_device_get_match_data(&client->dev);
  	else
  		rv8803->type = id->driver_data;
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
531
  	i2c_set_clientdata(client, rv8803);
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
532
  	flags = rv8803_read_reg(client, RV8803_FLAG);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
533
534
535
536
537
538
539
540
541
542
543
544
545
546
  	if (flags < 0)
  		return flags;
  
  	if (flags & RV8803_FLAG_V1F)
  		dev_warn(&client->dev, "Voltage low, temperature compensation stopped.
  ");
  
  	if (flags & RV8803_FLAG_V2F)
  		dev_warn(&client->dev, "Voltage low, data loss detected.
  ");
  
  	if (flags & RV8803_FLAG_AF)
  		dev_warn(&client->dev, "An alarm maybe have been missed.
  ");
7133eca19   Alexandre Belloni   rtc: rv8803: swit...
547
  	rv8803->rtc = devm_rtc_allocate_device(&client->dev);
44c638ce4   Alexandre Belloni   rtc: remove super...
548
  	if (IS_ERR(rv8803->rtc))
7133eca19   Alexandre Belloni   rtc: rv8803: swit...
549
  		return PTR_ERR(rv8803->rtc);
7133eca19   Alexandre Belloni   rtc: rv8803: swit...
550

1e3929ef0   Alexandre Belloni   rtc: Add a driver...
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
  	if (client->irq > 0) {
  		err = devm_request_threaded_irq(&client->dev, client->irq,
  						NULL, rv8803_handle_irq,
  						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  						"rv8803", client);
  		if (err) {
  			dev_warn(&client->dev, "unable to request IRQ, alarms disabled
  ");
  			client->irq = 0;
  		} else {
  			rv8803_rtc_ops.read_alarm = rv8803_get_alarm;
  			rv8803_rtc_ops.set_alarm = rv8803_set_alarm;
  			rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable;
  		}
  	}
d522649e2   Benoît Thébaudeau   rtc: rv8803: Alwa...
566
  	err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
567
568
  	if (err)
  		return err;
1cd713762   Oleksij Rempel   rtc: rv8803: set ...
569
570
571
572
573
574
  	err = rx8900_trickle_charger_init(rv8803);
  	if (err) {
  		dev_err(&client->dev, "failed to init charger
  ");
  		return err;
  	}
ce1ae8eb8   Alexandre Belloni   rtc: rv8803: fix ...
575
576
  	rv8803->rtc->ops = &rv8803_rtc_ops;
  	rv8803->rtc->nvram_old_abi = true;
2e17f8b98   Alexandre Belloni   rtc: rv8803: let ...
577
578
  	rv8803->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
  	rv8803->rtc->range_max = RTC_TIMESTAMP_END_2099;
ce1ae8eb8   Alexandre Belloni   rtc: rv8803: fix ...
579
580
581
582
583
  	err = rtc_register_device(rv8803->rtc);
  	if (err)
  		return err;
  
  	rtc_nvmem_register(rv8803->rtc, &nvmem_cfg);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
584
585
586
587
  	rv8803->rtc->max_user_freq = 1;
  
  	return 0;
  }
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
588
  static const struct i2c_device_id rv8803_id[] = {
1cd713762   Oleksij Rempel   rtc: rv8803: set ...
589
  	{ "rv8803", rv_8803 },
ac771ed74   Alexandre Belloni   rtc: rv8803: add ...
590
  	{ "rx8803", rv_8803 },
1cd713762   Oleksij Rempel   rtc: rv8803: set ...
591
  	{ "rx8900", rx_8900 },
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
592
593
594
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, rv8803_id);
740ad8f43   Javier Martinez Canillas   rtc: rv8803: Add ...
595
596
597
  static const struct of_device_id rv8803_of_match[] = {
  	{
  		.compatible = "microcrystal,rv8803",
c856618d2   Alexandre Belloni   rtc: rv8803: fix ...
598
  		.data = (void *)rv_8803
740ad8f43   Javier Martinez Canillas   rtc: rv8803: Add ...
599
600
  	},
  	{
ac771ed74   Alexandre Belloni   rtc: rv8803: add ...
601
602
603
604
  		.compatible = "epson,rx8803",
  		.data = (void *)rv_8803
  	},
  	{
740ad8f43   Javier Martinez Canillas   rtc: rv8803: Add ...
605
606
607
608
609
610
  		.compatible = "epson,rx8900",
  		.data = (void *)rx_8900
  	},
  	{ }
  };
  MODULE_DEVICE_TABLE(of, rv8803_of_match);
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
611
612
613
  static struct i2c_driver rv8803_driver = {
  	.driver = {
  		.name = "rtc-rv8803",
740ad8f43   Javier Martinez Canillas   rtc: rv8803: Add ...
614
  		.of_match_table = of_match_ptr(rv8803_of_match),
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
615
616
  	},
  	.probe		= rv8803_probe,
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
617
618
619
  	.id_table	= rv8803_id,
  };
  module_i2c_driver(rv8803_driver);
7d1e5bfe1   Alexandre Belloni   rtc: update my em...
620
  MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
1e3929ef0   Alexandre Belloni   rtc: Add a driver...
621
622
  MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
  MODULE_LICENSE("GPL v2");