Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Real Time Clock driver for Freescale MC13783 PMIC |
| 3 | * |
| 4 | * (C) 2009 Sascha Hauer, Pengutronix |
| 5 | * (C) 2009 Uwe Kleine-Koenig, Pengutronix |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/mfd/mc13783.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/rtc.h> |
| 17 | |
| 18 | #define DRIVER_NAME "mc13783-rtc" |
| 19 | |
| 20 | #define MC13783_RTCTOD 20 |
| 21 | #define MC13783_RTCTODA 21 |
| 22 | #define MC13783_RTCDAY 22 |
| 23 | #define MC13783_RTCDAYA 23 |
| 24 | |
| 25 | struct mc13783_rtc { |
| 26 | struct rtc_device *rtc; |
| 27 | struct mc13783 *mc13783; |
| 28 | int valid; |
| 29 | }; |
| 30 | |
Uwe Kleine-König | 9407351 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 31 | static int mc13783_rtc_irq_enable_unlocked(struct device *dev, |
| 32 | unsigned int enabled, int irq) |
| 33 | { |
| 34 | struct mc13783_rtc *priv = dev_get_drvdata(dev); |
| 35 | int (*func)(struct mc13783 *mc13783, int irq); |
| 36 | |
| 37 | if (!priv->valid) |
| 38 | return -ENODATA; |
| 39 | |
| 40 | func = enabled ? mc13783_irq_unmask : mc13783_irq_mask; |
| 41 | return func(priv->mc13783, irq); |
| 42 | } |
| 43 | |
| 44 | static int mc13783_rtc_irq_enable(struct device *dev, |
| 45 | unsigned int enabled, int irq) |
| 46 | { |
| 47 | struct mc13783_rtc *priv = dev_get_drvdata(dev); |
| 48 | int ret; |
| 49 | |
| 50 | mc13783_lock(priv->mc13783); |
| 51 | |
| 52 | ret = mc13783_rtc_irq_enable_unlocked(dev, enabled, irq); |
| 53 | |
| 54 | mc13783_unlock(priv->mc13783); |
| 55 | |
| 56 | return ret; |
| 57 | } |
| 58 | |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 59 | static int mc13783_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 60 | { |
| 61 | struct mc13783_rtc *priv = dev_get_drvdata(dev); |
| 62 | unsigned int seconds, days1, days2; |
| 63 | unsigned long s1970; |
| 64 | int ret; |
| 65 | |
| 66 | mc13783_lock(priv->mc13783); |
| 67 | |
| 68 | if (!priv->valid) { |
| 69 | ret = -ENODATA; |
| 70 | goto out; |
| 71 | } |
| 72 | |
| 73 | ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days1); |
| 74 | if (unlikely(ret)) |
| 75 | goto out; |
| 76 | |
| 77 | ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTOD, &seconds); |
| 78 | if (unlikely(ret)) |
| 79 | goto out; |
| 80 | |
| 81 | ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days2); |
| 82 | out: |
| 83 | mc13783_unlock(priv->mc13783); |
| 84 | |
| 85 | if (ret) |
| 86 | return ret; |
| 87 | |
| 88 | if (days2 == days1 + 1) { |
| 89 | if (seconds >= 86400 / 2) |
| 90 | days2 = days1; |
| 91 | else |
| 92 | days1 = days2; |
| 93 | } |
| 94 | |
| 95 | if (days1 != days2) |
| 96 | return -EIO; |
| 97 | |
| 98 | s1970 = days1 * 86400 + seconds; |
| 99 | |
| 100 | rtc_time_to_tm(s1970, tm); |
| 101 | |
| 102 | return rtc_valid_tm(tm); |
| 103 | } |
| 104 | |
| 105 | static int mc13783_rtc_set_mmss(struct device *dev, unsigned long secs) |
| 106 | { |
| 107 | struct mc13783_rtc *priv = dev_get_drvdata(dev); |
| 108 | unsigned int seconds, days; |
Uwe Kleine-König | 9407351 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 109 | unsigned int alarmseconds; |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 110 | int ret; |
| 111 | |
| 112 | seconds = secs % 86400; |
| 113 | days = secs / 86400; |
| 114 | |
| 115 | mc13783_lock(priv->mc13783); |
| 116 | |
| 117 | /* |
Uwe Kleine-König | 9407351 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 118 | * temporarily invalidate alarm to prevent triggering it when the day is |
| 119 | * already updated while the time isn't yet. |
| 120 | */ |
| 121 | ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTODA, &alarmseconds); |
| 122 | if (unlikely(ret)) |
| 123 | goto out; |
| 124 | |
| 125 | if (alarmseconds < 86400) { |
| 126 | ret = mc13783_reg_write(priv->mc13783, |
| 127 | MC13783_RTCTODA, 0x1ffff); |
| 128 | if (unlikely(ret)) |
| 129 | goto out; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * write seconds=0 to prevent a day switch between writing days |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 134 | * and seconds below |
| 135 | */ |
| 136 | ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, 0); |
| 137 | if (unlikely(ret)) |
| 138 | goto out; |
| 139 | |
| 140 | ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAY, days); |
| 141 | if (unlikely(ret)) |
| 142 | goto out; |
| 143 | |
| 144 | ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, seconds); |
| 145 | if (unlikely(ret)) |
| 146 | goto out; |
| 147 | |
Uwe Kleine-König | 9407351 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 148 | /* restore alarm */ |
| 149 | if (alarmseconds < 86400) { |
| 150 | ret = mc13783_reg_write(priv->mc13783, |
| 151 | MC13783_RTCTODA, alarmseconds); |
| 152 | if (unlikely(ret)) |
| 153 | goto out; |
| 154 | } |
| 155 | |
Uwe Kleine-König | 4dd4dd6 | 2010-03-05 13:44:28 -0800 | [diff] [blame] | 156 | ret = mc13783_irq_ack(priv->mc13783, MC13783_IRQ_RTCRST); |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 157 | if (unlikely(ret)) |
| 158 | goto out; |
| 159 | |
Uwe Kleine-König | 4dd4dd6 | 2010-03-05 13:44:28 -0800 | [diff] [blame] | 160 | ret = mc13783_irq_unmask(priv->mc13783, MC13783_IRQ_RTCRST); |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 161 | out: |
| 162 | priv->valid = !ret; |
| 163 | |
| 164 | mc13783_unlock(priv->mc13783); |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
Uwe Kleine-König | 9407351 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 169 | static int mc13783_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 170 | { |
| 171 | struct mc13783_rtc *priv = dev_get_drvdata(dev); |
| 172 | unsigned seconds, days; |
| 173 | unsigned long s1970; |
| 174 | int enabled, pending; |
| 175 | int ret; |
| 176 | |
| 177 | mc13783_lock(priv->mc13783); |
| 178 | |
| 179 | ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTODA, &seconds); |
| 180 | if (unlikely(ret)) |
| 181 | goto out; |
| 182 | if (seconds >= 86400) { |
| 183 | ret = -ENODATA; |
| 184 | goto out; |
| 185 | } |
| 186 | |
| 187 | ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days); |
| 188 | if (unlikely(ret)) |
| 189 | goto out; |
| 190 | |
| 191 | ret = mc13783_irq_status(priv->mc13783, MC13783_IRQ_TODA, |
| 192 | &enabled, &pending); |
| 193 | |
| 194 | out: |
| 195 | mc13783_unlock(priv->mc13783); |
| 196 | |
| 197 | if (ret) |
| 198 | return ret; |
| 199 | |
| 200 | alarm->enabled = enabled; |
| 201 | alarm->pending = pending; |
| 202 | |
| 203 | s1970 = days * 86400 + seconds; |
| 204 | |
| 205 | rtc_time_to_tm(s1970, &alarm->time); |
| 206 | dev_dbg(dev, "%s: %lu\n", __func__, s1970); |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static int mc13783_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 212 | { |
| 213 | struct mc13783_rtc *priv = dev_get_drvdata(dev); |
| 214 | unsigned long s1970; |
| 215 | unsigned seconds, days; |
| 216 | int ret; |
| 217 | |
| 218 | mc13783_lock(priv->mc13783); |
| 219 | |
| 220 | /* disable alarm to prevent false triggering */ |
| 221 | ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTODA, 0x1ffff); |
| 222 | if (unlikely(ret)) |
| 223 | goto out; |
| 224 | |
| 225 | ret = mc13783_irq_ack(priv->mc13783, MC13783_IRQ_TODA); |
| 226 | if (unlikely(ret)) |
| 227 | goto out; |
| 228 | |
| 229 | ret = rtc_tm_to_time(&alarm->time, &s1970); |
| 230 | if (unlikely(ret)) |
| 231 | goto out; |
| 232 | |
| 233 | dev_dbg(dev, "%s: o%2.s %lu\n", __func__, alarm->enabled ? "n" : "ff", |
| 234 | s1970); |
| 235 | |
| 236 | ret = mc13783_rtc_irq_enable_unlocked(dev, alarm->enabled, |
| 237 | MC13783_IRQ_TODA); |
| 238 | if (unlikely(ret)) |
| 239 | goto out; |
| 240 | |
| 241 | seconds = s1970 % 86400; |
| 242 | days = s1970 / 86400; |
| 243 | |
| 244 | ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAYA, days); |
| 245 | if (unlikely(ret)) |
| 246 | goto out; |
| 247 | |
| 248 | ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTODA, seconds); |
| 249 | |
| 250 | out: |
| 251 | mc13783_unlock(priv->mc13783); |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | static irqreturn_t mc13783_rtc_alarm_handler(int irq, void *dev) |
| 257 | { |
| 258 | struct mc13783_rtc *priv = dev; |
| 259 | struct mc13783 *mc13783 = priv->mc13783; |
| 260 | |
| 261 | dev_dbg(&priv->rtc->dev, "Alarm\n"); |
| 262 | |
| 263 | rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_AF); |
| 264 | |
| 265 | mc13783_irq_ack(mc13783, irq); |
| 266 | |
| 267 | return IRQ_HANDLED; |
| 268 | } |
| 269 | |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 270 | static irqreturn_t mc13783_rtc_update_handler(int irq, void *dev) |
| 271 | { |
| 272 | struct mc13783_rtc *priv = dev; |
| 273 | struct mc13783 *mc13783 = priv->mc13783; |
| 274 | |
| 275 | dev_dbg(&priv->rtc->dev, "1HZ\n"); |
| 276 | |
| 277 | rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_UF); |
| 278 | |
Uwe Kleine-König | 4dd4dd6 | 2010-03-05 13:44:28 -0800 | [diff] [blame] | 279 | mc13783_irq_ack(mc13783, irq); |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 280 | |
| 281 | return IRQ_HANDLED; |
| 282 | } |
| 283 | |
| 284 | static int mc13783_rtc_update_irq_enable(struct device *dev, |
| 285 | unsigned int enabled) |
| 286 | { |
Uwe Kleine-König | 9407351 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 287 | return mc13783_rtc_irq_enable(dev, enabled, MC13783_IRQ_1HZ); |
| 288 | } |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 289 | |
Uwe Kleine-König | 9407351 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 290 | static int mc13783_rtc_alarm_irq_enable(struct device *dev, |
| 291 | unsigned int enabled) |
| 292 | { |
| 293 | return mc13783_rtc_irq_enable(dev, enabled, MC13783_IRQ_TODA); |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static const struct rtc_class_ops mc13783_rtc_ops = { |
| 297 | .read_time = mc13783_rtc_read_time, |
| 298 | .set_mmss = mc13783_rtc_set_mmss, |
Uwe Kleine-König | 9407351 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 299 | .read_alarm = mc13783_rtc_read_alarm, |
| 300 | .set_alarm = mc13783_rtc_set_alarm, |
| 301 | .alarm_irq_enable = mc13783_rtc_alarm_irq_enable, |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 302 | .update_irq_enable = mc13783_rtc_update_irq_enable, |
| 303 | }; |
| 304 | |
| 305 | static irqreturn_t mc13783_rtc_reset_handler(int irq, void *dev) |
| 306 | { |
| 307 | struct mc13783_rtc *priv = dev; |
| 308 | struct mc13783 *mc13783 = priv->mc13783; |
| 309 | |
| 310 | dev_dbg(&priv->rtc->dev, "RTCRST\n"); |
| 311 | priv->valid = 0; |
| 312 | |
Uwe Kleine-König | 4dd4dd6 | 2010-03-05 13:44:28 -0800 | [diff] [blame] | 313 | mc13783_irq_mask(mc13783, irq); |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 314 | |
| 315 | return IRQ_HANDLED; |
| 316 | } |
| 317 | |
| 318 | static int __devinit mc13783_rtc_probe(struct platform_device *pdev) |
| 319 | { |
| 320 | int ret; |
| 321 | struct mc13783_rtc *priv; |
Uwe Kleine-König | 4c014e8 | 2010-03-05 13:44:30 -0800 | [diff] [blame] | 322 | int rtcrst_pending; |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 323 | |
| 324 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 325 | if (!priv) |
| 326 | return -ENOMEM; |
| 327 | |
| 328 | priv->mc13783 = dev_get_drvdata(pdev->dev.parent); |
| 329 | platform_set_drvdata(pdev, priv); |
| 330 | |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 331 | mc13783_lock(priv->mc13783); |
| 332 | |
| 333 | ret = mc13783_irq_request(priv->mc13783, MC13783_IRQ_RTCRST, |
| 334 | mc13783_rtc_reset_handler, DRIVER_NAME, priv); |
| 335 | if (ret) |
| 336 | goto err_reset_irq_request; |
| 337 | |
Uwe Kleine-König | 4c014e8 | 2010-03-05 13:44:30 -0800 | [diff] [blame] | 338 | ret = mc13783_irq_status(priv->mc13783, MC13783_IRQ_RTCRST, |
| 339 | NULL, &rtcrst_pending); |
| 340 | if (ret) |
| 341 | goto err_reset_irq_status; |
| 342 | |
| 343 | priv->valid = !rtcrst_pending; |
| 344 | |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 345 | ret = mc13783_irq_request_nounmask(priv->mc13783, MC13783_IRQ_1HZ, |
| 346 | mc13783_rtc_update_handler, DRIVER_NAME, priv); |
| 347 | if (ret) |
| 348 | goto err_update_irq_request; |
| 349 | |
Uwe Kleine-König | 9407351 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 350 | ret = mc13783_irq_request_nounmask(priv->mc13783, MC13783_IRQ_TODA, |
| 351 | mc13783_rtc_alarm_handler, DRIVER_NAME, priv); |
| 352 | if (ret) |
| 353 | goto err_alarm_irq_request; |
| 354 | |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 355 | priv->rtc = rtc_device_register(pdev->name, |
| 356 | &pdev->dev, &mc13783_rtc_ops, THIS_MODULE); |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 357 | if (IS_ERR(priv->rtc)) { |
| 358 | ret = PTR_ERR(priv->rtc); |
| 359 | |
Uwe Kleine-König | 9407351 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 360 | mc13783_irq_free(priv->mc13783, MC13783_IRQ_TODA, priv); |
| 361 | err_alarm_irq_request: |
| 362 | |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 363 | mc13783_irq_free(priv->mc13783, MC13783_IRQ_1HZ, priv); |
| 364 | err_update_irq_request: |
| 365 | |
Uwe Kleine-König | 4c014e8 | 2010-03-05 13:44:30 -0800 | [diff] [blame] | 366 | err_reset_irq_status: |
| 367 | |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 368 | mc13783_irq_free(priv->mc13783, MC13783_IRQ_RTCRST, priv); |
| 369 | err_reset_irq_request: |
| 370 | |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 371 | platform_set_drvdata(pdev, NULL); |
| 372 | kfree(priv); |
| 373 | } |
| 374 | |
Uwe Kleine-König | 4c014e8 | 2010-03-05 13:44:30 -0800 | [diff] [blame] | 375 | mc13783_unlock(priv->mc13783); |
| 376 | |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | static int __devexit mc13783_rtc_remove(struct platform_device *pdev) |
| 381 | { |
| 382 | struct mc13783_rtc *priv = platform_get_drvdata(pdev); |
| 383 | |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 384 | mc13783_lock(priv->mc13783); |
| 385 | |
Uwe Kleine-König | 4c014e8 | 2010-03-05 13:44:30 -0800 | [diff] [blame] | 386 | rtc_device_unregister(priv->rtc); |
| 387 | |
Uwe Kleine-König | 9407351 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 388 | mc13783_irq_free(priv->mc13783, MC13783_IRQ_TODA, priv); |
Uwe Kleine-König | 43299f2 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 389 | mc13783_irq_free(priv->mc13783, MC13783_IRQ_1HZ, priv); |
| 390 | mc13783_irq_free(priv->mc13783, MC13783_IRQ_RTCRST, priv); |
| 391 | |
| 392 | mc13783_unlock(priv->mc13783); |
| 393 | |
| 394 | platform_set_drvdata(pdev, NULL); |
| 395 | |
| 396 | kfree(priv); |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | static struct platform_driver mc13783_rtc_driver = { |
| 402 | .remove = __devexit_p(mc13783_rtc_remove), |
| 403 | .driver = { |
| 404 | .name = DRIVER_NAME, |
| 405 | .owner = THIS_MODULE, |
| 406 | }, |
| 407 | }; |
| 408 | |
| 409 | static int __init mc13783_rtc_init(void) |
| 410 | { |
| 411 | return platform_driver_probe(&mc13783_rtc_driver, &mc13783_rtc_probe); |
| 412 | } |
| 413 | module_init(mc13783_rtc_init); |
| 414 | |
| 415 | static void __exit mc13783_rtc_exit(void) |
| 416 | { |
| 417 | platform_driver_unregister(&mc13783_rtc_driver); |
| 418 | } |
| 419 | module_exit(mc13783_rtc_exit); |
| 420 | |
| 421 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); |
| 422 | MODULE_DESCRIPTION("RTC driver for Freescale MC13783 PMIC"); |
| 423 | MODULE_LICENSE("GPL v2"); |
| 424 | MODULE_ALIAS("platform:" DRIVER_NAME); |