xref: /linux/drivers/rtc/rtc-s5m.c (revision f8b23bbdad5dfb50af101a56c58ad4e83510a9a6)
15bccae6eSSangbeom Kim /*
2*f8b23bbdSKrzysztof Kozlowski  * Copyright (c) 2013-2014 Samsung Electronics Co., Ltd
35bccae6eSSangbeom Kim  *	http://www.samsung.com
45bccae6eSSangbeom Kim  *
55bccae6eSSangbeom Kim  *  Copyright (C) 2013 Google, Inc
65bccae6eSSangbeom Kim  *
75bccae6eSSangbeom Kim  *  This program is free software; you can redistribute it and/or modify
85bccae6eSSangbeom Kim  *  it under the terms of the GNU General Public License as published by
95bccae6eSSangbeom Kim  *  the Free Software Foundation; either version 2 of the License, or
105bccae6eSSangbeom Kim  *  (at your option) any later version.
115bccae6eSSangbeom Kim  *
125bccae6eSSangbeom Kim  *  This program is distributed in the hope that it will be useful,
135bccae6eSSangbeom Kim  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
145bccae6eSSangbeom Kim  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155bccae6eSSangbeom Kim  *  GNU General Public License for more details.
165bccae6eSSangbeom Kim  */
175bccae6eSSangbeom Kim 
185bccae6eSSangbeom Kim #include <linux/module.h>
195bccae6eSSangbeom Kim #include <linux/i2c.h>
205bccae6eSSangbeom Kim #include <linux/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 
41*f8b23bbdSKrzysztof Kozlowski /* Registers used by the driver which are different between chipsets. */
42*f8b23bbdSKrzysztof Kozlowski struct s5m_rtc_reg_config {
43*f8b23bbdSKrzysztof Kozlowski 	/* Number of registers used for setting time/alarm0/alarm1 */
44*f8b23bbdSKrzysztof Kozlowski 	unsigned int regs_count;
45*f8b23bbdSKrzysztof Kozlowski 	/* First register for time, seconds */
46*f8b23bbdSKrzysztof Kozlowski 	unsigned int time;
47*f8b23bbdSKrzysztof Kozlowski 	/* RTC control register */
48*f8b23bbdSKrzysztof Kozlowski 	unsigned int ctrl;
49*f8b23bbdSKrzysztof Kozlowski 	/* First register for alarm 0, seconds */
50*f8b23bbdSKrzysztof Kozlowski 	unsigned int alarm0;
51*f8b23bbdSKrzysztof Kozlowski 	/* First register for alarm 1, seconds */
52*f8b23bbdSKrzysztof Kozlowski 	unsigned int alarm1;
53*f8b23bbdSKrzysztof Kozlowski 	/* SMPL/WTSR register */
54*f8b23bbdSKrzysztof Kozlowski 	unsigned int smpl_wtsr;
55*f8b23bbdSKrzysztof Kozlowski 	/*
56*f8b23bbdSKrzysztof Kozlowski 	 * Register for update flag (UDR). Typically setting UDR field to 1
57*f8b23bbdSKrzysztof Kozlowski 	 * will enable update of time or alarm register. Then it will be
58*f8b23bbdSKrzysztof Kozlowski 	 * auto-cleared after successful update.
59*f8b23bbdSKrzysztof Kozlowski 	 */
60*f8b23bbdSKrzysztof Kozlowski 	unsigned int rtc_udr_update;
61*f8b23bbdSKrzysztof Kozlowski 	/* Mask for UDR field in 'rtc_udr_update' register */
62*f8b23bbdSKrzysztof Kozlowski 	unsigned int rtc_udr_mask;
63*f8b23bbdSKrzysztof Kozlowski };
64*f8b23bbdSKrzysztof Kozlowski 
65*f8b23bbdSKrzysztof Kozlowski /* Register map for S5M8763 and S5M8767 */
66*f8b23bbdSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s5m_rtc_regs = {
67*f8b23bbdSKrzysztof Kozlowski 	.regs_count		= 8,
68*f8b23bbdSKrzysztof Kozlowski 	.time			= S5M_RTC_SEC,
69*f8b23bbdSKrzysztof Kozlowski 	.ctrl			= S5M_ALARM1_CONF,
70*f8b23bbdSKrzysztof Kozlowski 	.alarm0			= S5M_ALARM0_SEC,
71*f8b23bbdSKrzysztof Kozlowski 	.alarm1			= S5M_ALARM1_SEC,
72*f8b23bbdSKrzysztof Kozlowski 	.smpl_wtsr		= S5M_WTSR_SMPL_CNTL,
73*f8b23bbdSKrzysztof Kozlowski 	.rtc_udr_update		= S5M_RTC_UDR_CON,
74*f8b23bbdSKrzysztof Kozlowski 	.rtc_udr_mask		= S5M_RTC_UDR_MASK,
75*f8b23bbdSKrzysztof Kozlowski };
76*f8b23bbdSKrzysztof Kozlowski 
775bccae6eSSangbeom Kim struct s5m_rtc_info {
785bccae6eSSangbeom Kim 	struct device *dev;
79e349c910SKrzysztof Kozlowski 	struct i2c_client *i2c;
805bccae6eSSangbeom Kim 	struct sec_pmic_dev *s5m87xx;
815ccb7d71SGeert Uytterhoeven 	struct regmap *regmap;
825bccae6eSSangbeom Kim 	struct rtc_device *rtc_dev;
835bccae6eSSangbeom Kim 	int irq;
845bccae6eSSangbeom Kim 	int device_type;
855bccae6eSSangbeom Kim 	int rtc_24hr_mode;
865bccae6eSSangbeom Kim 	bool wtsr_smpl;
87*f8b23bbdSKrzysztof Kozlowski 	const struct s5m_rtc_reg_config	*regs;
885bccae6eSSangbeom Kim };
895bccae6eSSangbeom Kim 
90e349c910SKrzysztof Kozlowski static const struct regmap_config s5m_rtc_regmap_config = {
91e349c910SKrzysztof Kozlowski 	.reg_bits = 8,
92e349c910SKrzysztof Kozlowski 	.val_bits = 8,
93e349c910SKrzysztof Kozlowski 
94602cb5bbSKrzysztof Kozlowski 	.max_register = S5M_RTC_REG_MAX,
95e349c910SKrzysztof Kozlowski };
96e349c910SKrzysztof Kozlowski 
97e349c910SKrzysztof Kozlowski static const struct regmap_config s2mps14_rtc_regmap_config = {
98e349c910SKrzysztof Kozlowski 	.reg_bits = 8,
99e349c910SKrzysztof Kozlowski 	.val_bits = 8,
100e349c910SKrzysztof Kozlowski 
101e349c910SKrzysztof Kozlowski 	.max_register = S2MPS_RTC_REG_MAX,
102e349c910SKrzysztof Kozlowski };
103e349c910SKrzysztof Kozlowski 
1045bccae6eSSangbeom Kim static void s5m8767_data_to_tm(u8 *data, struct rtc_time *tm,
1055bccae6eSSangbeom Kim 			       int rtc_24hr_mode)
1065bccae6eSSangbeom Kim {
1075bccae6eSSangbeom Kim 	tm->tm_sec = data[RTC_SEC] & 0x7f;
1085bccae6eSSangbeom Kim 	tm->tm_min = data[RTC_MIN] & 0x7f;
1095bccae6eSSangbeom Kim 	if (rtc_24hr_mode) {
1105bccae6eSSangbeom Kim 		tm->tm_hour = data[RTC_HOUR] & 0x1f;
1115bccae6eSSangbeom Kim 	} else {
1125bccae6eSSangbeom Kim 		tm->tm_hour = data[RTC_HOUR] & 0x0f;
1135bccae6eSSangbeom Kim 		if (data[RTC_HOUR] & HOUR_PM_MASK)
1145bccae6eSSangbeom Kim 			tm->tm_hour += 12;
1155bccae6eSSangbeom Kim 	}
1165bccae6eSSangbeom Kim 
1175bccae6eSSangbeom Kim 	tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f);
1185bccae6eSSangbeom Kim 	tm->tm_mday = data[RTC_DATE] & 0x1f;
1195bccae6eSSangbeom Kim 	tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
1205bccae6eSSangbeom Kim 	tm->tm_year = (data[RTC_YEAR1] & 0x7f) + 100;
1215bccae6eSSangbeom Kim 	tm->tm_yday = 0;
1225bccae6eSSangbeom Kim 	tm->tm_isdst = 0;
1235bccae6eSSangbeom Kim }
1245bccae6eSSangbeom Kim 
1255bccae6eSSangbeom Kim static int s5m8767_tm_to_data(struct rtc_time *tm, u8 *data)
1265bccae6eSSangbeom Kim {
1275bccae6eSSangbeom Kim 	data[RTC_SEC] = tm->tm_sec;
1285bccae6eSSangbeom Kim 	data[RTC_MIN] = tm->tm_min;
1295bccae6eSSangbeom Kim 
1305bccae6eSSangbeom Kim 	if (tm->tm_hour >= 12)
1315bccae6eSSangbeom Kim 		data[RTC_HOUR] = tm->tm_hour | HOUR_PM_MASK;
1325bccae6eSSangbeom Kim 	else
1335bccae6eSSangbeom Kim 		data[RTC_HOUR] = tm->tm_hour & ~HOUR_PM_MASK;
1345bccae6eSSangbeom Kim 
1355bccae6eSSangbeom Kim 	data[RTC_WEEKDAY] = 1 << tm->tm_wday;
1365bccae6eSSangbeom Kim 	data[RTC_DATE] = tm->tm_mday;
1375bccae6eSSangbeom Kim 	data[RTC_MONTH] = tm->tm_mon + 1;
1385bccae6eSSangbeom Kim 	data[RTC_YEAR1] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
1395bccae6eSSangbeom Kim 
1405bccae6eSSangbeom Kim 	if (tm->tm_year < 100) {
1415bccae6eSSangbeom Kim 		pr_err("s5m8767 RTC cannot handle the year %d.\n",
1425bccae6eSSangbeom Kim 		       1900 + tm->tm_year);
1435bccae6eSSangbeom Kim 		return -EINVAL;
1445bccae6eSSangbeom Kim 	} else {
1455bccae6eSSangbeom Kim 		return 0;
1465bccae6eSSangbeom Kim 	}
1475bccae6eSSangbeom Kim }
1485bccae6eSSangbeom Kim 
149d73238d4SKrzysztof Kozlowski /*
150d73238d4SKrzysztof Kozlowski  * Read RTC_UDR_CON register and wait till UDR field is cleared.
151d73238d4SKrzysztof Kozlowski  * This indicates that time/alarm update ended.
152d73238d4SKrzysztof Kozlowski  */
153d73238d4SKrzysztof Kozlowski static inline int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info)
154d73238d4SKrzysztof Kozlowski {
155d73238d4SKrzysztof Kozlowski 	int ret, retry = UDR_READ_RETRY_CNT;
156d73238d4SKrzysztof Kozlowski 	unsigned int data;
157d73238d4SKrzysztof Kozlowski 
158d73238d4SKrzysztof Kozlowski 	do {
159*f8b23bbdSKrzysztof Kozlowski 		ret = regmap_read(info->regmap, info->regs->rtc_udr_update,
160*f8b23bbdSKrzysztof Kozlowski 				&data);
161*f8b23bbdSKrzysztof Kozlowski 	} while (--retry && (data & info->regs->rtc_udr_mask) && !ret);
162d73238d4SKrzysztof Kozlowski 
163d73238d4SKrzysztof Kozlowski 	if (!retry)
164d73238d4SKrzysztof Kozlowski 		dev_err(info->dev, "waiting for UDR update, reached max number of retries\n");
165d73238d4SKrzysztof Kozlowski 
166d73238d4SKrzysztof Kozlowski 	return ret;
167d73238d4SKrzysztof Kozlowski }
168d73238d4SKrzysztof Kozlowski 
169*f8b23bbdSKrzysztof Kozlowski static inline int s5m_check_peding_alarm_interrupt(struct s5m_rtc_info *info,
170*f8b23bbdSKrzysztof Kozlowski 		struct rtc_wkalrm *alarm)
171*f8b23bbdSKrzysztof Kozlowski {
172*f8b23bbdSKrzysztof Kozlowski 	int ret;
173*f8b23bbdSKrzysztof Kozlowski 	unsigned int val;
174*f8b23bbdSKrzysztof Kozlowski 
175*f8b23bbdSKrzysztof Kozlowski 	switch (info->device_type) {
176*f8b23bbdSKrzysztof Kozlowski 	case S5M8767X:
177*f8b23bbdSKrzysztof Kozlowski 	case S5M8763X:
178*f8b23bbdSKrzysztof Kozlowski 		ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val);
179*f8b23bbdSKrzysztof Kozlowski 		val &= S5M_ALARM0_STATUS;
180*f8b23bbdSKrzysztof Kozlowski 		break;
181*f8b23bbdSKrzysztof Kozlowski 	default:
182*f8b23bbdSKrzysztof Kozlowski 		return -EINVAL;
183*f8b23bbdSKrzysztof Kozlowski 	}
184*f8b23bbdSKrzysztof Kozlowski 	if (ret < 0)
185*f8b23bbdSKrzysztof Kozlowski 		return ret;
186*f8b23bbdSKrzysztof Kozlowski 
187*f8b23bbdSKrzysztof Kozlowski 	if (val)
188*f8b23bbdSKrzysztof Kozlowski 		alarm->pending = 1;
189*f8b23bbdSKrzysztof Kozlowski 	else
190*f8b23bbdSKrzysztof Kozlowski 		alarm->pending = 0;
191*f8b23bbdSKrzysztof Kozlowski 
192*f8b23bbdSKrzysztof Kozlowski 	return 0;
193*f8b23bbdSKrzysztof Kozlowski }
194*f8b23bbdSKrzysztof Kozlowski 
1955bccae6eSSangbeom Kim static inline int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info)
1965bccae6eSSangbeom Kim {
1975bccae6eSSangbeom Kim 	int ret;
1985bccae6eSSangbeom Kim 	unsigned int data;
1995bccae6eSSangbeom Kim 
200*f8b23bbdSKrzysztof Kozlowski 	ret = regmap_read(info->regmap, info->regs->rtc_udr_update, &data);
2015bccae6eSSangbeom Kim 	if (ret < 0) {
2025bccae6eSSangbeom Kim 		dev_err(info->dev, "failed to read update reg(%d)\n", ret);
2035bccae6eSSangbeom Kim 		return ret;
2045bccae6eSSangbeom Kim 	}
2055bccae6eSSangbeom Kim 
206602cb5bbSKrzysztof Kozlowski 	data |= S5M_RTC_TIME_EN_MASK;
207*f8b23bbdSKrzysztof Kozlowski 	data |= info->regs->rtc_udr_mask;
2085bccae6eSSangbeom Kim 
209*f8b23bbdSKrzysztof Kozlowski 	ret = regmap_write(info->regmap, info->regs->rtc_udr_update, data);
2105bccae6eSSangbeom Kim 	if (ret < 0) {
2115bccae6eSSangbeom Kim 		dev_err(info->dev, "failed to write update reg(%d)\n", ret);
2125bccae6eSSangbeom Kim 		return ret;
2135bccae6eSSangbeom Kim 	}
2145bccae6eSSangbeom Kim 
215d73238d4SKrzysztof Kozlowski 	ret = s5m8767_wait_for_udr_update(info);
2165bccae6eSSangbeom Kim 
2175bccae6eSSangbeom Kim 	return ret;
2185bccae6eSSangbeom Kim }
2195bccae6eSSangbeom Kim 
2205bccae6eSSangbeom Kim static inline int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info)
2215bccae6eSSangbeom Kim {
2225bccae6eSSangbeom Kim 	int ret;
2235bccae6eSSangbeom Kim 	unsigned int data;
2245bccae6eSSangbeom Kim 
225*f8b23bbdSKrzysztof Kozlowski 	ret = regmap_read(info->regmap, info->regs->rtc_udr_update, &data);
2265bccae6eSSangbeom Kim 	if (ret < 0) {
2275bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to read update reg(%d)\n",
2285bccae6eSSangbeom Kim 			__func__, ret);
2295bccae6eSSangbeom Kim 		return ret;
2305bccae6eSSangbeom Kim 	}
2315bccae6eSSangbeom Kim 
232602cb5bbSKrzysztof Kozlowski 	data &= ~S5M_RTC_TIME_EN_MASK;
233*f8b23bbdSKrzysztof Kozlowski 	data |= info->regs->rtc_udr_mask;
2345bccae6eSSangbeom Kim 
235*f8b23bbdSKrzysztof Kozlowski 	ret = regmap_write(info->regmap, info->regs->rtc_udr_update, data);
2365bccae6eSSangbeom Kim 	if (ret < 0) {
2375bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to write update reg(%d)\n",
2385bccae6eSSangbeom Kim 			__func__, ret);
2395bccae6eSSangbeom Kim 		return ret;
2405bccae6eSSangbeom Kim 	}
2415bccae6eSSangbeom Kim 
242d73238d4SKrzysztof Kozlowski 	ret = s5m8767_wait_for_udr_update(info);
2435bccae6eSSangbeom Kim 
2445bccae6eSSangbeom Kim 	return ret;
2455bccae6eSSangbeom Kim }
2465bccae6eSSangbeom Kim 
2475bccae6eSSangbeom Kim static void s5m8763_data_to_tm(u8 *data, struct rtc_time *tm)
2485bccae6eSSangbeom Kim {
2495bccae6eSSangbeom Kim 	tm->tm_sec = bcd2bin(data[RTC_SEC]);
2505bccae6eSSangbeom Kim 	tm->tm_min = bcd2bin(data[RTC_MIN]);
2515bccae6eSSangbeom Kim 
2525bccae6eSSangbeom Kim 	if (data[RTC_HOUR] & HOUR_12) {
2535bccae6eSSangbeom Kim 		tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
2545bccae6eSSangbeom Kim 		if (data[RTC_HOUR] & HOUR_PM)
2555bccae6eSSangbeom Kim 			tm->tm_hour += 12;
2565bccae6eSSangbeom Kim 	} else {
2575bccae6eSSangbeom Kim 		tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
2585bccae6eSSangbeom Kim 	}
2595bccae6eSSangbeom Kim 
2605bccae6eSSangbeom Kim 	tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
2615bccae6eSSangbeom Kim 	tm->tm_mday = bcd2bin(data[RTC_DATE]);
2625bccae6eSSangbeom Kim 	tm->tm_mon = bcd2bin(data[RTC_MONTH]);
2635bccae6eSSangbeom Kim 	tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
2645bccae6eSSangbeom Kim 	tm->tm_year -= 1900;
2655bccae6eSSangbeom Kim }
2665bccae6eSSangbeom Kim 
2675bccae6eSSangbeom Kim static void s5m8763_tm_to_data(struct rtc_time *tm, u8 *data)
2685bccae6eSSangbeom Kim {
2695bccae6eSSangbeom Kim 	data[RTC_SEC] = bin2bcd(tm->tm_sec);
2705bccae6eSSangbeom Kim 	data[RTC_MIN] = bin2bcd(tm->tm_min);
2715bccae6eSSangbeom Kim 	data[RTC_HOUR] = bin2bcd(tm->tm_hour);
2725bccae6eSSangbeom Kim 	data[RTC_WEEKDAY] = tm->tm_wday;
2735bccae6eSSangbeom Kim 	data[RTC_DATE] = bin2bcd(tm->tm_mday);
2745bccae6eSSangbeom Kim 	data[RTC_MONTH] = bin2bcd(tm->tm_mon);
2755bccae6eSSangbeom Kim 	data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
2765bccae6eSSangbeom Kim 	data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
2775bccae6eSSangbeom Kim }
2785bccae6eSSangbeom Kim 
2795bccae6eSSangbeom Kim static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm)
2805bccae6eSSangbeom Kim {
2815bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
282*f8b23bbdSKrzysztof Kozlowski 	u8 data[info->regs->regs_count];
2835bccae6eSSangbeom Kim 	int ret;
2845bccae6eSSangbeom Kim 
285*f8b23bbdSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, info->regs->time, data,
286*f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
2875bccae6eSSangbeom Kim 	if (ret < 0)
2885bccae6eSSangbeom Kim 		return ret;
2895bccae6eSSangbeom Kim 
2905bccae6eSSangbeom Kim 	switch (info->device_type) {
2915bccae6eSSangbeom Kim 	case S5M8763X:
2925bccae6eSSangbeom Kim 		s5m8763_data_to_tm(data, tm);
2935bccae6eSSangbeom Kim 		break;
2945bccae6eSSangbeom Kim 
2955bccae6eSSangbeom Kim 	case S5M8767X:
2965bccae6eSSangbeom Kim 		s5m8767_data_to_tm(data, tm, info->rtc_24hr_mode);
2975bccae6eSSangbeom Kim 		break;
2985bccae6eSSangbeom Kim 
2995bccae6eSSangbeom Kim 	default:
3005bccae6eSSangbeom Kim 		return -EINVAL;
3015bccae6eSSangbeom Kim 	}
3025bccae6eSSangbeom Kim 
3035bccae6eSSangbeom Kim 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
3045bccae6eSSangbeom Kim 		1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
3055bccae6eSSangbeom Kim 		tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
3065bccae6eSSangbeom Kim 
3075bccae6eSSangbeom Kim 	return rtc_valid_tm(tm);
3085bccae6eSSangbeom Kim }
3095bccae6eSSangbeom Kim 
3105bccae6eSSangbeom Kim static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm)
3115bccae6eSSangbeom Kim {
3125bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
313*f8b23bbdSKrzysztof Kozlowski 	u8 data[info->regs->regs_count];
3145bccae6eSSangbeom Kim 	int ret = 0;
3155bccae6eSSangbeom Kim 
3165bccae6eSSangbeom Kim 	switch (info->device_type) {
3175bccae6eSSangbeom Kim 	case S5M8763X:
3185bccae6eSSangbeom Kim 		s5m8763_tm_to_data(tm, data);
3195bccae6eSSangbeom Kim 		break;
3205bccae6eSSangbeom Kim 	case S5M8767X:
3215bccae6eSSangbeom Kim 		ret = s5m8767_tm_to_data(tm, data);
3225bccae6eSSangbeom Kim 		break;
3235bccae6eSSangbeom Kim 	default:
3245bccae6eSSangbeom Kim 		return -EINVAL;
3255bccae6eSSangbeom Kim 	}
3265bccae6eSSangbeom Kim 
3275bccae6eSSangbeom Kim 	if (ret < 0)
3285bccae6eSSangbeom Kim 		return ret;
3295bccae6eSSangbeom Kim 
3305bccae6eSSangbeom Kim 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
3315bccae6eSSangbeom Kim 		1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
3325bccae6eSSangbeom Kim 		tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
3335bccae6eSSangbeom Kim 
334*f8b23bbdSKrzysztof Kozlowski 	ret = regmap_raw_write(info->regmap, info->regs->time, data,
335*f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
3365bccae6eSSangbeom Kim 	if (ret < 0)
3375bccae6eSSangbeom Kim 		return ret;
3385bccae6eSSangbeom Kim 
3395bccae6eSSangbeom Kim 	ret = s5m8767_rtc_set_time_reg(info);
3405bccae6eSSangbeom Kim 
3415bccae6eSSangbeom Kim 	return ret;
3425bccae6eSSangbeom Kim }
3435bccae6eSSangbeom Kim 
3445bccae6eSSangbeom Kim static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
3455bccae6eSSangbeom Kim {
3465bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
347*f8b23bbdSKrzysztof Kozlowski 	u8 data[info->regs->regs_count];
3485bccae6eSSangbeom Kim 	unsigned int val;
3495bccae6eSSangbeom Kim 	int ret, i;
3505bccae6eSSangbeom Kim 
351*f8b23bbdSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
352*f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
3535bccae6eSSangbeom Kim 	if (ret < 0)
3545bccae6eSSangbeom Kim 		return ret;
3555bccae6eSSangbeom Kim 
3565bccae6eSSangbeom Kim 	switch (info->device_type) {
3575bccae6eSSangbeom Kim 	case S5M8763X:
3585bccae6eSSangbeom Kim 		s5m8763_data_to_tm(data, &alrm->time);
359602cb5bbSKrzysztof Kozlowski 		ret = regmap_read(info->regmap, S5M_ALARM0_CONF, &val);
3605bccae6eSSangbeom Kim 		if (ret < 0)
3615bccae6eSSangbeom Kim 			return ret;
3625bccae6eSSangbeom Kim 
3635bccae6eSSangbeom Kim 		alrm->enabled = !!val;
3645bccae6eSSangbeom Kim 		break;
3655bccae6eSSangbeom Kim 
3665bccae6eSSangbeom Kim 	case S5M8767X:
3675bccae6eSSangbeom Kim 		s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
3685bccae6eSSangbeom Kim 		alrm->enabled = 0;
369*f8b23bbdSKrzysztof Kozlowski 		for (i = 0; i < info->regs->regs_count; i++) {
3705bccae6eSSangbeom Kim 			if (data[i] & ALARM_ENABLE_MASK) {
3715bccae6eSSangbeom Kim 				alrm->enabled = 1;
3725bccae6eSSangbeom Kim 				break;
3735bccae6eSSangbeom Kim 			}
3745bccae6eSSangbeom Kim 		}
3755bccae6eSSangbeom Kim 		break;
3765bccae6eSSangbeom Kim 
3775bccae6eSSangbeom Kim 	default:
3785bccae6eSSangbeom Kim 		return -EINVAL;
3795bccae6eSSangbeom Kim 	}
3805bccae6eSSangbeom Kim 
381*f8b23bbdSKrzysztof Kozlowski 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
382*f8b23bbdSKrzysztof Kozlowski 		1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
383*f8b23bbdSKrzysztof Kozlowski 		alrm->time.tm_mday, alrm->time.tm_hour,
384*f8b23bbdSKrzysztof Kozlowski 		alrm->time.tm_min, alrm->time.tm_sec,
385*f8b23bbdSKrzysztof Kozlowski 		alrm->time.tm_wday);
386*f8b23bbdSKrzysztof Kozlowski 
387*f8b23bbdSKrzysztof Kozlowski 	ret = s5m_check_peding_alarm_interrupt(info, alrm);
3885bccae6eSSangbeom Kim 
3895bccae6eSSangbeom Kim 	return 0;
3905bccae6eSSangbeom Kim }
3915bccae6eSSangbeom Kim 
3925bccae6eSSangbeom Kim static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info)
3935bccae6eSSangbeom Kim {
394*f8b23bbdSKrzysztof Kozlowski 	u8 data[info->regs->regs_count];
3955bccae6eSSangbeom Kim 	int ret, i;
3965bccae6eSSangbeom Kim 	struct rtc_time tm;
3975bccae6eSSangbeom Kim 
398*f8b23bbdSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
399*f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
4005bccae6eSSangbeom Kim 	if (ret < 0)
4015bccae6eSSangbeom Kim 		return ret;
4025bccae6eSSangbeom Kim 
4035bccae6eSSangbeom Kim 	s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
4045bccae6eSSangbeom Kim 	dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
4055bccae6eSSangbeom Kim 		1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
4065bccae6eSSangbeom Kim 		tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday);
4075bccae6eSSangbeom Kim 
4085bccae6eSSangbeom Kim 	switch (info->device_type) {
4095bccae6eSSangbeom Kim 	case S5M8763X:
410602cb5bbSKrzysztof Kozlowski 		ret = regmap_write(info->regmap, S5M_ALARM0_CONF, 0);
4115bccae6eSSangbeom Kim 		break;
4125bccae6eSSangbeom Kim 
4135bccae6eSSangbeom Kim 	case S5M8767X:
414*f8b23bbdSKrzysztof Kozlowski 		for (i = 0; i < info->regs->regs_count; i++)
4155bccae6eSSangbeom Kim 			data[i] &= ~ALARM_ENABLE_MASK;
4165bccae6eSSangbeom Kim 
417*f8b23bbdSKrzysztof Kozlowski 		ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
418*f8b23bbdSKrzysztof Kozlowski 				info->regs->regs_count);
4195bccae6eSSangbeom Kim 		if (ret < 0)
4205bccae6eSSangbeom Kim 			return ret;
4215bccae6eSSangbeom Kim 
4225bccae6eSSangbeom Kim 		ret = s5m8767_rtc_set_alarm_reg(info);
4235bccae6eSSangbeom Kim 
4245bccae6eSSangbeom Kim 		break;
4255bccae6eSSangbeom Kim 
4265bccae6eSSangbeom Kim 	default:
4275bccae6eSSangbeom Kim 		return -EINVAL;
4285bccae6eSSangbeom Kim 	}
4295bccae6eSSangbeom Kim 
4305bccae6eSSangbeom Kim 	return ret;
4315bccae6eSSangbeom Kim }
4325bccae6eSSangbeom Kim 
4335bccae6eSSangbeom Kim static int s5m_rtc_start_alarm(struct s5m_rtc_info *info)
4345bccae6eSSangbeom Kim {
4355bccae6eSSangbeom Kim 	int ret;
436*f8b23bbdSKrzysztof Kozlowski 	u8 data[info->regs->regs_count];
4375bccae6eSSangbeom Kim 	u8 alarm0_conf;
4385bccae6eSSangbeom Kim 	struct rtc_time tm;
4395bccae6eSSangbeom Kim 
440*f8b23bbdSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
441*f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
4425bccae6eSSangbeom Kim 	if (ret < 0)
4435bccae6eSSangbeom Kim 		return ret;
4445bccae6eSSangbeom Kim 
4455bccae6eSSangbeom Kim 	s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
4465bccae6eSSangbeom Kim 	dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
4475bccae6eSSangbeom Kim 		1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
4485bccae6eSSangbeom Kim 		tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday);
4495bccae6eSSangbeom Kim 
4505bccae6eSSangbeom Kim 	switch (info->device_type) {
4515bccae6eSSangbeom Kim 	case S5M8763X:
4525bccae6eSSangbeom Kim 		alarm0_conf = 0x77;
453602cb5bbSKrzysztof Kozlowski 		ret = regmap_write(info->regmap, S5M_ALARM0_CONF, alarm0_conf);
4545bccae6eSSangbeom Kim 		break;
4555bccae6eSSangbeom Kim 
4565bccae6eSSangbeom Kim 	case S5M8767X:
4575bccae6eSSangbeom Kim 		data[RTC_SEC] |= ALARM_ENABLE_MASK;
4585bccae6eSSangbeom Kim 		data[RTC_MIN] |= ALARM_ENABLE_MASK;
4595bccae6eSSangbeom Kim 		data[RTC_HOUR] |= ALARM_ENABLE_MASK;
4605bccae6eSSangbeom Kim 		data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
4615bccae6eSSangbeom Kim 		if (data[RTC_DATE] & 0x1f)
4625bccae6eSSangbeom Kim 			data[RTC_DATE] |= ALARM_ENABLE_MASK;
4635bccae6eSSangbeom Kim 		if (data[RTC_MONTH] & 0xf)
4645bccae6eSSangbeom Kim 			data[RTC_MONTH] |= ALARM_ENABLE_MASK;
4655bccae6eSSangbeom Kim 		if (data[RTC_YEAR1] & 0x7f)
4665bccae6eSSangbeom Kim 			data[RTC_YEAR1] |= ALARM_ENABLE_MASK;
4675bccae6eSSangbeom Kim 
468*f8b23bbdSKrzysztof Kozlowski 		ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
469*f8b23bbdSKrzysztof Kozlowski 				info->regs->regs_count);
4705bccae6eSSangbeom Kim 		if (ret < 0)
4715bccae6eSSangbeom Kim 			return ret;
4725bccae6eSSangbeom Kim 		ret = s5m8767_rtc_set_alarm_reg(info);
4735bccae6eSSangbeom Kim 
4745bccae6eSSangbeom Kim 		break;
4755bccae6eSSangbeom Kim 
4765bccae6eSSangbeom Kim 	default:
4775bccae6eSSangbeom Kim 		return -EINVAL;
4785bccae6eSSangbeom Kim 	}
4795bccae6eSSangbeom Kim 
4805bccae6eSSangbeom Kim 	return ret;
4815bccae6eSSangbeom Kim }
4825bccae6eSSangbeom Kim 
4835bccae6eSSangbeom Kim static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
4845bccae6eSSangbeom Kim {
4855bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
486*f8b23bbdSKrzysztof Kozlowski 	u8 data[info->regs->regs_count];
4875bccae6eSSangbeom Kim 	int ret;
4885bccae6eSSangbeom Kim 
4895bccae6eSSangbeom Kim 	switch (info->device_type) {
4905bccae6eSSangbeom Kim 	case S5M8763X:
4915bccae6eSSangbeom Kim 		s5m8763_tm_to_data(&alrm->time, data);
4925bccae6eSSangbeom Kim 		break;
4935bccae6eSSangbeom Kim 
4945bccae6eSSangbeom Kim 	case S5M8767X:
4955bccae6eSSangbeom Kim 		s5m8767_tm_to_data(&alrm->time, data);
4965bccae6eSSangbeom Kim 		break;
4975bccae6eSSangbeom Kim 
4985bccae6eSSangbeom Kim 	default:
4995bccae6eSSangbeom Kim 		return -EINVAL;
5005bccae6eSSangbeom Kim 	}
5015bccae6eSSangbeom Kim 
5025bccae6eSSangbeom Kim 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
5035bccae6eSSangbeom Kim 		1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
5045bccae6eSSangbeom Kim 		alrm->time.tm_mday, alrm->time.tm_hour, alrm->time.tm_min,
5055bccae6eSSangbeom Kim 		alrm->time.tm_sec, alrm->time.tm_wday);
5065bccae6eSSangbeom Kim 
5075bccae6eSSangbeom Kim 	ret = s5m_rtc_stop_alarm(info);
5085bccae6eSSangbeom Kim 	if (ret < 0)
5095bccae6eSSangbeom Kim 		return ret;
5105bccae6eSSangbeom Kim 
511*f8b23bbdSKrzysztof Kozlowski 	ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
512*f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
5135bccae6eSSangbeom Kim 	if (ret < 0)
5145bccae6eSSangbeom Kim 		return ret;
5155bccae6eSSangbeom Kim 
5165bccae6eSSangbeom Kim 	ret = s5m8767_rtc_set_alarm_reg(info);
5175bccae6eSSangbeom Kim 	if (ret < 0)
5185bccae6eSSangbeom Kim 		return ret;
5195bccae6eSSangbeom Kim 
5205bccae6eSSangbeom Kim 	if (alrm->enabled)
5215bccae6eSSangbeom Kim 		ret = s5m_rtc_start_alarm(info);
5225bccae6eSSangbeom Kim 
5235bccae6eSSangbeom Kim 	return ret;
5245bccae6eSSangbeom Kim }
5255bccae6eSSangbeom Kim 
5265bccae6eSSangbeom Kim static int s5m_rtc_alarm_irq_enable(struct device *dev,
5275bccae6eSSangbeom Kim 				    unsigned int enabled)
5285bccae6eSSangbeom Kim {
5295bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
5305bccae6eSSangbeom Kim 
5315bccae6eSSangbeom Kim 	if (enabled)
5325bccae6eSSangbeom Kim 		return s5m_rtc_start_alarm(info);
5335bccae6eSSangbeom Kim 	else
5345bccae6eSSangbeom Kim 		return s5m_rtc_stop_alarm(info);
5355bccae6eSSangbeom Kim }
5365bccae6eSSangbeom Kim 
5375bccae6eSSangbeom Kim static irqreturn_t s5m_rtc_alarm_irq(int irq, void *data)
5385bccae6eSSangbeom Kim {
5395bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = data;
5405bccae6eSSangbeom Kim 
5415bccae6eSSangbeom Kim 	rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
5425bccae6eSSangbeom Kim 
5435bccae6eSSangbeom Kim 	return IRQ_HANDLED;
5445bccae6eSSangbeom Kim }
5455bccae6eSSangbeom Kim 
5465bccae6eSSangbeom Kim static const struct rtc_class_ops s5m_rtc_ops = {
5475bccae6eSSangbeom Kim 	.read_time = s5m_rtc_read_time,
5485bccae6eSSangbeom Kim 	.set_time = s5m_rtc_set_time,
5495bccae6eSSangbeom Kim 	.read_alarm = s5m_rtc_read_alarm,
5505bccae6eSSangbeom Kim 	.set_alarm = s5m_rtc_set_alarm,
5515bccae6eSSangbeom Kim 	.alarm_irq_enable = s5m_rtc_alarm_irq_enable,
5525bccae6eSSangbeom Kim };
5535bccae6eSSangbeom Kim 
5545bccae6eSSangbeom Kim static void s5m_rtc_enable_wtsr(struct s5m_rtc_info *info, bool enable)
5555bccae6eSSangbeom Kim {
5565bccae6eSSangbeom Kim 	int ret;
557*f8b23bbdSKrzysztof Kozlowski 	ret = regmap_update_bits(info->regmap, info->regs->smpl_wtsr,
5585bccae6eSSangbeom Kim 				 WTSR_ENABLE_MASK,
5595bccae6eSSangbeom Kim 				 enable ? WTSR_ENABLE_MASK : 0);
5605bccae6eSSangbeom Kim 	if (ret < 0)
5615bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to update WTSR reg(%d)\n",
5625bccae6eSSangbeom Kim 			__func__, ret);
5635bccae6eSSangbeom Kim }
5645bccae6eSSangbeom Kim 
5655bccae6eSSangbeom Kim static void s5m_rtc_enable_smpl(struct s5m_rtc_info *info, bool enable)
5665bccae6eSSangbeom Kim {
5675bccae6eSSangbeom Kim 	int ret;
568*f8b23bbdSKrzysztof Kozlowski 	ret = regmap_update_bits(info->regmap, info->regs->smpl_wtsr,
5695bccae6eSSangbeom Kim 				 SMPL_ENABLE_MASK,
5705bccae6eSSangbeom Kim 				 enable ? SMPL_ENABLE_MASK : 0);
5715bccae6eSSangbeom Kim 	if (ret < 0)
5725bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to update SMPL reg(%d)\n",
5735bccae6eSSangbeom Kim 			__func__, ret);
5745bccae6eSSangbeom Kim }
5755bccae6eSSangbeom Kim 
5765bccae6eSSangbeom Kim static int s5m8767_rtc_init_reg(struct s5m_rtc_info *info)
5775bccae6eSSangbeom Kim {
5785bccae6eSSangbeom Kim 	u8 data[2];
5795bccae6eSSangbeom Kim 	int ret;
5805bccae6eSSangbeom Kim 
5810c5f5d9aSKrzysztof Kozlowski 	/* UDR update time. Default of 7.32 ms is too long. */
5820c5f5d9aSKrzysztof Kozlowski 	ret = regmap_update_bits(info->regmap, S5M_RTC_UDR_CON,
5830c5f5d9aSKrzysztof Kozlowski 			S5M_RTC_UDR_T_MASK, S5M_RTC_UDR_T_450_US);
5840c5f5d9aSKrzysztof Kozlowski 	if (ret < 0)
5850c5f5d9aSKrzysztof Kozlowski 		dev_err(info->dev, "%s: fail to change UDR time: %d\n",
5860c5f5d9aSKrzysztof Kozlowski 				__func__, ret);
5870c5f5d9aSKrzysztof Kozlowski 
5885bccae6eSSangbeom Kim 	/* Set RTC control register : Binary mode, 24hour mode */
5895bccae6eSSangbeom Kim 	data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
5905bccae6eSSangbeom Kim 	data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
5915bccae6eSSangbeom Kim 
5925bccae6eSSangbeom Kim 	info->rtc_24hr_mode = 1;
593602cb5bbSKrzysztof Kozlowski 	ret = regmap_raw_write(info->regmap, S5M_ALARM0_CONF, data, 2);
5945bccae6eSSangbeom Kim 	if (ret < 0) {
5955bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
5965bccae6eSSangbeom Kim 			__func__, ret);
5975bccae6eSSangbeom Kim 		return ret;
5985bccae6eSSangbeom Kim 	}
5995bccae6eSSangbeom Kim 
6005bccae6eSSangbeom Kim 	return ret;
6015bccae6eSSangbeom Kim }
6025bccae6eSSangbeom Kim 
6035bccae6eSSangbeom Kim static int s5m_rtc_probe(struct platform_device *pdev)
6045bccae6eSSangbeom Kim {
6055bccae6eSSangbeom Kim 	struct sec_pmic_dev *s5m87xx = dev_get_drvdata(pdev->dev.parent);
6065bccae6eSSangbeom Kim 	struct sec_platform_data *pdata = s5m87xx->pdata;
6075bccae6eSSangbeom Kim 	struct s5m_rtc_info *info;
608e349c910SKrzysztof Kozlowski 	const struct regmap_config *regmap_cfg;
6095bccae6eSSangbeom Kim 	int ret;
6105bccae6eSSangbeom Kim 
6115bccae6eSSangbeom Kim 	if (!pdata) {
6125bccae6eSSangbeom Kim 		dev_err(pdev->dev.parent, "Platform data not supplied\n");
6135bccae6eSSangbeom Kim 		return -ENODEV;
6145bccae6eSSangbeom Kim 	}
6155bccae6eSSangbeom Kim 
6165bccae6eSSangbeom Kim 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
6175bccae6eSSangbeom Kim 	if (!info)
6185bccae6eSSangbeom Kim 		return -ENOMEM;
6195bccae6eSSangbeom Kim 
620e349c910SKrzysztof Kozlowski 	switch (pdata->device_type) {
621e349c910SKrzysztof Kozlowski 	case S2MPS14X:
622e349c910SKrzysztof Kozlowski 		regmap_cfg = &s2mps14_rtc_regmap_config;
623e349c910SKrzysztof Kozlowski 		break;
624e349c910SKrzysztof Kozlowski 	case S5M8763X:
625e349c910SKrzysztof Kozlowski 		regmap_cfg = &s5m_rtc_regmap_config;
626*f8b23bbdSKrzysztof Kozlowski 		info->regs = &s5m_rtc_regs;
627e349c910SKrzysztof Kozlowski 		break;
628e349c910SKrzysztof Kozlowski 	case S5M8767X:
629e349c910SKrzysztof Kozlowski 		regmap_cfg = &s5m_rtc_regmap_config;
630*f8b23bbdSKrzysztof Kozlowski 		info->regs = &s5m_rtc_regs;
631e349c910SKrzysztof Kozlowski 		break;
632e349c910SKrzysztof Kozlowski 	default:
633e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Device type is not supported by RTC driver\n");
634e349c910SKrzysztof Kozlowski 		return -ENODEV;
635e349c910SKrzysztof Kozlowski 	}
636e349c910SKrzysztof Kozlowski 
637e349c910SKrzysztof Kozlowski 	info->i2c = i2c_new_dummy(s5m87xx->i2c->adapter, RTC_I2C_ADDR);
638e349c910SKrzysztof Kozlowski 	if (!info->i2c) {
639e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Failed to allocate I2C for RTC\n");
640e349c910SKrzysztof Kozlowski 		return -ENODEV;
641e349c910SKrzysztof Kozlowski 	}
642e349c910SKrzysztof Kozlowski 
643e349c910SKrzysztof Kozlowski 	info->regmap = devm_regmap_init_i2c(info->i2c, regmap_cfg);
644e349c910SKrzysztof Kozlowski 	if (IS_ERR(info->regmap)) {
645e349c910SKrzysztof Kozlowski 		ret = PTR_ERR(info->regmap);
646e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Failed to allocate RTC register map: %d\n",
647e349c910SKrzysztof Kozlowski 				ret);
648e349c910SKrzysztof Kozlowski 		goto err;
649e349c910SKrzysztof Kozlowski 	}
650e349c910SKrzysztof Kozlowski 
6515bccae6eSSangbeom Kim 	info->dev = &pdev->dev;
6525bccae6eSSangbeom Kim 	info->s5m87xx = s5m87xx;
6535bccae6eSSangbeom Kim 	info->device_type = s5m87xx->device_type;
6545bccae6eSSangbeom Kim 	info->wtsr_smpl = s5m87xx->wtsr_smpl;
6555bccae6eSSangbeom Kim 
6565bccae6eSSangbeom Kim 	switch (pdata->device_type) {
6575bccae6eSSangbeom Kim 	case S5M8763X:
6587b003be8SKrzysztof Kozlowski 		info->irq = regmap_irq_get_virq(s5m87xx->irq_data,
6597b003be8SKrzysztof Kozlowski 				S5M8763_IRQ_ALARM0);
6605bccae6eSSangbeom Kim 		break;
6615bccae6eSSangbeom Kim 
6625bccae6eSSangbeom Kim 	case S5M8767X:
6637b003be8SKrzysztof Kozlowski 		info->irq = regmap_irq_get_virq(s5m87xx->irq_data,
6647b003be8SKrzysztof Kozlowski 				S5M8767_IRQ_RTCA1);
6655bccae6eSSangbeom Kim 		break;
6665bccae6eSSangbeom Kim 
6675bccae6eSSangbeom Kim 	default:
6685bccae6eSSangbeom Kim 		ret = -EINVAL;
6695bccae6eSSangbeom Kim 		dev_err(&pdev->dev, "Unsupported device type: %d\n", ret);
670e349c910SKrzysztof Kozlowski 		goto err;
6715bccae6eSSangbeom Kim 	}
6725bccae6eSSangbeom Kim 
6735bccae6eSSangbeom Kim 	platform_set_drvdata(pdev, info);
6745bccae6eSSangbeom Kim 
6755bccae6eSSangbeom Kim 	ret = s5m8767_rtc_init_reg(info);
6765bccae6eSSangbeom Kim 
6775bccae6eSSangbeom Kim 	if (info->wtsr_smpl) {
6785bccae6eSSangbeom Kim 		s5m_rtc_enable_wtsr(info, true);
6795bccae6eSSangbeom Kim 		s5m_rtc_enable_smpl(info, true);
6805bccae6eSSangbeom Kim 	}
6815bccae6eSSangbeom Kim 
6825bccae6eSSangbeom Kim 	device_init_wakeup(&pdev->dev, 1);
6835bccae6eSSangbeom Kim 
6845bccae6eSSangbeom Kim 	info->rtc_dev = devm_rtc_device_register(&pdev->dev, "s5m-rtc",
6855bccae6eSSangbeom Kim 						 &s5m_rtc_ops, THIS_MODULE);
6865bccae6eSSangbeom Kim 
687e349c910SKrzysztof Kozlowski 	if (IS_ERR(info->rtc_dev)) {
688e349c910SKrzysztof Kozlowski 		ret = PTR_ERR(info->rtc_dev);
689e349c910SKrzysztof Kozlowski 		goto err;
690e349c910SKrzysztof Kozlowski 	}
6915bccae6eSSangbeom Kim 
6925bccae6eSSangbeom Kim 	ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
6935bccae6eSSangbeom Kim 					s5m_rtc_alarm_irq, 0, "rtc-alarm0",
6945bccae6eSSangbeom Kim 					info);
695e349c910SKrzysztof Kozlowski 	if (ret < 0) {
6965bccae6eSSangbeom Kim 		dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
6975bccae6eSSangbeom Kim 			info->irq, ret);
698e349c910SKrzysztof Kozlowski 		goto err;
699e349c910SKrzysztof Kozlowski 	}
700e349c910SKrzysztof Kozlowski 
701e349c910SKrzysztof Kozlowski 	return 0;
702e349c910SKrzysztof Kozlowski 
703e349c910SKrzysztof Kozlowski err:
704e349c910SKrzysztof Kozlowski 	i2c_unregister_device(info->i2c);
7055bccae6eSSangbeom Kim 
7065bccae6eSSangbeom Kim 	return ret;
7075bccae6eSSangbeom Kim }
7085bccae6eSSangbeom Kim 
7095bccae6eSSangbeom Kim static void s5m_rtc_shutdown(struct platform_device *pdev)
7105bccae6eSSangbeom Kim {
7115bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = platform_get_drvdata(pdev);
7125bccae6eSSangbeom Kim 	int i;
7135bccae6eSSangbeom Kim 	unsigned int val = 0;
7145bccae6eSSangbeom Kim 	if (info->wtsr_smpl) {
7155bccae6eSSangbeom Kim 		for (i = 0; i < 3; i++) {
7165bccae6eSSangbeom Kim 			s5m_rtc_enable_wtsr(info, false);
717*f8b23bbdSKrzysztof Kozlowski 			regmap_read(info->regmap, info->regs->smpl_wtsr, &val);
7185bccae6eSSangbeom Kim 			pr_debug("%s: WTSR_SMPL reg(0x%02x)\n", __func__, val);
7195bccae6eSSangbeom Kim 			if (val & WTSR_ENABLE_MASK)
7205bccae6eSSangbeom Kim 				pr_emerg("%s: fail to disable WTSR\n",
7215bccae6eSSangbeom Kim 					 __func__);
7225bccae6eSSangbeom Kim 			else {
7235bccae6eSSangbeom Kim 				pr_info("%s: success to disable WTSR\n",
7245bccae6eSSangbeom Kim 					__func__);
7255bccae6eSSangbeom Kim 				break;
7265bccae6eSSangbeom Kim 			}
7275bccae6eSSangbeom Kim 		}
7285bccae6eSSangbeom Kim 	}
7295bccae6eSSangbeom Kim 	/* Disable SMPL when power off */
7305bccae6eSSangbeom Kim 	s5m_rtc_enable_smpl(info, false);
7315bccae6eSSangbeom Kim }
7325bccae6eSSangbeom Kim 
733e349c910SKrzysztof Kozlowski static int s5m_rtc_remove(struct platform_device *pdev)
734e349c910SKrzysztof Kozlowski {
735e349c910SKrzysztof Kozlowski 	struct s5m_rtc_info *info = platform_get_drvdata(pdev);
736e349c910SKrzysztof Kozlowski 
737e349c910SKrzysztof Kozlowski 	/* Perform also all shutdown steps when removing */
738e349c910SKrzysztof Kozlowski 	s5m_rtc_shutdown(pdev);
739e349c910SKrzysztof Kozlowski 	i2c_unregister_device(info->i2c);
740e349c910SKrzysztof Kozlowski 
741e349c910SKrzysztof Kozlowski 	return 0;
742e349c910SKrzysztof Kozlowski }
743e349c910SKrzysztof Kozlowski 
74411ba5a1eSGeert Uytterhoeven #ifdef CONFIG_PM_SLEEP
745222ead7fSKrzysztof Kozlowski static int s5m_rtc_resume(struct device *dev)
746222ead7fSKrzysztof Kozlowski {
747222ead7fSKrzysztof Kozlowski 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
748222ead7fSKrzysztof Kozlowski 	int ret = 0;
749222ead7fSKrzysztof Kozlowski 
750222ead7fSKrzysztof Kozlowski 	if (device_may_wakeup(dev))
751222ead7fSKrzysztof Kozlowski 		ret = disable_irq_wake(info->irq);
752222ead7fSKrzysztof Kozlowski 
753222ead7fSKrzysztof Kozlowski 	return ret;
754222ead7fSKrzysztof Kozlowski }
755222ead7fSKrzysztof Kozlowski 
756222ead7fSKrzysztof Kozlowski static int s5m_rtc_suspend(struct device *dev)
757222ead7fSKrzysztof Kozlowski {
758222ead7fSKrzysztof Kozlowski 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
759222ead7fSKrzysztof Kozlowski 	int ret = 0;
760222ead7fSKrzysztof Kozlowski 
761222ead7fSKrzysztof Kozlowski 	if (device_may_wakeup(dev))
762222ead7fSKrzysztof Kozlowski 		ret = enable_irq_wake(info->irq);
763222ead7fSKrzysztof Kozlowski 
764222ead7fSKrzysztof Kozlowski 	return ret;
765222ead7fSKrzysztof Kozlowski }
76611ba5a1eSGeert Uytterhoeven #endif /* CONFIG_PM_SLEEP */
767222ead7fSKrzysztof Kozlowski 
768222ead7fSKrzysztof Kozlowski static SIMPLE_DEV_PM_OPS(s5m_rtc_pm_ops, s5m_rtc_suspend, s5m_rtc_resume);
769222ead7fSKrzysztof Kozlowski 
7705bccae6eSSangbeom Kim static const struct platform_device_id s5m_rtc_id[] = {
7715bccae6eSSangbeom Kim 	{ "s5m-rtc", 0 },
7725bccae6eSSangbeom Kim };
7735bccae6eSSangbeom Kim 
7745bccae6eSSangbeom Kim static struct platform_driver s5m_rtc_driver = {
7755bccae6eSSangbeom Kim 	.driver		= {
7765bccae6eSSangbeom Kim 		.name	= "s5m-rtc",
7775bccae6eSSangbeom Kim 		.owner	= THIS_MODULE,
778222ead7fSKrzysztof Kozlowski 		.pm	= &s5m_rtc_pm_ops,
7795bccae6eSSangbeom Kim 	},
7805bccae6eSSangbeom Kim 	.probe		= s5m_rtc_probe,
781e349c910SKrzysztof Kozlowski 	.remove		= s5m_rtc_remove,
7825bccae6eSSangbeom Kim 	.shutdown	= s5m_rtc_shutdown,
7835bccae6eSSangbeom Kim 	.id_table	= s5m_rtc_id,
7845bccae6eSSangbeom Kim };
7855bccae6eSSangbeom Kim 
7865bccae6eSSangbeom Kim module_platform_driver(s5m_rtc_driver);
7875bccae6eSSangbeom Kim 
7885bccae6eSSangbeom Kim /* Module information */
7895bccae6eSSangbeom Kim MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
7905bccae6eSSangbeom Kim MODULE_DESCRIPTION("Samsung S5M RTC driver");
7915bccae6eSSangbeom Kim MODULE_LICENSE("GPL");
7925bccae6eSSangbeom Kim MODULE_ALIAS("platform:s5m-rtc");
793