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