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