Blame view
drivers/rtc/rtc-pxa.c
11.2 KB
dc9443688 rtc: driver for p... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/* * Real Time Clock interface for XScale PXA27x and PXA3xx * * Copyright (C) 2008 Robert Jarzmik * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ |
0417ce2ad rtc: pxa27x/pxa3x... |
21 |
#include <linux/init.h> |
dc9443688 rtc: driver for p... |
22 23 24 25 26 |
#include <linux/platform_device.h> #include <linux/module.h> #include <linux/rtc.h> #include <linux/seq_file.h> #include <linux/interrupt.h> |
0417ce2ad rtc: pxa27x/pxa3x... |
27 |
#include <linux/io.h> |
5a0e3ad6a include cleanup: ... |
28 |
#include <linux/slab.h> |
dc9443688 rtc: driver for p... |
29 |
|
4216d0bd8 rtc-pxa: fix buil... |
30 |
#include <mach/hardware.h> |
dc9443688 rtc: driver for p... |
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
#define TIMER_FREQ CLOCK_TICK_RATE #define RTC_DEF_DIVIDER (32768 - 1) #define RTC_DEF_TRIM 0 #define MAXFREQ_PERIODIC 1000 /* * PXA Registers and bits definitions */ #define RTSR_PICE (1 << 15) /* Periodic interrupt count enable */ #define RTSR_PIALE (1 << 14) /* Periodic interrupt Alarm enable */ #define RTSR_PIAL (1 << 13) /* Periodic interrupt detected */ #define RTSR_SWALE2 (1 << 11) /* RTC stopwatch alarm2 enable */ #define RTSR_SWAL2 (1 << 10) /* RTC stopwatch alarm2 detected */ #define RTSR_SWALE1 (1 << 9) /* RTC stopwatch alarm1 enable */ #define RTSR_SWAL1 (1 << 8) /* RTC stopwatch alarm1 detected */ #define RTSR_RDALE2 (1 << 7) /* RTC alarm2 enable */ #define RTSR_RDAL2 (1 << 6) /* RTC alarm2 detected */ #define RTSR_RDALE1 (1 << 5) /* RTC alarm1 enable */ #define RTSR_RDAL1 (1 << 4) /* RTC alarm1 detected */ #define RTSR_HZE (1 << 3) /* HZ interrupt enable */ #define RTSR_ALE (1 << 2) /* RTC alarm interrupt enable */ #define RTSR_HZ (1 << 1) /* HZ rising-edge detected */ #define RTSR_AL (1 << 0) /* RTC alarm detected */ #define RTSR_TRIG_MASK (RTSR_AL | RTSR_HZ | RTSR_RDAL1 | RTSR_RDAL2\ | RTSR_SWAL1 | RTSR_SWAL2) #define RYxR_YEAR_S 9 #define RYxR_YEAR_MASK (0xfff << RYxR_YEAR_S) #define RYxR_MONTH_S 5 #define RYxR_MONTH_MASK (0xf << RYxR_MONTH_S) #define RYxR_DAY_MASK 0x1f #define RDxR_HOUR_S 12 #define RDxR_HOUR_MASK (0x1f << RDxR_HOUR_S) #define RDxR_MIN_S 6 #define RDxR_MIN_MASK (0x3f << RDxR_MIN_S) #define RDxR_SEC_MASK 0x3f #define RTSR 0x08 #define RTTR 0x0c #define RDCR 0x10 #define RYCR 0x14 #define RDAR1 0x18 #define RYAR1 0x1c #define RTCPICR 0x34 #define PIAR 0x38 #define rtc_readl(pxa_rtc, reg) \ __raw_readl((pxa_rtc)->base + (reg)) #define rtc_writel(pxa_rtc, reg, value) \ __raw_writel((value), (pxa_rtc)->base + (reg)) struct pxa_rtc { struct resource *ress; void __iomem *base; int irq_1Hz; int irq_Alrm; struct rtc_device *rtc; spinlock_t lock; /* Protects this structure */ |
dc9443688 rtc: driver for p... |
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
}; static u32 ryxr_calc(struct rtc_time *tm) { return ((tm->tm_year + 1900) << RYxR_YEAR_S) | ((tm->tm_mon + 1) << RYxR_MONTH_S) | tm->tm_mday; } static u32 rdxr_calc(struct rtc_time *tm) { return (tm->tm_hour << RDxR_HOUR_S) | (tm->tm_min << RDxR_MIN_S) | tm->tm_sec; } static void tm_calc(u32 rycr, u32 rdcr, struct rtc_time *tm) { tm->tm_year = ((rycr & RYxR_YEAR_MASK) >> RYxR_YEAR_S) - 1900; tm->tm_mon = (((rycr & RYxR_MONTH_MASK) >> RYxR_MONTH_S)) - 1; tm->tm_mday = (rycr & RYxR_DAY_MASK); tm->tm_hour = (rdcr & RDxR_HOUR_MASK) >> RDxR_HOUR_S; tm->tm_min = (rdcr & RDxR_MIN_MASK) >> RDxR_MIN_S; tm->tm_sec = rdcr & RDxR_SEC_MASK; } static void rtsr_clear_bits(struct pxa_rtc *pxa_rtc, u32 mask) { u32 rtsr; rtsr = rtc_readl(pxa_rtc, RTSR); rtsr &= ~RTSR_TRIG_MASK; rtsr &= ~mask; rtc_writel(pxa_rtc, RTSR, rtsr); } static void rtsr_set_bits(struct pxa_rtc *pxa_rtc, u32 mask) { u32 rtsr; rtsr = rtc_readl(pxa_rtc, RTSR); rtsr &= ~RTSR_TRIG_MASK; rtsr |= mask; rtc_writel(pxa_rtc, RTSR, rtsr); } static irqreturn_t pxa_rtc_irq(int irq, void *dev_id) { struct platform_device *pdev = to_platform_device(dev_id); struct pxa_rtc *pxa_rtc = platform_get_drvdata(pdev); u32 rtsr; unsigned long events = 0; spin_lock(&pxa_rtc->lock); /* clear interrupt sources */ rtsr = rtc_readl(pxa_rtc, RTSR); rtc_writel(pxa_rtc, RTSR, rtsr); /* temporary disable rtc interrupts */ rtsr_clear_bits(pxa_rtc, RTSR_RDALE1 | RTSR_PIALE | RTSR_HZE); /* clear alarm interrupt if it has occurred */ if (rtsr & RTSR_RDAL1) rtsr &= ~RTSR_RDALE1; /* update irq data & counter */ if (rtsr & RTSR_RDAL1) events |= RTC_AF | RTC_IRQF; if (rtsr & RTSR_HZ) events |= RTC_UF | RTC_IRQF; if (rtsr & RTSR_PIAL) events |= RTC_PF | RTC_IRQF; rtc_update_irq(pxa_rtc->rtc, 1, events); /* enable back rtc interrupts */ rtc_writel(pxa_rtc, RTSR, rtsr & ~RTSR_TRIG_MASK); spin_unlock(&pxa_rtc->lock); return IRQ_HANDLED; } static int pxa_rtc_open(struct device *dev) { struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev); int ret; ret = request_irq(pxa_rtc->irq_1Hz, pxa_rtc_irq, IRQF_DISABLED, "rtc 1Hz", dev); if (ret < 0) { dev_err(dev, "can't get irq %i, err %d ", pxa_rtc->irq_1Hz, ret); goto err_irq_1Hz; } ret = request_irq(pxa_rtc->irq_Alrm, pxa_rtc_irq, IRQF_DISABLED, "rtc Alrm", dev); if (ret < 0) { dev_err(dev, "can't get irq %i, err %d ", pxa_rtc->irq_Alrm, ret); goto err_irq_Alrm; } return 0; err_irq_Alrm: free_irq(pxa_rtc->irq_1Hz, dev); err_irq_1Hz: return ret; } static void pxa_rtc_release(struct device *dev) { struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev); spin_lock_irq(&pxa_rtc->lock); rtsr_clear_bits(pxa_rtc, RTSR_PIALE | RTSR_RDALE1 | RTSR_HZE); spin_unlock_irq(&pxa_rtc->lock); free_irq(pxa_rtc->irq_Alrm, dev); free_irq(pxa_rtc->irq_1Hz, dev); } |
93b1384fb rtc/pxa: remove u... |
211 |
static int pxa_alarm_irq_enable(struct device *dev, unsigned int enabled) |
dc9443688 rtc: driver for p... |
212 213 |
{ struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev); |
dc9443688 rtc: driver for p... |
214 215 |
spin_lock_irq(&pxa_rtc->lock); |
93b1384fb rtc/pxa: remove u... |
216 217 |
if (enabled) |
dc9443688 rtc: driver for p... |
218 |
rtsr_set_bits(pxa_rtc, RTSR_RDALE1); |
93b1384fb rtc/pxa: remove u... |
219 220 221 222 223 224 |
else rtsr_clear_bits(pxa_rtc, RTSR_RDALE1); spin_unlock_irq(&pxa_rtc->lock); return 0; } |
dc9443688 rtc: driver for p... |
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
static int pxa_rtc_read_time(struct device *dev, struct rtc_time *tm) { struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev); u32 rycr, rdcr; rycr = rtc_readl(pxa_rtc, RYCR); rdcr = rtc_readl(pxa_rtc, RDCR); tm_calc(rycr, rdcr, tm); return 0; } static int pxa_rtc_set_time(struct device *dev, struct rtc_time *tm) { struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev); rtc_writel(pxa_rtc, RYCR, ryxr_calc(tm)); rtc_writel(pxa_rtc, RDCR, rdxr_calc(tm)); return 0; } static int pxa_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) { struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev); u32 rtsr, ryar, rdar; ryar = rtc_readl(pxa_rtc, RYAR1); rdar = rtc_readl(pxa_rtc, RDAR1); tm_calc(ryar, rdar, &alrm->time); rtsr = rtc_readl(pxa_rtc, RTSR); alrm->enabled = (rtsr & RTSR_RDALE1) ? 1 : 0; alrm->pending = (rtsr & RTSR_RDAL1) ? 1 : 0; return 0; } static int pxa_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) { struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev); u32 rtsr; spin_lock_irq(&pxa_rtc->lock); rtc_writel(pxa_rtc, RYAR1, ryxr_calc(&alrm->time)); rtc_writel(pxa_rtc, RDAR1, rdxr_calc(&alrm->time)); rtsr = rtc_readl(pxa_rtc, RTSR); if (alrm->enabled) rtsr |= RTSR_RDALE1; else rtsr &= ~RTSR_RDALE1; rtc_writel(pxa_rtc, RTSR, rtsr); spin_unlock_irq(&pxa_rtc->lock); return 0; } static int pxa_rtc_proc(struct device *dev, struct seq_file *seq) { struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev); seq_printf(seq, "trim/divider\t: 0x%08x ", rtc_readl(pxa_rtc, RTTR)); seq_printf(seq, "update_IRQ\t: %s ", (rtc_readl(pxa_rtc, RTSR) & RTSR_HZE) ? "yes" : "no"); seq_printf(seq, "periodic_IRQ\t: %s ", (rtc_readl(pxa_rtc, RTSR) & RTSR_PIALE) ? "yes" : "no"); seq_printf(seq, "periodic_freq\t: %u ", rtc_readl(pxa_rtc, PIAR)); return 0; } static const struct rtc_class_ops pxa_rtc_ops = { .open = pxa_rtc_open, .release = pxa_rtc_release, |
dc9443688 rtc: driver for p... |
305 306 307 308 |
.read_time = pxa_rtc_read_time, .set_time = pxa_rtc_set_time, .read_alarm = pxa_rtc_read_alarm, .set_alarm = pxa_rtc_set_alarm, |
93b1384fb rtc/pxa: remove u... |
309 |
.alarm_irq_enable = pxa_alarm_irq_enable, |
dc9443688 rtc: driver for p... |
310 |
.proc = pxa_rtc_proc, |
dc9443688 rtc: driver for p... |
311 |
}; |
0417ce2ad rtc: pxa27x/pxa3x... |
312 |
static int __init pxa_rtc_probe(struct platform_device *pdev) |
dc9443688 rtc: driver for p... |
313 314 315 316 317 |
{ struct device *dev = &pdev->dev; struct pxa_rtc *pxa_rtc; int ret; u32 rttr; |
dc9443688 rtc: driver for p... |
318 319 |
pxa_rtc = kzalloc(sizeof(struct pxa_rtc), GFP_KERNEL); if (!pxa_rtc) |
0417ce2ad rtc: pxa27x/pxa3x... |
320 321 322 323 |
return -ENOMEM; spin_lock_init(&pxa_rtc->lock); platform_set_drvdata(pdev, pxa_rtc); |
dc9443688 rtc: driver for p... |
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
ret = -ENXIO; pxa_rtc->ress = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!pxa_rtc->ress) { dev_err(dev, "No I/O memory resource defined "); goto err_ress; } pxa_rtc->irq_1Hz = platform_get_irq(pdev, 0); if (pxa_rtc->irq_1Hz < 0) { dev_err(dev, "No 1Hz IRQ resource defined "); goto err_ress; } pxa_rtc->irq_Alrm = platform_get_irq(pdev, 1); if (pxa_rtc->irq_Alrm < 0) { dev_err(dev, "No alarm IRQ resource defined "); goto err_ress; } |
dc9443688 rtc: driver for p... |
345 346 |
ret = -ENOMEM; pxa_rtc->base = ioremap(pxa_rtc->ress->start, |
0417ce2ad rtc: pxa27x/pxa3x... |
347 |
resource_size(pxa_rtc->ress)); |
dc9443688 rtc: driver for p... |
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
if (!pxa_rtc->base) { dev_err(&pdev->dev, "Unable to map pxa RTC I/O memory "); goto err_map; } /* * If the clock divider is uninitialized then reset it to the * default value to get the 1Hz clock. */ if (rtc_readl(pxa_rtc, RTTR) == 0) { rttr = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16); rtc_writel(pxa_rtc, RTTR, rttr); dev_warn(dev, "warning: initializing default clock" " divider/trim value "); } rtsr_clear_bits(pxa_rtc, RTSR_PIALE | RTSR_RDALE1 | RTSR_HZE); |
0417ce2ad rtc: pxa27x/pxa3x... |
367 368 369 370 371 372 373 374 375 |
pxa_rtc->rtc = rtc_device_register("pxa-rtc", &pdev->dev, &pxa_rtc_ops, THIS_MODULE); ret = PTR_ERR(pxa_rtc->rtc); if (IS_ERR(pxa_rtc->rtc)) { dev_err(dev, "Failed to register RTC device -> %d ", ret); goto err_rtc_reg; } |
dc9443688 rtc: driver for p... |
376 377 378 |
device_init_wakeup(dev, 1); return 0; |
dc9443688 rtc: driver for p... |
379 |
err_rtc_reg: |
0417ce2ad rtc: pxa27x/pxa3x... |
380 |
iounmap(pxa_rtc->base); |
dc9443688 rtc: driver for p... |
381 |
err_ress: |
0417ce2ad rtc: pxa27x/pxa3x... |
382 |
err_map: |
dc9443688 rtc: driver for p... |
383 |
kfree(pxa_rtc); |
dc9443688 rtc: driver for p... |
384 385 |
return ret; } |
0417ce2ad rtc: pxa27x/pxa3x... |
386 |
static int __exit pxa_rtc_remove(struct platform_device *pdev) |
dc9443688 rtc: driver for p... |
387 388 |
{ struct pxa_rtc *pxa_rtc = platform_get_drvdata(pdev); |
0417ce2ad rtc: pxa27x/pxa3x... |
389 |
rtc_device_unregister(pxa_rtc->rtc); |
dc9443688 rtc: driver for p... |
390 391 |
spin_lock_irq(&pxa_rtc->lock); iounmap(pxa_rtc->base); |
dc9443688 rtc: driver for p... |
392 |
spin_unlock_irq(&pxa_rtc->lock); |
dc9443688 rtc: driver for p... |
393 394 395 396 397 398 |
kfree(pxa_rtc); return 0; } #ifdef CONFIG_PM |
e6e698a4f [ARM] pxa: update... |
399 |
static int pxa_rtc_suspend(struct device *dev) |
dc9443688 rtc: driver for p... |
400 |
{ |
e6e698a4f [ARM] pxa: update... |
401 |
struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev); |
dc9443688 rtc: driver for p... |
402 |
|
e6e698a4f [ARM] pxa: update... |
403 |
if (device_may_wakeup(dev)) |
dc9443688 rtc: driver for p... |
404 405 406 |
enable_irq_wake(pxa_rtc->irq_Alrm); return 0; } |
e6e698a4f [ARM] pxa: update... |
407 |
static int pxa_rtc_resume(struct device *dev) |
dc9443688 rtc: driver for p... |
408 |
{ |
e6e698a4f [ARM] pxa: update... |
409 |
struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev); |
dc9443688 rtc: driver for p... |
410 |
|
e6e698a4f [ARM] pxa: update... |
411 |
if (device_may_wakeup(dev)) |
dc9443688 rtc: driver for p... |
412 413 414 |
disable_irq_wake(pxa_rtc->irq_Alrm); return 0; } |
e6e698a4f [ARM] pxa: update... |
415 |
|
471452104 const: constify r... |
416 |
static const struct dev_pm_ops pxa_rtc_pm_ops = { |
e6e698a4f [ARM] pxa: update... |
417 418 419 |
.suspend = pxa_rtc_suspend, .resume = pxa_rtc_resume, }; |
dc9443688 rtc: driver for p... |
420 421 422 |
#endif static struct platform_driver pxa_rtc_driver = { |
dc9443688 rtc: driver for p... |
423 |
.remove = __exit_p(pxa_rtc_remove), |
dc9443688 rtc: driver for p... |
424 |
.driver = { |
e6e698a4f [ARM] pxa: update... |
425 426 427 428 |
.name = "pxa-rtc", #ifdef CONFIG_PM .pm = &pxa_rtc_pm_ops, #endif |
dc9443688 rtc: driver for p... |
429 430 431 432 433 434 |
}, }; static int __init pxa_rtc_init(void) { if (cpu_is_pxa27x() || cpu_is_pxa3xx()) |
0417ce2ad rtc: pxa27x/pxa3x... |
435 |
return platform_driver_probe(&pxa_rtc_driver, pxa_rtc_probe); |
dc9443688 rtc: driver for p... |
436 437 438 439 440 441 442 443 444 445 446 |
return -ENODEV; } static void __exit pxa_rtc_exit(void) { platform_driver_unregister(&pxa_rtc_driver); } module_init(pxa_rtc_init); module_exit(pxa_rtc_exit); |
57f63bc8f rtc: update maint... |
447 |
MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>"); |
dc9443688 rtc: driver for p... |
448 449 450 |
MODULE_DESCRIPTION("PXA27x/PXA3xx Realtime Clock Driver (RTC)"); MODULE_LICENSE("GPL"); MODULE_ALIAS("platform:pxa-rtc"); |