Thomas Gleixner | 9c92ab6 | 2019-05-29 07:17:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 2 | /* |
| 3 | * drivers/rtc/rtc-vt8500.c |
| 4 | * |
| 5 | * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com> |
| 6 | * |
| 7 | * Based on rtc-pxa.c |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/rtc.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/bcd.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/slab.h> |
Tony Prisk | 7f4b48b | 2012-08-03 20:55:28 +1200 | [diff] [blame] | 18 | #include <linux/of.h> |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * Register definitions |
| 22 | */ |
| 23 | #define VT8500_RTC_TS 0x00 /* Time set */ |
| 24 | #define VT8500_RTC_DS 0x04 /* Date set */ |
| 25 | #define VT8500_RTC_AS 0x08 /* Alarm set */ |
| 26 | #define VT8500_RTC_CR 0x0c /* Control */ |
| 27 | #define VT8500_RTC_TR 0x10 /* Time read */ |
| 28 | #define VT8500_RTC_DR 0x14 /* Date read */ |
| 29 | #define VT8500_RTC_WS 0x18 /* Write status */ |
| 30 | #define VT8500_RTC_CL 0x20 /* Calibration */ |
| 31 | #define VT8500_RTC_IS 0x24 /* Interrupt status */ |
| 32 | #define VT8500_RTC_ST 0x28 /* Status */ |
| 33 | |
| 34 | #define INVALID_TIME_BIT (1 << 31) |
| 35 | |
| 36 | #define DATE_CENTURY_S 19 |
| 37 | #define DATE_YEAR_S 11 |
| 38 | #define DATE_YEAR_MASK (0xff << DATE_YEAR_S) |
| 39 | #define DATE_MONTH_S 6 |
| 40 | #define DATE_MONTH_MASK (0x1f << DATE_MONTH_S) |
| 41 | #define DATE_DAY_MASK 0x3f |
| 42 | |
| 43 | #define TIME_DOW_S 20 |
| 44 | #define TIME_DOW_MASK (0x07 << TIME_DOW_S) |
| 45 | #define TIME_HOUR_S 14 |
| 46 | #define TIME_HOUR_MASK (0x3f << TIME_HOUR_S) |
| 47 | #define TIME_MIN_S 7 |
| 48 | #define TIME_MIN_MASK (0x7f << TIME_MIN_S) |
| 49 | #define TIME_SEC_MASK 0x7f |
| 50 | |
| 51 | #define ALARM_DAY_S 20 |
| 52 | #define ALARM_DAY_MASK (0x3f << ALARM_DAY_S) |
| 53 | |
| 54 | #define ALARM_DAY_BIT (1 << 29) |
| 55 | #define ALARM_HOUR_BIT (1 << 28) |
| 56 | #define ALARM_MIN_BIT (1 << 27) |
| 57 | #define ALARM_SEC_BIT (1 << 26) |
| 58 | |
| 59 | #define ALARM_ENABLE_MASK (ALARM_DAY_BIT \ |
| 60 | | ALARM_HOUR_BIT \ |
| 61 | | ALARM_MIN_BIT \ |
| 62 | | ALARM_SEC_BIT) |
| 63 | |
| 64 | #define VT8500_RTC_CR_ENABLE (1 << 0) /* Enable RTC */ |
Tony Prisk | 532db57 | 2013-01-04 15:35:47 -0800 | [diff] [blame] | 65 | #define VT8500_RTC_CR_12H (1 << 1) /* 12h time format */ |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 66 | #define VT8500_RTC_CR_SM_ENABLE (1 << 2) /* Enable periodic irqs */ |
| 67 | #define VT8500_RTC_CR_SM_SEC (1 << 3) /* 0: 1Hz/60, 1: 1Hz */ |
| 68 | #define VT8500_RTC_CR_CALIB (1 << 4) /* Enable calibration */ |
| 69 | |
Wolfram Sang | 07187ee | 2011-06-22 23:20:14 +0200 | [diff] [blame] | 70 | #define VT8500_RTC_IS_ALARM (1 << 0) /* Alarm interrupt status */ |
| 71 | |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 72 | struct vt8500_rtc { |
| 73 | void __iomem *regbase; |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 74 | int irq_alarm; |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 75 | struct rtc_device *rtc; |
| 76 | spinlock_t lock; /* Protects this structure */ |
| 77 | }; |
| 78 | |
| 79 | static irqreturn_t vt8500_rtc_irq(int irq, void *dev_id) |
| 80 | { |
| 81 | struct vt8500_rtc *vt8500_rtc = dev_id; |
| 82 | u32 isr; |
| 83 | unsigned long events = 0; |
| 84 | |
| 85 | spin_lock(&vt8500_rtc->lock); |
| 86 | |
| 87 | /* clear interrupt sources */ |
| 88 | isr = readl(vt8500_rtc->regbase + VT8500_RTC_IS); |
| 89 | writel(isr, vt8500_rtc->regbase + VT8500_RTC_IS); |
| 90 | |
| 91 | spin_unlock(&vt8500_rtc->lock); |
| 92 | |
Wolfram Sang | 07187ee | 2011-06-22 23:20:14 +0200 | [diff] [blame] | 93 | if (isr & VT8500_RTC_IS_ALARM) |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 94 | events |= RTC_AF | RTC_IRQF; |
| 95 | |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 96 | rtc_update_irq(vt8500_rtc->rtc, 1, events); |
| 97 | |
| 98 | return IRQ_HANDLED; |
| 99 | } |
| 100 | |
| 101 | static int vt8500_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 102 | { |
| 103 | struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev); |
| 104 | u32 date, time; |
| 105 | |
| 106 | date = readl(vt8500_rtc->regbase + VT8500_RTC_DR); |
| 107 | time = readl(vt8500_rtc->regbase + VT8500_RTC_TR); |
| 108 | |
| 109 | tm->tm_sec = bcd2bin(time & TIME_SEC_MASK); |
| 110 | tm->tm_min = bcd2bin((time & TIME_MIN_MASK) >> TIME_MIN_S); |
| 111 | tm->tm_hour = bcd2bin((time & TIME_HOUR_MASK) >> TIME_HOUR_S); |
| 112 | tm->tm_mday = bcd2bin(date & DATE_DAY_MASK); |
Tony Prisk | 2f90b68 | 2013-01-04 15:35:48 -0800 | [diff] [blame] | 113 | tm->tm_mon = bcd2bin((date & DATE_MONTH_MASK) >> DATE_MONTH_S) - 1; |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 114 | tm->tm_year = bcd2bin((date & DATE_YEAR_MASK) >> DATE_YEAR_S) |
| 115 | + ((date >> DATE_CENTURY_S) & 1 ? 200 : 100); |
| 116 | tm->tm_wday = (time & TIME_DOW_MASK) >> TIME_DOW_S; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int vt8500_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 122 | { |
| 123 | struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev); |
| 124 | |
| 125 | if (tm->tm_year < 100) { |
| 126 | dev_warn(dev, "Only years 2000-2199 are supported by the " |
| 127 | "hardware!\n"); |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | |
Tony Prisk | 3bdf8cd | 2013-02-04 14:28:44 -0800 | [diff] [blame] | 131 | writel((bin2bcd(tm->tm_year % 100) << DATE_YEAR_S) |
Tony Prisk | 2f90b68 | 2013-01-04 15:35:48 -0800 | [diff] [blame] | 132 | | (bin2bcd(tm->tm_mon + 1) << DATE_MONTH_S) |
| 133 | | (bin2bcd(tm->tm_mday)) |
| 134 | | ((tm->tm_year >= 200) << DATE_CENTURY_S), |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 135 | vt8500_rtc->regbase + VT8500_RTC_DS); |
| 136 | writel((bin2bcd(tm->tm_wday) << TIME_DOW_S) |
| 137 | | (bin2bcd(tm->tm_hour) << TIME_HOUR_S) |
| 138 | | (bin2bcd(tm->tm_min) << TIME_MIN_S) |
| 139 | | (bin2bcd(tm->tm_sec)), |
| 140 | vt8500_rtc->regbase + VT8500_RTC_TS); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int vt8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 146 | { |
| 147 | struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev); |
| 148 | u32 isr, alarm; |
| 149 | |
| 150 | alarm = readl(vt8500_rtc->regbase + VT8500_RTC_AS); |
| 151 | isr = readl(vt8500_rtc->regbase + VT8500_RTC_IS); |
| 152 | |
| 153 | alrm->time.tm_mday = bcd2bin((alarm & ALARM_DAY_MASK) >> ALARM_DAY_S); |
| 154 | alrm->time.tm_hour = bcd2bin((alarm & TIME_HOUR_MASK) >> TIME_HOUR_S); |
| 155 | alrm->time.tm_min = bcd2bin((alarm & TIME_MIN_MASK) >> TIME_MIN_S); |
| 156 | alrm->time.tm_sec = bcd2bin((alarm & TIME_SEC_MASK)); |
| 157 | |
| 158 | alrm->enabled = (alarm & ALARM_ENABLE_MASK) ? 1 : 0; |
Wolfram Sang | 07187ee | 2011-06-22 23:20:14 +0200 | [diff] [blame] | 159 | alrm->pending = (isr & VT8500_RTC_IS_ALARM) ? 1 : 0; |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 160 | |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 161 | return rtc_valid_tm(&alrm->time); |
| 162 | } |
| 163 | |
| 164 | static int vt8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 165 | { |
| 166 | struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev); |
| 167 | |
| 168 | writel((alrm->enabled ? ALARM_ENABLE_MASK : 0) |
| 169 | | (bin2bcd(alrm->time.tm_mday) << ALARM_DAY_S) |
| 170 | | (bin2bcd(alrm->time.tm_hour) << TIME_HOUR_S) |
| 171 | | (bin2bcd(alrm->time.tm_min) << TIME_MIN_S) |
| 172 | | (bin2bcd(alrm->time.tm_sec)), |
| 173 | vt8500_rtc->regbase + VT8500_RTC_AS); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int vt8500_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 179 | { |
| 180 | struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev); |
| 181 | unsigned long tmp = readl(vt8500_rtc->regbase + VT8500_RTC_AS); |
| 182 | |
| 183 | if (enabled) |
| 184 | tmp |= ALARM_ENABLE_MASK; |
| 185 | else |
| 186 | tmp &= ~ALARM_ENABLE_MASK; |
| 187 | |
| 188 | writel(tmp, vt8500_rtc->regbase + VT8500_RTC_AS); |
| 189 | return 0; |
| 190 | } |
| 191 | |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 192 | static const struct rtc_class_ops vt8500_rtc_ops = { |
| 193 | .read_time = vt8500_rtc_read_time, |
| 194 | .set_time = vt8500_rtc_set_time, |
| 195 | .read_alarm = vt8500_rtc_read_alarm, |
| 196 | .set_alarm = vt8500_rtc_set_alarm, |
| 197 | .alarm_irq_enable = vt8500_alarm_irq_enable, |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 200 | static int vt8500_rtc_probe(struct platform_device *pdev) |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 201 | { |
| 202 | struct vt8500_rtc *vt8500_rtc; |
Jingoo Han | caa3af2 | 2014-04-03 14:49:49 -0700 | [diff] [blame] | 203 | struct resource *res; |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 204 | int ret; |
| 205 | |
Devendra Naga | 0d7ecb8 | 2012-12-17 16:02:26 -0800 | [diff] [blame] | 206 | vt8500_rtc = devm_kzalloc(&pdev->dev, |
| 207 | sizeof(struct vt8500_rtc), GFP_KERNEL); |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 208 | if (!vt8500_rtc) |
| 209 | return -ENOMEM; |
| 210 | |
| 211 | spin_lock_init(&vt8500_rtc->lock); |
| 212 | platform_set_drvdata(pdev, vt8500_rtc); |
| 213 | |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 214 | vt8500_rtc->irq_alarm = platform_get_irq(pdev, 0); |
| 215 | if (vt8500_rtc->irq_alarm < 0) { |
| 216 | dev_err(&pdev->dev, "No alarm IRQ resource defined\n"); |
Sachin Kamat | 58f27d7 | 2013-11-12 15:10:28 -0800 | [diff] [blame] | 217 | return vt8500_rtc->irq_alarm; |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Jingoo Han | caa3af2 | 2014-04-03 14:49:49 -0700 | [diff] [blame] | 220 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 221 | vt8500_rtc->regbase = devm_ioremap_resource(&pdev->dev, res); |
| 222 | if (IS_ERR(vt8500_rtc->regbase)) |
| 223 | return PTR_ERR(vt8500_rtc->regbase); |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 224 | |
Alexey Charkov | 03ad2d5 | 2011-06-22 23:20:13 +0200 | [diff] [blame] | 225 | /* Enable RTC and set it to 24-hour mode */ |
Tony Prisk | 532db57 | 2013-01-04 15:35:47 -0800 | [diff] [blame] | 226 | writel(VT8500_RTC_CR_ENABLE, |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 227 | vt8500_rtc->regbase + VT8500_RTC_CR); |
| 228 | |
Jingoo Han | bc2e9c0 | 2013-04-29 16:19:19 -0700 | [diff] [blame] | 229 | vt8500_rtc->rtc = devm_rtc_device_register(&pdev->dev, "vt8500-rtc", |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 230 | &vt8500_rtc_ops, THIS_MODULE); |
| 231 | if (IS_ERR(vt8500_rtc->rtc)) { |
| 232 | ret = PTR_ERR(vt8500_rtc->rtc); |
| 233 | dev_err(&pdev->dev, |
| 234 | "Failed to register RTC device -> %d\n", ret); |
Jingoo Han | 0db29c1 | 2013-02-21 16:45:37 -0800 | [diff] [blame] | 235 | goto err_return; |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Jingoo Han | 0db29c1 | 2013-02-21 16:45:37 -0800 | [diff] [blame] | 238 | ret = devm_request_irq(&pdev->dev, vt8500_rtc->irq_alarm, |
| 239 | vt8500_rtc_irq, 0, "rtc alarm", vt8500_rtc); |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 240 | if (ret < 0) { |
| 241 | dev_err(&pdev->dev, "can't get irq %i, err %d\n", |
| 242 | vt8500_rtc->irq_alarm, ret); |
Jingoo Han | bc2e9c0 | 2013-04-29 16:19:19 -0700 | [diff] [blame] | 243 | goto err_return; |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | |
Jingoo Han | 0db29c1 | 2013-02-21 16:45:37 -0800 | [diff] [blame] | 248 | err_return: |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 249 | return ret; |
| 250 | } |
| 251 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 252 | static int vt8500_rtc_remove(struct platform_device *pdev) |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 253 | { |
| 254 | struct vt8500_rtc *vt8500_rtc = platform_get_drvdata(pdev); |
| 255 | |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 256 | /* Disable alarm matching */ |
| 257 | writel(0, vt8500_rtc->regbase + VT8500_RTC_IS); |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 258 | |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | |
Tony Prisk | 7f4b48b | 2012-08-03 20:55:28 +1200 | [diff] [blame] | 262 | static const struct of_device_id wmt_dt_ids[] = { |
| 263 | { .compatible = "via,vt8500-rtc", }, |
| 264 | {} |
| 265 | }; |
Javier Martinez Canillas | 73798d5 | 2015-08-27 13:52:02 +0200 | [diff] [blame] | 266 | MODULE_DEVICE_TABLE(of, wmt_dt_ids); |
Tony Prisk | 7f4b48b | 2012-08-03 20:55:28 +1200 | [diff] [blame] | 267 | |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 268 | static struct platform_driver vt8500_rtc_driver = { |
| 269 | .probe = vt8500_rtc_probe, |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 270 | .remove = vt8500_rtc_remove, |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 271 | .driver = { |
| 272 | .name = "vt8500-rtc", |
Sachin Kamat | ce6bafd | 2013-11-12 15:10:54 -0800 | [diff] [blame] | 273 | .of_match_table = wmt_dt_ids, |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 274 | }, |
| 275 | }; |
| 276 | |
Axel Lin | 0c4eae6 | 2012-01-10 15:10:48 -0800 | [diff] [blame] | 277 | module_platform_driver(vt8500_rtc_driver); |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 278 | |
| 279 | MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>"); |
| 280 | MODULE_DESCRIPTION("VIA VT8500 SoC Realtime Clock Driver (RTC)"); |
Tony Prisk | 7f4b48b | 2012-08-03 20:55:28 +1200 | [diff] [blame] | 281 | MODULE_LICENSE("GPL v2"); |
Alexey Charkov | f77fbdf | 2011-05-26 16:25:03 -0700 | [diff] [blame] | 282 | MODULE_ALIAS("platform:vt8500-rtc"); |