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