blob: 772d221ec2d9f16fc879787ff2729c3c731a416b [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;
66 spinlock_t flags_lock;
67};
68
69static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id)
70{
71 struct i2c_client *client = dev_id;
72 struct rx8010_data *rx8010 = i2c_get_clientdata(client);
73 int flagreg;
74
75 spin_lock(&rx8010->flags_lock);
76
77 flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
78
79 if (flagreg <= 0) {
80 spin_unlock(&rx8010->flags_lock);
81 return IRQ_NONE;
82 }
83
84 if (flagreg & RX8010_FLAG_VLF)
85 dev_warn(&client->dev, "Frequency stop detected\n");
86
87 if (flagreg & RX8010_FLAG_TF) {
88 flagreg &= ~RX8010_FLAG_TF;
89 rtc_update_irq(rx8010->rtc, 1, RTC_PF | RTC_IRQF);
90 }
91
92 if (flagreg & RX8010_FLAG_AF) {
93 flagreg &= ~RX8010_FLAG_AF;
94 rtc_update_irq(rx8010->rtc, 1, RTC_AF | RTC_IRQF);
95 }
96
97 if (flagreg & RX8010_FLAG_UF) {
98 flagreg &= ~RX8010_FLAG_UF;
99 rtc_update_irq(rx8010->rtc, 1, RTC_UF | RTC_IRQF);
100 }
101
102 i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg);
103
104 spin_unlock(&rx8010->flags_lock);
105 return IRQ_HANDLED;
106}
107
108static int rx8010_get_time(struct device *dev, struct rtc_time *dt)
109{
110 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
111 u8 date[7];
112 int flagreg;
113 int err;
114
115 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
116 if (flagreg < 0)
117 return flagreg;
118
119 if (flagreg & RX8010_FLAG_VLF) {
120 dev_warn(dev, "Frequency stop detected\n");
121 return -EINVAL;
122 }
123
124 err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_SEC,
125 7, date);
126 if (err != 7)
127 return err < 0 ? err : -EIO;
128
129 dt->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
130 dt->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
131 dt->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f);
132 dt->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f);
133 dt->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f) - 1;
134 dt->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 100;
135 dt->tm_wday = ffs(date[RX8010_WDAY - RX8010_SEC] & 0x7f);
136
137 return rtc_valid_tm(dt);
138}
139
140static int rx8010_set_time(struct device *dev, struct rtc_time *dt)
141{
142 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
143 u8 date[7];
144 int ctrl, flagreg;
145 int ret;
146 unsigned long irqflags;
147
148 if ((dt->tm_year < 100) || (dt->tm_year > 199))
149 return -EINVAL;
150
151 /* set STOP bit before changing clock/calendar */
152 ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL);
153 if (ctrl < 0)
154 return ctrl;
155 rx8010->ctrlreg = ctrl | RX8010_CTRL_STOP;
156 ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
157 rx8010->ctrlreg);
158 if (ret < 0)
159 return ret;
160
161 date[RX8010_SEC - RX8010_SEC] = bin2bcd(dt->tm_sec);
162 date[RX8010_MIN - RX8010_SEC] = bin2bcd(dt->tm_min);
163 date[RX8010_HOUR - RX8010_SEC] = bin2bcd(dt->tm_hour);
164 date[RX8010_MDAY - RX8010_SEC] = bin2bcd(dt->tm_mday);
165 date[RX8010_MONTH - RX8010_SEC] = bin2bcd(dt->tm_mon + 1);
166 date[RX8010_YEAR - RX8010_SEC] = bin2bcd(dt->tm_year - 100);
167 date[RX8010_WDAY - RX8010_SEC] = bin2bcd(1 << dt->tm_wday);
168
169 ret = i2c_smbus_write_i2c_block_data(rx8010->client,
170 RX8010_SEC, 7, date);
171 if (ret < 0)
172 return ret;
173
174 /* clear STOP bit after changing clock/calendar */
175 ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL);
176 if (ctrl < 0)
177 return ctrl;
178 rx8010->ctrlreg = ctrl & ~RX8010_CTRL_STOP;
179 ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
180 rx8010->ctrlreg);
181 if (ret < 0)
182 return ret;
183
184 spin_lock_irqsave(&rx8010->flags_lock, irqflags);
185
186 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
187 if (flagreg < 0) {
188 spin_unlock_irqrestore(&rx8010->flags_lock, irqflags);
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 spin_unlock_irqrestore(&rx8010->flags_lock, irqflags);
197
198 return 0;
199}
200
201static int rx8010_init_client(struct i2c_client *client)
202{
203 struct rx8010_data *rx8010 = i2c_get_clientdata(client);
204 u8 ctrl[2];
205 int need_clear = 0, err = 0;
206
207 /* Initialize reserved registers as specified in datasheet */
208 err = i2c_smbus_write_byte_data(client, RX8010_RESV17, 0xD8);
209 if (err < 0)
210 return err;
211
212 err = i2c_smbus_write_byte_data(client, RX8010_RESV30, 0x00);
213 if (err < 0)
214 return err;
215
216 err = i2c_smbus_write_byte_data(client, RX8010_RESV31, 0x08);
217 if (err < 0)
218 return err;
219
220 err = i2c_smbus_write_byte_data(client, RX8010_IRQ, 0x00);
221 if (err < 0)
222 return err;
223
224 err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_FLAG,
225 2, ctrl);
226 if (err != 2)
227 return err < 0 ? err : -EIO;
228
229 if (ctrl[0] & RX8010_FLAG_VLF)
230 dev_warn(&client->dev, "Frequency stop was detected\n");
231
232 if (ctrl[0] & RX8010_FLAG_AF) {
233 dev_warn(&client->dev, "Alarm was detected\n");
234 need_clear = 1;
235 }
236
237 if (ctrl[0] & RX8010_FLAG_TF)
238 need_clear = 1;
239
240 if (ctrl[0] & RX8010_FLAG_UF)
241 need_clear = 1;
242
243 if (need_clear) {
244 ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);
245 err = i2c_smbus_write_byte_data(client, RX8010_FLAG, ctrl[0]);
246 if (err < 0)
247 return err;
248 }
249
250 rx8010->ctrlreg = (ctrl[1] & ~RX8010_CTRL_TEST);
251
252 return err;
253}
254
255static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t)
256{
257 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
258 struct i2c_client *client = rx8010->client;
259 u8 alarmvals[3];
260 int flagreg;
261 int err;
262
263 err = i2c_smbus_read_i2c_block_data(client, RX8010_ALMIN, 3, alarmvals);
264 if (err != 3)
265 return err < 0 ? err : -EIO;
266
267 flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
268 if (flagreg < 0)
269 return flagreg;
270
271 t->time.tm_sec = 0;
272 t->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
273 t->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
274
275 if (alarmvals[2] & RX8010_ALARM_AE)
276 t->time.tm_mday = -1;
277 else
278 t->time.tm_mday = bcd2bin(alarmvals[2] & 0x7f);
279
280 t->time.tm_wday = -1;
281 t->time.tm_mon = -1;
282 t->time.tm_year = -1;
283
284 t->enabled = !!(rx8010->ctrlreg & RX8010_CTRL_AIE);
285 t->pending = (flagreg & RX8010_FLAG_AF) && t->enabled;
286
287 return err;
288}
289
290static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t)
291{
292 struct i2c_client *client = to_i2c_client(dev);
293 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
294 u8 alarmvals[3];
295 int extreg, flagreg;
296 int err;
297 unsigned long irqflags;
298
299 spin_lock_irqsave(&rx8010->flags_lock, irqflags);
300 flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
301 if (flagreg < 0) {
302 spin_unlock_irqrestore(&rx8010->flags_lock, irqflags);
303 return flagreg;
304 }
305
306 if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE)) {
307 rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE);
308 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
309 rx8010->ctrlreg);
310 if (err < 0) {
311 spin_unlock_irqrestore(&rx8010->flags_lock, irqflags);
312 return err;
313 }
314 }
315
316 flagreg &= ~RX8010_FLAG_AF;
317 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
318 spin_unlock_irqrestore(&rx8010->flags_lock, irqflags);
319 if (err < 0)
320 return err;
321
322 alarmvals[0] = bin2bcd(t->time.tm_min);
323 alarmvals[1] = bin2bcd(t->time.tm_hour);
324 alarmvals[2] = bin2bcd(t->time.tm_mday);
325
326 err = i2c_smbus_write_i2c_block_data(rx8010->client, RX8010_ALMIN,
327 2, alarmvals);
328 if (err < 0)
329 return err;
330
331 extreg = i2c_smbus_read_byte_data(client, RX8010_EXT);
332 if (extreg < 0)
333 return extreg;
334
335 extreg |= RX8010_EXT_WADA;
336 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_EXT, extreg);
337 if (err < 0)
338 return err;
339
340 if (alarmvals[2] == 0)
341 alarmvals[2] |= RX8010_ALARM_AE;
342
343 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_ALWDAY,
344 alarmvals[2]);
345 if (err < 0)
346 return err;
347
348 if (t->enabled) {
349 if (rx8010->rtc->uie_rtctimer.enabled)
350 rx8010->ctrlreg |= RX8010_CTRL_UIE;
351 if (rx8010->rtc->aie_timer.enabled)
352 rx8010->ctrlreg |=
353 (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
354
355 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
356 rx8010->ctrlreg);
357 if (err < 0)
358 return err;
359 }
360
361 return 0;
362}
363
364static int rx8010_alarm_irq_enable(struct device *dev,
365 unsigned int enabled)
366{
367 struct i2c_client *client = to_i2c_client(dev);
368 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
369 int flagreg;
370 u8 ctrl;
371 int err;
372
373 ctrl = rx8010->ctrlreg;
374
375 if (enabled) {
376 if (rx8010->rtc->uie_rtctimer.enabled)
377 ctrl |= RX8010_CTRL_UIE;
378 if (rx8010->rtc->aie_timer.enabled)
379 ctrl |= (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
380 } else {
381 if (!rx8010->rtc->uie_rtctimer.enabled)
382 ctrl &= ~RX8010_CTRL_UIE;
383 if (!rx8010->rtc->aie_timer.enabled)
384 ctrl &= ~RX8010_CTRL_AIE;
385 }
386
387 flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
388 if (flagreg < 0)
389 return flagreg;
390
391 flagreg &= ~RX8010_FLAG_AF;
392 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
393 if (err < 0)
394 return err;
395
396 if (ctrl != rx8010->ctrlreg) {
397 rx8010->ctrlreg = ctrl;
398 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
399 rx8010->ctrlreg);
400 if (err < 0)
401 return err;
402 }
403
404 return 0;
405}
406
407static int rx8010_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
408{
409 struct i2c_client *client = to_i2c_client(dev);
410 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
411 int ret, tmp;
412 int flagreg;
413 unsigned long irqflags;
414
415 switch (cmd) {
416 case RTC_VL_READ:
417 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
418 if (flagreg < 0)
419 return flagreg;
420
421 tmp = !!(flagreg & RX8010_FLAG_VLF);
422 if (copy_to_user((void __user *)arg, &tmp, sizeof(int)))
423 return -EFAULT;
424
425 return 0;
426
427 case RTC_VL_CLR:
428 spin_lock_irqsave(&rx8010->flags_lock, irqflags);
429 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
430 if (flagreg < 0) {
431 spin_unlock_irqrestore(&rx8010->flags_lock, irqflags);
432 return flagreg;
433 }
434
435 flagreg &= ~RX8010_FLAG_VLF;
436 ret = i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg);
437 spin_unlock_irqrestore(&rx8010->flags_lock, irqflags);
438 if (ret < 0)
439 return ret;
440
441 return 0;
442
443 default:
444 return -ENOIOCTLCMD;
445 }
446}
447
448static struct rtc_class_ops rx8010_rtc_ops = {
449 .read_time = rx8010_get_time,
450 .set_time = rx8010_set_time,
451 .ioctl = rx8010_ioctl,
452};
453
454static int rx8010_probe(struct i2c_client *client,
455 const struct i2c_device_id *id)
456{
457 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
458 struct rx8010_data *rx8010;
459 int err = 0;
460
461 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
462 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
463 dev_err(&adapter->dev, "doesn't support required functionality\n");
464 return -EIO;
465 }
466
467 rx8010 = devm_kzalloc(&client->dev, sizeof(struct rx8010_data),
468 GFP_KERNEL);
469 if (!rx8010)
470 return -ENOMEM;
471
472 rx8010->client = client;
473 i2c_set_clientdata(client, rx8010);
474
475 spin_lock_init(&rx8010->flags_lock);
476
477 err = rx8010_init_client(client);
478 if (err)
479 return err;
480
481 if (client->irq > 0) {
482 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
483 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
484 rx8010_irq_1_handler,
485 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
486 "rx8010", client);
487
488 if (err) {
489 dev_err(&client->dev, "unable to request IRQ\n");
490 client->irq = 0;
491 } else {
492 rx8010_rtc_ops.read_alarm = rx8010_read_alarm;
493 rx8010_rtc_ops.set_alarm = rx8010_set_alarm;
494 rx8010_rtc_ops.alarm_irq_enable = rx8010_alarm_irq_enable;
495 }
496 }
497
498 rx8010->rtc = devm_rtc_device_register(&client->dev, client->name,
499 &rx8010_rtc_ops, THIS_MODULE);
500
501 if (IS_ERR(rx8010->rtc)) {
502 dev_err(&client->dev, "unable to register the class device\n");
503 return PTR_ERR(rx8010->rtc);
504 }
505
506 rx8010->rtc->max_user_freq = 1;
507
508 return err;
509}
510
511static struct i2c_driver rx8010_driver = {
512 .driver = {
513 .name = "rtc-rx8010",
514 },
515 .probe = rx8010_probe,
516 .id_table = rx8010_id,
517};
518
519module_i2c_driver(rx8010_driver);
520
521MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>");
522MODULE_DESCRIPTION("Epson RX8010SJ RTC driver");
523MODULE_LICENSE("GPL v2");