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> |
Lothar Waßmann | 28a0c88 | 2013-08-13 16:00:56 -0700 | [diff] [blame] | 26 | #include <linux/delay.h> |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 27 | #include <linux/rtc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Marek Vasut | dd8d20a | 2012-06-26 16:29:27 +0800 | [diff] [blame] | 29 | #include <linux/of_device.h> |
Sachin Kamat | c8a6046 | 2013-02-21 16:44:28 -0800 | [diff] [blame] | 30 | #include <linux/of.h> |
Wolfram Sang | 1a71fb8 | 2011-09-27 22:21:37 +0200 | [diff] [blame] | 31 | #include <linux/stmp_device.h> |
| 32 | #include <linux/stmp3xxx_rtc_wdt.h> |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 33 | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 34 | #define STMP3XXX_RTC_CTRL 0x0 |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 35 | #define STMP3XXX_RTC_CTRL_SET 0x4 |
| 36 | #define STMP3XXX_RTC_CTRL_CLR 0x8 |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 37 | #define STMP3XXX_RTC_CTRL_ALARM_IRQ_EN 0x00000001 |
| 38 | #define STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN 0x00000002 |
| 39 | #define STMP3XXX_RTC_CTRL_ALARM_IRQ 0x00000004 |
Wolfram Sang | 1a71fb8 | 2011-09-27 22:21:37 +0200 | [diff] [blame] | 40 | #define STMP3XXX_RTC_CTRL_WATCHDOGEN 0x00000010 |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 41 | |
| 42 | #define STMP3XXX_RTC_STAT 0x10 |
| 43 | #define STMP3XXX_RTC_STAT_STALE_SHIFT 16 |
| 44 | #define STMP3XXX_RTC_STAT_RTC_PRESENT 0x80000000 |
Uwe Kleine-König | 7f48b21 | 2015-04-16 12:45:04 -0700 | [diff] [blame] | 45 | #define STMP3XXX_RTC_STAT_XTAL32000_PRESENT 0x10000000 |
| 46 | #define STMP3XXX_RTC_STAT_XTAL32768_PRESENT 0x08000000 |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 47 | |
| 48 | #define STMP3XXX_RTC_SECONDS 0x30 |
| 49 | |
| 50 | #define STMP3XXX_RTC_ALARM 0x40 |
| 51 | |
Wolfram Sang | 1a71fb8 | 2011-09-27 22:21:37 +0200 | [diff] [blame] | 52 | #define STMP3XXX_RTC_WATCHDOG 0x50 |
| 53 | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 54 | #define STMP3XXX_RTC_PERSISTENT0 0x60 |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 55 | #define STMP3XXX_RTC_PERSISTENT0_SET 0x64 |
| 56 | #define STMP3XXX_RTC_PERSISTENT0_CLR 0x68 |
Uwe Kleine-König | 7f48b21 | 2015-04-16 12:45:04 -0700 | [diff] [blame] | 57 | #define STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE (1 << 0) |
| 58 | #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN (1 << 1) |
| 59 | #define STMP3XXX_RTC_PERSISTENT0_ALARM_EN (1 << 2) |
| 60 | #define STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP (1 << 4) |
| 61 | #define STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP (1 << 5) |
| 62 | #define STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ (1 << 6) |
| 63 | #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE (1 << 7) |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 64 | |
Wolfram Sang | 1a71fb8 | 2011-09-27 22:21:37 +0200 | [diff] [blame] | 65 | #define STMP3XXX_RTC_PERSISTENT1 0x70 |
| 66 | /* missing bitmask in headers */ |
| 67 | #define STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER 0x80000000 |
| 68 | |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 69 | struct stmp3xxx_rtc_data { |
| 70 | struct rtc_device *rtc; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 71 | void __iomem *io; |
Wolfram Sang | 7e794cb | 2011-05-25 12:56:53 +0200 | [diff] [blame] | 72 | int irq_alarm; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Wolfram Sang | 1a71fb8 | 2011-09-27 22:21:37 +0200 | [diff] [blame] | 75 | #if IS_ENABLED(CONFIG_STMP3XXX_RTC_WATCHDOG) |
| 76 | /** |
| 77 | * stmp3xxx_wdt_set_timeout - configure the watchdog inside the STMP3xxx RTC |
| 78 | * @dev: the parent device of the watchdog (= the RTC) |
| 79 | * @timeout: the desired value for the timeout register of the watchdog. |
| 80 | * 0 disables the watchdog |
| 81 | * |
| 82 | * The watchdog needs one register and two bits which are in the RTC domain. |
| 83 | * To handle the resource conflict, the RTC driver will create another |
| 84 | * platform_device for the watchdog driver as a child of the RTC device. |
| 85 | * The watchdog driver is passed the below accessor function via platform_data |
| 86 | * to configure the watchdog. Locking is not needed because accessing SET/CLR |
| 87 | * registers is atomic. |
| 88 | */ |
| 89 | |
| 90 | static void stmp3xxx_wdt_set_timeout(struct device *dev, u32 timeout) |
| 91 | { |
| 92 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev); |
| 93 | |
| 94 | if (timeout) { |
| 95 | writel(timeout, rtc_data->io + STMP3XXX_RTC_WATCHDOG); |
| 96 | writel(STMP3XXX_RTC_CTRL_WATCHDOGEN, |
| 97 | rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_SET); |
| 98 | writel(STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER, |
| 99 | rtc_data->io + STMP3XXX_RTC_PERSISTENT1 + STMP_OFFSET_REG_SET); |
| 100 | } else { |
| 101 | writel(STMP3XXX_RTC_CTRL_WATCHDOGEN, |
| 102 | rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR); |
| 103 | writel(STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER, |
| 104 | rtc_data->io + STMP3XXX_RTC_PERSISTENT1 + STMP_OFFSET_REG_CLR); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static struct stmp3xxx_wdt_pdata wdt_pdata = { |
| 109 | .wdt_set_timeout = stmp3xxx_wdt_set_timeout, |
| 110 | }; |
| 111 | |
| 112 | static void stmp3xxx_wdt_register(struct platform_device *rtc_pdev) |
| 113 | { |
| 114 | struct platform_device *wdt_pdev = |
| 115 | platform_device_alloc("stmp3xxx_rtc_wdt", rtc_pdev->id); |
| 116 | |
| 117 | if (wdt_pdev) { |
| 118 | wdt_pdev->dev.parent = &rtc_pdev->dev; |
| 119 | wdt_pdev->dev.platform_data = &wdt_pdata; |
| 120 | platform_device_add(wdt_pdev); |
| 121 | } |
| 122 | } |
| 123 | #else |
| 124 | static void stmp3xxx_wdt_register(struct platform_device *rtc_pdev) |
| 125 | { |
| 126 | } |
| 127 | #endif /* CONFIG_STMP3XXX_RTC_WATCHDOG */ |
| 128 | |
Lothar Waßmann | 28a0c88 | 2013-08-13 16:00:56 -0700 | [diff] [blame] | 129 | static int stmp3xxx_wait_time(struct stmp3xxx_rtc_data *rtc_data) |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 130 | { |
Lothar Waßmann | 28a0c88 | 2013-08-13 16:00:56 -0700 | [diff] [blame] | 131 | int timeout = 5000; /* 3ms according to i.MX28 Ref Manual */ |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 132 | /* |
Lothar Waßmann | 28a0c88 | 2013-08-13 16:00:56 -0700 | [diff] [blame] | 133 | * The i.MX28 Applications Processor Reference Manual, Rev. 1, 2010 |
| 134 | * states: |
| 135 | * | The order in which registers are updated is |
| 136 | * | Persistent 0, 1, 2, 3, 4, 5, Alarm, Seconds. |
| 137 | * | (This list is in bitfield order, from LSB to MSB, as they would |
| 138 | * | appear in the STALE_REGS and NEW_REGS bitfields of the HW_RTC_STAT |
| 139 | * | register. For example, the Seconds register corresponds to |
| 140 | * | STALE_REGS or NEW_REGS containing 0x80.) |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 141 | */ |
Lothar Waßmann | 28a0c88 | 2013-08-13 16:00:56 -0700 | [diff] [blame] | 142 | do { |
| 143 | if (!(readl(rtc_data->io + STMP3XXX_RTC_STAT) & |
| 144 | (0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT))) |
| 145 | return 0; |
| 146 | udelay(1); |
| 147 | } while (--timeout > 0); |
| 148 | return (readl(rtc_data->io + STMP3XXX_RTC_STAT) & |
| 149 | (0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT)) ? -ETIME : 0; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* Time read/write */ |
| 153 | static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm) |
| 154 | { |
Lothar Waßmann | 28a0c88 | 2013-08-13 16:00:56 -0700 | [diff] [blame] | 155 | int ret; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 156 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev); |
| 157 | |
Lothar Waßmann | 28a0c88 | 2013-08-13 16:00:56 -0700 | [diff] [blame] | 158 | ret = stmp3xxx_wait_time(rtc_data); |
| 159 | if (ret) |
| 160 | return ret; |
| 161 | |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 162 | 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] | 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int stmp3xxx_rtc_set_mmss(struct device *dev, unsigned long t) |
| 167 | { |
| 168 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev); |
| 169 | |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 170 | writel(t, rtc_data->io + STMP3XXX_RTC_SECONDS); |
Lothar Waßmann | 28a0c88 | 2013-08-13 16:00:56 -0700 | [diff] [blame] | 171 | return stmp3xxx_wait_time(rtc_data); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* interrupt(s) handler */ |
| 175 | static irqreturn_t stmp3xxx_rtc_interrupt(int irq, void *dev_id) |
| 176 | { |
| 177 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev_id); |
Wolfram Sang | 7e794cb | 2011-05-25 12:56:53 +0200 | [diff] [blame] | 178 | u32 status = readl(rtc_data->io + STMP3XXX_RTC_CTRL); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 179 | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 180 | if (status & STMP3XXX_RTC_CTRL_ALARM_IRQ) { |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 181 | writel(STMP3XXX_RTC_CTRL_ALARM_IRQ, |
| 182 | rtc_data->io + STMP3XXX_RTC_CTRL_CLR); |
Wolfram Sang | 7e794cb | 2011-05-25 12:56:53 +0200 | [diff] [blame] | 183 | rtc_update_irq(rtc_data->rtc, 1, RTC_AF | RTC_IRQF); |
| 184 | return IRQ_HANDLED; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Wolfram Sang | 7e794cb | 2011-05-25 12:56:53 +0200 | [diff] [blame] | 187 | return IRQ_NONE; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static int stmp3xxx_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 191 | { |
| 192 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 193 | |
| 194 | if (enabled) { |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 195 | writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN | |
| 196 | STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN, |
| 197 | rtc_data->io + STMP3XXX_RTC_PERSISTENT0_SET); |
| 198 | writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN, |
| 199 | rtc_data->io + STMP3XXX_RTC_CTRL_SET); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 200 | } else { |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 201 | writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN | |
| 202 | STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN, |
| 203 | rtc_data->io + STMP3XXX_RTC_PERSISTENT0_CLR); |
| 204 | writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN, |
| 205 | rtc_data->io + STMP3XXX_RTC_CTRL_CLR); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 206 | } |
| 207 | return 0; |
| 208 | } |
| 209 | |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 210 | static int stmp3xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) |
| 211 | { |
| 212 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev); |
| 213 | |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 214 | 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] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int stmp3xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) |
| 219 | { |
| 220 | unsigned long t; |
| 221 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev); |
| 222 | |
| 223 | rtc_tm_to_time(&alm->time, &t); |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 224 | writel(t, rtc_data->io + STMP3XXX_RTC_ALARM); |
Wolfram Sang | 7e794cb | 2011-05-25 12:56:53 +0200 | [diff] [blame] | 225 | |
| 226 | stmp3xxx_alarm_irq_enable(dev, alm->enabled); |
| 227 | |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static struct rtc_class_ops stmp3xxx_rtc_ops = { |
| 232 | .alarm_irq_enable = |
| 233 | stmp3xxx_alarm_irq_enable, |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 234 | .read_time = stmp3xxx_rtc_gettime, |
| 235 | .set_mmss = stmp3xxx_rtc_set_mmss, |
| 236 | .read_alarm = stmp3xxx_rtc_read_alarm, |
| 237 | .set_alarm = stmp3xxx_rtc_set_alarm, |
| 238 | }; |
| 239 | |
| 240 | static int stmp3xxx_rtc_remove(struct platform_device *pdev) |
| 241 | { |
| 242 | struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(pdev); |
| 243 | |
| 244 | if (!rtc_data) |
| 245 | return 0; |
| 246 | |
Wolfram Sang | 7e794cb | 2011-05-25 12:56:53 +0200 | [diff] [blame] | 247 | writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN, |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 248 | rtc_data->io + STMP3XXX_RTC_CTRL_CLR); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int stmp3xxx_rtc_probe(struct platform_device *pdev) |
| 254 | { |
| 255 | struct stmp3xxx_rtc_data *rtc_data; |
| 256 | struct resource *r; |
Uwe Kleine-König | 7f48b21 | 2015-04-16 12:45:04 -0700 | [diff] [blame] | 257 | u32 rtc_stat; |
| 258 | u32 pers0_set, pers0_clr; |
| 259 | u32 crystalfreq = 0; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 260 | int err; |
| 261 | |
Jingoo Han | 87a8142 | 2013-04-29 16:20:55 -0700 | [diff] [blame] | 262 | rtc_data = devm_kzalloc(&pdev->dev, sizeof(*rtc_data), GFP_KERNEL); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 263 | if (!rtc_data) |
| 264 | return -ENOMEM; |
| 265 | |
| 266 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 267 | if (!r) { |
| 268 | dev_err(&pdev->dev, "failed to get resource\n"); |
Jingoo Han | 87a8142 | 2013-04-29 16:20:55 -0700 | [diff] [blame] | 269 | return -ENXIO; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Jingoo Han | 87a8142 | 2013-04-29 16:20:55 -0700 | [diff] [blame] | 272 | rtc_data->io = devm_ioremap(&pdev->dev, r->start, resource_size(r)); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 273 | if (!rtc_data->io) { |
| 274 | dev_err(&pdev->dev, "ioremap failed\n"); |
Jingoo Han | 87a8142 | 2013-04-29 16:20:55 -0700 | [diff] [blame] | 275 | return -EIO; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | rtc_data->irq_alarm = platform_get_irq(pdev, 0); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 279 | |
Uwe Kleine-König | 7f48b21 | 2015-04-16 12:45:04 -0700 | [diff] [blame] | 280 | rtc_stat = readl(rtc_data->io + STMP3XXX_RTC_STAT); |
| 281 | if (!(rtc_stat & STMP3XXX_RTC_STAT_RTC_PRESENT)) { |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 282 | dev_err(&pdev->dev, "no device onboard\n"); |
Jingoo Han | 87a8142 | 2013-04-29 16:20:55 -0700 | [diff] [blame] | 283 | return -ENODEV; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Wolfram Sang | a91d2ba | 2011-05-25 12:56:51 +0200 | [diff] [blame] | 286 | platform_set_drvdata(pdev, rtc_data); |
| 287 | |
Fabio Estevam | 4e80b18 | 2013-07-08 16:00:45 -0700 | [diff] [blame] | 288 | err = stmp_reset_block(rtc_data->io); |
| 289 | if (err) { |
| 290 | dev_err(&pdev->dev, "stmp_reset_block failed: %d\n", err); |
| 291 | return err; |
| 292 | } |
| 293 | |
Uwe Kleine-König | 7f48b21 | 2015-04-16 12:45:04 -0700 | [diff] [blame] | 294 | /* |
| 295 | * Obviously the rtc needs a clock input to be able to run. |
| 296 | * This clock can be provided by an external 32k crystal. If that one is |
| 297 | * missing XTAL must not be disabled in suspend which consumes a |
| 298 | * lot of power. Normally the presence and exact frequency (supported |
| 299 | * are 32000 Hz and 32768 Hz) is detectable from fuses, but as reality |
| 300 | * proves these fuses are not blown correctly on all machines, so the |
| 301 | * frequency can be overridden in the device tree. |
| 302 | */ |
| 303 | if (rtc_stat & STMP3XXX_RTC_STAT_XTAL32000_PRESENT) |
| 304 | crystalfreq = 32000; |
| 305 | else if (rtc_stat & STMP3XXX_RTC_STAT_XTAL32768_PRESENT) |
| 306 | crystalfreq = 32768; |
| 307 | |
| 308 | of_property_read_u32(pdev->dev.of_node, "stmp,crystal-freq", |
| 309 | &crystalfreq); |
| 310 | |
| 311 | switch (crystalfreq) { |
| 312 | case 32000: |
| 313 | /* keep 32kHz crystal running in low-power mode */ |
| 314 | pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ | |
| 315 | STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP | |
| 316 | STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE; |
| 317 | pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP; |
| 318 | break; |
| 319 | case 32768: |
| 320 | /* keep 32.768kHz crystal running in low-power mode */ |
| 321 | pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP | |
| 322 | STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE; |
| 323 | pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP | |
| 324 | STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ; |
| 325 | break; |
| 326 | default: |
| 327 | dev_warn(&pdev->dev, |
| 328 | "invalid crystal-freq specified in device-tree. Assuming no crystal\n"); |
| 329 | /* fall-through */ |
| 330 | case 0: |
| 331 | /* keep XTAL on in low-power mode */ |
| 332 | pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP; |
| 333 | pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP | |
| 334 | STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE; |
| 335 | } |
| 336 | |
| 337 | writel(pers0_set, rtc_data->io + STMP3XXX_RTC_PERSISTENT0_SET); |
| 338 | |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 339 | writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 340 | STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN | |
Uwe Kleine-König | 7f48b21 | 2015-04-16 12:45:04 -0700 | [diff] [blame] | 341 | STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE | pers0_clr, |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 342 | rtc_data->io + STMP3XXX_RTC_PERSISTENT0_CLR); |
Wolfram Sang | a91d2ba | 2011-05-25 12:56:51 +0200 | [diff] [blame] | 343 | |
Wolfram Sang | 7e794cb | 2011-05-25 12:56:53 +0200 | [diff] [blame] | 344 | writel(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN | |
| 345 | STMP3XXX_RTC_CTRL_ALARM_IRQ_EN, |
| 346 | rtc_data->io + STMP3XXX_RTC_CTRL_CLR); |
| 347 | |
Jingoo Han | 87a8142 | 2013-04-29 16:20:55 -0700 | [diff] [blame] | 348 | rtc_data->rtc = devm_rtc_device_register(&pdev->dev, pdev->name, |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 349 | &stmp3xxx_rtc_ops, THIS_MODULE); |
Jingoo Han | dfd2a17 | 2013-07-03 15:06:37 -0700 | [diff] [blame] | 350 | if (IS_ERR(rtc_data->rtc)) |
| 351 | return PTR_ERR(rtc_data->rtc); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 352 | |
Jingoo Han | 87a8142 | 2013-04-29 16:20:55 -0700 | [diff] [blame] | 353 | err = devm_request_irq(&pdev->dev, rtc_data->irq_alarm, |
| 354 | stmp3xxx_rtc_interrupt, 0, "RTC alarm", &pdev->dev); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 355 | if (err) { |
| 356 | dev_err(&pdev->dev, "Cannot claim IRQ%d\n", |
| 357 | rtc_data->irq_alarm); |
Jingoo Han | dfd2a17 | 2013-07-03 15:06:37 -0700 | [diff] [blame] | 358 | return err; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 359 | } |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 360 | |
Wolfram Sang | 1a71fb8 | 2011-09-27 22:21:37 +0200 | [diff] [blame] | 361 | stmp3xxx_wdt_register(pdev); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 362 | return 0; |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Jingoo Han | ef69a7f | 2013-04-29 16:21:03 -0700 | [diff] [blame] | 365 | #ifdef CONFIG_PM_SLEEP |
| 366 | static int stmp3xxx_rtc_suspend(struct device *dev) |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 367 | { |
| 368 | return 0; |
| 369 | } |
| 370 | |
Jingoo Han | ef69a7f | 2013-04-29 16:21:03 -0700 | [diff] [blame] | 371 | static int stmp3xxx_rtc_resume(struct device *dev) |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 372 | { |
Jingoo Han | ef69a7f | 2013-04-29 16:21:03 -0700 | [diff] [blame] | 373 | struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 374 | |
Shawn Guo | 36d1da1 | 2013-03-28 23:13:14 +0800 | [diff] [blame] | 375 | stmp_reset_block(rtc_data->io); |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 376 | writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN | |
Wolfram Sang | 47eac33 | 2011-05-25 12:56:49 +0200 | [diff] [blame] | 377 | STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN | |
| 378 | STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE, |
Wolfram Sang | b516715 | 2011-05-25 12:56:52 +0200 | [diff] [blame] | 379 | rtc_data->io + STMP3XXX_RTC_PERSISTENT0_CLR); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 380 | return 0; |
| 381 | } |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 382 | #endif |
| 383 | |
Jingoo Han | ef69a7f | 2013-04-29 16:21:03 -0700 | [diff] [blame] | 384 | static SIMPLE_DEV_PM_OPS(stmp3xxx_rtc_pm_ops, stmp3xxx_rtc_suspend, |
| 385 | stmp3xxx_rtc_resume); |
| 386 | |
Marek Vasut | dd8d20a | 2012-06-26 16:29:27 +0800 | [diff] [blame] | 387 | static const struct of_device_id rtc_dt_ids[] = { |
| 388 | { .compatible = "fsl,stmp3xxx-rtc", }, |
| 389 | { /* sentinel */ } |
| 390 | }; |
| 391 | MODULE_DEVICE_TABLE(of, rtc_dt_ids); |
| 392 | |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 393 | static struct platform_driver stmp3xxx_rtcdrv = { |
| 394 | .probe = stmp3xxx_rtc_probe, |
| 395 | .remove = stmp3xxx_rtc_remove, |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 396 | .driver = { |
| 397 | .name = "stmp3xxx-rtc", |
Jingoo Han | ef69a7f | 2013-04-29 16:21:03 -0700 | [diff] [blame] | 398 | .pm = &stmp3xxx_rtc_pm_ops, |
Sachin Kamat | 462a465 | 2013-11-12 15:10:58 -0800 | [diff] [blame] | 399 | .of_match_table = rtc_dt_ids, |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 400 | }, |
| 401 | }; |
| 402 | |
Axel Lin | 0c4eae6 | 2012-01-10 15:10:48 -0800 | [diff] [blame] | 403 | module_platform_driver(stmp3xxx_rtcdrv); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 404 | |
| 405 | MODULE_DESCRIPTION("STMP3xxx RTC Driver"); |
Wolfram Sang | 7e794cb | 2011-05-25 12:56:53 +0200 | [diff] [blame] | 406 | MODULE_AUTHOR("dmitry pervushin <dpervushin@embeddedalley.com> and " |
| 407 | "Wolfram Sang <w.sang@pengutronix.de>"); |
dmitry pervushin | df17f63 | 2009-09-22 16:46:26 -0700 | [diff] [blame] | 408 | MODULE_LICENSE("GPL"); |