xref: /linux/drivers/rtc/rtc-s5m.c (revision e349c910e2398cbff59d7c58851503191a8e9157)
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
33d73238d4SKrzysztof Kozlowski  * of SEC_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
36d73238d4SKrzysztof Kozlowski  * in SEC_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;
43*e349c910SKrzysztof 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 
53*e349c910SKrzysztof Kozlowski static const struct regmap_config s5m_rtc_regmap_config = {
54*e349c910SKrzysztof Kozlowski 	.reg_bits = 8,
55*e349c910SKrzysztof Kozlowski 	.val_bits = 8,
56*e349c910SKrzysztof Kozlowski 
57*e349c910SKrzysztof Kozlowski 	.max_register = SEC_RTC_REG_MAX,
58*e349c910SKrzysztof Kozlowski };
59*e349c910SKrzysztof Kozlowski 
60*e349c910SKrzysztof Kozlowski static const struct regmap_config s2mps14_rtc_regmap_config = {
61*e349c910SKrzysztof Kozlowski 	.reg_bits = 8,
62*e349c910SKrzysztof Kozlowski 	.val_bits = 8,
63*e349c910SKrzysztof Kozlowski 
64*e349c910SKrzysztof Kozlowski 	.max_register = S2MPS_RTC_REG_MAX,
65*e349c910SKrzysztof Kozlowski };
66*e349c910SKrzysztof 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 {
122d73238d4SKrzysztof Kozlowski 		ret = regmap_read(info->regmap, SEC_RTC_UDR_CON, &data);
123d73238d4SKrzysztof Kozlowski 	} while (--retry && (data & 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 
1365ccb7d71SGeert Uytterhoeven 	ret = regmap_read(info->regmap, SEC_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 
1425bccae6eSSangbeom Kim 	data |= RTC_TIME_EN_MASK;
1435bccae6eSSangbeom Kim 	data |= RTC_UDR_MASK;
1445bccae6eSSangbeom Kim 
1455ccb7d71SGeert Uytterhoeven 	ret = regmap_write(info->regmap, SEC_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 
1615ccb7d71SGeert Uytterhoeven 	ret = regmap_read(info->regmap, SEC_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 
1685bccae6eSSangbeom Kim 	data &= ~RTC_TIME_EN_MASK;
1695bccae6eSSangbeom Kim 	data |= RTC_UDR_MASK;
1705bccae6eSSangbeom Kim 
1715ccb7d71SGeert Uytterhoeven 	ret = regmap_write(info->regmap, SEC_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 
2215ccb7d71SGeert Uytterhoeven 	ret = regmap_bulk_read(info->regmap, SEC_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 
2695ccb7d71SGeert Uytterhoeven 	ret = regmap_raw_write(info->regmap, SEC_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 
2855ccb7d71SGeert Uytterhoeven 	ret = regmap_bulk_read(info->regmap, SEC_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);
2925ccb7d71SGeert Uytterhoeven 		ret = regmap_read(info->regmap, SEC_ALARM0_CONF, &val);
2935bccae6eSSangbeom Kim 		if (ret < 0)
2945bccae6eSSangbeom Kim 			return ret;
2955bccae6eSSangbeom Kim 
2965bccae6eSSangbeom Kim 		alrm->enabled = !!val;
2975bccae6eSSangbeom Kim 
2985ccb7d71SGeert Uytterhoeven 		ret = regmap_read(info->regmap, SEC_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;
3215ccb7d71SGeert Uytterhoeven 		ret = regmap_read(info->regmap, SEC_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 
3305bccae6eSSangbeom Kim 	if (val & 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 
3445ccb7d71SGeert Uytterhoeven 	ret = regmap_bulk_read(info->regmap, SEC_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:
3555ccb7d71SGeert Uytterhoeven 		ret = regmap_write(info->regmap, SEC_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 
3625ccb7d71SGeert Uytterhoeven 		ret = regmap_raw_write(info->regmap, SEC_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 
3845ccb7d71SGeert Uytterhoeven 	ret = regmap_bulk_read(info->regmap, SEC_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;
3965ccb7d71SGeert Uytterhoeven 		ret = regmap_write(info->regmap, SEC_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 
4115ccb7d71SGeert Uytterhoeven 		ret = regmap_raw_write(info->regmap, SEC_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 
4535ccb7d71SGeert Uytterhoeven 	ret = regmap_raw_write(info->regmap, SEC_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;
4985ccb7d71SGeert Uytterhoeven 	ret = regmap_update_bits(info->regmap, SEC_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;
5095ccb7d71SGeert Uytterhoeven 	ret = regmap_update_bits(info->regmap, SEC_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 	unsigned int tp_read;
5215bccae6eSSangbeom Kim 	int ret;
5225bccae6eSSangbeom Kim 	struct rtc_time tm;
5235bccae6eSSangbeom Kim 
5245ccb7d71SGeert Uytterhoeven 	ret = regmap_read(info->regmap, SEC_RTC_UDR_CON, &tp_read);
5255bccae6eSSangbeom Kim 	if (ret < 0) {
5265bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to read control reg(%d)\n",
5275bccae6eSSangbeom Kim 			__func__, ret);
5285bccae6eSSangbeom Kim 		return ret;
5295bccae6eSSangbeom Kim 	}
5305bccae6eSSangbeom Kim 
5315bccae6eSSangbeom Kim 	/* Set RTC control register : Binary mode, 24hour mode */
5325bccae6eSSangbeom Kim 	data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
5335bccae6eSSangbeom Kim 	data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
5345bccae6eSSangbeom Kim 
5355bccae6eSSangbeom Kim 	info->rtc_24hr_mode = 1;
5365ccb7d71SGeert Uytterhoeven 	ret = regmap_raw_write(info->regmap, SEC_ALARM0_CONF, data, 2);
5375bccae6eSSangbeom Kim 	if (ret < 0) {
5385bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
5395bccae6eSSangbeom Kim 			__func__, ret);
5405bccae6eSSangbeom Kim 		return ret;
5415bccae6eSSangbeom Kim 	}
5425bccae6eSSangbeom Kim 
5435bccae6eSSangbeom Kim 	/* In first boot time, Set rtc time to 1/1/2012 00:00:00(SUN) */
5445bccae6eSSangbeom Kim 	if ((tp_read & RTC_TCON_MASK) == 0) {
5455bccae6eSSangbeom Kim 		dev_dbg(info->dev, "rtc init\n");
5465bccae6eSSangbeom Kim 		tm.tm_sec = 0;
5475bccae6eSSangbeom Kim 		tm.tm_min = 0;
5485bccae6eSSangbeom Kim 		tm.tm_hour = 0;
5495bccae6eSSangbeom Kim 		tm.tm_wday = 0;
5505bccae6eSSangbeom Kim 		tm.tm_mday = 1;
5515bccae6eSSangbeom Kim 		tm.tm_mon = 0;
5525bccae6eSSangbeom Kim 		tm.tm_year = 112;
5535bccae6eSSangbeom Kim 		tm.tm_yday = 0;
5545bccae6eSSangbeom Kim 		tm.tm_isdst = 0;
5555bccae6eSSangbeom Kim 		ret = s5m_rtc_set_time(info->dev, &tm);
5565bccae6eSSangbeom Kim 	}
5575bccae6eSSangbeom Kim 
5585ccb7d71SGeert Uytterhoeven 	ret = regmap_update_bits(info->regmap, SEC_RTC_UDR_CON,
5595bccae6eSSangbeom Kim 				 RTC_TCON_MASK, tp_read | RTC_TCON_MASK);
5605bccae6eSSangbeom Kim 	if (ret < 0)
5615bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to update TCON reg(%d)\n",
5625bccae6eSSangbeom Kim 			__func__, ret);
5635bccae6eSSangbeom Kim 
5645bccae6eSSangbeom Kim 	return ret;
5655bccae6eSSangbeom Kim }
5665bccae6eSSangbeom Kim 
5675bccae6eSSangbeom Kim static int s5m_rtc_probe(struct platform_device *pdev)
5685bccae6eSSangbeom Kim {
5695bccae6eSSangbeom Kim 	struct sec_pmic_dev *s5m87xx = dev_get_drvdata(pdev->dev.parent);
5705bccae6eSSangbeom Kim 	struct sec_platform_data *pdata = s5m87xx->pdata;
5715bccae6eSSangbeom Kim 	struct s5m_rtc_info *info;
572*e349c910SKrzysztof Kozlowski 	const struct regmap_config *regmap_cfg;
5735bccae6eSSangbeom Kim 	int ret;
5745bccae6eSSangbeom Kim 
5755bccae6eSSangbeom Kim 	if (!pdata) {
5765bccae6eSSangbeom Kim 		dev_err(pdev->dev.parent, "Platform data not supplied\n");
5775bccae6eSSangbeom Kim 		return -ENODEV;
5785bccae6eSSangbeom Kim 	}
5795bccae6eSSangbeom Kim 
5805bccae6eSSangbeom Kim 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
5815bccae6eSSangbeom Kim 	if (!info)
5825bccae6eSSangbeom Kim 		return -ENOMEM;
5835bccae6eSSangbeom Kim 
584*e349c910SKrzysztof Kozlowski 	switch (pdata->device_type) {
585*e349c910SKrzysztof Kozlowski 	case S2MPS14X:
586*e349c910SKrzysztof Kozlowski 		regmap_cfg = &s2mps14_rtc_regmap_config;
587*e349c910SKrzysztof Kozlowski 		break;
588*e349c910SKrzysztof Kozlowski 	case S5M8763X:
589*e349c910SKrzysztof Kozlowski 		regmap_cfg = &s5m_rtc_regmap_config;
590*e349c910SKrzysztof Kozlowski 		break;
591*e349c910SKrzysztof Kozlowski 	case S5M8767X:
592*e349c910SKrzysztof Kozlowski 		regmap_cfg = &s5m_rtc_regmap_config;
593*e349c910SKrzysztof Kozlowski 		break;
594*e349c910SKrzysztof Kozlowski 	default:
595*e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Device type is not supported by RTC driver\n");
596*e349c910SKrzysztof Kozlowski 		return -ENODEV;
597*e349c910SKrzysztof Kozlowski 	}
598*e349c910SKrzysztof Kozlowski 
599*e349c910SKrzysztof Kozlowski 	info->i2c = i2c_new_dummy(s5m87xx->i2c->adapter, RTC_I2C_ADDR);
600*e349c910SKrzysztof Kozlowski 	if (!info->i2c) {
601*e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Failed to allocate I2C for RTC\n");
602*e349c910SKrzysztof Kozlowski 		return -ENODEV;
603*e349c910SKrzysztof Kozlowski 	}
604*e349c910SKrzysztof Kozlowski 
605*e349c910SKrzysztof Kozlowski 	info->regmap = devm_regmap_init_i2c(info->i2c, regmap_cfg);
606*e349c910SKrzysztof Kozlowski 	if (IS_ERR(info->regmap)) {
607*e349c910SKrzysztof Kozlowski 		ret = PTR_ERR(info->regmap);
608*e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Failed to allocate RTC register map: %d\n",
609*e349c910SKrzysztof Kozlowski 				ret);
610*e349c910SKrzysztof Kozlowski 		goto err;
611*e349c910SKrzysztof Kozlowski 	}
612*e349c910SKrzysztof Kozlowski 
6135bccae6eSSangbeom Kim 	info->dev = &pdev->dev;
6145bccae6eSSangbeom Kim 	info->s5m87xx = s5m87xx;
6155bccae6eSSangbeom Kim 	info->device_type = s5m87xx->device_type;
6165bccae6eSSangbeom Kim 	info->wtsr_smpl = s5m87xx->wtsr_smpl;
6175bccae6eSSangbeom Kim 
6185bccae6eSSangbeom Kim 	switch (pdata->device_type) {
6195bccae6eSSangbeom Kim 	case S5M8763X:
6207b003be8SKrzysztof Kozlowski 		info->irq = regmap_irq_get_virq(s5m87xx->irq_data,
6217b003be8SKrzysztof Kozlowski 				S5M8763_IRQ_ALARM0);
6225bccae6eSSangbeom Kim 		break;
6235bccae6eSSangbeom Kim 
6245bccae6eSSangbeom Kim 	case S5M8767X:
6257b003be8SKrzysztof Kozlowski 		info->irq = regmap_irq_get_virq(s5m87xx->irq_data,
6267b003be8SKrzysztof Kozlowski 				S5M8767_IRQ_RTCA1);
6275bccae6eSSangbeom Kim 		break;
6285bccae6eSSangbeom Kim 
6295bccae6eSSangbeom Kim 	default:
6305bccae6eSSangbeom Kim 		ret = -EINVAL;
6315bccae6eSSangbeom Kim 		dev_err(&pdev->dev, "Unsupported device type: %d\n", ret);
632*e349c910SKrzysztof Kozlowski 		goto err;
6335bccae6eSSangbeom Kim 	}
6345bccae6eSSangbeom Kim 
6355bccae6eSSangbeom Kim 	platform_set_drvdata(pdev, info);
6365bccae6eSSangbeom Kim 
6375bccae6eSSangbeom Kim 	ret = s5m8767_rtc_init_reg(info);
6385bccae6eSSangbeom Kim 
6395bccae6eSSangbeom Kim 	if (info->wtsr_smpl) {
6405bccae6eSSangbeom Kim 		s5m_rtc_enable_wtsr(info, true);
6415bccae6eSSangbeom Kim 		s5m_rtc_enable_smpl(info, true);
6425bccae6eSSangbeom Kim 	}
6435bccae6eSSangbeom Kim 
6445bccae6eSSangbeom Kim 	device_init_wakeup(&pdev->dev, 1);
6455bccae6eSSangbeom Kim 
6465bccae6eSSangbeom Kim 	info->rtc_dev = devm_rtc_device_register(&pdev->dev, "s5m-rtc",
6475bccae6eSSangbeom Kim 						 &s5m_rtc_ops, THIS_MODULE);
6485bccae6eSSangbeom Kim 
649*e349c910SKrzysztof Kozlowski 	if (IS_ERR(info->rtc_dev)) {
650*e349c910SKrzysztof Kozlowski 		ret = PTR_ERR(info->rtc_dev);
651*e349c910SKrzysztof Kozlowski 		goto err;
652*e349c910SKrzysztof Kozlowski 	}
6535bccae6eSSangbeom Kim 
6545bccae6eSSangbeom Kim 	ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
6555bccae6eSSangbeom Kim 					s5m_rtc_alarm_irq, 0, "rtc-alarm0",
6565bccae6eSSangbeom Kim 					info);
657*e349c910SKrzysztof Kozlowski 	if (ret < 0) {
6585bccae6eSSangbeom Kim 		dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
6595bccae6eSSangbeom Kim 			info->irq, ret);
660*e349c910SKrzysztof Kozlowski 		goto err;
661*e349c910SKrzysztof Kozlowski 	}
662*e349c910SKrzysztof Kozlowski 
663*e349c910SKrzysztof Kozlowski 	return 0;
664*e349c910SKrzysztof Kozlowski 
665*e349c910SKrzysztof Kozlowski err:
666*e349c910SKrzysztof Kozlowski 	i2c_unregister_device(info->i2c);
6675bccae6eSSangbeom Kim 
6685bccae6eSSangbeom Kim 	return ret;
6695bccae6eSSangbeom Kim }
6705bccae6eSSangbeom Kim 
6715bccae6eSSangbeom Kim static void s5m_rtc_shutdown(struct platform_device *pdev)
6725bccae6eSSangbeom Kim {
6735bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = platform_get_drvdata(pdev);
6745bccae6eSSangbeom Kim 	int i;
6755bccae6eSSangbeom Kim 	unsigned int val = 0;
6765bccae6eSSangbeom Kim 	if (info->wtsr_smpl) {
6775bccae6eSSangbeom Kim 		for (i = 0; i < 3; i++) {
6785bccae6eSSangbeom Kim 			s5m_rtc_enable_wtsr(info, false);
6795ccb7d71SGeert Uytterhoeven 			regmap_read(info->regmap, SEC_WTSR_SMPL_CNTL, &val);
6805bccae6eSSangbeom Kim 			pr_debug("%s: WTSR_SMPL reg(0x%02x)\n", __func__, val);
6815bccae6eSSangbeom Kim 			if (val & WTSR_ENABLE_MASK)
6825bccae6eSSangbeom Kim 				pr_emerg("%s: fail to disable WTSR\n",
6835bccae6eSSangbeom Kim 					 __func__);
6845bccae6eSSangbeom Kim 			else {
6855bccae6eSSangbeom Kim 				pr_info("%s: success to disable WTSR\n",
6865bccae6eSSangbeom Kim 					__func__);
6875bccae6eSSangbeom Kim 				break;
6885bccae6eSSangbeom Kim 			}
6895bccae6eSSangbeom Kim 		}
6905bccae6eSSangbeom Kim 	}
6915bccae6eSSangbeom Kim 	/* Disable SMPL when power off */
6925bccae6eSSangbeom Kim 	s5m_rtc_enable_smpl(info, false);
6935bccae6eSSangbeom Kim }
6945bccae6eSSangbeom Kim 
695*e349c910SKrzysztof Kozlowski static int s5m_rtc_remove(struct platform_device *pdev)
696*e349c910SKrzysztof Kozlowski {
697*e349c910SKrzysztof Kozlowski 	struct s5m_rtc_info *info = platform_get_drvdata(pdev);
698*e349c910SKrzysztof Kozlowski 
699*e349c910SKrzysztof Kozlowski 	/* Perform also all shutdown steps when removing */
700*e349c910SKrzysztof Kozlowski 	s5m_rtc_shutdown(pdev);
701*e349c910SKrzysztof Kozlowski 	i2c_unregister_device(info->i2c);
702*e349c910SKrzysztof Kozlowski 
703*e349c910SKrzysztof Kozlowski 	return 0;
704*e349c910SKrzysztof Kozlowski }
705*e349c910SKrzysztof Kozlowski 
70611ba5a1eSGeert Uytterhoeven #ifdef CONFIG_PM_SLEEP
707222ead7fSKrzysztof Kozlowski static int s5m_rtc_resume(struct device *dev)
708222ead7fSKrzysztof Kozlowski {
709222ead7fSKrzysztof Kozlowski 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
710222ead7fSKrzysztof Kozlowski 	int ret = 0;
711222ead7fSKrzysztof Kozlowski 
712222ead7fSKrzysztof Kozlowski 	if (device_may_wakeup(dev))
713222ead7fSKrzysztof Kozlowski 		ret = disable_irq_wake(info->irq);
714222ead7fSKrzysztof Kozlowski 
715222ead7fSKrzysztof Kozlowski 	return ret;
716222ead7fSKrzysztof Kozlowski }
717222ead7fSKrzysztof Kozlowski 
718222ead7fSKrzysztof Kozlowski static int s5m_rtc_suspend(struct device *dev)
719222ead7fSKrzysztof Kozlowski {
720222ead7fSKrzysztof Kozlowski 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
721222ead7fSKrzysztof Kozlowski 	int ret = 0;
722222ead7fSKrzysztof Kozlowski 
723222ead7fSKrzysztof Kozlowski 	if (device_may_wakeup(dev))
724222ead7fSKrzysztof Kozlowski 		ret = enable_irq_wake(info->irq);
725222ead7fSKrzysztof Kozlowski 
726222ead7fSKrzysztof Kozlowski 	return ret;
727222ead7fSKrzysztof Kozlowski }
72811ba5a1eSGeert Uytterhoeven #endif /* CONFIG_PM_SLEEP */
729222ead7fSKrzysztof Kozlowski 
730222ead7fSKrzysztof Kozlowski static SIMPLE_DEV_PM_OPS(s5m_rtc_pm_ops, s5m_rtc_suspend, s5m_rtc_resume);
731222ead7fSKrzysztof Kozlowski 
7325bccae6eSSangbeom Kim static const struct platform_device_id s5m_rtc_id[] = {
7335bccae6eSSangbeom Kim 	{ "s5m-rtc", 0 },
7345bccae6eSSangbeom Kim };
7355bccae6eSSangbeom Kim 
7365bccae6eSSangbeom Kim static struct platform_driver s5m_rtc_driver = {
7375bccae6eSSangbeom Kim 	.driver		= {
7385bccae6eSSangbeom Kim 		.name	= "s5m-rtc",
7395bccae6eSSangbeom Kim 		.owner	= THIS_MODULE,
740222ead7fSKrzysztof Kozlowski 		.pm	= &s5m_rtc_pm_ops,
7415bccae6eSSangbeom Kim 	},
7425bccae6eSSangbeom Kim 	.probe		= s5m_rtc_probe,
743*e349c910SKrzysztof Kozlowski 	.remove		= s5m_rtc_remove,
7445bccae6eSSangbeom Kim 	.shutdown	= s5m_rtc_shutdown,
7455bccae6eSSangbeom Kim 	.id_table	= s5m_rtc_id,
7465bccae6eSSangbeom Kim };
7475bccae6eSSangbeom Kim 
7485bccae6eSSangbeom Kim module_platform_driver(s5m_rtc_driver);
7495bccae6eSSangbeom Kim 
7505bccae6eSSangbeom Kim /* Module information */
7515bccae6eSSangbeom Kim MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
7525bccae6eSSangbeom Kim MODULE_DESCRIPTION("Samsung S5M RTC driver");
7535bccae6eSSangbeom Kim MODULE_LICENSE("GPL");
7545bccae6eSSangbeom Kim MODULE_ALIAS("platform:s5m-rtc");
755