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