diff options
| author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
|---|---|---|
| committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
| commit | fcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch) | |
| tree | a57612d1888735a2ec7972891b68c1ac5ec8faea /drivers/rtc/rtc-max8907c.c | |
| parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) | |
Diffstat (limited to 'drivers/rtc/rtc-max8907c.c')
| -rw-r--r-- | drivers/rtc/rtc-max8907c.c | 323 |
1 files changed, 323 insertions, 0 deletions
diff --git a/drivers/rtc/rtc-max8907c.c b/drivers/rtc/rtc-max8907c.c new file mode 100644 index 00000000000..f7287021da3 --- /dev/null +++ b/drivers/rtc/rtc-max8907c.c | |||
| @@ -0,0 +1,323 @@ | |||
| 1 | /* | ||
| 2 | * RTC driver for Maxim MAX8907c | ||
| 3 | * | ||
| 4 | * Copyright (c) 2011, NVIDIA Corporation. | ||
| 5 | * Based on drivers/rtc/rtc-max8925.c, Copyright (C) 2009-2010 Marvell International Ltd. | ||
| 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/module.h> | ||
| 13 | #include <linux/i2c.h> | ||
| 14 | #include <linux/slab.h> | ||
| 15 | #include <linux/rtc.h> | ||
| 16 | #include <linux/platform_device.h> | ||
| 17 | #include <linux/mfd/max8907c.h> | ||
| 18 | |||
| 19 | enum { | ||
| 20 | RTC_SEC = 0, | ||
| 21 | RTC_MIN, | ||
| 22 | RTC_HOUR, | ||
| 23 | RTC_WEEKDAY, | ||
| 24 | RTC_DATE, | ||
| 25 | RTC_MONTH, | ||
| 26 | RTC_YEAR1, | ||
| 27 | RTC_YEAR2, | ||
| 28 | }; | ||
| 29 | |||
| 30 | #define TIME_NUM 8 | ||
| 31 | #define ALARM_1SEC (1 << 7) | ||
| 32 | #define HOUR_12 (1 << 7) | ||
| 33 | #define HOUR_AM_PM (1 << 5) | ||
| 34 | #define ALARM0_IRQ (1 << 3) | ||
| 35 | #define ALARM1_IRQ (1 << 2) | ||
| 36 | #define ALARM0_STATUS (1 << 2) | ||
| 37 | #define ALARM1_STATUS (1 << 1) | ||
| 38 | |||
| 39 | struct max8907c_rtc_info { | ||
| 40 | struct rtc_device *rtc_dev; | ||
| 41 | struct i2c_client *i2c; | ||
| 42 | struct max8907c *chip; | ||
| 43 | }; | ||
| 44 | |||
| 45 | static irqreturn_t rtc_update_handler(int irq, void *data) | ||
| 46 | { | ||
| 47 | struct max8907c_rtc_info *info = (struct max8907c_rtc_info *)data; | ||
| 48 | |||
| 49 | /* disable ALARM0 except for 1SEC alarm */ | ||
| 50 | max8907c_set_bits(info->i2c, MAX8907C_REG_ALARM0_CNTL, 0x7f, 0); | ||
| 51 | rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF); | ||
| 52 | return IRQ_HANDLED; | ||
| 53 | } | ||
| 54 | |||
| 55 | static int tm_calc(struct rtc_time *tm, u8 *buf, int len) | ||
| 56 | { | ||
| 57 | if (len < TIME_NUM) | ||
| 58 | return -EINVAL; | ||
| 59 | tm->tm_year = (buf[RTC_YEAR2] >> 4) * 1000 | ||
| 60 | + (buf[RTC_YEAR2] & 0xf) * 100 | ||
| 61 | + (buf[RTC_YEAR1] >> 4) * 10 | ||
| 62 | + (buf[RTC_YEAR1] & 0xf); | ||
| 63 | tm->tm_year -= 1900; | ||
| 64 | /* RTC month index issue in max8907c | ||
| 65 | : January index is 1 but kernel assumes it as 0 */ | ||
| 66 | tm->tm_mon = ((buf[RTC_MONTH] >> 4) & 0x01) * 10 | ||
| 67 | + (buf[RTC_MONTH] & 0x0f) - 1; | ||
| 68 | tm->tm_mday = ((buf[RTC_DATE] >> 4) & 0x03) * 10 | ||
| 69 | + (buf[RTC_DATE] & 0x0f); | ||
| 70 | tm->tm_wday = buf[RTC_WEEKDAY] & 0x07; | ||
| 71 | if (buf[RTC_HOUR] & HOUR_12) { | ||
| 72 | tm->tm_hour = ((buf[RTC_HOUR] >> 4) & 0x1) * 10 | ||
| 73 | + (buf[RTC_HOUR] & 0x0f); | ||
| 74 | if (buf[RTC_HOUR] & HOUR_AM_PM) | ||
| 75 | tm->tm_hour += 12; | ||
| 76 | } else { | ||
| 77 | tm->tm_hour = ((buf[RTC_HOUR] >> 4) & 0x03) * 10 | ||
| 78 | + (buf[RTC_HOUR] & 0x0f); | ||
| 79 | } | ||
| 80 | tm->tm_min = ((buf[RTC_MIN] >> 4) & 0x7) * 10 | ||
| 81 | + (buf[RTC_MIN] & 0x0f); | ||
| 82 | tm->tm_sec = ((buf[RTC_SEC] >> 4) & 0x7) * 10 | ||
| 83 | + (buf[RTC_SEC] & 0x0f); | ||
| 84 | return 0; | ||
| 85 | } | ||
| 86 | |||
| 87 | static int data_calc(u8 *buf, struct rtc_time *tm, int len) | ||
| 88 | { | ||
| 89 | u8 high, low; | ||
| 90 | |||
| 91 | if (len < TIME_NUM) | ||
| 92 | return -EINVAL; | ||
| 93 | |||
| 94 | high = (tm->tm_year + 1900) / 1000; | ||
| 95 | low = (tm->tm_year + 1900) / 100; | ||
| 96 | low = low - high * 10; | ||
| 97 | buf[RTC_YEAR2] = (high << 4) + low; | ||
| 98 | high = (tm->tm_year + 1900) / 10; | ||
| 99 | low = tm->tm_year + 1900; | ||
| 100 | low = low - high * 10; | ||
| 101 | high = high - (high / 10) * 10; | ||
| 102 | buf[RTC_YEAR1] = (high << 4) + low; | ||
| 103 | |||
| 104 | /* RTC month index issue in max8907c | ||
| 105 | : January index is 1 but kernel assumes it as 0 */ | ||
| 106 | high = (tm->tm_mon + 1) / 10; | ||
| 107 | low = (tm->tm_mon + 1) % 10; | ||
| 108 | buf[RTC_MONTH] = (high << 4) + low; | ||
| 109 | |||
| 110 | high = tm->tm_mday / 10; | ||
| 111 | low = tm->tm_mday; | ||
| 112 | low = low - high * 10; | ||
| 113 | buf[RTC_DATE] = (high << 4) + low; | ||
| 114 | buf[RTC_WEEKDAY] = tm->tm_wday; | ||
| 115 | high = tm->tm_hour / 10; | ||
| 116 | low = tm->tm_hour; | ||
| 117 | low = low - high * 10; | ||
| 118 | buf[RTC_HOUR] = (high << 4) + low; | ||
| 119 | high = tm->tm_min / 10; | ||
| 120 | low = tm->tm_min; | ||
| 121 | low = low - high * 10; | ||
| 122 | buf[RTC_MIN] = (high << 4) + low; | ||
| 123 | high = tm->tm_sec / 10; | ||
| 124 | low = tm->tm_sec; | ||
| 125 | low = low - high * 10; | ||
| 126 | buf[RTC_SEC] = (high << 4) + low; | ||
| 127 | return 0; | ||
| 128 | } | ||
| 129 | |||
| 130 | static int max8907c_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
| 131 | { | ||
| 132 | struct max8907c_rtc_info *info = dev_get_drvdata(dev); | ||
| 133 | u8 buf[TIME_NUM]; | ||
| 134 | int ret; | ||
| 135 | |||
| 136 | ret = max8907c_reg_bulk_read(info->i2c, MAX8907C_REG_RTC_SEC, TIME_NUM, buf); | ||
| 137 | |||
| 138 | if (ret < 0) | ||
| 139 | return ret; | ||
| 140 | ret = tm_calc(tm, buf, TIME_NUM); | ||
| 141 | |||
| 142 | return ret; | ||
| 143 | } | ||
| 144 | |||
| 145 | static int max8907c_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
| 146 | { | ||
| 147 | struct max8907c_rtc_info *info = dev_get_drvdata(dev); | ||
| 148 | u8 buf[TIME_NUM]; | ||
| 149 | int ret; | ||
| 150 | |||
| 151 | ret = data_calc(buf, tm, TIME_NUM); | ||
| 152 | |||
| 153 | if (ret < 0) | ||
| 154 | return ret; | ||
| 155 | ret = max8907c_reg_bulk_write(info->i2c, MAX8907C_REG_RTC_SEC, TIME_NUM, buf); | ||
| 156 | |||
| 157 | return ret; | ||
| 158 | } | ||
| 159 | |||
| 160 | static int max8907c_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
| 161 | { | ||
| 162 | struct max8907c_rtc_info *info = dev_get_drvdata(dev); | ||
| 163 | unsigned char buf[TIME_NUM]; | ||
| 164 | int ret; | ||
| 165 | |||
| 166 | ret = max8907c_reg_bulk_read(info->i2c, MAX8907C_REG_ALARM0_SEC, TIME_NUM, buf); | ||
| 167 | if (ret < 0) | ||
| 168 | return ret; | ||
| 169 | ret = tm_calc(&alrm->time, buf, TIME_NUM); | ||
| 170 | if (ret < 0) | ||
| 171 | return ret; | ||
| 172 | ret = max8907c_reg_read(info->i2c, MAX8907C_REG_RTC_IRQ_MASK); | ||
| 173 | if (ret < 0) | ||
| 174 | return ret; | ||
| 175 | if ((ret & ALARM0_IRQ) == 0) | ||
| 176 | alrm->enabled = 1; | ||
| 177 | else | ||
| 178 | alrm->enabled = 0; | ||
| 179 | ret = max8907c_reg_read(info->i2c, MAX8907C_REG_RTC_STATUS); | ||
| 180 | if (ret < 0) | ||
| 181 | return ret; | ||
| 182 | if (ret & ALARM0_STATUS) | ||
| 183 | alrm->pending = 1; | ||
| 184 | else | ||
| 185 | alrm->pending = 0; | ||
| 186 | |||
| 187 | return ret; | ||
| 188 | } | ||
| 189 | |||
| 190 | static int max8907c_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
| 191 | { | ||
| 192 | struct max8907c_rtc_info *info = dev_get_drvdata(dev); | ||
| 193 | unsigned char buf[TIME_NUM]; | ||
| 194 | int ret; | ||
| 195 | |||
| 196 | ret = data_calc(buf, &alrm->time, TIME_NUM); | ||
| 197 | if (ret < 0) | ||
| 198 | return ret; | ||
| 199 | ret = max8907c_reg_bulk_write(info->i2c, MAX8907C_REG_ALARM0_SEC, TIME_NUM, buf); | ||
| 200 | if (ret < 0) | ||
| 201 | return ret; | ||
| 202 | /* only enable alarm on year/month/day/hour/min/sec */ | ||
| 203 | ret = max8907c_reg_write(info->i2c, MAX8907C_REG_ALARM0_CNTL, 0x77); | ||
| 204 | |||
| 205 | return ret; | ||
| 206 | } | ||
| 207 | |||
| 208 | static const struct rtc_class_ops max8907c_rtc_ops = { | ||
| 209 | .read_time = max8907c_rtc_read_time, | ||
| 210 | .set_time = max8907c_rtc_set_time, | ||
| 211 | .read_alarm = max8907c_rtc_read_alarm, | ||
| 212 | .set_alarm = max8907c_rtc_set_alarm, | ||
| 213 | }; | ||
| 214 | |||
| 215 | static int __devinit max8907c_rtc_probe(struct platform_device *pdev) | ||
| 216 | { | ||
| 217 | struct max8907c *chip = dev_get_drvdata(pdev->dev.parent); | ||
| 218 | struct max8907c_rtc_info *info; | ||
| 219 | int irq, ret; | ||
| 220 | |||
| 221 | info = kzalloc(sizeof(struct max8907c_rtc_info), GFP_KERNEL); | ||
| 222 | if (!info) | ||
| 223 | return -ENOMEM; | ||
| 224 | info->i2c = chip->i2c_rtc; | ||
| 225 | info->chip = chip; | ||
| 226 | |||
| 227 | irq = chip->irq_base + MAX8907C_IRQ_RTC_ALARM0; | ||
| 228 | |||
| 229 | ret = request_threaded_irq(irq, NULL, rtc_update_handler, | ||
| 230 | IRQF_ONESHOT, "rtc-alarm0", info); | ||
| 231 | if (ret < 0) { | ||
| 232 | dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", | ||
| 233 | irq, ret); | ||
| 234 | goto out_irq; | ||
| 235 | } | ||
| 236 | |||
| 237 | dev_set_drvdata(&pdev->dev, info); | ||
| 238 | info->rtc_dev = rtc_device_register("max8907c-rtc", &pdev->dev, | ||
| 239 | &max8907c_rtc_ops, THIS_MODULE); | ||
| 240 | ret = PTR_ERR(info->rtc_dev); | ||
| 241 | if (IS_ERR(info->rtc_dev)) { | ||
| 242 | dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret); | ||
| 243 | goto out_rtc; | ||
| 244 | } | ||
| 245 | |||
| 246 | max8907c_set_bits(chip->i2c_power, MAX8907C_REG_SYSENSEL, 0x2, 0x2); | ||
| 247 | |||
| 248 | platform_set_drvdata(pdev, info); | ||
| 249 | |||
| 250 | device_init_wakeup(&pdev->dev, 1); | ||
| 251 | |||
| 252 | return 0; | ||
| 253 | out_rtc: | ||
| 254 | free_irq(chip->irq_base + MAX8907C_IRQ_RTC_ALARM0, info); | ||
| 255 | |||
| 256 | out_irq: | ||
| 257 | kfree(info); | ||
| 258 | return ret; | ||
| 259 | } | ||
| 260 | |||
| 261 | static int __devexit max8907c_rtc_remove(struct platform_device *pdev) | ||
| 262 | { | ||
| 263 | struct max8907c_rtc_info *info = platform_get_drvdata(pdev); | ||
| 264 | |||
| 265 | if (info) { | ||
| 266 | free_irq(info->chip->irq_base + MAX8907C_IRQ_RTC_ALARM0, info); | ||
| 267 | |||
| 268 | rtc_device_unregister(info->rtc_dev); | ||
| 269 | kfree(info); | ||
| 270 | } | ||
| 271 | return 0; | ||
| 272 | } | ||
| 273 | |||
| 274 | #ifdef CONFIG_PM | ||
| 275 | static int max8907c_rtc_suspend(struct platform_device *pdev, pm_message_t state) | ||
| 276 | { | ||
| 277 | struct device *dev=&pdev->dev; | ||
| 278 | struct max8907c_rtc_info *info = platform_get_drvdata(pdev); | ||
| 279 | |||
| 280 | if (device_may_wakeup(dev)) | ||
| 281 | enable_irq_wake(info->chip->irq_base + MAX8907C_IRQ_RTC_ALARM0); | ||
| 282 | return 0; | ||
| 283 | } | ||
| 284 | |||
| 285 | static int max8907c_rtc_resume(struct platform_device *pdev) | ||
| 286 | { | ||
| 287 | struct device *dev=&pdev->dev; | ||
| 288 | struct max8907c_rtc_info *info = platform_get_drvdata(pdev); | ||
| 289 | |||
| 290 | if (device_may_wakeup(dev)) | ||
| 291 | disable_irq_wake(info->chip->irq_base + MAX8907C_IRQ_RTC_ALARM0); | ||
| 292 | return 0; | ||
| 293 | } | ||
| 294 | #endif | ||
| 295 | |||
| 296 | static struct platform_driver max8907c_rtc_driver = { | ||
| 297 | .driver = { | ||
| 298 | .name = "max8907c-rtc", | ||
| 299 | .owner = THIS_MODULE, | ||
| 300 | }, | ||
| 301 | .probe = max8907c_rtc_probe, | ||
| 302 | .remove = __devexit_p(max8907c_rtc_remove), | ||
| 303 | #ifdef CONFIG_PM | ||
| 304 | .suspend = max8907c_rtc_suspend, | ||
| 305 | .resume = max8907c_rtc_resume, | ||
| 306 | #endif | ||
| 307 | }; | ||
| 308 | |||
| 309 | static int __init max8907c_rtc_init(void) | ||
| 310 | { | ||
| 311 | return platform_driver_register(&max8907c_rtc_driver); | ||
| 312 | } | ||
| 313 | module_init(max8907c_rtc_init); | ||
| 314 | |||
| 315 | static void __exit max8907c_rtc_exit(void) | ||
| 316 | { | ||
| 317 | platform_driver_unregister(&max8907c_rtc_driver); | ||
| 318 | } | ||
| 319 | module_exit(max8907c_rtc_exit); | ||
| 320 | |||
| 321 | MODULE_DESCRIPTION("Maxim MAX8907C RTC driver"); | ||
| 322 | MODULE_LICENSE("GPL"); | ||
| 323 | |||
