xref: /linux/drivers/rtc/rtc-s5m.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
126dcc12cSKrzysztof Kozlowski // SPDX-License-Identifier: GPL-2.0+
226dcc12cSKrzysztof Kozlowski //
326dcc12cSKrzysztof Kozlowski // Copyright (c) 2013-2014 Samsung Electronics Co., Ltd
426dcc12cSKrzysztof Kozlowski //	http://www.samsung.com
526dcc12cSKrzysztof Kozlowski //
626dcc12cSKrzysztof Kozlowski //  Copyright (C) 2013 Google, Inc
75bccae6eSSangbeom Kim 
8a737e835SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9a737e835SJoe Perches 
105bccae6eSSangbeom Kim #include <linux/module.h>
115bccae6eSSangbeom Kim #include <linux/i2c.h>
125bccae6eSSangbeom Kim #include <linux/bcd.h>
135bccae6eSSangbeom Kim #include <linux/regmap.h>
145bccae6eSSangbeom Kim #include <linux/rtc.h>
155bccae6eSSangbeom Kim #include <linux/platform_device.h>
165bccae6eSSangbeom Kim #include <linux/mfd/samsung/core.h>
175bccae6eSSangbeom Kim #include <linux/mfd/samsung/irq.h>
185bccae6eSSangbeom Kim #include <linux/mfd/samsung/rtc.h>
190c5deb1eSKrzysztof Kozlowski #include <linux/mfd/samsung/s2mps14.h>
205bccae6eSSangbeom Kim 
21d73238d4SKrzysztof Kozlowski /*
22d73238d4SKrzysztof Kozlowski  * Maximum number of retries for checking changes in UDR field
23602cb5bbSKrzysztof Kozlowski  * of S5M_RTC_UDR_CON register (to limit possible endless loop).
24d73238d4SKrzysztof Kozlowski  *
25d73238d4SKrzysztof Kozlowski  * After writing to RTC registers (setting time or alarm) read the UDR field
26602cb5bbSKrzysztof Kozlowski  * in S5M_RTC_UDR_CON register. UDR is auto-cleared when data have
27d73238d4SKrzysztof Kozlowski  * been transferred.
28d73238d4SKrzysztof Kozlowski  */
29d73238d4SKrzysztof Kozlowski #define UDR_READ_RETRY_CNT	5
30d73238d4SKrzysztof Kozlowski 
314a681243SGustavo A. R. Silva enum {
324a681243SGustavo A. R. Silva 	RTC_SEC = 0,
334a681243SGustavo A. R. Silva 	RTC_MIN,
344a681243SGustavo A. R. Silva 	RTC_HOUR,
354a681243SGustavo A. R. Silva 	RTC_WEEKDAY,
364a681243SGustavo A. R. Silva 	RTC_DATE,
374a681243SGustavo A. R. Silva 	RTC_MONTH,
384a681243SGustavo A. R. Silva 	RTC_YEAR1,
394a681243SGustavo A. R. Silva 	RTC_YEAR2,
40756d5282SGustavo A. R. Silva 	/* Make sure this is always the last enum name. */
41756d5282SGustavo A. R. Silva 	RTC_MAX_NUM_TIME_REGS
424a681243SGustavo A. R. Silva };
434a681243SGustavo A. R. Silva 
448ae83b6fSKrzysztof Kozlowski /*
458ae83b6fSKrzysztof Kozlowski  * Registers used by the driver which are different between chipsets.
468ae83b6fSKrzysztof Kozlowski  *
478ae83b6fSKrzysztof Kozlowski  * Operations like read time and write alarm/time require updating
488ae83b6fSKrzysztof Kozlowski  * specific fields in UDR register. These fields usually are auto-cleared
498ae83b6fSKrzysztof Kozlowski  * (with some exceptions).
508ae83b6fSKrzysztof Kozlowski  *
518ae83b6fSKrzysztof Kozlowski  * Table of operations per device:
528ae83b6fSKrzysztof Kozlowski  *
538ae83b6fSKrzysztof Kozlowski  * Device     | Write time | Read time | Write alarm
548ae83b6fSKrzysztof Kozlowski  * =================================================
558ae83b6fSKrzysztof Kozlowski  * S5M8767    | UDR + TIME |           | UDR
568ae83b6fSKrzysztof Kozlowski  * S2MPS11/14 | WUDR       | RUDR      | WUDR + RUDR
578ae83b6fSKrzysztof Kozlowski  * S2MPS13    | WUDR       | RUDR      | WUDR + AUDR
588ae83b6fSKrzysztof Kozlowski  * S2MPS15    | WUDR       | RUDR      | AUDR
598ae83b6fSKrzysztof Kozlowski  */
60f8b23bbdSKrzysztof Kozlowski struct s5m_rtc_reg_config {
61f8b23bbdSKrzysztof Kozlowski 	/* Number of registers used for setting time/alarm0/alarm1 */
62f8b23bbdSKrzysztof Kozlowski 	unsigned int regs_count;
63f8b23bbdSKrzysztof Kozlowski 	/* First register for time, seconds */
64f8b23bbdSKrzysztof Kozlowski 	unsigned int time;
65f8b23bbdSKrzysztof Kozlowski 	/* RTC control register */
66f8b23bbdSKrzysztof Kozlowski 	unsigned int ctrl;
67f8b23bbdSKrzysztof Kozlowski 	/* First register for alarm 0, seconds */
68f8b23bbdSKrzysztof Kozlowski 	unsigned int alarm0;
69f8b23bbdSKrzysztof Kozlowski 	/* First register for alarm 1, seconds */
70f8b23bbdSKrzysztof Kozlowski 	unsigned int alarm1;
71f8b23bbdSKrzysztof Kozlowski 	/*
72f8b23bbdSKrzysztof Kozlowski 	 * Register for update flag (UDR). Typically setting UDR field to 1
73f8b23bbdSKrzysztof Kozlowski 	 * will enable update of time or alarm register. Then it will be
74f8b23bbdSKrzysztof Kozlowski 	 * auto-cleared after successful update.
75f8b23bbdSKrzysztof Kozlowski 	 */
76a83a793aSKrzysztof Kozlowski 	unsigned int udr_update;
7767a6025aSKrzysztof Kozlowski 	/* Auto-cleared mask in UDR field for writing time and alarm */
7867a6025aSKrzysztof Kozlowski 	unsigned int autoclear_udr_mask;
798ae83b6fSKrzysztof Kozlowski 	/*
808ae83b6fSKrzysztof Kozlowski 	 * Masks in UDR field for time and alarm operations.
818ae83b6fSKrzysztof Kozlowski 	 * The read time mask can be 0. Rest should not.
828ae83b6fSKrzysztof Kozlowski 	 */
838ae83b6fSKrzysztof Kozlowski 	unsigned int read_time_udr_mask;
848ae83b6fSKrzysztof Kozlowski 	unsigned int write_time_udr_mask;
858ae83b6fSKrzysztof Kozlowski 	unsigned int write_alarm_udr_mask;
86f8b23bbdSKrzysztof Kozlowski };
87f8b23bbdSKrzysztof Kozlowski 
88*a3165abaSDavid Virag /* Register map for S5M8767 */
89f8b23bbdSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s5m_rtc_regs = {
90f8b23bbdSKrzysztof Kozlowski 	.regs_count		= 8,
91f8b23bbdSKrzysztof Kozlowski 	.time			= S5M_RTC_SEC,
92f8b23bbdSKrzysztof Kozlowski 	.ctrl			= S5M_ALARM1_CONF,
93f8b23bbdSKrzysztof Kozlowski 	.alarm0			= S5M_ALARM0_SEC,
94f8b23bbdSKrzysztof Kozlowski 	.alarm1			= S5M_ALARM1_SEC,
95a83a793aSKrzysztof Kozlowski 	.udr_update		= S5M_RTC_UDR_CON,
9667a6025aSKrzysztof Kozlowski 	.autoclear_udr_mask	= S5M_RTC_UDR_MASK,
978ae83b6fSKrzysztof Kozlowski 	.read_time_udr_mask	= 0, /* Not needed */
988ae83b6fSKrzysztof Kozlowski 	.write_time_udr_mask	= S5M_RTC_UDR_MASK | S5M_RTC_TIME_EN_MASK,
998ae83b6fSKrzysztof Kozlowski 	.write_alarm_udr_mask	= S5M_RTC_UDR_MASK,
100f8b23bbdSKrzysztof Kozlowski };
101f8b23bbdSKrzysztof Kozlowski 
1028ae83b6fSKrzysztof Kozlowski /* Register map for S2MPS13 */
1038ae83b6fSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s2mps13_rtc_regs = {
1040c5deb1eSKrzysztof Kozlowski 	.regs_count		= 7,
1050c5deb1eSKrzysztof Kozlowski 	.time			= S2MPS_RTC_SEC,
1060c5deb1eSKrzysztof Kozlowski 	.ctrl			= S2MPS_RTC_CTRL,
1070c5deb1eSKrzysztof Kozlowski 	.alarm0			= S2MPS_ALARM0_SEC,
1080c5deb1eSKrzysztof Kozlowski 	.alarm1			= S2MPS_ALARM1_SEC,
109a83a793aSKrzysztof Kozlowski 	.udr_update		= S2MPS_RTC_UDR_CON,
11067a6025aSKrzysztof Kozlowski 	.autoclear_udr_mask	= S2MPS_RTC_WUDR_MASK,
1118ae83b6fSKrzysztof Kozlowski 	.read_time_udr_mask	= S2MPS_RTC_RUDR_MASK,
1128ae83b6fSKrzysztof Kozlowski 	.write_time_udr_mask	= S2MPS_RTC_WUDR_MASK,
1138ae83b6fSKrzysztof Kozlowski 	.write_alarm_udr_mask	= S2MPS_RTC_WUDR_MASK | S2MPS13_RTC_AUDR_MASK,
1148ae83b6fSKrzysztof Kozlowski };
1158ae83b6fSKrzysztof Kozlowski 
1168ae83b6fSKrzysztof Kozlowski /* Register map for S2MPS11/14 */
1178ae83b6fSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s2mps14_rtc_regs = {
1188ae83b6fSKrzysztof Kozlowski 	.regs_count		= 7,
1198ae83b6fSKrzysztof Kozlowski 	.time			= S2MPS_RTC_SEC,
1208ae83b6fSKrzysztof Kozlowski 	.ctrl			= S2MPS_RTC_CTRL,
1218ae83b6fSKrzysztof Kozlowski 	.alarm0			= S2MPS_ALARM0_SEC,
1228ae83b6fSKrzysztof Kozlowski 	.alarm1			= S2MPS_ALARM1_SEC,
1238ae83b6fSKrzysztof Kozlowski 	.udr_update		= S2MPS_RTC_UDR_CON,
1248ae83b6fSKrzysztof Kozlowski 	.autoclear_udr_mask	= S2MPS_RTC_WUDR_MASK,
1258ae83b6fSKrzysztof Kozlowski 	.read_time_udr_mask	= S2MPS_RTC_RUDR_MASK,
1268ae83b6fSKrzysztof Kozlowski 	.write_time_udr_mask	= S2MPS_RTC_WUDR_MASK,
1278ae83b6fSKrzysztof Kozlowski 	.write_alarm_udr_mask	= S2MPS_RTC_WUDR_MASK | S2MPS_RTC_RUDR_MASK,
1288ae83b6fSKrzysztof Kozlowski };
1298ae83b6fSKrzysztof Kozlowski 
1308ae83b6fSKrzysztof Kozlowski /*
1318ae83b6fSKrzysztof Kozlowski  * Register map for S2MPS15 - in comparison to S2MPS14 the WUDR and AUDR bits
1328ae83b6fSKrzysztof Kozlowski  * are swapped.
1338ae83b6fSKrzysztof Kozlowski  */
1348ae83b6fSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s2mps15_rtc_regs = {
1358ae83b6fSKrzysztof Kozlowski 	.regs_count		= 7,
1368ae83b6fSKrzysztof Kozlowski 	.time			= S2MPS_RTC_SEC,
1378ae83b6fSKrzysztof Kozlowski 	.ctrl			= S2MPS_RTC_CTRL,
1388ae83b6fSKrzysztof Kozlowski 	.alarm0			= S2MPS_ALARM0_SEC,
1398ae83b6fSKrzysztof Kozlowski 	.alarm1			= S2MPS_ALARM1_SEC,
1408ae83b6fSKrzysztof Kozlowski 	.udr_update		= S2MPS_RTC_UDR_CON,
1418ae83b6fSKrzysztof Kozlowski 	.autoclear_udr_mask	= S2MPS_RTC_WUDR_MASK,
1428ae83b6fSKrzysztof Kozlowski 	.read_time_udr_mask	= S2MPS_RTC_RUDR_MASK,
1438ae83b6fSKrzysztof Kozlowski 	.write_time_udr_mask	= S2MPS15_RTC_WUDR_MASK,
1448ae83b6fSKrzysztof Kozlowski 	.write_alarm_udr_mask	= S2MPS15_RTC_AUDR_MASK,
1450c5deb1eSKrzysztof Kozlowski };
1460c5deb1eSKrzysztof Kozlowski 
1475bccae6eSSangbeom Kim struct s5m_rtc_info {
1485bccae6eSSangbeom Kim 	struct device *dev;
149e349c910SKrzysztof Kozlowski 	struct i2c_client *i2c;
1505bccae6eSSangbeom Kim 	struct sec_pmic_dev *s5m87xx;
1515ccb7d71SGeert Uytterhoeven 	struct regmap *regmap;
1525bccae6eSSangbeom Kim 	struct rtc_device *rtc_dev;
1535bccae6eSSangbeom Kim 	int irq;
15494f91922SKrzysztof Kozlowski 	enum sec_device_type device_type;
1555bccae6eSSangbeom Kim 	int rtc_24hr_mode;
156f8b23bbdSKrzysztof Kozlowski 	const struct s5m_rtc_reg_config	*regs;
1575bccae6eSSangbeom Kim };
1585bccae6eSSangbeom Kim 
159e349c910SKrzysztof Kozlowski static const struct regmap_config s5m_rtc_regmap_config = {
160e349c910SKrzysztof Kozlowski 	.reg_bits = 8,
161e349c910SKrzysztof Kozlowski 	.val_bits = 8,
162e349c910SKrzysztof Kozlowski 
163602cb5bbSKrzysztof Kozlowski 	.max_register = S5M_RTC_REG_MAX,
164e349c910SKrzysztof Kozlowski };
165e349c910SKrzysztof Kozlowski 
166e349c910SKrzysztof Kozlowski static const struct regmap_config s2mps14_rtc_regmap_config = {
167e349c910SKrzysztof Kozlowski 	.reg_bits = 8,
168e349c910SKrzysztof Kozlowski 	.val_bits = 8,
169e349c910SKrzysztof Kozlowski 
170e349c910SKrzysztof Kozlowski 	.max_register = S2MPS_RTC_REG_MAX,
171e349c910SKrzysztof Kozlowski };
172e349c910SKrzysztof Kozlowski 
s5m8767_data_to_tm(u8 * data,struct rtc_time * tm,int rtc_24hr_mode)1735bccae6eSSangbeom Kim static void s5m8767_data_to_tm(u8 *data, struct rtc_time *tm,
1745bccae6eSSangbeom Kim 			       int rtc_24hr_mode)
1755bccae6eSSangbeom Kim {
1765bccae6eSSangbeom Kim 	tm->tm_sec = data[RTC_SEC] & 0x7f;
1775bccae6eSSangbeom Kim 	tm->tm_min = data[RTC_MIN] & 0x7f;
1785bccae6eSSangbeom Kim 	if (rtc_24hr_mode) {
1795bccae6eSSangbeom Kim 		tm->tm_hour = data[RTC_HOUR] & 0x1f;
1805bccae6eSSangbeom Kim 	} else {
1815bccae6eSSangbeom Kim 		tm->tm_hour = data[RTC_HOUR] & 0x0f;
1825bccae6eSSangbeom Kim 		if (data[RTC_HOUR] & HOUR_PM_MASK)
1835bccae6eSSangbeom Kim 			tm->tm_hour += 12;
1845bccae6eSSangbeom Kim 	}
1855bccae6eSSangbeom Kim 
1865bccae6eSSangbeom Kim 	tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f);
1875bccae6eSSangbeom Kim 	tm->tm_mday = data[RTC_DATE] & 0x1f;
1885bccae6eSSangbeom Kim 	tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
1895bccae6eSSangbeom Kim 	tm->tm_year = (data[RTC_YEAR1] & 0x7f) + 100;
1905bccae6eSSangbeom Kim 	tm->tm_yday = 0;
1915bccae6eSSangbeom Kim 	tm->tm_isdst = 0;
1925bccae6eSSangbeom Kim }
1935bccae6eSSangbeom Kim 
s5m8767_tm_to_data(struct rtc_time * tm,u8 * data)1945bccae6eSSangbeom Kim static int s5m8767_tm_to_data(struct rtc_time *tm, u8 *data)
1955bccae6eSSangbeom Kim {
1965bccae6eSSangbeom Kim 	data[RTC_SEC] = tm->tm_sec;
1975bccae6eSSangbeom Kim 	data[RTC_MIN] = tm->tm_min;
1985bccae6eSSangbeom Kim 
1995bccae6eSSangbeom Kim 	if (tm->tm_hour >= 12)
2005bccae6eSSangbeom Kim 		data[RTC_HOUR] = tm->tm_hour | HOUR_PM_MASK;
2015bccae6eSSangbeom Kim 	else
2025bccae6eSSangbeom Kim 		data[RTC_HOUR] = tm->tm_hour & ~HOUR_PM_MASK;
2035bccae6eSSangbeom Kim 
2045bccae6eSSangbeom Kim 	data[RTC_WEEKDAY] = 1 << tm->tm_wday;
2055bccae6eSSangbeom Kim 	data[RTC_DATE] = tm->tm_mday;
2065bccae6eSSangbeom Kim 	data[RTC_MONTH] = tm->tm_mon + 1;
207fffd603aSAlexandre Belloni 	data[RTC_YEAR1] = tm->tm_year - 100;
2085bccae6eSSangbeom Kim 
2095bccae6eSSangbeom Kim 	return 0;
2105bccae6eSSangbeom Kim }
2115bccae6eSSangbeom Kim 
212d73238d4SKrzysztof Kozlowski /*
213d73238d4SKrzysztof Kozlowski  * Read RTC_UDR_CON register and wait till UDR field is cleared.
214d73238d4SKrzysztof Kozlowski  * This indicates that time/alarm update ended.
215d73238d4SKrzysztof Kozlowski  */
s5m8767_wait_for_udr_update(struct s5m_rtc_info * info)2168124c711SKrzysztof Kozlowski static int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info)
217d73238d4SKrzysztof Kozlowski {
218d73238d4SKrzysztof Kozlowski 	int ret, retry = UDR_READ_RETRY_CNT;
219d73238d4SKrzysztof Kozlowski 	unsigned int data;
220d73238d4SKrzysztof Kozlowski 
221d73238d4SKrzysztof Kozlowski 	do {
222a83a793aSKrzysztof Kozlowski 		ret = regmap_read(info->regmap, info->regs->udr_update, &data);
22367a6025aSKrzysztof Kozlowski 	} while (--retry && (data & info->regs->autoclear_udr_mask) && !ret);
224d73238d4SKrzysztof Kozlowski 
225d73238d4SKrzysztof Kozlowski 	if (!retry)
226d73238d4SKrzysztof Kozlowski 		dev_err(info->dev, "waiting for UDR update, reached max number of retries\n");
227d73238d4SKrzysztof Kozlowski 
228d73238d4SKrzysztof Kozlowski 	return ret;
229d73238d4SKrzysztof Kozlowski }
230d73238d4SKrzysztof Kozlowski 
s5m_check_peding_alarm_interrupt(struct s5m_rtc_info * info,struct rtc_wkalrm * alarm)2318124c711SKrzysztof Kozlowski static int s5m_check_peding_alarm_interrupt(struct s5m_rtc_info *info,
232f8b23bbdSKrzysztof Kozlowski 		struct rtc_wkalrm *alarm)
233f8b23bbdSKrzysztof Kozlowski {
234f8b23bbdSKrzysztof Kozlowski 	int ret;
235f8b23bbdSKrzysztof Kozlowski 	unsigned int val;
236f8b23bbdSKrzysztof Kozlowski 
237f8b23bbdSKrzysztof Kozlowski 	switch (info->device_type) {
238f8b23bbdSKrzysztof Kozlowski 	case S5M8767X:
239f8b23bbdSKrzysztof Kozlowski 		ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val);
240f8b23bbdSKrzysztof Kozlowski 		val &= S5M_ALARM0_STATUS;
241f8b23bbdSKrzysztof Kozlowski 		break;
242a65e5efaSAlim Akhtar 	case S2MPS15X:
2430c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
2445281f94aSKrzysztof Kozlowski 	case S2MPS13X:
2450c5deb1eSKrzysztof Kozlowski 		ret = regmap_read(info->s5m87xx->regmap_pmic, S2MPS14_REG_ST2,
2460c5deb1eSKrzysztof Kozlowski 				&val);
2470c5deb1eSKrzysztof Kozlowski 		val &= S2MPS_ALARM0_STATUS;
2480c5deb1eSKrzysztof Kozlowski 		break;
249f8b23bbdSKrzysztof Kozlowski 	default:
250f8b23bbdSKrzysztof Kozlowski 		return -EINVAL;
251f8b23bbdSKrzysztof Kozlowski 	}
252f8b23bbdSKrzysztof Kozlowski 	if (ret < 0)
253f8b23bbdSKrzysztof Kozlowski 		return ret;
254f8b23bbdSKrzysztof Kozlowski 
255f8b23bbdSKrzysztof Kozlowski 	if (val)
256f8b23bbdSKrzysztof Kozlowski 		alarm->pending = 1;
257f8b23bbdSKrzysztof Kozlowski 	else
258f8b23bbdSKrzysztof Kozlowski 		alarm->pending = 0;
259f8b23bbdSKrzysztof Kozlowski 
260f8b23bbdSKrzysztof Kozlowski 	return 0;
261f8b23bbdSKrzysztof Kozlowski }
262f8b23bbdSKrzysztof Kozlowski 
s5m8767_rtc_set_time_reg(struct s5m_rtc_info * info)2638124c711SKrzysztof Kozlowski static int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info)
2645bccae6eSSangbeom Kim {
2655bccae6eSSangbeom Kim 	int ret;
2665bccae6eSSangbeom Kim 	unsigned int data;
2675bccae6eSSangbeom Kim 
268a83a793aSKrzysztof Kozlowski 	ret = regmap_read(info->regmap, info->regs->udr_update, &data);
2695bccae6eSSangbeom Kim 	if (ret < 0) {
2705bccae6eSSangbeom Kim 		dev_err(info->dev, "failed to read update reg(%d)\n", ret);
2715bccae6eSSangbeom Kim 		return ret;
2725bccae6eSSangbeom Kim 	}
2735bccae6eSSangbeom Kim 
2748ae83b6fSKrzysztof Kozlowski 	data |= info->regs->write_time_udr_mask;
275a65e5efaSAlim Akhtar 
276a83a793aSKrzysztof Kozlowski 	ret = regmap_write(info->regmap, info->regs->udr_update, data);
2775bccae6eSSangbeom Kim 	if (ret < 0) {
2785bccae6eSSangbeom Kim 		dev_err(info->dev, "failed to write update reg(%d)\n", ret);
2795bccae6eSSangbeom Kim 		return ret;
2805bccae6eSSangbeom Kim 	}
2815bccae6eSSangbeom Kim 
282d73238d4SKrzysztof Kozlowski 	ret = s5m8767_wait_for_udr_update(info);
2835bccae6eSSangbeom Kim 
2845bccae6eSSangbeom Kim 	return ret;
2855bccae6eSSangbeom Kim }
2865bccae6eSSangbeom Kim 
s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info * info)2878124c711SKrzysztof Kozlowski static int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info)
2885bccae6eSSangbeom Kim {
2895bccae6eSSangbeom Kim 	int ret;
2905bccae6eSSangbeom Kim 	unsigned int data;
2915bccae6eSSangbeom Kim 
292a83a793aSKrzysztof Kozlowski 	ret = regmap_read(info->regmap, info->regs->udr_update, &data);
2935bccae6eSSangbeom Kim 	if (ret < 0) {
2945bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to read update reg(%d)\n",
2955bccae6eSSangbeom Kim 			__func__, ret);
2965bccae6eSSangbeom Kim 		return ret;
2975bccae6eSSangbeom Kim 	}
2985bccae6eSSangbeom Kim 
2998ae83b6fSKrzysztof Kozlowski 	data |= info->regs->write_alarm_udr_mask;
3000c5deb1eSKrzysztof Kozlowski 	switch (info->device_type) {
3010c5deb1eSKrzysztof Kozlowski 	case S5M8767X:
3020c5deb1eSKrzysztof Kozlowski 		data &= ~S5M_RTC_TIME_EN_MASK;
3030c5deb1eSKrzysztof Kozlowski 		break;
304a65e5efaSAlim Akhtar 	case S2MPS15X:
3050c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
3065281f94aSKrzysztof Kozlowski 	case S2MPS13X:
3078ae83b6fSKrzysztof Kozlowski 		/* No exceptions needed */
3085281f94aSKrzysztof Kozlowski 		break;
3090c5deb1eSKrzysztof Kozlowski 	default:
3100c5deb1eSKrzysztof Kozlowski 		return -EINVAL;
3110c5deb1eSKrzysztof Kozlowski 	}
3125bccae6eSSangbeom Kim 
313a83a793aSKrzysztof Kozlowski 	ret = regmap_write(info->regmap, info->regs->udr_update, data);
3145bccae6eSSangbeom Kim 	if (ret < 0) {
3155bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to write update reg(%d)\n",
3165bccae6eSSangbeom Kim 			__func__, ret);
3175bccae6eSSangbeom Kim 		return ret;
3185bccae6eSSangbeom Kim 	}
3195bccae6eSSangbeom Kim 
320d73238d4SKrzysztof Kozlowski 	ret = s5m8767_wait_for_udr_update(info);
3215bccae6eSSangbeom Kim 
3225281f94aSKrzysztof Kozlowski 	/* On S2MPS13 the AUDR is not auto-cleared */
3235281f94aSKrzysztof Kozlowski 	if (info->device_type == S2MPS13X)
324a83a793aSKrzysztof Kozlowski 		regmap_update_bits(info->regmap, info->regs->udr_update,
3255281f94aSKrzysztof Kozlowski 				   S2MPS13_RTC_AUDR_MASK, 0);
3265281f94aSKrzysztof Kozlowski 
3275bccae6eSSangbeom Kim 	return ret;
3285bccae6eSSangbeom Kim }
3295bccae6eSSangbeom Kim 
s5m_rtc_read_time(struct device * dev,struct rtc_time * tm)3305bccae6eSSangbeom Kim static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm)
3315bccae6eSSangbeom Kim {
3325bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
333756d5282SGustavo A. R. Silva 	u8 data[RTC_MAX_NUM_TIME_REGS];
3345bccae6eSSangbeom Kim 	int ret;
3355bccae6eSSangbeom Kim 
3368ae83b6fSKrzysztof Kozlowski 	if (info->regs->read_time_udr_mask) {
3370c5deb1eSKrzysztof Kozlowski 		ret = regmap_update_bits(info->regmap,
338a83a793aSKrzysztof Kozlowski 				info->regs->udr_update,
3398ae83b6fSKrzysztof Kozlowski 				info->regs->read_time_udr_mask,
3408ae83b6fSKrzysztof Kozlowski 				info->regs->read_time_udr_mask);
3410c5deb1eSKrzysztof Kozlowski 		if (ret) {
3420c5deb1eSKrzysztof Kozlowski 			dev_err(dev,
3430c5deb1eSKrzysztof Kozlowski 				"Failed to prepare registers for time reading: %d\n",
3440c5deb1eSKrzysztof Kozlowski 				ret);
3450c5deb1eSKrzysztof Kozlowski 			return ret;
3460c5deb1eSKrzysztof Kozlowski 		}
3470c5deb1eSKrzysztof Kozlowski 	}
348f8b23bbdSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, info->regs->time, data,
349f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
3505bccae6eSSangbeom Kim 	if (ret < 0)
3515bccae6eSSangbeom Kim 		return ret;
3525bccae6eSSangbeom Kim 
3535bccae6eSSangbeom Kim 	switch (info->device_type) {
3545bccae6eSSangbeom Kim 	case S5M8767X:
355a65e5efaSAlim Akhtar 	case S2MPS15X:
3560c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
3575281f94aSKrzysztof Kozlowski 	case S2MPS13X:
3585bccae6eSSangbeom Kim 		s5m8767_data_to_tm(data, tm, info->rtc_24hr_mode);
3595bccae6eSSangbeom Kim 		break;
3605bccae6eSSangbeom Kim 
3615bccae6eSSangbeom Kim 	default:
3625bccae6eSSangbeom Kim 		return -EINVAL;
3635bccae6eSSangbeom Kim 	}
3645bccae6eSSangbeom Kim 
3655527842eSAndy Shevchenko 	dev_dbg(dev, "%s: %ptR(%d)\n", __func__, tm, tm->tm_wday);
3665bccae6eSSangbeom Kim 
36722652ba7SAlexandre Belloni 	return 0;
3685bccae6eSSangbeom Kim }
3695bccae6eSSangbeom Kim 
s5m_rtc_set_time(struct device * dev,struct rtc_time * tm)3705bccae6eSSangbeom Kim static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm)
3715bccae6eSSangbeom Kim {
3725bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
373756d5282SGustavo A. R. Silva 	u8 data[RTC_MAX_NUM_TIME_REGS];
3745bccae6eSSangbeom Kim 	int ret = 0;
3755bccae6eSSangbeom Kim 
3765bccae6eSSangbeom Kim 	switch (info->device_type) {
3775bccae6eSSangbeom Kim 	case S5M8767X:
378a65e5efaSAlim Akhtar 	case S2MPS15X:
3790c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
3805281f94aSKrzysztof Kozlowski 	case S2MPS13X:
3815bccae6eSSangbeom Kim 		ret = s5m8767_tm_to_data(tm, data);
3825bccae6eSSangbeom Kim 		break;
3835bccae6eSSangbeom Kim 	default:
3845bccae6eSSangbeom Kim 		return -EINVAL;
3855bccae6eSSangbeom Kim 	}
3865bccae6eSSangbeom Kim 
3875bccae6eSSangbeom Kim 	if (ret < 0)
3885bccae6eSSangbeom Kim 		return ret;
3895bccae6eSSangbeom Kim 
3905527842eSAndy Shevchenko 	dev_dbg(dev, "%s: %ptR(%d)\n", __func__, tm, tm->tm_wday);
3915bccae6eSSangbeom Kim 
392f8b23bbdSKrzysztof Kozlowski 	ret = regmap_raw_write(info->regmap, info->regs->time, data,
393f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
3945bccae6eSSangbeom Kim 	if (ret < 0)
3955bccae6eSSangbeom Kim 		return ret;
3965bccae6eSSangbeom Kim 
3975bccae6eSSangbeom Kim 	ret = s5m8767_rtc_set_time_reg(info);
3985bccae6eSSangbeom Kim 
3995bccae6eSSangbeom Kim 	return ret;
4005bccae6eSSangbeom Kim }
4015bccae6eSSangbeom Kim 
s5m_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alrm)4025bccae6eSSangbeom Kim static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
4035bccae6eSSangbeom Kim {
4045bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
405756d5282SGustavo A. R. Silva 	u8 data[RTC_MAX_NUM_TIME_REGS];
4065bccae6eSSangbeom Kim 	int ret, i;
4075bccae6eSSangbeom Kim 
408f8b23bbdSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
409f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
4105bccae6eSSangbeom Kim 	if (ret < 0)
4115bccae6eSSangbeom Kim 		return ret;
4125bccae6eSSangbeom Kim 
4135bccae6eSSangbeom Kim 	switch (info->device_type) {
4145bccae6eSSangbeom Kim 	case S5M8767X:
415a65e5efaSAlim Akhtar 	case S2MPS15X:
4160c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
4175281f94aSKrzysztof Kozlowski 	case S2MPS13X:
4185bccae6eSSangbeom Kim 		s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
4195bccae6eSSangbeom Kim 		alrm->enabled = 0;
420f8b23bbdSKrzysztof Kozlowski 		for (i = 0; i < info->regs->regs_count; i++) {
4215bccae6eSSangbeom Kim 			if (data[i] & ALARM_ENABLE_MASK) {
4225bccae6eSSangbeom Kim 				alrm->enabled = 1;
4235bccae6eSSangbeom Kim 				break;
4245bccae6eSSangbeom Kim 			}
4255bccae6eSSangbeom Kim 		}
4265bccae6eSSangbeom Kim 		break;
4275bccae6eSSangbeom Kim 
4285bccae6eSSangbeom Kim 	default:
4295bccae6eSSangbeom Kim 		return -EINVAL;
4305bccae6eSSangbeom Kim 	}
4315bccae6eSSangbeom Kim 
4325527842eSAndy Shevchenko 	dev_dbg(dev, "%s: %ptR(%d)\n", __func__, &alrm->time, alrm->time.tm_wday);
433f8b23bbdSKrzysztof Kozlowski 
434f2581b1dSNobuhiro Iwamatsu 	return s5m_check_peding_alarm_interrupt(info, alrm);
4355bccae6eSSangbeom Kim }
4365bccae6eSSangbeom Kim 
s5m_rtc_stop_alarm(struct s5m_rtc_info * info)4375bccae6eSSangbeom Kim static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info)
4385bccae6eSSangbeom Kim {
439756d5282SGustavo A. R. Silva 	u8 data[RTC_MAX_NUM_TIME_REGS];
4405bccae6eSSangbeom Kim 	int ret, i;
4415bccae6eSSangbeom Kim 	struct rtc_time tm;
4425bccae6eSSangbeom Kim 
443f8b23bbdSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
444f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
4455bccae6eSSangbeom Kim 	if (ret < 0)
4465bccae6eSSangbeom Kim 		return ret;
4475bccae6eSSangbeom Kim 
4485bccae6eSSangbeom Kim 	s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
4495527842eSAndy Shevchenko 	dev_dbg(info->dev, "%s: %ptR(%d)\n", __func__, &tm, tm.tm_wday);
4505bccae6eSSangbeom Kim 
4515bccae6eSSangbeom Kim 	switch (info->device_type) {
4525bccae6eSSangbeom Kim 	case S5M8767X:
453a65e5efaSAlim Akhtar 	case S2MPS15X:
4540c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
4555281f94aSKrzysztof Kozlowski 	case S2MPS13X:
456f8b23bbdSKrzysztof Kozlowski 		for (i = 0; i < info->regs->regs_count; i++)
4575bccae6eSSangbeom Kim 			data[i] &= ~ALARM_ENABLE_MASK;
4585bccae6eSSangbeom Kim 
459f8b23bbdSKrzysztof Kozlowski 		ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
460f8b23bbdSKrzysztof Kozlowski 				info->regs->regs_count);
4615bccae6eSSangbeom Kim 		if (ret < 0)
4625bccae6eSSangbeom Kim 			return ret;
4635bccae6eSSangbeom Kim 
4645bccae6eSSangbeom Kim 		ret = s5m8767_rtc_set_alarm_reg(info);
4655bccae6eSSangbeom Kim 
4665bccae6eSSangbeom Kim 		break;
4675bccae6eSSangbeom Kim 
4685bccae6eSSangbeom Kim 	default:
4695bccae6eSSangbeom Kim 		return -EINVAL;
4705bccae6eSSangbeom Kim 	}
4715bccae6eSSangbeom Kim 
4725bccae6eSSangbeom Kim 	return ret;
4735bccae6eSSangbeom Kim }
4745bccae6eSSangbeom Kim 
s5m_rtc_start_alarm(struct s5m_rtc_info * info)4755bccae6eSSangbeom Kim static int s5m_rtc_start_alarm(struct s5m_rtc_info *info)
4765bccae6eSSangbeom Kim {
4775bccae6eSSangbeom Kim 	int ret;
478756d5282SGustavo A. R. Silva 	u8 data[RTC_MAX_NUM_TIME_REGS];
4795bccae6eSSangbeom Kim 	struct rtc_time tm;
4805bccae6eSSangbeom Kim 
481f8b23bbdSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
482f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
4835bccae6eSSangbeom Kim 	if (ret < 0)
4845bccae6eSSangbeom Kim 		return ret;
4855bccae6eSSangbeom Kim 
4865bccae6eSSangbeom Kim 	s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
4875527842eSAndy Shevchenko 	dev_dbg(info->dev, "%s: %ptR(%d)\n", __func__, &tm, tm.tm_wday);
4885bccae6eSSangbeom Kim 
4895bccae6eSSangbeom Kim 	switch (info->device_type) {
4905bccae6eSSangbeom Kim 	case S5M8767X:
491a65e5efaSAlim Akhtar 	case S2MPS15X:
4920c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
4935281f94aSKrzysztof Kozlowski 	case S2MPS13X:
4945bccae6eSSangbeom Kim 		data[RTC_SEC] |= ALARM_ENABLE_MASK;
4955bccae6eSSangbeom Kim 		data[RTC_MIN] |= ALARM_ENABLE_MASK;
4965bccae6eSSangbeom Kim 		data[RTC_HOUR] |= ALARM_ENABLE_MASK;
4975bccae6eSSangbeom Kim 		data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
4985bccae6eSSangbeom Kim 		if (data[RTC_DATE] & 0x1f)
4995bccae6eSSangbeom Kim 			data[RTC_DATE] |= ALARM_ENABLE_MASK;
5005bccae6eSSangbeom Kim 		if (data[RTC_MONTH] & 0xf)
5015bccae6eSSangbeom Kim 			data[RTC_MONTH] |= ALARM_ENABLE_MASK;
5025bccae6eSSangbeom Kim 		if (data[RTC_YEAR1] & 0x7f)
5035bccae6eSSangbeom Kim 			data[RTC_YEAR1] |= ALARM_ENABLE_MASK;
5045bccae6eSSangbeom Kim 
505f8b23bbdSKrzysztof Kozlowski 		ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
506f8b23bbdSKrzysztof Kozlowski 				info->regs->regs_count);
5075bccae6eSSangbeom Kim 		if (ret < 0)
5085bccae6eSSangbeom Kim 			return ret;
5095bccae6eSSangbeom Kim 		ret = s5m8767_rtc_set_alarm_reg(info);
5105bccae6eSSangbeom Kim 
5115bccae6eSSangbeom Kim 		break;
5125bccae6eSSangbeom Kim 
5135bccae6eSSangbeom Kim 	default:
5145bccae6eSSangbeom Kim 		return -EINVAL;
5155bccae6eSSangbeom Kim 	}
5165bccae6eSSangbeom Kim 
5175bccae6eSSangbeom Kim 	return ret;
5185bccae6eSSangbeom Kim }
5195bccae6eSSangbeom Kim 
s5m_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alrm)5205bccae6eSSangbeom Kim static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
5215bccae6eSSangbeom Kim {
5225bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
523756d5282SGustavo A. R. Silva 	u8 data[RTC_MAX_NUM_TIME_REGS];
5245bccae6eSSangbeom Kim 	int ret;
5255bccae6eSSangbeom Kim 
5265bccae6eSSangbeom Kim 	switch (info->device_type) {
5275bccae6eSSangbeom Kim 	case S5M8767X:
528a65e5efaSAlim Akhtar 	case S2MPS15X:
5290c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
5305281f94aSKrzysztof Kozlowski 	case S2MPS13X:
5315bccae6eSSangbeom Kim 		s5m8767_tm_to_data(&alrm->time, data);
5325bccae6eSSangbeom Kim 		break;
5335bccae6eSSangbeom Kim 
5345bccae6eSSangbeom Kim 	default:
5355bccae6eSSangbeom Kim 		return -EINVAL;
5365bccae6eSSangbeom Kim 	}
5375bccae6eSSangbeom Kim 
5385527842eSAndy Shevchenko 	dev_dbg(dev, "%s: %ptR(%d)\n", __func__, &alrm->time, alrm->time.tm_wday);
5395bccae6eSSangbeom Kim 
5405bccae6eSSangbeom Kim 	ret = s5m_rtc_stop_alarm(info);
5415bccae6eSSangbeom Kim 	if (ret < 0)
5425bccae6eSSangbeom Kim 		return ret;
5435bccae6eSSangbeom Kim 
544f8b23bbdSKrzysztof Kozlowski 	ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
545f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
5465bccae6eSSangbeom Kim 	if (ret < 0)
5475bccae6eSSangbeom Kim 		return ret;
5485bccae6eSSangbeom Kim 
5495bccae6eSSangbeom Kim 	ret = s5m8767_rtc_set_alarm_reg(info);
5505bccae6eSSangbeom Kim 	if (ret < 0)
5515bccae6eSSangbeom Kim 		return ret;
5525bccae6eSSangbeom Kim 
5535bccae6eSSangbeom Kim 	if (alrm->enabled)
5545bccae6eSSangbeom Kim 		ret = s5m_rtc_start_alarm(info);
5555bccae6eSSangbeom Kim 
5565bccae6eSSangbeom Kim 	return ret;
5575bccae6eSSangbeom Kim }
5585bccae6eSSangbeom Kim 
s5m_rtc_alarm_irq_enable(struct device * dev,unsigned int enabled)5595bccae6eSSangbeom Kim static int s5m_rtc_alarm_irq_enable(struct device *dev,
5605bccae6eSSangbeom Kim 				    unsigned int enabled)
5615bccae6eSSangbeom Kim {
5625bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
5635bccae6eSSangbeom Kim 
5645bccae6eSSangbeom Kim 	if (enabled)
5655bccae6eSSangbeom Kim 		return s5m_rtc_start_alarm(info);
5665bccae6eSSangbeom Kim 	else
5675bccae6eSSangbeom Kim 		return s5m_rtc_stop_alarm(info);
5685bccae6eSSangbeom Kim }
5695bccae6eSSangbeom Kim 
s5m_rtc_alarm_irq(int irq,void * data)5705bccae6eSSangbeom Kim static irqreturn_t s5m_rtc_alarm_irq(int irq, void *data)
5715bccae6eSSangbeom Kim {
5725bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = data;
5735bccae6eSSangbeom Kim 
5745bccae6eSSangbeom Kim 	rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
5755bccae6eSSangbeom Kim 
5765bccae6eSSangbeom Kim 	return IRQ_HANDLED;
5775bccae6eSSangbeom Kim }
5785bccae6eSSangbeom Kim 
5795bccae6eSSangbeom Kim static const struct rtc_class_ops s5m_rtc_ops = {
5805bccae6eSSangbeom Kim 	.read_time = s5m_rtc_read_time,
5815bccae6eSSangbeom Kim 	.set_time = s5m_rtc_set_time,
5825bccae6eSSangbeom Kim 	.read_alarm = s5m_rtc_read_alarm,
5835bccae6eSSangbeom Kim 	.set_alarm = s5m_rtc_set_alarm,
5845bccae6eSSangbeom Kim 	.alarm_irq_enable = s5m_rtc_alarm_irq_enable,
5855bccae6eSSangbeom Kim };
5865bccae6eSSangbeom Kim 
s5m8767_rtc_init_reg(struct s5m_rtc_info * info)5875bccae6eSSangbeom Kim static int s5m8767_rtc_init_reg(struct s5m_rtc_info *info)
5885bccae6eSSangbeom Kim {
5895bccae6eSSangbeom Kim 	u8 data[2];
5905bccae6eSSangbeom Kim 	int ret;
5915bccae6eSSangbeom Kim 
5920c5deb1eSKrzysztof Kozlowski 	switch (info->device_type) {
5930c5deb1eSKrzysztof Kozlowski 	case S5M8767X:
5940c5f5d9aSKrzysztof Kozlowski 		/* UDR update time. Default of 7.32 ms is too long. */
5950c5f5d9aSKrzysztof Kozlowski 		ret = regmap_update_bits(info->regmap, S5M_RTC_UDR_CON,
5960c5f5d9aSKrzysztof Kozlowski 				S5M_RTC_UDR_T_MASK, S5M_RTC_UDR_T_450_US);
5970c5f5d9aSKrzysztof Kozlowski 		if (ret < 0)
5980c5f5d9aSKrzysztof Kozlowski 			dev_err(info->dev, "%s: fail to change UDR time: %d\n",
5990c5f5d9aSKrzysztof Kozlowski 					__func__, ret);
6000c5f5d9aSKrzysztof Kozlowski 
6015bccae6eSSangbeom Kim 		/* Set RTC control register : Binary mode, 24hour mode */
6025bccae6eSSangbeom Kim 		data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
6035bccae6eSSangbeom Kim 		data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
6045bccae6eSSangbeom Kim 
605602cb5bbSKrzysztof Kozlowski 		ret = regmap_raw_write(info->regmap, S5M_ALARM0_CONF, data, 2);
6060c5deb1eSKrzysztof Kozlowski 		break;
6070c5deb1eSKrzysztof Kozlowski 
608a65e5efaSAlim Akhtar 	case S2MPS15X:
6090c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
6105281f94aSKrzysztof Kozlowski 	case S2MPS13X:
6110c5deb1eSKrzysztof Kozlowski 		data[0] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
6120c5deb1eSKrzysztof Kozlowski 		ret = regmap_write(info->regmap, info->regs->ctrl, data[0]);
613ff02c044SJoonyoung Shim 		if (ret < 0)
614ff02c044SJoonyoung Shim 			break;
615ff02c044SJoonyoung Shim 
616ff02c044SJoonyoung Shim 		/*
617ff02c044SJoonyoung Shim 		 * Should set WUDR & (RUDR or AUDR) bits to high after writing
618ff02c044SJoonyoung Shim 		 * RTC_CTRL register like writing Alarm registers. We can't find
619ff02c044SJoonyoung Shim 		 * the description from datasheet but vendor code does that
620ff02c044SJoonyoung Shim 		 * really.
621ff02c044SJoonyoung Shim 		 */
622ff02c044SJoonyoung Shim 		ret = s5m8767_rtc_set_alarm_reg(info);
6230c5deb1eSKrzysztof Kozlowski 		break;
6240c5deb1eSKrzysztof Kozlowski 
6250c5deb1eSKrzysztof Kozlowski 	default:
6260c5deb1eSKrzysztof Kozlowski 		return -EINVAL;
6270c5deb1eSKrzysztof Kozlowski 	}
6280c5deb1eSKrzysztof Kozlowski 
6290c5deb1eSKrzysztof Kozlowski 	info->rtc_24hr_mode = 1;
6305bccae6eSSangbeom Kim 	if (ret < 0) {
6315bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
6325bccae6eSSangbeom Kim 			__func__, ret);
6335bccae6eSSangbeom Kim 		return ret;
6345bccae6eSSangbeom Kim 	}
6355bccae6eSSangbeom Kim 
6365bccae6eSSangbeom Kim 	return ret;
6375bccae6eSSangbeom Kim }
6385bccae6eSSangbeom Kim 
s5m_rtc_probe(struct platform_device * pdev)6395bccae6eSSangbeom Kim static int s5m_rtc_probe(struct platform_device *pdev)
6405bccae6eSSangbeom Kim {
6415bccae6eSSangbeom Kim 	struct sec_pmic_dev *s5m87xx = dev_get_drvdata(pdev->dev.parent);
6425bccae6eSSangbeom Kim 	struct s5m_rtc_info *info;
643e349c910SKrzysztof Kozlowski 	const struct regmap_config *regmap_cfg;
644a0347f20SKrzysztof Kozlowski 	int ret, alarm_irq;
6455bccae6eSSangbeom Kim 
6465bccae6eSSangbeom Kim 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
6475bccae6eSSangbeom Kim 	if (!info)
6485bccae6eSSangbeom Kim 		return -ENOMEM;
6495bccae6eSSangbeom Kim 
65094f91922SKrzysztof Kozlowski 	switch (platform_get_device_id(pdev)->driver_data) {
651a65e5efaSAlim Akhtar 	case S2MPS15X:
6528ae83b6fSKrzysztof Kozlowski 		regmap_cfg = &s2mps14_rtc_regmap_config;
6538ae83b6fSKrzysztof Kozlowski 		info->regs = &s2mps15_rtc_regs;
6548ae83b6fSKrzysztof Kozlowski 		alarm_irq = S2MPS14_IRQ_RTCA0;
6558ae83b6fSKrzysztof Kozlowski 		break;
656e349c910SKrzysztof Kozlowski 	case S2MPS14X:
6578ae83b6fSKrzysztof Kozlowski 		regmap_cfg = &s2mps14_rtc_regmap_config;
6588ae83b6fSKrzysztof Kozlowski 		info->regs = &s2mps14_rtc_regs;
6598ae83b6fSKrzysztof Kozlowski 		alarm_irq = S2MPS14_IRQ_RTCA0;
6608ae83b6fSKrzysztof Kozlowski 		break;
6615281f94aSKrzysztof Kozlowski 	case S2MPS13X:
662e349c910SKrzysztof Kozlowski 		regmap_cfg = &s2mps14_rtc_regmap_config;
6638ae83b6fSKrzysztof Kozlowski 		info->regs = &s2mps13_rtc_regs;
664a0347f20SKrzysztof Kozlowski 		alarm_irq = S2MPS14_IRQ_RTCA0;
665e349c910SKrzysztof Kozlowski 		break;
666e349c910SKrzysztof Kozlowski 	case S5M8767X:
667e349c910SKrzysztof Kozlowski 		regmap_cfg = &s5m_rtc_regmap_config;
668f8b23bbdSKrzysztof Kozlowski 		info->regs = &s5m_rtc_regs;
669a0347f20SKrzysztof Kozlowski 		alarm_irq = S5M8767_IRQ_RTCA1;
670e349c910SKrzysztof Kozlowski 		break;
671e349c910SKrzysztof Kozlowski 	default:
67294f91922SKrzysztof Kozlowski 		dev_err(&pdev->dev,
67394f91922SKrzysztof Kozlowski 				"Device type %lu is not supported by RTC driver\n",
67494f91922SKrzysztof Kozlowski 				platform_get_device_id(pdev)->driver_data);
675e349c910SKrzysztof Kozlowski 		return -ENODEV;
676e349c910SKrzysztof Kozlowski 	}
677e349c910SKrzysztof Kozlowski 
6787db7ad08SBartosz Golaszewski 	info->i2c = devm_i2c_new_dummy_device(&pdev->dev, s5m87xx->i2c->adapter,
6797db7ad08SBartosz Golaszewski 					      RTC_I2C_ADDR);
680aae364d2SWolfram Sang 	if (IS_ERR(info->i2c)) {
681e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Failed to allocate I2C for RTC\n");
682aae364d2SWolfram Sang 		return PTR_ERR(info->i2c);
683e349c910SKrzysztof Kozlowski 	}
684e349c910SKrzysztof Kozlowski 
685e349c910SKrzysztof Kozlowski 	info->regmap = devm_regmap_init_i2c(info->i2c, regmap_cfg);
686e349c910SKrzysztof Kozlowski 	if (IS_ERR(info->regmap)) {
687e349c910SKrzysztof Kozlowski 		ret = PTR_ERR(info->regmap);
688e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Failed to allocate RTC register map: %d\n",
689e349c910SKrzysztof Kozlowski 				ret);
6907db7ad08SBartosz Golaszewski 		return ret;
691e349c910SKrzysztof Kozlowski 	}
692e349c910SKrzysztof Kozlowski 
6935bccae6eSSangbeom Kim 	info->dev = &pdev->dev;
6945bccae6eSSangbeom Kim 	info->s5m87xx = s5m87xx;
69594f91922SKrzysztof Kozlowski 	info->device_type = platform_get_device_id(pdev)->driver_data;
6965bccae6eSSangbeom Kim 
697b7d5b9a9SBartlomiej Zolnierkiewicz 	if (s5m87xx->irq_data) {
698a0347f20SKrzysztof Kozlowski 		info->irq = regmap_irq_get_virq(s5m87xx->irq_data, alarm_irq);
699a0347f20SKrzysztof Kozlowski 		if (info->irq <= 0) {
700a0347f20SKrzysztof Kozlowski 			dev_err(&pdev->dev, "Failed to get virtual IRQ %d\n",
701a0347f20SKrzysztof Kozlowski 				alarm_irq);
7027db7ad08SBartosz Golaszewski 			return -EINVAL;
7035bccae6eSSangbeom Kim 		}
704b7d5b9a9SBartlomiej Zolnierkiewicz 	}
7055bccae6eSSangbeom Kim 
7065bccae6eSSangbeom Kim 	platform_set_drvdata(pdev, info);
7075bccae6eSSangbeom Kim 
7085bccae6eSSangbeom Kim 	ret = s5m8767_rtc_init_reg(info);
7093be95d27SBartosz Golaszewski 	if (ret)
7103be95d27SBartosz Golaszewski 		return ret;
7115bccae6eSSangbeom Kim 
712dae68c6bSAlexandre Belloni 	info->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
7137db7ad08SBartosz Golaszewski 	if (IS_ERR(info->rtc_dev))
7147db7ad08SBartosz Golaszewski 		return PTR_ERR(info->rtc_dev);
7155bccae6eSSangbeom Kim 
716dae68c6bSAlexandre Belloni 	info->rtc_dev->ops = &s5m_rtc_ops;
717dae68c6bSAlexandre Belloni 
718fffd603aSAlexandre Belloni 	info->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000;
719fffd603aSAlexandre Belloni 	info->rtc_dev->range_max = RTC_TIMESTAMP_END_2099;
720fffd603aSAlexandre Belloni 
721b7d5b9a9SBartlomiej Zolnierkiewicz 	if (!info->irq) {
7221ed4dba2SAlexandre Belloni 		clear_bit(RTC_FEATURE_ALARM, info->rtc_dev->features);
7231ed4dba2SAlexandre Belloni 	} else {
7245bccae6eSSangbeom Kim 		ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
7255bccae6eSSangbeom Kim 						s5m_rtc_alarm_irq, 0, "rtc-alarm0",
7265bccae6eSSangbeom Kim 						info);
727e349c910SKrzysztof Kozlowski 		if (ret < 0) {
7285bccae6eSSangbeom Kim 			dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
7295bccae6eSSangbeom Kim 				info->irq, ret);
7305bccae6eSSangbeom Kim 			return ret;
7315bccae6eSSangbeom Kim 		}
732308247d2SAlexandre Belloni 		device_init_wakeup(&pdev->dev, 1);
7331ed4dba2SAlexandre Belloni 	}
7345bccae6eSSangbeom Kim 
7351ed4dba2SAlexandre Belloni 	return devm_rtc_register_device(info->rtc_dev);
736e349c910SKrzysztof Kozlowski }
737e349c910SKrzysztof Kozlowski 
73811ba5a1eSGeert Uytterhoeven #ifdef CONFIG_PM_SLEEP
s5m_rtc_resume(struct device * dev)739222ead7fSKrzysztof Kozlowski static int s5m_rtc_resume(struct device *dev)
740222ead7fSKrzysztof Kozlowski {
741222ead7fSKrzysztof Kozlowski 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
742222ead7fSKrzysztof Kozlowski 	int ret = 0;
743222ead7fSKrzysztof Kozlowski 
744b7d5b9a9SBartlomiej Zolnierkiewicz 	if (info->irq && device_may_wakeup(dev))
745222ead7fSKrzysztof Kozlowski 		ret = disable_irq_wake(info->irq);
746222ead7fSKrzysztof Kozlowski 
747222ead7fSKrzysztof Kozlowski 	return ret;
748222ead7fSKrzysztof Kozlowski }
749222ead7fSKrzysztof Kozlowski 
s5m_rtc_suspend(struct device * dev)750222ead7fSKrzysztof Kozlowski static int s5m_rtc_suspend(struct device *dev)
751222ead7fSKrzysztof Kozlowski {
752222ead7fSKrzysztof Kozlowski 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
753222ead7fSKrzysztof Kozlowski 	int ret = 0;
754222ead7fSKrzysztof Kozlowski 
755b7d5b9a9SBartlomiej Zolnierkiewicz 	if (info->irq && device_may_wakeup(dev))
756222ead7fSKrzysztof Kozlowski 		ret = enable_irq_wake(info->irq);
757222ead7fSKrzysztof Kozlowski 
758222ead7fSKrzysztof Kozlowski 	return ret;
759222ead7fSKrzysztof Kozlowski }
76011ba5a1eSGeert Uytterhoeven #endif /* CONFIG_PM_SLEEP */
761222ead7fSKrzysztof Kozlowski 
762222ead7fSKrzysztof Kozlowski static SIMPLE_DEV_PM_OPS(s5m_rtc_pm_ops, s5m_rtc_suspend, s5m_rtc_resume);
763222ead7fSKrzysztof Kozlowski 
7645bccae6eSSangbeom Kim static const struct platform_device_id s5m_rtc_id[] = {
7650c5deb1eSKrzysztof Kozlowski 	{ "s5m-rtc",		S5M8767X },
7665281f94aSKrzysztof Kozlowski 	{ "s2mps13-rtc",	S2MPS13X },
7670c5deb1eSKrzysztof Kozlowski 	{ "s2mps14-rtc",	S2MPS14X },
768a65e5efaSAlim Akhtar 	{ "s2mps15-rtc",	S2MPS15X },
76945cd15e6SAndrey Ryabinin 	{ },
7705bccae6eSSangbeom Kim };
77163074cc3SJavier Martinez Canillas MODULE_DEVICE_TABLE(platform, s5m_rtc_id);
7725bccae6eSSangbeom Kim 
7735bccae6eSSangbeom Kim static struct platform_driver s5m_rtc_driver = {
7745bccae6eSSangbeom Kim 	.driver		= {
7755bccae6eSSangbeom Kim 		.name	= "s5m-rtc",
776222ead7fSKrzysztof Kozlowski 		.pm	= &s5m_rtc_pm_ops,
7775bccae6eSSangbeom Kim 	},
7785bccae6eSSangbeom Kim 	.probe		= s5m_rtc_probe,
7795bccae6eSSangbeom Kim 	.id_table	= s5m_rtc_id,
7805bccae6eSSangbeom Kim };
7815bccae6eSSangbeom Kim 
7825bccae6eSSangbeom Kim module_platform_driver(s5m_rtc_driver);
7835bccae6eSSangbeom Kim 
7845bccae6eSSangbeom Kim /* Module information */
7855bccae6eSSangbeom Kim MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
7860c5deb1eSKrzysztof Kozlowski MODULE_DESCRIPTION("Samsung S5M/S2MPS14 RTC driver");
7875bccae6eSSangbeom Kim MODULE_LICENSE("GPL");
788