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