2 * EPSON TOYOCOM RTC-7301SF/DG Driver
4 * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
6 * Based on rtc-rp5c01.c
8 * Datasheet: http://www5.epsondevice.com/en/products/parallel/rtc7301sf.html
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/delay.h>
16 #include <linux/regmap.h>
17 #include <linux/platform_device.h>
18 #include <linux/rtc.h>
20 #define DRV_NAME "rtc-r7301"
22 #define RTC7301_1_SEC 0x0 /* Bank 0 and Band 1 */
23 #define RTC7301_10_SEC 0x1 /* Bank 0 and Band 1 */
24 #define RTC7301_AE BIT(3)
25 #define RTC7301_1_MIN 0x2 /* Bank 0 and Band 1 */
26 #define RTC7301_10_MIN 0x3 /* Bank 0 and Band 1 */
27 #define RTC7301_1_HOUR 0x4 /* Bank 0 and Band 1 */
28 #define RTC7301_10_HOUR 0x5 /* Bank 0 and Band 1 */
29 #define RTC7301_DAY_OF_WEEK 0x6 /* Bank 0 and Band 1 */
30 #define RTC7301_1_DAY 0x7 /* Bank 0 and Band 1 */
31 #define RTC7301_10_DAY 0x8 /* Bank 0 and Band 1 */
32 #define RTC7301_1_MONTH 0x9 /* Bank 0 */
33 #define RTC7301_10_MONTH 0xa /* Bank 0 */
34 #define RTC7301_1_YEAR 0xb /* Bank 0 */
35 #define RTC7301_10_YEAR 0xc /* Bank 0 */
36 #define RTC7301_100_YEAR 0xd /* Bank 0 */
37 #define RTC7301_1000_YEAR 0xe /* Bank 0 */
38 #define RTC7301_ALARM_CONTROL 0xe /* Bank 1 */
39 #define RTC7301_ALARM_CONTROL_AIE BIT(0)
40 #define RTC7301_ALARM_CONTROL_AF BIT(1)
41 #define RTC7301_TIMER_CONTROL 0xe /* Bank 2 */
42 #define RTC7301_TIMER_CONTROL_TIE BIT(0)
43 #define RTC7301_TIMER_CONTROL_TF BIT(1)
44 #define RTC7301_CONTROL 0xf /* All banks */
45 #define RTC7301_CONTROL_BUSY BIT(0)
46 #define RTC7301_CONTROL_STOP BIT(1)
47 #define RTC7301_CONTROL_BANK_SEL_0 BIT(2)
48 #define RTC7301_CONTROL_BANK_SEL_1 BIT(3)
51 struct regmap
*regmap
;
57 static const struct regmap_config rtc7301_regmap_config
= {
63 static u8
rtc7301_read(struct rtc7301_priv
*priv
, unsigned int reg
)
65 int reg_stride
= regmap_get_reg_stride(priv
->regmap
);
68 regmap_read(priv
->regmap
, reg_stride
* reg
, &val
);
73 static void rtc7301_write(struct rtc7301_priv
*priv
, u8 val
, unsigned int reg
)
75 int reg_stride
= regmap_get_reg_stride(priv
->regmap
);
77 regmap_write(priv
->regmap
, reg_stride
* reg
, val
);
80 static void rtc7301_update_bits(struct rtc7301_priv
*priv
, unsigned int reg
,
83 int reg_stride
= regmap_get_reg_stride(priv
->regmap
);
85 regmap_update_bits(priv
->regmap
, reg_stride
* reg
, mask
, val
);
88 static int rtc7301_wait_while_busy(struct rtc7301_priv
*priv
)
92 while (retries
-- > 0) {
95 val
= rtc7301_read(priv
, RTC7301_CONTROL
);
96 if (!(val
& RTC7301_CONTROL_BUSY
))
105 static void rtc7301_stop(struct rtc7301_priv
*priv
)
107 rtc7301_update_bits(priv
, RTC7301_CONTROL
, RTC7301_CONTROL_STOP
,
108 RTC7301_CONTROL_STOP
);
111 static void rtc7301_start(struct rtc7301_priv
*priv
)
113 rtc7301_update_bits(priv
, RTC7301_CONTROL
, RTC7301_CONTROL_STOP
, 0);
116 static void rtc7301_select_bank(struct rtc7301_priv
*priv
, u8 bank
)
120 if (bank
== priv
->bank
)
124 val
|= RTC7301_CONTROL_BANK_SEL_0
;
126 val
|= RTC7301_CONTROL_BANK_SEL_1
;
128 rtc7301_update_bits(priv
, RTC7301_CONTROL
,
129 RTC7301_CONTROL_BANK_SEL_0
|
130 RTC7301_CONTROL_BANK_SEL_1
, val
);
135 static void rtc7301_get_time(struct rtc7301_priv
*priv
, struct rtc_time
*tm
,
140 tm
->tm_sec
= rtc7301_read(priv
, RTC7301_1_SEC
);
141 tm
->tm_sec
+= (rtc7301_read(priv
, RTC7301_10_SEC
) & ~RTC7301_AE
) * 10;
142 tm
->tm_min
= rtc7301_read(priv
, RTC7301_1_MIN
);
143 tm
->tm_min
+= (rtc7301_read(priv
, RTC7301_10_MIN
) & ~RTC7301_AE
) * 10;
144 tm
->tm_hour
= rtc7301_read(priv
, RTC7301_1_HOUR
);
145 tm
->tm_hour
+= (rtc7301_read(priv
, RTC7301_10_HOUR
) & ~RTC7301_AE
) * 10;
146 tm
->tm_mday
= rtc7301_read(priv
, RTC7301_1_DAY
);
147 tm
->tm_mday
+= (rtc7301_read(priv
, RTC7301_10_DAY
) & ~RTC7301_AE
) * 10;
158 tm
->tm_wday
= (rtc7301_read(priv
, RTC7301_DAY_OF_WEEK
) & ~RTC7301_AE
);
159 tm
->tm_mon
= rtc7301_read(priv
, RTC7301_10_MONTH
) * 10 +
160 rtc7301_read(priv
, RTC7301_1_MONTH
) - 1;
161 year
= rtc7301_read(priv
, RTC7301_1000_YEAR
) * 1000 +
162 rtc7301_read(priv
, RTC7301_100_YEAR
) * 100 +
163 rtc7301_read(priv
, RTC7301_10_YEAR
) * 10 +
164 rtc7301_read(priv
, RTC7301_1_YEAR
);
166 tm
->tm_year
= year
- 1900;
169 static void rtc7301_write_time(struct rtc7301_priv
*priv
, struct rtc_time
*tm
,
174 rtc7301_write(priv
, tm
->tm_sec
% 10, RTC7301_1_SEC
);
175 rtc7301_write(priv
, tm
->tm_sec
/ 10, RTC7301_10_SEC
);
177 rtc7301_write(priv
, tm
->tm_min
% 10, RTC7301_1_MIN
);
178 rtc7301_write(priv
, tm
->tm_min
/ 10, RTC7301_10_MIN
);
180 rtc7301_write(priv
, tm
->tm_hour
% 10, RTC7301_1_HOUR
);
181 rtc7301_write(priv
, tm
->tm_hour
/ 10, RTC7301_10_HOUR
);
183 rtc7301_write(priv
, tm
->tm_mday
% 10, RTC7301_1_DAY
);
184 rtc7301_write(priv
, tm
->tm_mday
/ 10, RTC7301_10_DAY
);
186 /* Don't care for alarm register */
187 rtc7301_write(priv
, alarm
? RTC7301_AE
: tm
->tm_wday
,
188 RTC7301_DAY_OF_WEEK
);
193 rtc7301_write(priv
, (tm
->tm_mon
+ 1) % 10, RTC7301_1_MONTH
);
194 rtc7301_write(priv
, (tm
->tm_mon
+ 1) / 10, RTC7301_10_MONTH
);
196 year
= tm
->tm_year
+ 1900;
198 rtc7301_write(priv
, year
% 10, RTC7301_1_YEAR
);
199 rtc7301_write(priv
, (year
/ 10) % 10, RTC7301_10_YEAR
);
200 rtc7301_write(priv
, (year
/ 100) % 10, RTC7301_100_YEAR
);
201 rtc7301_write(priv
, year
/ 1000, RTC7301_1000_YEAR
);
204 static void rtc7301_alarm_irq(struct rtc7301_priv
*priv
, unsigned int enabled
)
206 rtc7301_update_bits(priv
, RTC7301_ALARM_CONTROL
,
207 RTC7301_ALARM_CONTROL_AF
|
208 RTC7301_ALARM_CONTROL_AIE
,
209 enabled
? RTC7301_ALARM_CONTROL_AIE
: 0);
212 static int rtc7301_read_time(struct device
*dev
, struct rtc_time
*tm
)
214 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
218 spin_lock_irqsave(&priv
->lock
, flags
);
220 rtc7301_select_bank(priv
, 0);
222 err
= rtc7301_wait_while_busy(priv
);
224 rtc7301_get_time(priv
, tm
, false);
226 spin_unlock_irqrestore(&priv
->lock
, flags
);
231 static int rtc7301_set_time(struct device
*dev
, struct rtc_time
*tm
)
233 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
236 spin_lock_irqsave(&priv
->lock
, flags
);
240 rtc7301_select_bank(priv
, 0);
241 rtc7301_write_time(priv
, tm
, false);
244 spin_unlock_irqrestore(&priv
->lock
, flags
);
249 static int rtc7301_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
251 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
258 spin_lock_irqsave(&priv
->lock
, flags
);
260 rtc7301_select_bank(priv
, 1);
261 rtc7301_get_time(priv
, &alarm
->time
, true);
263 alrm_ctrl
= rtc7301_read(priv
, RTC7301_ALARM_CONTROL
);
265 alarm
->enabled
= !!(alrm_ctrl
& RTC7301_ALARM_CONTROL_AIE
);
266 alarm
->pending
= !!(alrm_ctrl
& RTC7301_ALARM_CONTROL_AF
);
268 spin_unlock_irqrestore(&priv
->lock
, flags
);
273 static int rtc7301_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
275 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
281 spin_lock_irqsave(&priv
->lock
, flags
);
283 rtc7301_select_bank(priv
, 1);
284 rtc7301_write_time(priv
, &alarm
->time
, true);
285 rtc7301_alarm_irq(priv
, alarm
->enabled
);
287 spin_unlock_irqrestore(&priv
->lock
, flags
);
292 static int rtc7301_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
294 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
300 spin_lock_irqsave(&priv
->lock
, flags
);
302 rtc7301_select_bank(priv
, 1);
303 rtc7301_alarm_irq(priv
, enabled
);
305 spin_unlock_irqrestore(&priv
->lock
, flags
);
310 static const struct rtc_class_ops rtc7301_rtc_ops
= {
311 .read_time
= rtc7301_read_time
,
312 .set_time
= rtc7301_set_time
,
313 .read_alarm
= rtc7301_read_alarm
,
314 .set_alarm
= rtc7301_set_alarm
,
315 .alarm_irq_enable
= rtc7301_alarm_irq_enable
,
318 static irqreturn_t
rtc7301_irq_handler(int irq
, void *dev_id
)
320 struct rtc_device
*rtc
= dev_id
;
321 struct rtc7301_priv
*priv
= dev_get_drvdata(rtc
->dev
.parent
);
323 irqreturn_t ret
= IRQ_NONE
;
326 spin_lock_irqsave(&priv
->lock
, flags
);
328 rtc7301_select_bank(priv
, 1);
330 alrm_ctrl
= rtc7301_read(priv
, RTC7301_ALARM_CONTROL
);
331 if (alrm_ctrl
& RTC7301_ALARM_CONTROL_AF
) {
333 rtc7301_alarm_irq(priv
, false);
334 rtc_update_irq(rtc
, 1, RTC_IRQF
| RTC_AF
);
337 spin_unlock_irqrestore(&priv
->lock
, flags
);
342 static void rtc7301_init(struct rtc7301_priv
*priv
)
346 spin_lock_irqsave(&priv
->lock
, flags
);
348 rtc7301_select_bank(priv
, 2);
349 rtc7301_write(priv
, 0, RTC7301_TIMER_CONTROL
);
351 spin_unlock_irqrestore(&priv
->lock
, flags
);
354 static int __init
rtc7301_rtc_probe(struct platform_device
*dev
)
356 struct resource
*res
;
358 struct rtc7301_priv
*priv
;
359 struct rtc_device
*rtc
;
362 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
366 priv
= devm_kzalloc(&dev
->dev
, sizeof(*priv
), GFP_KERNEL
);
370 regs
= devm_ioremap_resource(&dev
->dev
, res
);
372 return PTR_ERR(regs
);
374 priv
->regmap
= devm_regmap_init_mmio(&dev
->dev
, regs
,
375 &rtc7301_regmap_config
);
376 if (IS_ERR(priv
->regmap
))
377 return PTR_ERR(priv
->regmap
);
379 priv
->irq
= platform_get_irq(dev
, 0);
381 spin_lock_init(&priv
->lock
);
386 platform_set_drvdata(dev
, priv
);
388 rtc
= devm_rtc_device_register(&dev
->dev
, DRV_NAME
, &rtc7301_rtc_ops
,
394 ret
= devm_request_irq(&dev
->dev
, priv
->irq
,
395 rtc7301_irq_handler
, IRQF_SHARED
,
396 dev_name(&dev
->dev
), rtc
);
399 dev_err(&dev
->dev
, "unable to request IRQ\n");
401 device_set_wakeup_capable(&dev
->dev
, true);
408 #ifdef CONFIG_PM_SLEEP
410 static int rtc7301_suspend(struct device
*dev
)
412 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
414 if (device_may_wakeup(dev
))
415 enable_irq_wake(priv
->irq
);
420 static int rtc7301_resume(struct device
*dev
)
422 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
424 if (device_may_wakeup(dev
))
425 disable_irq_wake(priv
->irq
);
432 static SIMPLE_DEV_PM_OPS(rtc7301_pm_ops
, rtc7301_suspend
, rtc7301_resume
);
434 static const struct of_device_id rtc7301_dt_match
[] = {
435 { .compatible
= "epson,rtc7301sf" },
436 { .compatible
= "epson,rtc7301dg" },
439 MODULE_DEVICE_TABLE(of
, rtc7301_dt_match
);
441 static struct platform_driver rtc7301_rtc_driver
= {
444 .of_match_table
= rtc7301_dt_match
,
445 .pm
= &rtc7301_pm_ops
,
449 module_platform_driver_probe(rtc7301_rtc_driver
, rtc7301_rtc_probe
);
451 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
452 MODULE_LICENSE("GPL");
453 MODULE_DESCRIPTION("EPSON TOYOCOM RTC-7301SF/DG Driver");
454 MODULE_ALIAS("platform:rtc-r7301");