xref: /linux/drivers/rtc/rtc-s5m.c (revision 0c5f5d9af311013aabc519b68df19533d0d51cda)
15bccae6eSSangbeom Kim /*
25bccae6eSSangbeom Kim  * Copyright (c) 2013 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/slab.h>
215bccae6eSSangbeom Kim #include <linux/bcd.h>
225bccae6eSSangbeom Kim #include <linux/bitops.h>
235bccae6eSSangbeom Kim #include <linux/regmap.h>
245bccae6eSSangbeom Kim #include <linux/rtc.h>
255bccae6eSSangbeom Kim #include <linux/delay.h>
265bccae6eSSangbeom Kim #include <linux/platform_device.h>
275bccae6eSSangbeom Kim #include <linux/mfd/samsung/core.h>
285bccae6eSSangbeom Kim #include <linux/mfd/samsung/irq.h>
295bccae6eSSangbeom Kim #include <linux/mfd/samsung/rtc.h>
305bccae6eSSangbeom Kim 
31d73238d4SKrzysztof Kozlowski /*
32d73238d4SKrzysztof Kozlowski  * Maximum number of retries for checking changes in UDR field
33602cb5bbSKrzysztof Kozlowski  * of S5M_RTC_UDR_CON register (to limit possible endless loop).
34d73238d4SKrzysztof Kozlowski  *
35d73238d4SKrzysztof Kozlowski  * After writing to RTC registers (setting time or alarm) read the UDR field
36602cb5bbSKrzysztof Kozlowski  * in S5M_RTC_UDR_CON register. UDR is auto-cleared when data have
37d73238d4SKrzysztof Kozlowski  * been transferred.
38d73238d4SKrzysztof Kozlowski  */
39d73238d4SKrzysztof Kozlowski #define UDR_READ_RETRY_CNT	5
40d73238d4SKrzysztof Kozlowski 
415bccae6eSSangbeom Kim struct s5m_rtc_info {
425bccae6eSSangbeom Kim 	struct device *dev;
43e349c910SKrzysztof Kozlowski 	struct i2c_client *i2c;
445bccae6eSSangbeom Kim 	struct sec_pmic_dev *s5m87xx;
455ccb7d71SGeert Uytterhoeven 	struct regmap *regmap;
465bccae6eSSangbeom Kim 	struct rtc_device *rtc_dev;
475bccae6eSSangbeom Kim 	int irq;
485bccae6eSSangbeom Kim 	int device_type;
495bccae6eSSangbeom Kim 	int rtc_24hr_mode;
505bccae6eSSangbeom Kim 	bool wtsr_smpl;
515bccae6eSSangbeom Kim };
525bccae6eSSangbeom Kim 
53e349c910SKrzysztof Kozlowski static const struct regmap_config s5m_rtc_regmap_config = {
54e349c910SKrzysztof Kozlowski 	.reg_bits = 8,
55e349c910SKrzysztof Kozlowski 	.val_bits = 8,
56e349c910SKrzysztof Kozlowski 
57602cb5bbSKrzysztof Kozlowski 	.max_register = S5M_RTC_REG_MAX,
58e349c910SKrzysztof Kozlowski };
59e349c910SKrzysztof Kozlowski 
60e349c910SKrzysztof Kozlowski static const struct regmap_config s2mps14_rtc_regmap_config = {
61e349c910SKrzysztof Kozlowski 	.reg_bits = 8,
62e349c910SKrzysztof Kozlowski 	.val_bits = 8,
63e349c910SKrzysztof Kozlowski 
64e349c910SKrzysztof Kozlowski 	.max_register = S2MPS_RTC_REG_MAX,
65e349c910SKrzysztof Kozlowski };
66e349c910SKrzysztof Kozlowski 
675bccae6eSSangbeom Kim static void s5m8767_data_to_tm(u8 *data, struct rtc_time *tm,
685bccae6eSSangbeom Kim 			       int rtc_24hr_mode)
695bccae6eSSangbeom Kim {
705bccae6eSSangbeom Kim 	tm->tm_sec = data[RTC_SEC] & 0x7f;
715bccae6eSSangbeom Kim 	tm->tm_min = data[RTC_MIN] & 0x7f;
725bccae6eSSangbeom Kim 	if (rtc_24hr_mode) {
735bccae6eSSangbeom Kim 		tm->tm_hour = data[RTC_HOUR] & 0x1f;
745bccae6eSSangbeom Kim 	} else {
755bccae6eSSangbeom Kim 		tm->tm_hour = data[RTC_HOUR] & 0x0f;
765bccae6eSSangbeom Kim 		if (data[RTC_HOUR] & HOUR_PM_MASK)
775bccae6eSSangbeom Kim 			tm->tm_hour += 12;
785bccae6eSSangbeom Kim 	}
795bccae6eSSangbeom Kim 
805bccae6eSSangbeom Kim 	tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f);
815bccae6eSSangbeom Kim 	tm->tm_mday = data[RTC_DATE] & 0x1f;
825bccae6eSSangbeom Kim 	tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
835bccae6eSSangbeom Kim 	tm->tm_year = (data[RTC_YEAR1] & 0x7f) + 100;
845bccae6eSSangbeom Kim 	tm->tm_yday = 0;
855bccae6eSSangbeom Kim 	tm->tm_isdst = 0;
865bccae6eSSangbeom Kim }
875bccae6eSSangbeom Kim 
885bccae6eSSangbeom Kim static int s5m8767_tm_to_data(struct rtc_time *tm, u8 *data)
895bccae6eSSangbeom Kim {
905bccae6eSSangbeom Kim 	data[RTC_SEC] = tm->tm_sec;
915bccae6eSSangbeom Kim 	data[RTC_MIN] = tm->tm_min;
925bccae6eSSangbeom Kim 
935bccae6eSSangbeom Kim 	if (tm->tm_hour >= 12)
945bccae6eSSangbeom Kim 		data[RTC_HOUR] = tm->tm_hour | HOUR_PM_MASK;
955bccae6eSSangbeom Kim 	else
965bccae6eSSangbeom Kim 		data[RTC_HOUR] = tm->tm_hour & ~HOUR_PM_MASK;
975bccae6eSSangbeom Kim 
985bccae6eSSangbeom Kim 	data[RTC_WEEKDAY] = 1 << tm->tm_wday;
995bccae6eSSangbeom Kim 	data[RTC_DATE] = tm->tm_mday;
1005bccae6eSSangbeom Kim 	data[RTC_MONTH] = tm->tm_mon + 1;
1015bccae6eSSangbeom Kim 	data[RTC_YEAR1] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
1025bccae6eSSangbeom Kim 
1035bccae6eSSangbeom Kim 	if (tm->tm_year < 100) {
1045bccae6eSSangbeom Kim 		pr_err("s5m8767 RTC cannot handle the year %d.\n",
1055bccae6eSSangbeom Kim 		       1900 + tm->tm_year);
1065bccae6eSSangbeom Kim 		return -EINVAL;
1075bccae6eSSangbeom Kim 	} else {
1085bccae6eSSangbeom Kim 		return 0;
1095bccae6eSSangbeom Kim 	}
1105bccae6eSSangbeom Kim }
1115bccae6eSSangbeom Kim 
112d73238d4SKrzysztof Kozlowski /*
113d73238d4SKrzysztof Kozlowski  * Read RTC_UDR_CON register and wait till UDR field is cleared.
114d73238d4SKrzysztof Kozlowski  * This indicates that time/alarm update ended.
115d73238d4SKrzysztof Kozlowski  */
116d73238d4SKrzysztof Kozlowski static inline int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info)
117d73238d4SKrzysztof Kozlowski {
118d73238d4SKrzysztof Kozlowski 	int ret, retry = UDR_READ_RETRY_CNT;
119d73238d4SKrzysztof Kozlowski 	unsigned int data;
120d73238d4SKrzysztof Kozlowski 
121d73238d4SKrzysztof Kozlowski 	do {
122602cb5bbSKrzysztof Kozlowski 		ret = regmap_read(info->regmap, S5M_RTC_UDR_CON, &data);
123602cb5bbSKrzysztof Kozlowski 	} while (--retry && (data & S5M_RTC_UDR_MASK) && !ret);
124d73238d4SKrzysztof Kozlowski 
125d73238d4SKrzysztof Kozlowski 	if (!retry)
126d73238d4SKrzysztof Kozlowski 		dev_err(info->dev, "waiting for UDR update, reached max number of retries\n");
127d73238d4SKrzysztof Kozlowski 
128d73238d4SKrzysztof Kozlowski 	return ret;
129d73238d4SKrzysztof Kozlowski }
130d73238d4SKrzysztof Kozlowski 
1315bccae6eSSangbeom Kim static inline int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info)
1325bccae6eSSangbeom Kim {
1335bccae6eSSangbeom Kim 	int ret;
1345bccae6eSSangbeom Kim 	unsigned int data;
1355bccae6eSSangbeom Kim 
136602cb5bbSKrzysztof Kozlowski 	ret = regmap_read(info->regmap, S5M_RTC_UDR_CON, &data);
1375bccae6eSSangbeom Kim 	if (ret < 0) {
1385bccae6eSSangbeom Kim 		dev_err(info->dev, "failed to read update reg(%d)\n", ret);
1395bccae6eSSangbeom Kim 		return ret;
1405bccae6eSSangbeom Kim 	}
1415bccae6eSSangbeom Kim 
142602cb5bbSKrzysztof Kozlowski 	data |= S5M_RTC_TIME_EN_MASK;
143602cb5bbSKrzysztof Kozlowski 	data |= S5M_RTC_UDR_MASK;
1445bccae6eSSangbeom Kim 
145602cb5bbSKrzysztof Kozlowski 	ret = regmap_write(info->regmap, S5M_RTC_UDR_CON, data);
1465bccae6eSSangbeom Kim 	if (ret < 0) {
1475bccae6eSSangbeom Kim 		dev_err(info->dev, "failed to write update reg(%d)\n", ret);
1485bccae6eSSangbeom Kim 		return ret;
1495bccae6eSSangbeom Kim 	}
1505bccae6eSSangbeom Kim 
151d73238d4SKrzysztof Kozlowski 	ret = s5m8767_wait_for_udr_update(info);
1525bccae6eSSangbeom Kim 
1535bccae6eSSangbeom Kim 	return ret;
1545bccae6eSSangbeom Kim }
1555bccae6eSSangbeom Kim 
1565bccae6eSSangbeom Kim static inline int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info)
1575bccae6eSSangbeom Kim {
1585bccae6eSSangbeom Kim 	int ret;
1595bccae6eSSangbeom Kim 	unsigned int data;
1605bccae6eSSangbeom Kim 
161602cb5bbSKrzysztof Kozlowski 	ret = regmap_read(info->regmap, S5M_RTC_UDR_CON, &data);
1625bccae6eSSangbeom Kim 	if (ret < 0) {
1635bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to read update reg(%d)\n",
1645bccae6eSSangbeom Kim 			__func__, ret);
1655bccae6eSSangbeom Kim 		return ret;
1665bccae6eSSangbeom Kim 	}
1675bccae6eSSangbeom Kim 
168602cb5bbSKrzysztof Kozlowski 	data &= ~S5M_RTC_TIME_EN_MASK;
169602cb5bbSKrzysztof Kozlowski 	data |= S5M_RTC_UDR_MASK;
1705bccae6eSSangbeom Kim 
171602cb5bbSKrzysztof Kozlowski 	ret = regmap_write(info->regmap, S5M_RTC_UDR_CON, data);
1725bccae6eSSangbeom Kim 	if (ret < 0) {
1735bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to write update reg(%d)\n",
1745bccae6eSSangbeom Kim 			__func__, ret);
1755bccae6eSSangbeom Kim 		return ret;
1765bccae6eSSangbeom Kim 	}
1775bccae6eSSangbeom Kim 
178d73238d4SKrzysztof Kozlowski 	ret = s5m8767_wait_for_udr_update(info);
1795bccae6eSSangbeom Kim 
1805bccae6eSSangbeom Kim 	return ret;
1815bccae6eSSangbeom Kim }
1825bccae6eSSangbeom Kim 
1835bccae6eSSangbeom Kim static void s5m8763_data_to_tm(u8 *data, struct rtc_time *tm)
1845bccae6eSSangbeom Kim {
1855bccae6eSSangbeom Kim 	tm->tm_sec = bcd2bin(data[RTC_SEC]);
1865bccae6eSSangbeom Kim 	tm->tm_min = bcd2bin(data[RTC_MIN]);
1875bccae6eSSangbeom Kim 
1885bccae6eSSangbeom Kim 	if (data[RTC_HOUR] & HOUR_12) {
1895bccae6eSSangbeom Kim 		tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
1905bccae6eSSangbeom Kim 		if (data[RTC_HOUR] & HOUR_PM)
1915bccae6eSSangbeom Kim 			tm->tm_hour += 12;
1925bccae6eSSangbeom Kim 	} else {
1935bccae6eSSangbeom Kim 		tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
1945bccae6eSSangbeom Kim 	}
1955bccae6eSSangbeom Kim 
1965bccae6eSSangbeom Kim 	tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
1975bccae6eSSangbeom Kim 	tm->tm_mday = bcd2bin(data[RTC_DATE]);
1985bccae6eSSangbeom Kim 	tm->tm_mon = bcd2bin(data[RTC_MONTH]);
1995bccae6eSSangbeom Kim 	tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
2005bccae6eSSangbeom Kim 	tm->tm_year -= 1900;
2015bccae6eSSangbeom Kim }
2025bccae6eSSangbeom Kim 
2035bccae6eSSangbeom Kim static void s5m8763_tm_to_data(struct rtc_time *tm, u8 *data)
2045bccae6eSSangbeom Kim {
2055bccae6eSSangbeom Kim 	data[RTC_SEC] = bin2bcd(tm->tm_sec);
2065bccae6eSSangbeom Kim 	data[RTC_MIN] = bin2bcd(tm->tm_min);
2075bccae6eSSangbeom Kim 	data[RTC_HOUR] = bin2bcd(tm->tm_hour);
2085bccae6eSSangbeom Kim 	data[RTC_WEEKDAY] = tm->tm_wday;
2095bccae6eSSangbeom Kim 	data[RTC_DATE] = bin2bcd(tm->tm_mday);
2105bccae6eSSangbeom Kim 	data[RTC_MONTH] = bin2bcd(tm->tm_mon);
2115bccae6eSSangbeom Kim 	data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
2125bccae6eSSangbeom Kim 	data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
2135bccae6eSSangbeom Kim }
2145bccae6eSSangbeom Kim 
2155bccae6eSSangbeom Kim static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm)
2165bccae6eSSangbeom Kim {
2175bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
2185bccae6eSSangbeom Kim 	u8 data[8];
2195bccae6eSSangbeom Kim 	int ret;
2205bccae6eSSangbeom Kim 
221602cb5bbSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, S5M_RTC_SEC, data, 8);
2225bccae6eSSangbeom Kim 	if (ret < 0)
2235bccae6eSSangbeom Kim 		return ret;
2245bccae6eSSangbeom Kim 
2255bccae6eSSangbeom Kim 	switch (info->device_type) {
2265bccae6eSSangbeom Kim 	case S5M8763X:
2275bccae6eSSangbeom Kim 		s5m8763_data_to_tm(data, tm);
2285bccae6eSSangbeom Kim 		break;
2295bccae6eSSangbeom Kim 
2305bccae6eSSangbeom Kim 	case S5M8767X:
2315bccae6eSSangbeom Kim 		s5m8767_data_to_tm(data, tm, info->rtc_24hr_mode);
2325bccae6eSSangbeom Kim 		break;
2335bccae6eSSangbeom Kim 
2345bccae6eSSangbeom Kim 	default:
2355bccae6eSSangbeom Kim 		return -EINVAL;
2365bccae6eSSangbeom Kim 	}
2375bccae6eSSangbeom Kim 
2385bccae6eSSangbeom Kim 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
2395bccae6eSSangbeom Kim 		1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
2405bccae6eSSangbeom Kim 		tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
2415bccae6eSSangbeom Kim 
2425bccae6eSSangbeom Kim 	return rtc_valid_tm(tm);
2435bccae6eSSangbeom Kim }
2445bccae6eSSangbeom Kim 
2455bccae6eSSangbeom Kim static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm)
2465bccae6eSSangbeom Kim {
2475bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
2485bccae6eSSangbeom Kim 	u8 data[8];
2495bccae6eSSangbeom Kim 	int ret = 0;
2505bccae6eSSangbeom Kim 
2515bccae6eSSangbeom Kim 	switch (info->device_type) {
2525bccae6eSSangbeom Kim 	case S5M8763X:
2535bccae6eSSangbeom Kim 		s5m8763_tm_to_data(tm, data);
2545bccae6eSSangbeom Kim 		break;
2555bccae6eSSangbeom Kim 	case S5M8767X:
2565bccae6eSSangbeom Kim 		ret = s5m8767_tm_to_data(tm, data);
2575bccae6eSSangbeom Kim 		break;
2585bccae6eSSangbeom Kim 	default:
2595bccae6eSSangbeom Kim 		return -EINVAL;
2605bccae6eSSangbeom Kim 	}
2615bccae6eSSangbeom Kim 
2625bccae6eSSangbeom Kim 	if (ret < 0)
2635bccae6eSSangbeom Kim 		return ret;
2645bccae6eSSangbeom Kim 
2655bccae6eSSangbeom Kim 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
2665bccae6eSSangbeom Kim 		1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
2675bccae6eSSangbeom Kim 		tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
2685bccae6eSSangbeom Kim 
269602cb5bbSKrzysztof Kozlowski 	ret = regmap_raw_write(info->regmap, S5M_RTC_SEC, data, 8);
2705bccae6eSSangbeom Kim 	if (ret < 0)
2715bccae6eSSangbeom Kim 		return ret;
2725bccae6eSSangbeom Kim 
2735bccae6eSSangbeom Kim 	ret = s5m8767_rtc_set_time_reg(info);
2745bccae6eSSangbeom Kim 
2755bccae6eSSangbeom Kim 	return ret;
2765bccae6eSSangbeom Kim }
2775bccae6eSSangbeom Kim 
2785bccae6eSSangbeom Kim static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
2795bccae6eSSangbeom Kim {
2805bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
2815bccae6eSSangbeom Kim 	u8 data[8];
2825bccae6eSSangbeom Kim 	unsigned int val;
2835bccae6eSSangbeom Kim 	int ret, i;
2845bccae6eSSangbeom Kim 
285602cb5bbSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, S5M_ALARM0_SEC, data, 8);
2865bccae6eSSangbeom Kim 	if (ret < 0)
2875bccae6eSSangbeom Kim 		return ret;
2885bccae6eSSangbeom Kim 
2895bccae6eSSangbeom Kim 	switch (info->device_type) {
2905bccae6eSSangbeom Kim 	case S5M8763X:
2915bccae6eSSangbeom Kim 		s5m8763_data_to_tm(data, &alrm->time);
292602cb5bbSKrzysztof Kozlowski 		ret = regmap_read(info->regmap, S5M_ALARM0_CONF, &val);
2935bccae6eSSangbeom Kim 		if (ret < 0)
2945bccae6eSSangbeom Kim 			return ret;
2955bccae6eSSangbeom Kim 
2965bccae6eSSangbeom Kim 		alrm->enabled = !!val;
2975bccae6eSSangbeom Kim 
298602cb5bbSKrzysztof Kozlowski 		ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val);
2995bccae6eSSangbeom Kim 		if (ret < 0)
3005bccae6eSSangbeom Kim 			return ret;
3015bccae6eSSangbeom Kim 
3025bccae6eSSangbeom Kim 		break;
3035bccae6eSSangbeom Kim 
3045bccae6eSSangbeom Kim 	case S5M8767X:
3055bccae6eSSangbeom Kim 		s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
3065bccae6eSSangbeom Kim 		dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
3075bccae6eSSangbeom Kim 			1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
3085bccae6eSSangbeom Kim 			alrm->time.tm_mday, alrm->time.tm_hour,
3095bccae6eSSangbeom Kim 			alrm->time.tm_min, alrm->time.tm_sec,
3105bccae6eSSangbeom Kim 			alrm->time.tm_wday);
3115bccae6eSSangbeom Kim 
3125bccae6eSSangbeom Kim 		alrm->enabled = 0;
3135bccae6eSSangbeom Kim 		for (i = 0; i < 7; i++) {
3145bccae6eSSangbeom Kim 			if (data[i] & ALARM_ENABLE_MASK) {
3155bccae6eSSangbeom Kim 				alrm->enabled = 1;
3165bccae6eSSangbeom Kim 				break;
3175bccae6eSSangbeom Kim 			}
3185bccae6eSSangbeom Kim 		}
3195bccae6eSSangbeom Kim 
3205bccae6eSSangbeom Kim 		alrm->pending = 0;
321602cb5bbSKrzysztof Kozlowski 		ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val);
3225bccae6eSSangbeom Kim 		if (ret < 0)
3235bccae6eSSangbeom Kim 			return ret;
3245bccae6eSSangbeom Kim 		break;
3255bccae6eSSangbeom Kim 
3265bccae6eSSangbeom Kim 	default:
3275bccae6eSSangbeom Kim 		return -EINVAL;
3285bccae6eSSangbeom Kim 	}
3295bccae6eSSangbeom Kim 
330602cb5bbSKrzysztof Kozlowski 	if (val & S5M_ALARM0_STATUS)
3315bccae6eSSangbeom Kim 		alrm->pending = 1;
3325bccae6eSSangbeom Kim 	else
3335bccae6eSSangbeom Kim 		alrm->pending = 0;
3345bccae6eSSangbeom Kim 
3355bccae6eSSangbeom Kim 	return 0;
3365bccae6eSSangbeom Kim }
3375bccae6eSSangbeom Kim 
3385bccae6eSSangbeom Kim static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info)
3395bccae6eSSangbeom Kim {
3405bccae6eSSangbeom Kim 	u8 data[8];
3415bccae6eSSangbeom Kim 	int ret, i;
3425bccae6eSSangbeom Kim 	struct rtc_time tm;
3435bccae6eSSangbeom Kim 
344602cb5bbSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, S5M_ALARM0_SEC, data, 8);
3455bccae6eSSangbeom Kim 	if (ret < 0)
3465bccae6eSSangbeom Kim 		return ret;
3475bccae6eSSangbeom Kim 
3485bccae6eSSangbeom Kim 	s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
3495bccae6eSSangbeom Kim 	dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
3505bccae6eSSangbeom Kim 		1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
3515bccae6eSSangbeom Kim 		tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday);
3525bccae6eSSangbeom Kim 
3535bccae6eSSangbeom Kim 	switch (info->device_type) {
3545bccae6eSSangbeom Kim 	case S5M8763X:
355602cb5bbSKrzysztof Kozlowski 		ret = regmap_write(info->regmap, S5M_ALARM0_CONF, 0);
3565bccae6eSSangbeom Kim 		break;
3575bccae6eSSangbeom Kim 
3585bccae6eSSangbeom Kim 	case S5M8767X:
3595bccae6eSSangbeom Kim 		for (i = 0; i < 7; i++)
3605bccae6eSSangbeom Kim 			data[i] &= ~ALARM_ENABLE_MASK;
3615bccae6eSSangbeom Kim 
362602cb5bbSKrzysztof Kozlowski 		ret = regmap_raw_write(info->regmap, S5M_ALARM0_SEC, data, 8);
3635bccae6eSSangbeom Kim 		if (ret < 0)
3645bccae6eSSangbeom Kim 			return ret;
3655bccae6eSSangbeom Kim 
3665bccae6eSSangbeom Kim 		ret = s5m8767_rtc_set_alarm_reg(info);
3675bccae6eSSangbeom Kim 
3685bccae6eSSangbeom Kim 		break;
3695bccae6eSSangbeom Kim 
3705bccae6eSSangbeom Kim 	default:
3715bccae6eSSangbeom Kim 		return -EINVAL;
3725bccae6eSSangbeom Kim 	}
3735bccae6eSSangbeom Kim 
3745bccae6eSSangbeom Kim 	return ret;
3755bccae6eSSangbeom Kim }
3765bccae6eSSangbeom Kim 
3775bccae6eSSangbeom Kim static int s5m_rtc_start_alarm(struct s5m_rtc_info *info)
3785bccae6eSSangbeom Kim {
3795bccae6eSSangbeom Kim 	int ret;
3805bccae6eSSangbeom Kim 	u8 data[8];
3815bccae6eSSangbeom Kim 	u8 alarm0_conf;
3825bccae6eSSangbeom Kim 	struct rtc_time tm;
3835bccae6eSSangbeom Kim 
384602cb5bbSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, S5M_ALARM0_SEC, data, 8);
3855bccae6eSSangbeom Kim 	if (ret < 0)
3865bccae6eSSangbeom Kim 		return ret;
3875bccae6eSSangbeom Kim 
3885bccae6eSSangbeom Kim 	s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
3895bccae6eSSangbeom Kim 	dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
3905bccae6eSSangbeom Kim 		1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
3915bccae6eSSangbeom Kim 		tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday);
3925bccae6eSSangbeom Kim 
3935bccae6eSSangbeom Kim 	switch (info->device_type) {
3945bccae6eSSangbeom Kim 	case S5M8763X:
3955bccae6eSSangbeom Kim 		alarm0_conf = 0x77;
396602cb5bbSKrzysztof Kozlowski 		ret = regmap_write(info->regmap, S5M_ALARM0_CONF, alarm0_conf);
3975bccae6eSSangbeom Kim 		break;
3985bccae6eSSangbeom Kim 
3995bccae6eSSangbeom Kim 	case S5M8767X:
4005bccae6eSSangbeom Kim 		data[RTC_SEC] |= ALARM_ENABLE_MASK;
4015bccae6eSSangbeom Kim 		data[RTC_MIN] |= ALARM_ENABLE_MASK;
4025bccae6eSSangbeom Kim 		data[RTC_HOUR] |= ALARM_ENABLE_MASK;
4035bccae6eSSangbeom Kim 		data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
4045bccae6eSSangbeom Kim 		if (data[RTC_DATE] & 0x1f)
4055bccae6eSSangbeom Kim 			data[RTC_DATE] |= ALARM_ENABLE_MASK;
4065bccae6eSSangbeom Kim 		if (data[RTC_MONTH] & 0xf)
4075bccae6eSSangbeom Kim 			data[RTC_MONTH] |= ALARM_ENABLE_MASK;
4085bccae6eSSangbeom Kim 		if (data[RTC_YEAR1] & 0x7f)
4095bccae6eSSangbeom Kim 			data[RTC_YEAR1] |= ALARM_ENABLE_MASK;
4105bccae6eSSangbeom Kim 
411602cb5bbSKrzysztof Kozlowski 		ret = regmap_raw_write(info->regmap, S5M_ALARM0_SEC, data, 8);
4125bccae6eSSangbeom Kim 		if (ret < 0)
4135bccae6eSSangbeom Kim 			return ret;
4145bccae6eSSangbeom Kim 		ret = s5m8767_rtc_set_alarm_reg(info);
4155bccae6eSSangbeom Kim 
4165bccae6eSSangbeom Kim 		break;
4175bccae6eSSangbeom Kim 
4185bccae6eSSangbeom Kim 	default:
4195bccae6eSSangbeom Kim 		return -EINVAL;
4205bccae6eSSangbeom Kim 	}
4215bccae6eSSangbeom Kim 
4225bccae6eSSangbeom Kim 	return ret;
4235bccae6eSSangbeom Kim }
4245bccae6eSSangbeom Kim 
4255bccae6eSSangbeom Kim static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
4265bccae6eSSangbeom Kim {
4275bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
4285bccae6eSSangbeom Kim 	u8 data[8];
4295bccae6eSSangbeom Kim 	int ret;
4305bccae6eSSangbeom Kim 
4315bccae6eSSangbeom Kim 	switch (info->device_type) {
4325bccae6eSSangbeom Kim 	case S5M8763X:
4335bccae6eSSangbeom Kim 		s5m8763_tm_to_data(&alrm->time, data);
4345bccae6eSSangbeom Kim 		break;
4355bccae6eSSangbeom Kim 
4365bccae6eSSangbeom Kim 	case S5M8767X:
4375bccae6eSSangbeom Kim 		s5m8767_tm_to_data(&alrm->time, data);
4385bccae6eSSangbeom Kim 		break;
4395bccae6eSSangbeom Kim 
4405bccae6eSSangbeom Kim 	default:
4415bccae6eSSangbeom Kim 		return -EINVAL;
4425bccae6eSSangbeom Kim 	}
4435bccae6eSSangbeom Kim 
4445bccae6eSSangbeom Kim 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
4455bccae6eSSangbeom Kim 		1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
4465bccae6eSSangbeom Kim 		alrm->time.tm_mday, alrm->time.tm_hour, alrm->time.tm_min,
4475bccae6eSSangbeom Kim 		alrm->time.tm_sec, alrm->time.tm_wday);
4485bccae6eSSangbeom Kim 
4495bccae6eSSangbeom Kim 	ret = s5m_rtc_stop_alarm(info);
4505bccae6eSSangbeom Kim 	if (ret < 0)
4515bccae6eSSangbeom Kim 		return ret;
4525bccae6eSSangbeom Kim 
453602cb5bbSKrzysztof Kozlowski 	ret = regmap_raw_write(info->regmap, S5M_ALARM0_SEC, data, 8);
4545bccae6eSSangbeom Kim 	if (ret < 0)
4555bccae6eSSangbeom Kim 		return ret;
4565bccae6eSSangbeom Kim 
4575bccae6eSSangbeom Kim 	ret = s5m8767_rtc_set_alarm_reg(info);
4585bccae6eSSangbeom Kim 	if (ret < 0)
4595bccae6eSSangbeom Kim 		return ret;
4605bccae6eSSangbeom Kim 
4615bccae6eSSangbeom Kim 	if (alrm->enabled)
4625bccae6eSSangbeom Kim 		ret = s5m_rtc_start_alarm(info);
4635bccae6eSSangbeom Kim 
4645bccae6eSSangbeom Kim 	return ret;
4655bccae6eSSangbeom Kim }
4665bccae6eSSangbeom Kim 
4675bccae6eSSangbeom Kim static int s5m_rtc_alarm_irq_enable(struct device *dev,
4685bccae6eSSangbeom Kim 				    unsigned int enabled)
4695bccae6eSSangbeom Kim {
4705bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
4715bccae6eSSangbeom Kim 
4725bccae6eSSangbeom Kim 	if (enabled)
4735bccae6eSSangbeom Kim 		return s5m_rtc_start_alarm(info);
4745bccae6eSSangbeom Kim 	else
4755bccae6eSSangbeom Kim 		return s5m_rtc_stop_alarm(info);
4765bccae6eSSangbeom Kim }
4775bccae6eSSangbeom Kim 
4785bccae6eSSangbeom Kim static irqreturn_t s5m_rtc_alarm_irq(int irq, void *data)
4795bccae6eSSangbeom Kim {
4805bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = data;
4815bccae6eSSangbeom Kim 
4825bccae6eSSangbeom Kim 	rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
4835bccae6eSSangbeom Kim 
4845bccae6eSSangbeom Kim 	return IRQ_HANDLED;
4855bccae6eSSangbeom Kim }
4865bccae6eSSangbeom Kim 
4875bccae6eSSangbeom Kim static const struct rtc_class_ops s5m_rtc_ops = {
4885bccae6eSSangbeom Kim 	.read_time = s5m_rtc_read_time,
4895bccae6eSSangbeom Kim 	.set_time = s5m_rtc_set_time,
4905bccae6eSSangbeom Kim 	.read_alarm = s5m_rtc_read_alarm,
4915bccae6eSSangbeom Kim 	.set_alarm = s5m_rtc_set_alarm,
4925bccae6eSSangbeom Kim 	.alarm_irq_enable = s5m_rtc_alarm_irq_enable,
4935bccae6eSSangbeom Kim };
4945bccae6eSSangbeom Kim 
4955bccae6eSSangbeom Kim static void s5m_rtc_enable_wtsr(struct s5m_rtc_info *info, bool enable)
4965bccae6eSSangbeom Kim {
4975bccae6eSSangbeom Kim 	int ret;
498602cb5bbSKrzysztof Kozlowski 	ret = regmap_update_bits(info->regmap, S5M_WTSR_SMPL_CNTL,
4995bccae6eSSangbeom Kim 				 WTSR_ENABLE_MASK,
5005bccae6eSSangbeom Kim 				 enable ? WTSR_ENABLE_MASK : 0);
5015bccae6eSSangbeom Kim 	if (ret < 0)
5025bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to update WTSR reg(%d)\n",
5035bccae6eSSangbeom Kim 			__func__, ret);
5045bccae6eSSangbeom Kim }
5055bccae6eSSangbeom Kim 
5065bccae6eSSangbeom Kim static void s5m_rtc_enable_smpl(struct s5m_rtc_info *info, bool enable)
5075bccae6eSSangbeom Kim {
5085bccae6eSSangbeom Kim 	int ret;
509602cb5bbSKrzysztof Kozlowski 	ret = regmap_update_bits(info->regmap, S5M_WTSR_SMPL_CNTL,
5105bccae6eSSangbeom Kim 				 SMPL_ENABLE_MASK,
5115bccae6eSSangbeom Kim 				 enable ? SMPL_ENABLE_MASK : 0);
5125bccae6eSSangbeom Kim 	if (ret < 0)
5135bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to update SMPL reg(%d)\n",
5145bccae6eSSangbeom Kim 			__func__, ret);
5155bccae6eSSangbeom Kim }
5165bccae6eSSangbeom Kim 
5175bccae6eSSangbeom Kim static int s5m8767_rtc_init_reg(struct s5m_rtc_info *info)
5185bccae6eSSangbeom Kim {
5195bccae6eSSangbeom Kim 	u8 data[2];
5205bccae6eSSangbeom Kim 	int ret;
5215bccae6eSSangbeom Kim 
522*0c5f5d9aSKrzysztof Kozlowski 	/* UDR update time. Default of 7.32 ms is too long. */
523*0c5f5d9aSKrzysztof Kozlowski 	ret = regmap_update_bits(info->regmap, S5M_RTC_UDR_CON,
524*0c5f5d9aSKrzysztof Kozlowski 			S5M_RTC_UDR_T_MASK, S5M_RTC_UDR_T_450_US);
525*0c5f5d9aSKrzysztof Kozlowski 	if (ret < 0)
526*0c5f5d9aSKrzysztof Kozlowski 		dev_err(info->dev, "%s: fail to change UDR time: %d\n",
527*0c5f5d9aSKrzysztof Kozlowski 				__func__, ret);
528*0c5f5d9aSKrzysztof Kozlowski 
5295bccae6eSSangbeom Kim 	/* Set RTC control register : Binary mode, 24hour mode */
5305bccae6eSSangbeom Kim 	data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
5315bccae6eSSangbeom Kim 	data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
5325bccae6eSSangbeom Kim 
5335bccae6eSSangbeom Kim 	info->rtc_24hr_mode = 1;
534602cb5bbSKrzysztof Kozlowski 	ret = regmap_raw_write(info->regmap, S5M_ALARM0_CONF, data, 2);
5355bccae6eSSangbeom Kim 	if (ret < 0) {
5365bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
5375bccae6eSSangbeom Kim 			__func__, ret);
5385bccae6eSSangbeom Kim 		return ret;
5395bccae6eSSangbeom Kim 	}
5405bccae6eSSangbeom Kim 
5415bccae6eSSangbeom Kim 	return ret;
5425bccae6eSSangbeom Kim }
5435bccae6eSSangbeom Kim 
5445bccae6eSSangbeom Kim static int s5m_rtc_probe(struct platform_device *pdev)
5455bccae6eSSangbeom Kim {
5465bccae6eSSangbeom Kim 	struct sec_pmic_dev *s5m87xx = dev_get_drvdata(pdev->dev.parent);
5475bccae6eSSangbeom Kim 	struct sec_platform_data *pdata = s5m87xx->pdata;
5485bccae6eSSangbeom Kim 	struct s5m_rtc_info *info;
549e349c910SKrzysztof Kozlowski 	const struct regmap_config *regmap_cfg;
5505bccae6eSSangbeom Kim 	int ret;
5515bccae6eSSangbeom Kim 
5525bccae6eSSangbeom Kim 	if (!pdata) {
5535bccae6eSSangbeom Kim 		dev_err(pdev->dev.parent, "Platform data not supplied\n");
5545bccae6eSSangbeom Kim 		return -ENODEV;
5555bccae6eSSangbeom Kim 	}
5565bccae6eSSangbeom Kim 
5575bccae6eSSangbeom Kim 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
5585bccae6eSSangbeom Kim 	if (!info)
5595bccae6eSSangbeom Kim 		return -ENOMEM;
5605bccae6eSSangbeom Kim 
561e349c910SKrzysztof Kozlowski 	switch (pdata->device_type) {
562e349c910SKrzysztof Kozlowski 	case S2MPS14X:
563e349c910SKrzysztof Kozlowski 		regmap_cfg = &s2mps14_rtc_regmap_config;
564e349c910SKrzysztof Kozlowski 		break;
565e349c910SKrzysztof Kozlowski 	case S5M8763X:
566e349c910SKrzysztof Kozlowski 		regmap_cfg = &s5m_rtc_regmap_config;
567e349c910SKrzysztof Kozlowski 		break;
568e349c910SKrzysztof Kozlowski 	case S5M8767X:
569e349c910SKrzysztof Kozlowski 		regmap_cfg = &s5m_rtc_regmap_config;
570e349c910SKrzysztof Kozlowski 		break;
571e349c910SKrzysztof Kozlowski 	default:
572e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Device type is not supported by RTC driver\n");
573e349c910SKrzysztof Kozlowski 		return -ENODEV;
574e349c910SKrzysztof Kozlowski 	}
575e349c910SKrzysztof Kozlowski 
576e349c910SKrzysztof Kozlowski 	info->i2c = i2c_new_dummy(s5m87xx->i2c->adapter, RTC_I2C_ADDR);
577e349c910SKrzysztof Kozlowski 	if (!info->i2c) {
578e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Failed to allocate I2C for RTC\n");
579e349c910SKrzysztof Kozlowski 		return -ENODEV;
580e349c910SKrzysztof Kozlowski 	}
581e349c910SKrzysztof Kozlowski 
582e349c910SKrzysztof Kozlowski 	info->regmap = devm_regmap_init_i2c(info->i2c, regmap_cfg);
583e349c910SKrzysztof Kozlowski 	if (IS_ERR(info->regmap)) {
584e349c910SKrzysztof Kozlowski 		ret = PTR_ERR(info->regmap);
585e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Failed to allocate RTC register map: %d\n",
586e349c910SKrzysztof Kozlowski 				ret);
587e349c910SKrzysztof Kozlowski 		goto err;
588e349c910SKrzysztof Kozlowski 	}
589e349c910SKrzysztof Kozlowski 
5905bccae6eSSangbeom Kim 	info->dev = &pdev->dev;
5915bccae6eSSangbeom Kim 	info->s5m87xx = s5m87xx;
5925bccae6eSSangbeom Kim 	info->device_type = s5m87xx->device_type;
5935bccae6eSSangbeom Kim 	info->wtsr_smpl = s5m87xx->wtsr_smpl;
5945bccae6eSSangbeom Kim 
5955bccae6eSSangbeom Kim 	switch (pdata->device_type) {
5965bccae6eSSangbeom Kim 	case S5M8763X:
5977b003be8SKrzysztof Kozlowski 		info->irq = regmap_irq_get_virq(s5m87xx->irq_data,
5987b003be8SKrzysztof Kozlowski 				S5M8763_IRQ_ALARM0);
5995bccae6eSSangbeom Kim 		break;
6005bccae6eSSangbeom Kim 
6015bccae6eSSangbeom Kim 	case S5M8767X:
6027b003be8SKrzysztof Kozlowski 		info->irq = regmap_irq_get_virq(s5m87xx->irq_data,
6037b003be8SKrzysztof Kozlowski 				S5M8767_IRQ_RTCA1);
6045bccae6eSSangbeom Kim 		break;
6055bccae6eSSangbeom Kim 
6065bccae6eSSangbeom Kim 	default:
6075bccae6eSSangbeom Kim 		ret = -EINVAL;
6085bccae6eSSangbeom Kim 		dev_err(&pdev->dev, "Unsupported device type: %d\n", ret);
609e349c910SKrzysztof Kozlowski 		goto err;
6105bccae6eSSangbeom Kim 	}
6115bccae6eSSangbeom Kim 
6125bccae6eSSangbeom Kim 	platform_set_drvdata(pdev, info);
6135bccae6eSSangbeom Kim 
6145bccae6eSSangbeom Kim 	ret = s5m8767_rtc_init_reg(info);
6155bccae6eSSangbeom Kim 
6165bccae6eSSangbeom Kim 	if (info->wtsr_smpl) {
6175bccae6eSSangbeom Kim 		s5m_rtc_enable_wtsr(info, true);
6185bccae6eSSangbeom Kim 		s5m_rtc_enable_smpl(info, true);
6195bccae6eSSangbeom Kim 	}
6205bccae6eSSangbeom Kim 
6215bccae6eSSangbeom Kim 	device_init_wakeup(&pdev->dev, 1);
6225bccae6eSSangbeom Kim 
6235bccae6eSSangbeom Kim 	info->rtc_dev = devm_rtc_device_register(&pdev->dev, "s5m-rtc",
6245bccae6eSSangbeom Kim 						 &s5m_rtc_ops, THIS_MODULE);
6255bccae6eSSangbeom Kim 
626e349c910SKrzysztof Kozlowski 	if (IS_ERR(info->rtc_dev)) {
627e349c910SKrzysztof Kozlowski 		ret = PTR_ERR(info->rtc_dev);
628e349c910SKrzysztof Kozlowski 		goto err;
629e349c910SKrzysztof Kozlowski 	}
6305bccae6eSSangbeom Kim 
6315bccae6eSSangbeom Kim 	ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
6325bccae6eSSangbeom Kim 					s5m_rtc_alarm_irq, 0, "rtc-alarm0",
6335bccae6eSSangbeom Kim 					info);
634e349c910SKrzysztof Kozlowski 	if (ret < 0) {
6355bccae6eSSangbeom Kim 		dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
6365bccae6eSSangbeom Kim 			info->irq, ret);
637e349c910SKrzysztof Kozlowski 		goto err;
638e349c910SKrzysztof Kozlowski 	}
639e349c910SKrzysztof Kozlowski 
640e349c910SKrzysztof Kozlowski 	return 0;
641e349c910SKrzysztof Kozlowski 
642e349c910SKrzysztof Kozlowski err:
643e349c910SKrzysztof Kozlowski 	i2c_unregister_device(info->i2c);
6445bccae6eSSangbeom Kim 
6455bccae6eSSangbeom Kim 	return ret;
6465bccae6eSSangbeom Kim }
6475bccae6eSSangbeom Kim 
6485bccae6eSSangbeom Kim static void s5m_rtc_shutdown(struct platform_device *pdev)
6495bccae6eSSangbeom Kim {
6505bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = platform_get_drvdata(pdev);
6515bccae6eSSangbeom Kim 	int i;
6525bccae6eSSangbeom Kim 	unsigned int val = 0;
6535bccae6eSSangbeom Kim 	if (info->wtsr_smpl) {
6545bccae6eSSangbeom Kim 		for (i = 0; i < 3; i++) {
6555bccae6eSSangbeom Kim 			s5m_rtc_enable_wtsr(info, false);
656602cb5bbSKrzysztof Kozlowski 			regmap_read(info->regmap, S5M_WTSR_SMPL_CNTL, &val);
6575bccae6eSSangbeom Kim 			pr_debug("%s: WTSR_SMPL reg(0x%02x)\n", __func__, val);
6585bccae6eSSangbeom Kim 			if (val & WTSR_ENABLE_MASK)
6595bccae6eSSangbeom Kim 				pr_emerg("%s: fail to disable WTSR\n",
6605bccae6eSSangbeom Kim 					 __func__);
6615bccae6eSSangbeom Kim 			else {
6625bccae6eSSangbeom Kim 				pr_info("%s: success to disable WTSR\n",
6635bccae6eSSangbeom Kim 					__func__);
6645bccae6eSSangbeom Kim 				break;
6655bccae6eSSangbeom Kim 			}
6665bccae6eSSangbeom Kim 		}
6675bccae6eSSangbeom Kim 	}
6685bccae6eSSangbeom Kim 	/* Disable SMPL when power off */
6695bccae6eSSangbeom Kim 	s5m_rtc_enable_smpl(info, false);
6705bccae6eSSangbeom Kim }
6715bccae6eSSangbeom Kim 
672e349c910SKrzysztof Kozlowski static int s5m_rtc_remove(struct platform_device *pdev)
673e349c910SKrzysztof Kozlowski {
674e349c910SKrzysztof Kozlowski 	struct s5m_rtc_info *info = platform_get_drvdata(pdev);
675e349c910SKrzysztof Kozlowski 
676e349c910SKrzysztof Kozlowski 	/* Perform also all shutdown steps when removing */
677e349c910SKrzysztof Kozlowski 	s5m_rtc_shutdown(pdev);
678e349c910SKrzysztof Kozlowski 	i2c_unregister_device(info->i2c);
679e349c910SKrzysztof Kozlowski 
680e349c910SKrzysztof Kozlowski 	return 0;
681e349c910SKrzysztof Kozlowski }
682e349c910SKrzysztof Kozlowski 
68311ba5a1eSGeert Uytterhoeven #ifdef CONFIG_PM_SLEEP
684222ead7fSKrzysztof Kozlowski static int s5m_rtc_resume(struct device *dev)
685222ead7fSKrzysztof Kozlowski {
686222ead7fSKrzysztof Kozlowski 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
687222ead7fSKrzysztof Kozlowski 	int ret = 0;
688222ead7fSKrzysztof Kozlowski 
689222ead7fSKrzysztof Kozlowski 	if (device_may_wakeup(dev))
690222ead7fSKrzysztof Kozlowski 		ret = disable_irq_wake(info->irq);
691222ead7fSKrzysztof Kozlowski 
692222ead7fSKrzysztof Kozlowski 	return ret;
693222ead7fSKrzysztof Kozlowski }
694222ead7fSKrzysztof Kozlowski 
695222ead7fSKrzysztof Kozlowski static int s5m_rtc_suspend(struct device *dev)
696222ead7fSKrzysztof Kozlowski {
697222ead7fSKrzysztof Kozlowski 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
698222ead7fSKrzysztof Kozlowski 	int ret = 0;
699222ead7fSKrzysztof Kozlowski 
700222ead7fSKrzysztof Kozlowski 	if (device_may_wakeup(dev))
701222ead7fSKrzysztof Kozlowski 		ret = enable_irq_wake(info->irq);
702222ead7fSKrzysztof Kozlowski 
703222ead7fSKrzysztof Kozlowski 	return ret;
704222ead7fSKrzysztof Kozlowski }
70511ba5a1eSGeert Uytterhoeven #endif /* CONFIG_PM_SLEEP */
706222ead7fSKrzysztof Kozlowski 
707222ead7fSKrzysztof Kozlowski static SIMPLE_DEV_PM_OPS(s5m_rtc_pm_ops, s5m_rtc_suspend, s5m_rtc_resume);
708222ead7fSKrzysztof Kozlowski 
7095bccae6eSSangbeom Kim static const struct platform_device_id s5m_rtc_id[] = {
7105bccae6eSSangbeom Kim 	{ "s5m-rtc", 0 },
7115bccae6eSSangbeom Kim };
7125bccae6eSSangbeom Kim 
7135bccae6eSSangbeom Kim static struct platform_driver s5m_rtc_driver = {
7145bccae6eSSangbeom Kim 	.driver		= {
7155bccae6eSSangbeom Kim 		.name	= "s5m-rtc",
7165bccae6eSSangbeom Kim 		.owner	= THIS_MODULE,
717222ead7fSKrzysztof Kozlowski 		.pm	= &s5m_rtc_pm_ops,
7185bccae6eSSangbeom Kim 	},
7195bccae6eSSangbeom Kim 	.probe		= s5m_rtc_probe,
720e349c910SKrzysztof Kozlowski 	.remove		= s5m_rtc_remove,
7215bccae6eSSangbeom Kim 	.shutdown	= s5m_rtc_shutdown,
7225bccae6eSSangbeom Kim 	.id_table	= s5m_rtc_id,
7235bccae6eSSangbeom Kim };
7245bccae6eSSangbeom Kim 
7255bccae6eSSangbeom Kim module_platform_driver(s5m_rtc_driver);
7265bccae6eSSangbeom Kim 
7275bccae6eSSangbeom Kim /* Module information */
7285bccae6eSSangbeom Kim MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
7295bccae6eSSangbeom Kim MODULE_DESCRIPTION("Samsung S5M RTC driver");
7305bccae6eSSangbeom Kim MODULE_LICENSE("GPL");
7315bccae6eSSangbeom Kim MODULE_ALIAS("platform:s5m-rtc");
732