2 * Driver for the Epson RTC module RX-8010 SJ
4 * Copyright(C) Timesys Corporation 2015
5 * Copyright(C) General Electric Company 2015
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.
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>
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
41 #define RX8010_EXT_WADA BIT(3)
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)
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)
53 #define RX8010_ALARM_AE BIT(7)
55 static const struct i2c_device_id rx8010_id
[] = {
59 MODULE_DEVICE_TABLE(i2c
, rx8010_id
);
61 static const struct of_device_id rx8010_of_match
[] = {
62 { .compatible
= "epson,rx8010" },
65 MODULE_DEVICE_TABLE(of
, rx8010_of_match
);
68 struct i2c_client
*client
;
69 struct rtc_device
*rtc
;
73 static irqreturn_t
rx8010_irq_1_handler(int irq
, void *dev_id
)
75 struct i2c_client
*client
= dev_id
;
76 struct rx8010_data
*rx8010
= i2c_get_clientdata(client
);
79 mutex_lock(&rx8010
->rtc
->ops_lock
);
81 flagreg
= i2c_smbus_read_byte_data(client
, RX8010_FLAG
);
84 mutex_unlock(&rx8010
->rtc
->ops_lock
);
88 if (flagreg
& RX8010_FLAG_VLF
)
89 dev_warn(&client
->dev
, "Frequency stop detected\n");
91 if (flagreg
& RX8010_FLAG_TF
) {
92 flagreg
&= ~RX8010_FLAG_TF
;
93 rtc_update_irq(rx8010
->rtc
, 1, RTC_PF
| RTC_IRQF
);
96 if (flagreg
& RX8010_FLAG_AF
) {
97 flagreg
&= ~RX8010_FLAG_AF
;
98 rtc_update_irq(rx8010
->rtc
, 1, RTC_AF
| RTC_IRQF
);
101 if (flagreg
& RX8010_FLAG_UF
) {
102 flagreg
&= ~RX8010_FLAG_UF
;
103 rtc_update_irq(rx8010
->rtc
, 1, RTC_UF
| RTC_IRQF
);
106 i2c_smbus_write_byte_data(client
, RX8010_FLAG
, flagreg
);
108 mutex_unlock(&rx8010
->rtc
->ops_lock
);
112 static int rx8010_get_time(struct device
*dev
, struct rtc_time
*dt
)
114 struct rx8010_data
*rx8010
= dev_get_drvdata(dev
);
119 flagreg
= i2c_smbus_read_byte_data(rx8010
->client
, RX8010_FLAG
);
123 if (flagreg
& RX8010_FLAG_VLF
) {
124 dev_warn(dev
, "Frequency stop detected\n");
128 err
= i2c_smbus_read_i2c_block_data(rx8010
->client
, RX8010_SEC
,
131 return err
< 0 ? err
: -EIO
;
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);
141 return rtc_valid_tm(dt
);
144 static int rx8010_set_time(struct device
*dev
, struct rtc_time
*dt
)
146 struct rx8010_data
*rx8010
= dev_get_drvdata(dev
);
151 if ((dt
->tm_year
< 100) || (dt
->tm_year
> 199))
154 /* set STOP bit before changing clock/calendar */
155 ctrl
= i2c_smbus_read_byte_data(rx8010
->client
, RX8010_CTRL
);
158 rx8010
->ctrlreg
= ctrl
| RX8010_CTRL_STOP
;
159 ret
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_CTRL
,
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
);
172 ret
= i2c_smbus_write_i2c_block_data(rx8010
->client
,
173 RX8010_SEC
, 7, date
);
177 /* clear STOP bit after changing clock/calendar */
178 ctrl
= i2c_smbus_read_byte_data(rx8010
->client
, RX8010_CTRL
);
181 rx8010
->ctrlreg
= ctrl
& ~RX8010_CTRL_STOP
;
182 ret
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_CTRL
,
187 flagreg
= i2c_smbus_read_byte_data(rx8010
->client
, RX8010_FLAG
);
192 if (flagreg
& RX8010_FLAG_VLF
)
193 ret
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_FLAG
,
194 flagreg
& ~RX8010_FLAG_VLF
);
199 static int rx8010_init_client(struct i2c_client
*client
)
201 struct rx8010_data
*rx8010
= i2c_get_clientdata(client
);
203 int need_clear
= 0, err
= 0;
205 /* Initialize reserved registers as specified in datasheet */
206 err
= i2c_smbus_write_byte_data(client
, RX8010_RESV17
, 0xD8);
210 err
= i2c_smbus_write_byte_data(client
, RX8010_RESV30
, 0x00);
214 err
= i2c_smbus_write_byte_data(client
, RX8010_RESV31
, 0x08);
218 err
= i2c_smbus_write_byte_data(client
, RX8010_IRQ
, 0x00);
222 err
= i2c_smbus_read_i2c_block_data(rx8010
->client
, RX8010_FLAG
,
225 return err
< 0 ? err
: -EIO
;
227 if (ctrl
[0] & RX8010_FLAG_VLF
)
228 dev_warn(&client
->dev
, "Frequency stop was detected\n");
230 if (ctrl
[0] & RX8010_FLAG_AF
) {
231 dev_warn(&client
->dev
, "Alarm was detected\n");
235 if (ctrl
[0] & RX8010_FLAG_TF
)
238 if (ctrl
[0] & RX8010_FLAG_UF
)
242 ctrl
[0] &= ~(RX8010_FLAG_AF
| RX8010_FLAG_TF
| RX8010_FLAG_UF
);
243 err
= i2c_smbus_write_byte_data(client
, RX8010_FLAG
, ctrl
[0]);
248 rx8010
->ctrlreg
= (ctrl
[1] & ~RX8010_CTRL_TEST
);
253 static int rx8010_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
255 struct rx8010_data
*rx8010
= dev_get_drvdata(dev
);
256 struct i2c_client
*client
= rx8010
->client
;
261 err
= i2c_smbus_read_i2c_block_data(client
, RX8010_ALMIN
, 3, alarmvals
);
263 return err
< 0 ? err
: -EIO
;
265 flagreg
= i2c_smbus_read_byte_data(client
, RX8010_FLAG
);
270 t
->time
.tm_min
= bcd2bin(alarmvals
[0] & 0x7f);
271 t
->time
.tm_hour
= bcd2bin(alarmvals
[1] & 0x3f);
273 if (!(alarmvals
[2] & RX8010_ALARM_AE
))
274 t
->time
.tm_mday
= bcd2bin(alarmvals
[2] & 0x7f);
276 t
->enabled
= !!(rx8010
->ctrlreg
& RX8010_CTRL_AIE
);
277 t
->pending
= (flagreg
& RX8010_FLAG_AF
) && t
->enabled
;
282 static int rx8010_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
284 struct i2c_client
*client
= to_i2c_client(dev
);
285 struct rx8010_data
*rx8010
= dev_get_drvdata(dev
);
290 flagreg
= i2c_smbus_read_byte_data(client
, RX8010_FLAG
);
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
,
304 flagreg
&= ~RX8010_FLAG_AF
;
305 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_FLAG
, flagreg
);
309 alarmvals
[0] = bin2bcd(t
->time
.tm_min
);
310 alarmvals
[1] = bin2bcd(t
->time
.tm_hour
);
311 alarmvals
[2] = bin2bcd(t
->time
.tm_mday
);
313 err
= i2c_smbus_write_i2c_block_data(rx8010
->client
, RX8010_ALMIN
,
318 extreg
= i2c_smbus_read_byte_data(client
, RX8010_EXT
);
322 extreg
|= RX8010_EXT_WADA
;
323 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_EXT
, extreg
);
327 if (alarmvals
[2] == 0)
328 alarmvals
[2] |= RX8010_ALARM_AE
;
330 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_ALWDAY
,
336 if (rx8010
->rtc
->uie_rtctimer
.enabled
)
337 rx8010
->ctrlreg
|= RX8010_CTRL_UIE
;
338 if (rx8010
->rtc
->aie_timer
.enabled
)
340 (RX8010_CTRL_AIE
| RX8010_CTRL_UIE
);
342 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_CTRL
,
351 static int rx8010_alarm_irq_enable(struct device
*dev
,
352 unsigned int enabled
)
354 struct i2c_client
*client
= to_i2c_client(dev
);
355 struct rx8010_data
*rx8010
= dev_get_drvdata(dev
);
360 ctrl
= rx8010
->ctrlreg
;
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
);
368 if (!rx8010
->rtc
->uie_rtctimer
.enabled
)
369 ctrl
&= ~RX8010_CTRL_UIE
;
370 if (!rx8010
->rtc
->aie_timer
.enabled
)
371 ctrl
&= ~RX8010_CTRL_AIE
;
374 flagreg
= i2c_smbus_read_byte_data(client
, RX8010_FLAG
);
378 flagreg
&= ~RX8010_FLAG_AF
;
379 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_FLAG
, flagreg
);
383 if (ctrl
!= rx8010
->ctrlreg
) {
384 rx8010
->ctrlreg
= ctrl
;
385 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_CTRL
,
394 static int rx8010_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
396 struct i2c_client
*client
= to_i2c_client(dev
);
397 struct rx8010_data
*rx8010
= dev_get_drvdata(dev
);
403 flagreg
= i2c_smbus_read_byte_data(rx8010
->client
, RX8010_FLAG
);
407 tmp
= !!(flagreg
& RX8010_FLAG_VLF
);
408 if (copy_to_user((void __user
*)arg
, &tmp
, sizeof(int)))
414 flagreg
= i2c_smbus_read_byte_data(rx8010
->client
, RX8010_FLAG
);
419 flagreg
&= ~RX8010_FLAG_VLF
;
420 ret
= i2c_smbus_write_byte_data(client
, RX8010_FLAG
, flagreg
);
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
,
437 static int rx8010_probe(struct i2c_client
*client
,
438 const struct i2c_device_id
*id
)
440 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
441 struct rx8010_data
*rx8010
;
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");
450 rx8010
= devm_kzalloc(&client
->dev
, sizeof(struct rx8010_data
),
455 rx8010
->client
= client
;
456 i2c_set_clientdata(client
, rx8010
);
458 err
= rx8010_init_client(client
);
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
,
470 dev_err(&client
->dev
, "unable to request IRQ\n");
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
;
479 rx8010
->rtc
= devm_rtc_device_register(&client
->dev
, client
->name
,
480 &rx8010_rtc_ops
, THIS_MODULE
);
482 if (IS_ERR(rx8010
->rtc
)) {
483 dev_err(&client
->dev
, "unable to register the class device\n");
484 return PTR_ERR(rx8010
->rtc
);
487 rx8010
->rtc
->max_user_freq
= 1;
492 static struct i2c_driver rx8010_driver
= {
494 .name
= "rtc-rx8010",
495 .of_match_table
= of_match_ptr(rx8010_of_match
),
497 .probe
= rx8010_probe
,
498 .id_table
= rx8010_id
,
501 module_i2c_driver(rx8010_driver
);
503 MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>");
504 MODULE_DESCRIPTION("Epson RX8010SJ RTC driver");
505 MODULE_LICENSE("GPL v2");