Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 2 | /* drivers/rtc/rtc-s3c.c |
| 3 | * |
Atul Dahiya | e48add8 | 2010-07-20 12:19:14 +0530 | [diff] [blame] | 4 | * Copyright (c) 2010 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com/ |
| 6 | * |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 7 | * Copyright (c) 2004,2006 Simtec Electronics |
| 8 | * Ben Dooks, <ben@simtec.co.uk> |
| 9 | * http://armlinux.simtec.co.uk/ |
| 10 | * |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 11 | * S3C2410/S3C2440/S3C24XX Internal RTC Driver |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/fs.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/rtc.h> |
| 21 | #include <linux/bcd.h> |
| 22 | #include <linux/clk.h> |
Robert P. J. Day | 9974b6e | 2008-02-06 01:38:42 -0800 | [diff] [blame] | 23 | #include <linux/log2.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Thomas Abraham | 39ce408 | 2011-10-24 14:49:04 +0200 | [diff] [blame] | 25 | #include <linux/of.h> |
Marek Szyprowski | 64704c9 | 2019-01-18 14:28:37 +0100 | [diff] [blame] | 26 | #include <linux/of_device.h> |
Sachin Kamat | dbd9acb | 2012-07-30 14:41:48 -0700 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
| 28 | #include <linux/io.h> |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 29 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 30 | #include <asm/irq.h> |
Arnd Bergmann | b9d7c5d | 2013-03-05 12:04:37 +0100 | [diff] [blame] | 31 | #include "rtc-s3c.h" |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 32 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 33 | struct s3c_rtc { |
| 34 | struct device *dev; |
| 35 | struct rtc_device *rtc; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 36 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 37 | void __iomem *base; |
| 38 | struct clk *rtc_clk; |
Chanwoo Choi | df9e26d | 2014-10-13 15:52:35 -0700 | [diff] [blame] | 39 | struct clk *rtc_src_clk; |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 40 | bool alarm_enabled; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 41 | |
Krzysztof Kozlowski | 6b72086 | 2017-06-16 21:28:05 +0200 | [diff] [blame] | 42 | const struct s3c_rtc_data *data; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 43 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 44 | int irq_alarm; |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 45 | spinlock_t alarm_lock; |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 46 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 47 | bool wake_en; |
| 48 | }; |
| 49 | |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 50 | struct s3c_rtc_data { |
Chanwoo Choi | df9e26d | 2014-10-13 15:52:35 -0700 | [diff] [blame] | 51 | bool needs_src_clk; |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 52 | |
| 53 | void (*irq_handler) (struct s3c_rtc *info, int mask); |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 54 | void (*enable) (struct s3c_rtc *info); |
| 55 | void (*disable) (struct s3c_rtc *info); |
| 56 | }; |
| 57 | |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 58 | static int s3c_rtc_enable_clk(struct s3c_rtc *info) |
Donggeun Kim | 88cee8f | 2011-09-14 16:22:19 -0700 | [diff] [blame] | 59 | { |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 60 | int ret; |
Donggeun Kim | 88cee8f | 2011-09-14 16:22:19 -0700 | [diff] [blame] | 61 | |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 62 | ret = clk_enable(info->rtc_clk); |
| 63 | if (ret) |
| 64 | return ret; |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 65 | |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 66 | if (info->data->needs_src_clk) { |
| 67 | ret = clk_enable(info->rtc_src_clk); |
| 68 | if (ret) { |
| 69 | clk_disable(info->rtc_clk); |
| 70 | return ret; |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 71 | } |
Joonyoung Shim | 1fb1c35 | 2015-08-12 19:21:46 +0900 | [diff] [blame] | 72 | } |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 73 | return 0; |
Chanwoo Choi | 24e1455 | 2015-04-16 12:45:15 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void s3c_rtc_disable_clk(struct s3c_rtc *info) |
| 77 | { |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 78 | if (info->data->needs_src_clk) |
| 79 | clk_disable(info->rtc_src_clk); |
| 80 | clk_disable(info->rtc_clk); |
Donggeun Kim | 88cee8f | 2011-09-14 16:22:19 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Marek Szyprowski | ce9af893 | 2020-12-02 12:13:18 +0100 | [diff] [blame] | 83 | /* IRQ Handler */ |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 84 | static irqreturn_t s3c_rtc_alarmirq(int irq, void *id) |
| 85 | { |
| 86 | struct s3c_rtc *info = (struct s3c_rtc *)id; |
| 87 | |
| 88 | if (info->data->irq_handler) |
| 89 | info->data->irq_handler(info, S3C2410_INTP_ALM); |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 90 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 91 | return IRQ_HANDLED; |
| 92 | } |
| 93 | |
| 94 | /* Update control registers */ |
Axel Lin | 2ec38a0 | 2011-03-04 17:36:19 -0800 | [diff] [blame] | 95 | static int s3c_rtc_setaie(struct device *dev, unsigned int enabled) |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 96 | { |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 97 | struct s3c_rtc *info = dev_get_drvdata(dev); |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 98 | unsigned long flags; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 99 | unsigned int tmp; |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 100 | int ret; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 101 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 102 | dev_dbg(info->dev, "%s: aie=%d\n", __func__, enabled); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 103 | |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 104 | ret = s3c_rtc_enable_clk(info); |
| 105 | if (ret) |
| 106 | return ret; |
Chanwoo Choi | 24e1455 | 2015-04-16 12:45:15 -0700 | [diff] [blame] | 107 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 108 | tmp = readb(info->base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 109 | |
Axel Lin | 2ec38a0 | 2011-03-04 17:36:19 -0800 | [diff] [blame] | 110 | if (enabled) |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 111 | tmp |= S3C2410_RTCALM_ALMEN; |
| 112 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 113 | writeb(tmp, info->base + S3C2410_RTCALM); |
Axel Lin | 2ec38a0 | 2011-03-04 17:36:19 -0800 | [diff] [blame] | 114 | |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 115 | spin_lock_irqsave(&info->alarm_lock, flags); |
| 116 | |
| 117 | if (info->alarm_enabled && !enabled) |
| 118 | s3c_rtc_disable_clk(info); |
| 119 | else if (!info->alarm_enabled && enabled) |
| 120 | ret = s3c_rtc_enable_clk(info); |
| 121 | |
| 122 | info->alarm_enabled = enabled; |
| 123 | spin_unlock_irqrestore(&info->alarm_lock, flags); |
| 124 | |
Chanwoo Choi | 24e1455 | 2015-04-16 12:45:15 -0700 | [diff] [blame] | 125 | s3c_rtc_disable_clk(info); |
Donggeun Kim | 88cee8f | 2011-09-14 16:22:19 -0700 | [diff] [blame] | 126 | |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 127 | return ret; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 130 | /* Time read/write */ |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 131 | static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm) |
| 132 | { |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 133 | struct s3c_rtc *info = dev_get_drvdata(dev); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 134 | unsigned int have_retried = 0; |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 135 | int ret; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 136 | |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 137 | ret = s3c_rtc_enable_clk(info); |
| 138 | if (ret) |
| 139 | return ret; |
Chanwoo Choi | df9e26d | 2014-10-13 15:52:35 -0700 | [diff] [blame] | 140 | |
Krzysztof Kozlowski | fc1afe6 | 2017-06-16 21:28:03 +0200 | [diff] [blame] | 141 | retry_get_time: |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 142 | rtc_tm->tm_min = readb(info->base + S3C2410_RTCMIN); |
| 143 | rtc_tm->tm_hour = readb(info->base + S3C2410_RTCHOUR); |
| 144 | rtc_tm->tm_mday = readb(info->base + S3C2410_RTCDATE); |
| 145 | rtc_tm->tm_mon = readb(info->base + S3C2410_RTCMON); |
| 146 | rtc_tm->tm_year = readb(info->base + S3C2410_RTCYEAR); |
| 147 | rtc_tm->tm_sec = readb(info->base + S3C2410_RTCSEC); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 148 | |
Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 149 | /* the only way to work out whether the system was mid-update |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 150 | * when we read it is to check the second counter, and if it |
| 151 | * is zero, then we re-try the entire read |
| 152 | */ |
| 153 | |
| 154 | if (rtc_tm->tm_sec == 0 && !have_retried) { |
| 155 | have_retried = 1; |
| 156 | goto retry_get_time; |
| 157 | } |
| 158 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 159 | rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec); |
| 160 | rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min); |
| 161 | rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour); |
| 162 | rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday); |
| 163 | rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon); |
| 164 | rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 165 | |
Chanwoo Choi | 24e1455 | 2015-04-16 12:45:15 -0700 | [diff] [blame] | 166 | s3c_rtc_disable_clk(info); |
| 167 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 168 | rtc_tm->tm_year += 100; |
| 169 | rtc_tm->tm_mon -= 1; |
| 170 | |
Andy Shevchenko | 9a1bacf | 2018-12-04 23:23:25 +0200 | [diff] [blame] | 171 | dev_dbg(dev, "read time %ptR\n", rtc_tm); |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 172 | return 0; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm) |
| 176 | { |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 177 | struct s3c_rtc *info = dev_get_drvdata(dev); |
Ben Dooks | 641741e | 2006-08-27 01:23:27 -0700 | [diff] [blame] | 178 | int year = tm->tm_year - 100; |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 179 | int ret; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 180 | |
Andy Shevchenko | 9a1bacf | 2018-12-04 23:23:25 +0200 | [diff] [blame] | 181 | dev_dbg(dev, "set time %ptR\n", tm); |
Ben Dooks | 9a65451 | 2006-08-27 01:23:22 -0700 | [diff] [blame] | 182 | |
Ben Dooks | 641741e | 2006-08-27 01:23:27 -0700 | [diff] [blame] | 183 | /* we get around y2k by simply not supporting it */ |
| 184 | |
| 185 | if (year < 0 || year >= 100) { |
| 186 | dev_err(dev, "rtc only supports 100 years\n"); |
| 187 | return -EINVAL; |
| 188 | } |
| 189 | |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 190 | ret = s3c_rtc_enable_clk(info); |
| 191 | if (ret) |
| 192 | return ret; |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 193 | |
| 194 | writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_RTCSEC); |
| 195 | writeb(bin2bcd(tm->tm_min), info->base + S3C2410_RTCMIN); |
| 196 | writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_RTCHOUR); |
| 197 | writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_RTCDATE); |
| 198 | writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_RTCMON); |
| 199 | writeb(bin2bcd(year), info->base + S3C2410_RTCYEAR); |
| 200 | |
Chanwoo Choi | 24e1455 | 2015-04-16 12:45:15 -0700 | [diff] [blame] | 201 | s3c_rtc_disable_clk(info); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 207 | { |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 208 | struct s3c_rtc *info = dev_get_drvdata(dev); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 209 | struct rtc_time *alm_tm = &alrm->time; |
| 210 | unsigned int alm_en; |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 211 | int ret; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 212 | |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 213 | ret = s3c_rtc_enable_clk(info); |
| 214 | if (ret) |
| 215 | return ret; |
Chanwoo Choi | df9e26d | 2014-10-13 15:52:35 -0700 | [diff] [blame] | 216 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 217 | alm_tm->tm_sec = readb(info->base + S3C2410_ALMSEC); |
| 218 | alm_tm->tm_min = readb(info->base + S3C2410_ALMMIN); |
| 219 | alm_tm->tm_hour = readb(info->base + S3C2410_ALMHOUR); |
| 220 | alm_tm->tm_mon = readb(info->base + S3C2410_ALMMON); |
| 221 | alm_tm->tm_mday = readb(info->base + S3C2410_ALMDATE); |
| 222 | alm_tm->tm_year = readb(info->base + S3C2410_ALMYEAR); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 223 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 224 | alm_en = readb(info->base + S3C2410_RTCALM); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 225 | |
Chanwoo Choi | 24e1455 | 2015-04-16 12:45:15 -0700 | [diff] [blame] | 226 | s3c_rtc_disable_clk(info); |
| 227 | |
David Brownell | a2db8df | 2006-12-13 00:35:08 -0800 | [diff] [blame] | 228 | alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0; |
| 229 | |
Andy Shevchenko | 9a1bacf | 2018-12-04 23:23:25 +0200 | [diff] [blame] | 230 | dev_dbg(dev, "read alarm %d, %ptR\n", alm_en, alm_tm); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 231 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 232 | /* decode the alarm enable field */ |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 233 | if (alm_en & S3C2410_RTCALM_SECEN) |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 234 | alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 235 | |
| 236 | if (alm_en & S3C2410_RTCALM_MINEN) |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 237 | alm_tm->tm_min = bcd2bin(alm_tm->tm_min); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 238 | |
| 239 | if (alm_en & S3C2410_RTCALM_HOUREN) |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 240 | alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 241 | |
| 242 | if (alm_en & S3C2410_RTCALM_DAYEN) |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 243 | alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 244 | |
| 245 | if (alm_en & S3C2410_RTCALM_MONEN) { |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 246 | alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 247 | alm_tm->tm_mon -= 1; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | if (alm_en & S3C2410_RTCALM_YEAREN) |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 251 | alm_tm->tm_year = bcd2bin(alm_tm->tm_year); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 257 | { |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 258 | struct s3c_rtc *info = dev_get_drvdata(dev); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 259 | struct rtc_time *tm = &alrm->time; |
| 260 | unsigned int alrm_en; |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 261 | int ret; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 262 | |
Andy Shevchenko | 9a1bacf | 2018-12-04 23:23:25 +0200 | [diff] [blame] | 263 | dev_dbg(dev, "s3c_rtc_setalarm: %d, %ptR\n", alrm->enabled, tm); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 264 | |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 265 | ret = s3c_rtc_enable_clk(info); |
| 266 | if (ret) |
| 267 | return ret; |
Chanwoo Choi | 24e1455 | 2015-04-16 12:45:15 -0700 | [diff] [blame] | 268 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 269 | alrm_en = readb(info->base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN; |
| 270 | writeb(0x00, info->base + S3C2410_RTCALM); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 271 | |
| 272 | if (tm->tm_sec < 60 && tm->tm_sec >= 0) { |
| 273 | alrm_en |= S3C2410_RTCALM_SECEN; |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 274 | writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_ALMSEC); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | if (tm->tm_min < 60 && tm->tm_min >= 0) { |
| 278 | alrm_en |= S3C2410_RTCALM_MINEN; |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 279 | writeb(bin2bcd(tm->tm_min), info->base + S3C2410_ALMMIN); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | if (tm->tm_hour < 24 && tm->tm_hour >= 0) { |
| 283 | alrm_en |= S3C2410_RTCALM_HOUREN; |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 284 | writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_ALMHOUR); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Krzysztof Kozlowski | fb4ac3c | 2015-11-01 20:49:04 +0900 | [diff] [blame] | 287 | if (tm->tm_mon < 12 && tm->tm_mon >= 0) { |
| 288 | alrm_en |= S3C2410_RTCALM_MONEN; |
| 289 | writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_ALMMON); |
| 290 | } |
| 291 | |
| 292 | if (tm->tm_mday <= 31 && tm->tm_mday >= 1) { |
| 293 | alrm_en |= S3C2410_RTCALM_DAYEN; |
| 294 | writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_ALMDATE); |
| 295 | } |
| 296 | |
Jingoo Han | d4a48c2 | 2013-02-21 16:45:06 -0800 | [diff] [blame] | 297 | dev_dbg(dev, "setting S3C2410_RTCALM to %08x\n", alrm_en); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 298 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 299 | writeb(alrm_en, info->base + S3C2410_RTCALM); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 300 | |
Chanwoo Choi | 24e1455 | 2015-04-16 12:45:15 -0700 | [diff] [blame] | 301 | s3c_rtc_setaie(dev, alrm->enabled); |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 302 | |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 303 | s3c_rtc_disable_clk(info); |
| 304 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 308 | static const struct rtc_class_ops s3c_rtcops = { |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 309 | .read_time = s3c_rtc_gettime, |
| 310 | .set_time = s3c_rtc_settime, |
| 311 | .read_alarm = s3c_rtc_getalarm, |
| 312 | .set_alarm = s3c_rtc_setalarm, |
Changhwan Youn | e6eb524 | 2010-10-27 15:33:09 -0700 | [diff] [blame] | 313 | .alarm_irq_enable = s3c_rtc_setaie, |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 314 | }; |
| 315 | |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 316 | static void s3c24xx_rtc_enable(struct s3c_rtc *info) |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 317 | { |
Chanwoo Choi | d67288d | 2014-10-13 15:52:31 -0700 | [diff] [blame] | 318 | unsigned int con, tmp; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 319 | |
Chanwoo Choi | d67288d | 2014-10-13 15:52:31 -0700 | [diff] [blame] | 320 | con = readw(info->base + S3C2410_RTCCON); |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 321 | /* re-enable the device, and check it is ok */ |
| 322 | if ((con & S3C2410_RTCCON_RTCEN) == 0) { |
| 323 | dev_info(info->dev, "rtc disabled, re-enabling\n"); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 324 | |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 325 | tmp = readw(info->base + S3C2410_RTCCON); |
Krzysztof Kozlowski | fc1afe6 | 2017-06-16 21:28:03 +0200 | [diff] [blame] | 326 | writew(tmp | S3C2410_RTCCON_RTCEN, info->base + S3C2410_RTCCON); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 327 | } |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 328 | |
| 329 | if (con & S3C2410_RTCCON_CNTSEL) { |
| 330 | dev_info(info->dev, "removing RTCCON_CNTSEL\n"); |
| 331 | |
| 332 | tmp = readw(info->base + S3C2410_RTCCON); |
| 333 | writew(tmp & ~S3C2410_RTCCON_CNTSEL, |
Krzysztof Kozlowski | fc1afe6 | 2017-06-16 21:28:03 +0200 | [diff] [blame] | 334 | info->base + S3C2410_RTCCON); |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | if (con & S3C2410_RTCCON_CLKRST) { |
| 338 | dev_info(info->dev, "removing RTCCON_CLKRST\n"); |
| 339 | |
| 340 | tmp = readw(info->base + S3C2410_RTCCON); |
| 341 | writew(tmp & ~S3C2410_RTCCON_CLKRST, |
Krzysztof Kozlowski | fc1afe6 | 2017-06-16 21:28:03 +0200 | [diff] [blame] | 342 | info->base + S3C2410_RTCCON); |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 343 | } |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static void s3c24xx_rtc_disable(struct s3c_rtc *info) |
| 347 | { |
| 348 | unsigned int con; |
| 349 | |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 350 | con = readw(info->base + S3C2410_RTCCON); |
| 351 | con &= ~S3C2410_RTCCON_RTCEN; |
| 352 | writew(con, info->base + S3C2410_RTCCON); |
| 353 | |
| 354 | con = readb(info->base + S3C2410_TICNT); |
| 355 | con &= ~S3C2410_TICNT_ENABLE; |
| 356 | writeb(con, info->base + S3C2410_TICNT); |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static void s3c6410_rtc_disable(struct s3c_rtc *info) |
| 360 | { |
| 361 | unsigned int con; |
| 362 | |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 363 | con = readw(info->base + S3C2410_RTCCON); |
| 364 | con &= ~S3C64XX_RTCCON_TICEN; |
| 365 | con &= ~S3C2410_RTCCON_RTCEN; |
| 366 | writew(con, info->base + S3C2410_RTCCON); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 369 | static int s3c_rtc_remove(struct platform_device *pdev) |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 370 | { |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 371 | struct s3c_rtc *info = platform_get_drvdata(pdev); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 372 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 373 | s3c_rtc_setaie(info->dev, 0); |
| 374 | |
Joonyoung Shim | 7f23a93 | 2015-08-11 20:28:19 +0900 | [diff] [blame] | 375 | if (info->data->needs_src_clk) |
| 376 | clk_unprepare(info->rtc_src_clk); |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 377 | clk_unprepare(info->rtc_clk); |
Atul Dahiya | e48add8 | 2010-07-20 12:19:14 +0530 | [diff] [blame] | 378 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 382 | static int s3c_rtc_probe(struct platform_device *pdev) |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 383 | { |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 384 | struct s3c_rtc *info = NULL; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 385 | int ret; |
| 386 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 387 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 388 | if (!info) |
| 389 | return -ENOMEM; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 390 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 391 | info->dev = &pdev->dev; |
Marek Szyprowski | 64704c9 | 2019-01-18 14:28:37 +0100 | [diff] [blame] | 392 | info->data = of_device_get_match_data(&pdev->dev); |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 393 | if (!info->data) { |
| 394 | dev_err(&pdev->dev, "failed getting s3c_rtc_data\n"); |
| 395 | return -EINVAL; |
| 396 | } |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 397 | spin_lock_init(&info->alarm_lock); |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 398 | |
| 399 | platform_set_drvdata(pdev, info); |
| 400 | |
| 401 | info->irq_alarm = platform_get_irq(pdev, 0); |
Stephen Boyd | faac910 | 2019-07-30 11:15:39 -0700 | [diff] [blame] | 402 | if (info->irq_alarm < 0) |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 403 | return info->irq_alarm; |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 404 | |
Marek Szyprowski | ce9af893 | 2020-12-02 12:13:18 +0100 | [diff] [blame] | 405 | dev_dbg(&pdev->dev, "s3c2410_rtc: alarm irq %d\n", info->irq_alarm); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 406 | |
| 407 | /* get the memory region */ |
YueHaibing | 09ef18b | 2019-10-06 18:29:20 +0800 | [diff] [blame] | 408 | info->base = devm_platform_ioremap_resource(pdev, 0); |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 409 | if (IS_ERR(info->base)) |
| 410 | return PTR_ERR(info->base); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 411 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 412 | info->rtc_clk = devm_clk_get(&pdev->dev, "rtc"); |
| 413 | if (IS_ERR(info->rtc_clk)) { |
Javier Martinez Canillas | ae6e00b | 2016-03-14 22:38:38 -0300 | [diff] [blame] | 414 | ret = PTR_ERR(info->rtc_clk); |
| 415 | if (ret != -EPROBE_DEFER) |
| 416 | dev_err(&pdev->dev, "failed to find rtc clock\n"); |
| 417 | else |
| 418 | dev_dbg(&pdev->dev, "probe deferred due to missing rtc clk\n"); |
| 419 | return ret; |
Atul Dahiya | e48add8 | 2010-07-20 12:19:14 +0530 | [diff] [blame] | 420 | } |
Krzysztof Kozlowski | 9903f68 | 2017-06-16 21:28:06 +0200 | [diff] [blame] | 421 | ret = clk_prepare_enable(info->rtc_clk); |
| 422 | if (ret) |
| 423 | return ret; |
Atul Dahiya | e48add8 | 2010-07-20 12:19:14 +0530 | [diff] [blame] | 424 | |
Marek Szyprowski | eaf3a65 | 2014-10-29 14:50:38 -0700 | [diff] [blame] | 425 | if (info->data->needs_src_clk) { |
| 426 | info->rtc_src_clk = devm_clk_get(&pdev->dev, "rtc_src"); |
| 427 | if (IS_ERR(info->rtc_src_clk)) { |
Krzysztof Kozlowski | c52d270 | 2020-08-30 10:09:37 +0200 | [diff] [blame] | 428 | ret = dev_err_probe(&pdev->dev, PTR_ERR(info->rtc_src_clk), |
| 429 | "failed to find rtc source clock\n"); |
Krzysztof Kozlowski | 8768e7b | 2017-06-16 21:28:02 +0200 | [diff] [blame] | 430 | goto err_src_clk; |
Marek Szyprowski | eaf3a65 | 2014-10-29 14:50:38 -0700 | [diff] [blame] | 431 | } |
Krzysztof Kozlowski | 9903f68 | 2017-06-16 21:28:06 +0200 | [diff] [blame] | 432 | ret = clk_prepare_enable(info->rtc_src_clk); |
| 433 | if (ret) |
| 434 | goto err_src_clk; |
Chanwoo Choi | df9e26d | 2014-10-13 15:52:35 -0700 | [diff] [blame] | 435 | } |
Chanwoo Choi | df9e26d | 2014-10-13 15:52:35 -0700 | [diff] [blame] | 436 | |
Marek Szyprowski | 31b16d9 | 2020-12-02 12:13:17 +0100 | [diff] [blame] | 437 | /* disable RTC enable bits potentially set by the bootloader */ |
| 438 | if (info->data->disable) |
| 439 | info->data->disable(info); |
| 440 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 441 | /* check to see if everything is setup correctly */ |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 442 | if (info->data->enable) |
| 443 | info->data->enable(info); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 444 | |
Jingoo Han | d4a48c2 | 2013-02-21 16:45:06 -0800 | [diff] [blame] | 445 | dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n", |
Krzysztof Kozlowski | fc1afe6 | 2017-06-16 21:28:03 +0200 | [diff] [blame] | 446 | readw(info->base + S3C2410_RTCCON)); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 447 | |
Yauhen Kharuzhy | 51b7616 | 2008-10-29 14:01:16 -0700 | [diff] [blame] | 448 | device_init_wakeup(&pdev->dev, 1); |
| 449 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 450 | /* register RTC and exit */ |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 451 | info->rtc = devm_rtc_device_register(&pdev->dev, "s3c", &s3c_rtcops, |
Krzysztof Kozlowski | fc1afe6 | 2017-06-16 21:28:03 +0200 | [diff] [blame] | 452 | THIS_MODULE); |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 453 | if (IS_ERR(info->rtc)) { |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 454 | dev_err(&pdev->dev, "cannot attach rtc\n"); |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 455 | ret = PTR_ERR(info->rtc); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 456 | goto err_nortc; |
| 457 | } |
| 458 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 459 | ret = devm_request_irq(&pdev->dev, info->irq_alarm, s3c_rtc_alarmirq, |
Krzysztof Kozlowski | fc1afe6 | 2017-06-16 21:28:03 +0200 | [diff] [blame] | 460 | 0, "s3c2410-rtc alarm", info); |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 461 | if (ret) { |
| 462 | dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_alarm, ret); |
| 463 | goto err_nortc; |
| 464 | } |
| 465 | |
Marek Szyprowski | 5a5b614 | 2019-01-21 12:09:30 +0100 | [diff] [blame] | 466 | s3c_rtc_disable_clk(info); |
| 467 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 468 | return 0; |
| 469 | |
Krzysztof Kozlowski | fc1afe6 | 2017-06-16 21:28:03 +0200 | [diff] [blame] | 470 | err_nortc: |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 471 | if (info->data->disable) |
| 472 | info->data->disable(info); |
Chanwoo Choi | 24e1455 | 2015-04-16 12:45:15 -0700 | [diff] [blame] | 473 | |
| 474 | if (info->data->needs_src_clk) |
| 475 | clk_disable_unprepare(info->rtc_src_clk); |
Krzysztof Kozlowski | 8768e7b | 2017-06-16 21:28:02 +0200 | [diff] [blame] | 476 | err_src_clk: |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 477 | clk_disable_unprepare(info->rtc_clk); |
Atul Dahiya | e48add8 | 2010-07-20 12:19:14 +0530 | [diff] [blame] | 478 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 479 | return ret; |
| 480 | } |
| 481 | |
Jingoo Han | 32e445a | 2013-04-29 16:19:27 -0700 | [diff] [blame] | 482 | #ifdef CONFIG_PM_SLEEP |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 483 | |
Jingoo Han | 32e445a | 2013-04-29 16:19:27 -0700 | [diff] [blame] | 484 | static int s3c_rtc_suspend(struct device *dev) |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 485 | { |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 486 | struct s3c_rtc *info = dev_get_drvdata(dev); |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 487 | int ret; |
Jingoo Han | 32e445a | 2013-04-29 16:19:27 -0700 | [diff] [blame] | 488 | |
Krzysztof Kozlowski | 498bcf3 | 2017-06-16 21:28:07 +0200 | [diff] [blame] | 489 | ret = s3c_rtc_enable_clk(info); |
| 490 | if (ret) |
| 491 | return ret; |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 492 | |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 493 | if (info->data->disable) |
| 494 | info->data->disable(info); |
Vladimir Zapolskiy | f501ed5 | 2010-09-22 13:05:13 -0700 | [diff] [blame] | 495 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 496 | if (device_may_wakeup(dev) && !info->wake_en) { |
| 497 | if (enable_irq_wake(info->irq_alarm) == 0) |
| 498 | info->wake_en = true; |
Ben Dooks | 52cd4e5 | 2011-05-11 15:13:28 -0700 | [diff] [blame] | 499 | else |
Jingoo Han | 32e445a | 2013-04-29 16:19:27 -0700 | [diff] [blame] | 500 | dev_err(dev, "enable_irq_wake failed\n"); |
Ben Dooks | 52cd4e5 | 2011-05-11 15:13:28 -0700 | [diff] [blame] | 501 | } |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 502 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 503 | return 0; |
| 504 | } |
| 505 | |
Jingoo Han | 32e445a | 2013-04-29 16:19:27 -0700 | [diff] [blame] | 506 | static int s3c_rtc_resume(struct device *dev) |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 507 | { |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 508 | struct s3c_rtc *info = dev_get_drvdata(dev); |
Maurus Cuelenaere | 9f4123b | 2010-05-24 14:33:43 -0700 | [diff] [blame] | 509 | |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 510 | if (info->data->enable) |
| 511 | info->data->enable(info); |
| 512 | |
Chanwoo Choi | 24e1455 | 2015-04-16 12:45:15 -0700 | [diff] [blame] | 513 | s3c_rtc_disable_clk(info); |
| 514 | |
Chanwoo Choi | 19be09f | 2014-10-13 15:52:28 -0700 | [diff] [blame] | 515 | if (device_may_wakeup(dev) && info->wake_en) { |
| 516 | disable_irq_wake(info->irq_alarm); |
| 517 | info->wake_en = false; |
Ben Dooks | 52cd4e5 | 2011-05-11 15:13:28 -0700 | [diff] [blame] | 518 | } |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 519 | |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 520 | return 0; |
| 521 | } |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 522 | #endif |
Jingoo Han | 32e445a | 2013-04-29 16:19:27 -0700 | [diff] [blame] | 523 | static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume); |
| 524 | |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 525 | static void s3c24xx_rtc_irq(struct s3c_rtc *info, int mask) |
| 526 | { |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 527 | rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF); |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static void s3c6410_rtc_irq(struct s3c_rtc *info, int mask) |
| 531 | { |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 532 | rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF); |
| 533 | writeb(mask, info->base + S3C2410_INTP); |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 536 | static struct s3c_rtc_data const s3c2410_rtc_data = { |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 537 | .irq_handler = s3c24xx_rtc_irq, |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 538 | .enable = s3c24xx_rtc_enable, |
| 539 | .disable = s3c24xx_rtc_disable, |
| 540 | }; |
| 541 | |
| 542 | static struct s3c_rtc_data const s3c2416_rtc_data = { |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 543 | .irq_handler = s3c24xx_rtc_irq, |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 544 | .enable = s3c24xx_rtc_enable, |
| 545 | .disable = s3c24xx_rtc_disable, |
| 546 | }; |
| 547 | |
| 548 | static struct s3c_rtc_data const s3c2443_rtc_data = { |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 549 | .irq_handler = s3c24xx_rtc_irq, |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 550 | .enable = s3c24xx_rtc_enable, |
| 551 | .disable = s3c24xx_rtc_disable, |
| 552 | }; |
| 553 | |
| 554 | static struct s3c_rtc_data const s3c6410_rtc_data = { |
Javier Martinez Canillas | 8792f777 | 2015-03-12 16:25:49 -0700 | [diff] [blame] | 555 | .needs_src_clk = true, |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 556 | .irq_handler = s3c6410_rtc_irq, |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 557 | .enable = s3c24xx_rtc_enable, |
| 558 | .disable = s3c6410_rtc_disable, |
Tushar Behera | c3cba92 | 2012-04-12 12:49:14 -0700 | [diff] [blame] | 559 | }; |
| 560 | |
Alexandre Belloni | 7d6bec2 | 2021-02-02 12:29:34 +0100 | [diff] [blame] | 561 | static const __maybe_unused struct of_device_id s3c_rtc_dt_match[] = { |
Heiko Stuebner | d2524ca | 2011-12-24 10:52:14 +0900 | [diff] [blame] | 562 | { |
Tushar Behera | cd1e6f9 | 2012-04-12 12:49:14 -0700 | [diff] [blame] | 563 | .compatible = "samsung,s3c2410-rtc", |
Krzysztof Kozlowski | 21df6fe | 2017-06-16 21:28:04 +0200 | [diff] [blame] | 564 | .data = &s3c2410_rtc_data, |
Heiko Stuebner | d2524ca | 2011-12-24 10:52:14 +0900 | [diff] [blame] | 565 | }, { |
Tushar Behera | cd1e6f9 | 2012-04-12 12:49:14 -0700 | [diff] [blame] | 566 | .compatible = "samsung,s3c2416-rtc", |
Krzysztof Kozlowski | 21df6fe | 2017-06-16 21:28:04 +0200 | [diff] [blame] | 567 | .data = &s3c2416_rtc_data, |
Heiko Stuebner | 25c1a24 | 2011-12-24 10:52:19 +0900 | [diff] [blame] | 568 | }, { |
Tushar Behera | cd1e6f9 | 2012-04-12 12:49:14 -0700 | [diff] [blame] | 569 | .compatible = "samsung,s3c2443-rtc", |
Krzysztof Kozlowski | 21df6fe | 2017-06-16 21:28:04 +0200 | [diff] [blame] | 570 | .data = &s3c2443_rtc_data, |
Heiko Stuebner | 25c1a24 | 2011-12-24 10:52:19 +0900 | [diff] [blame] | 571 | }, { |
Tushar Behera | cd1e6f9 | 2012-04-12 12:49:14 -0700 | [diff] [blame] | 572 | .compatible = "samsung,s3c6410-rtc", |
Krzysztof Kozlowski | 21df6fe | 2017-06-16 21:28:04 +0200 | [diff] [blame] | 573 | .data = &s3c6410_rtc_data, |
Chanwoo Choi | df9e26d | 2014-10-13 15:52:35 -0700 | [diff] [blame] | 574 | }, { |
| 575 | .compatible = "samsung,exynos3250-rtc", |
Krzysztof Kozlowski | 21df6fe | 2017-06-16 21:28:04 +0200 | [diff] [blame] | 576 | .data = &s3c6410_rtc_data, |
Heiko Stuebner | d2524ca | 2011-12-24 10:52:14 +0900 | [diff] [blame] | 577 | }, |
Chanwoo Choi | ae05c95 | 2014-10-13 15:52:33 -0700 | [diff] [blame] | 578 | { /* sentinel */ }, |
Thomas Abraham | 39ce408 | 2011-10-24 14:49:04 +0200 | [diff] [blame] | 579 | }; |
| 580 | MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match); |
Maurus Cuelenaere | 9f4123b | 2010-05-24 14:33:43 -0700 | [diff] [blame] | 581 | |
| 582 | static struct platform_driver s3c_rtc_driver = { |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 583 | .probe = s3c_rtc_probe, |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 584 | .remove = s3c_rtc_remove, |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 585 | .driver = { |
Maurus Cuelenaere | 9f4123b | 2010-05-24 14:33:43 -0700 | [diff] [blame] | 586 | .name = "s3c-rtc", |
Jingoo Han | 32e445a | 2013-04-29 16:19:27 -0700 | [diff] [blame] | 587 | .pm = &s3c_rtc_pm_ops, |
Sachin Kamat | 04a373f | 2012-12-17 16:02:52 -0800 | [diff] [blame] | 588 | .of_match_table = of_match_ptr(s3c_rtc_dt_match), |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 589 | }, |
| 590 | }; |
Axel Lin | 0c4eae6 | 2012-01-10 15:10:48 -0800 | [diff] [blame] | 591 | module_platform_driver(s3c_rtc_driver); |
Ben Dooks | 1add678 | 2006-07-01 04:36:26 -0700 | [diff] [blame] | 592 | |
| 593 | MODULE_DESCRIPTION("Samsung S3C RTC Driver"); |
| 594 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); |
| 595 | MODULE_LICENSE("GPL"); |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 596 | MODULE_ALIAS("platform:s3c2410-rtc"); |