Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Real Time Clock interface for StrongARM SA1x00 and XScale PXA2xx |
| 3 | * |
| 4 | * Copyright (c) 2000 Nils Faerber |
| 5 | * |
| 6 | * Based on rtc.c by Paul Gortmaker |
| 7 | * |
| 8 | * Original Driver by Nils Faerber <nils@kernelconcepts.de> |
| 9 | * |
| 10 | * Modifications from: |
| 11 | * CIH <cih@coventive.com> |
Nicolas Pitre | 2f82af0 | 2009-09-14 03:25:28 -0400 | [diff] [blame] | 12 | * Nicolas Pitre <nico@fluxnic.net> |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 13 | * Andrew Christian <andrew.christian@hp.com> |
| 14 | * |
| 15 | * Converted to the RTC subsystem and Driver Model |
| 16 | * by Richard Purdie <rpurdie@rpsys.net> |
| 17 | * |
| 18 | * This program is free software; you can redistribute it and/or |
| 19 | * modify it under the terms of the GNU General Public License |
| 20 | * as published by the Free Software Foundation; either version |
| 21 | * 2 of the License, or (at your option) any later version. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/rtc.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/fs.h> |
| 29 | #include <linux/interrupt.h> |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 30 | #include <linux/pm.h> |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 31 | #include <linux/slab.h> |
| 32 | #include <linux/clk.h> |
| 33 | #include <linux/io.h> |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 34 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 35 | #include <mach/hardware.h> |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 36 | #include <asm/irq.h> |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 37 | |
Marcelo Roberto Jimenez | a404ad1 | 2010-10-18 22:33:53 +0100 | [diff] [blame] | 38 | #define RTC_DEF_DIVIDER (32768 - 1) |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 39 | #define RTC_DEF_TRIM 0 |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 40 | #define RTC_FREQ 1024 |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 41 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 42 | #define RCNR 0x00 /* RTC Count Register */ |
| 43 | #define RTAR 0x04 /* RTC Alarm Register */ |
| 44 | #define RTSR 0x08 /* RTC Status Register */ |
| 45 | #define RTTR 0x0c /* RTC Timer Trim Register */ |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 46 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 47 | #define RTSR_HZE (1 << 3) /* HZ interrupt enable */ |
| 48 | #define RTSR_ALE (1 << 2) /* RTC alarm interrupt enable */ |
| 49 | #define RTSR_HZ (1 << 1) /* HZ rising-edge detected */ |
| 50 | #define RTSR_AL (1 << 0) /* RTC alarm detected */ |
| 51 | |
| 52 | #define rtc_readl(sa1100_rtc, reg) \ |
| 53 | readl_relaxed((sa1100_rtc)->base + (reg)) |
| 54 | #define rtc_writel(sa1100_rtc, reg, value) \ |
| 55 | writel_relaxed((value), (sa1100_rtc)->base + (reg)) |
| 56 | |
| 57 | struct sa1100_rtc { |
| 58 | struct resource *ress; |
| 59 | void __iomem *base; |
| 60 | struct clk *clk; |
| 61 | int irq_1Hz; |
| 62 | int irq_Alrm; |
| 63 | struct rtc_device *rtc; |
| 64 | spinlock_t lock; /* Protects this structure */ |
| 65 | }; |
Russell King | 797276ec | 2008-04-20 12:30:41 +0100 | [diff] [blame] | 66 | /* |
| 67 | * Calculate the next alarm time given the requested alarm time mask |
| 68 | * and the current time. |
| 69 | */ |
Marcelo Roberto Jimenez | a404ad1 | 2010-10-18 22:33:53 +0100 | [diff] [blame] | 70 | static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, |
| 71 | struct rtc_time *alrm) |
Russell King | 797276ec | 2008-04-20 12:30:41 +0100 | [diff] [blame] | 72 | { |
| 73 | unsigned long next_time; |
| 74 | unsigned long now_time; |
| 75 | |
| 76 | next->tm_year = now->tm_year; |
| 77 | next->tm_mon = now->tm_mon; |
| 78 | next->tm_mday = now->tm_mday; |
| 79 | next->tm_hour = alrm->tm_hour; |
| 80 | next->tm_min = alrm->tm_min; |
| 81 | next->tm_sec = alrm->tm_sec; |
| 82 | |
| 83 | rtc_tm_to_time(now, &now_time); |
| 84 | rtc_tm_to_time(next, &next_time); |
| 85 | |
| 86 | if (next_time < now_time) { |
| 87 | /* Advance one day */ |
| 88 | next_time += 60 * 60 * 24; |
| 89 | rtc_time_to_tm(next_time, next); |
| 90 | } |
| 91 | } |
| 92 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 93 | static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id) |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 94 | { |
| 95 | struct platform_device *pdev = to_platform_device(dev_id); |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 96 | struct sa1100_rtc *sa1100_rtc = platform_get_drvdata(pdev); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 97 | unsigned int rtsr; |
| 98 | unsigned long events = 0; |
| 99 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 100 | spin_lock(&sa1100_rtc->lock); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 101 | |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 102 | /* clear interrupt sources */ |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 103 | rtsr = rtc_readl(sa1100_rtc, RTSR); |
| 104 | rtc_writel(sa1100_rtc, RTSR, 0); |
| 105 | |
Marcelo Roberto Jimenez | 7decaa5 | 2010-10-18 22:35:54 +0100 | [diff] [blame] | 106 | /* Fix for a nasty initialization problem the in SA11xx RTSR register. |
| 107 | * See also the comments in sa1100_rtc_probe(). */ |
| 108 | if (rtsr & (RTSR_ALE | RTSR_HZE)) { |
| 109 | /* This is the original code, before there was the if test |
| 110 | * above. This code does not clear interrupts that were not |
| 111 | * enabled. */ |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 112 | rtc_writel(sa1100_rtc, RTSR, (RTSR_AL | RTSR_HZ) & (rtsr >> 2)); |
Marcelo Roberto Jimenez | 7decaa5 | 2010-10-18 22:35:54 +0100 | [diff] [blame] | 113 | } else { |
| 114 | /* For some reason, it is possible to enter this routine |
| 115 | * without interruptions enabled, it has been tested with |
| 116 | * several units (Bug in SA11xx chip?). |
| 117 | * |
| 118 | * This situation leads to an infinite "loop" of interrupt |
| 119 | * routine calling and as a result the processor seems to |
| 120 | * lock on its first call to open(). */ |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 121 | rtc_writel(sa1100_rtc, RTSR, (RTSR_AL | RTSR_HZ)); |
Marcelo Roberto Jimenez | 7decaa5 | 2010-10-18 22:35:54 +0100 | [diff] [blame] | 122 | } |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 123 | |
| 124 | /* clear alarm interrupt if it has occurred */ |
| 125 | if (rtsr & RTSR_AL) |
| 126 | rtsr &= ~RTSR_ALE; |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 127 | rtc_writel(sa1100_rtc, RTSR, rtsr & (RTSR_ALE | RTSR_HZE)); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 128 | |
| 129 | /* update irq data & counter */ |
| 130 | if (rtsr & RTSR_AL) |
| 131 | events |= RTC_AF | RTC_IRQF; |
| 132 | if (rtsr & RTSR_HZ) |
| 133 | events |= RTC_UF | RTC_IRQF; |
| 134 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 135 | rtc_update_irq(sa1100_rtc->rtc, 1, events); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 136 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 137 | spin_unlock(&sa1100_rtc->lock); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 138 | |
| 139 | return IRQ_HANDLED; |
| 140 | } |
| 141 | |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 142 | static int sa1100_rtc_open(struct device *dev) |
| 143 | { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 144 | struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 145 | int ret; |
| 146 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 147 | ret = request_irq(sa1100_rtc->irq_1Hz, sa1100_rtc_interrupt, |
| 148 | IRQF_DISABLED, "rtc 1Hz", dev); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 149 | if (ret) { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 150 | dev_err(dev, "IRQ %d already in use.\n", sa1100_rtc->irq_1Hz); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 151 | goto fail_ui; |
| 152 | } |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 153 | ret = request_irq(sa1100_rtc->irq_Alrm, sa1100_rtc_interrupt, |
| 154 | IRQF_DISABLED, "rtc Alrm", dev); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 155 | if (ret) { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 156 | dev_err(dev, "IRQ %d already in use.\n", sa1100_rtc->irq_Alrm); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 157 | goto fail_ai; |
| 158 | } |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 159 | sa1100_rtc->rtc->max_user_freq = RTC_FREQ; |
| 160 | rtc_irq_set_freq(sa1100_rtc->rtc, NULL, RTC_FREQ); |
Marcelo Roberto Jimenez | d2ccb52 | 2010-12-16 21:31:32 +0100 | [diff] [blame] | 161 | |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 162 | return 0; |
| 163 | |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 164 | fail_ai: |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 165 | free_irq(sa1100_rtc->irq_1Hz, dev); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 166 | fail_ui: |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static void sa1100_rtc_release(struct device *dev) |
| 171 | { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 172 | struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 173 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 174 | spin_lock_irq(&sa1100_rtc->lock); |
| 175 | rtc_writel(sa1100_rtc, RTSR, 0); |
| 176 | spin_unlock_irq(&sa1100_rtc->lock); |
| 177 | |
| 178 | free_irq(sa1100_rtc->irq_Alrm, dev); |
| 179 | free_irq(sa1100_rtc->irq_1Hz, dev); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 180 | } |
| 181 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 182 | static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 183 | { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 184 | struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev); |
| 185 | unsigned int rtsr; |
| 186 | |
| 187 | spin_lock_irq(&sa1100_rtc->lock); |
| 188 | |
| 189 | rtsr = rtc_readl(sa1100_rtc, RTSR); |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 190 | if (enabled) |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 191 | rtsr |= RTSR_ALE; |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 192 | else |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 193 | rtsr &= ~RTSR_ALE; |
| 194 | rtc_writel(sa1100_rtc, RTSR, rtsr); |
| 195 | |
| 196 | spin_unlock_irq(&sa1100_rtc->lock); |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 200 | static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 201 | { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 202 | struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev); |
| 203 | |
| 204 | rtc_time_to_tm(rtc_readl(sa1100_rtc, RCNR), tm); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 209 | { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 210 | struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 211 | unsigned long time; |
| 212 | int ret; |
| 213 | |
| 214 | ret = rtc_tm_to_time(tm, &time); |
| 215 | if (ret == 0) |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 216 | rtc_writel(sa1100_rtc, RCNR, time); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 221 | { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 222 | struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev); |
| 223 | unsigned long time; |
| 224 | unsigned int rtsr; |
David Brownell | 32b49da | 2007-02-20 13:58:13 -0800 | [diff] [blame] | 225 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 226 | time = rtc_readl(sa1100_rtc, RCNR); |
| 227 | rtc_time_to_tm(time, &alrm->time); |
| 228 | rtsr = rtc_readl(sa1100_rtc, RTSR); |
David Brownell | 32b49da | 2007-02-20 13:58:13 -0800 | [diff] [blame] | 229 | alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0; |
| 230 | alrm->pending = (rtsr & RTSR_AL) ? 1 : 0; |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 235 | { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 236 | struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev); |
Jett.Zhou | 4287475 | 2011-11-30 12:26:22 +0800 | [diff] [blame] | 237 | struct rtc_time now_tm, alarm_tm; |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 238 | unsigned long time, alarm; |
| 239 | unsigned int rtsr; |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 240 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 241 | spin_lock_irq(&sa1100_rtc->lock); |
Jett.Zhou | 4287475 | 2011-11-30 12:26:22 +0800 | [diff] [blame] | 242 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 243 | time = rtc_readl(sa1100_rtc, RCNR); |
| 244 | rtc_time_to_tm(time, &now_tm); |
| 245 | rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->time); |
| 246 | rtc_tm_to_time(&alarm_tm, &alarm); |
| 247 | rtc_writel(sa1100_rtc, RTAR, alarm); |
| 248 | |
| 249 | rtsr = rtc_readl(sa1100_rtc, RTSR); |
Jett.Zhou | 4287475 | 2011-11-30 12:26:22 +0800 | [diff] [blame] | 250 | if (alrm->enabled) |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 251 | rtsr |= RTSR_ALE; |
Jett.Zhou | 4287475 | 2011-11-30 12:26:22 +0800 | [diff] [blame] | 252 | else |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 253 | rtsr &= ~RTSR_ALE; |
| 254 | rtc_writel(sa1100_rtc, RTSR, rtsr); |
Jett.Zhou | 4287475 | 2011-11-30 12:26:22 +0800 | [diff] [blame] | 255 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 256 | spin_unlock_irq(&sa1100_rtc->lock); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 257 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 258 | return 0; |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq) |
| 262 | { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 263 | struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 264 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 265 | seq_printf(seq, "trim/divider\t\t: 0x%08x\n", |
| 266 | rtc_readl(sa1100_rtc, RTTR)); |
| 267 | seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", |
| 268 | rtc_readl(sa1100_rtc, RTSR)); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 272 | static const struct rtc_class_ops sa1100_rtc_ops = { |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 273 | .open = sa1100_rtc_open, |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 274 | .release = sa1100_rtc_release, |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 275 | .read_time = sa1100_rtc_read_time, |
| 276 | .set_time = sa1100_rtc_set_time, |
| 277 | .read_alarm = sa1100_rtc_read_alarm, |
| 278 | .set_alarm = sa1100_rtc_set_alarm, |
| 279 | .proc = sa1100_rtc_proc, |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 280 | .alarm_irq_enable = sa1100_rtc_alarm_irq_enable, |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | static int sa1100_rtc_probe(struct platform_device *pdev) |
| 284 | { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 285 | struct sa1100_rtc *sa1100_rtc; |
| 286 | unsigned int rttr; |
| 287 | int ret; |
| 288 | |
| 289 | sa1100_rtc = kzalloc(sizeof(struct sa1100_rtc), GFP_KERNEL); |
| 290 | if (!sa1100_rtc) |
| 291 | return -ENOMEM; |
| 292 | |
| 293 | spin_lock_init(&sa1100_rtc->lock); |
| 294 | platform_set_drvdata(pdev, sa1100_rtc); |
| 295 | |
| 296 | ret = -ENXIO; |
| 297 | sa1100_rtc->ress = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 298 | if (!sa1100_rtc->ress) { |
| 299 | dev_err(&pdev->dev, "No I/O memory resource defined\n"); |
| 300 | goto err_ress; |
| 301 | } |
| 302 | |
| 303 | sa1100_rtc->irq_1Hz = platform_get_irq(pdev, 0); |
| 304 | if (sa1100_rtc->irq_1Hz < 0) { |
| 305 | dev_err(&pdev->dev, "No 1Hz IRQ resource defined\n"); |
| 306 | goto err_ress; |
| 307 | } |
| 308 | sa1100_rtc->irq_Alrm = platform_get_irq(pdev, 1); |
| 309 | if (sa1100_rtc->irq_Alrm < 0) { |
| 310 | dev_err(&pdev->dev, "No alarm IRQ resource defined\n"); |
| 311 | goto err_ress; |
| 312 | } |
| 313 | |
| 314 | ret = -ENOMEM; |
| 315 | sa1100_rtc->base = ioremap(sa1100_rtc->ress->start, |
| 316 | resource_size(sa1100_rtc->ress)); |
| 317 | if (!sa1100_rtc->base) { |
| 318 | dev_err(&pdev->dev, "Unable to map pxa RTC I/O memory\n"); |
| 319 | goto err_map; |
| 320 | } |
| 321 | |
| 322 | sa1100_rtc->clk = clk_get(&pdev->dev, NULL); |
| 323 | if (IS_ERR(sa1100_rtc->clk)) { |
| 324 | dev_err(&pdev->dev, "failed to find rtc clock source\n"); |
| 325 | ret = PTR_ERR(sa1100_rtc->clk); |
| 326 | goto err_clk; |
| 327 | } |
| 328 | clk_prepare(sa1100_rtc->clk); |
| 329 | clk_enable(sa1100_rtc->clk); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 330 | |
| 331 | /* |
| 332 | * According to the manual we should be able to let RTTR be zero |
| 333 | * and then a default diviser for a 32.768KHz clock is used. |
| 334 | * Apparently this doesn't work, at least for my SA1110 rev 5. |
| 335 | * If the clock divider is uninitialized then reset it to the |
| 336 | * default value to get the 1Hz clock. |
| 337 | */ |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 338 | if (rtc_readl(sa1100_rtc, RTTR) == 0) { |
| 339 | rttr = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16); |
| 340 | rtc_writel(sa1100_rtc, RTTR, rttr); |
| 341 | dev_warn(&pdev->dev, "warning: initializing default clock" |
| 342 | " divider/trim value\n"); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 343 | /* The current RTC value probably doesn't make sense either */ |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 344 | rtc_writel(sa1100_rtc, RCNR, 0); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Uli Luckas | e5a2c9c | 2008-06-18 09:54:03 +0100 | [diff] [blame] | 347 | device_init_wakeup(&pdev->dev, 1); |
| 348 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 349 | sa1100_rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, |
| 350 | &sa1100_rtc_ops, THIS_MODULE); |
| 351 | if (IS_ERR(sa1100_rtc->rtc)) { |
| 352 | dev_err(&pdev->dev, "Failed to register RTC device -> %d\n", |
| 353 | ret); |
| 354 | goto err_rtc_reg; |
| 355 | } |
Marcelo Roberto Jimenez | 7decaa5 | 2010-10-18 22:35:54 +0100 | [diff] [blame] | 356 | /* Fix for a nasty initialization problem the in SA11xx RTSR register. |
| 357 | * See also the comments in sa1100_rtc_interrupt(). |
| 358 | * |
| 359 | * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an |
| 360 | * interrupt pending, even though interrupts were never enabled. |
| 361 | * In this case, this bit it must be reset before enabling |
| 362 | * interruptions to avoid a nonexistent interrupt to occur. |
| 363 | * |
| 364 | * In principle, the same problem would apply to bit 0, although it has |
| 365 | * never been observed to happen. |
| 366 | * |
| 367 | * This issue is addressed both here and in sa1100_rtc_interrupt(). |
| 368 | * If the issue is not addressed here, in the times when the processor |
| 369 | * wakes up with the bit set there will be one spurious interrupt. |
| 370 | * |
| 371 | * The issue is also dealt with in sa1100_rtc_interrupt() to be on the |
| 372 | * safe side, once the condition that lead to this strange |
| 373 | * initialization is unknown and could in principle happen during |
| 374 | * normal processing. |
| 375 | * |
| 376 | * Notice that clearing bit 1 and 0 is accomplished by writting ONES to |
| 377 | * the corresponding bits in RTSR. */ |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 378 | rtc_writel(sa1100_rtc, RTSR, (RTSR_AL | RTSR_HZ)); |
Marcelo Roberto Jimenez | 7decaa5 | 2010-10-18 22:35:54 +0100 | [diff] [blame] | 379 | |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 380 | return 0; |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 381 | |
| 382 | err_rtc_reg: |
| 383 | err_clk: |
| 384 | iounmap(sa1100_rtc->base); |
| 385 | err_ress: |
| 386 | err_map: |
| 387 | kfree(sa1100_rtc); |
| 388 | return ret; |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static int sa1100_rtc_remove(struct platform_device *pdev) |
| 392 | { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 393 | struct sa1100_rtc *sa1100_rtc = platform_get_drvdata(pdev); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 394 | |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 395 | rtc_device_unregister(sa1100_rtc->rtc); |
| 396 | clk_disable(sa1100_rtc->clk); |
| 397 | clk_unprepare(sa1100_rtc->clk); |
| 398 | iounmap(sa1100_rtc->base); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
Russell King | 6bc54e6 | 2007-11-12 22:49:58 +0000 | [diff] [blame] | 402 | #ifdef CONFIG_PM |
Haojian Zhuang | 5d027cd | 2009-07-21 14:31:09 +0800 | [diff] [blame] | 403 | static int sa1100_rtc_suspend(struct device *dev) |
Russell King | 6bc54e6 | 2007-11-12 22:49:58 +0000 | [diff] [blame] | 404 | { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 405 | struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev); |
| 406 | |
Haojian Zhuang | 5d027cd | 2009-07-21 14:31:09 +0800 | [diff] [blame] | 407 | if (device_may_wakeup(dev)) |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 408 | enable_irq_wake(sa1100_rtc->irq_Alrm); |
Russell King | 6bc54e6 | 2007-11-12 22:49:58 +0000 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
Haojian Zhuang | 5d027cd | 2009-07-21 14:31:09 +0800 | [diff] [blame] | 412 | static int sa1100_rtc_resume(struct device *dev) |
Russell King | 6bc54e6 | 2007-11-12 22:49:58 +0000 | [diff] [blame] | 413 | { |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 414 | struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev); |
| 415 | |
Haojian Zhuang | 5d027cd | 2009-07-21 14:31:09 +0800 | [diff] [blame] | 416 | if (device_may_wakeup(dev)) |
Jett.Zhou | 7cea006 | 2011-11-30 12:26:23 +0800 | [diff] [blame] | 417 | disable_irq_wake(sa1100_rtc->irq_Alrm); |
Russell King | 6bc54e6 | 2007-11-12 22:49:58 +0000 | [diff] [blame] | 418 | return 0; |
| 419 | } |
Haojian Zhuang | 5d027cd | 2009-07-21 14:31:09 +0800 | [diff] [blame] | 420 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 421 | static const struct dev_pm_ops sa1100_rtc_pm_ops = { |
Haojian Zhuang | 5d027cd | 2009-07-21 14:31:09 +0800 | [diff] [blame] | 422 | .suspend = sa1100_rtc_suspend, |
| 423 | .resume = sa1100_rtc_resume, |
| 424 | }; |
Russell King | 6bc54e6 | 2007-11-12 22:49:58 +0000 | [diff] [blame] | 425 | #endif |
| 426 | |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 427 | static struct platform_driver sa1100_rtc_driver = { |
| 428 | .probe = sa1100_rtc_probe, |
| 429 | .remove = sa1100_rtc_remove, |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 430 | .driver = { |
Haojian Zhuang | 5d027cd | 2009-07-21 14:31:09 +0800 | [diff] [blame] | 431 | .name = "sa1100-rtc", |
| 432 | #ifdef CONFIG_PM |
| 433 | .pm = &sa1100_rtc_pm_ops, |
| 434 | #endif |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 435 | }, |
| 436 | }; |
| 437 | |
Axel Lin | 0c4eae6 | 2012-01-10 15:10:48 -0800 | [diff] [blame^] | 438 | module_platform_driver(sa1100_rtc_driver); |
Richard Purdie | e842f1c | 2006-03-27 01:16:46 -0800 | [diff] [blame] | 439 | |
| 440 | MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>"); |
| 441 | MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)"); |
| 442 | MODULE_LICENSE("GPL"); |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 443 | MODULE_ALIAS("platform:sa1100-rtc"); |