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/delay.h>
15 #include <linux/regmap.h>
16 #include <linux/platform_device.h>
17 #include <linux/rtc.h>
19 #define DRV_NAME "rtc-r7301"
21 #define RTC7301_1_SEC 0x0 /* Bank 0 and Band 1 */
22 #define RTC7301_10_SEC 0x1 /* Bank 0 and Band 1 */
23 #define RTC7301_AE BIT(3)
24 #define RTC7301_1_MIN 0x2 /* Bank 0 and Band 1 */
25 #define RTC7301_10_MIN 0x3 /* Bank 0 and Band 1 */
26 #define RTC7301_1_HOUR 0x4 /* Bank 0 and Band 1 */
27 #define RTC7301_10_HOUR 0x5 /* Bank 0 and Band 1 */
28 #define RTC7301_DAY_OF_WEEK 0x6 /* Bank 0 and Band 1 */
29 #define RTC7301_1_DAY 0x7 /* Bank 0 and Band 1 */
30 #define RTC7301_10_DAY 0x8 /* Bank 0 and Band 1 */
31 #define RTC7301_1_MONTH 0x9 /* Bank 0 */
32 #define RTC7301_10_MONTH 0xa /* Bank 0 */
33 #define RTC7301_1_YEAR 0xb /* Bank 0 */
34 #define RTC7301_10_YEAR 0xc /* Bank 0 */
35 #define RTC7301_100_YEAR 0xd /* Bank 0 */
36 #define RTC7301_1000_YEAR 0xe /* Bank 0 */
37 #define RTC7301_ALARM_CONTROL 0xe /* Bank 1 */
38 #define RTC7301_ALARM_CONTROL_AIE BIT(0)
39 #define RTC7301_ALARM_CONTROL_AF BIT(1)
40 #define RTC7301_TIMER_CONTROL 0xe /* Bank 2 */
41 #define RTC7301_TIMER_CONTROL_TIE BIT(0)
42 #define RTC7301_TIMER_CONTROL_TF BIT(1)
43 #define RTC7301_CONTROL 0xf /* All banks */
44 #define RTC7301_CONTROL_BUSY BIT(0)
45 #define RTC7301_CONTROL_STOP BIT(1)
46 #define RTC7301_CONTROL_BANK_SEL_0 BIT(2)
47 #define RTC7301_CONTROL_BANK_SEL_1 BIT(3)
50 struct regmap
*regmap
;
56 static const struct regmap_config rtc7301_regmap_config
= {
62 static u8
rtc7301_read(struct rtc7301_priv
*priv
, unsigned int reg
)
64 int reg_stride
= regmap_get_reg_stride(priv
->regmap
);
67 regmap_read(priv
->regmap
, reg_stride
* reg
, &val
);
72 static void rtc7301_write(struct rtc7301_priv
*priv
, u8 val
, unsigned int reg
)
74 int reg_stride
= regmap_get_reg_stride(priv
->regmap
);
76 regmap_write(priv
->regmap
, reg_stride
* reg
, val
);
79 static void rtc7301_update_bits(struct rtc7301_priv
*priv
, unsigned int reg
,
82 int reg_stride
= regmap_get_reg_stride(priv
->regmap
);
84 regmap_update_bits(priv
->regmap
, reg_stride
* reg
, mask
, val
);
87 static int rtc7301_wait_while_busy(struct rtc7301_priv
*priv
)
91 while (retries
-- > 0) {
94 val
= rtc7301_read(priv
, RTC7301_CONTROL
);
95 if (!(val
& RTC7301_CONTROL_BUSY
))
98 usleep_range(200, 300);
104 static void rtc7301_stop(struct rtc7301_priv
*priv
)
106 rtc7301_update_bits(priv
, RTC7301_CONTROL
, RTC7301_CONTROL_STOP
,
107 RTC7301_CONTROL_STOP
);
110 static void rtc7301_start(struct rtc7301_priv
*priv
)
112 rtc7301_update_bits(priv
, RTC7301_CONTROL
, RTC7301_CONTROL_STOP
, 0);
115 static void rtc7301_select_bank(struct rtc7301_priv
*priv
, u8 bank
)
119 if (bank
== priv
->bank
)
123 val
|= RTC7301_CONTROL_BANK_SEL_0
;
125 val
|= RTC7301_CONTROL_BANK_SEL_1
;
127 rtc7301_update_bits(priv
, RTC7301_CONTROL
,
128 RTC7301_CONTROL_BANK_SEL_0
|
129 RTC7301_CONTROL_BANK_SEL_1
, val
);
134 static void rtc7301_get_time(struct rtc7301_priv
*priv
, struct rtc_time
*tm
,
139 tm
->tm_sec
= rtc7301_read(priv
, RTC7301_1_SEC
);
140 tm
->tm_sec
+= (rtc7301_read(priv
, RTC7301_10_SEC
) & ~RTC7301_AE
) * 10;
141 tm
->tm_min
= rtc7301_read(priv
, RTC7301_1_MIN
);
142 tm
->tm_min
+= (rtc7301_read(priv
, RTC7301_10_MIN
) & ~RTC7301_AE
) * 10;
143 tm
->tm_hour
= rtc7301_read(priv
, RTC7301_1_HOUR
);
144 tm
->tm_hour
+= (rtc7301_read(priv
, RTC7301_10_HOUR
) & ~RTC7301_AE
) * 10;
145 tm
->tm_mday
= rtc7301_read(priv
, RTC7301_1_DAY
);
146 tm
->tm_mday
+= (rtc7301_read(priv
, RTC7301_10_DAY
) & ~RTC7301_AE
) * 10;
157 tm
->tm_wday
= (rtc7301_read(priv
, RTC7301_DAY_OF_WEEK
) & ~RTC7301_AE
);
158 tm
->tm_mon
= rtc7301_read(priv
, RTC7301_10_MONTH
) * 10 +
159 rtc7301_read(priv
, RTC7301_1_MONTH
) - 1;
160 year
= rtc7301_read(priv
, RTC7301_1000_YEAR
) * 1000 +
161 rtc7301_read(priv
, RTC7301_100_YEAR
) * 100 +
162 rtc7301_read(priv
, RTC7301_10_YEAR
) * 10 +
163 rtc7301_read(priv
, RTC7301_1_YEAR
);
165 tm
->tm_year
= year
- 1900;
168 static void rtc7301_write_time(struct rtc7301_priv
*priv
, struct rtc_time
*tm
,
173 rtc7301_write(priv
, tm
->tm_sec
% 10, RTC7301_1_SEC
);
174 rtc7301_write(priv
, tm
->tm_sec
/ 10, RTC7301_10_SEC
);
176 rtc7301_write(priv
, tm
->tm_min
% 10, RTC7301_1_MIN
);
177 rtc7301_write(priv
, tm
->tm_min
/ 10, RTC7301_10_MIN
);
179 rtc7301_write(priv
, tm
->tm_hour
% 10, RTC7301_1_HOUR
);
180 rtc7301_write(priv
, tm
->tm_hour
/ 10, RTC7301_10_HOUR
);
182 rtc7301_write(priv
, tm
->tm_mday
% 10, RTC7301_1_DAY
);
183 rtc7301_write(priv
, tm
->tm_mday
/ 10, RTC7301_10_DAY
);
185 /* Don't care for alarm register */
186 rtc7301_write(priv
, alarm
? RTC7301_AE
: tm
->tm_wday
,
187 RTC7301_DAY_OF_WEEK
);
192 rtc7301_write(priv
, (tm
->tm_mon
+ 1) % 10, RTC7301_1_MONTH
);
193 rtc7301_write(priv
, (tm
->tm_mon
+ 1) / 10, RTC7301_10_MONTH
);
195 year
= tm
->tm_year
+ 1900;
197 rtc7301_write(priv
, year
% 10, RTC7301_1_YEAR
);
198 rtc7301_write(priv
, (year
/ 10) % 10, RTC7301_10_YEAR
);
199 rtc7301_write(priv
, (year
/ 100) % 10, RTC7301_100_YEAR
);
200 rtc7301_write(priv
, year
/ 1000, RTC7301_1000_YEAR
);
203 static void rtc7301_alarm_irq(struct rtc7301_priv
*priv
, unsigned int enabled
)
205 rtc7301_update_bits(priv
, RTC7301_ALARM_CONTROL
,
206 RTC7301_ALARM_CONTROL_AF
|
207 RTC7301_ALARM_CONTROL_AIE
,
208 enabled
? RTC7301_ALARM_CONTROL_AIE
: 0);
211 static int rtc7301_read_time(struct device
*dev
, struct rtc_time
*tm
)
213 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
217 spin_lock_irqsave(&priv
->lock
, flags
);
219 rtc7301_select_bank(priv
, 0);
221 err
= rtc7301_wait_while_busy(priv
);
223 rtc7301_get_time(priv
, tm
, false);
225 spin_unlock_irqrestore(&priv
->lock
, flags
);
227 return err
? err
: rtc_valid_tm(tm
);
230 static int rtc7301_set_time(struct device
*dev
, struct rtc_time
*tm
)
232 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
235 spin_lock_irqsave(&priv
->lock
, flags
);
238 usleep_range(200, 300);
239 rtc7301_select_bank(priv
, 0);
240 rtc7301_write_time(priv
, tm
, false);
243 spin_unlock_irqrestore(&priv
->lock
, flags
);
248 static int rtc7301_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
250 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
257 spin_lock_irqsave(&priv
->lock
, flags
);
259 rtc7301_select_bank(priv
, 1);
260 rtc7301_get_time(priv
, &alarm
->time
, true);
262 alrm_ctrl
= rtc7301_read(priv
, RTC7301_ALARM_CONTROL
);
264 alarm
->enabled
= !!(alrm_ctrl
& RTC7301_ALARM_CONTROL_AIE
);
265 alarm
->pending
= !!(alrm_ctrl
& RTC7301_ALARM_CONTROL_AF
);
267 spin_unlock_irqrestore(&priv
->lock
, flags
);
272 static int rtc7301_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
274 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
280 spin_lock_irqsave(&priv
->lock
, flags
);
282 rtc7301_select_bank(priv
, 1);
283 rtc7301_write_time(priv
, &alarm
->time
, true);
284 rtc7301_alarm_irq(priv
, alarm
->enabled
);
286 spin_unlock_irqrestore(&priv
->lock
, flags
);
291 static int rtc7301_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
293 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
299 spin_lock_irqsave(&priv
->lock
, flags
);
301 rtc7301_select_bank(priv
, 1);
302 rtc7301_alarm_irq(priv
, enabled
);
304 spin_unlock_irqrestore(&priv
->lock
, flags
);
309 static const struct rtc_class_ops rtc7301_rtc_ops
= {
310 .read_time
= rtc7301_read_time
,
311 .set_time
= rtc7301_set_time
,
312 .read_alarm
= rtc7301_read_alarm
,
313 .set_alarm
= rtc7301_set_alarm
,
314 .alarm_irq_enable
= rtc7301_alarm_irq_enable
,
317 static irqreturn_t
rtc7301_irq_handler(int irq
, void *dev_id
)
319 struct rtc_device
*rtc
= dev_id
;
320 struct rtc7301_priv
*priv
= dev_get_drvdata(rtc
->dev
.parent
);
322 irqreturn_t ret
= IRQ_NONE
;
325 spin_lock_irqsave(&priv
->lock
, flags
);
327 rtc7301_select_bank(priv
, 1);
329 alrm_ctrl
= rtc7301_read(priv
, RTC7301_ALARM_CONTROL
);
330 if (alrm_ctrl
& RTC7301_ALARM_CONTROL_AF
) {
332 rtc7301_alarm_irq(priv
, false);
333 rtc_update_irq(rtc
, 1, RTC_IRQF
| RTC_AF
);
336 spin_unlock_irqrestore(&priv
->lock
, flags
);
341 static void rtc7301_init(struct rtc7301_priv
*priv
)
345 spin_lock_irqsave(&priv
->lock
, flags
);
347 rtc7301_select_bank(priv
, 2);
348 rtc7301_write(priv
, 0, RTC7301_TIMER_CONTROL
);
350 spin_unlock_irqrestore(&priv
->lock
, flags
);
353 static int __init
rtc7301_rtc_probe(struct platform_device
*dev
)
355 struct resource
*res
;
357 struct rtc7301_priv
*priv
;
358 struct rtc_device
*rtc
;
361 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
365 priv
= devm_kzalloc(&dev
->dev
, sizeof(*priv
), GFP_KERNEL
);
369 regs
= devm_ioremap_resource(&dev
->dev
, res
);
371 return PTR_ERR(regs
);
373 priv
->regmap
= devm_regmap_init_mmio(&dev
->dev
, regs
,
374 &rtc7301_regmap_config
);
375 if (IS_ERR(priv
->regmap
))
376 return PTR_ERR(priv
->regmap
);
378 priv
->irq
= platform_get_irq(dev
, 0);
380 spin_lock_init(&priv
->lock
);
385 platform_set_drvdata(dev
, priv
);
387 rtc
= devm_rtc_device_register(&dev
->dev
, DRV_NAME
, &rtc7301_rtc_ops
,
393 ret
= devm_request_irq(&dev
->dev
, priv
->irq
,
394 rtc7301_irq_handler
, IRQF_SHARED
,
395 dev_name(&dev
->dev
), rtc
);
398 dev_err(&dev
->dev
, "unable to request IRQ\n");
400 device_set_wakeup_capable(&dev
->dev
, true);
407 #ifdef CONFIG_PM_SLEEP
409 static int rtc7301_suspend(struct device
*dev
)
411 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
413 if (device_may_wakeup(dev
))
414 enable_irq_wake(priv
->irq
);
419 static int rtc7301_resume(struct device
*dev
)
421 struct rtc7301_priv
*priv
= dev_get_drvdata(dev
);
423 if (device_may_wakeup(dev
))
424 disable_irq_wake(priv
->irq
);
431 static SIMPLE_DEV_PM_OPS(rtc7301_pm_ops
, rtc7301_suspend
, rtc7301_resume
);
433 static const struct of_device_id rtc7301_dt_match
[] = {
434 { .compatible
= "epson,rtc7301sf" },
435 { .compatible
= "epson,rtc7301dg" },
438 MODULE_DEVICE_TABLE(of
, rtc7301_dt_match
);
440 static struct platform_driver rtc7301_rtc_driver
= {
443 .of_match_table
= rtc7301_dt_match
,
444 .pm
= &rtc7301_pm_ops
,
448 module_platform_driver_probe(rtc7301_rtc_driver
, rtc7301_rtc_probe
);
450 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
451 MODULE_LICENSE("GPL");
452 MODULE_DESCRIPTION("EPSON TOYOCOM RTC-7301SF/DG Driver");
453 MODULE_ALIAS("platform:rtc-r7301");