126dcc12cSKrzysztof Kozlowski // SPDX-License-Identifier: GPL-2.0+ 226dcc12cSKrzysztof Kozlowski // 326dcc12cSKrzysztof Kozlowski // Copyright (c) 2013-2014 Samsung Electronics Co., Ltd 426dcc12cSKrzysztof Kozlowski // http://www.samsung.com 526dcc12cSKrzysztof Kozlowski // 626dcc12cSKrzysztof Kozlowski // Copyright (C) 2013 Google, Inc 75bccae6eSSangbeom Kim 8a737e835SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9a737e835SJoe Perches 105bccae6eSSangbeom Kim #include <linux/module.h> 115bccae6eSSangbeom Kim #include <linux/i2c.h> 125bccae6eSSangbeom Kim #include <linux/bcd.h> 135bccae6eSSangbeom Kim #include <linux/regmap.h> 145bccae6eSSangbeom Kim #include <linux/rtc.h> 155bccae6eSSangbeom Kim #include <linux/platform_device.h> 165bccae6eSSangbeom Kim #include <linux/mfd/samsung/core.h> 175bccae6eSSangbeom Kim #include <linux/mfd/samsung/irq.h> 185bccae6eSSangbeom Kim #include <linux/mfd/samsung/rtc.h> 190c5deb1eSKrzysztof Kozlowski #include <linux/mfd/samsung/s2mps14.h> 205bccae6eSSangbeom Kim 21d73238d4SKrzysztof Kozlowski /* 22d73238d4SKrzysztof Kozlowski * Maximum number of retries for checking changes in UDR field 23602cb5bbSKrzysztof Kozlowski * of S5M_RTC_UDR_CON register (to limit possible endless loop). 24d73238d4SKrzysztof Kozlowski * 25d73238d4SKrzysztof Kozlowski * After writing to RTC registers (setting time or alarm) read the UDR field 26602cb5bbSKrzysztof Kozlowski * in S5M_RTC_UDR_CON register. UDR is auto-cleared when data have 27d73238d4SKrzysztof Kozlowski * been transferred. 28d73238d4SKrzysztof Kozlowski */ 29d73238d4SKrzysztof Kozlowski #define UDR_READ_RETRY_CNT 5 30d73238d4SKrzysztof Kozlowski 314a681243SGustavo A. R. Silva enum { 324a681243SGustavo A. R. Silva RTC_SEC = 0, 334a681243SGustavo A. R. Silva RTC_MIN, 344a681243SGustavo A. R. Silva RTC_HOUR, 354a681243SGustavo A. R. Silva RTC_WEEKDAY, 364a681243SGustavo A. R. Silva RTC_DATE, 374a681243SGustavo A. R. Silva RTC_MONTH, 384a681243SGustavo A. R. Silva RTC_YEAR1, 394a681243SGustavo A. R. Silva RTC_YEAR2, 40756d5282SGustavo A. R. Silva /* Make sure this is always the last enum name. */ 41756d5282SGustavo A. R. Silva RTC_MAX_NUM_TIME_REGS 424a681243SGustavo A. R. Silva }; 434a681243SGustavo A. R. Silva 448ae83b6fSKrzysztof Kozlowski /* 458ae83b6fSKrzysztof Kozlowski * Registers used by the driver which are different between chipsets. 468ae83b6fSKrzysztof Kozlowski * 478ae83b6fSKrzysztof Kozlowski * Operations like read time and write alarm/time require updating 488ae83b6fSKrzysztof Kozlowski * specific fields in UDR register. These fields usually are auto-cleared 498ae83b6fSKrzysztof Kozlowski * (with some exceptions). 508ae83b6fSKrzysztof Kozlowski * 518ae83b6fSKrzysztof Kozlowski * Table of operations per device: 528ae83b6fSKrzysztof Kozlowski * 538ae83b6fSKrzysztof Kozlowski * Device | Write time | Read time | Write alarm 548ae83b6fSKrzysztof Kozlowski * ================================================= 558ae83b6fSKrzysztof Kozlowski * S5M8767 | UDR + TIME | | UDR 568ae83b6fSKrzysztof Kozlowski * S2MPS11/14 | WUDR | RUDR | WUDR + RUDR 578ae83b6fSKrzysztof Kozlowski * S2MPS13 | WUDR | RUDR | WUDR + AUDR 588ae83b6fSKrzysztof Kozlowski * S2MPS15 | WUDR | RUDR | AUDR 598ae83b6fSKrzysztof Kozlowski */ 60f8b23bbdSKrzysztof Kozlowski struct s5m_rtc_reg_config { 61f8b23bbdSKrzysztof Kozlowski /* Number of registers used for setting time/alarm0/alarm1 */ 62f8b23bbdSKrzysztof Kozlowski unsigned int regs_count; 63f8b23bbdSKrzysztof Kozlowski /* First register for time, seconds */ 64f8b23bbdSKrzysztof Kozlowski unsigned int time; 65f8b23bbdSKrzysztof Kozlowski /* RTC control register */ 66f8b23bbdSKrzysztof Kozlowski unsigned int ctrl; 67f8b23bbdSKrzysztof Kozlowski /* First register for alarm 0, seconds */ 68f8b23bbdSKrzysztof Kozlowski unsigned int alarm0; 69f8b23bbdSKrzysztof Kozlowski /* First register for alarm 1, seconds */ 70f8b23bbdSKrzysztof Kozlowski unsigned int alarm1; 71f8b23bbdSKrzysztof Kozlowski /* 72f8b23bbdSKrzysztof Kozlowski * Register for update flag (UDR). Typically setting UDR field to 1 73f8b23bbdSKrzysztof Kozlowski * will enable update of time or alarm register. Then it will be 74f8b23bbdSKrzysztof Kozlowski * auto-cleared after successful update. 75f8b23bbdSKrzysztof Kozlowski */ 76a83a793aSKrzysztof Kozlowski unsigned int udr_update; 7767a6025aSKrzysztof Kozlowski /* Auto-cleared mask in UDR field for writing time and alarm */ 7867a6025aSKrzysztof Kozlowski unsigned int autoclear_udr_mask; 798ae83b6fSKrzysztof Kozlowski /* 808ae83b6fSKrzysztof Kozlowski * Masks in UDR field for time and alarm operations. 818ae83b6fSKrzysztof Kozlowski * The read time mask can be 0. Rest should not. 828ae83b6fSKrzysztof Kozlowski */ 838ae83b6fSKrzysztof Kozlowski unsigned int read_time_udr_mask; 848ae83b6fSKrzysztof Kozlowski unsigned int write_time_udr_mask; 858ae83b6fSKrzysztof Kozlowski unsigned int write_alarm_udr_mask; 86f8b23bbdSKrzysztof Kozlowski }; 87f8b23bbdSKrzysztof Kozlowski 88f8b23bbdSKrzysztof Kozlowski /* Register map for S5M8763 and S5M8767 */ 89f8b23bbdSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s5m_rtc_regs = { 90f8b23bbdSKrzysztof Kozlowski .regs_count = 8, 91f8b23bbdSKrzysztof Kozlowski .time = S5M_RTC_SEC, 92f8b23bbdSKrzysztof Kozlowski .ctrl = S5M_ALARM1_CONF, 93f8b23bbdSKrzysztof Kozlowski .alarm0 = S5M_ALARM0_SEC, 94f8b23bbdSKrzysztof Kozlowski .alarm1 = S5M_ALARM1_SEC, 95a83a793aSKrzysztof Kozlowski .udr_update = S5M_RTC_UDR_CON, 9667a6025aSKrzysztof Kozlowski .autoclear_udr_mask = S5M_RTC_UDR_MASK, 978ae83b6fSKrzysztof Kozlowski .read_time_udr_mask = 0, /* Not needed */ 988ae83b6fSKrzysztof Kozlowski .write_time_udr_mask = S5M_RTC_UDR_MASK | S5M_RTC_TIME_EN_MASK, 998ae83b6fSKrzysztof Kozlowski .write_alarm_udr_mask = S5M_RTC_UDR_MASK, 100f8b23bbdSKrzysztof Kozlowski }; 101f8b23bbdSKrzysztof Kozlowski 1028ae83b6fSKrzysztof Kozlowski /* Register map for S2MPS13 */ 1038ae83b6fSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s2mps13_rtc_regs = { 1040c5deb1eSKrzysztof Kozlowski .regs_count = 7, 1050c5deb1eSKrzysztof Kozlowski .time = S2MPS_RTC_SEC, 1060c5deb1eSKrzysztof Kozlowski .ctrl = S2MPS_RTC_CTRL, 1070c5deb1eSKrzysztof Kozlowski .alarm0 = S2MPS_ALARM0_SEC, 1080c5deb1eSKrzysztof Kozlowski .alarm1 = S2MPS_ALARM1_SEC, 109a83a793aSKrzysztof Kozlowski .udr_update = S2MPS_RTC_UDR_CON, 11067a6025aSKrzysztof Kozlowski .autoclear_udr_mask = S2MPS_RTC_WUDR_MASK, 1118ae83b6fSKrzysztof Kozlowski .read_time_udr_mask = S2MPS_RTC_RUDR_MASK, 1128ae83b6fSKrzysztof Kozlowski .write_time_udr_mask = S2MPS_RTC_WUDR_MASK, 1138ae83b6fSKrzysztof Kozlowski .write_alarm_udr_mask = S2MPS_RTC_WUDR_MASK | S2MPS13_RTC_AUDR_MASK, 1148ae83b6fSKrzysztof Kozlowski }; 1158ae83b6fSKrzysztof Kozlowski 1168ae83b6fSKrzysztof Kozlowski /* Register map for S2MPS11/14 */ 1178ae83b6fSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s2mps14_rtc_regs = { 1188ae83b6fSKrzysztof Kozlowski .regs_count = 7, 1198ae83b6fSKrzysztof Kozlowski .time = S2MPS_RTC_SEC, 1208ae83b6fSKrzysztof Kozlowski .ctrl = S2MPS_RTC_CTRL, 1218ae83b6fSKrzysztof Kozlowski .alarm0 = S2MPS_ALARM0_SEC, 1228ae83b6fSKrzysztof Kozlowski .alarm1 = S2MPS_ALARM1_SEC, 1238ae83b6fSKrzysztof Kozlowski .udr_update = S2MPS_RTC_UDR_CON, 1248ae83b6fSKrzysztof Kozlowski .autoclear_udr_mask = S2MPS_RTC_WUDR_MASK, 1258ae83b6fSKrzysztof Kozlowski .read_time_udr_mask = S2MPS_RTC_RUDR_MASK, 1268ae83b6fSKrzysztof Kozlowski .write_time_udr_mask = S2MPS_RTC_WUDR_MASK, 1278ae83b6fSKrzysztof Kozlowski .write_alarm_udr_mask = S2MPS_RTC_WUDR_MASK | S2MPS_RTC_RUDR_MASK, 1288ae83b6fSKrzysztof Kozlowski }; 1298ae83b6fSKrzysztof Kozlowski 1308ae83b6fSKrzysztof Kozlowski /* 1318ae83b6fSKrzysztof Kozlowski * Register map for S2MPS15 - in comparison to S2MPS14 the WUDR and AUDR bits 1328ae83b6fSKrzysztof Kozlowski * are swapped. 1338ae83b6fSKrzysztof Kozlowski */ 1348ae83b6fSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s2mps15_rtc_regs = { 1358ae83b6fSKrzysztof Kozlowski .regs_count = 7, 1368ae83b6fSKrzysztof Kozlowski .time = S2MPS_RTC_SEC, 1378ae83b6fSKrzysztof Kozlowski .ctrl = S2MPS_RTC_CTRL, 1388ae83b6fSKrzysztof Kozlowski .alarm0 = S2MPS_ALARM0_SEC, 1398ae83b6fSKrzysztof Kozlowski .alarm1 = S2MPS_ALARM1_SEC, 1408ae83b6fSKrzysztof Kozlowski .udr_update = S2MPS_RTC_UDR_CON, 1418ae83b6fSKrzysztof Kozlowski .autoclear_udr_mask = S2MPS_RTC_WUDR_MASK, 1428ae83b6fSKrzysztof Kozlowski .read_time_udr_mask = S2MPS_RTC_RUDR_MASK, 1438ae83b6fSKrzysztof Kozlowski .write_time_udr_mask = S2MPS15_RTC_WUDR_MASK, 1448ae83b6fSKrzysztof Kozlowski .write_alarm_udr_mask = S2MPS15_RTC_AUDR_MASK, 1450c5deb1eSKrzysztof Kozlowski }; 1460c5deb1eSKrzysztof Kozlowski 1475bccae6eSSangbeom Kim struct s5m_rtc_info { 1485bccae6eSSangbeom Kim struct device *dev; 149e349c910SKrzysztof Kozlowski struct i2c_client *i2c; 1505bccae6eSSangbeom Kim struct sec_pmic_dev *s5m87xx; 1515ccb7d71SGeert Uytterhoeven struct regmap *regmap; 1525bccae6eSSangbeom Kim struct rtc_device *rtc_dev; 1535bccae6eSSangbeom Kim int irq; 15494f91922SKrzysztof Kozlowski enum sec_device_type device_type; 1555bccae6eSSangbeom Kim int rtc_24hr_mode; 156f8b23bbdSKrzysztof Kozlowski const struct s5m_rtc_reg_config *regs; 1575bccae6eSSangbeom Kim }; 1585bccae6eSSangbeom Kim 159e349c910SKrzysztof Kozlowski static const struct regmap_config s5m_rtc_regmap_config = { 160e349c910SKrzysztof Kozlowski .reg_bits = 8, 161e349c910SKrzysztof Kozlowski .val_bits = 8, 162e349c910SKrzysztof Kozlowski 163602cb5bbSKrzysztof Kozlowski .max_register = S5M_RTC_REG_MAX, 164e349c910SKrzysztof Kozlowski }; 165e349c910SKrzysztof Kozlowski 166e349c910SKrzysztof Kozlowski static const struct regmap_config s2mps14_rtc_regmap_config = { 167e349c910SKrzysztof Kozlowski .reg_bits = 8, 168e349c910SKrzysztof Kozlowski .val_bits = 8, 169e349c910SKrzysztof Kozlowski 170e349c910SKrzysztof Kozlowski .max_register = S2MPS_RTC_REG_MAX, 171e349c910SKrzysztof Kozlowski }; 172e349c910SKrzysztof Kozlowski 1735bccae6eSSangbeom Kim static void s5m8767_data_to_tm(u8 *data, struct rtc_time *tm, 1745bccae6eSSangbeom Kim int rtc_24hr_mode) 1755bccae6eSSangbeom Kim { 1765bccae6eSSangbeom Kim tm->tm_sec = data[RTC_SEC] & 0x7f; 1775bccae6eSSangbeom Kim tm->tm_min = data[RTC_MIN] & 0x7f; 1785bccae6eSSangbeom Kim if (rtc_24hr_mode) { 1795bccae6eSSangbeom Kim tm->tm_hour = data[RTC_HOUR] & 0x1f; 1805bccae6eSSangbeom Kim } else { 1815bccae6eSSangbeom Kim tm->tm_hour = data[RTC_HOUR] & 0x0f; 1825bccae6eSSangbeom Kim if (data[RTC_HOUR] & HOUR_PM_MASK) 1835bccae6eSSangbeom Kim tm->tm_hour += 12; 1845bccae6eSSangbeom Kim } 1855bccae6eSSangbeom Kim 1865bccae6eSSangbeom Kim tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f); 1875bccae6eSSangbeom Kim tm->tm_mday = data[RTC_DATE] & 0x1f; 1885bccae6eSSangbeom Kim tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1; 1895bccae6eSSangbeom Kim tm->tm_year = (data[RTC_YEAR1] & 0x7f) + 100; 1905bccae6eSSangbeom Kim tm->tm_yday = 0; 1915bccae6eSSangbeom Kim tm->tm_isdst = 0; 1925bccae6eSSangbeom Kim } 1935bccae6eSSangbeom Kim 1945bccae6eSSangbeom Kim static int s5m8767_tm_to_data(struct rtc_time *tm, u8 *data) 1955bccae6eSSangbeom Kim { 1965bccae6eSSangbeom Kim data[RTC_SEC] = tm->tm_sec; 1975bccae6eSSangbeom Kim data[RTC_MIN] = tm->tm_min; 1985bccae6eSSangbeom Kim 1995bccae6eSSangbeom Kim if (tm->tm_hour >= 12) 2005bccae6eSSangbeom Kim data[RTC_HOUR] = tm->tm_hour | HOUR_PM_MASK; 2015bccae6eSSangbeom Kim else 2025bccae6eSSangbeom Kim data[RTC_HOUR] = tm->tm_hour & ~HOUR_PM_MASK; 2035bccae6eSSangbeom Kim 2045bccae6eSSangbeom Kim data[RTC_WEEKDAY] = 1 << tm->tm_wday; 2055bccae6eSSangbeom Kim data[RTC_DATE] = tm->tm_mday; 2065bccae6eSSangbeom Kim data[RTC_MONTH] = tm->tm_mon + 1; 207*fffd603aSAlexandre Belloni data[RTC_YEAR1] = tm->tm_year - 100; 2085bccae6eSSangbeom Kim 2095bccae6eSSangbeom Kim return 0; 2105bccae6eSSangbeom Kim } 2115bccae6eSSangbeom Kim 212d73238d4SKrzysztof Kozlowski /* 213d73238d4SKrzysztof Kozlowski * Read RTC_UDR_CON register and wait till UDR field is cleared. 214d73238d4SKrzysztof Kozlowski * This indicates that time/alarm update ended. 215d73238d4SKrzysztof Kozlowski */ 2168124c711SKrzysztof Kozlowski static int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info) 217d73238d4SKrzysztof Kozlowski { 218d73238d4SKrzysztof Kozlowski int ret, retry = UDR_READ_RETRY_CNT; 219d73238d4SKrzysztof Kozlowski unsigned int data; 220d73238d4SKrzysztof Kozlowski 221d73238d4SKrzysztof Kozlowski do { 222a83a793aSKrzysztof Kozlowski ret = regmap_read(info->regmap, info->regs->udr_update, &data); 22367a6025aSKrzysztof Kozlowski } while (--retry && (data & info->regs->autoclear_udr_mask) && !ret); 224d73238d4SKrzysztof Kozlowski 225d73238d4SKrzysztof Kozlowski if (!retry) 226d73238d4SKrzysztof Kozlowski dev_err(info->dev, "waiting for UDR update, reached max number of retries\n"); 227d73238d4SKrzysztof Kozlowski 228d73238d4SKrzysztof Kozlowski return ret; 229d73238d4SKrzysztof Kozlowski } 230d73238d4SKrzysztof Kozlowski 2318124c711SKrzysztof Kozlowski static int s5m_check_peding_alarm_interrupt(struct s5m_rtc_info *info, 232f8b23bbdSKrzysztof Kozlowski struct rtc_wkalrm *alarm) 233f8b23bbdSKrzysztof Kozlowski { 234f8b23bbdSKrzysztof Kozlowski int ret; 235f8b23bbdSKrzysztof Kozlowski unsigned int val; 236f8b23bbdSKrzysztof Kozlowski 237f8b23bbdSKrzysztof Kozlowski switch (info->device_type) { 238f8b23bbdSKrzysztof Kozlowski case S5M8767X: 239f8b23bbdSKrzysztof Kozlowski case S5M8763X: 240f8b23bbdSKrzysztof Kozlowski ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val); 241f8b23bbdSKrzysztof Kozlowski val &= S5M_ALARM0_STATUS; 242f8b23bbdSKrzysztof Kozlowski break; 243a65e5efaSAlim Akhtar case S2MPS15X: 2440c5deb1eSKrzysztof Kozlowski case S2MPS14X: 2455281f94aSKrzysztof Kozlowski case S2MPS13X: 2460c5deb1eSKrzysztof Kozlowski ret = regmap_read(info->s5m87xx->regmap_pmic, S2MPS14_REG_ST2, 2470c5deb1eSKrzysztof Kozlowski &val); 2480c5deb1eSKrzysztof Kozlowski val &= S2MPS_ALARM0_STATUS; 2490c5deb1eSKrzysztof Kozlowski break; 250f8b23bbdSKrzysztof Kozlowski default: 251f8b23bbdSKrzysztof Kozlowski return -EINVAL; 252f8b23bbdSKrzysztof Kozlowski } 253f8b23bbdSKrzysztof Kozlowski if (ret < 0) 254f8b23bbdSKrzysztof Kozlowski return ret; 255f8b23bbdSKrzysztof Kozlowski 256f8b23bbdSKrzysztof Kozlowski if (val) 257f8b23bbdSKrzysztof Kozlowski alarm->pending = 1; 258f8b23bbdSKrzysztof Kozlowski else 259f8b23bbdSKrzysztof Kozlowski alarm->pending = 0; 260f8b23bbdSKrzysztof Kozlowski 261f8b23bbdSKrzysztof Kozlowski return 0; 262f8b23bbdSKrzysztof Kozlowski } 263f8b23bbdSKrzysztof Kozlowski 2648124c711SKrzysztof Kozlowski static int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info) 2655bccae6eSSangbeom Kim { 2665bccae6eSSangbeom Kim int ret; 2675bccae6eSSangbeom Kim unsigned int data; 2685bccae6eSSangbeom Kim 269a83a793aSKrzysztof Kozlowski ret = regmap_read(info->regmap, info->regs->udr_update, &data); 2705bccae6eSSangbeom Kim if (ret < 0) { 2715bccae6eSSangbeom Kim dev_err(info->dev, "failed to read update reg(%d)\n", ret); 2725bccae6eSSangbeom Kim return ret; 2735bccae6eSSangbeom Kim } 2745bccae6eSSangbeom Kim 2758ae83b6fSKrzysztof Kozlowski data |= info->regs->write_time_udr_mask; 276a65e5efaSAlim Akhtar 277a83a793aSKrzysztof Kozlowski ret = regmap_write(info->regmap, info->regs->udr_update, data); 2785bccae6eSSangbeom Kim if (ret < 0) { 2795bccae6eSSangbeom Kim dev_err(info->dev, "failed to write update reg(%d)\n", ret); 2805bccae6eSSangbeom Kim return ret; 2815bccae6eSSangbeom Kim } 2825bccae6eSSangbeom Kim 283d73238d4SKrzysztof Kozlowski ret = s5m8767_wait_for_udr_update(info); 2845bccae6eSSangbeom Kim 2855bccae6eSSangbeom Kim return ret; 2865bccae6eSSangbeom Kim } 2875bccae6eSSangbeom Kim 2888124c711SKrzysztof Kozlowski static int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info) 2895bccae6eSSangbeom Kim { 2905bccae6eSSangbeom Kim int ret; 2915bccae6eSSangbeom Kim unsigned int data; 2925bccae6eSSangbeom Kim 293a83a793aSKrzysztof Kozlowski ret = regmap_read(info->regmap, info->regs->udr_update, &data); 2945bccae6eSSangbeom Kim if (ret < 0) { 2955bccae6eSSangbeom Kim dev_err(info->dev, "%s: fail to read update reg(%d)\n", 2965bccae6eSSangbeom Kim __func__, ret); 2975bccae6eSSangbeom Kim return ret; 2985bccae6eSSangbeom Kim } 2995bccae6eSSangbeom Kim 3008ae83b6fSKrzysztof Kozlowski data |= info->regs->write_alarm_udr_mask; 3010c5deb1eSKrzysztof Kozlowski switch (info->device_type) { 3020c5deb1eSKrzysztof Kozlowski case S5M8763X: 3030c5deb1eSKrzysztof Kozlowski case S5M8767X: 3040c5deb1eSKrzysztof Kozlowski data &= ~S5M_RTC_TIME_EN_MASK; 3050c5deb1eSKrzysztof Kozlowski break; 306a65e5efaSAlim Akhtar case S2MPS15X: 3070c5deb1eSKrzysztof Kozlowski case S2MPS14X: 3085281f94aSKrzysztof Kozlowski case S2MPS13X: 3098ae83b6fSKrzysztof Kozlowski /* No exceptions needed */ 3105281f94aSKrzysztof Kozlowski break; 3110c5deb1eSKrzysztof Kozlowski default: 3120c5deb1eSKrzysztof Kozlowski return -EINVAL; 3130c5deb1eSKrzysztof Kozlowski } 3145bccae6eSSangbeom Kim 315a83a793aSKrzysztof Kozlowski ret = regmap_write(info->regmap, info->regs->udr_update, data); 3165bccae6eSSangbeom Kim if (ret < 0) { 3175bccae6eSSangbeom Kim dev_err(info->dev, "%s: fail to write update reg(%d)\n", 3185bccae6eSSangbeom Kim __func__, ret); 3195bccae6eSSangbeom Kim return ret; 3205bccae6eSSangbeom Kim } 3215bccae6eSSangbeom Kim 322d73238d4SKrzysztof Kozlowski ret = s5m8767_wait_for_udr_update(info); 3235bccae6eSSangbeom Kim 3245281f94aSKrzysztof Kozlowski /* On S2MPS13 the AUDR is not auto-cleared */ 3255281f94aSKrzysztof Kozlowski if (info->device_type == S2MPS13X) 326a83a793aSKrzysztof Kozlowski regmap_update_bits(info->regmap, info->regs->udr_update, 3275281f94aSKrzysztof Kozlowski S2MPS13_RTC_AUDR_MASK, 0); 3285281f94aSKrzysztof Kozlowski 3295bccae6eSSangbeom Kim return ret; 3305bccae6eSSangbeom Kim } 3315bccae6eSSangbeom Kim 3325bccae6eSSangbeom Kim static void s5m8763_data_to_tm(u8 *data, struct rtc_time *tm) 3335bccae6eSSangbeom Kim { 3345bccae6eSSangbeom Kim tm->tm_sec = bcd2bin(data[RTC_SEC]); 3355bccae6eSSangbeom Kim tm->tm_min = bcd2bin(data[RTC_MIN]); 3365bccae6eSSangbeom Kim 3375bccae6eSSangbeom Kim if (data[RTC_HOUR] & HOUR_12) { 3385bccae6eSSangbeom Kim tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f); 3395bccae6eSSangbeom Kim if (data[RTC_HOUR] & HOUR_PM) 3405bccae6eSSangbeom Kim tm->tm_hour += 12; 3415bccae6eSSangbeom Kim } else { 3425bccae6eSSangbeom Kim tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f); 3435bccae6eSSangbeom Kim } 3445bccae6eSSangbeom Kim 3455bccae6eSSangbeom Kim tm->tm_wday = data[RTC_WEEKDAY] & 0x07; 3465bccae6eSSangbeom Kim tm->tm_mday = bcd2bin(data[RTC_DATE]); 3475bccae6eSSangbeom Kim tm->tm_mon = bcd2bin(data[RTC_MONTH]); 3485bccae6eSSangbeom Kim tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100; 3495bccae6eSSangbeom Kim tm->tm_year -= 1900; 3505bccae6eSSangbeom Kim } 3515bccae6eSSangbeom Kim 3525bccae6eSSangbeom Kim static void s5m8763_tm_to_data(struct rtc_time *tm, u8 *data) 3535bccae6eSSangbeom Kim { 3545bccae6eSSangbeom Kim data[RTC_SEC] = bin2bcd(tm->tm_sec); 3555bccae6eSSangbeom Kim data[RTC_MIN] = bin2bcd(tm->tm_min); 3565bccae6eSSangbeom Kim data[RTC_HOUR] = bin2bcd(tm->tm_hour); 3575bccae6eSSangbeom Kim data[RTC_WEEKDAY] = tm->tm_wday; 3585bccae6eSSangbeom Kim data[RTC_DATE] = bin2bcd(tm->tm_mday); 3595bccae6eSSangbeom Kim data[RTC_MONTH] = bin2bcd(tm->tm_mon); 3605bccae6eSSangbeom Kim data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100); 3615bccae6eSSangbeom Kim data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100); 3625bccae6eSSangbeom Kim } 3635bccae6eSSangbeom Kim 3645bccae6eSSangbeom Kim static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm) 3655bccae6eSSangbeom Kim { 3665bccae6eSSangbeom Kim struct s5m_rtc_info *info = dev_get_drvdata(dev); 367756d5282SGustavo A. R. Silva u8 data[RTC_MAX_NUM_TIME_REGS]; 3685bccae6eSSangbeom Kim int ret; 3695bccae6eSSangbeom Kim 3708ae83b6fSKrzysztof Kozlowski if (info->regs->read_time_udr_mask) { 3710c5deb1eSKrzysztof Kozlowski ret = regmap_update_bits(info->regmap, 372a83a793aSKrzysztof Kozlowski info->regs->udr_update, 3738ae83b6fSKrzysztof Kozlowski info->regs->read_time_udr_mask, 3748ae83b6fSKrzysztof Kozlowski info->regs->read_time_udr_mask); 3750c5deb1eSKrzysztof Kozlowski if (ret) { 3760c5deb1eSKrzysztof Kozlowski dev_err(dev, 3770c5deb1eSKrzysztof Kozlowski "Failed to prepare registers for time reading: %d\n", 3780c5deb1eSKrzysztof Kozlowski ret); 3790c5deb1eSKrzysztof Kozlowski return ret; 3800c5deb1eSKrzysztof Kozlowski } 3810c5deb1eSKrzysztof Kozlowski } 382f8b23bbdSKrzysztof Kozlowski ret = regmap_bulk_read(info->regmap, info->regs->time, data, 383f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 3845bccae6eSSangbeom Kim if (ret < 0) 3855bccae6eSSangbeom Kim return ret; 3865bccae6eSSangbeom Kim 3875bccae6eSSangbeom Kim switch (info->device_type) { 3885bccae6eSSangbeom Kim case S5M8763X: 3895bccae6eSSangbeom Kim s5m8763_data_to_tm(data, tm); 3905bccae6eSSangbeom Kim break; 3915bccae6eSSangbeom Kim 3925bccae6eSSangbeom Kim case S5M8767X: 393a65e5efaSAlim Akhtar case S2MPS15X: 3940c5deb1eSKrzysztof Kozlowski case S2MPS14X: 3955281f94aSKrzysztof Kozlowski case S2MPS13X: 3965bccae6eSSangbeom Kim s5m8767_data_to_tm(data, tm, info->rtc_24hr_mode); 3975bccae6eSSangbeom Kim break; 3985bccae6eSSangbeom Kim 3995bccae6eSSangbeom Kim default: 4005bccae6eSSangbeom Kim return -EINVAL; 4015bccae6eSSangbeom Kim } 4025bccae6eSSangbeom Kim 4035527842eSAndy Shevchenko dev_dbg(dev, "%s: %ptR(%d)\n", __func__, tm, tm->tm_wday); 4045bccae6eSSangbeom Kim 40522652ba7SAlexandre Belloni return 0; 4065bccae6eSSangbeom Kim } 4075bccae6eSSangbeom Kim 4085bccae6eSSangbeom Kim static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm) 4095bccae6eSSangbeom Kim { 4105bccae6eSSangbeom Kim struct s5m_rtc_info *info = dev_get_drvdata(dev); 411756d5282SGustavo A. R. Silva u8 data[RTC_MAX_NUM_TIME_REGS]; 4125bccae6eSSangbeom Kim int ret = 0; 4135bccae6eSSangbeom Kim 4145bccae6eSSangbeom Kim switch (info->device_type) { 4155bccae6eSSangbeom Kim case S5M8763X: 4165bccae6eSSangbeom Kim s5m8763_tm_to_data(tm, data); 4175bccae6eSSangbeom Kim break; 4185bccae6eSSangbeom Kim case S5M8767X: 419a65e5efaSAlim Akhtar case S2MPS15X: 4200c5deb1eSKrzysztof Kozlowski case S2MPS14X: 4215281f94aSKrzysztof Kozlowski case S2MPS13X: 4225bccae6eSSangbeom Kim ret = s5m8767_tm_to_data(tm, data); 4235bccae6eSSangbeom Kim break; 4245bccae6eSSangbeom Kim default: 4255bccae6eSSangbeom Kim return -EINVAL; 4265bccae6eSSangbeom Kim } 4275bccae6eSSangbeom Kim 4285bccae6eSSangbeom Kim if (ret < 0) 4295bccae6eSSangbeom Kim return ret; 4305bccae6eSSangbeom Kim 4315527842eSAndy Shevchenko dev_dbg(dev, "%s: %ptR(%d)\n", __func__, tm, tm->tm_wday); 4325bccae6eSSangbeom Kim 433f8b23bbdSKrzysztof Kozlowski ret = regmap_raw_write(info->regmap, info->regs->time, data, 434f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 4355bccae6eSSangbeom Kim if (ret < 0) 4365bccae6eSSangbeom Kim return ret; 4375bccae6eSSangbeom Kim 4385bccae6eSSangbeom Kim ret = s5m8767_rtc_set_time_reg(info); 4395bccae6eSSangbeom Kim 4405bccae6eSSangbeom Kim return ret; 4415bccae6eSSangbeom Kim } 4425bccae6eSSangbeom Kim 4435bccae6eSSangbeom Kim static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 4445bccae6eSSangbeom Kim { 4455bccae6eSSangbeom Kim struct s5m_rtc_info *info = dev_get_drvdata(dev); 446756d5282SGustavo A. R. Silva u8 data[RTC_MAX_NUM_TIME_REGS]; 4475bccae6eSSangbeom Kim unsigned int val; 4485bccae6eSSangbeom Kim int ret, i; 4495bccae6eSSangbeom Kim 450f8b23bbdSKrzysztof Kozlowski ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data, 451f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 4525bccae6eSSangbeom Kim if (ret < 0) 4535bccae6eSSangbeom Kim return ret; 4545bccae6eSSangbeom Kim 4555bccae6eSSangbeom Kim switch (info->device_type) { 4565bccae6eSSangbeom Kim case S5M8763X: 4575bccae6eSSangbeom Kim s5m8763_data_to_tm(data, &alrm->time); 458602cb5bbSKrzysztof Kozlowski ret = regmap_read(info->regmap, S5M_ALARM0_CONF, &val); 4595bccae6eSSangbeom Kim if (ret < 0) 4605bccae6eSSangbeom Kim return ret; 4615bccae6eSSangbeom Kim 4625bccae6eSSangbeom Kim alrm->enabled = !!val; 4635bccae6eSSangbeom Kim break; 4645bccae6eSSangbeom Kim 4655bccae6eSSangbeom Kim case S5M8767X: 466a65e5efaSAlim Akhtar case S2MPS15X: 4670c5deb1eSKrzysztof Kozlowski case S2MPS14X: 4685281f94aSKrzysztof Kozlowski case S2MPS13X: 4695bccae6eSSangbeom Kim s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode); 4705bccae6eSSangbeom Kim alrm->enabled = 0; 471f8b23bbdSKrzysztof Kozlowski for (i = 0; i < info->regs->regs_count; i++) { 4725bccae6eSSangbeom Kim if (data[i] & ALARM_ENABLE_MASK) { 4735bccae6eSSangbeom Kim alrm->enabled = 1; 4745bccae6eSSangbeom Kim break; 4755bccae6eSSangbeom Kim } 4765bccae6eSSangbeom Kim } 4775bccae6eSSangbeom Kim break; 4785bccae6eSSangbeom Kim 4795bccae6eSSangbeom Kim default: 4805bccae6eSSangbeom Kim return -EINVAL; 4815bccae6eSSangbeom Kim } 4825bccae6eSSangbeom Kim 4835527842eSAndy Shevchenko dev_dbg(dev, "%s: %ptR(%d)\n", __func__, &alrm->time, alrm->time.tm_wday); 484f8b23bbdSKrzysztof Kozlowski 485f2581b1dSNobuhiro Iwamatsu return s5m_check_peding_alarm_interrupt(info, alrm); 4865bccae6eSSangbeom Kim } 4875bccae6eSSangbeom Kim 4885bccae6eSSangbeom Kim static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info) 4895bccae6eSSangbeom Kim { 490756d5282SGustavo A. R. Silva u8 data[RTC_MAX_NUM_TIME_REGS]; 4915bccae6eSSangbeom Kim int ret, i; 4925bccae6eSSangbeom Kim struct rtc_time tm; 4935bccae6eSSangbeom Kim 494f8b23bbdSKrzysztof Kozlowski ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data, 495f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 4965bccae6eSSangbeom Kim if (ret < 0) 4975bccae6eSSangbeom Kim return ret; 4985bccae6eSSangbeom Kim 4995bccae6eSSangbeom Kim s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode); 5005527842eSAndy Shevchenko dev_dbg(info->dev, "%s: %ptR(%d)\n", __func__, &tm, tm.tm_wday); 5015bccae6eSSangbeom Kim 5025bccae6eSSangbeom Kim switch (info->device_type) { 5035bccae6eSSangbeom Kim case S5M8763X: 504602cb5bbSKrzysztof Kozlowski ret = regmap_write(info->regmap, S5M_ALARM0_CONF, 0); 5055bccae6eSSangbeom Kim break; 5065bccae6eSSangbeom Kim 5075bccae6eSSangbeom Kim case S5M8767X: 508a65e5efaSAlim Akhtar case S2MPS15X: 5090c5deb1eSKrzysztof Kozlowski case S2MPS14X: 5105281f94aSKrzysztof Kozlowski case S2MPS13X: 511f8b23bbdSKrzysztof Kozlowski for (i = 0; i < info->regs->regs_count; i++) 5125bccae6eSSangbeom Kim data[i] &= ~ALARM_ENABLE_MASK; 5135bccae6eSSangbeom Kim 514f8b23bbdSKrzysztof Kozlowski ret = regmap_raw_write(info->regmap, info->regs->alarm0, data, 515f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 5165bccae6eSSangbeom Kim if (ret < 0) 5175bccae6eSSangbeom Kim return ret; 5185bccae6eSSangbeom Kim 5195bccae6eSSangbeom Kim ret = s5m8767_rtc_set_alarm_reg(info); 5205bccae6eSSangbeom Kim 5215bccae6eSSangbeom Kim break; 5225bccae6eSSangbeom Kim 5235bccae6eSSangbeom Kim default: 5245bccae6eSSangbeom Kim return -EINVAL; 5255bccae6eSSangbeom Kim } 5265bccae6eSSangbeom Kim 5275bccae6eSSangbeom Kim return ret; 5285bccae6eSSangbeom Kim } 5295bccae6eSSangbeom Kim 5305bccae6eSSangbeom Kim static int s5m_rtc_start_alarm(struct s5m_rtc_info *info) 5315bccae6eSSangbeom Kim { 5325bccae6eSSangbeom Kim int ret; 533756d5282SGustavo A. R. Silva u8 data[RTC_MAX_NUM_TIME_REGS]; 5345bccae6eSSangbeom Kim u8 alarm0_conf; 5355bccae6eSSangbeom Kim struct rtc_time tm; 5365bccae6eSSangbeom Kim 537f8b23bbdSKrzysztof Kozlowski ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data, 538f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 5395bccae6eSSangbeom Kim if (ret < 0) 5405bccae6eSSangbeom Kim return ret; 5415bccae6eSSangbeom Kim 5425bccae6eSSangbeom Kim s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode); 5435527842eSAndy Shevchenko dev_dbg(info->dev, "%s: %ptR(%d)\n", __func__, &tm, tm.tm_wday); 5445bccae6eSSangbeom Kim 5455bccae6eSSangbeom Kim switch (info->device_type) { 5465bccae6eSSangbeom Kim case S5M8763X: 5475bccae6eSSangbeom Kim alarm0_conf = 0x77; 548602cb5bbSKrzysztof Kozlowski ret = regmap_write(info->regmap, S5M_ALARM0_CONF, alarm0_conf); 5495bccae6eSSangbeom Kim break; 5505bccae6eSSangbeom Kim 5515bccae6eSSangbeom Kim case S5M8767X: 552a65e5efaSAlim Akhtar case S2MPS15X: 5530c5deb1eSKrzysztof Kozlowski case S2MPS14X: 5545281f94aSKrzysztof Kozlowski case S2MPS13X: 5555bccae6eSSangbeom Kim data[RTC_SEC] |= ALARM_ENABLE_MASK; 5565bccae6eSSangbeom Kim data[RTC_MIN] |= ALARM_ENABLE_MASK; 5575bccae6eSSangbeom Kim data[RTC_HOUR] |= ALARM_ENABLE_MASK; 5585bccae6eSSangbeom Kim data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK; 5595bccae6eSSangbeom Kim if (data[RTC_DATE] & 0x1f) 5605bccae6eSSangbeom Kim data[RTC_DATE] |= ALARM_ENABLE_MASK; 5615bccae6eSSangbeom Kim if (data[RTC_MONTH] & 0xf) 5625bccae6eSSangbeom Kim data[RTC_MONTH] |= ALARM_ENABLE_MASK; 5635bccae6eSSangbeom Kim if (data[RTC_YEAR1] & 0x7f) 5645bccae6eSSangbeom Kim data[RTC_YEAR1] |= ALARM_ENABLE_MASK; 5655bccae6eSSangbeom Kim 566f8b23bbdSKrzysztof Kozlowski ret = regmap_raw_write(info->regmap, info->regs->alarm0, data, 567f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 5685bccae6eSSangbeom Kim if (ret < 0) 5695bccae6eSSangbeom Kim return ret; 5705bccae6eSSangbeom Kim ret = s5m8767_rtc_set_alarm_reg(info); 5715bccae6eSSangbeom Kim 5725bccae6eSSangbeom Kim break; 5735bccae6eSSangbeom Kim 5745bccae6eSSangbeom Kim default: 5755bccae6eSSangbeom Kim return -EINVAL; 5765bccae6eSSangbeom Kim } 5775bccae6eSSangbeom Kim 5785bccae6eSSangbeom Kim return ret; 5795bccae6eSSangbeom Kim } 5805bccae6eSSangbeom Kim 5815bccae6eSSangbeom Kim static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 5825bccae6eSSangbeom Kim { 5835bccae6eSSangbeom Kim struct s5m_rtc_info *info = dev_get_drvdata(dev); 584756d5282SGustavo A. R. Silva u8 data[RTC_MAX_NUM_TIME_REGS]; 5855bccae6eSSangbeom Kim int ret; 5865bccae6eSSangbeom Kim 5875bccae6eSSangbeom Kim switch (info->device_type) { 5885bccae6eSSangbeom Kim case S5M8763X: 5895bccae6eSSangbeom Kim s5m8763_tm_to_data(&alrm->time, data); 5905bccae6eSSangbeom Kim break; 5915bccae6eSSangbeom Kim 5925bccae6eSSangbeom Kim case S5M8767X: 593a65e5efaSAlim Akhtar case S2MPS15X: 5940c5deb1eSKrzysztof Kozlowski case S2MPS14X: 5955281f94aSKrzysztof Kozlowski case S2MPS13X: 5965bccae6eSSangbeom Kim s5m8767_tm_to_data(&alrm->time, data); 5975bccae6eSSangbeom Kim break; 5985bccae6eSSangbeom Kim 5995bccae6eSSangbeom Kim default: 6005bccae6eSSangbeom Kim return -EINVAL; 6015bccae6eSSangbeom Kim } 6025bccae6eSSangbeom Kim 6035527842eSAndy Shevchenko dev_dbg(dev, "%s: %ptR(%d)\n", __func__, &alrm->time, alrm->time.tm_wday); 6045bccae6eSSangbeom Kim 6055bccae6eSSangbeom Kim ret = s5m_rtc_stop_alarm(info); 6065bccae6eSSangbeom Kim if (ret < 0) 6075bccae6eSSangbeom Kim return ret; 6085bccae6eSSangbeom Kim 609f8b23bbdSKrzysztof Kozlowski ret = regmap_raw_write(info->regmap, info->regs->alarm0, data, 610f8b23bbdSKrzysztof Kozlowski info->regs->regs_count); 6115bccae6eSSangbeom Kim if (ret < 0) 6125bccae6eSSangbeom Kim return ret; 6135bccae6eSSangbeom Kim 6145bccae6eSSangbeom Kim ret = s5m8767_rtc_set_alarm_reg(info); 6155bccae6eSSangbeom Kim if (ret < 0) 6165bccae6eSSangbeom Kim return ret; 6175bccae6eSSangbeom Kim 6185bccae6eSSangbeom Kim if (alrm->enabled) 6195bccae6eSSangbeom Kim ret = s5m_rtc_start_alarm(info); 6205bccae6eSSangbeom Kim 6215bccae6eSSangbeom Kim return ret; 6225bccae6eSSangbeom Kim } 6235bccae6eSSangbeom Kim 6245bccae6eSSangbeom Kim static int s5m_rtc_alarm_irq_enable(struct device *dev, 6255bccae6eSSangbeom Kim unsigned int enabled) 6265bccae6eSSangbeom Kim { 6275bccae6eSSangbeom Kim struct s5m_rtc_info *info = dev_get_drvdata(dev); 6285bccae6eSSangbeom Kim 6295bccae6eSSangbeom Kim if (enabled) 6305bccae6eSSangbeom Kim return s5m_rtc_start_alarm(info); 6315bccae6eSSangbeom Kim else 6325bccae6eSSangbeom Kim return s5m_rtc_stop_alarm(info); 6335bccae6eSSangbeom Kim } 6345bccae6eSSangbeom Kim 6355bccae6eSSangbeom Kim static irqreturn_t s5m_rtc_alarm_irq(int irq, void *data) 6365bccae6eSSangbeom Kim { 6375bccae6eSSangbeom Kim struct s5m_rtc_info *info = data; 6385bccae6eSSangbeom Kim 6395bccae6eSSangbeom Kim rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF); 6405bccae6eSSangbeom Kim 6415bccae6eSSangbeom Kim return IRQ_HANDLED; 6425bccae6eSSangbeom Kim } 6435bccae6eSSangbeom Kim 6445bccae6eSSangbeom Kim static const struct rtc_class_ops s5m_rtc_ops = { 6455bccae6eSSangbeom Kim .read_time = s5m_rtc_read_time, 6465bccae6eSSangbeom Kim .set_time = s5m_rtc_set_time, 6475bccae6eSSangbeom Kim .read_alarm = s5m_rtc_read_alarm, 6485bccae6eSSangbeom Kim .set_alarm = s5m_rtc_set_alarm, 6495bccae6eSSangbeom Kim .alarm_irq_enable = s5m_rtc_alarm_irq_enable, 6505bccae6eSSangbeom Kim }; 6515bccae6eSSangbeom Kim 6525bccae6eSSangbeom Kim static int s5m8767_rtc_init_reg(struct s5m_rtc_info *info) 6535bccae6eSSangbeom Kim { 6545bccae6eSSangbeom Kim u8 data[2]; 6555bccae6eSSangbeom Kim int ret; 6565bccae6eSSangbeom Kim 6570c5deb1eSKrzysztof Kozlowski switch (info->device_type) { 6580c5deb1eSKrzysztof Kozlowski case S5M8763X: 6590c5deb1eSKrzysztof Kozlowski case S5M8767X: 6600c5f5d9aSKrzysztof Kozlowski /* UDR update time. Default of 7.32 ms is too long. */ 6610c5f5d9aSKrzysztof Kozlowski ret = regmap_update_bits(info->regmap, S5M_RTC_UDR_CON, 6620c5f5d9aSKrzysztof Kozlowski S5M_RTC_UDR_T_MASK, S5M_RTC_UDR_T_450_US); 6630c5f5d9aSKrzysztof Kozlowski if (ret < 0) 6640c5f5d9aSKrzysztof Kozlowski dev_err(info->dev, "%s: fail to change UDR time: %d\n", 6650c5f5d9aSKrzysztof Kozlowski __func__, ret); 6660c5f5d9aSKrzysztof Kozlowski 6675bccae6eSSangbeom Kim /* Set RTC control register : Binary mode, 24hour mode */ 6685bccae6eSSangbeom Kim data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); 6695bccae6eSSangbeom Kim data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); 6705bccae6eSSangbeom Kim 671602cb5bbSKrzysztof Kozlowski ret = regmap_raw_write(info->regmap, S5M_ALARM0_CONF, data, 2); 6720c5deb1eSKrzysztof Kozlowski break; 6730c5deb1eSKrzysztof Kozlowski 674a65e5efaSAlim Akhtar case S2MPS15X: 6750c5deb1eSKrzysztof Kozlowski case S2MPS14X: 6765281f94aSKrzysztof Kozlowski case S2MPS13X: 6770c5deb1eSKrzysztof Kozlowski data[0] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); 6780c5deb1eSKrzysztof Kozlowski ret = regmap_write(info->regmap, info->regs->ctrl, data[0]); 679ff02c044SJoonyoung Shim if (ret < 0) 680ff02c044SJoonyoung Shim break; 681ff02c044SJoonyoung Shim 682ff02c044SJoonyoung Shim /* 683ff02c044SJoonyoung Shim * Should set WUDR & (RUDR or AUDR) bits to high after writing 684ff02c044SJoonyoung Shim * RTC_CTRL register like writing Alarm registers. We can't find 685ff02c044SJoonyoung Shim * the description from datasheet but vendor code does that 686ff02c044SJoonyoung Shim * really. 687ff02c044SJoonyoung Shim */ 688ff02c044SJoonyoung Shim ret = s5m8767_rtc_set_alarm_reg(info); 6890c5deb1eSKrzysztof Kozlowski break; 6900c5deb1eSKrzysztof Kozlowski 6910c5deb1eSKrzysztof Kozlowski default: 6920c5deb1eSKrzysztof Kozlowski return -EINVAL; 6930c5deb1eSKrzysztof Kozlowski } 6940c5deb1eSKrzysztof Kozlowski 6950c5deb1eSKrzysztof Kozlowski info->rtc_24hr_mode = 1; 6965bccae6eSSangbeom Kim if (ret < 0) { 6975bccae6eSSangbeom Kim dev_err(info->dev, "%s: fail to write controlm reg(%d)\n", 6985bccae6eSSangbeom Kim __func__, ret); 6995bccae6eSSangbeom Kim return ret; 7005bccae6eSSangbeom Kim } 7015bccae6eSSangbeom Kim 7025bccae6eSSangbeom Kim return ret; 7035bccae6eSSangbeom Kim } 7045bccae6eSSangbeom Kim 7055bccae6eSSangbeom Kim static int s5m_rtc_probe(struct platform_device *pdev) 7065bccae6eSSangbeom Kim { 7075bccae6eSSangbeom Kim struct sec_pmic_dev *s5m87xx = dev_get_drvdata(pdev->dev.parent); 7085bccae6eSSangbeom Kim struct s5m_rtc_info *info; 709e349c910SKrzysztof Kozlowski const struct regmap_config *regmap_cfg; 710a0347f20SKrzysztof Kozlowski int ret, alarm_irq; 7115bccae6eSSangbeom Kim 7125bccae6eSSangbeom Kim info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); 7135bccae6eSSangbeom Kim if (!info) 7145bccae6eSSangbeom Kim return -ENOMEM; 7155bccae6eSSangbeom Kim 71694f91922SKrzysztof Kozlowski switch (platform_get_device_id(pdev)->driver_data) { 717a65e5efaSAlim Akhtar case S2MPS15X: 7188ae83b6fSKrzysztof Kozlowski regmap_cfg = &s2mps14_rtc_regmap_config; 7198ae83b6fSKrzysztof Kozlowski info->regs = &s2mps15_rtc_regs; 7208ae83b6fSKrzysztof Kozlowski alarm_irq = S2MPS14_IRQ_RTCA0; 7218ae83b6fSKrzysztof Kozlowski break; 722e349c910SKrzysztof Kozlowski case S2MPS14X: 7238ae83b6fSKrzysztof Kozlowski regmap_cfg = &s2mps14_rtc_regmap_config; 7248ae83b6fSKrzysztof Kozlowski info->regs = &s2mps14_rtc_regs; 7258ae83b6fSKrzysztof Kozlowski alarm_irq = S2MPS14_IRQ_RTCA0; 7268ae83b6fSKrzysztof Kozlowski break; 7275281f94aSKrzysztof Kozlowski case S2MPS13X: 728e349c910SKrzysztof Kozlowski regmap_cfg = &s2mps14_rtc_regmap_config; 7298ae83b6fSKrzysztof Kozlowski info->regs = &s2mps13_rtc_regs; 730a0347f20SKrzysztof Kozlowski alarm_irq = S2MPS14_IRQ_RTCA0; 731e349c910SKrzysztof Kozlowski break; 732e349c910SKrzysztof Kozlowski case S5M8763X: 733e349c910SKrzysztof Kozlowski regmap_cfg = &s5m_rtc_regmap_config; 734f8b23bbdSKrzysztof Kozlowski info->regs = &s5m_rtc_regs; 735a0347f20SKrzysztof Kozlowski alarm_irq = S5M8763_IRQ_ALARM0; 736e349c910SKrzysztof Kozlowski break; 737e349c910SKrzysztof Kozlowski case S5M8767X: 738e349c910SKrzysztof Kozlowski regmap_cfg = &s5m_rtc_regmap_config; 739f8b23bbdSKrzysztof Kozlowski info->regs = &s5m_rtc_regs; 740a0347f20SKrzysztof Kozlowski alarm_irq = S5M8767_IRQ_RTCA1; 741e349c910SKrzysztof Kozlowski break; 742e349c910SKrzysztof Kozlowski default: 74394f91922SKrzysztof Kozlowski dev_err(&pdev->dev, 74494f91922SKrzysztof Kozlowski "Device type %lu is not supported by RTC driver\n", 74594f91922SKrzysztof Kozlowski platform_get_device_id(pdev)->driver_data); 746e349c910SKrzysztof Kozlowski return -ENODEV; 747e349c910SKrzysztof Kozlowski } 748e349c910SKrzysztof Kozlowski 7497db7ad08SBartosz Golaszewski info->i2c = devm_i2c_new_dummy_device(&pdev->dev, s5m87xx->i2c->adapter, 7507db7ad08SBartosz Golaszewski RTC_I2C_ADDR); 751aae364d2SWolfram Sang if (IS_ERR(info->i2c)) { 752e349c910SKrzysztof Kozlowski dev_err(&pdev->dev, "Failed to allocate I2C for RTC\n"); 753aae364d2SWolfram Sang return PTR_ERR(info->i2c); 754e349c910SKrzysztof Kozlowski } 755e349c910SKrzysztof Kozlowski 756e349c910SKrzysztof Kozlowski info->regmap = devm_regmap_init_i2c(info->i2c, regmap_cfg); 757e349c910SKrzysztof Kozlowski if (IS_ERR(info->regmap)) { 758e349c910SKrzysztof Kozlowski ret = PTR_ERR(info->regmap); 759e349c910SKrzysztof Kozlowski dev_err(&pdev->dev, "Failed to allocate RTC register map: %d\n", 760e349c910SKrzysztof Kozlowski ret); 7617db7ad08SBartosz Golaszewski return ret; 762e349c910SKrzysztof Kozlowski } 763e349c910SKrzysztof Kozlowski 7645bccae6eSSangbeom Kim info->dev = &pdev->dev; 7655bccae6eSSangbeom Kim info->s5m87xx = s5m87xx; 76694f91922SKrzysztof Kozlowski info->device_type = platform_get_device_id(pdev)->driver_data; 7675bccae6eSSangbeom Kim 768b7d5b9a9SBartlomiej Zolnierkiewicz if (s5m87xx->irq_data) { 769a0347f20SKrzysztof Kozlowski info->irq = regmap_irq_get_virq(s5m87xx->irq_data, alarm_irq); 770a0347f20SKrzysztof Kozlowski if (info->irq <= 0) { 771a0347f20SKrzysztof Kozlowski dev_err(&pdev->dev, "Failed to get virtual IRQ %d\n", 772a0347f20SKrzysztof Kozlowski alarm_irq); 7737db7ad08SBartosz Golaszewski return -EINVAL; 7745bccae6eSSangbeom Kim } 775b7d5b9a9SBartlomiej Zolnierkiewicz } 7765bccae6eSSangbeom Kim 7775bccae6eSSangbeom Kim platform_set_drvdata(pdev, info); 7785bccae6eSSangbeom Kim 7795bccae6eSSangbeom Kim ret = s5m8767_rtc_init_reg(info); 7803be95d27SBartosz Golaszewski if (ret) 7813be95d27SBartosz Golaszewski return ret; 7825bccae6eSSangbeom Kim 783dae68c6bSAlexandre Belloni info->rtc_dev = devm_rtc_allocate_device(&pdev->dev); 7847db7ad08SBartosz Golaszewski if (IS_ERR(info->rtc_dev)) 7857db7ad08SBartosz Golaszewski return PTR_ERR(info->rtc_dev); 7865bccae6eSSangbeom Kim 787dae68c6bSAlexandre Belloni info->rtc_dev->ops = &s5m_rtc_ops; 788dae68c6bSAlexandre Belloni 789*fffd603aSAlexandre Belloni if (info->device_type == S5M8763X) { 790*fffd603aSAlexandre Belloni info->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_0000; 791*fffd603aSAlexandre Belloni info->rtc_dev->range_max = RTC_TIMESTAMP_END_9999; 792*fffd603aSAlexandre Belloni } else { 793*fffd603aSAlexandre Belloni info->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000; 794*fffd603aSAlexandre Belloni info->rtc_dev->range_max = RTC_TIMESTAMP_END_2099; 795*fffd603aSAlexandre Belloni } 796*fffd603aSAlexandre Belloni 797b7d5b9a9SBartlomiej Zolnierkiewicz if (!info->irq) { 7981ed4dba2SAlexandre Belloni clear_bit(RTC_FEATURE_ALARM, info->rtc_dev->features); 7991ed4dba2SAlexandre Belloni } else { 8005bccae6eSSangbeom Kim ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL, 8015bccae6eSSangbeom Kim s5m_rtc_alarm_irq, 0, "rtc-alarm0", 8025bccae6eSSangbeom Kim info); 803e349c910SKrzysztof Kozlowski if (ret < 0) { 8045bccae6eSSangbeom Kim dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n", 8055bccae6eSSangbeom Kim info->irq, ret); 8065bccae6eSSangbeom Kim return ret; 8075bccae6eSSangbeom Kim } 808308247d2SAlexandre Belloni device_init_wakeup(&pdev->dev, 1); 8091ed4dba2SAlexandre Belloni } 8105bccae6eSSangbeom Kim 8111ed4dba2SAlexandre Belloni return devm_rtc_register_device(info->rtc_dev); 812e349c910SKrzysztof Kozlowski } 813e349c910SKrzysztof Kozlowski 81411ba5a1eSGeert Uytterhoeven #ifdef CONFIG_PM_SLEEP 815222ead7fSKrzysztof Kozlowski static int s5m_rtc_resume(struct device *dev) 816222ead7fSKrzysztof Kozlowski { 817222ead7fSKrzysztof Kozlowski struct s5m_rtc_info *info = dev_get_drvdata(dev); 818222ead7fSKrzysztof Kozlowski int ret = 0; 819222ead7fSKrzysztof Kozlowski 820b7d5b9a9SBartlomiej Zolnierkiewicz if (info->irq && device_may_wakeup(dev)) 821222ead7fSKrzysztof Kozlowski ret = disable_irq_wake(info->irq); 822222ead7fSKrzysztof Kozlowski 823222ead7fSKrzysztof Kozlowski return ret; 824222ead7fSKrzysztof Kozlowski } 825222ead7fSKrzysztof Kozlowski 826222ead7fSKrzysztof Kozlowski static int s5m_rtc_suspend(struct device *dev) 827222ead7fSKrzysztof Kozlowski { 828222ead7fSKrzysztof Kozlowski struct s5m_rtc_info *info = dev_get_drvdata(dev); 829222ead7fSKrzysztof Kozlowski int ret = 0; 830222ead7fSKrzysztof Kozlowski 831b7d5b9a9SBartlomiej Zolnierkiewicz if (info->irq && device_may_wakeup(dev)) 832222ead7fSKrzysztof Kozlowski ret = enable_irq_wake(info->irq); 833222ead7fSKrzysztof Kozlowski 834222ead7fSKrzysztof Kozlowski return ret; 835222ead7fSKrzysztof Kozlowski } 83611ba5a1eSGeert Uytterhoeven #endif /* CONFIG_PM_SLEEP */ 837222ead7fSKrzysztof Kozlowski 838222ead7fSKrzysztof Kozlowski static SIMPLE_DEV_PM_OPS(s5m_rtc_pm_ops, s5m_rtc_suspend, s5m_rtc_resume); 839222ead7fSKrzysztof Kozlowski 8405bccae6eSSangbeom Kim static const struct platform_device_id s5m_rtc_id[] = { 8410c5deb1eSKrzysztof Kozlowski { "s5m-rtc", S5M8767X }, 8425281f94aSKrzysztof Kozlowski { "s2mps13-rtc", S2MPS13X }, 8430c5deb1eSKrzysztof Kozlowski { "s2mps14-rtc", S2MPS14X }, 844a65e5efaSAlim Akhtar { "s2mps15-rtc", S2MPS15X }, 84545cd15e6SAndrey Ryabinin { }, 8465bccae6eSSangbeom Kim }; 84763074cc3SJavier Martinez Canillas MODULE_DEVICE_TABLE(platform, s5m_rtc_id); 8485bccae6eSSangbeom Kim 8495bccae6eSSangbeom Kim static struct platform_driver s5m_rtc_driver = { 8505bccae6eSSangbeom Kim .driver = { 8515bccae6eSSangbeom Kim .name = "s5m-rtc", 852222ead7fSKrzysztof Kozlowski .pm = &s5m_rtc_pm_ops, 8535bccae6eSSangbeom Kim }, 8545bccae6eSSangbeom Kim .probe = s5m_rtc_probe, 8555bccae6eSSangbeom Kim .id_table = s5m_rtc_id, 8565bccae6eSSangbeom Kim }; 8575bccae6eSSangbeom Kim 8585bccae6eSSangbeom Kim module_platform_driver(s5m_rtc_driver); 8595bccae6eSSangbeom Kim 8605bccae6eSSangbeom Kim /* Module information */ 8615bccae6eSSangbeom Kim MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>"); 8620c5deb1eSKrzysztof Kozlowski MODULE_DESCRIPTION("Samsung S5M/S2MPS14 RTC driver"); 8635bccae6eSSangbeom Kim MODULE_LICENSE("GPL"); 8645bccae6eSSangbeom Kim MODULE_ALIAS("platform:s5m-rtc"); 865