blob: bf502fcd6077281ac44f15cc67140f48fa8535bf [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * TI OMAP Real Time Clock interface for Linux
4 *
5 * Copyright (C) 2003 MontaVista Software, Inc.
6 * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
7 *
8 * Copyright (C) 2006 David Brownell (new RTC framework)
9 * Copyright (C) 2014 Johan Hovold <johan@kernel.org>
10 */
11
12#include <dt-bindings/gpio/gpio.h>
13#include <linux/bcd.h>
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/init.h>
17#include <linux/io.h>
18#include <linux/ioport.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/pinctrl/pinctrl.h>
24#include <linux/pinctrl/pinconf.h>
25#include <linux/pinctrl/pinconf-generic.h>
26#include <linux/platform_device.h>
27#include <linux/pm_runtime.h>
28#include <linux/rtc.h>
29#include <linux/rtc/rtc-omap.h>
30
31/*
32 * The OMAP RTC is a year/month/day/hours/minutes/seconds BCD clock
33 * with century-range alarm matching, driven by the 32kHz clock.
34 *
35 * The main user-visible ways it differs from PC RTCs are by omitting
36 * "don't care" alarm fields and sub-second periodic IRQs, and having
37 * an autoadjust mechanism to calibrate to the true oscillator rate.
38 *
39 * Board-specific wiring options include using split power mode with
40 * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
41 * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
42 * low power modes) for OMAP1 boards (OMAP-L138 has this built into
43 * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
44 */
45
46/* RTC registers */
47#define OMAP_RTC_SECONDS_REG 0x00
48#define OMAP_RTC_MINUTES_REG 0x04
49#define OMAP_RTC_HOURS_REG 0x08
50#define OMAP_RTC_DAYS_REG 0x0C
51#define OMAP_RTC_MONTHS_REG 0x10
52#define OMAP_RTC_YEARS_REG 0x14
53#define OMAP_RTC_WEEKS_REG 0x18
54
55#define OMAP_RTC_ALARM_SECONDS_REG 0x20
56#define OMAP_RTC_ALARM_MINUTES_REG 0x24
57#define OMAP_RTC_ALARM_HOURS_REG 0x28
58#define OMAP_RTC_ALARM_DAYS_REG 0x2c
59#define OMAP_RTC_ALARM_MONTHS_REG 0x30
60#define OMAP_RTC_ALARM_YEARS_REG 0x34
61
62#define OMAP_RTC_CTRL_REG 0x40
63#define OMAP_RTC_STATUS_REG 0x44
64#define OMAP_RTC_INTERRUPTS_REG 0x48
65
66#define OMAP_RTC_COMP_LSB_REG 0x4c
67#define OMAP_RTC_COMP_MSB_REG 0x50
68#define OMAP_RTC_OSC_REG 0x54
69
70#define OMAP_RTC_SCRATCH0_REG 0x60
71#define OMAP_RTC_SCRATCH1_REG 0x64
72#define OMAP_RTC_SCRATCH2_REG 0x68
73
74#define OMAP_RTC_KICK0_REG 0x6c
75#define OMAP_RTC_KICK1_REG 0x70
76
77#define OMAP_RTC_IRQWAKEEN 0x7c
78
79#define OMAP_RTC_ALARM2_SECONDS_REG 0x80
80#define OMAP_RTC_ALARM2_MINUTES_REG 0x84
81#define OMAP_RTC_ALARM2_HOURS_REG 0x88
82#define OMAP_RTC_ALARM2_DAYS_REG 0x8c
83#define OMAP_RTC_ALARM2_MONTHS_REG 0x90
84#define OMAP_RTC_ALARM2_YEARS_REG 0x94
85
86#define OMAP_RTC_PMIC_REG 0x98
87
88/* OMAP_RTC_CTRL_REG bit fields: */
89#define OMAP_RTC_CTRL_SPLIT BIT(7)
90#define OMAP_RTC_CTRL_DISABLE BIT(6)
91#define OMAP_RTC_CTRL_SET_32_COUNTER BIT(5)
92#define OMAP_RTC_CTRL_TEST BIT(4)
93#define OMAP_RTC_CTRL_MODE_12_24 BIT(3)
94#define OMAP_RTC_CTRL_AUTO_COMP BIT(2)
95#define OMAP_RTC_CTRL_ROUND_30S BIT(1)
96#define OMAP_RTC_CTRL_STOP BIT(0)
97
98/* OMAP_RTC_STATUS_REG bit fields: */
99#define OMAP_RTC_STATUS_POWER_UP BIT(7)
100#define OMAP_RTC_STATUS_ALARM2 BIT(7)
101#define OMAP_RTC_STATUS_ALARM BIT(6)
102#define OMAP_RTC_STATUS_1D_EVENT BIT(5)
103#define OMAP_RTC_STATUS_1H_EVENT BIT(4)
104#define OMAP_RTC_STATUS_1M_EVENT BIT(3)
105#define OMAP_RTC_STATUS_1S_EVENT BIT(2)
106#define OMAP_RTC_STATUS_RUN BIT(1)
107#define OMAP_RTC_STATUS_BUSY BIT(0)
108
109/* OMAP_RTC_INTERRUPTS_REG bit fields: */
110#define OMAP_RTC_INTERRUPTS_IT_ALARM2 BIT(4)
111#define OMAP_RTC_INTERRUPTS_IT_ALARM BIT(3)
112#define OMAP_RTC_INTERRUPTS_IT_TIMER BIT(2)
113
114/* OMAP_RTC_OSC_REG bit fields: */
115#define OMAP_RTC_OSC_32KCLK_EN BIT(6)
116#define OMAP_RTC_OSC_SEL_32KCLK_SRC BIT(3)
117#define OMAP_RTC_OSC_OSC32K_GZ_DISABLE BIT(4)
118
119/* OMAP_RTC_IRQWAKEEN bit fields: */
120#define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN BIT(1)
121
122/* OMAP_RTC_PMIC bit fields: */
123#define OMAP_RTC_PMIC_POWER_EN_EN BIT(16)
124#define OMAP_RTC_PMIC_EXT_WKUP_EN(x) BIT(x)
125#define OMAP_RTC_PMIC_EXT_WKUP_POL(x) BIT(4 + x)
126
127/* OMAP_RTC_KICKER values */
128#define KICK0_VALUE 0x83e70b13
129#define KICK1_VALUE 0x95a4f1e0
130
131struct omap_rtc;
132
133struct omap_rtc_device_type {
134 bool has_32kclk_en;
135 bool has_irqwakeen;
136 bool has_pmic_mode;
137 bool has_power_up_reset;
138 void (*lock)(struct omap_rtc *rtc);
139 void (*unlock)(struct omap_rtc *rtc);
140};
141
142struct omap_rtc {
143 struct rtc_device *rtc;
144 void __iomem *base;
145 struct clk *clk;
146 int irq_alarm;
147 int irq_timer;
148 u8 interrupts_reg;
149 bool is_pmic_controller;
150 bool has_ext_clk;
151 bool is_suspending;
152 const struct omap_rtc_device_type *type;
153 struct pinctrl_dev *pctldev;
154};
155
156static inline u8 rtc_read(struct omap_rtc *rtc, unsigned int reg)
157{
158 return readb(rtc->base + reg);
159}
160
161static inline u32 rtc_readl(struct omap_rtc *rtc, unsigned int reg)
162{
163 return readl(rtc->base + reg);
164}
165
166static inline void rtc_write(struct omap_rtc *rtc, unsigned int reg, u8 val)
167{
168 writeb(val, rtc->base + reg);
169}
170
171static inline void rtc_writel(struct omap_rtc *rtc, unsigned int reg, u32 val)
172{
173 writel(val, rtc->base + reg);
174}
175
176static void am3352_rtc_unlock(struct omap_rtc *rtc)
177{
178 rtc_writel(rtc, OMAP_RTC_KICK0_REG, KICK0_VALUE);
179 rtc_writel(rtc, OMAP_RTC_KICK1_REG, KICK1_VALUE);
180}
181
182static void am3352_rtc_lock(struct omap_rtc *rtc)
183{
184 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
185 rtc_writel(rtc, OMAP_RTC_KICK1_REG, 0);
186}
187
188static void default_rtc_unlock(struct omap_rtc *rtc)
189{
190}
191
192static void default_rtc_lock(struct omap_rtc *rtc)
193{
194}
195
196/*
197 * We rely on the rtc framework to handle locking (rtc->ops_lock),
198 * so the only other requirement is that register accesses which
199 * require BUSY to be clear are made with IRQs locally disabled
200 */
201static void rtc_wait_not_busy(struct omap_rtc *rtc)
202{
203 int count;
204 u8 status;
205
206 /* BUSY may stay active for 1/32768 second (~30 usec) */
207 for (count = 0; count < 50; count++) {
208 status = rtc_read(rtc, OMAP_RTC_STATUS_REG);
209 if (!(status & OMAP_RTC_STATUS_BUSY))
210 break;
211 udelay(1);
212 }
213 /* now we have ~15 usec to read/write various registers */
214}
215
216static irqreturn_t rtc_irq(int irq, void *dev_id)
217{
218 struct omap_rtc *rtc = dev_id;
219 unsigned long events = 0;
220 u8 irq_data;
221
222 irq_data = rtc_read(rtc, OMAP_RTC_STATUS_REG);
223
224 /* alarm irq? */
225 if (irq_data & OMAP_RTC_STATUS_ALARM) {
226 rtc->type->unlock(rtc);
227 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
228 rtc->type->lock(rtc);
229 events |= RTC_IRQF | RTC_AF;
230 }
231
232 /* 1/sec periodic/update irq? */
233 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
234 events |= RTC_IRQF | RTC_UF;
235
236 rtc_update_irq(rtc->rtc, 1, events);
237
238 return IRQ_HANDLED;
239}
240
241static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
242{
243 struct omap_rtc *rtc = dev_get_drvdata(dev);
244 u8 reg, irqwake_reg = 0;
245
246 local_irq_disable();
247 rtc_wait_not_busy(rtc);
248 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
249 if (rtc->type->has_irqwakeen)
250 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
251
252 if (enabled) {
253 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
254 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
255 } else {
256 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
257 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
258 }
259 rtc_wait_not_busy(rtc);
260 rtc->type->unlock(rtc);
261 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
262 if (rtc->type->has_irqwakeen)
263 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
264 rtc->type->lock(rtc);
265 local_irq_enable();
266
267 return 0;
268}
269
270/* this hardware doesn't support "don't care" alarm fields */
271static void tm2bcd(struct rtc_time *tm)
272{
273 tm->tm_sec = bin2bcd(tm->tm_sec);
274 tm->tm_min = bin2bcd(tm->tm_min);
275 tm->tm_hour = bin2bcd(tm->tm_hour);
276 tm->tm_mday = bin2bcd(tm->tm_mday);
277
278 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
279 tm->tm_year = bin2bcd(tm->tm_year - 100);
280}
281
282static void bcd2tm(struct rtc_time *tm)
283{
284 tm->tm_sec = bcd2bin(tm->tm_sec);
285 tm->tm_min = bcd2bin(tm->tm_min);
286 tm->tm_hour = bcd2bin(tm->tm_hour);
287 tm->tm_mday = bcd2bin(tm->tm_mday);
288 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
289 /* epoch == 1900 */
290 tm->tm_year = bcd2bin(tm->tm_year) + 100;
291}
292
293static void omap_rtc_read_time_raw(struct omap_rtc *rtc, struct rtc_time *tm)
294{
295 tm->tm_sec = rtc_read(rtc, OMAP_RTC_SECONDS_REG);
296 tm->tm_min = rtc_read(rtc, OMAP_RTC_MINUTES_REG);
297 tm->tm_hour = rtc_read(rtc, OMAP_RTC_HOURS_REG);
298 tm->tm_mday = rtc_read(rtc, OMAP_RTC_DAYS_REG);
299 tm->tm_mon = rtc_read(rtc, OMAP_RTC_MONTHS_REG);
300 tm->tm_year = rtc_read(rtc, OMAP_RTC_YEARS_REG);
301}
302
303static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
304{
305 struct omap_rtc *rtc = dev_get_drvdata(dev);
306
307 /* we don't report wday/yday/isdst ... */
308 local_irq_disable();
309 rtc_wait_not_busy(rtc);
310 omap_rtc_read_time_raw(rtc, tm);
311 local_irq_enable();
312
313 bcd2tm(tm);
314
315 return 0;
316}
317
318static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
319{
320 struct omap_rtc *rtc = dev_get_drvdata(dev);
321
322 tm2bcd(tm);
323
324 local_irq_disable();
325 rtc_wait_not_busy(rtc);
326
327 rtc->type->unlock(rtc);
328 rtc_write(rtc, OMAP_RTC_YEARS_REG, tm->tm_year);
329 rtc_write(rtc, OMAP_RTC_MONTHS_REG, tm->tm_mon);
330 rtc_write(rtc, OMAP_RTC_DAYS_REG, tm->tm_mday);
331 rtc_write(rtc, OMAP_RTC_HOURS_REG, tm->tm_hour);
332 rtc_write(rtc, OMAP_RTC_MINUTES_REG, tm->tm_min);
333 rtc_write(rtc, OMAP_RTC_SECONDS_REG, tm->tm_sec);
334 rtc->type->lock(rtc);
335
336 local_irq_enable();
337
338 return 0;
339}
340
341static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
342{
343 struct omap_rtc *rtc = dev_get_drvdata(dev);
344 u8 interrupts;
345
346 local_irq_disable();
347 rtc_wait_not_busy(rtc);
348
349 alm->time.tm_sec = rtc_read(rtc, OMAP_RTC_ALARM_SECONDS_REG);
350 alm->time.tm_min = rtc_read(rtc, OMAP_RTC_ALARM_MINUTES_REG);
351 alm->time.tm_hour = rtc_read(rtc, OMAP_RTC_ALARM_HOURS_REG);
352 alm->time.tm_mday = rtc_read(rtc, OMAP_RTC_ALARM_DAYS_REG);
353 alm->time.tm_mon = rtc_read(rtc, OMAP_RTC_ALARM_MONTHS_REG);
354 alm->time.tm_year = rtc_read(rtc, OMAP_RTC_ALARM_YEARS_REG);
355
356 local_irq_enable();
357
358 bcd2tm(&alm->time);
359
360 interrupts = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
361 alm->enabled = !!(interrupts & OMAP_RTC_INTERRUPTS_IT_ALARM);
362
363 return 0;
364}
365
366static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
367{
368 struct omap_rtc *rtc = dev_get_drvdata(dev);
369 u8 reg, irqwake_reg = 0;
370
371 tm2bcd(&alm->time);
372
373 local_irq_disable();
374 rtc_wait_not_busy(rtc);
375
376 rtc->type->unlock(rtc);
377 rtc_write(rtc, OMAP_RTC_ALARM_YEARS_REG, alm->time.tm_year);
378 rtc_write(rtc, OMAP_RTC_ALARM_MONTHS_REG, alm->time.tm_mon);
379 rtc_write(rtc, OMAP_RTC_ALARM_DAYS_REG, alm->time.tm_mday);
380 rtc_write(rtc, OMAP_RTC_ALARM_HOURS_REG, alm->time.tm_hour);
381 rtc_write(rtc, OMAP_RTC_ALARM_MINUTES_REG, alm->time.tm_min);
382 rtc_write(rtc, OMAP_RTC_ALARM_SECONDS_REG, alm->time.tm_sec);
383
384 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
385 if (rtc->type->has_irqwakeen)
386 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
387
388 if (alm->enabled) {
389 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
390 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
391 } else {
392 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
393 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
394 }
395 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
396 if (rtc->type->has_irqwakeen)
397 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
398 rtc->type->lock(rtc);
399
400 local_irq_enable();
401
402 return 0;
403}
404
405static struct omap_rtc *omap_rtc_power_off_rtc;
406
407/**
408 * omap_rtc_power_off_program: Set the pmic power off sequence. The RTC
409 * generates pmic_pwr_enable control, which can be used to control an external
410 * PMIC.
411 */
412int omap_rtc_power_off_program(struct device *dev)
413{
414 struct omap_rtc *rtc = omap_rtc_power_off_rtc;
415 struct rtc_time tm;
416 unsigned long now;
417 int seconds;
418 u32 val;
419
420 rtc->type->unlock(rtc);
421 /* enable pmic_power_en control */
422 val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
423 rtc_writel(rtc, OMAP_RTC_PMIC_REG, val | OMAP_RTC_PMIC_POWER_EN_EN);
424
425again:
426 /* Clear any existing ALARM2 event */
427 rtc_writel(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM2);
428
429 /* set alarm one second from now */
430 omap_rtc_read_time_raw(rtc, &tm);
431 seconds = tm.tm_sec;
432 bcd2tm(&tm);
433 now = rtc_tm_to_time64(&tm);
434 rtc_time64_to_tm(now + 1, &tm);
435
436 tm2bcd(&tm);
437
438 rtc_wait_not_busy(rtc);
439
440 rtc_write(rtc, OMAP_RTC_ALARM2_SECONDS_REG, tm.tm_sec);
441 rtc_write(rtc, OMAP_RTC_ALARM2_MINUTES_REG, tm.tm_min);
442 rtc_write(rtc, OMAP_RTC_ALARM2_HOURS_REG, tm.tm_hour);
443 rtc_write(rtc, OMAP_RTC_ALARM2_DAYS_REG, tm.tm_mday);
444 rtc_write(rtc, OMAP_RTC_ALARM2_MONTHS_REG, tm.tm_mon);
445 rtc_write(rtc, OMAP_RTC_ALARM2_YEARS_REG, tm.tm_year);
446
447 /*
448 * enable ALARM2 interrupt
449 *
450 * NOTE: this fails on AM3352 if rtc_write (writeb) is used
451 */
452 val = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
453 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG,
454 val | OMAP_RTC_INTERRUPTS_IT_ALARM2);
455
456 /* Retry in case roll over happened before alarm was armed. */
457 if (rtc_read(rtc, OMAP_RTC_SECONDS_REG) != seconds) {
458 val = rtc_read(rtc, OMAP_RTC_STATUS_REG);
459 if (!(val & OMAP_RTC_STATUS_ALARM2))
460 goto again;
461 }
462
463 rtc->type->lock(rtc);
464
465 return 0;
466}
467EXPORT_SYMBOL(omap_rtc_power_off_program);
468
469/*
470 * omap_rtc_poweroff: RTC-controlled power off
471 *
472 * The RTC can be used to control an external PMIC via the pmic_power_en pin,
473 * which can be configured to transition to OFF on ALARM2 events.
474 *
475 * Notes:
476 * The one-second alarm offset is the shortest offset possible as the alarm
477 * registers must be set before the next timer update and the offset
478 * calculation is too heavy for everything to be done within a single access
479 * period (~15 us).
480 *
481 * Called with local interrupts disabled.
482 */
483static void omap_rtc_power_off(void)
484{
485 struct rtc_device *rtc = omap_rtc_power_off_rtc->rtc;
486 u32 val;
487
488 omap_rtc_power_off_program(rtc->dev.parent);
489
490 /* Set PMIC power enable and EXT_WAKEUP in case PB power on is used */
491 omap_rtc_power_off_rtc->type->unlock(omap_rtc_power_off_rtc);
492 val = rtc_readl(omap_rtc_power_off_rtc, OMAP_RTC_PMIC_REG);
493 val |= OMAP_RTC_PMIC_POWER_EN_EN | OMAP_RTC_PMIC_EXT_WKUP_POL(0) |
494 OMAP_RTC_PMIC_EXT_WKUP_EN(0);
495 rtc_writel(omap_rtc_power_off_rtc, OMAP_RTC_PMIC_REG, val);
496 omap_rtc_power_off_rtc->type->lock(omap_rtc_power_off_rtc);
497
498 /*
499 * Wait for alarm to trigger (within one second) and external PMIC to
500 * power off the system. Add a 500 ms margin for external latencies
501 * (e.g. debounce circuits).
502 */
503 mdelay(1500);
504}
505
506static const struct rtc_class_ops omap_rtc_ops = {
507 .read_time = omap_rtc_read_time,
508 .set_time = omap_rtc_set_time,
509 .read_alarm = omap_rtc_read_alarm,
510 .set_alarm = omap_rtc_set_alarm,
511 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
512};
513
514static const struct omap_rtc_device_type omap_rtc_default_type = {
515 .has_power_up_reset = true,
516 .lock = default_rtc_lock,
517 .unlock = default_rtc_unlock,
518};
519
520static const struct omap_rtc_device_type omap_rtc_am3352_type = {
521 .has_32kclk_en = true,
522 .has_irqwakeen = true,
523 .has_pmic_mode = true,
524 .lock = am3352_rtc_lock,
525 .unlock = am3352_rtc_unlock,
526};
527
528static const struct omap_rtc_device_type omap_rtc_da830_type = {
529 .lock = am3352_rtc_lock,
530 .unlock = am3352_rtc_unlock,
531};
532
533static const struct platform_device_id omap_rtc_id_table[] = {
534 {
535 .name = "omap_rtc",
536 .driver_data = (kernel_ulong_t)&omap_rtc_default_type,
537 }, {
538 .name = "am3352-rtc",
539 .driver_data = (kernel_ulong_t)&omap_rtc_am3352_type,
540 }, {
541 .name = "da830-rtc",
542 .driver_data = (kernel_ulong_t)&omap_rtc_da830_type,
543 }, {
544 /* sentinel */
545 }
546};
547MODULE_DEVICE_TABLE(platform, omap_rtc_id_table);
548
549static const struct of_device_id omap_rtc_of_match[] = {
550 {
551 .compatible = "ti,am3352-rtc",
552 .data = &omap_rtc_am3352_type,
553 }, {
554 .compatible = "ti,da830-rtc",
555 .data = &omap_rtc_da830_type,
556 }, {
557 /* sentinel */
558 }
559};
560MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
561
562static const struct pinctrl_pin_desc rtc_pins_desc[] = {
563 PINCTRL_PIN(0, "ext_wakeup0"),
564 PINCTRL_PIN(1, "ext_wakeup1"),
565 PINCTRL_PIN(2, "ext_wakeup2"),
566 PINCTRL_PIN(3, "ext_wakeup3"),
567};
568
569static int rtc_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
570{
571 return 0;
572}
573
574static const char *rtc_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
575 unsigned int group)
576{
577 return NULL;
578}
579
580static const struct pinctrl_ops rtc_pinctrl_ops = {
581 .get_groups_count = rtc_pinctrl_get_groups_count,
582 .get_group_name = rtc_pinctrl_get_group_name,
583 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
584 .dt_free_map = pinconf_generic_dt_free_map,
585};
586
587#define PIN_CONFIG_ACTIVE_HIGH (PIN_CONFIG_END + 1)
588
589static const struct pinconf_generic_params rtc_params[] = {
590 {"ti,active-high", PIN_CONFIG_ACTIVE_HIGH, 0},
591};
592
593#ifdef CONFIG_DEBUG_FS
594static const struct pin_config_item rtc_conf_items[ARRAY_SIZE(rtc_params)] = {
595 PCONFDUMP(PIN_CONFIG_ACTIVE_HIGH, "input active high", NULL, false),
596};
597#endif
598
599static int rtc_pinconf_get(struct pinctrl_dev *pctldev,
600 unsigned int pin, unsigned long *config)
601{
602 struct omap_rtc *rtc = pinctrl_dev_get_drvdata(pctldev);
603 unsigned int param = pinconf_to_config_param(*config);
604 u32 val;
605 u16 arg = 0;
606
607 val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
608
609 switch (param) {
610 case PIN_CONFIG_INPUT_ENABLE:
611 if (!(val & OMAP_RTC_PMIC_EXT_WKUP_EN(pin)))
612 return -EINVAL;
613 break;
614 case PIN_CONFIG_ACTIVE_HIGH:
615 if (val & OMAP_RTC_PMIC_EXT_WKUP_POL(pin))
616 return -EINVAL;
617 break;
618 default:
619 return -ENOTSUPP;
620 };
621
622 *config = pinconf_to_config_packed(param, arg);
623
624 return 0;
625}
626
627static int rtc_pinconf_set(struct pinctrl_dev *pctldev,
628 unsigned int pin, unsigned long *configs,
629 unsigned int num_configs)
630{
631 struct omap_rtc *rtc = pinctrl_dev_get_drvdata(pctldev);
632 u32 val;
633 unsigned int param;
634 u32 param_val;
635 int i;
636
637 val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
638
639 /* active low by default */
640 val |= OMAP_RTC_PMIC_EXT_WKUP_POL(pin);
641
642 for (i = 0; i < num_configs; i++) {
643 param = pinconf_to_config_param(configs[i]);
644 param_val = pinconf_to_config_argument(configs[i]);
645
646 switch (param) {
647 case PIN_CONFIG_INPUT_ENABLE:
648 if (param_val)
649 val |= OMAP_RTC_PMIC_EXT_WKUP_EN(pin);
650 else
651 val &= ~OMAP_RTC_PMIC_EXT_WKUP_EN(pin);
652 break;
653 case PIN_CONFIG_ACTIVE_HIGH:
654 val &= ~OMAP_RTC_PMIC_EXT_WKUP_POL(pin);
655 break;
656 default:
657 dev_err(&rtc->rtc->dev, "Property %u not supported\n",
658 param);
659 return -ENOTSUPP;
660 }
661 }
662
663 rtc->type->unlock(rtc);
664 rtc_writel(rtc, OMAP_RTC_PMIC_REG, val);
665 rtc->type->lock(rtc);
666
667 return 0;
668}
669
670static const struct pinconf_ops rtc_pinconf_ops = {
671 .is_generic = true,
672 .pin_config_get = rtc_pinconf_get,
673 .pin_config_set = rtc_pinconf_set,
674};
675
676static struct pinctrl_desc rtc_pinctrl_desc = {
677 .pins = rtc_pins_desc,
678 .npins = ARRAY_SIZE(rtc_pins_desc),
679 .pctlops = &rtc_pinctrl_ops,
680 .confops = &rtc_pinconf_ops,
681 .custom_params = rtc_params,
682 .num_custom_params = ARRAY_SIZE(rtc_params),
683#ifdef CONFIG_DEBUG_FS
684 .custom_conf_items = rtc_conf_items,
685#endif
686 .owner = THIS_MODULE,
687};
688
689static int omap_rtc_scratch_read(void *priv, unsigned int offset, void *_val,
690 size_t bytes)
691{
692 struct omap_rtc *rtc = priv;
693 u32 *val = _val;
694 int i;
695
696 for (i = 0; i < bytes / 4; i++)
697 val[i] = rtc_readl(rtc,
698 OMAP_RTC_SCRATCH0_REG + offset + (i * 4));
699
700 return 0;
701}
702
703static int omap_rtc_scratch_write(void *priv, unsigned int offset, void *_val,
704 size_t bytes)
705{
706 struct omap_rtc *rtc = priv;
707 u32 *val = _val;
708 int i;
709
710 rtc->type->unlock(rtc);
711 for (i = 0; i < bytes / 4; i++)
712 rtc_writel(rtc,
713 OMAP_RTC_SCRATCH0_REG + offset + (i * 4), val[i]);
714 rtc->type->lock(rtc);
715
716 return 0;
717}
718
719static struct nvmem_config omap_rtc_nvmem_config = {
720 .name = "omap_rtc_scratch",
721 .word_size = 4,
722 .stride = 4,
723 .size = OMAP_RTC_KICK0_REG - OMAP_RTC_SCRATCH0_REG,
724 .reg_read = omap_rtc_scratch_read,
725 .reg_write = omap_rtc_scratch_write,
726};
727
728static int omap_rtc_probe(struct platform_device *pdev)
729{
730 struct omap_rtc *rtc;
731 struct resource *res;
732 u8 reg, mask, new_ctrl;
733 const struct platform_device_id *id_entry;
734 const struct of_device_id *of_id;
735 int ret;
736
737 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
738 if (!rtc)
739 return -ENOMEM;
740
741 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
742 if (of_id) {
743 rtc->type = of_id->data;
744 rtc->is_pmic_controller = rtc->type->has_pmic_mode &&
745 of_device_is_system_power_controller(pdev->dev.of_node);
746 } else {
747 id_entry = platform_get_device_id(pdev);
748 rtc->type = (void *)id_entry->driver_data;
749 }
750
751 rtc->irq_timer = platform_get_irq(pdev, 0);
752 if (rtc->irq_timer <= 0)
753 return -ENOENT;
754
755 rtc->irq_alarm = platform_get_irq(pdev, 1);
756 if (rtc->irq_alarm <= 0)
757 return -ENOENT;
758
759 rtc->clk = devm_clk_get(&pdev->dev, "ext-clk");
760 if (!IS_ERR(rtc->clk))
761 rtc->has_ext_clk = true;
762 else
763 rtc->clk = devm_clk_get(&pdev->dev, "int-clk");
764
765 if (!IS_ERR(rtc->clk))
766 clk_prepare_enable(rtc->clk);
767
768 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
769 rtc->base = devm_ioremap_resource(&pdev->dev, res);
770 if (IS_ERR(rtc->base)) {
771 clk_disable_unprepare(rtc->clk);
772 return PTR_ERR(rtc->base);
773 }
774
775 platform_set_drvdata(pdev, rtc);
776
777 /* Enable the clock/module so that we can access the registers */
778 pm_runtime_enable(&pdev->dev);
779 pm_runtime_get_sync(&pdev->dev);
780
781 rtc->type->unlock(rtc);
782
783 /*
784 * disable interrupts
785 *
786 * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
787 */
788 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
789
790 /* enable RTC functional clock */
791 if (rtc->type->has_32kclk_en) {
792 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
793 rtc_writel(rtc, OMAP_RTC_OSC_REG,
794 reg | OMAP_RTC_OSC_32KCLK_EN);
795 }
796
797 /* clear old status */
798 reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
799
800 mask = OMAP_RTC_STATUS_ALARM;
801
802 if (rtc->type->has_pmic_mode)
803 mask |= OMAP_RTC_STATUS_ALARM2;
804
805 if (rtc->type->has_power_up_reset) {
806 mask |= OMAP_RTC_STATUS_POWER_UP;
807 if (reg & OMAP_RTC_STATUS_POWER_UP)
808 dev_info(&pdev->dev, "RTC power up reset detected\n");
809 }
810
811 if (reg & mask)
812 rtc_write(rtc, OMAP_RTC_STATUS_REG, reg & mask);
813
814 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
815 reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
816 if (reg & OMAP_RTC_CTRL_STOP)
817 dev_info(&pdev->dev, "already running\n");
818
819 /* force to 24 hour mode */
820 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT | OMAP_RTC_CTRL_AUTO_COMP);
821 new_ctrl |= OMAP_RTC_CTRL_STOP;
822
823 /*
824 * BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
825 *
826 * - Device wake-up capability setting should come through chip
827 * init logic. OMAP1 boards should initialize the "wakeup capable"
828 * flag in the platform device if the board is wired right for
829 * being woken up by RTC alarm. For OMAP-L138, this capability
830 * is built into the SoC by the "Deep Sleep" capability.
831 *
832 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
833 * rather than nPWRON_RESET, should forcibly enable split
834 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
835 * is write-only, and always reads as zero...)
836 */
837
838 if (new_ctrl & OMAP_RTC_CTRL_SPLIT)
839 dev_info(&pdev->dev, "split power mode\n");
840
841 if (reg != new_ctrl)
842 rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
843
844 /*
845 * If we have the external clock then switch to it so we can keep
846 * ticking across suspend.
847 */
848 if (rtc->has_ext_clk) {
849 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
850 reg &= ~OMAP_RTC_OSC_OSC32K_GZ_DISABLE;
851 reg |= OMAP_RTC_OSC_32KCLK_EN | OMAP_RTC_OSC_SEL_32KCLK_SRC;
852 rtc_writel(rtc, OMAP_RTC_OSC_REG, reg);
853 }
854
855 rtc->type->lock(rtc);
856
857 device_init_wakeup(&pdev->dev, true);
858
859 rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
860 if (IS_ERR(rtc->rtc)) {
861 ret = PTR_ERR(rtc->rtc);
862 goto err;
863 }
864
865 rtc->rtc->ops = &omap_rtc_ops;
866 rtc->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
867 rtc->rtc->range_max = RTC_TIMESTAMP_END_2099;
868 omap_rtc_nvmem_config.priv = rtc;
869
870 /* handle periodic and alarm irqs */
871 ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
872 dev_name(&rtc->rtc->dev), rtc);
873 if (ret)
874 goto err;
875
876 if (rtc->irq_timer != rtc->irq_alarm) {
877 ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
878 dev_name(&rtc->rtc->dev), rtc);
879 if (ret)
880 goto err;
881 }
882
883 /* Support ext_wakeup pinconf */
884 rtc_pinctrl_desc.name = dev_name(&pdev->dev);
885
886 rtc->pctldev = pinctrl_register(&rtc_pinctrl_desc, &pdev->dev, rtc);
887 if (IS_ERR(rtc->pctldev)) {
888 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
889 ret = PTR_ERR(rtc->pctldev);
890 goto err;
891 }
892
893 ret = rtc_register_device(rtc->rtc);
894 if (ret)
895 goto err_deregister_pinctrl;
896
897 rtc_nvmem_register(rtc->rtc, &omap_rtc_nvmem_config);
898
899 if (rtc->is_pmic_controller) {
900 if (!pm_power_off) {
901 omap_rtc_power_off_rtc = rtc;
902 pm_power_off = omap_rtc_power_off;
903 }
904 }
905
906 return 0;
907
908err_deregister_pinctrl:
909 pinctrl_unregister(rtc->pctldev);
910err:
911 clk_disable_unprepare(rtc->clk);
912 device_init_wakeup(&pdev->dev, false);
913 rtc->type->lock(rtc);
914 pm_runtime_put_sync(&pdev->dev);
915 pm_runtime_disable(&pdev->dev);
916
917 return ret;
918}
919
920static int omap_rtc_remove(struct platform_device *pdev)
921{
922 struct omap_rtc *rtc = platform_get_drvdata(pdev);
923 u8 reg;
924
925 if (pm_power_off == omap_rtc_power_off &&
926 omap_rtc_power_off_rtc == rtc) {
927 pm_power_off = NULL;
928 omap_rtc_power_off_rtc = NULL;
929 }
930
931 device_init_wakeup(&pdev->dev, 0);
932
933 if (!IS_ERR(rtc->clk))
934 clk_disable_unprepare(rtc->clk);
935
936 rtc->type->unlock(rtc);
937 /* leave rtc running, but disable irqs */
938 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
939
940 if (rtc->has_ext_clk) {
941 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
942 reg &= ~OMAP_RTC_OSC_SEL_32KCLK_SRC;
943 rtc_write(rtc, OMAP_RTC_OSC_REG, reg);
944 }
945
946 rtc->type->lock(rtc);
947
948 /* Disable the clock/module */
949 pm_runtime_put_sync(&pdev->dev);
950 pm_runtime_disable(&pdev->dev);
951
952 /* Remove ext_wakeup pinconf */
953 pinctrl_unregister(rtc->pctldev);
954
955 return 0;
956}
957
958static int __maybe_unused omap_rtc_suspend(struct device *dev)
959{
960 struct omap_rtc *rtc = dev_get_drvdata(dev);
961
962 rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
963
964 rtc->type->unlock(rtc);
965 /*
966 * FIXME: the RTC alarm is not currently acting as a wakeup event
967 * source on some platforms, and in fact this enable() call is just
968 * saving a flag that's never used...
969 */
970 if (device_may_wakeup(dev))
971 enable_irq_wake(rtc->irq_alarm);
972 else
973 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
974 rtc->type->lock(rtc);
975
976 rtc->is_suspending = true;
977
978 return 0;
979}
980
981static int __maybe_unused omap_rtc_resume(struct device *dev)
982{
983 struct omap_rtc *rtc = dev_get_drvdata(dev);
984
985 rtc->type->unlock(rtc);
986 if (device_may_wakeup(dev))
987 disable_irq_wake(rtc->irq_alarm);
988 else
989 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
990 rtc->type->lock(rtc);
991
992 rtc->is_suspending = false;
993
994 return 0;
995}
996
997static int __maybe_unused omap_rtc_runtime_suspend(struct device *dev)
998{
999 struct omap_rtc *rtc = dev_get_drvdata(dev);
1000
1001 if (rtc->is_suspending && !rtc->has_ext_clk)
1002 return -EBUSY;
1003
1004 return 0;
1005}
1006
1007static const struct dev_pm_ops omap_rtc_pm_ops = {
1008 SET_SYSTEM_SLEEP_PM_OPS(omap_rtc_suspend, omap_rtc_resume)
1009 SET_RUNTIME_PM_OPS(omap_rtc_runtime_suspend, NULL, NULL)
1010};
1011
1012static void omap_rtc_shutdown(struct platform_device *pdev)
1013{
1014 struct omap_rtc *rtc = platform_get_drvdata(pdev);
1015 u8 mask;
1016
1017 /*
1018 * Keep the ALARM interrupt enabled to allow the system to power up on
1019 * alarm events.
1020 */
1021 rtc->type->unlock(rtc);
1022 mask = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
1023 mask &= OMAP_RTC_INTERRUPTS_IT_ALARM;
1024 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, mask);
1025 rtc->type->lock(rtc);
1026}
1027
1028static struct platform_driver omap_rtc_driver = {
1029 .probe = omap_rtc_probe,
1030 .remove = omap_rtc_remove,
1031 .shutdown = omap_rtc_shutdown,
1032 .driver = {
1033 .name = "omap_rtc",
1034 .pm = &omap_rtc_pm_ops,
1035 .of_match_table = omap_rtc_of_match,
1036 },
1037 .id_table = omap_rtc_id_table,
1038};
1039
1040module_platform_driver(omap_rtc_driver);
1041
1042MODULE_ALIAS("platform:omap_rtc");
1043MODULE_AUTHOR("George G. Davis (and others)");
1044MODULE_LICENSE("GPL");