Uwe Kleine-König | 43299f285 | 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 | |
| 31 | static int mc13783_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 32 | { |
| 33 | struct mc13783_rtc *priv = dev_get_drvdata(dev); |
| 34 | unsigned int seconds, days1, days2; |
| 35 | unsigned long s1970; |
| 36 | int ret; |
| 37 | |
| 38 | mc13783_lock(priv->mc13783); |
| 39 | |
| 40 | if (!priv->valid) { |
| 41 | ret = -ENODATA; |
| 42 | goto out; |
| 43 | } |
| 44 | |
| 45 | ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days1); |
| 46 | if (unlikely(ret)) |
| 47 | goto out; |
| 48 | |
| 49 | ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTOD, &seconds); |
| 50 | if (unlikely(ret)) |
| 51 | goto out; |
| 52 | |
| 53 | ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days2); |
| 54 | out: |
| 55 | mc13783_unlock(priv->mc13783); |
| 56 | |
| 57 | if (ret) |
| 58 | return ret; |
| 59 | |
| 60 | if (days2 == days1 + 1) { |
| 61 | if (seconds >= 86400 / 2) |
| 62 | days2 = days1; |
| 63 | else |
| 64 | days1 = days2; |
| 65 | } |
| 66 | |
| 67 | if (days1 != days2) |
| 68 | return -EIO; |
| 69 | |
| 70 | s1970 = days1 * 86400 + seconds; |
| 71 | |
| 72 | rtc_time_to_tm(s1970, tm); |
| 73 | |
| 74 | return rtc_valid_tm(tm); |
| 75 | } |
| 76 | |
| 77 | static int mc13783_rtc_set_mmss(struct device *dev, unsigned long secs) |
| 78 | { |
| 79 | struct mc13783_rtc *priv = dev_get_drvdata(dev); |
| 80 | unsigned int seconds, days; |
| 81 | int ret; |
| 82 | |
| 83 | seconds = secs % 86400; |
| 84 | days = secs / 86400; |
| 85 | |
| 86 | mc13783_lock(priv->mc13783); |
| 87 | |
| 88 | /* |
| 89 | * first write seconds=0 to prevent a day switch between writing days |
| 90 | * and seconds below |
| 91 | */ |
| 92 | ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, 0); |
| 93 | if (unlikely(ret)) |
| 94 | goto out; |
| 95 | |
| 96 | ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAY, days); |
| 97 | if (unlikely(ret)) |
| 98 | goto out; |
| 99 | |
| 100 | ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, seconds); |
| 101 | if (unlikely(ret)) |
| 102 | goto out; |
| 103 | |
Uwe Kleine-König | 4dd4dd6 | 2010-03-05 13:44:28 -0800 | [diff] [blame] | 104 | ret = mc13783_irq_ack(priv->mc13783, MC13783_IRQ_RTCRST); |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 105 | if (unlikely(ret)) |
| 106 | goto out; |
| 107 | |
Uwe Kleine-König | 4dd4dd6 | 2010-03-05 13:44:28 -0800 | [diff] [blame] | 108 | ret = mc13783_irq_unmask(priv->mc13783, MC13783_IRQ_RTCRST); |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 109 | out: |
| 110 | priv->valid = !ret; |
| 111 | |
| 112 | mc13783_unlock(priv->mc13783); |
| 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static irqreturn_t mc13783_rtc_update_handler(int irq, void *dev) |
| 118 | { |
| 119 | struct mc13783_rtc *priv = dev; |
| 120 | struct mc13783 *mc13783 = priv->mc13783; |
| 121 | |
| 122 | dev_dbg(&priv->rtc->dev, "1HZ\n"); |
| 123 | |
| 124 | rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_UF); |
| 125 | |
Uwe Kleine-König | 4dd4dd6 | 2010-03-05 13:44:28 -0800 | [diff] [blame] | 126 | mc13783_irq_ack(mc13783, irq); |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 127 | |
| 128 | return IRQ_HANDLED; |
| 129 | } |
| 130 | |
| 131 | static int mc13783_rtc_update_irq_enable(struct device *dev, |
| 132 | unsigned int enabled) |
| 133 | { |
| 134 | struct mc13783_rtc *priv = dev_get_drvdata(dev); |
| 135 | int ret = -ENODATA; |
| 136 | |
| 137 | mc13783_lock(priv->mc13783); |
| 138 | if (!priv->valid) |
| 139 | goto out; |
| 140 | |
Uwe Kleine-König | 4dd4dd6 | 2010-03-05 13:44:28 -0800 | [diff] [blame] | 141 | ret = (enabled ? mc13783_irq_unmask : mc13783_irq_mask)(priv->mc13783, |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 142 | MC13783_IRQ_1HZ); |
| 143 | out: |
| 144 | mc13783_unlock(priv->mc13783); |
| 145 | |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | static const struct rtc_class_ops mc13783_rtc_ops = { |
| 150 | .read_time = mc13783_rtc_read_time, |
| 151 | .set_mmss = mc13783_rtc_set_mmss, |
| 152 | .update_irq_enable = mc13783_rtc_update_irq_enable, |
| 153 | }; |
| 154 | |
| 155 | static irqreturn_t mc13783_rtc_reset_handler(int irq, void *dev) |
| 156 | { |
| 157 | struct mc13783_rtc *priv = dev; |
| 158 | struct mc13783 *mc13783 = priv->mc13783; |
| 159 | |
| 160 | dev_dbg(&priv->rtc->dev, "RTCRST\n"); |
| 161 | priv->valid = 0; |
| 162 | |
Uwe Kleine-König | 4dd4dd6 | 2010-03-05 13:44:28 -0800 | [diff] [blame] | 163 | mc13783_irq_mask(mc13783, irq); |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 164 | |
| 165 | return IRQ_HANDLED; |
| 166 | } |
| 167 | |
| 168 | static int __devinit mc13783_rtc_probe(struct platform_device *pdev) |
| 169 | { |
| 170 | int ret; |
| 171 | struct mc13783_rtc *priv; |
Uwe Kleine-König | 4c014e8 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 172 | int rtcrst_pending; |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 173 | |
| 174 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 175 | if (!priv) |
| 176 | return -ENOMEM; |
| 177 | |
| 178 | priv->mc13783 = dev_get_drvdata(pdev->dev.parent); |
| 179 | platform_set_drvdata(pdev, priv); |
| 180 | |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 181 | mc13783_lock(priv->mc13783); |
| 182 | |
| 183 | ret = mc13783_irq_request(priv->mc13783, MC13783_IRQ_RTCRST, |
| 184 | mc13783_rtc_reset_handler, DRIVER_NAME, priv); |
| 185 | if (ret) |
| 186 | goto err_reset_irq_request; |
| 187 | |
Uwe Kleine-König | 4c014e8 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 188 | ret = mc13783_irq_status(priv->mc13783, MC13783_IRQ_RTCRST, |
| 189 | NULL, &rtcrst_pending); |
| 190 | if (ret) |
| 191 | goto err_reset_irq_status; |
| 192 | |
| 193 | priv->valid = !rtcrst_pending; |
| 194 | |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 195 | ret = mc13783_irq_request_nounmask(priv->mc13783, MC13783_IRQ_1HZ, |
| 196 | mc13783_rtc_update_handler, DRIVER_NAME, priv); |
| 197 | if (ret) |
| 198 | goto err_update_irq_request; |
| 199 | |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 200 | priv->rtc = rtc_device_register(pdev->name, |
| 201 | &pdev->dev, &mc13783_rtc_ops, THIS_MODULE); |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 202 | if (IS_ERR(priv->rtc)) { |
| 203 | ret = PTR_ERR(priv->rtc); |
| 204 | |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 205 | mc13783_irq_free(priv->mc13783, MC13783_IRQ_1HZ, priv); |
| 206 | err_update_irq_request: |
| 207 | |
Uwe Kleine-König | 4c014e8 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 208 | err_reset_irq_status: |
| 209 | |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 210 | mc13783_irq_free(priv->mc13783, MC13783_IRQ_RTCRST, priv); |
| 211 | err_reset_irq_request: |
| 212 | |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 213 | platform_set_drvdata(pdev, NULL); |
| 214 | kfree(priv); |
| 215 | } |
| 216 | |
Uwe Kleine-König | 4c014e8 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 217 | mc13783_unlock(priv->mc13783); |
| 218 | |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | static int __devexit mc13783_rtc_remove(struct platform_device *pdev) |
| 223 | { |
| 224 | struct mc13783_rtc *priv = platform_get_drvdata(pdev); |
| 225 | |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 226 | mc13783_lock(priv->mc13783); |
| 227 | |
Uwe Kleine-König | 4c014e8 | 2010-03-05 13:44:30 -0800 | [diff] [blame^] | 228 | rtc_device_unregister(priv->rtc); |
| 229 | |
Uwe Kleine-König | 43299f285 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 230 | mc13783_irq_free(priv->mc13783, MC13783_IRQ_1HZ, priv); |
| 231 | mc13783_irq_free(priv->mc13783, MC13783_IRQ_RTCRST, priv); |
| 232 | |
| 233 | mc13783_unlock(priv->mc13783); |
| 234 | |
| 235 | platform_set_drvdata(pdev, NULL); |
| 236 | |
| 237 | kfree(priv); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static struct platform_driver mc13783_rtc_driver = { |
| 243 | .remove = __devexit_p(mc13783_rtc_remove), |
| 244 | .driver = { |
| 245 | .name = DRIVER_NAME, |
| 246 | .owner = THIS_MODULE, |
| 247 | }, |
| 248 | }; |
| 249 | |
| 250 | static int __init mc13783_rtc_init(void) |
| 251 | { |
| 252 | return platform_driver_probe(&mc13783_rtc_driver, &mc13783_rtc_probe); |
| 253 | } |
| 254 | module_init(mc13783_rtc_init); |
| 255 | |
| 256 | static void __exit mc13783_rtc_exit(void) |
| 257 | { |
| 258 | platform_driver_unregister(&mc13783_rtc_driver); |
| 259 | } |
| 260 | module_exit(mc13783_rtc_exit); |
| 261 | |
| 262 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); |
| 263 | MODULE_DESCRIPTION("RTC driver for Freescale MC13783 PMIC"); |
| 264 | MODULE_LICENSE("GPL v2"); |
| 265 | MODULE_ALIAS("platform:" DRIVER_NAME); |