xref: /openbmc/linux/drivers/rtc/rtc-rv8803.c (revision 0edbfea5)
1 /*
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>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/rtc.h>
21 
22 #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 
52 struct rv8803_data {
53 	struct i2c_client *client;
54 	struct rtc_device *rtc;
55 	struct mutex flags_lock;
56 	u8 ctrl;
57 };
58 
59 static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
60 {
61 	struct i2c_client *client = dev_id;
62 	struct rv8803_data *rv8803 = i2c_get_clientdata(client);
63 	unsigned long events = 0;
64 	int flags, try = 0;
65 
66 	mutex_lock(&rv8803->flags_lock);
67 
68 	do {
69 		flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
70 		try++;
71 	} while ((flags == -ENXIO) && (try < 3));
72 	if (flags <= 0) {
73 		mutex_unlock(&rv8803->flags_lock);
74 		return IRQ_NONE;
75 	}
76 
77 	if (flags & RV8803_FLAG_V1F)
78 		dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
79 
80 	if (flags & RV8803_FLAG_V2F)
81 		dev_warn(&client->dev, "Voltage low, data loss detected.\n");
82 
83 	if (flags & RV8803_FLAG_TF) {
84 		flags &= ~RV8803_FLAG_TF;
85 		rv8803->ctrl &= ~RV8803_CTRL_TIE;
86 		events |= RTC_PF;
87 	}
88 
89 	if (flags & RV8803_FLAG_AF) {
90 		flags &= ~RV8803_FLAG_AF;
91 		rv8803->ctrl &= ~RV8803_CTRL_AIE;
92 		events |= RTC_AF;
93 	}
94 
95 	if (flags & RV8803_FLAG_UF) {
96 		flags &= ~RV8803_FLAG_UF;
97 		rv8803->ctrl &= ~RV8803_CTRL_UIE;
98 		events |= RTC_UF;
99 	}
100 
101 	if (events) {
102 		rtc_update_irq(rv8803->rtc, 1, events);
103 		i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
104 		i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
105 					  rv8803->ctrl);
106 	}
107 
108 	mutex_unlock(&rv8803->flags_lock);
109 
110 	return IRQ_HANDLED;
111 }
112 
113 static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
114 {
115 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
116 	u8 date1[7];
117 	u8 date2[7];
118 	u8 *date = date1;
119 	int ret, flags;
120 
121 	flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG);
122 	if (flags < 0)
123 		return flags;
124 
125 	if (flags & RV8803_FLAG_V2F) {
126 		dev_warn(dev, "Voltage low, data is invalid.\n");
127 		return -EINVAL;
128 	}
129 
130 	ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC,
131 					    7, date);
132 	if (ret != 7)
133 		return ret < 0 ? ret : -EIO;
134 
135 	if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
136 		ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC,
137 						    7, date2);
138 		if (ret != 7)
139 			return ret < 0 ? ret : -EIO;
140 
141 		if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
142 			date = date2;
143 	}
144 
145 	tm->tm_sec  = bcd2bin(date[RV8803_SEC] & 0x7f);
146 	tm->tm_min  = bcd2bin(date[RV8803_MIN] & 0x7f);
147 	tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
148 	tm->tm_wday = ffs(date[RV8803_WEEK] & 0x7f);
149 	tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
150 	tm->tm_mon  = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
151 	tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
152 
153 	return rtc_valid_tm(tm);
154 }
155 
156 static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
157 {
158 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
159 	u8 date[7];
160 	int flags, ret;
161 
162 	if ((tm->tm_year < 100) || (tm->tm_year > 199))
163 		return -EINVAL;
164 
165 	date[RV8803_SEC]   = bin2bcd(tm->tm_sec);
166 	date[RV8803_MIN]   = bin2bcd(tm->tm_min);
167 	date[RV8803_HOUR]  = bin2bcd(tm->tm_hour);
168 	date[RV8803_WEEK]  = 1 << (tm->tm_wday);
169 	date[RV8803_DAY]   = bin2bcd(tm->tm_mday);
170 	date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
171 	date[RV8803_YEAR]  = bin2bcd(tm->tm_year - 100);
172 
173 	ret = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_SEC,
174 					     7, date);
175 	if (ret < 0)
176 		return ret;
177 
178 	mutex_lock(&rv8803->flags_lock);
179 
180 	flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG);
181 	if (flags < 0) {
182 		mutex_unlock(&rv8803->flags_lock);
183 		return flags;
184 	}
185 
186 	ret = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG,
187 					flags & ~RV8803_FLAG_V2F);
188 
189 	mutex_unlock(&rv8803->flags_lock);
190 
191 	return ret;
192 }
193 
194 static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
195 {
196 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
197 	struct i2c_client *client = rv8803->client;
198 	u8 alarmvals[3];
199 	int flags, ret;
200 
201 	ret = i2c_smbus_read_i2c_block_data(client, RV8803_ALARM_MIN,
202 					    3, alarmvals);
203 	if (ret != 3)
204 		return ret < 0 ? ret : -EIO;
205 
206 	flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
207 	if (flags < 0)
208 		return flags;
209 
210 	alrm->time.tm_sec  = 0;
211 	alrm->time.tm_min  = bcd2bin(alarmvals[0] & 0x7f);
212 	alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
213 	alrm->time.tm_wday = -1;
214 	alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
215 	alrm->time.tm_mon  = -1;
216 	alrm->time.tm_year = -1;
217 
218 	alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
219 	alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
220 
221 	return 0;
222 }
223 
224 static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
225 {
226 	struct i2c_client *client = to_i2c_client(dev);
227 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
228 	u8 alarmvals[3];
229 	u8 ctrl[2];
230 	int ret, err;
231 
232 	/* The alarm has no seconds, round up to nearest minute */
233 	if (alrm->time.tm_sec) {
234 		time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
235 
236 		alarm_time += 60 - alrm->time.tm_sec;
237 		rtc_time64_to_tm(alarm_time, &alrm->time);
238 	}
239 
240 	mutex_lock(&rv8803->flags_lock);
241 
242 	ret = i2c_smbus_read_i2c_block_data(client, RV8803_FLAG, 2, ctrl);
243 	if (ret != 2) {
244 		mutex_unlock(&rv8803->flags_lock);
245 		return ret < 0 ? ret : -EIO;
246 	}
247 
248 	alarmvals[0] = bin2bcd(alrm->time.tm_min);
249 	alarmvals[1] = bin2bcd(alrm->time.tm_hour);
250 	alarmvals[2] = bin2bcd(alrm->time.tm_mday);
251 
252 	if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
253 		rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
254 		err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
255 						rv8803->ctrl);
256 		if (err) {
257 			mutex_unlock(&rv8803->flags_lock);
258 			return err;
259 		}
260 	}
261 
262 	ctrl[1] &= ~RV8803_FLAG_AF;
263 	err = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG, ctrl[1]);
264 	mutex_unlock(&rv8803->flags_lock);
265 	if (err)
266 		return err;
267 
268 	err = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_ALARM_MIN,
269 					     3, alarmvals);
270 	if (err)
271 		return err;
272 
273 	if (alrm->enabled) {
274 		if (rv8803->rtc->uie_rtctimer.enabled)
275 			rv8803->ctrl |= RV8803_CTRL_UIE;
276 		if (rv8803->rtc->aie_timer.enabled)
277 			rv8803->ctrl |= RV8803_CTRL_AIE;
278 
279 		err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
280 						rv8803->ctrl);
281 		if (err)
282 			return err;
283 	}
284 
285 	return 0;
286 }
287 
288 static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
289 {
290 	struct i2c_client *client = to_i2c_client(dev);
291 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
292 	int ctrl, flags, err;
293 
294 	ctrl = rv8803->ctrl;
295 
296 	if (enabled) {
297 		if (rv8803->rtc->uie_rtctimer.enabled)
298 			ctrl |= RV8803_CTRL_UIE;
299 		if (rv8803->rtc->aie_timer.enabled)
300 			ctrl |= RV8803_CTRL_AIE;
301 	} else {
302 		if (!rv8803->rtc->uie_rtctimer.enabled)
303 			ctrl &= ~RV8803_CTRL_UIE;
304 		if (!rv8803->rtc->aie_timer.enabled)
305 			ctrl &= ~RV8803_CTRL_AIE;
306 	}
307 
308 	mutex_lock(&rv8803->flags_lock);
309 	flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
310 	if (flags < 0) {
311 		mutex_unlock(&rv8803->flags_lock);
312 		return flags;
313 	}
314 	flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
315 	err = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
316 	mutex_unlock(&rv8803->flags_lock);
317 	if (err)
318 		return err;
319 
320 	if (ctrl != rv8803->ctrl) {
321 		rv8803->ctrl = ctrl;
322 		err = i2c_smbus_write_byte_data(client, RV8803_CTRL,
323 						rv8803->ctrl);
324 		if (err)
325 			return err;
326 	}
327 
328 	return 0;
329 }
330 
331 static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
332 {
333 	struct i2c_client *client = to_i2c_client(dev);
334 	struct rv8803_data *rv8803 = dev_get_drvdata(dev);
335 	int flags, ret = 0;
336 
337 	switch (cmd) {
338 	case RTC_VL_READ:
339 		flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
340 		if (flags < 0)
341 			return flags;
342 
343 		if (flags & RV8803_FLAG_V1F)
344 			dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
345 
346 		if (flags & RV8803_FLAG_V2F)
347 			dev_warn(&client->dev, "Voltage low, data loss detected.\n");
348 
349 		flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F;
350 
351 		if (copy_to_user((void __user *)arg, &flags, sizeof(int)))
352 			return -EFAULT;
353 
354 		return 0;
355 
356 	case RTC_VL_CLR:
357 		mutex_lock(&rv8803->flags_lock);
358 		flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
359 		if (flags < 0) {
360 			mutex_unlock(&rv8803->flags_lock);
361 			return flags;
362 		}
363 
364 		flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F);
365 		ret = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
366 		mutex_unlock(&rv8803->flags_lock);
367 		if (ret < 0)
368 			return ret;
369 
370 		return 0;
371 
372 	default:
373 		return -ENOIOCTLCMD;
374 	}
375 }
376 
377 static ssize_t rv8803_nvram_write(struct file *filp, struct kobject *kobj,
378 				  struct bin_attribute *attr,
379 				  char *buf, loff_t off, size_t count)
380 {
381 	struct device *dev = kobj_to_dev(kobj);
382 	struct i2c_client *client = to_i2c_client(dev);
383 	int ret;
384 
385 	ret = i2c_smbus_write_byte_data(client, RV8803_RAM, buf[0]);
386 	if (ret < 0)
387 		return ret;
388 
389 	return 1;
390 }
391 
392 static ssize_t rv8803_nvram_read(struct file *filp, struct kobject *kobj,
393 				 struct bin_attribute *attr,
394 				 char *buf, loff_t off, size_t count)
395 {
396 	struct device *dev = kobj_to_dev(kobj);
397 	struct i2c_client *client = to_i2c_client(dev);
398 	int ret;
399 
400 	ret = i2c_smbus_read_byte_data(client, RV8803_RAM);
401 	if (ret < 0)
402 		return ret;
403 
404 	buf[0] = ret;
405 
406 	return 1;
407 }
408 
409 static struct bin_attribute rv8803_nvram_attr = {
410 	.attr = {
411 		.name = "nvram",
412 		.mode = S_IRUGO | S_IWUSR,
413 	},
414 	.size = 1,
415 	.read = rv8803_nvram_read,
416 	.write = rv8803_nvram_write,
417 };
418 
419 static struct rtc_class_ops rv8803_rtc_ops = {
420 	.read_time = rv8803_get_time,
421 	.set_time = rv8803_set_time,
422 	.ioctl = rv8803_ioctl,
423 };
424 
425 static int rv8803_probe(struct i2c_client *client,
426 			const struct i2c_device_id *id)
427 {
428 	struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
429 	struct rv8803_data *rv8803;
430 	int err, flags, try = 0;
431 
432 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
433 				     I2C_FUNC_SMBUS_I2C_BLOCK)) {
434 		dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
435 		return -EIO;
436 	}
437 
438 	rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
439 			      GFP_KERNEL);
440 	if (!rv8803)
441 		return -ENOMEM;
442 
443 	mutex_init(&rv8803->flags_lock);
444 	rv8803->client = client;
445 	i2c_set_clientdata(client, rv8803);
446 
447 	/*
448 	 * There is a 60µs window where the RTC may not reply on the i2c bus in
449 	 * that case, the transfer is not ACKed. In that case, ensure there are
450 	 * multiple attempts.
451 	 */
452 	do {
453 		flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
454 		try++;
455 	} while ((flags == -ENXIO) && (try < 3));
456 
457 	if (flags < 0)
458 		return flags;
459 
460 	if (flags & RV8803_FLAG_V1F)
461 		dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
462 
463 	if (flags & RV8803_FLAG_V2F)
464 		dev_warn(&client->dev, "Voltage low, data loss detected.\n");
465 
466 	if (flags & RV8803_FLAG_AF)
467 		dev_warn(&client->dev, "An alarm maybe have been missed.\n");
468 
469 	if (client->irq > 0) {
470 		err = devm_request_threaded_irq(&client->dev, client->irq,
471 						NULL, rv8803_handle_irq,
472 						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
473 						"rv8803", client);
474 		if (err) {
475 			dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
476 			client->irq = 0;
477 		} else {
478 			rv8803_rtc_ops.read_alarm = rv8803_get_alarm;
479 			rv8803_rtc_ops.set_alarm = rv8803_set_alarm;
480 			rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable;
481 		}
482 	}
483 
484 	rv8803->rtc = devm_rtc_device_register(&client->dev, client->name,
485 					       &rv8803_rtc_ops, THIS_MODULE);
486 	if (IS_ERR(rv8803->rtc)) {
487 		dev_err(&client->dev, "unable to register the class device\n");
488 		return PTR_ERR(rv8803->rtc);
489 	}
490 
491 	try = 0;
492 	do {
493 		err = i2c_smbus_write_byte_data(rv8803->client, RV8803_EXT,
494 						RV8803_EXT_WADA);
495 		try++;
496 	} while ((err == -ENXIO) && (try < 3));
497 	if (err)
498 		return err;
499 
500 	err = device_create_bin_file(&client->dev, &rv8803_nvram_attr);
501 	if (err)
502 		return err;
503 
504 	rv8803->rtc->max_user_freq = 1;
505 
506 	return 0;
507 }
508 
509 static int rv8803_remove(struct i2c_client *client)
510 {
511 	device_remove_bin_file(&client->dev, &rv8803_nvram_attr);
512 
513 	return 0;
514 }
515 
516 static const struct i2c_device_id rv8803_id[] = {
517 	{ "rv8803", 0 },
518 	{ "rx8900", 0 },
519 	{ }
520 };
521 MODULE_DEVICE_TABLE(i2c, rv8803_id);
522 
523 static struct i2c_driver rv8803_driver = {
524 	.driver = {
525 		.name = "rtc-rv8803",
526 	},
527 	.probe		= rv8803_probe,
528 	.remove		= rv8803_remove,
529 	.id_table	= rv8803_id,
530 };
531 module_i2c_driver(rv8803_driver);
532 
533 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
534 MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
535 MODULE_LICENSE("GPL v2");
536