Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 1 | /* rtc-da9063.c - Real time clock device driver for DA9063 |
Steve Twiss | ce006ca | 2015-10-02 11:21:11 +0100 | [diff] [blame] | 2 | * Copyright (C) 2013-2015 Dialog Semiconductor Ltd. |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 3 | * |
Steve Twiss | ce006ca | 2015-10-02 11:21:11 +0100 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 8 | * |
Steve Twiss | ce006ca | 2015-10-02 11:21:11 +0100 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Steve Twiss | ce006ca | 2015-10-02 11:21:11 +0100 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 15 | #include <linux/delay.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/interrupt.h> |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 20 | #include <linux/of.h> |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 22 | #include <linux/regmap.h> |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 23 | #include <linux/rtc.h> |
| 24 | #include <linux/slab.h> |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 25 | |
| 26 | #include <linux/mfd/da9062/registers.h> |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 27 | #include <linux/mfd/da9063/registers.h> |
| 28 | #include <linux/mfd/da9063/core.h> |
| 29 | |
| 30 | #define YEARS_TO_DA9063(year) ((year) - 100) |
| 31 | #define MONTHS_TO_DA9063(month) ((month) + 1) |
| 32 | #define YEARS_FROM_DA9063(year) ((year) + 100) |
| 33 | #define MONTHS_FROM_DA9063(month) ((month) - 1) |
| 34 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 35 | enum { |
| 36 | RTC_SEC = 0, |
| 37 | RTC_MIN = 1, |
| 38 | RTC_HOUR = 2, |
| 39 | RTC_DAY = 3, |
| 40 | RTC_MONTH = 4, |
| 41 | RTC_YEAR = 5, |
| 42 | RTC_DATA_LEN |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 45 | struct da9063_compatible_rtc_regmap { |
| 46 | /* REGS */ |
| 47 | int rtc_enable_reg; |
| 48 | int rtc_enable_32k_crystal_reg; |
| 49 | int rtc_alarm_secs_reg; |
| 50 | int rtc_alarm_year_reg; |
| 51 | int rtc_count_secs_reg; |
| 52 | int rtc_count_year_reg; |
| 53 | int rtc_event_reg; |
| 54 | /* MASKS */ |
| 55 | int rtc_enable_mask; |
| 56 | int rtc_crystal_mask; |
| 57 | int rtc_event_alarm_mask; |
| 58 | int rtc_alarm_on_mask; |
| 59 | int rtc_alarm_status_mask; |
| 60 | int rtc_tick_on_mask; |
| 61 | int rtc_ready_to_read_mask; |
| 62 | int rtc_count_sec_mask; |
| 63 | int rtc_count_min_mask; |
| 64 | int rtc_count_hour_mask; |
| 65 | int rtc_count_day_mask; |
| 66 | int rtc_count_month_mask; |
| 67 | int rtc_count_year_mask; |
| 68 | /* ALARM CONFIG */ |
| 69 | int rtc_data_start; |
| 70 | int rtc_alarm_len; |
| 71 | }; |
| 72 | |
| 73 | struct da9063_compatible_rtc { |
| 74 | struct rtc_device *rtc_dev; |
| 75 | struct rtc_time alarm_time; |
| 76 | struct regmap *regmap; |
| 77 | const struct da9063_compatible_rtc_regmap *config; |
| 78 | bool rtc_sync; |
| 79 | }; |
| 80 | |
| 81 | static const struct da9063_compatible_rtc_regmap da9063_ad_regs = { |
| 82 | /* REGS */ |
| 83 | .rtc_enable_reg = DA9063_REG_CONTROL_E, |
| 84 | .rtc_alarm_secs_reg = DA9063_AD_REG_ALARM_MI, |
| 85 | .rtc_alarm_year_reg = DA9063_AD_REG_ALARM_Y, |
| 86 | .rtc_count_secs_reg = DA9063_REG_COUNT_S, |
| 87 | .rtc_count_year_reg = DA9063_REG_COUNT_Y, |
| 88 | .rtc_event_reg = DA9063_REG_EVENT_A, |
| 89 | /* MASKS */ |
| 90 | .rtc_enable_mask = DA9063_RTC_EN, |
| 91 | .rtc_crystal_mask = DA9063_CRYSTAL, |
| 92 | .rtc_enable_32k_crystal_reg = DA9063_REG_EN_32K, |
| 93 | .rtc_event_alarm_mask = DA9063_E_ALARM, |
| 94 | .rtc_alarm_on_mask = DA9063_ALARM_ON, |
| 95 | .rtc_alarm_status_mask = DA9063_ALARM_STATUS_ALARM | |
| 96 | DA9063_ALARM_STATUS_TICK, |
| 97 | .rtc_tick_on_mask = DA9063_TICK_ON, |
| 98 | .rtc_ready_to_read_mask = DA9063_RTC_READ, |
| 99 | .rtc_count_sec_mask = DA9063_COUNT_SEC_MASK, |
| 100 | .rtc_count_min_mask = DA9063_COUNT_MIN_MASK, |
| 101 | .rtc_count_hour_mask = DA9063_COUNT_HOUR_MASK, |
| 102 | .rtc_count_day_mask = DA9063_COUNT_DAY_MASK, |
| 103 | .rtc_count_month_mask = DA9063_COUNT_MONTH_MASK, |
| 104 | .rtc_count_year_mask = DA9063_COUNT_YEAR_MASK, |
| 105 | /* ALARM CONFIG */ |
| 106 | .rtc_data_start = RTC_MIN, |
| 107 | .rtc_alarm_len = RTC_DATA_LEN - 1, |
| 108 | }; |
| 109 | |
| 110 | static const struct da9063_compatible_rtc_regmap da9063_bb_regs = { |
| 111 | /* REGS */ |
| 112 | .rtc_enable_reg = DA9063_REG_CONTROL_E, |
| 113 | .rtc_alarm_secs_reg = DA9063_BB_REG_ALARM_S, |
| 114 | .rtc_alarm_year_reg = DA9063_BB_REG_ALARM_Y, |
| 115 | .rtc_count_secs_reg = DA9063_REG_COUNT_S, |
| 116 | .rtc_count_year_reg = DA9063_REG_COUNT_Y, |
| 117 | .rtc_event_reg = DA9063_REG_EVENT_A, |
| 118 | /* MASKS */ |
| 119 | .rtc_enable_mask = DA9063_RTC_EN, |
| 120 | .rtc_crystal_mask = DA9063_CRYSTAL, |
| 121 | .rtc_enable_32k_crystal_reg = DA9063_REG_EN_32K, |
| 122 | .rtc_event_alarm_mask = DA9063_E_ALARM, |
| 123 | .rtc_alarm_on_mask = DA9063_ALARM_ON, |
| 124 | .rtc_alarm_status_mask = DA9063_ALARM_STATUS_ALARM | |
| 125 | DA9063_ALARM_STATUS_TICK, |
| 126 | .rtc_tick_on_mask = DA9063_TICK_ON, |
| 127 | .rtc_ready_to_read_mask = DA9063_RTC_READ, |
| 128 | .rtc_count_sec_mask = DA9063_COUNT_SEC_MASK, |
| 129 | .rtc_count_min_mask = DA9063_COUNT_MIN_MASK, |
| 130 | .rtc_count_hour_mask = DA9063_COUNT_HOUR_MASK, |
| 131 | .rtc_count_day_mask = DA9063_COUNT_DAY_MASK, |
| 132 | .rtc_count_month_mask = DA9063_COUNT_MONTH_MASK, |
| 133 | .rtc_count_year_mask = DA9063_COUNT_YEAR_MASK, |
| 134 | /* ALARM CONFIG */ |
| 135 | .rtc_data_start = RTC_SEC, |
| 136 | .rtc_alarm_len = RTC_DATA_LEN, |
| 137 | }; |
| 138 | |
| 139 | static const struct da9063_compatible_rtc_regmap da9062_aa_regs = { |
| 140 | /* REGS */ |
| 141 | .rtc_enable_reg = DA9062AA_CONTROL_E, |
| 142 | .rtc_alarm_secs_reg = DA9062AA_ALARM_S, |
| 143 | .rtc_alarm_year_reg = DA9062AA_ALARM_Y, |
| 144 | .rtc_count_secs_reg = DA9062AA_COUNT_S, |
| 145 | .rtc_count_year_reg = DA9062AA_COUNT_Y, |
| 146 | .rtc_event_reg = DA9062AA_EVENT_A, |
| 147 | /* MASKS */ |
| 148 | .rtc_enable_mask = DA9062AA_RTC_EN_MASK, |
| 149 | .rtc_crystal_mask = DA9062AA_CRYSTAL_MASK, |
| 150 | .rtc_enable_32k_crystal_reg = DA9062AA_EN_32K, |
| 151 | .rtc_event_alarm_mask = DA9062AA_M_ALARM_MASK, |
| 152 | .rtc_alarm_on_mask = DA9062AA_ALARM_ON_MASK, |
| 153 | .rtc_alarm_status_mask = (0x02 << 6), |
| 154 | .rtc_tick_on_mask = DA9062AA_TICK_ON_MASK, |
| 155 | .rtc_ready_to_read_mask = DA9062AA_RTC_READ_MASK, |
| 156 | .rtc_count_sec_mask = DA9062AA_COUNT_SEC_MASK, |
| 157 | .rtc_count_min_mask = DA9062AA_COUNT_MIN_MASK, |
| 158 | .rtc_count_hour_mask = DA9062AA_COUNT_HOUR_MASK, |
| 159 | .rtc_count_day_mask = DA9062AA_COUNT_DAY_MASK, |
| 160 | .rtc_count_month_mask = DA9062AA_COUNT_MONTH_MASK, |
| 161 | .rtc_count_year_mask = DA9062AA_COUNT_YEAR_MASK, |
| 162 | /* ALARM CONFIG */ |
| 163 | .rtc_data_start = RTC_SEC, |
| 164 | .rtc_alarm_len = RTC_DATA_LEN, |
| 165 | }; |
| 166 | |
| 167 | static const struct of_device_id da9063_compatible_reg_id_table[] = { |
| 168 | { .compatible = "dlg,da9063-rtc", .data = &da9063_bb_regs }, |
| 169 | { .compatible = "dlg,da9062-rtc", .data = &da9062_aa_regs }, |
| 170 | { }, |
| 171 | }; |
Javier Martinez Canillas | 73798d5 | 2015-08-27 13:52:02 +0200 | [diff] [blame] | 172 | MODULE_DEVICE_TABLE(of, da9063_compatible_reg_id_table); |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 173 | |
| 174 | static void da9063_data_to_tm(u8 *data, struct rtc_time *tm, |
| 175 | struct da9063_compatible_rtc *rtc) |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 176 | { |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 177 | const struct da9063_compatible_rtc_regmap *config = rtc->config; |
| 178 | |
| 179 | tm->tm_sec = data[RTC_SEC] & config->rtc_count_sec_mask; |
| 180 | tm->tm_min = data[RTC_MIN] & config->rtc_count_min_mask; |
| 181 | tm->tm_hour = data[RTC_HOUR] & config->rtc_count_hour_mask; |
| 182 | tm->tm_mday = data[RTC_DAY] & config->rtc_count_day_mask; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 183 | tm->tm_mon = MONTHS_FROM_DA9063(data[RTC_MONTH] & |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 184 | config->rtc_count_month_mask); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 185 | tm->tm_year = YEARS_FROM_DA9063(data[RTC_YEAR] & |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 186 | config->rtc_count_year_mask); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 187 | } |
| 188 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 189 | static void da9063_tm_to_data(struct rtc_time *tm, u8 *data, |
| 190 | struct da9063_compatible_rtc *rtc) |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 191 | { |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 192 | const struct da9063_compatible_rtc_regmap *config = rtc->config; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 193 | |
Enrico Scholz | 2ad2c17 | 2015-11-27 13:02:55 +0100 | [diff] [blame] | 194 | data[RTC_SEC] = tm->tm_sec & config->rtc_count_sec_mask; |
| 195 | data[RTC_MIN] = tm->tm_min & config->rtc_count_min_mask; |
| 196 | data[RTC_HOUR] = tm->tm_hour & config->rtc_count_hour_mask; |
| 197 | data[RTC_DAY] = tm->tm_mday & config->rtc_count_day_mask; |
| 198 | data[RTC_MONTH] = MONTHS_TO_DA9063(tm->tm_mon) & |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 199 | config->rtc_count_month_mask; |
Enrico Scholz | 2ad2c17 | 2015-11-27 13:02:55 +0100 | [diff] [blame] | 200 | data[RTC_YEAR] = YEARS_TO_DA9063(tm->tm_year) & |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 201 | config->rtc_count_year_mask; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static int da9063_rtc_stop_alarm(struct device *dev) |
| 205 | { |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 206 | struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev); |
| 207 | const struct da9063_compatible_rtc_regmap *config = rtc->config; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 208 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 209 | return regmap_update_bits(rtc->regmap, |
| 210 | config->rtc_alarm_year_reg, |
| 211 | config->rtc_alarm_on_mask, |
| 212 | 0); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static int da9063_rtc_start_alarm(struct device *dev) |
| 216 | { |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 217 | struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev); |
| 218 | const struct da9063_compatible_rtc_regmap *config = rtc->config; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 219 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 220 | return regmap_update_bits(rtc->regmap, |
| 221 | config->rtc_alarm_year_reg, |
| 222 | config->rtc_alarm_on_mask, |
| 223 | config->rtc_alarm_on_mask); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static int da9063_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 227 | { |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 228 | struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev); |
| 229 | const struct da9063_compatible_rtc_regmap *config = rtc->config; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 230 | unsigned long tm_secs; |
| 231 | unsigned long al_secs; |
| 232 | u8 data[RTC_DATA_LEN]; |
| 233 | int ret; |
| 234 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 235 | ret = regmap_bulk_read(rtc->regmap, |
| 236 | config->rtc_count_secs_reg, |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 237 | data, RTC_DATA_LEN); |
| 238 | if (ret < 0) { |
| 239 | dev_err(dev, "Failed to read RTC time data: %d\n", ret); |
| 240 | return ret; |
| 241 | } |
| 242 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 243 | if (!(data[RTC_SEC] & config->rtc_ready_to_read_mask)) { |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 244 | dev_dbg(dev, "RTC not yet ready to be read by the host\n"); |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 248 | da9063_data_to_tm(data, tm, rtc); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 249 | |
| 250 | rtc_tm_to_time(tm, &tm_secs); |
| 251 | rtc_tm_to_time(&rtc->alarm_time, &al_secs); |
| 252 | |
| 253 | /* handle the rtc synchronisation delay */ |
| 254 | if (rtc->rtc_sync == true && al_secs - tm_secs == 1) |
| 255 | memcpy(tm, &rtc->alarm_time, sizeof(struct rtc_time)); |
| 256 | else |
| 257 | rtc->rtc_sync = false; |
| 258 | |
| 259 | return rtc_valid_tm(tm); |
| 260 | } |
| 261 | |
| 262 | static int da9063_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 263 | { |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 264 | struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev); |
| 265 | const struct da9063_compatible_rtc_regmap *config = rtc->config; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 266 | u8 data[RTC_DATA_LEN]; |
| 267 | int ret; |
| 268 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 269 | da9063_tm_to_data(tm, data, rtc); |
| 270 | ret = regmap_bulk_write(rtc->regmap, |
| 271 | config->rtc_count_secs_reg, |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 272 | data, RTC_DATA_LEN); |
| 273 | if (ret < 0) |
| 274 | dev_err(dev, "Failed to set RTC time data: %d\n", ret); |
| 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | static int da9063_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 280 | { |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 281 | struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev); |
| 282 | const struct da9063_compatible_rtc_regmap *config = rtc->config; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 283 | u8 data[RTC_DATA_LEN]; |
| 284 | int ret; |
| 285 | unsigned int val; |
| 286 | |
Opensource [Steve Twiss] | 9cb42e2 | 2014-07-21 11:39:33 +0100 | [diff] [blame] | 287 | data[RTC_SEC] = 0; |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 288 | ret = regmap_bulk_read(rtc->regmap, |
| 289 | config->rtc_alarm_secs_reg, |
| 290 | &data[config->rtc_data_start], |
| 291 | config->rtc_alarm_len); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 292 | if (ret < 0) |
| 293 | return ret; |
| 294 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 295 | da9063_data_to_tm(data, &alrm->time, rtc); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 296 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 297 | alrm->enabled = !!(data[RTC_YEAR] & config->rtc_alarm_on_mask); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 298 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 299 | ret = regmap_read(rtc->regmap, |
| 300 | config->rtc_event_reg, |
| 301 | &val); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 302 | if (ret < 0) |
| 303 | return ret; |
| 304 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 305 | if (val & config->rtc_event_alarm_mask) |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 306 | alrm->pending = 1; |
| 307 | else |
| 308 | alrm->pending = 0; |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int da9063_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 314 | { |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 315 | struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev); |
| 316 | const struct da9063_compatible_rtc_regmap *config = rtc->config; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 317 | u8 data[RTC_DATA_LEN]; |
| 318 | int ret; |
| 319 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 320 | da9063_tm_to_data(&alrm->time, data, rtc); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 321 | |
| 322 | ret = da9063_rtc_stop_alarm(dev); |
| 323 | if (ret < 0) { |
| 324 | dev_err(dev, "Failed to stop alarm: %d\n", ret); |
| 325 | return ret; |
| 326 | } |
| 327 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 328 | ret = regmap_bulk_write(rtc->regmap, |
| 329 | config->rtc_alarm_secs_reg, |
| 330 | &data[config->rtc_data_start], |
| 331 | config->rtc_alarm_len); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 332 | if (ret < 0) { |
| 333 | dev_err(dev, "Failed to write alarm: %d\n", ret); |
| 334 | return ret; |
| 335 | } |
| 336 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 337 | da9063_data_to_tm(data, &rtc->alarm_time, rtc); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 338 | |
| 339 | if (alrm->enabled) { |
| 340 | ret = da9063_rtc_start_alarm(dev); |
| 341 | if (ret < 0) { |
| 342 | dev_err(dev, "Failed to start alarm: %d\n", ret); |
| 343 | return ret; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return ret; |
| 348 | } |
| 349 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 350 | static int da9063_rtc_alarm_irq_enable(struct device *dev, |
| 351 | unsigned int enabled) |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 352 | { |
| 353 | if (enabled) |
| 354 | return da9063_rtc_start_alarm(dev); |
| 355 | else |
| 356 | return da9063_rtc_stop_alarm(dev); |
| 357 | } |
| 358 | |
| 359 | static irqreturn_t da9063_alarm_event(int irq, void *data) |
| 360 | { |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 361 | struct da9063_compatible_rtc *rtc = data; |
| 362 | const struct da9063_compatible_rtc_regmap *config = rtc->config; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 363 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 364 | regmap_update_bits(rtc->regmap, |
| 365 | config->rtc_alarm_year_reg, |
| 366 | config->rtc_alarm_on_mask, |
| 367 | 0); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 368 | |
| 369 | rtc->rtc_sync = true; |
| 370 | rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF); |
| 371 | |
| 372 | return IRQ_HANDLED; |
| 373 | } |
| 374 | |
| 375 | static const struct rtc_class_ops da9063_rtc_ops = { |
| 376 | .read_time = da9063_rtc_read_time, |
| 377 | .set_time = da9063_rtc_set_time, |
| 378 | .read_alarm = da9063_rtc_read_alarm, |
| 379 | .set_alarm = da9063_rtc_set_alarm, |
| 380 | .alarm_irq_enable = da9063_rtc_alarm_irq_enable, |
| 381 | }; |
| 382 | |
| 383 | static int da9063_rtc_probe(struct platform_device *pdev) |
| 384 | { |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 385 | struct da9063_compatible_rtc *rtc; |
| 386 | const struct da9063_compatible_rtc_regmap *config; |
| 387 | const struct of_device_id *match; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 388 | int irq_alarm; |
| 389 | u8 data[RTC_DATA_LEN]; |
| 390 | int ret; |
| 391 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 392 | if (!pdev->dev.of_node) |
| 393 | return -ENXIO; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 394 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 395 | match = of_match_node(da9063_compatible_reg_id_table, |
| 396 | pdev->dev.of_node); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 397 | |
Opensource [Steve Twiss] | 9cb42e2 | 2014-07-21 11:39:33 +0100 | [diff] [blame] | 398 | rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); |
| 399 | if (!rtc) |
| 400 | return -ENOMEM; |
| 401 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 402 | rtc->config = match->data; |
| 403 | if (of_device_is_compatible(pdev->dev.of_node, "dlg,da9063-rtc")) { |
| 404 | struct da9063 *chip = dev_get_drvdata(pdev->dev.parent); |
| 405 | |
| 406 | if (chip->variant_code == PMIC_DA9063_AD) |
| 407 | rtc->config = &da9063_ad_regs; |
Opensource [Steve Twiss] | 9cb42e2 | 2014-07-21 11:39:33 +0100 | [diff] [blame] | 408 | } |
| 409 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 410 | rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL); |
| 411 | if (!rtc->regmap) { |
| 412 | dev_warn(&pdev->dev, "Parent regmap unavailable.\n"); |
| 413 | return -ENXIO; |
| 414 | } |
| 415 | |
| 416 | config = rtc->config; |
| 417 | ret = regmap_update_bits(rtc->regmap, |
| 418 | config->rtc_enable_reg, |
| 419 | config->rtc_enable_mask, |
| 420 | config->rtc_enable_mask); |
| 421 | if (ret < 0) { |
| 422 | dev_err(&pdev->dev, "Failed to enable RTC\n"); |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | ret = regmap_update_bits(rtc->regmap, |
| 427 | config->rtc_enable_32k_crystal_reg, |
| 428 | config->rtc_crystal_mask, |
| 429 | config->rtc_crystal_mask); |
| 430 | if (ret < 0) { |
| 431 | dev_err(&pdev->dev, "Failed to run 32kHz oscillator\n"); |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | ret = regmap_update_bits(rtc->regmap, |
| 436 | config->rtc_alarm_secs_reg, |
| 437 | config->rtc_alarm_status_mask, |
| 438 | 0); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 439 | if (ret < 0) { |
| 440 | dev_err(&pdev->dev, "Failed to access RTC alarm register\n"); |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 441 | return ret; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 442 | } |
| 443 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 444 | ret = regmap_update_bits(rtc->regmap, |
| 445 | config->rtc_alarm_secs_reg, |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 446 | DA9063_ALARM_STATUS_ALARM, |
| 447 | DA9063_ALARM_STATUS_ALARM); |
| 448 | if (ret < 0) { |
| 449 | dev_err(&pdev->dev, "Failed to access RTC alarm register\n"); |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 450 | return ret; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 451 | } |
| 452 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 453 | ret = regmap_update_bits(rtc->regmap, |
| 454 | config->rtc_alarm_year_reg, |
| 455 | config->rtc_tick_on_mask, |
| 456 | 0); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 457 | if (ret < 0) { |
| 458 | dev_err(&pdev->dev, "Failed to disable TICKs\n"); |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 459 | return ret; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Opensource [Steve Twiss] | 9cb42e2 | 2014-07-21 11:39:33 +0100 | [diff] [blame] | 462 | data[RTC_SEC] = 0; |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 463 | ret = regmap_bulk_read(rtc->regmap, |
| 464 | config->rtc_alarm_secs_reg, |
| 465 | &data[config->rtc_data_start], |
| 466 | config->rtc_alarm_len); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 467 | if (ret < 0) { |
| 468 | dev_err(&pdev->dev, "Failed to read initial alarm data: %d\n", |
| 469 | ret); |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 470 | return ret; |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 473 | platform_set_drvdata(pdev, rtc); |
| 474 | |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 475 | rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, DA9063_DRVNAME_RTC, |
| 476 | &da9063_rtc_ops, THIS_MODULE); |
| 477 | if (IS_ERR(rtc->rtc_dev)) |
| 478 | return PTR_ERR(rtc->rtc_dev); |
| 479 | |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 480 | da9063_data_to_tm(data, &rtc->alarm_time, rtc); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 481 | rtc->rtc_sync = false; |
Steve Twiss | 77535ac | 2015-12-08 16:28:39 +0000 | [diff] [blame] | 482 | |
| 483 | irq_alarm = platform_get_irq_byname(pdev, "ALARM"); |
| 484 | ret = devm_request_threaded_irq(&pdev->dev, irq_alarm, NULL, |
| 485 | da9063_alarm_event, |
| 486 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 487 | "ALARM", rtc); |
| 488 | if (ret) |
| 489 | dev_err(&pdev->dev, "Failed to request ALARM IRQ %d: %d\n", |
| 490 | irq_alarm, ret); |
| 491 | |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | static struct platform_driver da9063_rtc_driver = { |
| 496 | .probe = da9063_rtc_probe, |
| 497 | .driver = { |
| 498 | .name = DA9063_DRVNAME_RTC, |
S Twiss | 80ca327 | 2015-07-21 11:29:07 +0100 | [diff] [blame] | 499 | .of_match_table = da9063_compatible_reg_id_table, |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 500 | }, |
| 501 | }; |
| 502 | |
| 503 | module_platform_driver(da9063_rtc_driver); |
| 504 | |
| 505 | MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>"); |
| 506 | MODULE_DESCRIPTION("Real time clock device driver for Dialog DA9063"); |
Steve Twiss | ce006ca | 2015-10-02 11:21:11 +0100 | [diff] [blame] | 507 | MODULE_LICENSE("GPL"); |
Opensource [Steve Twiss] | c2a5755 | 2014-06-06 14:36:03 -0700 | [diff] [blame] | 508 | MODULE_ALIAS("platform:" DA9063_DRVNAME_RTC); |