Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 1 | /* |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 2 | * Copyright (c) 2013-2014 Samsung Electronics Co., Ltd |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 3 | * http://www.samsung.com |
| 4 | * |
| 5 | * Copyright (C) 2013 Google, Inc |
| 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 as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
Joe Perches | a737e83 | 2015-04-16 12:46:14 -0700 | [diff] [blame] | 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/i2c.h> |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 22 | #include <linux/bcd.h> |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 23 | #include <linux/regmap.h> |
| 24 | #include <linux/rtc.h> |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/mfd/samsung/core.h> |
| 27 | #include <linux/mfd/samsung/irq.h> |
| 28 | #include <linux/mfd/samsung/rtc.h> |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 29 | #include <linux/mfd/samsung/s2mps14.h> |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 30 | |
Krzysztof Kozlowski | d73238d | 2013-12-12 17:12:28 -0800 | [diff] [blame] | 31 | /* |
| 32 | * Maximum number of retries for checking changes in UDR field |
Krzysztof Kozlowski | 602cb5b | 2014-06-10 15:18:40 -0700 | [diff] [blame] | 33 | * of S5M_RTC_UDR_CON register (to limit possible endless loop). |
Krzysztof Kozlowski | d73238d | 2013-12-12 17:12:28 -0800 | [diff] [blame] | 34 | * |
| 35 | * After writing to RTC registers (setting time or alarm) read the UDR field |
Krzysztof Kozlowski | 602cb5b | 2014-06-10 15:18:40 -0700 | [diff] [blame] | 36 | * in S5M_RTC_UDR_CON register. UDR is auto-cleared when data have |
Krzysztof Kozlowski | d73238d | 2013-12-12 17:12:28 -0800 | [diff] [blame] | 37 | * been transferred. |
| 38 | */ |
| 39 | #define UDR_READ_RETRY_CNT 5 |
| 40 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 41 | /* Registers used by the driver which are different between chipsets. */ |
| 42 | struct s5m_rtc_reg_config { |
| 43 | /* Number of registers used for setting time/alarm0/alarm1 */ |
| 44 | unsigned int regs_count; |
| 45 | /* First register for time, seconds */ |
| 46 | unsigned int time; |
| 47 | /* RTC control register */ |
| 48 | unsigned int ctrl; |
| 49 | /* First register for alarm 0, seconds */ |
| 50 | unsigned int alarm0; |
| 51 | /* First register for alarm 1, seconds */ |
| 52 | unsigned int alarm1; |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 53 | /* |
| 54 | * Register for update flag (UDR). Typically setting UDR field to 1 |
| 55 | * will enable update of time or alarm register. Then it will be |
| 56 | * auto-cleared after successful update. |
| 57 | */ |
| 58 | unsigned int rtc_udr_update; |
| 59 | /* Mask for UDR field in 'rtc_udr_update' register */ |
| 60 | unsigned int rtc_udr_mask; |
| 61 | }; |
| 62 | |
| 63 | /* Register map for S5M8763 and S5M8767 */ |
| 64 | static const struct s5m_rtc_reg_config s5m_rtc_regs = { |
| 65 | .regs_count = 8, |
| 66 | .time = S5M_RTC_SEC, |
| 67 | .ctrl = S5M_ALARM1_CONF, |
| 68 | .alarm0 = S5M_ALARM0_SEC, |
| 69 | .alarm1 = S5M_ALARM1_SEC, |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 70 | .rtc_udr_update = S5M_RTC_UDR_CON, |
| 71 | .rtc_udr_mask = S5M_RTC_UDR_MASK, |
| 72 | }; |
| 73 | |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 74 | /* |
| 75 | * Register map for S2MPS14. |
| 76 | * It may be also suitable for S2MPS11 but this was not tested. |
| 77 | */ |
| 78 | static const struct s5m_rtc_reg_config s2mps_rtc_regs = { |
| 79 | .regs_count = 7, |
| 80 | .time = S2MPS_RTC_SEC, |
| 81 | .ctrl = S2MPS_RTC_CTRL, |
| 82 | .alarm0 = S2MPS_ALARM0_SEC, |
| 83 | .alarm1 = S2MPS_ALARM1_SEC, |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 84 | .rtc_udr_update = S2MPS_RTC_UDR_CON, |
| 85 | .rtc_udr_mask = S2MPS_RTC_WUDR_MASK, |
| 86 | }; |
| 87 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 88 | struct s5m_rtc_info { |
| 89 | struct device *dev; |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 90 | struct i2c_client *i2c; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 91 | struct sec_pmic_dev *s5m87xx; |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 92 | struct regmap *regmap; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 93 | struct rtc_device *rtc_dev; |
| 94 | int irq; |
Krzysztof Kozlowski | 94f9192 | 2015-04-16 12:49:27 -0700 | [diff] [blame] | 95 | enum sec_device_type device_type; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 96 | int rtc_24hr_mode; |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 97 | const struct s5m_rtc_reg_config *regs; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 100 | static const struct regmap_config s5m_rtc_regmap_config = { |
| 101 | .reg_bits = 8, |
| 102 | .val_bits = 8, |
| 103 | |
Krzysztof Kozlowski | 602cb5b | 2014-06-10 15:18:40 -0700 | [diff] [blame] | 104 | .max_register = S5M_RTC_REG_MAX, |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | static const struct regmap_config s2mps14_rtc_regmap_config = { |
| 108 | .reg_bits = 8, |
| 109 | .val_bits = 8, |
| 110 | |
| 111 | .max_register = S2MPS_RTC_REG_MAX, |
| 112 | }; |
| 113 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 114 | static void s5m8767_data_to_tm(u8 *data, struct rtc_time *tm, |
| 115 | int rtc_24hr_mode) |
| 116 | { |
| 117 | tm->tm_sec = data[RTC_SEC] & 0x7f; |
| 118 | tm->tm_min = data[RTC_MIN] & 0x7f; |
| 119 | if (rtc_24hr_mode) { |
| 120 | tm->tm_hour = data[RTC_HOUR] & 0x1f; |
| 121 | } else { |
| 122 | tm->tm_hour = data[RTC_HOUR] & 0x0f; |
| 123 | if (data[RTC_HOUR] & HOUR_PM_MASK) |
| 124 | tm->tm_hour += 12; |
| 125 | } |
| 126 | |
| 127 | tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f); |
| 128 | tm->tm_mday = data[RTC_DATE] & 0x1f; |
| 129 | tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1; |
| 130 | tm->tm_year = (data[RTC_YEAR1] & 0x7f) + 100; |
| 131 | tm->tm_yday = 0; |
| 132 | tm->tm_isdst = 0; |
| 133 | } |
| 134 | |
| 135 | static int s5m8767_tm_to_data(struct rtc_time *tm, u8 *data) |
| 136 | { |
| 137 | data[RTC_SEC] = tm->tm_sec; |
| 138 | data[RTC_MIN] = tm->tm_min; |
| 139 | |
| 140 | if (tm->tm_hour >= 12) |
| 141 | data[RTC_HOUR] = tm->tm_hour | HOUR_PM_MASK; |
| 142 | else |
| 143 | data[RTC_HOUR] = tm->tm_hour & ~HOUR_PM_MASK; |
| 144 | |
| 145 | data[RTC_WEEKDAY] = 1 << tm->tm_wday; |
| 146 | data[RTC_DATE] = tm->tm_mday; |
| 147 | data[RTC_MONTH] = tm->tm_mon + 1; |
| 148 | data[RTC_YEAR1] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0; |
| 149 | |
| 150 | if (tm->tm_year < 100) { |
Joe Perches | a737e83 | 2015-04-16 12:46:14 -0700 | [diff] [blame] | 151 | pr_err("RTC cannot handle the year %d\n", |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 152 | 1900 + tm->tm_year); |
| 153 | return -EINVAL; |
| 154 | } else { |
| 155 | return 0; |
| 156 | } |
| 157 | } |
| 158 | |
Krzysztof Kozlowski | d73238d | 2013-12-12 17:12:28 -0800 | [diff] [blame] | 159 | /* |
| 160 | * Read RTC_UDR_CON register and wait till UDR field is cleared. |
| 161 | * This indicates that time/alarm update ended. |
| 162 | */ |
| 163 | static inline int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info) |
| 164 | { |
| 165 | int ret, retry = UDR_READ_RETRY_CNT; |
| 166 | unsigned int data; |
| 167 | |
| 168 | do { |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 169 | ret = regmap_read(info->regmap, info->regs->rtc_udr_update, |
| 170 | &data); |
| 171 | } while (--retry && (data & info->regs->rtc_udr_mask) && !ret); |
Krzysztof Kozlowski | d73238d | 2013-12-12 17:12:28 -0800 | [diff] [blame] | 172 | |
| 173 | if (!retry) |
| 174 | dev_err(info->dev, "waiting for UDR update, reached max number of retries\n"); |
| 175 | |
| 176 | return ret; |
| 177 | } |
| 178 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 179 | static inline int s5m_check_peding_alarm_interrupt(struct s5m_rtc_info *info, |
| 180 | struct rtc_wkalrm *alarm) |
| 181 | { |
| 182 | int ret; |
| 183 | unsigned int val; |
| 184 | |
| 185 | switch (info->device_type) { |
| 186 | case S5M8767X: |
| 187 | case S5M8763X: |
| 188 | ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val); |
| 189 | val &= S5M_ALARM0_STATUS; |
| 190 | break; |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 191 | case S2MPS14X: |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 192 | case S2MPS13X: |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 193 | ret = regmap_read(info->s5m87xx->regmap_pmic, S2MPS14_REG_ST2, |
| 194 | &val); |
| 195 | val &= S2MPS_ALARM0_STATUS; |
| 196 | break; |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 197 | default: |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | if (ret < 0) |
| 201 | return ret; |
| 202 | |
| 203 | if (val) |
| 204 | alarm->pending = 1; |
| 205 | else |
| 206 | alarm->pending = 0; |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 211 | static inline int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info) |
| 212 | { |
| 213 | int ret; |
| 214 | unsigned int data; |
| 215 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 216 | ret = regmap_read(info->regmap, info->regs->rtc_udr_update, &data); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 217 | if (ret < 0) { |
| 218 | dev_err(info->dev, "failed to read update reg(%d)\n", ret); |
| 219 | return ret; |
| 220 | } |
| 221 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 222 | data |= info->regs->rtc_udr_mask; |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 223 | if (info->device_type == S5M8763X || info->device_type == S5M8767X) |
| 224 | data |= S5M_RTC_TIME_EN_MASK; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 225 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 226 | ret = regmap_write(info->regmap, info->regs->rtc_udr_update, data); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 227 | if (ret < 0) { |
| 228 | dev_err(info->dev, "failed to write update reg(%d)\n", ret); |
| 229 | return ret; |
| 230 | } |
| 231 | |
Krzysztof Kozlowski | d73238d | 2013-12-12 17:12:28 -0800 | [diff] [blame] | 232 | ret = s5m8767_wait_for_udr_update(info); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 233 | |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | static inline int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info) |
| 238 | { |
| 239 | int ret; |
| 240 | unsigned int data; |
| 241 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 242 | ret = regmap_read(info->regmap, info->regs->rtc_udr_update, &data); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 243 | if (ret < 0) { |
| 244 | dev_err(info->dev, "%s: fail to read update reg(%d)\n", |
| 245 | __func__, ret); |
| 246 | return ret; |
| 247 | } |
| 248 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 249 | data |= info->regs->rtc_udr_mask; |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 250 | switch (info->device_type) { |
| 251 | case S5M8763X: |
| 252 | case S5M8767X: |
| 253 | data &= ~S5M_RTC_TIME_EN_MASK; |
| 254 | break; |
| 255 | case S2MPS14X: |
| 256 | data |= S2MPS_RTC_RUDR_MASK; |
| 257 | break; |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 258 | case S2MPS13X: |
| 259 | data |= S2MPS13_RTC_AUDR_MASK; |
| 260 | break; |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 261 | default: |
| 262 | return -EINVAL; |
| 263 | } |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 264 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 265 | ret = regmap_write(info->regmap, info->regs->rtc_udr_update, data); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 266 | if (ret < 0) { |
| 267 | dev_err(info->dev, "%s: fail to write update reg(%d)\n", |
| 268 | __func__, ret); |
| 269 | return ret; |
| 270 | } |
| 271 | |
Krzysztof Kozlowski | d73238d | 2013-12-12 17:12:28 -0800 | [diff] [blame] | 272 | ret = s5m8767_wait_for_udr_update(info); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 273 | |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 274 | /* On S2MPS13 the AUDR is not auto-cleared */ |
| 275 | if (info->device_type == S2MPS13X) |
| 276 | regmap_update_bits(info->regmap, info->regs->rtc_udr_update, |
| 277 | S2MPS13_RTC_AUDR_MASK, 0); |
| 278 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | static void s5m8763_data_to_tm(u8 *data, struct rtc_time *tm) |
| 283 | { |
| 284 | tm->tm_sec = bcd2bin(data[RTC_SEC]); |
| 285 | tm->tm_min = bcd2bin(data[RTC_MIN]); |
| 286 | |
| 287 | if (data[RTC_HOUR] & HOUR_12) { |
| 288 | tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f); |
| 289 | if (data[RTC_HOUR] & HOUR_PM) |
| 290 | tm->tm_hour += 12; |
| 291 | } else { |
| 292 | tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f); |
| 293 | } |
| 294 | |
| 295 | tm->tm_wday = data[RTC_WEEKDAY] & 0x07; |
| 296 | tm->tm_mday = bcd2bin(data[RTC_DATE]); |
| 297 | tm->tm_mon = bcd2bin(data[RTC_MONTH]); |
| 298 | tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100; |
| 299 | tm->tm_year -= 1900; |
| 300 | } |
| 301 | |
| 302 | static void s5m8763_tm_to_data(struct rtc_time *tm, u8 *data) |
| 303 | { |
| 304 | data[RTC_SEC] = bin2bcd(tm->tm_sec); |
| 305 | data[RTC_MIN] = bin2bcd(tm->tm_min); |
| 306 | data[RTC_HOUR] = bin2bcd(tm->tm_hour); |
| 307 | data[RTC_WEEKDAY] = tm->tm_wday; |
| 308 | data[RTC_DATE] = bin2bcd(tm->tm_mday); |
| 309 | data[RTC_MONTH] = bin2bcd(tm->tm_mon); |
| 310 | data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100); |
| 311 | data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100); |
| 312 | } |
| 313 | |
| 314 | static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 315 | { |
| 316 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 317 | u8 data[info->regs->regs_count]; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 318 | int ret; |
| 319 | |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 320 | if (info->device_type == S2MPS14X || info->device_type == S2MPS13X) { |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 321 | ret = regmap_update_bits(info->regmap, |
| 322 | info->regs->rtc_udr_update, |
| 323 | S2MPS_RTC_RUDR_MASK, S2MPS_RTC_RUDR_MASK); |
| 324 | if (ret) { |
| 325 | dev_err(dev, |
| 326 | "Failed to prepare registers for time reading: %d\n", |
| 327 | ret); |
| 328 | return ret; |
| 329 | } |
| 330 | } |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 331 | ret = regmap_bulk_read(info->regmap, info->regs->time, data, |
| 332 | info->regs->regs_count); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 333 | if (ret < 0) |
| 334 | return ret; |
| 335 | |
| 336 | switch (info->device_type) { |
| 337 | case S5M8763X: |
| 338 | s5m8763_data_to_tm(data, tm); |
| 339 | break; |
| 340 | |
| 341 | case S5M8767X: |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 342 | case S2MPS14X: |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 343 | case S2MPS13X: |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 344 | s5m8767_data_to_tm(data, tm, info->rtc_24hr_mode); |
| 345 | break; |
| 346 | |
| 347 | default: |
| 348 | return -EINVAL; |
| 349 | } |
| 350 | |
| 351 | dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, |
| 352 | 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, |
| 353 | tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday); |
| 354 | |
| 355 | return rtc_valid_tm(tm); |
| 356 | } |
| 357 | |
| 358 | static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 359 | { |
| 360 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 361 | u8 data[info->regs->regs_count]; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 362 | int ret = 0; |
| 363 | |
| 364 | switch (info->device_type) { |
| 365 | case S5M8763X: |
| 366 | s5m8763_tm_to_data(tm, data); |
| 367 | break; |
| 368 | case S5M8767X: |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 369 | case S2MPS14X: |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 370 | case S2MPS13X: |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 371 | ret = s5m8767_tm_to_data(tm, data); |
| 372 | break; |
| 373 | default: |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | |
| 377 | if (ret < 0) |
| 378 | return ret; |
| 379 | |
| 380 | dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, |
| 381 | 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, |
| 382 | tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday); |
| 383 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 384 | ret = regmap_raw_write(info->regmap, info->regs->time, data, |
| 385 | info->regs->regs_count); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 386 | if (ret < 0) |
| 387 | return ret; |
| 388 | |
| 389 | ret = s5m8767_rtc_set_time_reg(info); |
| 390 | |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 395 | { |
| 396 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 397 | u8 data[info->regs->regs_count]; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 398 | unsigned int val; |
| 399 | int ret, i; |
| 400 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 401 | ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data, |
| 402 | info->regs->regs_count); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 403 | if (ret < 0) |
| 404 | return ret; |
| 405 | |
| 406 | switch (info->device_type) { |
| 407 | case S5M8763X: |
| 408 | s5m8763_data_to_tm(data, &alrm->time); |
Krzysztof Kozlowski | 602cb5b | 2014-06-10 15:18:40 -0700 | [diff] [blame] | 409 | ret = regmap_read(info->regmap, S5M_ALARM0_CONF, &val); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 410 | if (ret < 0) |
| 411 | return ret; |
| 412 | |
| 413 | alrm->enabled = !!val; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 414 | break; |
| 415 | |
| 416 | case S5M8767X: |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 417 | case S2MPS14X: |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 418 | case S2MPS13X: |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 419 | s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 420 | alrm->enabled = 0; |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 421 | for (i = 0; i < info->regs->regs_count; i++) { |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 422 | if (data[i] & ALARM_ENABLE_MASK) { |
| 423 | alrm->enabled = 1; |
| 424 | break; |
| 425 | } |
| 426 | } |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 427 | break; |
| 428 | |
| 429 | default: |
| 430 | return -EINVAL; |
| 431 | } |
| 432 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 433 | dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, |
| 434 | 1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon, |
| 435 | alrm->time.tm_mday, alrm->time.tm_hour, |
| 436 | alrm->time.tm_min, alrm->time.tm_sec, |
| 437 | alrm->time.tm_wday); |
| 438 | |
| 439 | ret = s5m_check_peding_alarm_interrupt(info, alrm); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info) |
| 445 | { |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 446 | u8 data[info->regs->regs_count]; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 447 | int ret, i; |
| 448 | struct rtc_time tm; |
| 449 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 450 | ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data, |
| 451 | info->regs->regs_count); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 452 | if (ret < 0) |
| 453 | return ret; |
| 454 | |
| 455 | s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode); |
| 456 | dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, |
| 457 | 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday, |
| 458 | tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday); |
| 459 | |
| 460 | switch (info->device_type) { |
| 461 | case S5M8763X: |
Krzysztof Kozlowski | 602cb5b | 2014-06-10 15:18:40 -0700 | [diff] [blame] | 462 | ret = regmap_write(info->regmap, S5M_ALARM0_CONF, 0); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 463 | break; |
| 464 | |
| 465 | case S5M8767X: |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 466 | case S2MPS14X: |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 467 | case S2MPS13X: |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 468 | for (i = 0; i < info->regs->regs_count; i++) |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 469 | data[i] &= ~ALARM_ENABLE_MASK; |
| 470 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 471 | ret = regmap_raw_write(info->regmap, info->regs->alarm0, data, |
| 472 | info->regs->regs_count); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 473 | if (ret < 0) |
| 474 | return ret; |
| 475 | |
| 476 | ret = s5m8767_rtc_set_alarm_reg(info); |
| 477 | |
| 478 | break; |
| 479 | |
| 480 | default: |
| 481 | return -EINVAL; |
| 482 | } |
| 483 | |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | static int s5m_rtc_start_alarm(struct s5m_rtc_info *info) |
| 488 | { |
| 489 | int ret; |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 490 | u8 data[info->regs->regs_count]; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 491 | u8 alarm0_conf; |
| 492 | struct rtc_time tm; |
| 493 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 494 | ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data, |
| 495 | info->regs->regs_count); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 496 | if (ret < 0) |
| 497 | return ret; |
| 498 | |
| 499 | s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode); |
| 500 | dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, |
| 501 | 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday, |
| 502 | tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday); |
| 503 | |
| 504 | switch (info->device_type) { |
| 505 | case S5M8763X: |
| 506 | alarm0_conf = 0x77; |
Krzysztof Kozlowski | 602cb5b | 2014-06-10 15:18:40 -0700 | [diff] [blame] | 507 | ret = regmap_write(info->regmap, S5M_ALARM0_CONF, alarm0_conf); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 508 | break; |
| 509 | |
| 510 | case S5M8767X: |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 511 | case S2MPS14X: |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 512 | case S2MPS13X: |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 513 | data[RTC_SEC] |= ALARM_ENABLE_MASK; |
| 514 | data[RTC_MIN] |= ALARM_ENABLE_MASK; |
| 515 | data[RTC_HOUR] |= ALARM_ENABLE_MASK; |
| 516 | data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK; |
| 517 | if (data[RTC_DATE] & 0x1f) |
| 518 | data[RTC_DATE] |= ALARM_ENABLE_MASK; |
| 519 | if (data[RTC_MONTH] & 0xf) |
| 520 | data[RTC_MONTH] |= ALARM_ENABLE_MASK; |
| 521 | if (data[RTC_YEAR1] & 0x7f) |
| 522 | data[RTC_YEAR1] |= ALARM_ENABLE_MASK; |
| 523 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 524 | ret = regmap_raw_write(info->regmap, info->regs->alarm0, data, |
| 525 | info->regs->regs_count); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 526 | if (ret < 0) |
| 527 | return ret; |
| 528 | ret = s5m8767_rtc_set_alarm_reg(info); |
| 529 | |
| 530 | break; |
| 531 | |
| 532 | default: |
| 533 | return -EINVAL; |
| 534 | } |
| 535 | |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 540 | { |
| 541 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 542 | u8 data[info->regs->regs_count]; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 543 | int ret; |
| 544 | |
| 545 | switch (info->device_type) { |
| 546 | case S5M8763X: |
| 547 | s5m8763_tm_to_data(&alrm->time, data); |
| 548 | break; |
| 549 | |
| 550 | case S5M8767X: |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 551 | case S2MPS14X: |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 552 | case S2MPS13X: |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 553 | s5m8767_tm_to_data(&alrm->time, data); |
| 554 | break; |
| 555 | |
| 556 | default: |
| 557 | return -EINVAL; |
| 558 | } |
| 559 | |
| 560 | dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, |
| 561 | 1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon, |
| 562 | alrm->time.tm_mday, alrm->time.tm_hour, alrm->time.tm_min, |
| 563 | alrm->time.tm_sec, alrm->time.tm_wday); |
| 564 | |
| 565 | ret = s5m_rtc_stop_alarm(info); |
| 566 | if (ret < 0) |
| 567 | return ret; |
| 568 | |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 569 | ret = regmap_raw_write(info->regmap, info->regs->alarm0, data, |
| 570 | info->regs->regs_count); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 571 | if (ret < 0) |
| 572 | return ret; |
| 573 | |
| 574 | ret = s5m8767_rtc_set_alarm_reg(info); |
| 575 | if (ret < 0) |
| 576 | return ret; |
| 577 | |
| 578 | if (alrm->enabled) |
| 579 | ret = s5m_rtc_start_alarm(info); |
| 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | static int s5m_rtc_alarm_irq_enable(struct device *dev, |
| 585 | unsigned int enabled) |
| 586 | { |
| 587 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
| 588 | |
| 589 | if (enabled) |
| 590 | return s5m_rtc_start_alarm(info); |
| 591 | else |
| 592 | return s5m_rtc_stop_alarm(info); |
| 593 | } |
| 594 | |
| 595 | static irqreturn_t s5m_rtc_alarm_irq(int irq, void *data) |
| 596 | { |
| 597 | struct s5m_rtc_info *info = data; |
| 598 | |
| 599 | rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF); |
| 600 | |
| 601 | return IRQ_HANDLED; |
| 602 | } |
| 603 | |
| 604 | static const struct rtc_class_ops s5m_rtc_ops = { |
| 605 | .read_time = s5m_rtc_read_time, |
| 606 | .set_time = s5m_rtc_set_time, |
| 607 | .read_alarm = s5m_rtc_read_alarm, |
| 608 | .set_alarm = s5m_rtc_set_alarm, |
| 609 | .alarm_irq_enable = s5m_rtc_alarm_irq_enable, |
| 610 | }; |
| 611 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 612 | static int s5m8767_rtc_init_reg(struct s5m_rtc_info *info) |
| 613 | { |
| 614 | u8 data[2]; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 615 | int ret; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 616 | |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 617 | switch (info->device_type) { |
| 618 | case S5M8763X: |
| 619 | case S5M8767X: |
| 620 | /* UDR update time. Default of 7.32 ms is too long. */ |
| 621 | ret = regmap_update_bits(info->regmap, S5M_RTC_UDR_CON, |
| 622 | S5M_RTC_UDR_T_MASK, S5M_RTC_UDR_T_450_US); |
| 623 | if (ret < 0) |
| 624 | dev_err(info->dev, "%s: fail to change UDR time: %d\n", |
| 625 | __func__, ret); |
Krzysztof Kozlowski | 0c5f5d9 | 2014-06-10 15:18:43 -0700 | [diff] [blame] | 626 | |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 627 | /* Set RTC control register : Binary mode, 24hour mode */ |
| 628 | data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); |
| 629 | data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); |
| 630 | |
| 631 | ret = regmap_raw_write(info->regmap, S5M_ALARM0_CONF, data, 2); |
| 632 | break; |
| 633 | |
| 634 | case S2MPS14X: |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 635 | case S2MPS13X: |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 636 | data[0] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); |
| 637 | ret = regmap_write(info->regmap, info->regs->ctrl, data[0]); |
| 638 | break; |
| 639 | |
| 640 | default: |
| 641 | return -EINVAL; |
| 642 | } |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 643 | |
| 644 | info->rtc_24hr_mode = 1; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 645 | if (ret < 0) { |
| 646 | dev_err(info->dev, "%s: fail to write controlm reg(%d)\n", |
| 647 | __func__, ret); |
| 648 | return ret; |
| 649 | } |
| 650 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | static int s5m_rtc_probe(struct platform_device *pdev) |
| 655 | { |
| 656 | struct sec_pmic_dev *s5m87xx = dev_get_drvdata(pdev->dev.parent); |
| 657 | struct sec_platform_data *pdata = s5m87xx->pdata; |
| 658 | struct s5m_rtc_info *info; |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 659 | const struct regmap_config *regmap_cfg; |
Krzysztof Kozlowski | a0347f2 | 2014-06-10 15:18:47 -0700 | [diff] [blame] | 660 | int ret, alarm_irq; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 661 | |
| 662 | if (!pdata) { |
| 663 | dev_err(pdev->dev.parent, "Platform data not supplied\n"); |
| 664 | return -ENODEV; |
| 665 | } |
| 666 | |
| 667 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 668 | if (!info) |
| 669 | return -ENOMEM; |
| 670 | |
Krzysztof Kozlowski | 94f9192 | 2015-04-16 12:49:27 -0700 | [diff] [blame] | 671 | switch (platform_get_device_id(pdev)->driver_data) { |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 672 | case S2MPS14X: |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 673 | case S2MPS13X: |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 674 | regmap_cfg = &s2mps14_rtc_regmap_config; |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 675 | info->regs = &s2mps_rtc_regs; |
Krzysztof Kozlowski | a0347f2 | 2014-06-10 15:18:47 -0700 | [diff] [blame] | 676 | alarm_irq = S2MPS14_IRQ_RTCA0; |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 677 | break; |
| 678 | case S5M8763X: |
| 679 | regmap_cfg = &s5m_rtc_regmap_config; |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 680 | info->regs = &s5m_rtc_regs; |
Krzysztof Kozlowski | a0347f2 | 2014-06-10 15:18:47 -0700 | [diff] [blame] | 681 | alarm_irq = S5M8763_IRQ_ALARM0; |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 682 | break; |
| 683 | case S5M8767X: |
| 684 | regmap_cfg = &s5m_rtc_regmap_config; |
Krzysztof Kozlowski | f8b23bb | 2014-06-10 15:18:44 -0700 | [diff] [blame] | 685 | info->regs = &s5m_rtc_regs; |
Krzysztof Kozlowski | a0347f2 | 2014-06-10 15:18:47 -0700 | [diff] [blame] | 686 | alarm_irq = S5M8767_IRQ_RTCA1; |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 687 | break; |
| 688 | default: |
Krzysztof Kozlowski | 94f9192 | 2015-04-16 12:49:27 -0700 | [diff] [blame] | 689 | dev_err(&pdev->dev, |
| 690 | "Device type %lu is not supported by RTC driver\n", |
| 691 | platform_get_device_id(pdev)->driver_data); |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 692 | return -ENODEV; |
| 693 | } |
| 694 | |
| 695 | info->i2c = i2c_new_dummy(s5m87xx->i2c->adapter, RTC_I2C_ADDR); |
| 696 | if (!info->i2c) { |
| 697 | dev_err(&pdev->dev, "Failed to allocate I2C for RTC\n"); |
| 698 | return -ENODEV; |
| 699 | } |
| 700 | |
| 701 | info->regmap = devm_regmap_init_i2c(info->i2c, regmap_cfg); |
| 702 | if (IS_ERR(info->regmap)) { |
| 703 | ret = PTR_ERR(info->regmap); |
| 704 | dev_err(&pdev->dev, "Failed to allocate RTC register map: %d\n", |
| 705 | ret); |
| 706 | goto err; |
| 707 | } |
| 708 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 709 | info->dev = &pdev->dev; |
| 710 | info->s5m87xx = s5m87xx; |
Krzysztof Kozlowski | 94f9192 | 2015-04-16 12:49:27 -0700 | [diff] [blame] | 711 | info->device_type = platform_get_device_id(pdev)->driver_data; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 712 | |
Bartlomiej Zolnierkiewicz | b7d5b9a | 2014-08-29 15:18:53 -0700 | [diff] [blame] | 713 | if (s5m87xx->irq_data) { |
| 714 | info->irq = regmap_irq_get_virq(s5m87xx->irq_data, alarm_irq); |
| 715 | if (info->irq <= 0) { |
| 716 | ret = -EINVAL; |
| 717 | dev_err(&pdev->dev, "Failed to get virtual IRQ %d\n", |
Krzysztof Kozlowski | a0347f2 | 2014-06-10 15:18:47 -0700 | [diff] [blame] | 718 | alarm_irq); |
Bartlomiej Zolnierkiewicz | b7d5b9a | 2014-08-29 15:18:53 -0700 | [diff] [blame] | 719 | goto err; |
| 720 | } |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | platform_set_drvdata(pdev, info); |
| 724 | |
| 725 | ret = s5m8767_rtc_init_reg(info); |
| 726 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 727 | device_init_wakeup(&pdev->dev, 1); |
| 728 | |
| 729 | info->rtc_dev = devm_rtc_device_register(&pdev->dev, "s5m-rtc", |
| 730 | &s5m_rtc_ops, THIS_MODULE); |
| 731 | |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 732 | if (IS_ERR(info->rtc_dev)) { |
| 733 | ret = PTR_ERR(info->rtc_dev); |
| 734 | goto err; |
| 735 | } |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 736 | |
Bartlomiej Zolnierkiewicz | b7d5b9a | 2014-08-29 15:18:53 -0700 | [diff] [blame] | 737 | if (!info->irq) { |
| 738 | dev_info(&pdev->dev, "Alarm IRQ not available\n"); |
| 739 | return 0; |
| 740 | } |
| 741 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 742 | ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL, |
| 743 | s5m_rtc_alarm_irq, 0, "rtc-alarm0", |
| 744 | info); |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 745 | if (ret < 0) { |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 746 | dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n", |
| 747 | info->irq, ret); |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 748 | goto err; |
| 749 | } |
| 750 | |
| 751 | return 0; |
| 752 | |
| 753 | err: |
| 754 | i2c_unregister_device(info->i2c); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 755 | |
| 756 | return ret; |
| 757 | } |
| 758 | |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 759 | static int s5m_rtc_remove(struct platform_device *pdev) |
| 760 | { |
| 761 | struct s5m_rtc_info *info = platform_get_drvdata(pdev); |
| 762 | |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 763 | i2c_unregister_device(info->i2c); |
| 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
Geert Uytterhoeven | 11ba5a1 | 2014-01-23 15:55:19 -0800 | [diff] [blame] | 768 | #ifdef CONFIG_PM_SLEEP |
Krzysztof Kozlowski | 222ead7 | 2013-12-12 17:12:30 -0800 | [diff] [blame] | 769 | static int s5m_rtc_resume(struct device *dev) |
| 770 | { |
| 771 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
| 772 | int ret = 0; |
| 773 | |
Bartlomiej Zolnierkiewicz | b7d5b9a | 2014-08-29 15:18:53 -0700 | [diff] [blame] | 774 | if (info->irq && device_may_wakeup(dev)) |
Krzysztof Kozlowski | 222ead7 | 2013-12-12 17:12:30 -0800 | [diff] [blame] | 775 | ret = disable_irq_wake(info->irq); |
| 776 | |
| 777 | return ret; |
| 778 | } |
| 779 | |
| 780 | static int s5m_rtc_suspend(struct device *dev) |
| 781 | { |
| 782 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
| 783 | int ret = 0; |
| 784 | |
Bartlomiej Zolnierkiewicz | b7d5b9a | 2014-08-29 15:18:53 -0700 | [diff] [blame] | 785 | if (info->irq && device_may_wakeup(dev)) |
Krzysztof Kozlowski | 222ead7 | 2013-12-12 17:12:30 -0800 | [diff] [blame] | 786 | ret = enable_irq_wake(info->irq); |
| 787 | |
| 788 | return ret; |
| 789 | } |
Geert Uytterhoeven | 11ba5a1 | 2014-01-23 15:55:19 -0800 | [diff] [blame] | 790 | #endif /* CONFIG_PM_SLEEP */ |
Krzysztof Kozlowski | 222ead7 | 2013-12-12 17:12:30 -0800 | [diff] [blame] | 791 | |
| 792 | static SIMPLE_DEV_PM_OPS(s5m_rtc_pm_ops, s5m_rtc_suspend, s5m_rtc_resume); |
| 793 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 794 | static const struct platform_device_id s5m_rtc_id[] = { |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 795 | { "s5m-rtc", S5M8767X }, |
Krzysztof Kozlowski | 5281f94 | 2015-04-16 12:45:45 -0700 | [diff] [blame] | 796 | { "s2mps13-rtc", S2MPS13X }, |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 797 | { "s2mps14-rtc", S2MPS14X }, |
Andrey Ryabinin | 45cd15e | 2015-01-26 12:58:46 -0800 | [diff] [blame] | 798 | { }, |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 799 | }; |
| 800 | |
| 801 | static struct platform_driver s5m_rtc_driver = { |
| 802 | .driver = { |
| 803 | .name = "s5m-rtc", |
Krzysztof Kozlowski | 222ead7 | 2013-12-12 17:12:30 -0800 | [diff] [blame] | 804 | .pm = &s5m_rtc_pm_ops, |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 805 | }, |
| 806 | .probe = s5m_rtc_probe, |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame] | 807 | .remove = s5m_rtc_remove, |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 808 | .id_table = s5m_rtc_id, |
| 809 | }; |
| 810 | |
| 811 | module_platform_driver(s5m_rtc_driver); |
| 812 | |
| 813 | /* Module information */ |
| 814 | MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>"); |
Krzysztof Kozlowski | 0c5deb1 | 2014-06-10 15:18:46 -0700 | [diff] [blame] | 815 | MODULE_DESCRIPTION("Samsung S5M/S2MPS14 RTC driver"); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 816 | MODULE_LICENSE("GPL"); |
| 817 | MODULE_ALIAS("platform:s5m-rtc"); |