15bccae6eSSangbeom Kim /* 2f8b23bbdSKrzysztof Kozlowski * Copyright (c) 2013-2014 Samsung Electronics Co., Ltd 35bccae6eSSangbeom Kim * http://www.samsung.com 45bccae6eSSangbeom Kim * 55bccae6eSSangbeom Kim * Copyright (C) 2013 Google, Inc 65bccae6eSSangbeom Kim * 75bccae6eSSangbeom Kim * This program is free software; you can redistribute it and/or modify 85bccae6eSSangbeom Kim * it under the terms of the GNU General Public License as published by 95bccae6eSSangbeom Kim * the Free Software Foundation; either version 2 of the License, or 105bccae6eSSangbeom Kim * (at your option) any later version. 115bccae6eSSangbeom Kim * 125bccae6eSSangbeom Kim * This program is distributed in the hope that it will be useful, 135bccae6eSSangbeom Kim * but WITHOUT ANY WARRANTY; without even the implied warranty of 145bccae6eSSangbeom Kim * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 155bccae6eSSangbeom Kim * GNU General Public License for more details. 165bccae6eSSangbeom Kim */ 175bccae6eSSangbeom Kim 185bccae6eSSangbeom Kim #include <linux/module.h> 195bccae6eSSangbeom Kim #include <linux/i2c.h> 205bccae6eSSangbeom Kim #include <linux/bcd.h> 215bccae6eSSangbeom Kim #include <linux/regmap.h> 225bccae6eSSangbeom Kim #include <linux/rtc.h> 235bccae6eSSangbeom Kim #include <linux/platform_device.h> 245bccae6eSSangbeom Kim #include <linux/mfd/samsung/core.h> 255bccae6eSSangbeom Kim #include <linux/mfd/samsung/irq.h> 265bccae6eSSangbeom Kim #include <linux/mfd/samsung/rtc.h> 270c5deb1eSKrzysztof Kozlowski #include <linux/mfd/samsung/s2mps14.h> 285bccae6eSSangbeom Kim 29d73238d4SKrzysztof Kozlowski /* 30d73238d4SKrzysztof Kozlowski * Maximum number of retries for checking changes in UDR field 31602cb5bbSKrzysztof Kozlowski * of S5M_RTC_UDR_CON register (to limit possible endless loop). 32d73238d4SKrzysztof Kozlowski * 33d73238d4SKrzysztof Kozlowski * After writing to RTC registers (setting time or alarm) read the UDR field 34602cb5bbSKrzysztof Kozlowski * in S5M_RTC_UDR_CON register. UDR is auto-cleared when data have 35d73238d4SKrzysztof Kozlowski * been transferred. 36d73238d4SKrzysztof Kozlowski */ 37d73238d4SKrzysztof Kozlowski #define UDR_READ_RETRY_CNT 5 38d73238d4SKrzysztof Kozlowski 39f8b23bbdSKrzysztof Kozlowski /* Registers used by the driver which are different between chipsets. */ 40f8b23bbdSKrzysztof Kozlowski struct s5m_rtc_reg_config { 41f8b23bbdSKrzysztof Kozlowski /* Number of registers used for setting time/alarm0/alarm1 */ 42f8b23bbdSKrzysztof Kozlowski unsigned int regs_count; 43f8b23bbdSKrzysztof Kozlowski /* First register for time, seconds */ 44f8b23bbdSKrzysztof Kozlowski unsigned int time; 45f8b23bbdSKrzysztof Kozlowski /* RTC control register */ 46f8b23bbdSKrzysztof Kozlowski unsigned int ctrl; 47f8b23bbdSKrzysztof Kozlowski /* First register for alarm 0, seconds */ 48f8b23bbdSKrzysztof Kozlowski unsigned int alarm0; 49f8b23bbdSKrzysztof Kozlowski /* First register for alarm 1, seconds */ 50f8b23bbdSKrzysztof Kozlowski unsigned int alarm1; 51f8b23bbdSKrzysztof Kozlowski /* SMPL/WTSR register */ 52f8b23bbdSKrzysztof Kozlowski unsigned int smpl_wtsr; 53f8b23bbdSKrzysztof Kozlowski /* 54f8b23bbdSKrzysztof Kozlowski * Register for update flag (UDR). Typically setting UDR field to 1 55f8b23bbdSKrzysztof Kozlowski * will enable update of time or alarm register. Then it will be 56f8b23bbdSKrzysztof Kozlowski * auto-cleared after successful update. 57f8b23bbdSKrzysztof Kozlowski */ 58f8b23bbdSKrzysztof Kozlowski unsigned int rtc_udr_update; 59f8b23bbdSKrzysztof Kozlowski /* Mask for UDR field in 'rtc_udr_update' register */ 60f8b23bbdSKrzysztof Kozlowski unsigned int rtc_udr_mask; 61f8b23bbdSKrzysztof Kozlowski }; 62f8b23bbdSKrzysztof Kozlowski 63f8b23bbdSKrzysztof Kozlowski /* Register map for S5M8763 and S5M8767 */ 64f8b23bbdSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s5m_rtc_regs = { 65f8b23bbdSKrzysztof Kozlowski .regs_count = 8, 66f8b23bbdSKrzysztof Kozlowski .time = S5M_RTC_SEC, 67f8b23bbdSKrzysztof Kozlowski .ctrl = S5M_ALARM1_CONF, 68f8b23bbdSKrzysztof Kozlowski .alarm0 = S5M_ALARM0_SEC, 69f8b23bbdSKrzysztof Kozlowski .alarm1 = S5M_ALARM1_SEC, 70f8b23bbdSKrzysztof Kozlowski .smpl_wtsr = S5M_WTSR_SMPL_CNTL, 71f8b23bbdSKrzysztof Kozlowski .rtc_udr_update = S5M_RTC_UDR_CON, 72f8b23bbdSKrzysztof Kozlowski .rtc_udr_mask = S5M_RTC_UDR_MASK, 73f8b23bbdSKrzysztof Kozlowski }; 74f8b23bbdSKrzysztof Kozlowski 750c5deb1eSKrzysztof Kozlowski /* 760c5deb1eSKrzysztof Kozlowski * Register map for S2MPS14. 770c5deb1eSKrzysztof Kozlowski * It may be also suitable for S2MPS11 but this was not tested. 780c5deb1eSKrzysztof Kozlowski */ 790c5deb1eSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s2mps_rtc_regs = { 800c5deb1eSKrzysztof Kozlowski .regs_count = 7, 810c5deb1eSKrzysztof Kozlowski .time = S2MPS_RTC_SEC, 820c5deb1eSKrzysztof Kozlowski .ctrl = S2MPS_RTC_CTRL, 830c5deb1eSKrzysztof Kozlowski .alarm0 = S2MPS_ALARM0_SEC, 840c5deb1eSKrzysztof Kozlowski .alarm1 = S2MPS_ALARM1_SEC, 850c5deb1eSKrzysztof Kozlowski .smpl_wtsr = S2MPS_WTSR_SMPL_CNTL, 860c5deb1eSKrzysztof Kozlowski .rtc_udr_update = S2MPS_RTC_UDR_CON, 870c5deb1eSKrzysztof Kozlowski .rtc_udr_mask = S2MPS_RTC_WUDR_MASK, 880c5deb1eSKrzysztof Kozlowski }; 890c5deb1eSKrzysztof Kozlowski 905bccae6eSSangbeom Kim struct s5m_rtc_info { 915bccae6eSSangbeom Kim struct device *dev; 92e349c910SKrzysztof Kozlowski struct i2c_client *i2c; 935bccae6eSSangbeom Kim struct sec_pmic_dev *s5m87xx; 945ccb7d71SGeert Uytterhoeven struct regmap *regmap; 955bccae6eSSangbeom Kim struct rtc_device *rtc_dev; 965bccae6eSSangbeom Kim int irq; 975bccae6eSSangbeom Kim int device_type; 985bccae6eSSangbeom Kim int rtc_24hr_mode; 995bccae6eSSangbeom Kim bool wtsr_smpl; 100f8b23bbdSKrzysztof Kozlowski const struct s5m_rtc_reg_config *regs; 1015bccae6eSSangbeom Kim }; 1025bccae6eSSangbeom Kim 103e349c910SKrzysztof Kozlowski static const struct regmap_config s5m_rtc_regmap_config = { 104e349c910SKrzysztof Kozlowski .reg_bits = 8, 105e349c910SKrzysztof Kozlowski .val_bits = 8, 106e349c910SKrzysztof Kozlowski 107602cb5bbSKrzysztof Kozlowski .max_register = S5M_RTC_REG_MAX, 108e349c910SKrzysztof Kozlowski }; 109e349c910SKrzysztof Kozlowski 110e349c910SKrzysztof Kozlowski static const struct regmap_config s2mps14_rtc_regmap_config = { 111e349c910SKrzysztof Kozlowski .reg_bits = 8, 112e349c910SKrzysztof Kozlowski .val_bits = 8, 113e349c910SKrzysztof Kozlowski 114e349c910SKrzysztof Kozlowski .max_register = S2MPS_RTC_REG_MAX, 115e349c910SKrzysztof Kozlowski }; 116e349c910SKrzysztof Kozlowski 1175bccae6eSSangbeom Kim static void s5m8767_data_to_tm(u8 *data, struct rtc_time *tm, 1185bccae6eSSangbeom Kim int rtc_24hr_mode) 1195bccae6eSSangbeom Kim { 1205bccae6eSSangbeom Kim tm->tm_sec = data[RTC_SEC] & 0x7f; 1215bccae6eSSangbeom Kim tm->tm_min = data[RTC_MIN] & 0x7f; 1225bccae6eSSangbeom Kim if (rtc_24hr_mode) { 1235bccae6eSSangbeom Kim tm->tm_hour = data[RTC_HOUR] & 0x1f; 1245bccae6eSSangbeom Kim } else { 1255bccae6eSSangbeom Kim tm->tm_hour = data[RTC_HOUR] & 0x0f; 1265bccae6eSSangbeom Kim if (data[RTC_HOUR] & HOUR_PM_MASK) 1275bccae6eSSangbeom Kim tm->tm_hour += 12; 1285bccae6eSSangbeom Kim } 1295bccae6eSSangbeom Kim 1305bccae6eSSangbeom Kim tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f); 1315bccae6eSSangbeom Kim tm->tm_mday = data[RTC_DATE] & 0x1f; 1325bccae6eSSangbeom Kim tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1; 1335bccae6eSSangbeom Kim tm->tm_year = (data[RTC_YEAR1] & 0x7f) + 100; 1345bccae6eSSangbeom Kim tm->tm_yday = 0; 1355bccae6eSSangbeom Kim tm->tm_isdst = 0; 1365bccae6eSSangbeom Kim } 1375bccae6eSSangbeom Kim 1385bccae6eSSangbeom Kim static int s5m8767_tm_to_data(struct rtc_time *tm, u8 *data) 1395bccae6eSSangbeom Kim { 1405bccae6eSSangbeom Kim data[RTC_SEC] = tm->tm_sec; 1415bccae6eSSangbeom Kim data[RTC_MIN] = tm->tm_min; 1425bccae6eSSangbeom Kim 1435bccae6eSSangbeom Kim if (tm->tm_hour >= 12) 1445bccae6eSSangbeom Kim data[RTC_HOUR] = tm->tm_hour | HOUR_PM_MASK; 1455bccae6eSSangbeom Kim else 1465bccae6eSSangbeom Kim data[RTC_HOUR] = tm->tm_hour & ~HOUR_PM_MASK; 1475bccae6eSSangbeom Kim 1485bccae6eSSangbeom Kim data[RTC_WEEKDAY] = 1 << tm->tm_wday; 1495bccae6eSSangbeom Kim data[RTC_DATE] = tm->tm_mday; 1505bccae6eSSangbeom Kim data[RTC_MONTH] = tm->tm_mon + 1; 1515bccae6eSSangbeom Kim data[RTC_YEAR1] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0; 1525bccae6eSSangbeom Kim 1535bccae6eSSangbeom Kim if (tm->tm_year < 100) { 1545bccae6eSSangbeom Kim pr_err("s5m8767 RTC cannot handle the year %d.\n", 1555bccae6eSSangbeom Kim 1900 + tm->tm_year); 1565bccae6eSSangbeom Kim return -EINVAL; 1575bccae6eSSangbeom Kim } else { 1585bccae6eSSangbeom Kim return 0; 1595bccae6eSSangbeom Kim } 1605bccae6eSSangbeom Kim } 1615bccae6eSSangbeom Kim 162d73238d4SKrzysztof Kozlowski /* 163d73238d4SKrzysztof Kozlowski * Read RTC_UDR_CON register and wait till UDR field is cleared. 164d73238d4SKrzysztof Kozlowski * This indicates that time/alarm update ended. 165d73238d4SKrzysztof Kozlowski */ 166d73238d4SKrzysztof Kozlowski static inline int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info) 167d73238d4SKrzysztof Kozlowski { 168d73238d4SKrzysztof Kozlowski int ret, retry = UDR_READ_RETRY_CNT; 169d73238d4SKrzysztof Kozlowski unsigned int data; 170d73238d4SKrzysztof Kozlowski 171d73238d4SKrzysztof Kozlowski do { 172f8b23bbdSKrzysztof Kozlowski ret = regmap_read(info->regmap, info->regs->rtc_udr_update, 173f8b23bbdSKrzysztof Kozlowski &data); 174f8b23bbdSKrzysztof Kozlowski } while (--retry && (data & info->regs->rtc_udr_mask) && !ret); 175d73238d4SKrzysztof Kozlowski 176d73238d4SKrzysztof Kozlowski if (!retry) 177d73238d4SKrzysztof Kozlowski dev_err(info->dev, "waiting for UDR update, reached max number of retries\n"); 178d73238d4SKrzysztof Kozlowski 179d73238d4SKrzysztof Kozlowski return ret; 180d73238d4SKrzysztof Kozlowski } 181d73238d4SKrzysztof Kozlowski 182f8b23bbdSKrzysztof Kozlowski static inline int s5m_check_peding_alarm_interrupt(struct s5m_rtc_info *info, 183f8b23bbdSKrzysztof Kozlowski struct rtc_wkalrm *alarm) 184f8b23bbdSKrzysztof Kozlowski { 185f8b23bbdSKrzysztof Kozlowski int ret; 186f8b23bbdSKrzysztof Kozlowski unsigned int val; 187f8b23bbdSKrzysztof Kozlowski 188f8b23bbdSKrzysztof Kozlowski switch (info->device_type) { 189f8b23bbdSKrzysztof Kozlowski case S5M8767X: 190f8b23bbdSKrzysztof Kozlowski case S5M8763X: 191f8b23bbdSKrzysztof Kozlowski ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val); 192f8b23bbdSKrzysztof Kozlowski val &= S5M_ALARM0_STATUS; 193f8b23bbdSKrzysztof Kozlowski break; 1940c5deb1eSKrzysztof Kozlowski case S2MPS14X: 1950c5deb1eSKrzysztof Kozlowski ret = regmap_read(info->s5m87xx->regmap_pmic, S2MPS14_REG_ST2, 1960c5deb1eSKrzysztof Kozlowski &val); 1970c5deb1eSKrzysztof Kozlowski val &= S2MPS_ALARM0_STATUS; 1980c5deb1eSKrzysztof Kozlowski break; 199f8b23bbdSKrzysztof Kozlowski default: 200f8b23bbdSKrzysztof Kozlowski return -EINVAL; 201f8b23bbdSKrzysztof Kozlowski } 202f8b23bbdSKrzysztof Kozlowski if (ret < 0) 203f8b23bbdSKrzysztof Kozlowski return ret; 204f8b23bbdSKrzysztof Kozlowski 205f8b23bbdSKrzysztof Kozlowski if (val) 206f8b23bbdSKrzysztof Kozlowski alarm->pending = 1; 207f8b23bbdSKrzysztof Kozlowski else 208f8b23bbdSKrzysztof Kozlowski alarm->pending = 0; 209f8b23bbdSKrzysztof Kozlowski 210f8b23bbdSKrzysztof Kozlowski return 0; 211f8b23bbdSKrzysztof Kozlowski } 212f8b23bbdSKrzysztof Kozlowski 2135bccae6eSSangbeom Kim static inline int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info) 2145bccae6eSSangbeom Kim { 2155bccae6eSSangbeom Kim int ret; 2165bccae6eSSangbeom Kim unsigned int data; 2175bccae6eSSangbeom Kim 218f8b23bbdSKrzysztof Kozlowski ret = regmap_read(info->regmap, info->regs->rtc_udr_update, &data); 2195bccae6eSSangbeom Kim if (ret < 0) { 2205bccae6eSSangbeom Kim dev_err(info->dev, "failed to read update reg(%d)\n", ret); 2215bccae6eSSangbeom Kim return ret; 2225bccae6eSSangbeom Kim } 2235bccae6eSSangbeom Kim 224f8b23bbdSKrzysztof Kozlowski data |= info->regs->rtc_udr_mask; 2250c5deb1eSKrzysztof Kozlowski if (info->device_type == S5M8763X || info->device_type == S5M8767X) 2260c5deb1eSKrzysztof Kozlowski data |= S5M_RTC_TIME_EN_MASK; 2275bccae6eSSangbeom Kim 228f8b23bbdSKrzysztof Kozlowski ret = regmap_write(info->regmap, info->regs->rtc_udr_update, data); 2295bccae6eSSangbeom Kim if (ret < 0) { 2305bccae6eSSangbeom Kim dev_err(info->dev, "failed to write update reg(%d)\n", ret); 2315bccae6eSSangbeom Kim return ret; 2325bccae6eSSangbeom Kim } 2335bccae6eSSangbeom Kim 234d73238d4SKrzysztof Kozlowski ret = s5m8767_wait_for_udr_update(info); 2355bccae6eSSangbeom Kim 2365bccae6eSSangbeom Kim return ret; 2375bccae6eSSangbeom Kim } 2385bccae6eSSangbeom Kim 2395bccae6eSSangbeom Kim static inline int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info) 2405bccae6eSSangbeom Kim { 2415bccae6eSSangbeom Kim int ret; 2425bccae6eSSangbeom Kim unsigned int data; 2435bccae6eSSangbeom Kim 244f8b23bbdSKrzysztof Kozlowski ret = regmap_read(info->regmap, info->regs->rtc_udr_update, &data); 2455bccae6eSSangbeom Kim if (ret < 0) { 2465bccae6eSSangbeom Kim dev_err(info->dev, "%s: fail to read update reg(%d)\n", 2475bccae6eSSangbeom Kim __func__, ret); 2485bccae6eSSangbeom Kim return ret; 2495bccae6eSSangbeom Kim } 2505bccae6eSSangbeom Kim 251f8b23bbdSKrzysztof Kozlowski data |= info->regs->rtc_udr_mask; 2520c5deb1eSKrzysztof Kozlowski switch (info->device_type) { 2530c5deb1eSKrzysztof Kozlowski case S5M8763X: 2540c5deb1eSKrzysztof Kozlowski case S5M8767X: 2550c5deb1eSKrzysztof Kozlowski data &= ~S5M_RTC_TIME_EN_MASK; 2560c5deb1eSKrzysztof Kozlowski break; 2570c5deb1eSKrzysztof Kozlowski case S2MPS14X: 2580c5deb1eSKrzysztof Kozlowski data |= S2MPS_RTC_RUDR_MASK; 2590c5deb1eSKrzysztof Kozlowski break; 2600c5deb1eSKrzysztof Kozlowski default: 2610c5deb1eSKrzysztof Kozlowski return -EINVAL; 2620c5deb1eSKrzysztof Kozlowski } 2635bccae6eSSangbeom Kim 264f8b23bbdSKrzysztof Kozlowski ret = regmap_write(info->regmap, info->regs->rtc_udr_update, data); 2655bccae6eSSangbeom Kim if (ret < 0) { 2665bccae6eSSangbeom Kim dev_err(info->dev, "%s: fail to write update reg(%d)\n", 2675bccae6eSSangbeom Kim __func__, ret); 2685bccae6eSSangbeom Kim return ret; 2695bccae6eSSangbeom Kim } 2705bccae6eSSangbeom Kim 271d73238d4SKrzysztof Kozlowski ret = s5m8767_wait_for_udr_update(info); 2725bccae6eSSangbeom Kim 2735bccae6eSSangbeom Kim return ret; 2745bccae6eSSangbeom Kim } 2755bccae6eSSangbeom Kim 2765bccae6eSSangbeom Kim static void s5m8763_data_to_tm(u8 *data, struct rtc_time *tm) 2775bccae6eSSangbeom Kim { 2785bccae6eSSangbeom Kim tm->tm_sec = bcd2bin(data[RTC_SEC]); 2795bccae6eSSangbeom Kim tm->tm_min = bcd2bin(data[RTC_MIN]); 2805bccae6eSSangbeom Kim 2815bccae6eSSangbeom Kim if (data[RTC_HOUR] & HOUR_12) { 2825bccae6eSSangbeom Kim tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f); 2835bccae6eSSangbeom Kim if (data[RTC_HOUR] & HOUR_PM) 2845bccae6eSSangbeom Kim tm->tm_hour += 12; 2855bccae6eSSangbeom Kim } else { 2865bccae6eSSangbeom Kim tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f); 2875bccae6eSSangbeom Kim } 2885bccae6eSSangbeom Kim 2895bccae6eSSangbeom Kim tm->tm_wday = data[RTC_WEEKDAY] & 0x07; 2905bccae6eSSangbeom Kim tm->tm_mday = bcd2bin(data[RTC_DATE]); 2915bccae6eSSangbeom Kim tm->tm_mon = bcd2bin(data[RTC_MONTH]); 2925bccae6eSSangbeom Kim tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100; 2935bccae6eSSangbeom Kim tm->tm_year -= 1900; 2945bccae6eSSangbeom Kim } 2955bccae6eSSangbeom Kim 2965bccae6eSSangbeom Kim static void s5m8763_tm_to_data(struct rtc_time *tm, u8 *data) 2975bccae6eSSangbeom Kim { 2985bccae6eSSangbeom Kim data[RTC_SEC] = bin2bcd(tm->tm_sec); 2995bccae6eSSangbeom Kim data[RTC_MIN] = bin2bcd(tm->tm_min); 3005bccae6eSSangbeom Kim data[RTC_HOUR] = bin2bcd(tm->tm_hour); 3015bccae6eSSangbeom Kim data[RTC_WEEKDAY] = tm->tm_wday; 3025bccae6eSSangbeom Kim data[RTC_DATE] = bin2bcd(tm->tm_mday); 3035bccae6eSSangbeom Kim data[RTC_MONTH] = bin2bcd(tm->tm_mon); 3045bccae6eSSangbeom Kim data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100); 3055bccae6eSSangbeom Kim data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100); 3065bccae6eSSangbeom Kim } 3075bccae6eSSangbeom Kim 3085bccae6eSSangbeom Kim static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm) 3095bccae6eSSangbeom Kim { 3105bccae6eSSangbeom Kim struct s5m_rtc_info *info = dev_get_drvdata(dev); 311f8b23bbdSKrzysztof Kozlowski u8 data[info->regs->regs_count]; 3125bccae6eSSangbeom Kim int ret; 3135bccae6eSSangbeom Kim 3140c5deb1eSKrzysztof Kozlowski if (info->device_type == S2MPS14X) { 3150c5deb1eSKrzysztof Kozlowski ret = regmap_update_bits(info->regmap, 3160c5deb1eSKrzysztof Kozlowski info->regs->rtc_udr_update, 3170c5deb1eSKrzysztof Kozlowski S2MPS_RTC_RUDR_MASK, S2MPS_RTC_RUDR_MASK); 3180c5deb1eSKrzysztof Kozlowski if (ret) { 3190c5deb1eSKrzysztof Kozlowski dev_err(dev, 3200c5deb1eSKrzysztof Kozlowski "Failed to prepare registers for time reading: %d\n", 3210c5deb1eSKrzysztof Kozlowski ret); 3220c5deb1eSKrzysztof Kozlowski return ret; 3230c5deb1eSKrzysztof Kozlowski } 3240c5deb1eSKrzysztof Kozlowski } 325f8b23bbdSKrzysztof Kozlowski ret = regmap_bulk_read(info->regmap, info->regs->time, data, 326f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 3275bccae6eSSangbeom Kim if (ret < 0) 3285bccae6eSSangbeom Kim return ret; 3295bccae6eSSangbeom Kim 3305bccae6eSSangbeom Kim switch (info->device_type) { 3315bccae6eSSangbeom Kim case S5M8763X: 3325bccae6eSSangbeom Kim s5m8763_data_to_tm(data, tm); 3335bccae6eSSangbeom Kim break; 3345bccae6eSSangbeom Kim 3355bccae6eSSangbeom Kim case S5M8767X: 3360c5deb1eSKrzysztof Kozlowski case S2MPS14X: 3375bccae6eSSangbeom Kim s5m8767_data_to_tm(data, tm, info->rtc_24hr_mode); 3385bccae6eSSangbeom Kim break; 3395bccae6eSSangbeom Kim 3405bccae6eSSangbeom Kim default: 3415bccae6eSSangbeom Kim return -EINVAL; 3425bccae6eSSangbeom Kim } 3435bccae6eSSangbeom Kim 3445bccae6eSSangbeom Kim dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, 3455bccae6eSSangbeom Kim 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, 3465bccae6eSSangbeom Kim tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday); 3475bccae6eSSangbeom Kim 3485bccae6eSSangbeom Kim return rtc_valid_tm(tm); 3495bccae6eSSangbeom Kim } 3505bccae6eSSangbeom Kim 3515bccae6eSSangbeom Kim static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm) 3525bccae6eSSangbeom Kim { 3535bccae6eSSangbeom Kim struct s5m_rtc_info *info = dev_get_drvdata(dev); 354f8b23bbdSKrzysztof Kozlowski u8 data[info->regs->regs_count]; 3555bccae6eSSangbeom Kim int ret = 0; 3565bccae6eSSangbeom Kim 3575bccae6eSSangbeom Kim switch (info->device_type) { 3585bccae6eSSangbeom Kim case S5M8763X: 3595bccae6eSSangbeom Kim s5m8763_tm_to_data(tm, data); 3605bccae6eSSangbeom Kim break; 3615bccae6eSSangbeom Kim case S5M8767X: 3620c5deb1eSKrzysztof Kozlowski case S2MPS14X: 3635bccae6eSSangbeom Kim ret = s5m8767_tm_to_data(tm, data); 3645bccae6eSSangbeom Kim break; 3655bccae6eSSangbeom Kim default: 3665bccae6eSSangbeom Kim return -EINVAL; 3675bccae6eSSangbeom Kim } 3685bccae6eSSangbeom Kim 3695bccae6eSSangbeom Kim if (ret < 0) 3705bccae6eSSangbeom Kim return ret; 3715bccae6eSSangbeom Kim 3725bccae6eSSangbeom Kim dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, 3735bccae6eSSangbeom Kim 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, 3745bccae6eSSangbeom Kim tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday); 3755bccae6eSSangbeom Kim 376f8b23bbdSKrzysztof Kozlowski ret = regmap_raw_write(info->regmap, info->regs->time, data, 377f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 3785bccae6eSSangbeom Kim if (ret < 0) 3795bccae6eSSangbeom Kim return ret; 3805bccae6eSSangbeom Kim 3815bccae6eSSangbeom Kim ret = s5m8767_rtc_set_time_reg(info); 3825bccae6eSSangbeom Kim 3835bccae6eSSangbeom Kim return ret; 3845bccae6eSSangbeom Kim } 3855bccae6eSSangbeom Kim 3865bccae6eSSangbeom Kim static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 3875bccae6eSSangbeom Kim { 3885bccae6eSSangbeom Kim struct s5m_rtc_info *info = dev_get_drvdata(dev); 389f8b23bbdSKrzysztof Kozlowski u8 data[info->regs->regs_count]; 3905bccae6eSSangbeom Kim unsigned int val; 3915bccae6eSSangbeom Kim int ret, i; 3925bccae6eSSangbeom Kim 393f8b23bbdSKrzysztof Kozlowski ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data, 394f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 3955bccae6eSSangbeom Kim if (ret < 0) 3965bccae6eSSangbeom Kim return ret; 3975bccae6eSSangbeom Kim 3985bccae6eSSangbeom Kim switch (info->device_type) { 3995bccae6eSSangbeom Kim case S5M8763X: 4005bccae6eSSangbeom Kim s5m8763_data_to_tm(data, &alrm->time); 401602cb5bbSKrzysztof Kozlowski ret = regmap_read(info->regmap, S5M_ALARM0_CONF, &val); 4025bccae6eSSangbeom Kim if (ret < 0) 4035bccae6eSSangbeom Kim return ret; 4045bccae6eSSangbeom Kim 4055bccae6eSSangbeom Kim alrm->enabled = !!val; 4065bccae6eSSangbeom Kim break; 4075bccae6eSSangbeom Kim 4085bccae6eSSangbeom Kim case S5M8767X: 4090c5deb1eSKrzysztof Kozlowski case S2MPS14X: 4105bccae6eSSangbeom Kim s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode); 4115bccae6eSSangbeom Kim alrm->enabled = 0; 412f8b23bbdSKrzysztof Kozlowski for (i = 0; i < info->regs->regs_count; i++) { 4135bccae6eSSangbeom Kim if (data[i] & ALARM_ENABLE_MASK) { 4145bccae6eSSangbeom Kim alrm->enabled = 1; 4155bccae6eSSangbeom Kim break; 4165bccae6eSSangbeom Kim } 4175bccae6eSSangbeom Kim } 4185bccae6eSSangbeom Kim break; 4195bccae6eSSangbeom Kim 4205bccae6eSSangbeom Kim default: 4215bccae6eSSangbeom Kim return -EINVAL; 4225bccae6eSSangbeom Kim } 4235bccae6eSSangbeom Kim 424f8b23bbdSKrzysztof Kozlowski dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, 425f8b23bbdSKrzysztof Kozlowski 1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon, 426f8b23bbdSKrzysztof Kozlowski alrm->time.tm_mday, alrm->time.tm_hour, 427f8b23bbdSKrzysztof Kozlowski alrm->time.tm_min, alrm->time.tm_sec, 428f8b23bbdSKrzysztof Kozlowski alrm->time.tm_wday); 429f8b23bbdSKrzysztof Kozlowski 430f8b23bbdSKrzysztof Kozlowski ret = s5m_check_peding_alarm_interrupt(info, alrm); 4315bccae6eSSangbeom Kim 4325bccae6eSSangbeom Kim return 0; 4335bccae6eSSangbeom Kim } 4345bccae6eSSangbeom Kim 4355bccae6eSSangbeom Kim static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info) 4365bccae6eSSangbeom Kim { 437f8b23bbdSKrzysztof Kozlowski u8 data[info->regs->regs_count]; 4385bccae6eSSangbeom Kim int ret, i; 4395bccae6eSSangbeom Kim struct rtc_time tm; 4405bccae6eSSangbeom Kim 441f8b23bbdSKrzysztof Kozlowski ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data, 442f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 4435bccae6eSSangbeom Kim if (ret < 0) 4445bccae6eSSangbeom Kim return ret; 4455bccae6eSSangbeom Kim 4465bccae6eSSangbeom Kim s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode); 4475bccae6eSSangbeom Kim dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, 4485bccae6eSSangbeom Kim 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday, 4495bccae6eSSangbeom Kim tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday); 4505bccae6eSSangbeom Kim 4515bccae6eSSangbeom Kim switch (info->device_type) { 4525bccae6eSSangbeom Kim case S5M8763X: 453602cb5bbSKrzysztof Kozlowski ret = regmap_write(info->regmap, S5M_ALARM0_CONF, 0); 4545bccae6eSSangbeom Kim break; 4555bccae6eSSangbeom Kim 4565bccae6eSSangbeom Kim case S5M8767X: 4570c5deb1eSKrzysztof Kozlowski case S2MPS14X: 458f8b23bbdSKrzysztof Kozlowski for (i = 0; i < info->regs->regs_count; i++) 4595bccae6eSSangbeom Kim data[i] &= ~ALARM_ENABLE_MASK; 4605bccae6eSSangbeom Kim 461f8b23bbdSKrzysztof Kozlowski ret = regmap_raw_write(info->regmap, info->regs->alarm0, data, 462f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 4635bccae6eSSangbeom Kim if (ret < 0) 4645bccae6eSSangbeom Kim return ret; 4655bccae6eSSangbeom Kim 4665bccae6eSSangbeom Kim ret = s5m8767_rtc_set_alarm_reg(info); 4675bccae6eSSangbeom Kim 4685bccae6eSSangbeom Kim break; 4695bccae6eSSangbeom Kim 4705bccae6eSSangbeom Kim default: 4715bccae6eSSangbeom Kim return -EINVAL; 4725bccae6eSSangbeom Kim } 4735bccae6eSSangbeom Kim 4745bccae6eSSangbeom Kim return ret; 4755bccae6eSSangbeom Kim } 4765bccae6eSSangbeom Kim 4775bccae6eSSangbeom Kim static int s5m_rtc_start_alarm(struct s5m_rtc_info *info) 4785bccae6eSSangbeom Kim { 4795bccae6eSSangbeom Kim int ret; 480f8b23bbdSKrzysztof Kozlowski u8 data[info->regs->regs_count]; 4815bccae6eSSangbeom Kim u8 alarm0_conf; 4825bccae6eSSangbeom Kim struct rtc_time tm; 4835bccae6eSSangbeom Kim 484f8b23bbdSKrzysztof Kozlowski ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data, 485f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 4865bccae6eSSangbeom Kim if (ret < 0) 4875bccae6eSSangbeom Kim return ret; 4885bccae6eSSangbeom Kim 4895bccae6eSSangbeom Kim s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode); 4905bccae6eSSangbeom Kim dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, 4915bccae6eSSangbeom Kim 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday, 4925bccae6eSSangbeom Kim tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday); 4935bccae6eSSangbeom Kim 4945bccae6eSSangbeom Kim switch (info->device_type) { 4955bccae6eSSangbeom Kim case S5M8763X: 4965bccae6eSSangbeom Kim alarm0_conf = 0x77; 497602cb5bbSKrzysztof Kozlowski ret = regmap_write(info->regmap, S5M_ALARM0_CONF, alarm0_conf); 4985bccae6eSSangbeom Kim break; 4995bccae6eSSangbeom Kim 5005bccae6eSSangbeom Kim case S5M8767X: 5010c5deb1eSKrzysztof Kozlowski case S2MPS14X: 5025bccae6eSSangbeom Kim data[RTC_SEC] |= ALARM_ENABLE_MASK; 5035bccae6eSSangbeom Kim data[RTC_MIN] |= ALARM_ENABLE_MASK; 5045bccae6eSSangbeom Kim data[RTC_HOUR] |= ALARM_ENABLE_MASK; 5055bccae6eSSangbeom Kim data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK; 5065bccae6eSSangbeom Kim if (data[RTC_DATE] & 0x1f) 5075bccae6eSSangbeom Kim data[RTC_DATE] |= ALARM_ENABLE_MASK; 5085bccae6eSSangbeom Kim if (data[RTC_MONTH] & 0xf) 5095bccae6eSSangbeom Kim data[RTC_MONTH] |= ALARM_ENABLE_MASK; 5105bccae6eSSangbeom Kim if (data[RTC_YEAR1] & 0x7f) 5115bccae6eSSangbeom Kim data[RTC_YEAR1] |= ALARM_ENABLE_MASK; 5125bccae6eSSangbeom Kim 513f8b23bbdSKrzysztof Kozlowski ret = regmap_raw_write(info->regmap, info->regs->alarm0, data, 514f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 5155bccae6eSSangbeom Kim if (ret < 0) 5165bccae6eSSangbeom Kim return ret; 5175bccae6eSSangbeom Kim ret = s5m8767_rtc_set_alarm_reg(info); 5185bccae6eSSangbeom Kim 5195bccae6eSSangbeom Kim break; 5205bccae6eSSangbeom Kim 5215bccae6eSSangbeom Kim default: 5225bccae6eSSangbeom Kim return -EINVAL; 5235bccae6eSSangbeom Kim } 5245bccae6eSSangbeom Kim 5255bccae6eSSangbeom Kim return ret; 5265bccae6eSSangbeom Kim } 5275bccae6eSSangbeom Kim 5285bccae6eSSangbeom Kim static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 5295bccae6eSSangbeom Kim { 5305bccae6eSSangbeom Kim struct s5m_rtc_info *info = dev_get_drvdata(dev); 531f8b23bbdSKrzysztof Kozlowski u8 data[info->regs->regs_count]; 5325bccae6eSSangbeom Kim int ret; 5335bccae6eSSangbeom Kim 5345bccae6eSSangbeom Kim switch (info->device_type) { 5355bccae6eSSangbeom Kim case S5M8763X: 5365bccae6eSSangbeom Kim s5m8763_tm_to_data(&alrm->time, data); 5375bccae6eSSangbeom Kim break; 5385bccae6eSSangbeom Kim 5395bccae6eSSangbeom Kim case S5M8767X: 5400c5deb1eSKrzysztof Kozlowski case S2MPS14X: 5415bccae6eSSangbeom Kim s5m8767_tm_to_data(&alrm->time, data); 5425bccae6eSSangbeom Kim break; 5435bccae6eSSangbeom Kim 5445bccae6eSSangbeom Kim default: 5455bccae6eSSangbeom Kim return -EINVAL; 5465bccae6eSSangbeom Kim } 5475bccae6eSSangbeom Kim 5485bccae6eSSangbeom Kim dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__, 5495bccae6eSSangbeom Kim 1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon, 5505bccae6eSSangbeom Kim alrm->time.tm_mday, alrm->time.tm_hour, alrm->time.tm_min, 5515bccae6eSSangbeom Kim alrm->time.tm_sec, alrm->time.tm_wday); 5525bccae6eSSangbeom Kim 5535bccae6eSSangbeom Kim ret = s5m_rtc_stop_alarm(info); 5545bccae6eSSangbeom Kim if (ret < 0) 5555bccae6eSSangbeom Kim return ret; 5565bccae6eSSangbeom Kim 557f8b23bbdSKrzysztof Kozlowski ret = regmap_raw_write(info->regmap, info->regs->alarm0, data, 558f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 5595bccae6eSSangbeom Kim if (ret < 0) 5605bccae6eSSangbeom Kim return ret; 5615bccae6eSSangbeom Kim 5625bccae6eSSangbeom Kim ret = s5m8767_rtc_set_alarm_reg(info); 5635bccae6eSSangbeom Kim if (ret < 0) 5645bccae6eSSangbeom Kim return ret; 5655bccae6eSSangbeom Kim 5665bccae6eSSangbeom Kim if (alrm->enabled) 5675bccae6eSSangbeom Kim ret = s5m_rtc_start_alarm(info); 5685bccae6eSSangbeom Kim 5695bccae6eSSangbeom Kim return ret; 5705bccae6eSSangbeom Kim } 5715bccae6eSSangbeom Kim 5725bccae6eSSangbeom Kim static int s5m_rtc_alarm_irq_enable(struct device *dev, 5735bccae6eSSangbeom Kim unsigned int enabled) 5745bccae6eSSangbeom Kim { 5755bccae6eSSangbeom Kim struct s5m_rtc_info *info = dev_get_drvdata(dev); 5765bccae6eSSangbeom Kim 5775bccae6eSSangbeom Kim if (enabled) 5785bccae6eSSangbeom Kim return s5m_rtc_start_alarm(info); 5795bccae6eSSangbeom Kim else 5805bccae6eSSangbeom Kim return s5m_rtc_stop_alarm(info); 5815bccae6eSSangbeom Kim } 5825bccae6eSSangbeom Kim 5835bccae6eSSangbeom Kim static irqreturn_t s5m_rtc_alarm_irq(int irq, void *data) 5845bccae6eSSangbeom Kim { 5855bccae6eSSangbeom Kim struct s5m_rtc_info *info = data; 5865bccae6eSSangbeom Kim 5875bccae6eSSangbeom Kim rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF); 5885bccae6eSSangbeom Kim 5895bccae6eSSangbeom Kim return IRQ_HANDLED; 5905bccae6eSSangbeom Kim } 5915bccae6eSSangbeom Kim 5925bccae6eSSangbeom Kim static const struct rtc_class_ops s5m_rtc_ops = { 5935bccae6eSSangbeom Kim .read_time = s5m_rtc_read_time, 5945bccae6eSSangbeom Kim .set_time = s5m_rtc_set_time, 5955bccae6eSSangbeom Kim .read_alarm = s5m_rtc_read_alarm, 5965bccae6eSSangbeom Kim .set_alarm = s5m_rtc_set_alarm, 5975bccae6eSSangbeom Kim .alarm_irq_enable = s5m_rtc_alarm_irq_enable, 5985bccae6eSSangbeom Kim }; 5995bccae6eSSangbeom Kim 6005bccae6eSSangbeom Kim static void s5m_rtc_enable_wtsr(struct s5m_rtc_info *info, bool enable) 6015bccae6eSSangbeom Kim { 6025bccae6eSSangbeom Kim int ret; 603f8b23bbdSKrzysztof Kozlowski ret = regmap_update_bits(info->regmap, info->regs->smpl_wtsr, 6045bccae6eSSangbeom Kim WTSR_ENABLE_MASK, 6055bccae6eSSangbeom Kim enable ? WTSR_ENABLE_MASK : 0); 6065bccae6eSSangbeom Kim if (ret < 0) 6075bccae6eSSangbeom Kim dev_err(info->dev, "%s: fail to update WTSR reg(%d)\n", 6085bccae6eSSangbeom Kim __func__, ret); 6095bccae6eSSangbeom Kim } 6105bccae6eSSangbeom Kim 6115bccae6eSSangbeom Kim static void s5m_rtc_enable_smpl(struct s5m_rtc_info *info, bool enable) 6125bccae6eSSangbeom Kim { 6135bccae6eSSangbeom Kim int ret; 614f8b23bbdSKrzysztof Kozlowski ret = regmap_update_bits(info->regmap, info->regs->smpl_wtsr, 6155bccae6eSSangbeom Kim SMPL_ENABLE_MASK, 6165bccae6eSSangbeom Kim enable ? SMPL_ENABLE_MASK : 0); 6175bccae6eSSangbeom Kim if (ret < 0) 6185bccae6eSSangbeom Kim dev_err(info->dev, "%s: fail to update SMPL reg(%d)\n", 6195bccae6eSSangbeom Kim __func__, ret); 6205bccae6eSSangbeom Kim } 6215bccae6eSSangbeom Kim 6225bccae6eSSangbeom Kim static int s5m8767_rtc_init_reg(struct s5m_rtc_info *info) 6235bccae6eSSangbeom Kim { 6245bccae6eSSangbeom Kim u8 data[2]; 6255bccae6eSSangbeom Kim int ret; 6265bccae6eSSangbeom Kim 6270c5deb1eSKrzysztof Kozlowski switch (info->device_type) { 6280c5deb1eSKrzysztof Kozlowski case S5M8763X: 6290c5deb1eSKrzysztof Kozlowski case S5M8767X: 6300c5f5d9aSKrzysztof Kozlowski /* UDR update time. Default of 7.32 ms is too long. */ 6310c5f5d9aSKrzysztof Kozlowski ret = regmap_update_bits(info->regmap, S5M_RTC_UDR_CON, 6320c5f5d9aSKrzysztof Kozlowski S5M_RTC_UDR_T_MASK, S5M_RTC_UDR_T_450_US); 6330c5f5d9aSKrzysztof Kozlowski if (ret < 0) 6340c5f5d9aSKrzysztof Kozlowski dev_err(info->dev, "%s: fail to change UDR time: %d\n", 6350c5f5d9aSKrzysztof Kozlowski __func__, ret); 6360c5f5d9aSKrzysztof Kozlowski 6375bccae6eSSangbeom Kim /* Set RTC control register : Binary mode, 24hour mode */ 6385bccae6eSSangbeom Kim data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); 6395bccae6eSSangbeom Kim data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); 6405bccae6eSSangbeom Kim 641602cb5bbSKrzysztof Kozlowski ret = regmap_raw_write(info->regmap, S5M_ALARM0_CONF, data, 2); 6420c5deb1eSKrzysztof Kozlowski break; 6430c5deb1eSKrzysztof Kozlowski 6440c5deb1eSKrzysztof Kozlowski case S2MPS14X: 6450c5deb1eSKrzysztof Kozlowski data[0] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); 6460c5deb1eSKrzysztof Kozlowski ret = regmap_write(info->regmap, info->regs->ctrl, data[0]); 6470c5deb1eSKrzysztof Kozlowski break; 6480c5deb1eSKrzysztof Kozlowski 6490c5deb1eSKrzysztof Kozlowski default: 6500c5deb1eSKrzysztof Kozlowski return -EINVAL; 6510c5deb1eSKrzysztof Kozlowski } 6520c5deb1eSKrzysztof Kozlowski 6530c5deb1eSKrzysztof Kozlowski info->rtc_24hr_mode = 1; 6545bccae6eSSangbeom Kim if (ret < 0) { 6555bccae6eSSangbeom Kim dev_err(info->dev, "%s: fail to write controlm reg(%d)\n", 6565bccae6eSSangbeom Kim __func__, ret); 6575bccae6eSSangbeom Kim return ret; 6585bccae6eSSangbeom Kim } 6595bccae6eSSangbeom Kim 6605bccae6eSSangbeom Kim return ret; 6615bccae6eSSangbeom Kim } 6625bccae6eSSangbeom Kim 6635bccae6eSSangbeom Kim static int s5m_rtc_probe(struct platform_device *pdev) 6645bccae6eSSangbeom Kim { 6655bccae6eSSangbeom Kim struct sec_pmic_dev *s5m87xx = dev_get_drvdata(pdev->dev.parent); 6665bccae6eSSangbeom Kim struct sec_platform_data *pdata = s5m87xx->pdata; 6675bccae6eSSangbeom Kim struct s5m_rtc_info *info; 668e349c910SKrzysztof Kozlowski const struct regmap_config *regmap_cfg; 669*a0347f20SKrzysztof Kozlowski int ret, alarm_irq; 6705bccae6eSSangbeom Kim 6715bccae6eSSangbeom Kim if (!pdata) { 6725bccae6eSSangbeom Kim dev_err(pdev->dev.parent, "Platform data not supplied\n"); 6735bccae6eSSangbeom Kim return -ENODEV; 6745bccae6eSSangbeom Kim } 6755bccae6eSSangbeom Kim 6765bccae6eSSangbeom Kim info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); 6775bccae6eSSangbeom Kim if (!info) 6785bccae6eSSangbeom Kim return -ENOMEM; 6795bccae6eSSangbeom Kim 680e349c910SKrzysztof Kozlowski switch (pdata->device_type) { 681e349c910SKrzysztof Kozlowski case S2MPS14X: 682e349c910SKrzysztof Kozlowski regmap_cfg = &s2mps14_rtc_regmap_config; 6830c5deb1eSKrzysztof Kozlowski info->regs = &s2mps_rtc_regs; 684*a0347f20SKrzysztof Kozlowski alarm_irq = S2MPS14_IRQ_RTCA0; 685e349c910SKrzysztof Kozlowski break; 686e349c910SKrzysztof Kozlowski case S5M8763X: 687e349c910SKrzysztof Kozlowski regmap_cfg = &s5m_rtc_regmap_config; 688f8b23bbdSKrzysztof Kozlowski info->regs = &s5m_rtc_regs; 689*a0347f20SKrzysztof Kozlowski alarm_irq = S5M8763_IRQ_ALARM0; 690e349c910SKrzysztof Kozlowski break; 691e349c910SKrzysztof Kozlowski case S5M8767X: 692e349c910SKrzysztof Kozlowski regmap_cfg = &s5m_rtc_regmap_config; 693f8b23bbdSKrzysztof Kozlowski info->regs = &s5m_rtc_regs; 694*a0347f20SKrzysztof Kozlowski alarm_irq = S5M8767_IRQ_RTCA1; 695e349c910SKrzysztof Kozlowski break; 696e349c910SKrzysztof Kozlowski default: 697e349c910SKrzysztof Kozlowski dev_err(&pdev->dev, "Device type is not supported by RTC driver\n"); 698e349c910SKrzysztof Kozlowski return -ENODEV; 699e349c910SKrzysztof Kozlowski } 700e349c910SKrzysztof Kozlowski 701e349c910SKrzysztof Kozlowski info->i2c = i2c_new_dummy(s5m87xx->i2c->adapter, RTC_I2C_ADDR); 702e349c910SKrzysztof Kozlowski if (!info->i2c) { 703e349c910SKrzysztof Kozlowski dev_err(&pdev->dev, "Failed to allocate I2C for RTC\n"); 704e349c910SKrzysztof Kozlowski return -ENODEV; 705e349c910SKrzysztof Kozlowski } 706e349c910SKrzysztof Kozlowski 707e349c910SKrzysztof Kozlowski info->regmap = devm_regmap_init_i2c(info->i2c, regmap_cfg); 708e349c910SKrzysztof Kozlowski if (IS_ERR(info->regmap)) { 709e349c910SKrzysztof Kozlowski ret = PTR_ERR(info->regmap); 710e349c910SKrzysztof Kozlowski dev_err(&pdev->dev, "Failed to allocate RTC register map: %d\n", 711e349c910SKrzysztof Kozlowski ret); 712e349c910SKrzysztof Kozlowski goto err; 713e349c910SKrzysztof Kozlowski } 714e349c910SKrzysztof Kozlowski 7155bccae6eSSangbeom Kim info->dev = &pdev->dev; 7165bccae6eSSangbeom Kim info->s5m87xx = s5m87xx; 7175bccae6eSSangbeom Kim info->device_type = s5m87xx->device_type; 7185bccae6eSSangbeom Kim info->wtsr_smpl = s5m87xx->wtsr_smpl; 7195bccae6eSSangbeom Kim 720*a0347f20SKrzysztof Kozlowski info->irq = regmap_irq_get_virq(s5m87xx->irq_data, alarm_irq); 721*a0347f20SKrzysztof Kozlowski if (info->irq <= 0) { 7225bccae6eSSangbeom Kim ret = -EINVAL; 723*a0347f20SKrzysztof Kozlowski dev_err(&pdev->dev, "Failed to get virtual IRQ %d\n", 724*a0347f20SKrzysztof Kozlowski alarm_irq); 725e349c910SKrzysztof Kozlowski goto err; 7265bccae6eSSangbeom Kim } 7275bccae6eSSangbeom Kim 7285bccae6eSSangbeom Kim platform_set_drvdata(pdev, info); 7295bccae6eSSangbeom Kim 7305bccae6eSSangbeom Kim ret = s5m8767_rtc_init_reg(info); 7315bccae6eSSangbeom Kim 7325bccae6eSSangbeom Kim if (info->wtsr_smpl) { 7335bccae6eSSangbeom Kim s5m_rtc_enable_wtsr(info, true); 7345bccae6eSSangbeom Kim s5m_rtc_enable_smpl(info, true); 7355bccae6eSSangbeom Kim } 7365bccae6eSSangbeom Kim 7375bccae6eSSangbeom Kim device_init_wakeup(&pdev->dev, 1); 7385bccae6eSSangbeom Kim 7395bccae6eSSangbeom Kim info->rtc_dev = devm_rtc_device_register(&pdev->dev, "s5m-rtc", 7405bccae6eSSangbeom Kim &s5m_rtc_ops, THIS_MODULE); 7415bccae6eSSangbeom Kim 742e349c910SKrzysztof Kozlowski if (IS_ERR(info->rtc_dev)) { 743e349c910SKrzysztof Kozlowski ret = PTR_ERR(info->rtc_dev); 744e349c910SKrzysztof Kozlowski goto err; 745e349c910SKrzysztof Kozlowski } 7465bccae6eSSangbeom Kim 7475bccae6eSSangbeom Kim ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL, 7485bccae6eSSangbeom Kim s5m_rtc_alarm_irq, 0, "rtc-alarm0", 7495bccae6eSSangbeom Kim info); 750e349c910SKrzysztof Kozlowski if (ret < 0) { 7515bccae6eSSangbeom Kim dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n", 7525bccae6eSSangbeom Kim info->irq, ret); 753e349c910SKrzysztof Kozlowski goto err; 754e349c910SKrzysztof Kozlowski } 755e349c910SKrzysztof Kozlowski 756e349c910SKrzysztof Kozlowski return 0; 757e349c910SKrzysztof Kozlowski 758e349c910SKrzysztof Kozlowski err: 759e349c910SKrzysztof Kozlowski i2c_unregister_device(info->i2c); 7605bccae6eSSangbeom Kim 7615bccae6eSSangbeom Kim return ret; 7625bccae6eSSangbeom Kim } 7635bccae6eSSangbeom Kim 7645bccae6eSSangbeom Kim static void s5m_rtc_shutdown(struct platform_device *pdev) 7655bccae6eSSangbeom Kim { 7665bccae6eSSangbeom Kim struct s5m_rtc_info *info = platform_get_drvdata(pdev); 7675bccae6eSSangbeom Kim int i; 7685bccae6eSSangbeom Kim unsigned int val = 0; 7695bccae6eSSangbeom Kim if (info->wtsr_smpl) { 7705bccae6eSSangbeom Kim for (i = 0; i < 3; i++) { 7715bccae6eSSangbeom Kim s5m_rtc_enable_wtsr(info, false); 772f8b23bbdSKrzysztof Kozlowski regmap_read(info->regmap, info->regs->smpl_wtsr, &val); 7735bccae6eSSangbeom Kim pr_debug("%s: WTSR_SMPL reg(0x%02x)\n", __func__, val); 7745bccae6eSSangbeom Kim if (val & WTSR_ENABLE_MASK) 7755bccae6eSSangbeom Kim pr_emerg("%s: fail to disable WTSR\n", 7765bccae6eSSangbeom Kim __func__); 7775bccae6eSSangbeom Kim else { 7785bccae6eSSangbeom Kim pr_info("%s: success to disable WTSR\n", 7795bccae6eSSangbeom Kim __func__); 7805bccae6eSSangbeom Kim break; 7815bccae6eSSangbeom Kim } 7825bccae6eSSangbeom Kim } 7835bccae6eSSangbeom Kim } 7845bccae6eSSangbeom Kim /* Disable SMPL when power off */ 7855bccae6eSSangbeom Kim s5m_rtc_enable_smpl(info, false); 7865bccae6eSSangbeom Kim } 7875bccae6eSSangbeom Kim 788e349c910SKrzysztof Kozlowski static int s5m_rtc_remove(struct platform_device *pdev) 789e349c910SKrzysztof Kozlowski { 790e349c910SKrzysztof Kozlowski struct s5m_rtc_info *info = platform_get_drvdata(pdev); 791e349c910SKrzysztof Kozlowski 792e349c910SKrzysztof Kozlowski /* Perform also all shutdown steps when removing */ 793e349c910SKrzysztof Kozlowski s5m_rtc_shutdown(pdev); 794e349c910SKrzysztof Kozlowski i2c_unregister_device(info->i2c); 795e349c910SKrzysztof Kozlowski 796e349c910SKrzysztof Kozlowski return 0; 797e349c910SKrzysztof Kozlowski } 798e349c910SKrzysztof Kozlowski 79911ba5a1eSGeert Uytterhoeven #ifdef CONFIG_PM_SLEEP 800222ead7fSKrzysztof Kozlowski static int s5m_rtc_resume(struct device *dev) 801222ead7fSKrzysztof Kozlowski { 802222ead7fSKrzysztof Kozlowski struct s5m_rtc_info *info = dev_get_drvdata(dev); 803222ead7fSKrzysztof Kozlowski int ret = 0; 804222ead7fSKrzysztof Kozlowski 805222ead7fSKrzysztof Kozlowski if (device_may_wakeup(dev)) 806222ead7fSKrzysztof Kozlowski ret = disable_irq_wake(info->irq); 807222ead7fSKrzysztof Kozlowski 808222ead7fSKrzysztof Kozlowski return ret; 809222ead7fSKrzysztof Kozlowski } 810222ead7fSKrzysztof Kozlowski 811222ead7fSKrzysztof Kozlowski static int s5m_rtc_suspend(struct device *dev) 812222ead7fSKrzysztof Kozlowski { 813222ead7fSKrzysztof Kozlowski struct s5m_rtc_info *info = dev_get_drvdata(dev); 814222ead7fSKrzysztof Kozlowski int ret = 0; 815222ead7fSKrzysztof Kozlowski 816222ead7fSKrzysztof Kozlowski if (device_may_wakeup(dev)) 817222ead7fSKrzysztof Kozlowski ret = enable_irq_wake(info->irq); 818222ead7fSKrzysztof Kozlowski 819222ead7fSKrzysztof Kozlowski return ret; 820222ead7fSKrzysztof Kozlowski } 82111ba5a1eSGeert Uytterhoeven #endif /* CONFIG_PM_SLEEP */ 822222ead7fSKrzysztof Kozlowski 823222ead7fSKrzysztof Kozlowski static SIMPLE_DEV_PM_OPS(s5m_rtc_pm_ops, s5m_rtc_suspend, s5m_rtc_resume); 824222ead7fSKrzysztof Kozlowski 8255bccae6eSSangbeom Kim static const struct platform_device_id s5m_rtc_id[] = { 8260c5deb1eSKrzysztof Kozlowski { "s5m-rtc", S5M8767X }, 8270c5deb1eSKrzysztof Kozlowski { "s2mps14-rtc", S2MPS14X }, 8285bccae6eSSangbeom Kim }; 8295bccae6eSSangbeom Kim 8305bccae6eSSangbeom Kim static struct platform_driver s5m_rtc_driver = { 8315bccae6eSSangbeom Kim .driver = { 8325bccae6eSSangbeom Kim .name = "s5m-rtc", 8335bccae6eSSangbeom Kim .owner = THIS_MODULE, 834222ead7fSKrzysztof Kozlowski .pm = &s5m_rtc_pm_ops, 8355bccae6eSSangbeom Kim }, 8365bccae6eSSangbeom Kim .probe = s5m_rtc_probe, 837e349c910SKrzysztof Kozlowski .remove = s5m_rtc_remove, 8385bccae6eSSangbeom Kim .shutdown = s5m_rtc_shutdown, 8395bccae6eSSangbeom Kim .id_table = s5m_rtc_id, 8405bccae6eSSangbeom Kim }; 8415bccae6eSSangbeom Kim 8425bccae6eSSangbeom Kim module_platform_driver(s5m_rtc_driver); 8435bccae6eSSangbeom Kim 8445bccae6eSSangbeom Kim /* Module information */ 8455bccae6eSSangbeom Kim MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>"); 8460c5deb1eSKrzysztof Kozlowski MODULE_DESCRIPTION("Samsung S5M/S2MPS14 RTC driver"); 8475bccae6eSSangbeom Kim MODULE_LICENSE("GPL"); 8485bccae6eSSangbeom Kim MODULE_ALIAS("platform:s5m-rtc"); 849