1 /* 2 * Real Time Clock driver for Freescale MC13XXX 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/mc13xxx.h> 13 #include <linux/platform_device.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/mod_devicetable.h> 17 #include <linux/slab.h> 18 #include <linux/rtc.h> 19 20 #define DRIVER_NAME "mc13xxx-rtc" 21 22 #define MC13XXX_RTCTOD 20 23 #define MC13XXX_RTCTODA 21 24 #define MC13XXX_RTCDAY 22 25 #define MC13XXX_RTCDAYA 23 26 27 #define SEC_PER_DAY (24 * 60 * 60) 28 29 struct mc13xxx_rtc { 30 struct rtc_device *rtc; 31 struct mc13xxx *mc13xxx; 32 int valid; 33 }; 34 35 static int mc13xxx_rtc_irq_enable_unlocked(struct device *dev, 36 unsigned int enabled, int irq) 37 { 38 struct mc13xxx_rtc *priv = dev_get_drvdata(dev); 39 int (*func)(struct mc13xxx *mc13xxx, int irq); 40 41 if (!priv->valid) 42 return -ENODATA; 43 44 func = enabled ? mc13xxx_irq_unmask : mc13xxx_irq_mask; 45 return func(priv->mc13xxx, irq); 46 } 47 48 static int mc13xxx_rtc_alarm_irq_enable(struct device *dev, 49 unsigned int enabled) 50 { 51 struct mc13xxx_rtc *priv = dev_get_drvdata(dev); 52 int ret; 53 54 mc13xxx_lock(priv->mc13xxx); 55 56 ret = mc13xxx_rtc_irq_enable_unlocked(dev, enabled, MC13XXX_IRQ_TODA); 57 58 mc13xxx_unlock(priv->mc13xxx); 59 60 return ret; 61 } 62 63 static int mc13xxx_rtc_read_time(struct device *dev, struct rtc_time *tm) 64 { 65 struct mc13xxx_rtc *priv = dev_get_drvdata(dev); 66 unsigned int seconds, days1, days2; 67 68 if (!priv->valid) 69 return -ENODATA; 70 71 do { 72 int ret; 73 74 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days1); 75 if (ret) 76 return ret; 77 78 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTOD, &seconds); 79 if (ret) 80 return ret; 81 82 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days2); 83 if (ret) 84 return ret; 85 } while (days1 != days2); 86 87 rtc_time64_to_tm((time64_t)days1 * SEC_PER_DAY + seconds, tm); 88 89 return 0; 90 } 91 92 static int mc13xxx_rtc_set_mmss(struct device *dev, time64_t secs) 93 { 94 struct mc13xxx_rtc *priv = dev_get_drvdata(dev); 95 unsigned int seconds, days; 96 unsigned int alarmseconds; 97 int ret; 98 99 days = div_s64_rem(secs, SEC_PER_DAY, &seconds); 100 101 mc13xxx_lock(priv->mc13xxx); 102 103 /* 104 * temporarily invalidate alarm to prevent triggering it when the day is 105 * already updated while the time isn't yet. 106 */ 107 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &alarmseconds); 108 if (unlikely(ret)) 109 goto out; 110 111 if (alarmseconds < SEC_PER_DAY) { 112 ret = mc13xxx_reg_write(priv->mc13xxx, 113 MC13XXX_RTCTODA, 0x1ffff); 114 if (unlikely(ret)) 115 goto out; 116 } 117 118 /* 119 * write seconds=0 to prevent a day switch between writing days 120 * and seconds below 121 */ 122 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, 0); 123 if (unlikely(ret)) 124 goto out; 125 126 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAY, days); 127 if (unlikely(ret)) 128 goto out; 129 130 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, seconds); 131 if (unlikely(ret)) 132 goto out; 133 134 /* restore alarm */ 135 if (alarmseconds < SEC_PER_DAY) { 136 ret = mc13xxx_reg_write(priv->mc13xxx, 137 MC13XXX_RTCTODA, alarmseconds); 138 if (unlikely(ret)) 139 goto out; 140 } 141 142 if (!priv->valid) { 143 ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_RTCRST); 144 if (unlikely(ret)) 145 goto out; 146 147 ret = mc13xxx_irq_unmask(priv->mc13xxx, MC13XXX_IRQ_RTCRST); 148 } 149 150 out: 151 priv->valid = !ret; 152 153 mc13xxx_unlock(priv->mc13xxx); 154 155 return ret; 156 } 157 158 static int mc13xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) 159 { 160 struct mc13xxx_rtc *priv = dev_get_drvdata(dev); 161 unsigned seconds, days; 162 time64_t s1970; 163 int enabled, pending; 164 int ret; 165 166 mc13xxx_lock(priv->mc13xxx); 167 168 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &seconds); 169 if (unlikely(ret)) 170 goto out; 171 if (seconds >= SEC_PER_DAY) { 172 ret = -ENODATA; 173 goto out; 174 } 175 176 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days); 177 if (unlikely(ret)) 178 goto out; 179 180 ret = mc13xxx_irq_status(priv->mc13xxx, MC13XXX_IRQ_TODA, 181 &enabled, &pending); 182 183 out: 184 mc13xxx_unlock(priv->mc13xxx); 185 186 if (ret) 187 return ret; 188 189 alarm->enabled = enabled; 190 alarm->pending = pending; 191 192 s1970 = (time64_t)days * SEC_PER_DAY + seconds; 193 194 rtc_time64_to_tm(s1970, &alarm->time); 195 dev_dbg(dev, "%s: %lld\n", __func__, (long long)s1970); 196 197 return 0; 198 } 199 200 static int mc13xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 201 { 202 struct mc13xxx_rtc *priv = dev_get_drvdata(dev); 203 time64_t s1970; 204 u32 seconds, days; 205 int ret; 206 207 mc13xxx_lock(priv->mc13xxx); 208 209 /* disable alarm to prevent false triggering */ 210 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, 0x1ffff); 211 if (unlikely(ret)) 212 goto out; 213 214 ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_TODA); 215 if (unlikely(ret)) 216 goto out; 217 218 s1970 = rtc_tm_to_time64(&alarm->time); 219 220 dev_dbg(dev, "%s: %s %lld\n", __func__, alarm->enabled ? "on" : "off", 221 (long long)s1970); 222 223 ret = mc13xxx_rtc_irq_enable_unlocked(dev, alarm->enabled, 224 MC13XXX_IRQ_TODA); 225 if (unlikely(ret)) 226 goto out; 227 228 days = div_s64_rem(s1970, SEC_PER_DAY, &seconds); 229 230 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAYA, days); 231 if (unlikely(ret)) 232 goto out; 233 234 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, seconds); 235 236 out: 237 mc13xxx_unlock(priv->mc13xxx); 238 239 return ret; 240 } 241 242 static irqreturn_t mc13xxx_rtc_alarm_handler(int irq, void *dev) 243 { 244 struct mc13xxx_rtc *priv = dev; 245 struct mc13xxx *mc13xxx = priv->mc13xxx; 246 247 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_AF); 248 249 mc13xxx_irq_ack(mc13xxx, irq); 250 251 return IRQ_HANDLED; 252 } 253 254 static const struct rtc_class_ops mc13xxx_rtc_ops = { 255 .read_time = mc13xxx_rtc_read_time, 256 .set_mmss64 = mc13xxx_rtc_set_mmss, 257 .read_alarm = mc13xxx_rtc_read_alarm, 258 .set_alarm = mc13xxx_rtc_set_alarm, 259 .alarm_irq_enable = mc13xxx_rtc_alarm_irq_enable, 260 }; 261 262 static irqreturn_t mc13xxx_rtc_reset_handler(int irq, void *dev) 263 { 264 struct mc13xxx_rtc *priv = dev; 265 struct mc13xxx *mc13xxx = priv->mc13xxx; 266 267 priv->valid = 0; 268 269 mc13xxx_irq_mask(mc13xxx, irq); 270 271 return IRQ_HANDLED; 272 } 273 274 static int __init mc13xxx_rtc_probe(struct platform_device *pdev) 275 { 276 int ret; 277 struct mc13xxx_rtc *priv; 278 struct mc13xxx *mc13xxx; 279 280 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 281 if (!priv) 282 return -ENOMEM; 283 284 mc13xxx = dev_get_drvdata(pdev->dev.parent); 285 priv->mc13xxx = mc13xxx; 286 priv->valid = 1; 287 288 platform_set_drvdata(pdev, priv); 289 290 mc13xxx_lock(mc13xxx); 291 292 mc13xxx_irq_ack(mc13xxx, MC13XXX_IRQ_RTCRST); 293 294 ret = mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_RTCRST, 295 mc13xxx_rtc_reset_handler, DRIVER_NAME, priv); 296 if (ret) 297 goto err_irq_request; 298 299 ret = mc13xxx_irq_request_nounmask(mc13xxx, MC13XXX_IRQ_TODA, 300 mc13xxx_rtc_alarm_handler, DRIVER_NAME, priv); 301 if (ret) 302 goto err_irq_request; 303 304 mc13xxx_unlock(mc13xxx); 305 306 priv->rtc = devm_rtc_device_register(&pdev->dev, pdev->name, 307 &mc13xxx_rtc_ops, THIS_MODULE); 308 309 return 0; 310 311 err_irq_request: 312 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_TODA, priv); 313 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_RTCRST, priv); 314 315 mc13xxx_unlock(mc13xxx); 316 317 return ret; 318 } 319 320 static int mc13xxx_rtc_remove(struct platform_device *pdev) 321 { 322 struct mc13xxx_rtc *priv = platform_get_drvdata(pdev); 323 324 mc13xxx_lock(priv->mc13xxx); 325 326 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TODA, priv); 327 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_RTCRST, priv); 328 329 mc13xxx_unlock(priv->mc13xxx); 330 331 return 0; 332 } 333 334 static const struct platform_device_id mc13xxx_rtc_idtable[] = { 335 { 336 .name = "mc13783-rtc", 337 }, { 338 .name = "mc13892-rtc", 339 }, { 340 .name = "mc34708-rtc", 341 }, 342 { /* sentinel */ } 343 }; 344 MODULE_DEVICE_TABLE(platform, mc13xxx_rtc_idtable); 345 346 static struct platform_driver mc13xxx_rtc_driver = { 347 .id_table = mc13xxx_rtc_idtable, 348 .remove = mc13xxx_rtc_remove, 349 .driver = { 350 .name = DRIVER_NAME, 351 }, 352 }; 353 354 module_platform_driver_probe(mc13xxx_rtc_driver, &mc13xxx_rtc_probe); 355 356 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); 357 MODULE_DESCRIPTION("RTC driver for Freescale MC13XXX PMIC"); 358 MODULE_LICENSE("GPL v2"); 359