Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Real time clock driver for DA9052 |
| 3 | * |
| 4 | * Copyright(c) 2012 Dialog Semiconductor Ltd. |
| 5 | * |
| 6 | * Author: Dajun Dajun Chen <dajun.chen@diasemi.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/rtc.h> |
Sachin Kamat | 11f54d0 | 2013-07-03 15:07:48 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <linux/mfd/da9052/da9052.h> |
| 21 | #include <linux/mfd/da9052/reg.h> |
| 22 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 23 | #define rtc_err(rtc, fmt, ...) \ |
| 24 | dev_err(rtc->da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__) |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 25 | |
| 26 | struct da9052_rtc { |
| 27 | struct rtc_device *rtc; |
| 28 | struct da9052 *da9052; |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 31 | static int da9052_rtc_enable_alarm(struct da9052_rtc *rtc, bool enable) |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 32 | { |
| 33 | int ret; |
| 34 | if (enable) { |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 35 | ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG, |
| 36 | DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON, |
| 37 | DA9052_ALARM_Y_ALARM_ON); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 38 | if (ret != 0) |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 39 | rtc_err(rtc, "Failed to enable ALM: %d\n", ret); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 40 | } else { |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 41 | ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG, |
| 42 | DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON, 0); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 43 | if (ret != 0) |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 44 | rtc_err(rtc, "Write error: %d\n", ret); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 45 | } |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | static irqreturn_t da9052_rtc_irq(int irq, void *data) |
| 50 | { |
| 51 | struct da9052_rtc *rtc = data; |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 52 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 53 | rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 54 | |
| 55 | return IRQ_HANDLED; |
| 56 | } |
| 57 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 58 | static int da9052_read_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm) |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 59 | { |
| 60 | int ret; |
| 61 | uint8_t v[5]; |
| 62 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 63 | ret = da9052_group_read(rtc->da9052, DA9052_ALARM_MI_REG, 5, v); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 64 | if (ret != 0) { |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 65 | rtc_err(rtc, "Failed to group read ALM: %d\n", ret); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | rtc_tm->tm_year = (v[4] & DA9052_RTC_YEAR) + 100; |
| 70 | rtc_tm->tm_mon = (v[3] & DA9052_RTC_MONTH) - 1; |
| 71 | rtc_tm->tm_mday = v[2] & DA9052_RTC_DAY; |
| 72 | rtc_tm->tm_hour = v[1] & DA9052_RTC_HOUR; |
| 73 | rtc_tm->tm_min = v[0] & DA9052_RTC_MIN; |
| 74 | |
| 75 | ret = rtc_valid_tm(rtc_tm); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 76 | return ret; |
| 77 | } |
| 78 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 79 | static int da9052_set_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm) |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 80 | { |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 81 | struct da9052 *da9052 = rtc->da9052; |
| 82 | unsigned long alm_time; |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 83 | int ret; |
| 84 | uint8_t v[3]; |
| 85 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 86 | ret = rtc_tm_to_time(rtc_tm, &alm_time); |
| 87 | if (ret != 0) |
| 88 | return ret; |
| 89 | |
| 90 | if (rtc_tm->tm_sec > 0) { |
| 91 | alm_time += 60 - rtc_tm->tm_sec; |
| 92 | rtc_time_to_tm(alm_time, rtc_tm); |
| 93 | } |
| 94 | BUG_ON(rtc_tm->tm_sec); /* it will cause repeated irqs if not zero */ |
| 95 | |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 96 | rtc_tm->tm_year -= 100; |
| 97 | rtc_tm->tm_mon += 1; |
| 98 | |
| 99 | ret = da9052_reg_update(da9052, DA9052_ALARM_MI_REG, |
| 100 | DA9052_RTC_MIN, rtc_tm->tm_min); |
| 101 | if (ret != 0) { |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 102 | rtc_err(rtc, "Failed to write ALRM MIN: %d\n", ret); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | v[0] = rtc_tm->tm_hour; |
| 107 | v[1] = rtc_tm->tm_mday; |
| 108 | v[2] = rtc_tm->tm_mon; |
| 109 | |
| 110 | ret = da9052_group_write(da9052, DA9052_ALARM_H_REG, 3, v); |
| 111 | if (ret < 0) |
| 112 | return ret; |
| 113 | |
| 114 | ret = da9052_reg_update(da9052, DA9052_ALARM_Y_REG, |
| 115 | DA9052_RTC_YEAR, rtc_tm->tm_year); |
| 116 | if (ret != 0) |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 117 | rtc_err(rtc, "Failed to write ALRM YEAR: %d\n", ret); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 122 | static int da9052_rtc_get_alarm_status(struct da9052_rtc *rtc) |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 123 | { |
| 124 | int ret; |
| 125 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 126 | ret = da9052_reg_read(rtc->da9052, DA9052_ALARM_Y_REG); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 127 | if (ret < 0) { |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 128 | rtc_err(rtc, "Failed to read ALM: %d\n", ret); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 129 | return ret; |
| 130 | } |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 131 | |
| 132 | return !!(ret&DA9052_ALARM_Y_ALARM_ON); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static int da9052_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm) |
| 136 | { |
| 137 | struct da9052_rtc *rtc = dev_get_drvdata(dev); |
| 138 | uint8_t v[6]; |
| 139 | int ret; |
| 140 | |
| 141 | ret = da9052_group_read(rtc->da9052, DA9052_COUNT_S_REG, 6, v); |
| 142 | if (ret < 0) { |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 143 | rtc_err(rtc, "Failed to read RTC time : %d\n", ret); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | rtc_tm->tm_year = (v[5] & DA9052_RTC_YEAR) + 100; |
| 148 | rtc_tm->tm_mon = (v[4] & DA9052_RTC_MONTH) - 1; |
| 149 | rtc_tm->tm_mday = v[3] & DA9052_RTC_DAY; |
| 150 | rtc_tm->tm_hour = v[2] & DA9052_RTC_HOUR; |
| 151 | rtc_tm->tm_min = v[1] & DA9052_RTC_MIN; |
| 152 | rtc_tm->tm_sec = v[0] & DA9052_RTC_SEC; |
| 153 | |
| 154 | ret = rtc_valid_tm(rtc_tm); |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 155 | return ret; |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int da9052_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 159 | { |
| 160 | struct da9052_rtc *rtc; |
| 161 | uint8_t v[6]; |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 162 | int ret; |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 163 | |
| 164 | rtc = dev_get_drvdata(dev); |
| 165 | |
| 166 | v[0] = tm->tm_sec; |
| 167 | v[1] = tm->tm_min; |
| 168 | v[2] = tm->tm_hour; |
| 169 | v[3] = tm->tm_mday; |
| 170 | v[4] = tm->tm_mon + 1; |
| 171 | v[5] = tm->tm_year - 100; |
| 172 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 173 | ret = da9052_group_write(rtc->da9052, DA9052_COUNT_S_REG, 6, v); |
| 174 | if (ret < 0) |
| 175 | rtc_err(rtc, "failed to set RTC time: %d\n", ret); |
| 176 | return ret; |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static int da9052_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 180 | { |
| 181 | int ret; |
| 182 | struct rtc_time *tm = &alrm->time; |
| 183 | struct da9052_rtc *rtc = dev_get_drvdata(dev); |
| 184 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 185 | ret = da9052_read_alarm(rtc, tm); |
| 186 | if (ret < 0) { |
| 187 | rtc_err(rtc, "failed to read RTC alarm: %d\n", ret); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 188 | return ret; |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 189 | } |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 190 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 191 | alrm->enabled = da9052_rtc_get_alarm_status(rtc); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int da9052_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 196 | { |
| 197 | int ret; |
| 198 | struct rtc_time *tm = &alrm->time; |
| 199 | struct da9052_rtc *rtc = dev_get_drvdata(dev); |
| 200 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 201 | ret = da9052_rtc_enable_alarm(rtc, 0); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 202 | if (ret < 0) |
| 203 | return ret; |
| 204 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 205 | ret = da9052_set_alarm(rtc, tm); |
| 206 | if (ret < 0) |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 207 | return ret; |
| 208 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 209 | ret = da9052_rtc_enable_alarm(rtc, 1); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | static int da9052_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 214 | { |
| 215 | struct da9052_rtc *rtc = dev_get_drvdata(dev); |
| 216 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 217 | return da9052_rtc_enable_alarm(rtc, enabled); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static const struct rtc_class_ops da9052_rtc_ops = { |
| 221 | .read_time = da9052_rtc_read_time, |
| 222 | .set_time = da9052_rtc_set_time, |
| 223 | .read_alarm = da9052_rtc_read_alarm, |
| 224 | .set_alarm = da9052_rtc_set_alarm, |
| 225 | .alarm_irq_enable = da9052_rtc_alarm_irq_enable, |
| 226 | }; |
| 227 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 228 | static int da9052_rtc_probe(struct platform_device *pdev) |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 229 | { |
| 230 | struct da9052_rtc *rtc; |
| 231 | int ret; |
| 232 | |
| 233 | rtc = devm_kzalloc(&pdev->dev, sizeof(struct da9052_rtc), GFP_KERNEL); |
| 234 | if (!rtc) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | rtc->da9052 = dev_get_drvdata(pdev->dev.parent); |
| 238 | platform_set_drvdata(pdev, rtc); |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 239 | |
| 240 | ret = da9052_reg_write(rtc->da9052, DA9052_BBAT_CONT_REG, 0xFE); |
| 241 | if (ret < 0) { |
| 242 | rtc_err(rtc, |
| 243 | "Failed to setup RTC battery charging: %d\n", ret); |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG, |
| 248 | DA9052_ALARM_Y_TICK_ON, 0); |
| 249 | if (ret != 0) |
| 250 | rtc_err(rtc, "Failed to disable TICKS: %d\n", ret); |
| 251 | |
Anthony Olech | c2c0eed | 2014-04-03 14:49:52 -0700 | [diff] [blame] | 252 | ret = da9052_request_irq(rtc->da9052, DA9052_IRQ_ALARM, "ALM", |
Ashish Jangam | 925e8ea | 2013-03-22 15:04:44 -0700 | [diff] [blame] | 253 | da9052_rtc_irq, rtc); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 254 | if (ret != 0) { |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 255 | rtc_err(rtc, "irq registration failed: %d\n", ret); |
Devendra Naga | 007def0 | 2012-07-30 14:41:54 -0700 | [diff] [blame] | 256 | return ret; |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Jingoo Han | 3689cd7 | 2013-04-29 16:18:57 -0700 | [diff] [blame] | 259 | rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name, |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 260 | &da9052_rtc_ops, THIS_MODULE); |
Sachin Kamat | dac30a9 | 2013-07-15 15:43:32 +0530 | [diff] [blame] | 261 | return PTR_ERR_OR_ZERO(rtc->rtc); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 264 | static struct platform_driver da9052_rtc_driver = { |
| 265 | .probe = da9052_rtc_probe, |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 266 | .driver = { |
| 267 | .name = "da9052-rtc", |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 268 | }, |
| 269 | }; |
| 270 | |
| 271 | module_platform_driver(da9052_rtc_driver); |
| 272 | |
Anthony Olech | 7c994c0 | 2014-06-06 14:35:54 -0700 | [diff] [blame] | 273 | MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>"); |
Ashish Jangam | fef931f | 2012-03-23 15:02:36 -0700 | [diff] [blame] | 274 | MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC"); |
| 275 | MODULE_LICENSE("GPL"); |
| 276 | MODULE_ALIAS("platform:da9052-rtc"); |