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_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
42 #define RX8010_EXT_WADA BIT(3)
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)
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)
54 #define RX8010_ALARM_AE BIT(7)
56 static const struct i2c_device_id rx8010_id
[] = {
60 MODULE_DEVICE_TABLE(i2c
, rx8010_id
);
62 static const struct of_device_id rx8010_of_match
[] = {
63 { .compatible
= "epson,rx8010" },
66 MODULE_DEVICE_TABLE(of
, rx8010_of_match
);
69 struct i2c_client
*client
;
70 struct rtc_device
*rtc
;
74 static irqreturn_t
rx8010_irq_1_handler(int irq
, void *dev_id
)
76 struct i2c_client
*client
= dev_id
;
77 struct rx8010_data
*rx8010
= i2c_get_clientdata(client
);
80 mutex_lock(&rx8010
->rtc
->ops_lock
);
82 flagreg
= i2c_smbus_read_byte_data(client
, RX8010_FLAG
);
85 mutex_unlock(&rx8010
->rtc
->ops_lock
);
89 if (flagreg
& RX8010_FLAG_VLF
)
90 dev_warn(&client
->dev
, "Frequency stop detected\n");
92 if (flagreg
& RX8010_FLAG_TF
) {
93 flagreg
&= ~RX8010_FLAG_TF
;
94 rtc_update_irq(rx8010
->rtc
, 1, RTC_PF
| RTC_IRQF
);
97 if (flagreg
& RX8010_FLAG_AF
) {
98 flagreg
&= ~RX8010_FLAG_AF
;
99 rtc_update_irq(rx8010
->rtc
, 1, RTC_AF
| RTC_IRQF
);
102 if (flagreg
& RX8010_FLAG_UF
) {
103 flagreg
&= ~RX8010_FLAG_UF
;
104 rtc_update_irq(rx8010
->rtc
, 1, RTC_UF
| RTC_IRQF
);
107 i2c_smbus_write_byte_data(client
, RX8010_FLAG
, flagreg
);
109 mutex_unlock(&rx8010
->rtc
->ops_lock
);
113 static int rx8010_get_time(struct device
*dev
, struct rtc_time
*dt
)
115 struct rx8010_data
*rx8010
= dev_get_drvdata(dev
);
120 flagreg
= i2c_smbus_read_byte_data(rx8010
->client
, RX8010_FLAG
);
124 if (flagreg
& RX8010_FLAG_VLF
) {
125 dev_warn(dev
, "Frequency stop detected\n");
129 err
= i2c_smbus_read_i2c_block_data(rx8010
->client
, RX8010_SEC
,
132 return err
< 0 ? err
: -EIO
;
134 dt
->tm_sec
= bcd2bin(date
[RX8010_SEC
- RX8010_SEC
] & 0x7f);
135 dt
->tm_min
= bcd2bin(date
[RX8010_MIN
- RX8010_SEC
] & 0x7f);
136 dt
->tm_hour
= bcd2bin(date
[RX8010_HOUR
- RX8010_SEC
] & 0x3f);
137 dt
->tm_mday
= bcd2bin(date
[RX8010_MDAY
- RX8010_SEC
] & 0x3f);
138 dt
->tm_mon
= bcd2bin(date
[RX8010_MONTH
- RX8010_SEC
] & 0x1f) - 1;
139 dt
->tm_year
= bcd2bin(date
[RX8010_YEAR
- RX8010_SEC
]) + 100;
140 dt
->tm_wday
= ffs(date
[RX8010_WDAY
- RX8010_SEC
] & 0x7f);
142 return rtc_valid_tm(dt
);
145 static int rx8010_set_time(struct device
*dev
, struct rtc_time
*dt
)
147 struct rx8010_data
*rx8010
= dev_get_drvdata(dev
);
152 if ((dt
->tm_year
< 100) || (dt
->tm_year
> 199))
155 /* set STOP bit before changing clock/calendar */
156 ctrl
= i2c_smbus_read_byte_data(rx8010
->client
, RX8010_CTRL
);
159 rx8010
->ctrlreg
= ctrl
| RX8010_CTRL_STOP
;
160 ret
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_CTRL
,
165 date
[RX8010_SEC
- RX8010_SEC
] = bin2bcd(dt
->tm_sec
);
166 date
[RX8010_MIN
- RX8010_SEC
] = bin2bcd(dt
->tm_min
);
167 date
[RX8010_HOUR
- RX8010_SEC
] = bin2bcd(dt
->tm_hour
);
168 date
[RX8010_MDAY
- RX8010_SEC
] = bin2bcd(dt
->tm_mday
);
169 date
[RX8010_MONTH
- RX8010_SEC
] = bin2bcd(dt
->tm_mon
+ 1);
170 date
[RX8010_YEAR
- RX8010_SEC
] = bin2bcd(dt
->tm_year
- 100);
171 date
[RX8010_WDAY
- RX8010_SEC
] = bin2bcd(1 << dt
->tm_wday
);
173 ret
= i2c_smbus_write_i2c_block_data(rx8010
->client
,
174 RX8010_SEC
, 7, date
);
178 /* clear STOP bit after changing clock/calendar */
179 ctrl
= i2c_smbus_read_byte_data(rx8010
->client
, RX8010_CTRL
);
182 rx8010
->ctrlreg
= ctrl
& ~RX8010_CTRL_STOP
;
183 ret
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_CTRL
,
188 flagreg
= i2c_smbus_read_byte_data(rx8010
->client
, RX8010_FLAG
);
193 if (flagreg
& RX8010_FLAG_VLF
)
194 ret
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_FLAG
,
195 flagreg
& ~RX8010_FLAG_VLF
);
200 static int rx8010_init_client(struct i2c_client
*client
)
202 struct rx8010_data
*rx8010
= i2c_get_clientdata(client
);
204 int need_clear
= 0, err
= 0;
206 /* Initialize reserved registers as specified in datasheet */
207 err
= i2c_smbus_write_byte_data(client
, RX8010_RESV17
, 0xD8);
211 err
= i2c_smbus_write_byte_data(client
, RX8010_RESV30
, 0x00);
215 err
= i2c_smbus_write_byte_data(client
, RX8010_RESV31
, 0x08);
219 err
= i2c_smbus_write_byte_data(client
, RX8010_IRQ
, 0x00);
223 err
= i2c_smbus_read_i2c_block_data(rx8010
->client
, RX8010_FLAG
,
226 return err
< 0 ? err
: -EIO
;
228 if (ctrl
[0] & RX8010_FLAG_VLF
)
229 dev_warn(&client
->dev
, "Frequency stop was detected\n");
231 if (ctrl
[0] & RX8010_FLAG_AF
) {
232 dev_warn(&client
->dev
, "Alarm was detected\n");
236 if (ctrl
[0] & RX8010_FLAG_TF
)
239 if (ctrl
[0] & RX8010_FLAG_UF
)
243 ctrl
[0] &= ~(RX8010_FLAG_AF
| RX8010_FLAG_TF
| RX8010_FLAG_UF
);
244 err
= i2c_smbus_write_byte_data(client
, RX8010_FLAG
, ctrl
[0]);
249 rx8010
->ctrlreg
= (ctrl
[1] & ~RX8010_CTRL_TEST
);
254 static int rx8010_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
256 struct rx8010_data
*rx8010
= dev_get_drvdata(dev
);
257 struct i2c_client
*client
= rx8010
->client
;
262 err
= i2c_smbus_read_i2c_block_data(client
, RX8010_ALMIN
, 3, alarmvals
);
264 return err
< 0 ? err
: -EIO
;
266 flagreg
= i2c_smbus_read_byte_data(client
, RX8010_FLAG
);
271 t
->time
.tm_min
= bcd2bin(alarmvals
[0] & 0x7f);
272 t
->time
.tm_hour
= bcd2bin(alarmvals
[1] & 0x3f);
274 if (!(alarmvals
[2] & RX8010_ALARM_AE
))
275 t
->time
.tm_mday
= bcd2bin(alarmvals
[2] & 0x7f);
277 t
->enabled
= !!(rx8010
->ctrlreg
& RX8010_CTRL_AIE
);
278 t
->pending
= (flagreg
& RX8010_FLAG_AF
) && t
->enabled
;
283 static int rx8010_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
285 struct i2c_client
*client
= to_i2c_client(dev
);
286 struct rx8010_data
*rx8010
= dev_get_drvdata(dev
);
291 flagreg
= i2c_smbus_read_byte_data(client
, RX8010_FLAG
);
296 if (rx8010
->ctrlreg
& (RX8010_CTRL_AIE
| RX8010_CTRL_UIE
)) {
297 rx8010
->ctrlreg
&= ~(RX8010_CTRL_AIE
| RX8010_CTRL_UIE
);
298 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_CTRL
,
305 flagreg
&= ~RX8010_FLAG_AF
;
306 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_FLAG
, flagreg
);
310 alarmvals
[0] = bin2bcd(t
->time
.tm_min
);
311 alarmvals
[1] = bin2bcd(t
->time
.tm_hour
);
312 alarmvals
[2] = bin2bcd(t
->time
.tm_mday
);
314 err
= i2c_smbus_write_i2c_block_data(rx8010
->client
, RX8010_ALMIN
,
319 extreg
= i2c_smbus_read_byte_data(client
, RX8010_EXT
);
323 extreg
|= RX8010_EXT_WADA
;
324 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_EXT
, extreg
);
328 if (alarmvals
[2] == 0)
329 alarmvals
[2] |= RX8010_ALARM_AE
;
331 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_ALWDAY
,
337 if (rx8010
->rtc
->uie_rtctimer
.enabled
)
338 rx8010
->ctrlreg
|= RX8010_CTRL_UIE
;
339 if (rx8010
->rtc
->aie_timer
.enabled
)
341 (RX8010_CTRL_AIE
| RX8010_CTRL_UIE
);
343 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_CTRL
,
352 static int rx8010_alarm_irq_enable(struct device
*dev
,
353 unsigned int enabled
)
355 struct i2c_client
*client
= to_i2c_client(dev
);
356 struct rx8010_data
*rx8010
= dev_get_drvdata(dev
);
361 ctrl
= rx8010
->ctrlreg
;
364 if (rx8010
->rtc
->uie_rtctimer
.enabled
)
365 ctrl
|= RX8010_CTRL_UIE
;
366 if (rx8010
->rtc
->aie_timer
.enabled
)
367 ctrl
|= (RX8010_CTRL_AIE
| RX8010_CTRL_UIE
);
369 if (!rx8010
->rtc
->uie_rtctimer
.enabled
)
370 ctrl
&= ~RX8010_CTRL_UIE
;
371 if (!rx8010
->rtc
->aie_timer
.enabled
)
372 ctrl
&= ~RX8010_CTRL_AIE
;
375 flagreg
= i2c_smbus_read_byte_data(client
, RX8010_FLAG
);
379 flagreg
&= ~RX8010_FLAG_AF
;
380 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_FLAG
, flagreg
);
384 if (ctrl
!= rx8010
->ctrlreg
) {
385 rx8010
->ctrlreg
= ctrl
;
386 err
= i2c_smbus_write_byte_data(rx8010
->client
, RX8010_CTRL
,
395 static int rx8010_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
397 struct i2c_client
*client
= to_i2c_client(dev
);
398 struct rx8010_data
*rx8010
= dev_get_drvdata(dev
);
404 flagreg
= i2c_smbus_read_byte_data(rx8010
->client
, RX8010_FLAG
);
408 tmp
= !!(flagreg
& RX8010_FLAG_VLF
);
409 if (copy_to_user((void __user
*)arg
, &tmp
, sizeof(int)))
415 flagreg
= i2c_smbus_read_byte_data(rx8010
->client
, RX8010_FLAG
);
420 flagreg
&= ~RX8010_FLAG_VLF
;
421 ret
= i2c_smbus_write_byte_data(client
, RX8010_FLAG
, flagreg
);
432 static struct rtc_class_ops rx8010_rtc_ops
= {
433 .read_time
= rx8010_get_time
,
434 .set_time
= rx8010_set_time
,
435 .ioctl
= rx8010_ioctl
,
438 static int rx8010_probe(struct i2c_client
*client
,
439 const struct i2c_device_id
*id
)
441 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
442 struct rx8010_data
*rx8010
;
445 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
446 | I2C_FUNC_SMBUS_I2C_BLOCK
)) {
447 dev_err(&adapter
->dev
, "doesn't support required functionality\n");
451 rx8010
= devm_kzalloc(&client
->dev
, sizeof(struct rx8010_data
),
456 rx8010
->client
= client
;
457 i2c_set_clientdata(client
, rx8010
);
459 err
= rx8010_init_client(client
);
463 if (client
->irq
> 0) {
464 dev_info(&client
->dev
, "IRQ %d supplied\n", client
->irq
);
465 err
= devm_request_threaded_irq(&client
->dev
, client
->irq
, NULL
,
466 rx8010_irq_1_handler
,
467 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
471 dev_err(&client
->dev
, "unable to request IRQ\n");
474 rx8010_rtc_ops
.read_alarm
= rx8010_read_alarm
;
475 rx8010_rtc_ops
.set_alarm
= rx8010_set_alarm
;
476 rx8010_rtc_ops
.alarm_irq_enable
= rx8010_alarm_irq_enable
;
480 rx8010
->rtc
= devm_rtc_device_register(&client
->dev
, client
->name
,
481 &rx8010_rtc_ops
, THIS_MODULE
);
483 if (IS_ERR(rx8010
->rtc
)) {
484 dev_err(&client
->dev
, "unable to register the class device\n");
485 return PTR_ERR(rx8010
->rtc
);
488 rx8010
->rtc
->max_user_freq
= 1;
493 static struct i2c_driver rx8010_driver
= {
495 .name
= "rtc-rx8010",
496 .of_match_table
= of_match_ptr(rx8010_of_match
),
498 .probe
= rx8010_probe
,
499 .id_table
= rx8010_id
,
502 module_i2c_driver(rx8010_driver
);
504 MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>");
505 MODULE_DESCRIPTION("Epson RX8010SJ RTC driver");
506 MODULE_LICENSE("GPL v2");