drm/msm/dpu: remove RM topology definition
[linux/fpc-iii.git] / drivers / rtc / rtc-r7301.c
blob1943c815115266ff336690abdb0ac6331cb690fd
1 /*
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
9 */
11 #include <linux/io.h>
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)
50 struct rtc7301_priv {
51 struct regmap *regmap;
52 int irq;
53 spinlock_t lock;
54 u8 bank;
57 static const struct regmap_config rtc7301_regmap_config = {
58 .reg_bits = 32,
59 .val_bits = 8,
60 .reg_stride = 4,
63 static u8 rtc7301_read(struct rtc7301_priv *priv, unsigned int reg)
65 int reg_stride = regmap_get_reg_stride(priv->regmap);
66 unsigned int val;
68 regmap_read(priv->regmap, reg_stride * reg, &val);
70 return val & 0xf;
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,
81 u8 mask, u8 val)
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)
90 int retries = 100;
92 while (retries-- > 0) {
93 u8 val;
95 val = rtc7301_read(priv, RTC7301_CONTROL);
96 if (!(val & RTC7301_CONTROL_BUSY))
97 return 0;
99 udelay(300);
102 return -ETIMEDOUT;
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)
118 u8 val = 0;
120 if (bank == priv->bank)
121 return;
123 if (bank & BIT(0))
124 val |= RTC7301_CONTROL_BANK_SEL_0;
125 if (bank & BIT(1))
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);
132 priv->bank = bank;
135 static void rtc7301_get_time(struct rtc7301_priv *priv, struct rtc_time *tm,
136 bool alarm)
138 int year;
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;
149 if (alarm) {
150 tm->tm_wday = -1;
151 tm->tm_mon = -1;
152 tm->tm_year = -1;
153 tm->tm_yday = -1;
154 tm->tm_isdst = -1;
155 return;
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,
170 bool alarm)
172 int year;
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);
190 if (alarm)
191 return;
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);
215 unsigned long flags;
216 int err;
218 spin_lock_irqsave(&priv->lock, flags);
220 rtc7301_select_bank(priv, 0);
222 err = rtc7301_wait_while_busy(priv);
223 if (!err)
224 rtc7301_get_time(priv, tm, false);
226 spin_unlock_irqrestore(&priv->lock, flags);
228 return err;
231 static int rtc7301_set_time(struct device *dev, struct rtc_time *tm)
233 struct rtc7301_priv *priv = dev_get_drvdata(dev);
234 unsigned long flags;
236 spin_lock_irqsave(&priv->lock, flags);
238 rtc7301_stop(priv);
239 udelay(300);
240 rtc7301_select_bank(priv, 0);
241 rtc7301_write_time(priv, tm, false);
242 rtc7301_start(priv);
244 spin_unlock_irqrestore(&priv->lock, flags);
246 return 0;
249 static int rtc7301_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
251 struct rtc7301_priv *priv = dev_get_drvdata(dev);
252 unsigned long flags;
253 u8 alrm_ctrl;
255 if (priv->irq <= 0)
256 return -EINVAL;
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);
270 return 0;
273 static int rtc7301_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
275 struct rtc7301_priv *priv = dev_get_drvdata(dev);
276 unsigned long flags;
278 if (priv->irq <= 0)
279 return -EINVAL;
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);
289 return 0;
292 static int rtc7301_alarm_irq_enable(struct device *dev, unsigned int enabled)
294 struct rtc7301_priv *priv = dev_get_drvdata(dev);
295 unsigned long flags;
297 if (priv->irq <= 0)
298 return -EINVAL;
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);
307 return 0;
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);
322 unsigned long flags;
323 irqreturn_t ret = IRQ_NONE;
324 u8 alrm_ctrl;
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) {
332 ret = IRQ_HANDLED;
333 rtc7301_alarm_irq(priv, false);
334 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
337 spin_unlock_irqrestore(&priv->lock, flags);
339 return ret;
342 static void rtc7301_init(struct rtc7301_priv *priv)
344 unsigned long flags;
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;
357 void __iomem *regs;
358 struct rtc7301_priv *priv;
359 struct rtc_device *rtc;
360 int ret;
362 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
363 if (!res)
364 return -ENODEV;
366 priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
367 if (!priv)
368 return -ENOMEM;
370 regs = devm_ioremap_resource(&dev->dev, res);
371 if (IS_ERR(regs))
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);
382 priv->bank = -1;
384 rtc7301_init(priv);
386 platform_set_drvdata(dev, priv);
388 rtc = devm_rtc_device_register(&dev->dev, DRV_NAME, &rtc7301_rtc_ops,
389 THIS_MODULE);
390 if (IS_ERR(rtc))
391 return PTR_ERR(rtc);
393 if (priv->irq > 0) {
394 ret = devm_request_irq(&dev->dev, priv->irq,
395 rtc7301_irq_handler, IRQF_SHARED,
396 dev_name(&dev->dev), rtc);
397 if (ret) {
398 priv->irq = 0;
399 dev_err(&dev->dev, "unable to request IRQ\n");
400 } else {
401 device_set_wakeup_capable(&dev->dev, true);
405 return 0;
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);
417 return 0;
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);
427 return 0;
430 #endif
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 = {
442 .driver = {
443 .name = DRV_NAME,
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");