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