xref: /openbmc/linux/drivers/rtc/rtc-rx8010.c (revision feac8c8b)
1 /*
2  * Driver for the Epson RTC module RX-8010 SJ
3  *
4  * Copyright(C) Timesys Corporation 2015
5  * Copyright(C) General Electric Company 2015
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  */
12 
13 #include <linux/bcd.h>
14 #include <linux/bitops.h>
15 #include <linux/i2c.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/rtc.h>
19 
20 #define RX8010_SEC     0x10
21 #define RX8010_MIN     0x11
22 #define RX8010_HOUR    0x12
23 #define RX8010_WDAY    0x13
24 #define RX8010_MDAY    0x14
25 #define RX8010_MONTH   0x15
26 #define RX8010_YEAR    0x16
27 #define RX8010_RESV17  0x17
28 #define RX8010_ALMIN   0x18
29 #define RX8010_ALHOUR  0x19
30 #define RX8010_ALWDAY  0x1A
31 #define RX8010_TCOUNT0 0x1B
32 #define RX8010_TCOUNT1 0x1C
33 #define RX8010_EXT     0x1D
34 #define RX8010_FLAG    0x1E
35 #define RX8010_CTRL    0x1F
36 /* 0x20 to 0x2F are user registers */
37 #define RX8010_RESV30  0x30
38 #define RX8010_RESV31  0x31
39 #define RX8010_IRQ     0x32
40 
41 #define RX8010_EXT_WADA  BIT(3)
42 
43 #define RX8010_FLAG_VLF  BIT(1)
44 #define RX8010_FLAG_AF   BIT(3)
45 #define RX8010_FLAG_TF   BIT(4)
46 #define RX8010_FLAG_UF   BIT(5)
47 
48 #define RX8010_CTRL_AIE  BIT(3)
49 #define RX8010_CTRL_UIE  BIT(5)
50 #define RX8010_CTRL_STOP BIT(6)
51 #define RX8010_CTRL_TEST BIT(7)
52 
53 #define RX8010_ALARM_AE  BIT(7)
54 
55 static const struct i2c_device_id rx8010_id[] = {
56 	{ "rx8010", 0 },
57 	{ }
58 };
59 MODULE_DEVICE_TABLE(i2c, rx8010_id);
60 
61 static const struct of_device_id rx8010_of_match[] = {
62 	{ .compatible = "epson,rx8010" },
63 	{ }
64 };
65 MODULE_DEVICE_TABLE(of, rx8010_of_match);
66 
67 struct rx8010_data {
68 	struct i2c_client *client;
69 	struct rtc_device *rtc;
70 	u8 ctrlreg;
71 };
72 
73 static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id)
74 {
75 	struct i2c_client *client = dev_id;
76 	struct rx8010_data *rx8010 = i2c_get_clientdata(client);
77 	int flagreg;
78 
79 	mutex_lock(&rx8010->rtc->ops_lock);
80 
81 	flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
82 
83 	if (flagreg <= 0) {
84 		mutex_unlock(&rx8010->rtc->ops_lock);
85 		return IRQ_NONE;
86 	}
87 
88 	if (flagreg & RX8010_FLAG_VLF)
89 		dev_warn(&client->dev, "Frequency stop detected\n");
90 
91 	if (flagreg & RX8010_FLAG_TF) {
92 		flagreg &= ~RX8010_FLAG_TF;
93 		rtc_update_irq(rx8010->rtc, 1, RTC_PF | RTC_IRQF);
94 	}
95 
96 	if (flagreg & RX8010_FLAG_AF) {
97 		flagreg &= ~RX8010_FLAG_AF;
98 		rtc_update_irq(rx8010->rtc, 1, RTC_AF | RTC_IRQF);
99 	}
100 
101 	if (flagreg & RX8010_FLAG_UF) {
102 		flagreg &= ~RX8010_FLAG_UF;
103 		rtc_update_irq(rx8010->rtc, 1, RTC_UF | RTC_IRQF);
104 	}
105 
106 	i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg);
107 
108 	mutex_unlock(&rx8010->rtc->ops_lock);
109 	return IRQ_HANDLED;
110 }
111 
112 static int rx8010_get_time(struct device *dev, struct rtc_time *dt)
113 {
114 	struct rx8010_data *rx8010 = dev_get_drvdata(dev);
115 	u8 date[7];
116 	int flagreg;
117 	int err;
118 
119 	flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
120 	if (flagreg < 0)
121 		return flagreg;
122 
123 	if (flagreg & RX8010_FLAG_VLF) {
124 		dev_warn(dev, "Frequency stop detected\n");
125 		return -EINVAL;
126 	}
127 
128 	err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_SEC,
129 					    7, date);
130 	if (err != 7)
131 		return err < 0 ? err : -EIO;
132 
133 	dt->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
134 	dt->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
135 	dt->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f);
136 	dt->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f);
137 	dt->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f) - 1;
138 	dt->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 100;
139 	dt->tm_wday = ffs(date[RX8010_WDAY - RX8010_SEC] & 0x7f);
140 
141 	return rtc_valid_tm(dt);
142 }
143 
144 static int rx8010_set_time(struct device *dev, struct rtc_time *dt)
145 {
146 	struct rx8010_data *rx8010 = dev_get_drvdata(dev);
147 	u8 date[7];
148 	int ctrl, flagreg;
149 	int ret;
150 
151 	if ((dt->tm_year < 100) || (dt->tm_year > 199))
152 		return -EINVAL;
153 
154 	/* set STOP bit before changing clock/calendar */
155 	ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL);
156 	if (ctrl < 0)
157 		return ctrl;
158 	rx8010->ctrlreg = ctrl | RX8010_CTRL_STOP;
159 	ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
160 					rx8010->ctrlreg);
161 	if (ret < 0)
162 		return ret;
163 
164 	date[RX8010_SEC - RX8010_SEC] = bin2bcd(dt->tm_sec);
165 	date[RX8010_MIN - RX8010_SEC] = bin2bcd(dt->tm_min);
166 	date[RX8010_HOUR - RX8010_SEC] = bin2bcd(dt->tm_hour);
167 	date[RX8010_MDAY - RX8010_SEC] = bin2bcd(dt->tm_mday);
168 	date[RX8010_MONTH - RX8010_SEC] = bin2bcd(dt->tm_mon + 1);
169 	date[RX8010_YEAR - RX8010_SEC] = bin2bcd(dt->tm_year - 100);
170 	date[RX8010_WDAY - RX8010_SEC] = bin2bcd(1 << dt->tm_wday);
171 
172 	ret = i2c_smbus_write_i2c_block_data(rx8010->client,
173 					     RX8010_SEC, 7, date);
174 	if (ret < 0)
175 		return ret;
176 
177 	/* clear STOP bit after changing clock/calendar */
178 	ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL);
179 	if (ctrl < 0)
180 		return ctrl;
181 	rx8010->ctrlreg = ctrl & ~RX8010_CTRL_STOP;
182 	ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
183 					rx8010->ctrlreg);
184 	if (ret < 0)
185 		return ret;
186 
187 	flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
188 	if (flagreg < 0) {
189 		return flagreg;
190 	}
191 
192 	if (flagreg & RX8010_FLAG_VLF)
193 		ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG,
194 						flagreg & ~RX8010_FLAG_VLF);
195 
196 	return 0;
197 }
198 
199 static int rx8010_init_client(struct i2c_client *client)
200 {
201 	struct rx8010_data *rx8010 = i2c_get_clientdata(client);
202 	u8 ctrl[2];
203 	int need_clear = 0, err = 0;
204 
205 	/* Initialize reserved registers as specified in datasheet */
206 	err = i2c_smbus_write_byte_data(client, RX8010_RESV17, 0xD8);
207 	if (err < 0)
208 		return err;
209 
210 	err = i2c_smbus_write_byte_data(client, RX8010_RESV30, 0x00);
211 	if (err < 0)
212 		return err;
213 
214 	err = i2c_smbus_write_byte_data(client, RX8010_RESV31, 0x08);
215 	if (err < 0)
216 		return err;
217 
218 	err = i2c_smbus_write_byte_data(client, RX8010_IRQ, 0x00);
219 	if (err < 0)
220 		return err;
221 
222 	err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_FLAG,
223 					    2, ctrl);
224 	if (err != 2)
225 		return err < 0 ? err : -EIO;
226 
227 	if (ctrl[0] & RX8010_FLAG_VLF)
228 		dev_warn(&client->dev, "Frequency stop was detected\n");
229 
230 	if (ctrl[0] & RX8010_FLAG_AF) {
231 		dev_warn(&client->dev, "Alarm was detected\n");
232 		need_clear = 1;
233 	}
234 
235 	if (ctrl[0] & RX8010_FLAG_TF)
236 		need_clear = 1;
237 
238 	if (ctrl[0] & RX8010_FLAG_UF)
239 		need_clear = 1;
240 
241 	if (need_clear) {
242 		ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);
243 		err = i2c_smbus_write_byte_data(client, RX8010_FLAG, ctrl[0]);
244 		if (err < 0)
245 			return err;
246 	}
247 
248 	rx8010->ctrlreg = (ctrl[1] & ~RX8010_CTRL_TEST);
249 
250 	return 0;
251 }
252 
253 static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t)
254 {
255 	struct rx8010_data *rx8010 = dev_get_drvdata(dev);
256 	struct i2c_client *client = rx8010->client;
257 	u8 alarmvals[3];
258 	int flagreg;
259 	int err;
260 
261 	err = i2c_smbus_read_i2c_block_data(client, RX8010_ALMIN, 3, alarmvals);
262 	if (err != 3)
263 		return err < 0 ? err : -EIO;
264 
265 	flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
266 	if (flagreg < 0)
267 		return flagreg;
268 
269 	t->time.tm_sec = 0;
270 	t->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
271 	t->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
272 
273 	if (!(alarmvals[2] & RX8010_ALARM_AE))
274 		t->time.tm_mday = bcd2bin(alarmvals[2] & 0x7f);
275 
276 	t->enabled = !!(rx8010->ctrlreg & RX8010_CTRL_AIE);
277 	t->pending = (flagreg & RX8010_FLAG_AF) && t->enabled;
278 
279 	return 0;
280 }
281 
282 static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t)
283 {
284 	struct i2c_client *client = to_i2c_client(dev);
285 	struct rx8010_data *rx8010 = dev_get_drvdata(dev);
286 	u8 alarmvals[3];
287 	int extreg, flagreg;
288 	int err;
289 
290 	flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
291 	if (flagreg < 0) {
292 		return flagreg;
293 	}
294 
295 	if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE)) {
296 		rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE);
297 		err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
298 						rx8010->ctrlreg);
299 		if (err < 0) {
300 			return err;
301 		}
302 	}
303 
304 	flagreg &= ~RX8010_FLAG_AF;
305 	err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
306 	if (err < 0)
307 		return err;
308 
309 	alarmvals[0] = bin2bcd(t->time.tm_min);
310 	alarmvals[1] = bin2bcd(t->time.tm_hour);
311 	alarmvals[2] = bin2bcd(t->time.tm_mday);
312 
313 	err = i2c_smbus_write_i2c_block_data(rx8010->client, RX8010_ALMIN,
314 					     2, alarmvals);
315 	if (err < 0)
316 		return err;
317 
318 	extreg = i2c_smbus_read_byte_data(client, RX8010_EXT);
319 	if (extreg < 0)
320 		return extreg;
321 
322 	extreg |= RX8010_EXT_WADA;
323 	err = i2c_smbus_write_byte_data(rx8010->client, RX8010_EXT, extreg);
324 	if (err < 0)
325 		return err;
326 
327 	if (alarmvals[2] == 0)
328 		alarmvals[2] |= RX8010_ALARM_AE;
329 
330 	err = i2c_smbus_write_byte_data(rx8010->client, RX8010_ALWDAY,
331 					alarmvals[2]);
332 	if (err < 0)
333 		return err;
334 
335 	if (t->enabled) {
336 		if (rx8010->rtc->uie_rtctimer.enabled)
337 			rx8010->ctrlreg |= RX8010_CTRL_UIE;
338 		if (rx8010->rtc->aie_timer.enabled)
339 			rx8010->ctrlreg |=
340 				(RX8010_CTRL_AIE | RX8010_CTRL_UIE);
341 
342 		err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
343 						rx8010->ctrlreg);
344 		if (err < 0)
345 			return err;
346 	}
347 
348 	return 0;
349 }
350 
351 static int rx8010_alarm_irq_enable(struct device *dev,
352 				   unsigned int enabled)
353 {
354 	struct i2c_client *client = to_i2c_client(dev);
355 	struct rx8010_data *rx8010 = dev_get_drvdata(dev);
356 	int flagreg;
357 	u8 ctrl;
358 	int err;
359 
360 	ctrl = rx8010->ctrlreg;
361 
362 	if (enabled) {
363 		if (rx8010->rtc->uie_rtctimer.enabled)
364 			ctrl |= RX8010_CTRL_UIE;
365 		if (rx8010->rtc->aie_timer.enabled)
366 			ctrl |= (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
367 	} else {
368 		if (!rx8010->rtc->uie_rtctimer.enabled)
369 			ctrl &= ~RX8010_CTRL_UIE;
370 		if (!rx8010->rtc->aie_timer.enabled)
371 			ctrl &= ~RX8010_CTRL_AIE;
372 	}
373 
374 	flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
375 	if (flagreg < 0)
376 		return flagreg;
377 
378 	flagreg &= ~RX8010_FLAG_AF;
379 	err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
380 	if (err < 0)
381 		return err;
382 
383 	if (ctrl != rx8010->ctrlreg) {
384 		rx8010->ctrlreg = ctrl;
385 		err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
386 						rx8010->ctrlreg);
387 		if (err < 0)
388 			return err;
389 	}
390 
391 	return 0;
392 }
393 
394 static int rx8010_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
395 {
396 	struct i2c_client *client = to_i2c_client(dev);
397 	struct rx8010_data *rx8010 = dev_get_drvdata(dev);
398 	int ret, tmp;
399 	int flagreg;
400 
401 	switch (cmd) {
402 	case RTC_VL_READ:
403 		flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
404 		if (flagreg < 0)
405 			return flagreg;
406 
407 		tmp = !!(flagreg & RX8010_FLAG_VLF);
408 		if (copy_to_user((void __user *)arg, &tmp, sizeof(int)))
409 			return -EFAULT;
410 
411 		return 0;
412 
413 	case RTC_VL_CLR:
414 		flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
415 		if (flagreg < 0) {
416 			return flagreg;
417 		}
418 
419 		flagreg &= ~RX8010_FLAG_VLF;
420 		ret = i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg);
421 		if (ret < 0)
422 			return ret;
423 
424 		return 0;
425 
426 	default:
427 		return -ENOIOCTLCMD;
428 	}
429 }
430 
431 static struct rtc_class_ops rx8010_rtc_ops = {
432 	.read_time = rx8010_get_time,
433 	.set_time = rx8010_set_time,
434 	.ioctl = rx8010_ioctl,
435 };
436 
437 static int rx8010_probe(struct i2c_client *client,
438 			const struct i2c_device_id *id)
439 {
440 	struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
441 	struct rx8010_data *rx8010;
442 	int err = 0;
443 
444 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
445 		| I2C_FUNC_SMBUS_I2C_BLOCK)) {
446 		dev_err(&adapter->dev, "doesn't support required functionality\n");
447 		return -EIO;
448 	}
449 
450 	rx8010 = devm_kzalloc(&client->dev, sizeof(struct rx8010_data),
451 			      GFP_KERNEL);
452 	if (!rx8010)
453 		return -ENOMEM;
454 
455 	rx8010->client = client;
456 	i2c_set_clientdata(client, rx8010);
457 
458 	err = rx8010_init_client(client);
459 	if (err)
460 		return err;
461 
462 	if (client->irq > 0) {
463 		dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
464 		err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
465 						rx8010_irq_1_handler,
466 						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
467 						"rx8010", client);
468 
469 		if (err) {
470 			dev_err(&client->dev, "unable to request IRQ\n");
471 			client->irq = 0;
472 		} else {
473 			rx8010_rtc_ops.read_alarm = rx8010_read_alarm;
474 			rx8010_rtc_ops.set_alarm = rx8010_set_alarm;
475 			rx8010_rtc_ops.alarm_irq_enable = rx8010_alarm_irq_enable;
476 		}
477 	}
478 
479 	rx8010->rtc = devm_rtc_device_register(&client->dev, client->name,
480 		&rx8010_rtc_ops, THIS_MODULE);
481 
482 	if (IS_ERR(rx8010->rtc)) {
483 		dev_err(&client->dev, "unable to register the class device\n");
484 		return PTR_ERR(rx8010->rtc);
485 	}
486 
487 	rx8010->rtc->max_user_freq = 1;
488 
489 	return err;
490 }
491 
492 static struct i2c_driver rx8010_driver = {
493 	.driver = {
494 		.name = "rtc-rx8010",
495 		.of_match_table = of_match_ptr(rx8010_of_match),
496 	},
497 	.probe		= rx8010_probe,
498 	.id_table	= rx8010_id,
499 };
500 
501 module_i2c_driver(rx8010_driver);
502 
503 MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>");
504 MODULE_DESCRIPTION("Epson RX8010SJ RTC driver");
505 MODULE_LICENSE("GPL v2");
506