1eae854b2SBalaji Rao /* NXP PCF50633 RTC Driver 2eae854b2SBalaji Rao * 3eae854b2SBalaji Rao * (C) 2006-2008 by Openmoko, Inc. 4eae854b2SBalaji Rao * Author: Balaji Rao <balajirrao@openmoko.org> 5eae854b2SBalaji Rao * All rights reserved. 6eae854b2SBalaji Rao * 7eae854b2SBalaji Rao * Broken down from monstrous PCF50633 driver mainly by 8eae854b2SBalaji Rao * Harald Welte, Andy Green and Werner Almesberger 9eae854b2SBalaji Rao * 10eae854b2SBalaji Rao * This program is free software; you can redistribute it and/or modify it 11eae854b2SBalaji Rao * under the terms of the GNU General Public License as published by the 12eae854b2SBalaji Rao * Free Software Foundation; either version 2 of the License, or (at your 13eae854b2SBalaji Rao * option) any later version. 14eae854b2SBalaji Rao * 15eae854b2SBalaji Rao */ 16eae854b2SBalaji Rao 17eae854b2SBalaji Rao #include <linux/kernel.h> 18eae854b2SBalaji Rao #include <linux/module.h> 19eae854b2SBalaji Rao #include <linux/init.h> 20eae854b2SBalaji Rao #include <linux/device.h> 215a0e3ad6STejun Heo #include <linux/slab.h> 22eae854b2SBalaji Rao #include <linux/platform_device.h> 23eae854b2SBalaji Rao #include <linux/rtc.h> 24eae854b2SBalaji Rao #include <linux/bcd.h> 25eae854b2SBalaji Rao #include <linux/err.h> 26eae854b2SBalaji Rao 27eae854b2SBalaji Rao #include <linux/mfd/pcf50633/core.h> 28eae854b2SBalaji Rao 29eae854b2SBalaji Rao #define PCF50633_REG_RTCSC 0x59 /* Second */ 30eae854b2SBalaji Rao #define PCF50633_REG_RTCMN 0x5a /* Minute */ 31eae854b2SBalaji Rao #define PCF50633_REG_RTCHR 0x5b /* Hour */ 32eae854b2SBalaji Rao #define PCF50633_REG_RTCWD 0x5c /* Weekday */ 33eae854b2SBalaji Rao #define PCF50633_REG_RTCDT 0x5d /* Day */ 34eae854b2SBalaji Rao #define PCF50633_REG_RTCMT 0x5e /* Month */ 35eae854b2SBalaji Rao #define PCF50633_REG_RTCYR 0x5f /* Year */ 36eae854b2SBalaji Rao #define PCF50633_REG_RTCSCA 0x60 /* Alarm Second */ 37eae854b2SBalaji Rao #define PCF50633_REG_RTCMNA 0x61 /* Alarm Minute */ 38eae854b2SBalaji Rao #define PCF50633_REG_RTCHRA 0x62 /* Alarm Hour */ 39eae854b2SBalaji Rao #define PCF50633_REG_RTCWDA 0x63 /* Alarm Weekday */ 40eae854b2SBalaji Rao #define PCF50633_REG_RTCDTA 0x64 /* Alarm Day */ 41eae854b2SBalaji Rao #define PCF50633_REG_RTCMTA 0x65 /* Alarm Month */ 42eae854b2SBalaji Rao #define PCF50633_REG_RTCYRA 0x66 /* Alarm Year */ 43eae854b2SBalaji Rao 44eae854b2SBalaji Rao enum pcf50633_time_indexes { 45eae854b2SBalaji Rao PCF50633_TI_SEC, 46eae854b2SBalaji Rao PCF50633_TI_MIN, 47eae854b2SBalaji Rao PCF50633_TI_HOUR, 48eae854b2SBalaji Rao PCF50633_TI_WKDAY, 49eae854b2SBalaji Rao PCF50633_TI_DAY, 50eae854b2SBalaji Rao PCF50633_TI_MONTH, 51eae854b2SBalaji Rao PCF50633_TI_YEAR, 52eae854b2SBalaji Rao PCF50633_TI_EXTENT /* always last */ 53eae854b2SBalaji Rao }; 54eae854b2SBalaji Rao 55eae854b2SBalaji Rao struct pcf50633_time { 56eae854b2SBalaji Rao u_int8_t time[PCF50633_TI_EXTENT]; 57eae854b2SBalaji Rao }; 58eae854b2SBalaji Rao 59eae854b2SBalaji Rao struct pcf50633_rtc { 60eae854b2SBalaji Rao int alarm_enabled; 61a766ae3eSWerner Almesberger int alarm_pending; 62eae854b2SBalaji Rao 63eae854b2SBalaji Rao struct pcf50633 *pcf; 64eae854b2SBalaji Rao struct rtc_device *rtc_dev; 65eae854b2SBalaji Rao }; 66eae854b2SBalaji Rao 67eae854b2SBalaji Rao static void pcf2rtc_time(struct rtc_time *rtc, struct pcf50633_time *pcf) 68eae854b2SBalaji Rao { 69eae854b2SBalaji Rao rtc->tm_sec = bcd2bin(pcf->time[PCF50633_TI_SEC]); 70eae854b2SBalaji Rao rtc->tm_min = bcd2bin(pcf->time[PCF50633_TI_MIN]); 71eae854b2SBalaji Rao rtc->tm_hour = bcd2bin(pcf->time[PCF50633_TI_HOUR]); 72eae854b2SBalaji Rao rtc->tm_wday = bcd2bin(pcf->time[PCF50633_TI_WKDAY]); 73eae854b2SBalaji Rao rtc->tm_mday = bcd2bin(pcf->time[PCF50633_TI_DAY]); 74265f2a05SRask Ingemann Lambertsen rtc->tm_mon = bcd2bin(pcf->time[PCF50633_TI_MONTH]) - 1; 75eae854b2SBalaji Rao rtc->tm_year = bcd2bin(pcf->time[PCF50633_TI_YEAR]) + 100; 76eae854b2SBalaji Rao } 77eae854b2SBalaji Rao 78eae854b2SBalaji Rao static void rtc2pcf_time(struct pcf50633_time *pcf, struct rtc_time *rtc) 79eae854b2SBalaji Rao { 80eae854b2SBalaji Rao pcf->time[PCF50633_TI_SEC] = bin2bcd(rtc->tm_sec); 81eae854b2SBalaji Rao pcf->time[PCF50633_TI_MIN] = bin2bcd(rtc->tm_min); 82eae854b2SBalaji Rao pcf->time[PCF50633_TI_HOUR] = bin2bcd(rtc->tm_hour); 83eae854b2SBalaji Rao pcf->time[PCF50633_TI_WKDAY] = bin2bcd(rtc->tm_wday); 84eae854b2SBalaji Rao pcf->time[PCF50633_TI_DAY] = bin2bcd(rtc->tm_mday); 85265f2a05SRask Ingemann Lambertsen pcf->time[PCF50633_TI_MONTH] = bin2bcd(rtc->tm_mon + 1); 86eae854b2SBalaji Rao pcf->time[PCF50633_TI_YEAR] = bin2bcd(rtc->tm_year % 100); 87eae854b2SBalaji Rao } 88eae854b2SBalaji Rao 89eae854b2SBalaji Rao static int 90eae854b2SBalaji Rao pcf50633_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) 91eae854b2SBalaji Rao { 92eae854b2SBalaji Rao struct pcf50633_rtc *rtc = dev_get_drvdata(dev); 93eae854b2SBalaji Rao int err; 94eae854b2SBalaji Rao 95eae854b2SBalaji Rao if (enabled) 96eae854b2SBalaji Rao err = pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM); 97eae854b2SBalaji Rao else 98eae854b2SBalaji Rao err = pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM); 99eae854b2SBalaji Rao 100eae854b2SBalaji Rao if (err < 0) 101eae854b2SBalaji Rao return err; 102eae854b2SBalaji Rao 103eae854b2SBalaji Rao rtc->alarm_enabled = enabled; 104eae854b2SBalaji Rao 105eae854b2SBalaji Rao return 0; 106eae854b2SBalaji Rao } 107eae854b2SBalaji Rao 108eae854b2SBalaji Rao static int pcf50633_rtc_read_time(struct device *dev, struct rtc_time *tm) 109eae854b2SBalaji Rao { 110eae854b2SBalaji Rao struct pcf50633_rtc *rtc; 111eae854b2SBalaji Rao struct pcf50633_time pcf_tm; 112eae854b2SBalaji Rao int ret; 113eae854b2SBalaji Rao 114eae854b2SBalaji Rao rtc = dev_get_drvdata(dev); 115eae854b2SBalaji Rao 116eae854b2SBalaji Rao ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSC, 117eae854b2SBalaji Rao PCF50633_TI_EXTENT, 118eae854b2SBalaji Rao &pcf_tm.time[0]); 119eae854b2SBalaji Rao if (ret != PCF50633_TI_EXTENT) { 120eae854b2SBalaji Rao dev_err(dev, "Failed to read time\n"); 121eae854b2SBalaji Rao return -EIO; 122eae854b2SBalaji Rao } 123eae854b2SBalaji Rao 124eae854b2SBalaji Rao dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n", 125eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_DAY], 126eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_MONTH], 127eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_YEAR], 128eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_HOUR], 129eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_MIN], 130eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_SEC]); 131eae854b2SBalaji Rao 132eae854b2SBalaji Rao pcf2rtc_time(tm, &pcf_tm); 133eae854b2SBalaji Rao 134eae854b2SBalaji Rao dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n", 135eae854b2SBalaji Rao tm->tm_mday, tm->tm_mon, tm->tm_year, 136eae854b2SBalaji Rao tm->tm_hour, tm->tm_min, tm->tm_sec); 137eae854b2SBalaji Rao 138eae854b2SBalaji Rao return rtc_valid_tm(tm); 139eae854b2SBalaji Rao } 140eae854b2SBalaji Rao 141eae854b2SBalaji Rao static int pcf50633_rtc_set_time(struct device *dev, struct rtc_time *tm) 142eae854b2SBalaji Rao { 143eae854b2SBalaji Rao struct pcf50633_rtc *rtc; 144eae854b2SBalaji Rao struct pcf50633_time pcf_tm; 145*3d62b8f5SLars-Peter Clausen int alarm_masked, ret = 0; 146eae854b2SBalaji Rao 147eae854b2SBalaji Rao rtc = dev_get_drvdata(dev); 148eae854b2SBalaji Rao 149eae854b2SBalaji Rao dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n", 150eae854b2SBalaji Rao tm->tm_mday, tm->tm_mon, tm->tm_year, 151eae854b2SBalaji Rao tm->tm_hour, tm->tm_min, tm->tm_sec); 152eae854b2SBalaji Rao 153eae854b2SBalaji Rao rtc2pcf_time(&pcf_tm, tm); 154eae854b2SBalaji Rao 155eae854b2SBalaji Rao dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n", 156eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_DAY], 157eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_MONTH], 158eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_YEAR], 159eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_HOUR], 160eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_MIN], 161eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_SEC]); 162eae854b2SBalaji Rao 163eae854b2SBalaji Rao 164eae854b2SBalaji Rao alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM); 165eae854b2SBalaji Rao 166eae854b2SBalaji Rao if (!alarm_masked) 167eae854b2SBalaji Rao pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM); 168eae854b2SBalaji Rao 169eae854b2SBalaji Rao /* Returns 0 on success */ 170eae854b2SBalaji Rao ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSC, 171eae854b2SBalaji Rao PCF50633_TI_EXTENT, 172eae854b2SBalaji Rao &pcf_tm.time[0]); 173eae854b2SBalaji Rao 174eae854b2SBalaji Rao if (!alarm_masked) 175eae854b2SBalaji Rao pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM); 176eae854b2SBalaji Rao 177eae854b2SBalaji Rao return ret; 178eae854b2SBalaji Rao } 179eae854b2SBalaji Rao 180eae854b2SBalaji Rao static int pcf50633_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 181eae854b2SBalaji Rao { 182eae854b2SBalaji Rao struct pcf50633_rtc *rtc; 183eae854b2SBalaji Rao struct pcf50633_time pcf_tm; 184eae854b2SBalaji Rao int ret = 0; 185eae854b2SBalaji Rao 186eae854b2SBalaji Rao rtc = dev_get_drvdata(dev); 187eae854b2SBalaji Rao 188eae854b2SBalaji Rao alrm->enabled = rtc->alarm_enabled; 189a766ae3eSWerner Almesberger alrm->pending = rtc->alarm_pending; 190eae854b2SBalaji Rao 191eae854b2SBalaji Rao ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSCA, 192eae854b2SBalaji Rao PCF50633_TI_EXTENT, &pcf_tm.time[0]); 193eae854b2SBalaji Rao if (ret != PCF50633_TI_EXTENT) { 194eae854b2SBalaji Rao dev_err(dev, "Failed to read time\n"); 195eae854b2SBalaji Rao return -EIO; 196eae854b2SBalaji Rao } 197eae854b2SBalaji Rao 198eae854b2SBalaji Rao pcf2rtc_time(&alrm->time, &pcf_tm); 199eae854b2SBalaji Rao 200eae854b2SBalaji Rao return rtc_valid_tm(&alrm->time); 201eae854b2SBalaji Rao } 202eae854b2SBalaji Rao 203eae854b2SBalaji Rao static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 204eae854b2SBalaji Rao { 205eae854b2SBalaji Rao struct pcf50633_rtc *rtc; 206eae854b2SBalaji Rao struct pcf50633_time pcf_tm; 207eae854b2SBalaji Rao int alarm_masked, ret = 0; 208eae854b2SBalaji Rao 209eae854b2SBalaji Rao rtc = dev_get_drvdata(dev); 210eae854b2SBalaji Rao 211eae854b2SBalaji Rao rtc2pcf_time(&pcf_tm, &alrm->time); 212eae854b2SBalaji Rao 213eae854b2SBalaji Rao /* do like mktime does and ignore tm_wday */ 214eae854b2SBalaji Rao pcf_tm.time[PCF50633_TI_WKDAY] = 7; 215eae854b2SBalaji Rao 216eae854b2SBalaji Rao alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM); 217eae854b2SBalaji Rao 218eae854b2SBalaji Rao /* disable alarm interrupt */ 219eae854b2SBalaji Rao if (!alarm_masked) 220eae854b2SBalaji Rao pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM); 221eae854b2SBalaji Rao 222eae854b2SBalaji Rao /* Returns 0 on success */ 223eae854b2SBalaji Rao ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSCA, 224eae854b2SBalaji Rao PCF50633_TI_EXTENT, &pcf_tm.time[0]); 225a766ae3eSWerner Almesberger if (!alrm->enabled) 226a766ae3eSWerner Almesberger rtc->alarm_pending = 0; 227eae854b2SBalaji Rao 22805f45d7dSWerner Almesberger if (!alarm_masked || alrm->enabled) 229eae854b2SBalaji Rao pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM); 23005f45d7dSWerner Almesberger rtc->alarm_enabled = alrm->enabled; 231eae854b2SBalaji Rao 232eae854b2SBalaji Rao return ret; 233eae854b2SBalaji Rao } 234eae854b2SBalaji Rao 235eae854b2SBalaji Rao static struct rtc_class_ops pcf50633_rtc_ops = { 236eae854b2SBalaji Rao .read_time = pcf50633_rtc_read_time, 237eae854b2SBalaji Rao .set_time = pcf50633_rtc_set_time, 238eae854b2SBalaji Rao .read_alarm = pcf50633_rtc_read_alarm, 239eae854b2SBalaji Rao .set_alarm = pcf50633_rtc_set_alarm, 240eae854b2SBalaji Rao .alarm_irq_enable = pcf50633_rtc_alarm_irq_enable, 241eae854b2SBalaji Rao }; 242eae854b2SBalaji Rao 243eae854b2SBalaji Rao static void pcf50633_rtc_irq(int irq, void *data) 244eae854b2SBalaji Rao { 245eae854b2SBalaji Rao struct pcf50633_rtc *rtc = data; 246eae854b2SBalaji Rao 247eae854b2SBalaji Rao rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF); 248a766ae3eSWerner Almesberger rtc->alarm_pending = 1; 249eae854b2SBalaji Rao } 250eae854b2SBalaji Rao 251eae854b2SBalaji Rao static int __devinit pcf50633_rtc_probe(struct platform_device *pdev) 252eae854b2SBalaji Rao { 253eae854b2SBalaji Rao struct pcf50633_rtc *rtc; 254eae854b2SBalaji Rao 255eae854b2SBalaji Rao rtc = kzalloc(sizeof(*rtc), GFP_KERNEL); 256eae854b2SBalaji Rao if (!rtc) 257eae854b2SBalaji Rao return -ENOMEM; 258eae854b2SBalaji Rao 25968d641efSLars-Peter Clausen rtc->pcf = dev_to_pcf50633(pdev->dev.parent); 260eae854b2SBalaji Rao platform_set_drvdata(pdev, rtc); 261eae854b2SBalaji Rao rtc->rtc_dev = rtc_device_register("pcf50633-rtc", &pdev->dev, 262eae854b2SBalaji Rao &pcf50633_rtc_ops, THIS_MODULE); 263eae854b2SBalaji Rao 264eae854b2SBalaji Rao if (IS_ERR(rtc->rtc_dev)) { 265fa00e106SDan Carpenter int ret = PTR_ERR(rtc->rtc_dev); 266eae854b2SBalaji Rao kfree(rtc); 267fa00e106SDan Carpenter return ret; 268eae854b2SBalaji Rao } 269eae854b2SBalaji Rao 270eae854b2SBalaji Rao pcf50633_register_irq(rtc->pcf, PCF50633_IRQ_ALARM, 271eae854b2SBalaji Rao pcf50633_rtc_irq, rtc); 272eae854b2SBalaji Rao return 0; 273eae854b2SBalaji Rao } 274eae854b2SBalaji Rao 275eae854b2SBalaji Rao static int __devexit pcf50633_rtc_remove(struct platform_device *pdev) 276eae854b2SBalaji Rao { 277eae854b2SBalaji Rao struct pcf50633_rtc *rtc; 278eae854b2SBalaji Rao 279eae854b2SBalaji Rao rtc = platform_get_drvdata(pdev); 280eae854b2SBalaji Rao 281eae854b2SBalaji Rao pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_ALARM); 282eae854b2SBalaji Rao 283eae854b2SBalaji Rao rtc_device_unregister(rtc->rtc_dev); 284eae854b2SBalaji Rao kfree(rtc); 285eae854b2SBalaji Rao 286eae854b2SBalaji Rao return 0; 287eae854b2SBalaji Rao } 288eae854b2SBalaji Rao 289eae854b2SBalaji Rao static struct platform_driver pcf50633_rtc_driver = { 290eae854b2SBalaji Rao .driver = { 291eae854b2SBalaji Rao .name = "pcf50633-rtc", 292eae854b2SBalaji Rao }, 293eae854b2SBalaji Rao .probe = pcf50633_rtc_probe, 294eae854b2SBalaji Rao .remove = __devexit_p(pcf50633_rtc_remove), 295eae854b2SBalaji Rao }; 296eae854b2SBalaji Rao 297eae854b2SBalaji Rao static int __init pcf50633_rtc_init(void) 298eae854b2SBalaji Rao { 299eae854b2SBalaji Rao return platform_driver_register(&pcf50633_rtc_driver); 300eae854b2SBalaji Rao } 301eae854b2SBalaji Rao module_init(pcf50633_rtc_init); 302eae854b2SBalaji Rao 303eae854b2SBalaji Rao static void __exit pcf50633_rtc_exit(void) 304eae854b2SBalaji Rao { 305eae854b2SBalaji Rao platform_driver_unregister(&pcf50633_rtc_driver); 306eae854b2SBalaji Rao } 307eae854b2SBalaji Rao module_exit(pcf50633_rtc_exit); 308eae854b2SBalaji Rao 309eae854b2SBalaji Rao MODULE_DESCRIPTION("PCF50633 RTC driver"); 310eae854b2SBalaji Rao MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>"); 311eae854b2SBalaji Rao MODULE_LICENSE("GPL"); 312eae854b2SBalaji Rao 313