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