2 * drivers/rtc/rtc-spear.c
4 * Copyright (C) 2010 ST Microelectronics
5 * Rajeev Kumar<rajeev-dlh.kumar@st.com>
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
12 #include <linux/bcd.h>
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/init.h>
17 #include <linux/irq.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/rtc.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
28 #define ALARM_TIME_REG 0x08
29 #define ALARM_DATE_REG 0x0C
31 #define STATUS_REG 0x14
33 /* TIME_REG & ALARM_TIME_REG */
34 #define SECONDS_UNITS (0xf<<0) /* seconds units position */
35 #define SECONDS_TENS (0x7<<4) /* seconds tens position */
36 #define MINUTES_UNITS (0xf<<8) /* minutes units position */
37 #define MINUTES_TENS (0x7<<12) /* minutes tens position */
38 #define HOURS_UNITS (0xf<<16) /* hours units position */
39 #define HOURS_TENS (0x3<<20) /* hours tens position */
41 /* DATE_REG & ALARM_DATE_REG */
42 #define DAYS_UNITS (0xf<<0) /* days units position */
43 #define DAYS_TENS (0x3<<4) /* days tens position */
44 #define MONTHS_UNITS (0xf<<8) /* months units position */
45 #define MONTHS_TENS (0x1<<12) /* months tens position */
46 #define YEARS_UNITS (0xf<<16) /* years units position */
47 #define YEARS_TENS (0xf<<20) /* years tens position */
48 #define YEARS_HUNDREDS (0xf<<24) /* years hundereds position */
49 #define YEARS_MILLENIUMS (0xf<<28) /* years millenium position */
51 /* MASK SHIFT TIME_REG & ALARM_TIME_REG*/
52 #define SECOND_SHIFT 0x00 /* seconds units */
53 #define MINUTE_SHIFT 0x08 /* minutes units position */
54 #define HOUR_SHIFT 0x10 /* hours units position */
55 #define MDAY_SHIFT 0x00 /* Month day shift */
56 #define MONTH_SHIFT 0x08 /* Month shift */
57 #define YEAR_SHIFT 0x10 /* Year shift */
59 #define SECOND_MASK 0x7F
61 #define HOUR_MASK 0x3F
63 #define MONTH_MASK 0x7F
64 #define YEAR_MASK 0xFFFF
66 /* date reg equal to time reg, for debug only */
67 #define TIME_BYP (1<<9)
68 #define INT_ENABLE (1<<31) /* interrupt enable */
71 #define CLK_UNCONNECTED (1<<0)
72 #define PEND_WR_TIME (1<<2)
73 #define PEND_WR_DATE (1<<3)
74 #define LOST_WR_TIME (1<<4)
75 #define LOST_WR_DATE (1<<5)
76 #define RTC_INT_MASK (1<<31)
77 #define STATUS_BUSY (PEND_WR_TIME | PEND_WR_DATE)
78 #define STATUS_FAIL (LOST_WR_TIME | LOST_WR_DATE)
80 struct spear_rtc_config
{
81 struct rtc_device
*rtc
;
85 unsigned int irq_wake
;
88 static inline void spear_rtc_clear_interrupt(struct spear_rtc_config
*config
)
93 spin_lock_irqsave(&config
->lock
, flags
);
94 val
= readl(config
->ioaddr
+ STATUS_REG
);
96 writel(val
, config
->ioaddr
+ STATUS_REG
);
97 spin_unlock_irqrestore(&config
->lock
, flags
);
100 static inline void spear_rtc_enable_interrupt(struct spear_rtc_config
*config
)
104 val
= readl(config
->ioaddr
+ CTRL_REG
);
105 if (!(val
& INT_ENABLE
)) {
106 spear_rtc_clear_interrupt(config
);
108 writel(val
, config
->ioaddr
+ CTRL_REG
);
112 static inline void spear_rtc_disable_interrupt(struct spear_rtc_config
*config
)
116 val
= readl(config
->ioaddr
+ CTRL_REG
);
117 if (val
& INT_ENABLE
) {
119 writel(val
, config
->ioaddr
+ CTRL_REG
);
123 static inline int is_write_complete(struct spear_rtc_config
*config
)
128 spin_lock_irqsave(&config
->lock
, flags
);
129 if ((readl(config
->ioaddr
+ STATUS_REG
)) & STATUS_FAIL
)
131 spin_unlock_irqrestore(&config
->lock
, flags
);
136 static void rtc_wait_not_busy(struct spear_rtc_config
*config
)
138 int status
, count
= 0;
141 /* Assuming BUSY may stay active for 80 msec) */
142 for (count
= 0; count
< 80; count
++) {
143 spin_lock_irqsave(&config
->lock
, flags
);
144 status
= readl(config
->ioaddr
+ STATUS_REG
);
145 spin_unlock_irqrestore(&config
->lock
, flags
);
146 if ((status
& STATUS_BUSY
) == 0)
148 /* check status busy, after each msec */
153 static irqreturn_t
spear_rtc_irq(int irq
, void *dev_id
)
155 struct spear_rtc_config
*config
= dev_id
;
156 unsigned long flags
, events
= 0;
157 unsigned int irq_data
;
159 spin_lock_irqsave(&config
->lock
, flags
);
160 irq_data
= readl(config
->ioaddr
+ STATUS_REG
);
161 spin_unlock_irqrestore(&config
->lock
, flags
);
163 if ((irq_data
& RTC_INT_MASK
)) {
164 spear_rtc_clear_interrupt(config
);
165 events
= RTC_IRQF
| RTC_AF
;
166 rtc_update_irq(config
->rtc
, 1, events
);
173 static int tm2bcd(struct rtc_time
*tm
)
175 if (rtc_valid_tm(tm
) != 0)
177 tm
->tm_sec
= bin2bcd(tm
->tm_sec
);
178 tm
->tm_min
= bin2bcd(tm
->tm_min
);
179 tm
->tm_hour
= bin2bcd(tm
->tm_hour
);
180 tm
->tm_mday
= bin2bcd(tm
->tm_mday
);
181 tm
->tm_mon
= bin2bcd(tm
->tm_mon
+ 1);
182 tm
->tm_year
= bin2bcd(tm
->tm_year
);
187 static void bcd2tm(struct rtc_time
*tm
)
189 tm
->tm_sec
= bcd2bin(tm
->tm_sec
);
190 tm
->tm_min
= bcd2bin(tm
->tm_min
);
191 tm
->tm_hour
= bcd2bin(tm
->tm_hour
);
192 tm
->tm_mday
= bcd2bin(tm
->tm_mday
);
193 tm
->tm_mon
= bcd2bin(tm
->tm_mon
) - 1;
195 tm
->tm_year
= bcd2bin(tm
->tm_year
);
199 * spear_rtc_read_time - set the time
200 * @dev: rtc device in use
201 * @tm: holds date and time
203 * This function read time and date. On success it will return 0
204 * otherwise -ve error is returned.
206 static int spear_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
208 struct spear_rtc_config
*config
= dev_get_drvdata(dev
);
209 unsigned int time
, date
;
211 /* we don't report wday/yday/isdst ... */
212 rtc_wait_not_busy(config
);
214 time
= readl(config
->ioaddr
+ TIME_REG
);
215 date
= readl(config
->ioaddr
+ DATE_REG
);
216 tm
->tm_sec
= (time
>> SECOND_SHIFT
) & SECOND_MASK
;
217 tm
->tm_min
= (time
>> MINUTE_SHIFT
) & MIN_MASK
;
218 tm
->tm_hour
= (time
>> HOUR_SHIFT
) & HOUR_MASK
;
219 tm
->tm_mday
= (date
>> MDAY_SHIFT
) & DAY_MASK
;
220 tm
->tm_mon
= (date
>> MONTH_SHIFT
) & MONTH_MASK
;
221 tm
->tm_year
= (date
>> YEAR_SHIFT
) & YEAR_MASK
;
228 * spear_rtc_set_time - set the time
229 * @dev: rtc device in use
230 * @tm: holds date and time
232 * This function set time and date. On success it will return 0
233 * otherwise -ve error is returned.
235 static int spear_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
237 struct spear_rtc_config
*config
= dev_get_drvdata(dev
);
238 unsigned int time
, date
;
243 rtc_wait_not_busy(config
);
244 time
= (tm
->tm_sec
<< SECOND_SHIFT
) | (tm
->tm_min
<< MINUTE_SHIFT
) |
245 (tm
->tm_hour
<< HOUR_SHIFT
);
246 date
= (tm
->tm_mday
<< MDAY_SHIFT
) | (tm
->tm_mon
<< MONTH_SHIFT
) |
247 (tm
->tm_year
<< YEAR_SHIFT
);
248 writel(time
, config
->ioaddr
+ TIME_REG
);
249 writel(date
, config
->ioaddr
+ DATE_REG
);
251 return is_write_complete(config
);
255 * spear_rtc_read_alarm - read the alarm time
256 * @dev: rtc device in use
257 * @alm: holds alarm date and time
259 * This function read alarm time and date. On success it will return 0
260 * otherwise -ve error is returned.
262 static int spear_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
264 struct spear_rtc_config
*config
= dev_get_drvdata(dev
);
265 unsigned int time
, date
;
267 rtc_wait_not_busy(config
);
269 time
= readl(config
->ioaddr
+ ALARM_TIME_REG
);
270 date
= readl(config
->ioaddr
+ ALARM_DATE_REG
);
271 alm
->time
.tm_sec
= (time
>> SECOND_SHIFT
) & SECOND_MASK
;
272 alm
->time
.tm_min
= (time
>> MINUTE_SHIFT
) & MIN_MASK
;
273 alm
->time
.tm_hour
= (time
>> HOUR_SHIFT
) & HOUR_MASK
;
274 alm
->time
.tm_mday
= (date
>> MDAY_SHIFT
) & DAY_MASK
;
275 alm
->time
.tm_mon
= (date
>> MONTH_SHIFT
) & MONTH_MASK
;
276 alm
->time
.tm_year
= (date
>> YEAR_SHIFT
) & YEAR_MASK
;
279 alm
->enabled
= readl(config
->ioaddr
+ CTRL_REG
) & INT_ENABLE
;
285 * spear_rtc_set_alarm - set the alarm time
286 * @dev: rtc device in use
287 * @alm: holds alarm date and time
289 * This function set alarm time and date. On success it will return 0
290 * otherwise -ve error is returned.
292 static int spear_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
294 struct spear_rtc_config
*config
= dev_get_drvdata(dev
);
295 unsigned int time
, date
;
298 if (tm2bcd(&alm
->time
) < 0)
301 rtc_wait_not_busy(config
);
303 time
= (alm
->time
.tm_sec
<< SECOND_SHIFT
) | (alm
->time
.tm_min
<<
304 MINUTE_SHIFT
) | (alm
->time
.tm_hour
<< HOUR_SHIFT
);
305 date
= (alm
->time
.tm_mday
<< MDAY_SHIFT
) | (alm
->time
.tm_mon
<<
306 MONTH_SHIFT
) | (alm
->time
.tm_year
<< YEAR_SHIFT
);
308 writel(time
, config
->ioaddr
+ ALARM_TIME_REG
);
309 writel(date
, config
->ioaddr
+ ALARM_DATE_REG
);
310 err
= is_write_complete(config
);
315 spear_rtc_enable_interrupt(config
);
317 spear_rtc_disable_interrupt(config
);
322 static int spear_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
324 struct spear_rtc_config
*config
= dev_get_drvdata(dev
);
327 spear_rtc_clear_interrupt(config
);
332 spear_rtc_disable_interrupt(config
);
336 spear_rtc_enable_interrupt(config
);
346 static struct rtc_class_ops spear_rtc_ops
= {
347 .read_time
= spear_rtc_read_time
,
348 .set_time
= spear_rtc_set_time
,
349 .read_alarm
= spear_rtc_read_alarm
,
350 .set_alarm
= spear_rtc_set_alarm
,
351 .alarm_irq_enable
= spear_alarm_irq_enable
,
354 static int spear_rtc_probe(struct platform_device
*pdev
)
356 struct resource
*res
;
357 struct spear_rtc_config
*config
;
361 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
363 dev_err(&pdev
->dev
, "no resource defined\n");
367 config
= devm_kzalloc(&pdev
->dev
, sizeof(*config
), GFP_KERNEL
);
369 dev_err(&pdev
->dev
, "out of memory\n");
374 irq
= platform_get_irq(pdev
, 0);
376 dev_err(&pdev
->dev
, "no update irq?\n");
380 status
= devm_request_irq(&pdev
->dev
, irq
, spear_rtc_irq
, 0, pdev
->name
,
383 dev_err(&pdev
->dev
, "Alarm interrupt IRQ%d already claimed\n",
388 config
->ioaddr
= devm_request_and_ioremap(&pdev
->dev
, res
);
389 if (!config
->ioaddr
) {
390 dev_err(&pdev
->dev
, "request-ioremap fail\n");
394 config
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
395 if (IS_ERR(config
->clk
))
396 return PTR_ERR(config
->clk
);
398 status
= clk_prepare_enable(config
->clk
);
402 spin_lock_init(&config
->lock
);
403 platform_set_drvdata(pdev
, config
);
405 config
->rtc
= rtc_device_register(pdev
->name
, &pdev
->dev
,
406 &spear_rtc_ops
, THIS_MODULE
);
407 if (IS_ERR(config
->rtc
)) {
408 dev_err(&pdev
->dev
, "can't register RTC device, err %ld\n",
409 PTR_ERR(config
->rtc
));
410 status
= PTR_ERR(config
->rtc
);
411 goto err_disable_clock
;
414 config
->rtc
->uie_unsupported
= 1;
416 if (!device_can_wakeup(&pdev
->dev
))
417 device_init_wakeup(&pdev
->dev
, 1);
422 platform_set_drvdata(pdev
, NULL
);
423 clk_disable_unprepare(config
->clk
);
428 static int spear_rtc_remove(struct platform_device
*pdev
)
430 struct spear_rtc_config
*config
= platform_get_drvdata(pdev
);
432 rtc_device_unregister(config
->rtc
);
433 spear_rtc_disable_interrupt(config
);
434 clk_disable_unprepare(config
->clk
);
435 device_init_wakeup(&pdev
->dev
, 0);
442 static int spear_rtc_suspend(struct platform_device
*pdev
, pm_message_t state
)
444 struct spear_rtc_config
*config
= platform_get_drvdata(pdev
);
447 irq
= platform_get_irq(pdev
, 0);
448 if (device_may_wakeup(&pdev
->dev
)) {
449 if (!enable_irq_wake(irq
))
450 config
->irq_wake
= 1;
452 spear_rtc_disable_interrupt(config
);
453 clk_disable(config
->clk
);
459 static int spear_rtc_resume(struct platform_device
*pdev
)
461 struct spear_rtc_config
*config
= platform_get_drvdata(pdev
);
464 irq
= platform_get_irq(pdev
, 0);
466 if (device_may_wakeup(&pdev
->dev
)) {
467 if (config
->irq_wake
) {
468 disable_irq_wake(irq
);
469 config
->irq_wake
= 0;
472 clk_enable(config
->clk
);
473 spear_rtc_enable_interrupt(config
);
480 #define spear_rtc_suspend NULL
481 #define spear_rtc_resume NULL
484 static void spear_rtc_shutdown(struct platform_device
*pdev
)
486 struct spear_rtc_config
*config
= platform_get_drvdata(pdev
);
488 spear_rtc_disable_interrupt(config
);
489 clk_disable(config
->clk
);
493 static const struct of_device_id spear_rtc_id_table
[] = {
494 { .compatible
= "st,spear600-rtc" },
497 MODULE_DEVICE_TABLE(of
, spear_rtc_id_table
);
500 static struct platform_driver spear_rtc_driver
= {
501 .probe
= spear_rtc_probe
,
502 .remove
= spear_rtc_remove
,
503 .suspend
= spear_rtc_suspend
,
504 .resume
= spear_rtc_resume
,
505 .shutdown
= spear_rtc_shutdown
,
508 .of_match_table
= of_match_ptr(spear_rtc_id_table
),
512 module_platform_driver(spear_rtc_driver
);
514 MODULE_ALIAS("platform:rtc-spear");
515 MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
516 MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
517 MODULE_LICENSE("GPL");