dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Freescale STMP37XX/STMP378X Real Time Clock driver |
| 3 | * |
| 4 | * Copyright (c) 2007 Sigmatel, Inc. |
| 5 | * Peter Hartley, <peter.hartley@sigmatel.com> |
| 6 | * |
| 7 | * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved. |
| 8 | * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved. |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * The code contained herein is licensed under the GNU General Public |
| 13 | * License. You may obtain a copy of the GNU General Public License |
| 14 | * Version 2 or later at the following locations: |
| 15 | * |
| 16 | * http://www.opensource.org/licenses/gpl-license.html |
| 17 | * http://www.gnu.org/copyleft/gpl.html |
| 18 | */ |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/rtc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 26 | |
Wolfram Sang | 46b2121 | 2011-05-25 12:56:50 +0200 | [diff] [blame^] | 27 | #include <mach/common.h> |
| 28 | #include <mach/mxs.h> |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 29 | |
| 30 | #define STMP3XXX_RTC_CTRL 0x0 |
| 31 | #define STMP3XXX_RTC_CTRL_ALARM_IRQ_EN 0x00000001 |
| 32 | #define STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN 0x00000002 |
| 33 | #define STMP3XXX_RTC_CTRL_ALARM_IRQ 0x00000004 |
| 34 | #define STMP3XXX_RTC_CTRL_ONEMSEC_IRQ 0x00000008 |
| 35 | |
| 36 | #define STMP3XXX_RTC_STAT 0x10 |
| 37 | #define STMP3XXX_RTC_STAT_STALE_SHIFT 16 |
| 38 | #define STMP3XXX_RTC_STAT_RTC_PRESENT 0x80000000 |
| 39 | |
| 40 | #define STMP3XXX_RTC_SECONDS 0x30 |
| 41 | |
| 42 | #define STMP3XXX_RTC_ALARM 0x40 |
| 43 | |
| 44 | #define STMP3XXX_RTC_PERSISTENT0 0x60 |
| 45 | #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN 0x00000002 |
| 46 | #define STMP3XXX_RTC_PERSISTENT0_ALARM_EN 0x00000004 |
| 47 | #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE 0x00000080 |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 48 | |
| 49 | struct stmp3xxx_rtc_data { |
| 50 | struct rtc_device *rtc; |
| 51 | unsigned irq_count; |
| 52 | void __iomem *io; |
| 53 | int irq_alarm, irq_1msec; |
| 54 | }; |
| 55 | |
| 56 | static void stmp3xxx_wait_time(struct stmp3xxx_rtc_data *rtc_data) |
| 57 | { |
| 58 | /* |
| 59 | * The datasheet doesn't say which way round the |
| 60 | * NEW_REGS/STALE_REGS bitfields go. In fact it's 0x1=P0, |
| 61 | * 0x2=P1, .., 0x20=P5, 0x40=ALARM, 0x80=SECONDS |
| 62 | */ |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 63 | while (__raw_readl(rtc_data->io + STMP3XXX_RTC_STAT) & |
| 64 | (0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT)) |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 65 | cpu_relax(); |
| 66 | } |
| 67 | |
| 68 | /* Time read/write */ |
| 69 | static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm) |
| 70 | { |
| 71 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev); |
| 72 | |
| 73 | stmp3xxx_wait_time(rtc_data); |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 74 | rtc_time_to_tm(__raw_readl(rtc_data->io + STMP3XXX_RTC_SECONDS), |
| 75 | rtc_tm); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int stmp3xxx_rtc_set_mmss(struct device *dev, unsigned long t) |
| 80 | { |
| 81 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev); |
| 82 | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 83 | __raw_writel(t, rtc_data->io + STMP3XXX_RTC_SECONDS); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 84 | stmp3xxx_wait_time(rtc_data); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* interrupt(s) handler */ |
| 89 | static irqreturn_t stmp3xxx_rtc_interrupt(int irq, void *dev_id) |
| 90 | { |
| 91 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev_id); |
| 92 | u32 status; |
| 93 | u32 events = 0; |
| 94 | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 95 | status = __raw_readl(rtc_data->io + STMP3XXX_RTC_CTRL) & |
| 96 | (STMP3XXX_RTC_CTRL_ALARM_IRQ | |
| 97 | STMP3XXX_RTC_CTRL_ONEMSEC_IRQ); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 98 | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 99 | if (status & STMP3XXX_RTC_CTRL_ALARM_IRQ) { |
Wolfram Sang | 46b2121 | 2011-05-25 12:56:50 +0200 | [diff] [blame^] | 100 | __mxs_clrl(STMP3XXX_RTC_CTRL_ALARM_IRQ, |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 101 | rtc_data->io + STMP3XXX_RTC_CTRL); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 102 | events |= RTC_AF | RTC_IRQF; |
| 103 | } |
| 104 | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 105 | if (status & STMP3XXX_RTC_CTRL_ONEMSEC_IRQ) { |
Wolfram Sang | 46b2121 | 2011-05-25 12:56:50 +0200 | [diff] [blame^] | 106 | __mxs_clrl(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ, |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 107 | rtc_data->io + STMP3XXX_RTC_CTRL); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 108 | if (++rtc_data->irq_count % 1000 == 0) { |
| 109 | events |= RTC_UF | RTC_IRQF; |
| 110 | rtc_data->irq_count = 0; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (events) |
| 115 | rtc_update_irq(rtc_data->rtc, 1, events); |
| 116 | |
| 117 | return IRQ_HANDLED; |
| 118 | } |
| 119 | |
| 120 | static int stmp3xxx_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 121 | { |
| 122 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev); |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 123 | void __iomem *p = rtc_data->io + STMP3XXX_RTC_PERSISTENT0, |
| 124 | *ctl = rtc_data->io + STMP3XXX_RTC_CTRL; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 125 | |
| 126 | if (enabled) { |
Wolfram Sang | 46b2121 | 2011-05-25 12:56:50 +0200 | [diff] [blame^] | 127 | __mxs_setl(STMP3XXX_RTC_PERSISTENT0_ALARM_EN | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 128 | STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN, p); |
Wolfram Sang | 46b2121 | 2011-05-25 12:56:50 +0200 | [diff] [blame^] | 129 | __mxs_setl(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN, ctl); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 130 | } else { |
Wolfram Sang | 46b2121 | 2011-05-25 12:56:50 +0200 | [diff] [blame^] | 131 | __mxs_clrl(STMP3XXX_RTC_PERSISTENT0_ALARM_EN | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 132 | STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN, p); |
Wolfram Sang | 46b2121 | 2011-05-25 12:56:50 +0200 | [diff] [blame^] | 133 | __mxs_clrl(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN, ctl); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 138 | static int stmp3xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) |
| 139 | { |
| 140 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev); |
| 141 | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 142 | rtc_time_to_tm(__raw_readl(rtc_data->io + STMP3XXX_RTC_ALARM), |
| 143 | &alm->time); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int stmp3xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) |
| 148 | { |
| 149 | unsigned long t; |
| 150 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev); |
| 151 | |
| 152 | rtc_tm_to_time(&alm->time, &t); |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 153 | __raw_writel(t, rtc_data->io + STMP3XXX_RTC_ALARM); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static struct rtc_class_ops stmp3xxx_rtc_ops = { |
| 158 | .alarm_irq_enable = |
| 159 | stmp3xxx_alarm_irq_enable, |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 160 | .read_time = stmp3xxx_rtc_gettime, |
| 161 | .set_mmss = stmp3xxx_rtc_set_mmss, |
| 162 | .read_alarm = stmp3xxx_rtc_read_alarm, |
| 163 | .set_alarm = stmp3xxx_rtc_set_alarm, |
| 164 | }; |
| 165 | |
| 166 | static int stmp3xxx_rtc_remove(struct platform_device *pdev) |
| 167 | { |
| 168 | struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(pdev); |
| 169 | |
| 170 | if (!rtc_data) |
| 171 | return 0; |
| 172 | |
Wolfram Sang | 46b2121 | 2011-05-25 12:56:50 +0200 | [diff] [blame^] | 173 | __mxs_clrl(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 174 | STMP3XXX_RTC_CTRL_ALARM_IRQ_EN, |
| 175 | rtc_data->io + STMP3XXX_RTC_CTRL); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 176 | free_irq(rtc_data->irq_alarm, &pdev->dev); |
| 177 | free_irq(rtc_data->irq_1msec, &pdev->dev); |
| 178 | rtc_device_unregister(rtc_data->rtc); |
| 179 | iounmap(rtc_data->io); |
| 180 | kfree(rtc_data); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int stmp3xxx_rtc_probe(struct platform_device *pdev) |
| 186 | { |
| 187 | struct stmp3xxx_rtc_data *rtc_data; |
| 188 | struct resource *r; |
| 189 | int err; |
| 190 | |
| 191 | rtc_data = kzalloc(sizeof *rtc_data, GFP_KERNEL); |
| 192 | if (!rtc_data) |
| 193 | return -ENOMEM; |
| 194 | |
| 195 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 196 | if (!r) { |
| 197 | dev_err(&pdev->dev, "failed to get resource\n"); |
| 198 | err = -ENXIO; |
| 199 | goto out_free; |
| 200 | } |
| 201 | |
| 202 | rtc_data->io = ioremap(r->start, resource_size(r)); |
| 203 | if (!rtc_data->io) { |
| 204 | dev_err(&pdev->dev, "ioremap failed\n"); |
| 205 | err = -EIO; |
| 206 | goto out_free; |
| 207 | } |
| 208 | |
| 209 | rtc_data->irq_alarm = platform_get_irq(pdev, 0); |
| 210 | rtc_data->irq_1msec = platform_get_irq(pdev, 1); |
| 211 | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 212 | if (!(__raw_readl(STMP3XXX_RTC_STAT + rtc_data->io) & |
| 213 | STMP3XXX_RTC_STAT_RTC_PRESENT)) { |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 214 | dev_err(&pdev->dev, "no device onboard\n"); |
| 215 | err = -ENODEV; |
| 216 | goto out_remap; |
| 217 | } |
| 218 | |
Wolfram Sang | 46b2121 | 2011-05-25 12:56:50 +0200 | [diff] [blame^] | 219 | mxs_reset_block(rtc_data->io); |
| 220 | __mxs_clrl(STMP3XXX_RTC_PERSISTENT0_ALARM_EN | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 221 | STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN | |
| 222 | STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE, |
| 223 | rtc_data->io + STMP3XXX_RTC_PERSISTENT0); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 224 | rtc_data->rtc = rtc_device_register(pdev->name, &pdev->dev, |
| 225 | &stmp3xxx_rtc_ops, THIS_MODULE); |
| 226 | if (IS_ERR(rtc_data->rtc)) { |
| 227 | err = PTR_ERR(rtc_data->rtc); |
| 228 | goto out_remap; |
| 229 | } |
| 230 | |
| 231 | rtc_data->irq_count = 0; |
| 232 | err = request_irq(rtc_data->irq_alarm, stmp3xxx_rtc_interrupt, |
| 233 | IRQF_DISABLED, "RTC alarm", &pdev->dev); |
| 234 | if (err) { |
| 235 | dev_err(&pdev->dev, "Cannot claim IRQ%d\n", |
| 236 | rtc_data->irq_alarm); |
| 237 | goto out_irq_alarm; |
| 238 | } |
| 239 | err = request_irq(rtc_data->irq_1msec, stmp3xxx_rtc_interrupt, |
| 240 | IRQF_DISABLED, "RTC tick", &pdev->dev); |
| 241 | if (err) { |
| 242 | dev_err(&pdev->dev, "Cannot claim IRQ%d\n", |
| 243 | rtc_data->irq_1msec); |
| 244 | goto out_irq1; |
| 245 | } |
| 246 | |
| 247 | platform_set_drvdata(pdev, rtc_data); |
| 248 | |
| 249 | return 0; |
| 250 | |
| 251 | out_irq1: |
| 252 | free_irq(rtc_data->irq_alarm, &pdev->dev); |
| 253 | out_irq_alarm: |
Wolfram Sang | 46b2121 | 2011-05-25 12:56:50 +0200 | [diff] [blame^] | 254 | __mxs_clrl(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 255 | STMP3XXX_RTC_CTRL_ALARM_IRQ_EN, |
| 256 | rtc_data->io + STMP3XXX_RTC_CTRL); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 257 | rtc_device_unregister(rtc_data->rtc); |
| 258 | out_remap: |
| 259 | iounmap(rtc_data->io); |
| 260 | out_free: |
| 261 | kfree(rtc_data); |
| 262 | return err; |
| 263 | } |
| 264 | |
| 265 | #ifdef CONFIG_PM |
| 266 | static int stmp3xxx_rtc_suspend(struct platform_device *dev, pm_message_t state) |
| 267 | { |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int stmp3xxx_rtc_resume(struct platform_device *dev) |
| 272 | { |
| 273 | struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(dev); |
| 274 | |
Wolfram Sang | 46b2121 | 2011-05-25 12:56:50 +0200 | [diff] [blame^] | 275 | mxs_reset_block(rtc_data->io); |
| 276 | __mxs_clrl(STMP3XXX_RTC_PERSISTENT0_ALARM_EN | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 277 | STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN | |
| 278 | STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE, |
| 279 | rtc_data->io + STMP3XXX_RTC_PERSISTENT0); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | #else |
| 283 | #define stmp3xxx_rtc_suspend NULL |
| 284 | #define stmp3xxx_rtc_resume NULL |
| 285 | #endif |
| 286 | |
| 287 | static struct platform_driver stmp3xxx_rtcdrv = { |
| 288 | .probe = stmp3xxx_rtc_probe, |
| 289 | .remove = stmp3xxx_rtc_remove, |
| 290 | .suspend = stmp3xxx_rtc_suspend, |
| 291 | .resume = stmp3xxx_rtc_resume, |
| 292 | .driver = { |
| 293 | .name = "stmp3xxx-rtc", |
| 294 | .owner = THIS_MODULE, |
| 295 | }, |
| 296 | }; |
| 297 | |
| 298 | static int __init stmp3xxx_rtc_init(void) |
| 299 | { |
| 300 | return platform_driver_register(&stmp3xxx_rtcdrv); |
| 301 | } |
| 302 | |
| 303 | static void __exit stmp3xxx_rtc_exit(void) |
| 304 | { |
| 305 | platform_driver_unregister(&stmp3xxx_rtcdrv); |
| 306 | } |
| 307 | |
| 308 | module_init(stmp3xxx_rtc_init); |
| 309 | module_exit(stmp3xxx_rtc_exit); |
| 310 | |
| 311 | MODULE_DESCRIPTION("STMP3xxx RTC Driver"); |
| 312 | MODULE_AUTHOR("dmitry pervushin <dpervushin@embeddedalley.com>"); |
| 313 | MODULE_LICENSE("GPL"); |