Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Samsung Electronics Co., Ltd |
| 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 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/bcd.h> |
| 22 | #include <linux/bitops.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/rtc.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/mfd/samsung/core.h> |
| 28 | #include <linux/mfd/samsung/irq.h> |
| 29 | #include <linux/mfd/samsung/rtc.h> |
| 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 |
| 33 | * of SEC_RTC_UDR_CON register (to limit possible endless loop). |
| 34 | * |
| 35 | * After writing to RTC registers (setting time or alarm) read the UDR field |
| 36 | * in SEC_RTC_UDR_CON register. UDR is auto-cleared when data have |
| 37 | * been transferred. |
| 38 | */ |
| 39 | #define UDR_READ_RETRY_CNT 5 |
| 40 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 41 | struct s5m_rtc_info { |
| 42 | struct device *dev; |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame^] | 43 | struct i2c_client *i2c; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 44 | struct sec_pmic_dev *s5m87xx; |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 45 | struct regmap *regmap; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 46 | struct rtc_device *rtc_dev; |
| 47 | int irq; |
| 48 | int device_type; |
| 49 | int rtc_24hr_mode; |
| 50 | bool wtsr_smpl; |
| 51 | }; |
| 52 | |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame^] | 53 | static const struct regmap_config s5m_rtc_regmap_config = { |
| 54 | .reg_bits = 8, |
| 55 | .val_bits = 8, |
| 56 | |
| 57 | .max_register = SEC_RTC_REG_MAX, |
| 58 | }; |
| 59 | |
| 60 | static const struct regmap_config s2mps14_rtc_regmap_config = { |
| 61 | .reg_bits = 8, |
| 62 | .val_bits = 8, |
| 63 | |
| 64 | .max_register = S2MPS_RTC_REG_MAX, |
| 65 | }; |
| 66 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 67 | static void s5m8767_data_to_tm(u8 *data, struct rtc_time *tm, |
| 68 | int rtc_24hr_mode) |
| 69 | { |
| 70 | tm->tm_sec = data[RTC_SEC] & 0x7f; |
| 71 | tm->tm_min = data[RTC_MIN] & 0x7f; |
| 72 | if (rtc_24hr_mode) { |
| 73 | tm->tm_hour = data[RTC_HOUR] & 0x1f; |
| 74 | } else { |
| 75 | tm->tm_hour = data[RTC_HOUR] & 0x0f; |
| 76 | if (data[RTC_HOUR] & HOUR_PM_MASK) |
| 77 | tm->tm_hour += 12; |
| 78 | } |
| 79 | |
| 80 | tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f); |
| 81 | tm->tm_mday = data[RTC_DATE] & 0x1f; |
| 82 | tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1; |
| 83 | tm->tm_year = (data[RTC_YEAR1] & 0x7f) + 100; |
| 84 | tm->tm_yday = 0; |
| 85 | tm->tm_isdst = 0; |
| 86 | } |
| 87 | |
| 88 | static int s5m8767_tm_to_data(struct rtc_time *tm, u8 *data) |
| 89 | { |
| 90 | data[RTC_SEC] = tm->tm_sec; |
| 91 | data[RTC_MIN] = tm->tm_min; |
| 92 | |
| 93 | if (tm->tm_hour >= 12) |
| 94 | data[RTC_HOUR] = tm->tm_hour | HOUR_PM_MASK; |
| 95 | else |
| 96 | data[RTC_HOUR] = tm->tm_hour & ~HOUR_PM_MASK; |
| 97 | |
| 98 | data[RTC_WEEKDAY] = 1 << tm->tm_wday; |
| 99 | data[RTC_DATE] = tm->tm_mday; |
| 100 | data[RTC_MONTH] = tm->tm_mon + 1; |
| 101 | data[RTC_YEAR1] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0; |
| 102 | |
| 103 | if (tm->tm_year < 100) { |
| 104 | pr_err("s5m8767 RTC cannot handle the year %d.\n", |
| 105 | 1900 + tm->tm_year); |
| 106 | return -EINVAL; |
| 107 | } else { |
| 108 | return 0; |
| 109 | } |
| 110 | } |
| 111 | |
Krzysztof Kozlowski | d73238d | 2013-12-12 17:12:28 -0800 | [diff] [blame] | 112 | /* |
| 113 | * Read RTC_UDR_CON register and wait till UDR field is cleared. |
| 114 | * This indicates that time/alarm update ended. |
| 115 | */ |
| 116 | static inline int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info) |
| 117 | { |
| 118 | int ret, retry = UDR_READ_RETRY_CNT; |
| 119 | unsigned int data; |
| 120 | |
| 121 | do { |
| 122 | ret = regmap_read(info->regmap, SEC_RTC_UDR_CON, &data); |
| 123 | } while (--retry && (data & RTC_UDR_MASK) && !ret); |
| 124 | |
| 125 | if (!retry) |
| 126 | dev_err(info->dev, "waiting for UDR update, reached max number of retries\n"); |
| 127 | |
| 128 | return ret; |
| 129 | } |
| 130 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 131 | static inline int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info) |
| 132 | { |
| 133 | int ret; |
| 134 | unsigned int data; |
| 135 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 136 | ret = regmap_read(info->regmap, SEC_RTC_UDR_CON, &data); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 137 | if (ret < 0) { |
| 138 | dev_err(info->dev, "failed to read update reg(%d)\n", ret); |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | data |= RTC_TIME_EN_MASK; |
| 143 | data |= RTC_UDR_MASK; |
| 144 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 145 | ret = regmap_write(info->regmap, SEC_RTC_UDR_CON, data); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 146 | if (ret < 0) { |
| 147 | dev_err(info->dev, "failed to write update reg(%d)\n", ret); |
| 148 | return ret; |
| 149 | } |
| 150 | |
Krzysztof Kozlowski | d73238d | 2013-12-12 17:12:28 -0800 | [diff] [blame] | 151 | ret = s5m8767_wait_for_udr_update(info); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | static inline int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info) |
| 157 | { |
| 158 | int ret; |
| 159 | unsigned int data; |
| 160 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 161 | ret = regmap_read(info->regmap, SEC_RTC_UDR_CON, &data); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 162 | if (ret < 0) { |
| 163 | dev_err(info->dev, "%s: fail to read update reg(%d)\n", |
| 164 | __func__, ret); |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | data &= ~RTC_TIME_EN_MASK; |
| 169 | data |= RTC_UDR_MASK; |
| 170 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 171 | ret = regmap_write(info->regmap, SEC_RTC_UDR_CON, data); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 172 | if (ret < 0) { |
| 173 | dev_err(info->dev, "%s: fail to write update reg(%d)\n", |
| 174 | __func__, ret); |
| 175 | return ret; |
| 176 | } |
| 177 | |
Krzysztof Kozlowski | d73238d | 2013-12-12 17:12:28 -0800 | [diff] [blame] | 178 | ret = s5m8767_wait_for_udr_update(info); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 179 | |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static void s5m8763_data_to_tm(u8 *data, struct rtc_time *tm) |
| 184 | { |
| 185 | tm->tm_sec = bcd2bin(data[RTC_SEC]); |
| 186 | tm->tm_min = bcd2bin(data[RTC_MIN]); |
| 187 | |
| 188 | if (data[RTC_HOUR] & HOUR_12) { |
| 189 | tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f); |
| 190 | if (data[RTC_HOUR] & HOUR_PM) |
| 191 | tm->tm_hour += 12; |
| 192 | } else { |
| 193 | tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f); |
| 194 | } |
| 195 | |
| 196 | tm->tm_wday = data[RTC_WEEKDAY] & 0x07; |
| 197 | tm->tm_mday = bcd2bin(data[RTC_DATE]); |
| 198 | tm->tm_mon = bcd2bin(data[RTC_MONTH]); |
| 199 | tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100; |
| 200 | tm->tm_year -= 1900; |
| 201 | } |
| 202 | |
| 203 | static void s5m8763_tm_to_data(struct rtc_time *tm, u8 *data) |
| 204 | { |
| 205 | data[RTC_SEC] = bin2bcd(tm->tm_sec); |
| 206 | data[RTC_MIN] = bin2bcd(tm->tm_min); |
| 207 | data[RTC_HOUR] = bin2bcd(tm->tm_hour); |
| 208 | data[RTC_WEEKDAY] = tm->tm_wday; |
| 209 | data[RTC_DATE] = bin2bcd(tm->tm_mday); |
| 210 | data[RTC_MONTH] = bin2bcd(tm->tm_mon); |
| 211 | data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100); |
| 212 | data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100); |
| 213 | } |
| 214 | |
| 215 | static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 216 | { |
| 217 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
| 218 | u8 data[8]; |
| 219 | int ret; |
| 220 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 221 | ret = regmap_bulk_read(info->regmap, SEC_RTC_SEC, data, 8); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 222 | if (ret < 0) |
| 223 | return ret; |
| 224 | |
| 225 | switch (info->device_type) { |
| 226 | case S5M8763X: |
| 227 | s5m8763_data_to_tm(data, tm); |
| 228 | break; |
| 229 | |
| 230 | case S5M8767X: |
| 231 | s5m8767_data_to_tm(data, tm, info->rtc_24hr_mode); |
| 232 | break; |
| 233 | |
| 234 | default: |
| 235 | return -EINVAL; |
| 236 | } |
| 237 | |
| 238 | dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, |
| 239 | 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, |
| 240 | tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday); |
| 241 | |
| 242 | return rtc_valid_tm(tm); |
| 243 | } |
| 244 | |
| 245 | static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 246 | { |
| 247 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
| 248 | u8 data[8]; |
| 249 | int ret = 0; |
| 250 | |
| 251 | switch (info->device_type) { |
| 252 | case S5M8763X: |
| 253 | s5m8763_tm_to_data(tm, data); |
| 254 | break; |
| 255 | case S5M8767X: |
| 256 | ret = s5m8767_tm_to_data(tm, data); |
| 257 | break; |
| 258 | default: |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | |
| 262 | if (ret < 0) |
| 263 | return ret; |
| 264 | |
| 265 | dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, |
| 266 | 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, |
| 267 | tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday); |
| 268 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 269 | ret = regmap_raw_write(info->regmap, SEC_RTC_SEC, data, 8); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 270 | if (ret < 0) |
| 271 | return ret; |
| 272 | |
| 273 | ret = s5m8767_rtc_set_time_reg(info); |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 279 | { |
| 280 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
| 281 | u8 data[8]; |
| 282 | unsigned int val; |
| 283 | int ret, i; |
| 284 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 285 | ret = regmap_bulk_read(info->regmap, SEC_ALARM0_SEC, data, 8); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 286 | if (ret < 0) |
| 287 | return ret; |
| 288 | |
| 289 | switch (info->device_type) { |
| 290 | case S5M8763X: |
| 291 | s5m8763_data_to_tm(data, &alrm->time); |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 292 | ret = regmap_read(info->regmap, SEC_ALARM0_CONF, &val); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 293 | if (ret < 0) |
| 294 | return ret; |
| 295 | |
| 296 | alrm->enabled = !!val; |
| 297 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 298 | ret = regmap_read(info->regmap, SEC_RTC_STATUS, &val); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 299 | if (ret < 0) |
| 300 | return ret; |
| 301 | |
| 302 | break; |
| 303 | |
| 304 | case S5M8767X: |
| 305 | s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode); |
| 306 | dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, |
| 307 | 1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon, |
| 308 | alrm->time.tm_mday, alrm->time.tm_hour, |
| 309 | alrm->time.tm_min, alrm->time.tm_sec, |
| 310 | alrm->time.tm_wday); |
| 311 | |
| 312 | alrm->enabled = 0; |
| 313 | for (i = 0; i < 7; i++) { |
| 314 | if (data[i] & ALARM_ENABLE_MASK) { |
| 315 | alrm->enabled = 1; |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | alrm->pending = 0; |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 321 | ret = regmap_read(info->regmap, SEC_RTC_STATUS, &val); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 322 | if (ret < 0) |
| 323 | return ret; |
| 324 | break; |
| 325 | |
| 326 | default: |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | |
| 330 | if (val & ALARM0_STATUS) |
| 331 | alrm->pending = 1; |
| 332 | else |
| 333 | alrm->pending = 0; |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info) |
| 339 | { |
| 340 | u8 data[8]; |
| 341 | int ret, i; |
| 342 | struct rtc_time tm; |
| 343 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 344 | ret = regmap_bulk_read(info->regmap, SEC_ALARM0_SEC, data, 8); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 345 | if (ret < 0) |
| 346 | return ret; |
| 347 | |
| 348 | s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode); |
| 349 | dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, |
| 350 | 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday, |
| 351 | tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday); |
| 352 | |
| 353 | switch (info->device_type) { |
| 354 | case S5M8763X: |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 355 | ret = regmap_write(info->regmap, SEC_ALARM0_CONF, 0); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 356 | break; |
| 357 | |
| 358 | case S5M8767X: |
| 359 | for (i = 0; i < 7; i++) |
| 360 | data[i] &= ~ALARM_ENABLE_MASK; |
| 361 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 362 | ret = regmap_raw_write(info->regmap, SEC_ALARM0_SEC, data, 8); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 363 | if (ret < 0) |
| 364 | return ret; |
| 365 | |
| 366 | ret = s5m8767_rtc_set_alarm_reg(info); |
| 367 | |
| 368 | break; |
| 369 | |
| 370 | default: |
| 371 | return -EINVAL; |
| 372 | } |
| 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | static int s5m_rtc_start_alarm(struct s5m_rtc_info *info) |
| 378 | { |
| 379 | int ret; |
| 380 | u8 data[8]; |
| 381 | u8 alarm0_conf; |
| 382 | struct rtc_time tm; |
| 383 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 384 | ret = regmap_bulk_read(info->regmap, SEC_ALARM0_SEC, data, 8); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 385 | if (ret < 0) |
| 386 | return ret; |
| 387 | |
| 388 | s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode); |
| 389 | dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, |
| 390 | 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday, |
| 391 | tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday); |
| 392 | |
| 393 | switch (info->device_type) { |
| 394 | case S5M8763X: |
| 395 | alarm0_conf = 0x77; |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 396 | ret = regmap_write(info->regmap, SEC_ALARM0_CONF, alarm0_conf); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 397 | break; |
| 398 | |
| 399 | case S5M8767X: |
| 400 | data[RTC_SEC] |= ALARM_ENABLE_MASK; |
| 401 | data[RTC_MIN] |= ALARM_ENABLE_MASK; |
| 402 | data[RTC_HOUR] |= ALARM_ENABLE_MASK; |
| 403 | data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK; |
| 404 | if (data[RTC_DATE] & 0x1f) |
| 405 | data[RTC_DATE] |= ALARM_ENABLE_MASK; |
| 406 | if (data[RTC_MONTH] & 0xf) |
| 407 | data[RTC_MONTH] |= ALARM_ENABLE_MASK; |
| 408 | if (data[RTC_YEAR1] & 0x7f) |
| 409 | data[RTC_YEAR1] |= ALARM_ENABLE_MASK; |
| 410 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 411 | ret = regmap_raw_write(info->regmap, SEC_ALARM0_SEC, data, 8); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 412 | if (ret < 0) |
| 413 | return ret; |
| 414 | ret = s5m8767_rtc_set_alarm_reg(info); |
| 415 | |
| 416 | break; |
| 417 | |
| 418 | default: |
| 419 | return -EINVAL; |
| 420 | } |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 426 | { |
| 427 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
| 428 | u8 data[8]; |
| 429 | int ret; |
| 430 | |
| 431 | switch (info->device_type) { |
| 432 | case S5M8763X: |
| 433 | s5m8763_tm_to_data(&alrm->time, data); |
| 434 | break; |
| 435 | |
| 436 | case S5M8767X: |
| 437 | s5m8767_tm_to_data(&alrm->time, data); |
| 438 | break; |
| 439 | |
| 440 | default: |
| 441 | return -EINVAL; |
| 442 | } |
| 443 | |
| 444 | dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, |
| 445 | 1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon, |
| 446 | alrm->time.tm_mday, alrm->time.tm_hour, alrm->time.tm_min, |
| 447 | alrm->time.tm_sec, alrm->time.tm_wday); |
| 448 | |
| 449 | ret = s5m_rtc_stop_alarm(info); |
| 450 | if (ret < 0) |
| 451 | return ret; |
| 452 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 453 | ret = regmap_raw_write(info->regmap, SEC_ALARM0_SEC, data, 8); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 454 | if (ret < 0) |
| 455 | return ret; |
| 456 | |
| 457 | ret = s5m8767_rtc_set_alarm_reg(info); |
| 458 | if (ret < 0) |
| 459 | return ret; |
| 460 | |
| 461 | if (alrm->enabled) |
| 462 | ret = s5m_rtc_start_alarm(info); |
| 463 | |
| 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | static int s5m_rtc_alarm_irq_enable(struct device *dev, |
| 468 | unsigned int enabled) |
| 469 | { |
| 470 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
| 471 | |
| 472 | if (enabled) |
| 473 | return s5m_rtc_start_alarm(info); |
| 474 | else |
| 475 | return s5m_rtc_stop_alarm(info); |
| 476 | } |
| 477 | |
| 478 | static irqreturn_t s5m_rtc_alarm_irq(int irq, void *data) |
| 479 | { |
| 480 | struct s5m_rtc_info *info = data; |
| 481 | |
| 482 | rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF); |
| 483 | |
| 484 | return IRQ_HANDLED; |
| 485 | } |
| 486 | |
| 487 | static const struct rtc_class_ops s5m_rtc_ops = { |
| 488 | .read_time = s5m_rtc_read_time, |
| 489 | .set_time = s5m_rtc_set_time, |
| 490 | .read_alarm = s5m_rtc_read_alarm, |
| 491 | .set_alarm = s5m_rtc_set_alarm, |
| 492 | .alarm_irq_enable = s5m_rtc_alarm_irq_enable, |
| 493 | }; |
| 494 | |
| 495 | static void s5m_rtc_enable_wtsr(struct s5m_rtc_info *info, bool enable) |
| 496 | { |
| 497 | int ret; |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 498 | ret = regmap_update_bits(info->regmap, SEC_WTSR_SMPL_CNTL, |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 499 | WTSR_ENABLE_MASK, |
| 500 | enable ? WTSR_ENABLE_MASK : 0); |
| 501 | if (ret < 0) |
| 502 | dev_err(info->dev, "%s: fail to update WTSR reg(%d)\n", |
| 503 | __func__, ret); |
| 504 | } |
| 505 | |
| 506 | static void s5m_rtc_enable_smpl(struct s5m_rtc_info *info, bool enable) |
| 507 | { |
| 508 | int ret; |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 509 | ret = regmap_update_bits(info->regmap, SEC_WTSR_SMPL_CNTL, |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 510 | SMPL_ENABLE_MASK, |
| 511 | enable ? SMPL_ENABLE_MASK : 0); |
| 512 | if (ret < 0) |
| 513 | dev_err(info->dev, "%s: fail to update SMPL reg(%d)\n", |
| 514 | __func__, ret); |
| 515 | } |
| 516 | |
| 517 | static int s5m8767_rtc_init_reg(struct s5m_rtc_info *info) |
| 518 | { |
| 519 | u8 data[2]; |
| 520 | unsigned int tp_read; |
| 521 | int ret; |
| 522 | struct rtc_time tm; |
| 523 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 524 | ret = regmap_read(info->regmap, SEC_RTC_UDR_CON, &tp_read); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 525 | if (ret < 0) { |
| 526 | dev_err(info->dev, "%s: fail to read control reg(%d)\n", |
| 527 | __func__, ret); |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | /* Set RTC control register : Binary mode, 24hour mode */ |
| 532 | data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); |
| 533 | data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); |
| 534 | |
| 535 | info->rtc_24hr_mode = 1; |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 536 | ret = regmap_raw_write(info->regmap, SEC_ALARM0_CONF, data, 2); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 537 | if (ret < 0) { |
| 538 | dev_err(info->dev, "%s: fail to write controlm reg(%d)\n", |
| 539 | __func__, ret); |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | /* In first boot time, Set rtc time to 1/1/2012 00:00:00(SUN) */ |
| 544 | if ((tp_read & RTC_TCON_MASK) == 0) { |
| 545 | dev_dbg(info->dev, "rtc init\n"); |
| 546 | tm.tm_sec = 0; |
| 547 | tm.tm_min = 0; |
| 548 | tm.tm_hour = 0; |
| 549 | tm.tm_wday = 0; |
| 550 | tm.tm_mday = 1; |
| 551 | tm.tm_mon = 0; |
| 552 | tm.tm_year = 112; |
| 553 | tm.tm_yday = 0; |
| 554 | tm.tm_isdst = 0; |
| 555 | ret = s5m_rtc_set_time(info->dev, &tm); |
| 556 | } |
| 557 | |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 558 | ret = regmap_update_bits(info->regmap, SEC_RTC_UDR_CON, |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 559 | RTC_TCON_MASK, tp_read | RTC_TCON_MASK); |
| 560 | if (ret < 0) |
| 561 | dev_err(info->dev, "%s: fail to update TCON reg(%d)\n", |
| 562 | __func__, ret); |
| 563 | |
| 564 | return ret; |
| 565 | } |
| 566 | |
| 567 | static int s5m_rtc_probe(struct platform_device *pdev) |
| 568 | { |
| 569 | struct sec_pmic_dev *s5m87xx = dev_get_drvdata(pdev->dev.parent); |
| 570 | struct sec_platform_data *pdata = s5m87xx->pdata; |
| 571 | struct s5m_rtc_info *info; |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame^] | 572 | const struct regmap_config *regmap_cfg; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 573 | int ret; |
| 574 | |
| 575 | if (!pdata) { |
| 576 | dev_err(pdev->dev.parent, "Platform data not supplied\n"); |
| 577 | return -ENODEV; |
| 578 | } |
| 579 | |
| 580 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 581 | if (!info) |
| 582 | return -ENOMEM; |
| 583 | |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame^] | 584 | switch (pdata->device_type) { |
| 585 | case S2MPS14X: |
| 586 | regmap_cfg = &s2mps14_rtc_regmap_config; |
| 587 | break; |
| 588 | case S5M8763X: |
| 589 | regmap_cfg = &s5m_rtc_regmap_config; |
| 590 | break; |
| 591 | case S5M8767X: |
| 592 | regmap_cfg = &s5m_rtc_regmap_config; |
| 593 | break; |
| 594 | default: |
| 595 | dev_err(&pdev->dev, "Device type is not supported by RTC driver\n"); |
| 596 | return -ENODEV; |
| 597 | } |
| 598 | |
| 599 | info->i2c = i2c_new_dummy(s5m87xx->i2c->adapter, RTC_I2C_ADDR); |
| 600 | if (!info->i2c) { |
| 601 | dev_err(&pdev->dev, "Failed to allocate I2C for RTC\n"); |
| 602 | return -ENODEV; |
| 603 | } |
| 604 | |
| 605 | info->regmap = devm_regmap_init_i2c(info->i2c, regmap_cfg); |
| 606 | if (IS_ERR(info->regmap)) { |
| 607 | ret = PTR_ERR(info->regmap); |
| 608 | dev_err(&pdev->dev, "Failed to allocate RTC register map: %d\n", |
| 609 | ret); |
| 610 | goto err; |
| 611 | } |
| 612 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 613 | info->dev = &pdev->dev; |
| 614 | info->s5m87xx = s5m87xx; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 615 | info->device_type = s5m87xx->device_type; |
| 616 | info->wtsr_smpl = s5m87xx->wtsr_smpl; |
| 617 | |
| 618 | switch (pdata->device_type) { |
| 619 | case S5M8763X: |
Krzysztof Kozlowski | 7b003be | 2013-12-12 17:12:26 -0800 | [diff] [blame] | 620 | info->irq = regmap_irq_get_virq(s5m87xx->irq_data, |
| 621 | S5M8763_IRQ_ALARM0); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 622 | break; |
| 623 | |
| 624 | case S5M8767X: |
Krzysztof Kozlowski | 7b003be | 2013-12-12 17:12:26 -0800 | [diff] [blame] | 625 | info->irq = regmap_irq_get_virq(s5m87xx->irq_data, |
| 626 | S5M8767_IRQ_RTCA1); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 627 | break; |
| 628 | |
| 629 | default: |
| 630 | ret = -EINVAL; |
| 631 | dev_err(&pdev->dev, "Unsupported device type: %d\n", ret); |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame^] | 632 | goto err; |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | platform_set_drvdata(pdev, info); |
| 636 | |
| 637 | ret = s5m8767_rtc_init_reg(info); |
| 638 | |
| 639 | if (info->wtsr_smpl) { |
| 640 | s5m_rtc_enable_wtsr(info, true); |
| 641 | s5m_rtc_enable_smpl(info, true); |
| 642 | } |
| 643 | |
| 644 | device_init_wakeup(&pdev->dev, 1); |
| 645 | |
| 646 | info->rtc_dev = devm_rtc_device_register(&pdev->dev, "s5m-rtc", |
| 647 | &s5m_rtc_ops, THIS_MODULE); |
| 648 | |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame^] | 649 | if (IS_ERR(info->rtc_dev)) { |
| 650 | ret = PTR_ERR(info->rtc_dev); |
| 651 | goto err; |
| 652 | } |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 653 | |
| 654 | ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL, |
| 655 | s5m_rtc_alarm_irq, 0, "rtc-alarm0", |
| 656 | info); |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame^] | 657 | if (ret < 0) { |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 658 | dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n", |
| 659 | info->irq, ret); |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame^] | 660 | goto err; |
| 661 | } |
| 662 | |
| 663 | return 0; |
| 664 | |
| 665 | err: |
| 666 | i2c_unregister_device(info->i2c); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 667 | |
| 668 | return ret; |
| 669 | } |
| 670 | |
| 671 | static void s5m_rtc_shutdown(struct platform_device *pdev) |
| 672 | { |
| 673 | struct s5m_rtc_info *info = platform_get_drvdata(pdev); |
| 674 | int i; |
| 675 | unsigned int val = 0; |
| 676 | if (info->wtsr_smpl) { |
| 677 | for (i = 0; i < 3; i++) { |
| 678 | s5m_rtc_enable_wtsr(info, false); |
Geert Uytterhoeven | 5ccb7d7 | 2013-12-12 17:12:25 -0800 | [diff] [blame] | 679 | regmap_read(info->regmap, SEC_WTSR_SMPL_CNTL, &val); |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 680 | pr_debug("%s: WTSR_SMPL reg(0x%02x)\n", __func__, val); |
| 681 | if (val & WTSR_ENABLE_MASK) |
| 682 | pr_emerg("%s: fail to disable WTSR\n", |
| 683 | __func__); |
| 684 | else { |
| 685 | pr_info("%s: success to disable WTSR\n", |
| 686 | __func__); |
| 687 | break; |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | /* Disable SMPL when power off */ |
| 692 | s5m_rtc_enable_smpl(info, false); |
| 693 | } |
| 694 | |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame^] | 695 | static int s5m_rtc_remove(struct platform_device *pdev) |
| 696 | { |
| 697 | struct s5m_rtc_info *info = platform_get_drvdata(pdev); |
| 698 | |
| 699 | /* Perform also all shutdown steps when removing */ |
| 700 | s5m_rtc_shutdown(pdev); |
| 701 | i2c_unregister_device(info->i2c); |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
Geert Uytterhoeven | 11ba5a1 | 2014-01-23 15:55:19 -0800 | [diff] [blame] | 706 | #ifdef CONFIG_PM_SLEEP |
Krzysztof Kozlowski | 222ead7 | 2013-12-12 17:12:30 -0800 | [diff] [blame] | 707 | static int s5m_rtc_resume(struct device *dev) |
| 708 | { |
| 709 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
| 710 | int ret = 0; |
| 711 | |
| 712 | if (device_may_wakeup(dev)) |
| 713 | ret = disable_irq_wake(info->irq); |
| 714 | |
| 715 | return ret; |
| 716 | } |
| 717 | |
| 718 | static int s5m_rtc_suspend(struct device *dev) |
| 719 | { |
| 720 | struct s5m_rtc_info *info = dev_get_drvdata(dev); |
| 721 | int ret = 0; |
| 722 | |
| 723 | if (device_may_wakeup(dev)) |
| 724 | ret = enable_irq_wake(info->irq); |
| 725 | |
| 726 | return ret; |
| 727 | } |
Geert Uytterhoeven | 11ba5a1 | 2014-01-23 15:55:19 -0800 | [diff] [blame] | 728 | #endif /* CONFIG_PM_SLEEP */ |
Krzysztof Kozlowski | 222ead7 | 2013-12-12 17:12:30 -0800 | [diff] [blame] | 729 | |
| 730 | static SIMPLE_DEV_PM_OPS(s5m_rtc_pm_ops, s5m_rtc_suspend, s5m_rtc_resume); |
| 731 | |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 732 | static const struct platform_device_id s5m_rtc_id[] = { |
| 733 | { "s5m-rtc", 0 }, |
| 734 | }; |
| 735 | |
| 736 | static struct platform_driver s5m_rtc_driver = { |
| 737 | .driver = { |
| 738 | .name = "s5m-rtc", |
| 739 | .owner = THIS_MODULE, |
Krzysztof Kozlowski | 222ead7 | 2013-12-12 17:12:30 -0800 | [diff] [blame] | 740 | .pm = &s5m_rtc_pm_ops, |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 741 | }, |
| 742 | .probe = s5m_rtc_probe, |
Krzysztof Kozlowski | e349c91 | 2014-04-14 09:40:45 +0200 | [diff] [blame^] | 743 | .remove = s5m_rtc_remove, |
Sangbeom Kim | 5bccae6 | 2013-11-12 15:11:04 -0800 | [diff] [blame] | 744 | .shutdown = s5m_rtc_shutdown, |
| 745 | .id_table = s5m_rtc_id, |
| 746 | }; |
| 747 | |
| 748 | module_platform_driver(s5m_rtc_driver); |
| 749 | |
| 750 | /* Module information */ |
| 751 | MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>"); |
| 752 | MODULE_DESCRIPTION("Samsung S5M RTC driver"); |
| 753 | MODULE_LICENSE("GPL"); |
| 754 | MODULE_ALIAS("platform:s5m-rtc"); |