]>
Commit | Line | Data |
---|---|---|
0942a71e RK |
1 | /* |
2 | * drivers/rtc/rtc-spear.c | |
3 | * | |
4 | * Copyright (C) 2010 ST Microelectronics | |
5 | * Rajeev Kumar<rajeev-dlh.kumar@st.com> | |
6 | * | |
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. | |
10 | */ | |
11 | ||
12 | #include <linux/bcd.h> | |
13 | #include <linux/clk.h> | |
14 | #include <linux/delay.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/io.h> | |
17 | #include <linux/irq.h> | |
18 | #include <linux/module.h> | |
0108c4ff | 19 | #include <linux/of.h> |
0942a71e RK |
20 | #include <linux/platform_device.h> |
21 | #include <linux/rtc.h> | |
22 | #include <linux/slab.h> | |
23 | #include <linux/spinlock.h> | |
24 | ||
25 | /* RTC registers */ | |
26 | #define TIME_REG 0x00 | |
27 | #define DATE_REG 0x04 | |
28 | #define ALARM_TIME_REG 0x08 | |
29 | #define ALARM_DATE_REG 0x0C | |
30 | #define CTRL_REG 0x10 | |
31 | #define STATUS_REG 0x14 | |
32 | ||
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 */ | |
40 | ||
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 */ | |
50 | ||
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 */ | |
58 | ||
59 | #define SECOND_MASK 0x7F | |
60 | #define MIN_MASK 0x7F | |
61 | #define HOUR_MASK 0x3F | |
62 | #define DAY_MASK 0x3F | |
63 | #define MONTH_MASK 0x7F | |
64 | #define YEAR_MASK 0xFFFF | |
65 | ||
66 | /* date reg equal to time reg, for debug only */ | |
67 | #define TIME_BYP (1<<9) | |
68 | #define INT_ENABLE (1<<31) /* interrupt enable */ | |
69 | ||
70 | /* STATUS_REG */ | |
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) | |
79 | ||
80 | struct spear_rtc_config { | |
ee6c54ca | 81 | struct rtc_device *rtc; |
0942a71e RK |
82 | struct clk *clk; |
83 | spinlock_t lock; | |
84 | void __iomem *ioaddr; | |
cd0e08a8 | 85 | unsigned int irq_wake; |
0942a71e RK |
86 | }; |
87 | ||
88 | static inline void spear_rtc_clear_interrupt(struct spear_rtc_config *config) | |
89 | { | |
90 | unsigned int val; | |
91 | unsigned long flags; | |
92 | ||
93 | spin_lock_irqsave(&config->lock, flags); | |
94 | val = readl(config->ioaddr + STATUS_REG); | |
95 | val |= RTC_INT_MASK; | |
96 | writel(val, config->ioaddr + STATUS_REG); | |
97 | spin_unlock_irqrestore(&config->lock, flags); | |
98 | } | |
99 | ||
100 | static inline void spear_rtc_enable_interrupt(struct spear_rtc_config *config) | |
101 | { | |
102 | unsigned int val; | |
103 | ||
104 | val = readl(config->ioaddr + CTRL_REG); | |
105 | if (!(val & INT_ENABLE)) { | |
106 | spear_rtc_clear_interrupt(config); | |
107 | val |= INT_ENABLE; | |
108 | writel(val, config->ioaddr + CTRL_REG); | |
109 | } | |
110 | } | |
111 | ||
112 | static inline void spear_rtc_disable_interrupt(struct spear_rtc_config *config) | |
113 | { | |
114 | unsigned int val; | |
115 | ||
116 | val = readl(config->ioaddr + CTRL_REG); | |
117 | if (val & INT_ENABLE) { | |
118 | val &= ~INT_ENABLE; | |
119 | writel(val, config->ioaddr + CTRL_REG); | |
120 | } | |
121 | } | |
122 | ||
123 | static inline int is_write_complete(struct spear_rtc_config *config) | |
124 | { | |
125 | int ret = 0; | |
126 | unsigned long flags; | |
127 | ||
128 | spin_lock_irqsave(&config->lock, flags); | |
129 | if ((readl(config->ioaddr + STATUS_REG)) & STATUS_FAIL) | |
130 | ret = -EIO; | |
131 | spin_unlock_irqrestore(&config->lock, flags); | |
132 | ||
133 | return ret; | |
134 | } | |
135 | ||
136 | static void rtc_wait_not_busy(struct spear_rtc_config *config) | |
137 | { | |
138 | int status, count = 0; | |
139 | unsigned long flags; | |
140 | ||
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) | |
147 | break; | |
148 | /* check status busy, after each msec */ | |
149 | msleep(1); | |
150 | } | |
151 | } | |
152 | ||
153 | static irqreturn_t spear_rtc_irq(int irq, void *dev_id) | |
154 | { | |
ee6c54ca | 155 | struct spear_rtc_config *config = dev_id; |
0942a71e RK |
156 | unsigned long flags, events = 0; |
157 | unsigned int irq_data; | |
158 | ||
159 | spin_lock_irqsave(&config->lock, flags); | |
160 | irq_data = readl(config->ioaddr + STATUS_REG); | |
161 | spin_unlock_irqrestore(&config->lock, flags); | |
162 | ||
163 | if ((irq_data & RTC_INT_MASK)) { | |
164 | spear_rtc_clear_interrupt(config); | |
165 | events = RTC_IRQF | RTC_AF; | |
ee6c54ca | 166 | rtc_update_irq(config->rtc, 1, events); |
0942a71e RK |
167 | return IRQ_HANDLED; |
168 | } else | |
169 | return IRQ_NONE; | |
170 | ||
171 | } | |
172 | ||
9fb7aa75 | 173 | static void tm2bcd(struct rtc_time *tm) |
0942a71e | 174 | { |
0942a71e RK |
175 | tm->tm_sec = bin2bcd(tm->tm_sec); |
176 | tm->tm_min = bin2bcd(tm->tm_min); | |
177 | tm->tm_hour = bin2bcd(tm->tm_hour); | |
178 | tm->tm_mday = bin2bcd(tm->tm_mday); | |
179 | tm->tm_mon = bin2bcd(tm->tm_mon + 1); | |
180 | tm->tm_year = bin2bcd(tm->tm_year); | |
0942a71e RK |
181 | } |
182 | ||
183 | static void bcd2tm(struct rtc_time *tm) | |
184 | { | |
185 | tm->tm_sec = bcd2bin(tm->tm_sec); | |
186 | tm->tm_min = bcd2bin(tm->tm_min); | |
187 | tm->tm_hour = bcd2bin(tm->tm_hour); | |
188 | tm->tm_mday = bcd2bin(tm->tm_mday); | |
189 | tm->tm_mon = bcd2bin(tm->tm_mon) - 1; | |
190 | /* epoch == 1900 */ | |
191 | tm->tm_year = bcd2bin(tm->tm_year); | |
192 | } | |
193 | ||
194 | /* | |
195 | * spear_rtc_read_time - set the time | |
196 | * @dev: rtc device in use | |
197 | * @tm: holds date and time | |
198 | * | |
199 | * This function read time and date. On success it will return 0 | |
200 | * otherwise -ve error is returned. | |
201 | */ | |
202 | static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm) | |
203 | { | |
ee6c54ca | 204 | struct spear_rtc_config *config = dev_get_drvdata(dev); |
0942a71e RK |
205 | unsigned int time, date; |
206 | ||
207 | /* we don't report wday/yday/isdst ... */ | |
208 | rtc_wait_not_busy(config); | |
209 | ||
210 | time = readl(config->ioaddr + TIME_REG); | |
211 | date = readl(config->ioaddr + DATE_REG); | |
212 | tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK; | |
213 | tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK; | |
214 | tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK; | |
215 | tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK; | |
216 | tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK; | |
217 | tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK; | |
218 | ||
219 | bcd2tm(tm); | |
220 | return 0; | |
221 | } | |
222 | ||
223 | /* | |
224 | * spear_rtc_set_time - set the time | |
225 | * @dev: rtc device in use | |
226 | * @tm: holds date and time | |
227 | * | |
228 | * This function set time and date. On success it will return 0 | |
229 | * otherwise -ve error is returned. | |
230 | */ | |
231 | static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm) | |
232 | { | |
ee6c54ca | 233 | struct spear_rtc_config *config = dev_get_drvdata(dev); |
a16e8393 | 234 | unsigned int time, date; |
0942a71e | 235 | |
9fb7aa75 | 236 | tm2bcd(tm); |
0942a71e RK |
237 | |
238 | rtc_wait_not_busy(config); | |
239 | time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) | | |
240 | (tm->tm_hour << HOUR_SHIFT); | |
241 | date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) | | |
242 | (tm->tm_year << YEAR_SHIFT); | |
243 | writel(time, config->ioaddr + TIME_REG); | |
244 | writel(date, config->ioaddr + DATE_REG); | |
0942a71e | 245 | |
a16e8393 | 246 | return is_write_complete(config); |
0942a71e RK |
247 | } |
248 | ||
249 | /* | |
250 | * spear_rtc_read_alarm - read the alarm time | |
251 | * @dev: rtc device in use | |
252 | * @alm: holds alarm date and time | |
253 | * | |
254 | * This function read alarm time and date. On success it will return 0 | |
255 | * otherwise -ve error is returned. | |
256 | */ | |
257 | static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) | |
258 | { | |
ee6c54ca | 259 | struct spear_rtc_config *config = dev_get_drvdata(dev); |
0942a71e RK |
260 | unsigned int time, date; |
261 | ||
262 | rtc_wait_not_busy(config); | |
263 | ||
264 | time = readl(config->ioaddr + ALARM_TIME_REG); | |
265 | date = readl(config->ioaddr + ALARM_DATE_REG); | |
266 | alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK; | |
267 | alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK; | |
268 | alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK; | |
269 | alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK; | |
270 | alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK; | |
271 | alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK; | |
272 | ||
273 | bcd2tm(&alm->time); | |
274 | alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE; | |
275 | ||
276 | return 0; | |
277 | } | |
278 | ||
279 | /* | |
280 | * spear_rtc_set_alarm - set the alarm time | |
281 | * @dev: rtc device in use | |
282 | * @alm: holds alarm date and time | |
283 | * | |
284 | * This function set alarm time and date. On success it will return 0 | |
285 | * otherwise -ve error is returned. | |
286 | */ | |
287 | static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) | |
288 | { | |
ee6c54ca | 289 | struct spear_rtc_config *config = dev_get_drvdata(dev); |
a16e8393 LPC |
290 | unsigned int time, date; |
291 | int err; | |
0942a71e | 292 | |
9fb7aa75 | 293 | tm2bcd(&alm->time); |
0942a71e RK |
294 | |
295 | rtc_wait_not_busy(config); | |
296 | ||
297 | time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min << | |
298 | MINUTE_SHIFT) | (alm->time.tm_hour << HOUR_SHIFT); | |
299 | date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon << | |
300 | MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT); | |
301 | ||
302 | writel(time, config->ioaddr + ALARM_TIME_REG); | |
303 | writel(date, config->ioaddr + ALARM_DATE_REG); | |
304 | err = is_write_complete(config); | |
305 | if (err < 0) | |
306 | return err; | |
307 | ||
308 | if (alm->enabled) | |
309 | spear_rtc_enable_interrupt(config); | |
310 | else | |
311 | spear_rtc_disable_interrupt(config); | |
312 | ||
313 | return 0; | |
314 | } | |
131f8b75 SH |
315 | |
316 | static int spear_alarm_irq_enable(struct device *dev, unsigned int enabled) | |
317 | { | |
ee6c54ca | 318 | struct spear_rtc_config *config = dev_get_drvdata(dev); |
131f8b75 SH |
319 | int ret = 0; |
320 | ||
321 | spear_rtc_clear_interrupt(config); | |
322 | ||
323 | switch (enabled) { | |
324 | case 0: | |
325 | /* alarm off */ | |
326 | spear_rtc_disable_interrupt(config); | |
327 | break; | |
328 | case 1: | |
329 | /* alarm on */ | |
330 | spear_rtc_enable_interrupt(config); | |
331 | break; | |
332 | default: | |
333 | ret = -EINVAL; | |
334 | break; | |
335 | } | |
336 | ||
337 | return ret; | |
338 | } | |
339 | ||
34c7b3ac | 340 | static const struct rtc_class_ops spear_rtc_ops = { |
0942a71e RK |
341 | .read_time = spear_rtc_read_time, |
342 | .set_time = spear_rtc_set_time, | |
343 | .read_alarm = spear_rtc_read_alarm, | |
344 | .set_alarm = spear_rtc_set_alarm, | |
131f8b75 | 345 | .alarm_irq_enable = spear_alarm_irq_enable, |
0942a71e RK |
346 | }; |
347 | ||
5a167f45 | 348 | static int spear_rtc_probe(struct platform_device *pdev) |
0942a71e RK |
349 | { |
350 | struct resource *res; | |
0942a71e | 351 | struct spear_rtc_config *config; |
a16e8393 | 352 | int status = 0; |
0942a71e RK |
353 | int irq; |
354 | ||
bdaa2c63 | 355 | config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL); |
681acc9f | 356 | if (!config) |
bdaa2c63 | 357 | return -ENOMEM; |
0942a71e | 358 | |
bdaa2c63 VK |
359 | /* alarm irqs */ |
360 | irq = platform_get_irq(pdev, 0); | |
361 | if (irq < 0) { | |
362 | dev_err(&pdev->dev, "no update irq?\n"); | |
363 | return irq; | |
0942a71e RK |
364 | } |
365 | ||
bdaa2c63 VK |
366 | status = devm_request_irq(&pdev->dev, irq, spear_rtc_irq, 0, pdev->name, |
367 | config); | |
368 | if (status) { | |
369 | dev_err(&pdev->dev, "Alarm interrupt IRQ%d already claimed\n", | |
370 | irq); | |
371 | return status; | |
372 | } | |
0942a71e | 373 | |
e044253b | 374 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
8cbce1e5 TR |
375 | config->ioaddr = devm_ioremap_resource(&pdev->dev, res); |
376 | if (IS_ERR(config->ioaddr)) | |
377 | return PTR_ERR(config->ioaddr); | |
0942a71e | 378 | |
bdaa2c63 VK |
379 | config->clk = devm_clk_get(&pdev->dev, NULL); |
380 | if (IS_ERR(config->clk)) | |
381 | return PTR_ERR(config->clk); | |
382 | ||
93d78394 | 383 | status = clk_prepare_enable(config->clk); |
bdaa2c63 VK |
384 | if (status < 0) |
385 | return status; | |
386 | ||
0942a71e | 387 | spin_lock_init(&config->lock); |
ee6c54ca | 388 | platform_set_drvdata(pdev, config); |
0942a71e | 389 | |
246b1a99 JH |
390 | config->rtc = devm_rtc_device_register(&pdev->dev, pdev->name, |
391 | &spear_rtc_ops, THIS_MODULE); | |
ee6c54ca | 392 | if (IS_ERR(config->rtc)) { |
0942a71e | 393 | dev_err(&pdev->dev, "can't register RTC device, err %ld\n", |
ee6c54ca VK |
394 | PTR_ERR(config->rtc)); |
395 | status = PTR_ERR(config->rtc); | |
bdaa2c63 | 396 | goto err_disable_clock; |
0942a71e RK |
397 | } |
398 | ||
c9f5c7e7 DS |
399 | config->rtc->uie_unsupported = 1; |
400 | ||
0942a71e RK |
401 | if (!device_can_wakeup(&pdev->dev)) |
402 | device_init_wakeup(&pdev->dev, 1); | |
403 | ||
404 | return 0; | |
405 | ||
0942a71e | 406 | err_disable_clock: |
93d78394 | 407 | clk_disable_unprepare(config->clk); |
0942a71e RK |
408 | |
409 | return status; | |
410 | } | |
411 | ||
5a167f45 | 412 | static int spear_rtc_remove(struct platform_device *pdev) |
0942a71e | 413 | { |
ee6c54ca | 414 | struct spear_rtc_config *config = platform_get_drvdata(pdev); |
0942a71e | 415 | |
0942a71e | 416 | spear_rtc_disable_interrupt(config); |
93d78394 | 417 | clk_disable_unprepare(config->clk); |
bdaa2c63 | 418 | device_init_wakeup(&pdev->dev, 0); |
0942a71e RK |
419 | |
420 | return 0; | |
421 | } | |
422 | ||
b086e392 JH |
423 | #ifdef CONFIG_PM_SLEEP |
424 | static int spear_rtc_suspend(struct device *dev) | |
0942a71e | 425 | { |
b086e392 | 426 | struct platform_device *pdev = to_platform_device(dev); |
ee6c54ca | 427 | struct spear_rtc_config *config = platform_get_drvdata(pdev); |
0942a71e RK |
428 | int irq; |
429 | ||
430 | irq = platform_get_irq(pdev, 0); | |
cd0e08a8 DS |
431 | if (device_may_wakeup(&pdev->dev)) { |
432 | if (!enable_irq_wake(irq)) | |
433 | config->irq_wake = 1; | |
434 | } else { | |
0942a71e RK |
435 | spear_rtc_disable_interrupt(config); |
436 | clk_disable(config->clk); | |
437 | } | |
438 | ||
439 | return 0; | |
440 | } | |
441 | ||
b086e392 | 442 | static int spear_rtc_resume(struct device *dev) |
0942a71e | 443 | { |
b086e392 | 444 | struct platform_device *pdev = to_platform_device(dev); |
ee6c54ca | 445 | struct spear_rtc_config *config = platform_get_drvdata(pdev); |
0942a71e RK |
446 | int irq; |
447 | ||
448 | irq = platform_get_irq(pdev, 0); | |
449 | ||
cd0e08a8 DS |
450 | if (device_may_wakeup(&pdev->dev)) { |
451 | if (config->irq_wake) { | |
452 | disable_irq_wake(irq); | |
453 | config->irq_wake = 0; | |
454 | } | |
455 | } else { | |
0942a71e RK |
456 | clk_enable(config->clk); |
457 | spear_rtc_enable_interrupt(config); | |
458 | } | |
459 | ||
460 | return 0; | |
461 | } | |
0942a71e RK |
462 | #endif |
463 | ||
b086e392 JH |
464 | static SIMPLE_DEV_PM_OPS(spear_rtc_pm_ops, spear_rtc_suspend, spear_rtc_resume); |
465 | ||
0942a71e RK |
466 | static void spear_rtc_shutdown(struct platform_device *pdev) |
467 | { | |
ee6c54ca | 468 | struct spear_rtc_config *config = platform_get_drvdata(pdev); |
0942a71e RK |
469 | |
470 | spear_rtc_disable_interrupt(config); | |
471 | clk_disable(config->clk); | |
472 | } | |
473 | ||
0108c4ff VK |
474 | #ifdef CONFIG_OF |
475 | static const struct of_device_id spear_rtc_id_table[] = { | |
476 | { .compatible = "st,spear600-rtc" }, | |
477 | {} | |
478 | }; | |
479 | MODULE_DEVICE_TABLE(of, spear_rtc_id_table); | |
480 | #endif | |
481 | ||
0942a71e RK |
482 | static struct platform_driver spear_rtc_driver = { |
483 | .probe = spear_rtc_probe, | |
5a167f45 | 484 | .remove = spear_rtc_remove, |
0942a71e RK |
485 | .shutdown = spear_rtc_shutdown, |
486 | .driver = { | |
487 | .name = "rtc-spear", | |
b086e392 | 488 | .pm = &spear_rtc_pm_ops, |
0108c4ff | 489 | .of_match_table = of_match_ptr(spear_rtc_id_table), |
0942a71e RK |
490 | }, |
491 | }; | |
492 | ||
0c4eae66 | 493 | module_platform_driver(spear_rtc_driver); |
0942a71e RK |
494 | |
495 | MODULE_ALIAS("platform:rtc-spear"); | |
496 | MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>"); | |
497 | MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)"); | |
498 | MODULE_LICENSE("GPL"); |