xref: /linux/drivers/rtc/rtc-s5m.c (revision 8ae83b6f76fc74eb6535b9d331a3310a59c32f84)
15bccae6eSSangbeom Kim /*
2f8b23bbdSKrzysztof 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 
18a737e835SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19a737e835SJoe Perches 
205bccae6eSSangbeom Kim #include <linux/module.h>
215bccae6eSSangbeom Kim #include <linux/i2c.h>
225bccae6eSSangbeom Kim #include <linux/bcd.h>
235bccae6eSSangbeom Kim #include <linux/regmap.h>
245bccae6eSSangbeom Kim #include <linux/rtc.h>
255bccae6eSSangbeom Kim #include <linux/platform_device.h>
265bccae6eSSangbeom Kim #include <linux/mfd/samsung/core.h>
275bccae6eSSangbeom Kim #include <linux/mfd/samsung/irq.h>
285bccae6eSSangbeom Kim #include <linux/mfd/samsung/rtc.h>
290c5deb1eSKrzysztof Kozlowski #include <linux/mfd/samsung/s2mps14.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*8ae83b6fSKrzysztof Kozlowski /*
42*8ae83b6fSKrzysztof Kozlowski  * Registers used by the driver which are different between chipsets.
43*8ae83b6fSKrzysztof Kozlowski  *
44*8ae83b6fSKrzysztof Kozlowski  * Operations like read time and write alarm/time require updating
45*8ae83b6fSKrzysztof Kozlowski  * specific fields in UDR register. These fields usually are auto-cleared
46*8ae83b6fSKrzysztof Kozlowski  * (with some exceptions).
47*8ae83b6fSKrzysztof Kozlowski  *
48*8ae83b6fSKrzysztof Kozlowski  * Table of operations per device:
49*8ae83b6fSKrzysztof Kozlowski  *
50*8ae83b6fSKrzysztof Kozlowski  * Device     | Write time | Read time | Write alarm
51*8ae83b6fSKrzysztof Kozlowski  * =================================================
52*8ae83b6fSKrzysztof Kozlowski  * S5M8767    | UDR + TIME |           | UDR
53*8ae83b6fSKrzysztof Kozlowski  * S2MPS11/14 | WUDR       | RUDR      | WUDR + RUDR
54*8ae83b6fSKrzysztof Kozlowski  * S2MPS13    | WUDR       | RUDR      | WUDR + AUDR
55*8ae83b6fSKrzysztof Kozlowski  * S2MPS15    | WUDR       | RUDR      | AUDR
56*8ae83b6fSKrzysztof Kozlowski  */
57f8b23bbdSKrzysztof Kozlowski struct s5m_rtc_reg_config {
58f8b23bbdSKrzysztof Kozlowski 	/* Number of registers used for setting time/alarm0/alarm1 */
59f8b23bbdSKrzysztof Kozlowski 	unsigned int regs_count;
60f8b23bbdSKrzysztof Kozlowski 	/* First register for time, seconds */
61f8b23bbdSKrzysztof Kozlowski 	unsigned int time;
62f8b23bbdSKrzysztof Kozlowski 	/* RTC control register */
63f8b23bbdSKrzysztof Kozlowski 	unsigned int ctrl;
64f8b23bbdSKrzysztof Kozlowski 	/* First register for alarm 0, seconds */
65f8b23bbdSKrzysztof Kozlowski 	unsigned int alarm0;
66f8b23bbdSKrzysztof Kozlowski 	/* First register for alarm 1, seconds */
67f8b23bbdSKrzysztof Kozlowski 	unsigned int alarm1;
68f8b23bbdSKrzysztof Kozlowski 	/*
69f8b23bbdSKrzysztof Kozlowski 	 * Register for update flag (UDR). Typically setting UDR field to 1
70f8b23bbdSKrzysztof Kozlowski 	 * will enable update of time or alarm register. Then it will be
71f8b23bbdSKrzysztof Kozlowski 	 * auto-cleared after successful update.
72f8b23bbdSKrzysztof Kozlowski 	 */
73a83a793aSKrzysztof Kozlowski 	unsigned int udr_update;
7467a6025aSKrzysztof Kozlowski 	/* Auto-cleared mask in UDR field for writing time and alarm */
7567a6025aSKrzysztof Kozlowski 	unsigned int autoclear_udr_mask;
76*8ae83b6fSKrzysztof Kozlowski 	/*
77*8ae83b6fSKrzysztof Kozlowski 	 * Masks in UDR field for time and alarm operations.
78*8ae83b6fSKrzysztof Kozlowski 	 * The read time mask can be 0. Rest should not.
79*8ae83b6fSKrzysztof Kozlowski 	 */
80*8ae83b6fSKrzysztof Kozlowski 	unsigned int read_time_udr_mask;
81*8ae83b6fSKrzysztof Kozlowski 	unsigned int write_time_udr_mask;
82*8ae83b6fSKrzysztof Kozlowski 	unsigned int write_alarm_udr_mask;
83f8b23bbdSKrzysztof Kozlowski };
84f8b23bbdSKrzysztof Kozlowski 
85f8b23bbdSKrzysztof Kozlowski /* Register map for S5M8763 and S5M8767 */
86f8b23bbdSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s5m_rtc_regs = {
87f8b23bbdSKrzysztof Kozlowski 	.regs_count		= 8,
88f8b23bbdSKrzysztof Kozlowski 	.time			= S5M_RTC_SEC,
89f8b23bbdSKrzysztof Kozlowski 	.ctrl			= S5M_ALARM1_CONF,
90f8b23bbdSKrzysztof Kozlowski 	.alarm0			= S5M_ALARM0_SEC,
91f8b23bbdSKrzysztof Kozlowski 	.alarm1			= S5M_ALARM1_SEC,
92a83a793aSKrzysztof Kozlowski 	.udr_update		= S5M_RTC_UDR_CON,
9367a6025aSKrzysztof Kozlowski 	.autoclear_udr_mask	= S5M_RTC_UDR_MASK,
94*8ae83b6fSKrzysztof Kozlowski 	.read_time_udr_mask	= 0, /* Not needed */
95*8ae83b6fSKrzysztof Kozlowski 	.write_time_udr_mask	= S5M_RTC_UDR_MASK | S5M_RTC_TIME_EN_MASK,
96*8ae83b6fSKrzysztof Kozlowski 	.write_alarm_udr_mask	= S5M_RTC_UDR_MASK,
97f8b23bbdSKrzysztof Kozlowski };
98f8b23bbdSKrzysztof Kozlowski 
99*8ae83b6fSKrzysztof Kozlowski /* Register map for S2MPS13 */
100*8ae83b6fSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s2mps13_rtc_regs = {
1010c5deb1eSKrzysztof Kozlowski 	.regs_count		= 7,
1020c5deb1eSKrzysztof Kozlowski 	.time			= S2MPS_RTC_SEC,
1030c5deb1eSKrzysztof Kozlowski 	.ctrl			= S2MPS_RTC_CTRL,
1040c5deb1eSKrzysztof Kozlowski 	.alarm0			= S2MPS_ALARM0_SEC,
1050c5deb1eSKrzysztof Kozlowski 	.alarm1			= S2MPS_ALARM1_SEC,
106a83a793aSKrzysztof Kozlowski 	.udr_update		= S2MPS_RTC_UDR_CON,
10767a6025aSKrzysztof Kozlowski 	.autoclear_udr_mask	= S2MPS_RTC_WUDR_MASK,
108*8ae83b6fSKrzysztof Kozlowski 	.read_time_udr_mask	= S2MPS_RTC_RUDR_MASK,
109*8ae83b6fSKrzysztof Kozlowski 	.write_time_udr_mask	= S2MPS_RTC_WUDR_MASK,
110*8ae83b6fSKrzysztof Kozlowski 	.write_alarm_udr_mask	= S2MPS_RTC_WUDR_MASK | S2MPS13_RTC_AUDR_MASK,
111*8ae83b6fSKrzysztof Kozlowski };
112*8ae83b6fSKrzysztof Kozlowski 
113*8ae83b6fSKrzysztof Kozlowski /* Register map for S2MPS11/14 */
114*8ae83b6fSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s2mps14_rtc_regs = {
115*8ae83b6fSKrzysztof Kozlowski 	.regs_count		= 7,
116*8ae83b6fSKrzysztof Kozlowski 	.time			= S2MPS_RTC_SEC,
117*8ae83b6fSKrzysztof Kozlowski 	.ctrl			= S2MPS_RTC_CTRL,
118*8ae83b6fSKrzysztof Kozlowski 	.alarm0			= S2MPS_ALARM0_SEC,
119*8ae83b6fSKrzysztof Kozlowski 	.alarm1			= S2MPS_ALARM1_SEC,
120*8ae83b6fSKrzysztof Kozlowski 	.udr_update		= S2MPS_RTC_UDR_CON,
121*8ae83b6fSKrzysztof Kozlowski 	.autoclear_udr_mask	= S2MPS_RTC_WUDR_MASK,
122*8ae83b6fSKrzysztof Kozlowski 	.read_time_udr_mask	= S2MPS_RTC_RUDR_MASK,
123*8ae83b6fSKrzysztof Kozlowski 	.write_time_udr_mask	= S2MPS_RTC_WUDR_MASK,
124*8ae83b6fSKrzysztof Kozlowski 	.write_alarm_udr_mask	= S2MPS_RTC_WUDR_MASK | S2MPS_RTC_RUDR_MASK,
125*8ae83b6fSKrzysztof Kozlowski };
126*8ae83b6fSKrzysztof Kozlowski 
127*8ae83b6fSKrzysztof Kozlowski /*
128*8ae83b6fSKrzysztof Kozlowski  * Register map for S2MPS15 - in comparison to S2MPS14 the WUDR and AUDR bits
129*8ae83b6fSKrzysztof Kozlowski  * are swapped.
130*8ae83b6fSKrzysztof Kozlowski  */
131*8ae83b6fSKrzysztof Kozlowski static const struct s5m_rtc_reg_config s2mps15_rtc_regs = {
132*8ae83b6fSKrzysztof Kozlowski 	.regs_count		= 7,
133*8ae83b6fSKrzysztof Kozlowski 	.time			= S2MPS_RTC_SEC,
134*8ae83b6fSKrzysztof Kozlowski 	.ctrl			= S2MPS_RTC_CTRL,
135*8ae83b6fSKrzysztof Kozlowski 	.alarm0			= S2MPS_ALARM0_SEC,
136*8ae83b6fSKrzysztof Kozlowski 	.alarm1			= S2MPS_ALARM1_SEC,
137*8ae83b6fSKrzysztof Kozlowski 	.udr_update		= S2MPS_RTC_UDR_CON,
138*8ae83b6fSKrzysztof Kozlowski 	.autoclear_udr_mask	= S2MPS_RTC_WUDR_MASK,
139*8ae83b6fSKrzysztof Kozlowski 	.read_time_udr_mask	= S2MPS_RTC_RUDR_MASK,
140*8ae83b6fSKrzysztof Kozlowski 	.write_time_udr_mask	= S2MPS15_RTC_WUDR_MASK,
141*8ae83b6fSKrzysztof Kozlowski 	.write_alarm_udr_mask	= S2MPS15_RTC_AUDR_MASK,
1420c5deb1eSKrzysztof Kozlowski };
1430c5deb1eSKrzysztof Kozlowski 
1445bccae6eSSangbeom Kim struct s5m_rtc_info {
1455bccae6eSSangbeom Kim 	struct device *dev;
146e349c910SKrzysztof Kozlowski 	struct i2c_client *i2c;
1475bccae6eSSangbeom Kim 	struct sec_pmic_dev *s5m87xx;
1485ccb7d71SGeert Uytterhoeven 	struct regmap *regmap;
1495bccae6eSSangbeom Kim 	struct rtc_device *rtc_dev;
1505bccae6eSSangbeom Kim 	int irq;
15194f91922SKrzysztof Kozlowski 	enum sec_device_type device_type;
1525bccae6eSSangbeom Kim 	int rtc_24hr_mode;
153f8b23bbdSKrzysztof Kozlowski 	const struct s5m_rtc_reg_config	*regs;
1545bccae6eSSangbeom Kim };
1555bccae6eSSangbeom Kim 
156e349c910SKrzysztof Kozlowski static const struct regmap_config s5m_rtc_regmap_config = {
157e349c910SKrzysztof Kozlowski 	.reg_bits = 8,
158e349c910SKrzysztof Kozlowski 	.val_bits = 8,
159e349c910SKrzysztof Kozlowski 
160602cb5bbSKrzysztof Kozlowski 	.max_register = S5M_RTC_REG_MAX,
161e349c910SKrzysztof Kozlowski };
162e349c910SKrzysztof Kozlowski 
163e349c910SKrzysztof Kozlowski static const struct regmap_config s2mps14_rtc_regmap_config = {
164e349c910SKrzysztof Kozlowski 	.reg_bits = 8,
165e349c910SKrzysztof Kozlowski 	.val_bits = 8,
166e349c910SKrzysztof Kozlowski 
167e349c910SKrzysztof Kozlowski 	.max_register = S2MPS_RTC_REG_MAX,
168e349c910SKrzysztof Kozlowski };
169e349c910SKrzysztof Kozlowski 
1705bccae6eSSangbeom Kim static void s5m8767_data_to_tm(u8 *data, struct rtc_time *tm,
1715bccae6eSSangbeom Kim 			       int rtc_24hr_mode)
1725bccae6eSSangbeom Kim {
1735bccae6eSSangbeom Kim 	tm->tm_sec = data[RTC_SEC] & 0x7f;
1745bccae6eSSangbeom Kim 	tm->tm_min = data[RTC_MIN] & 0x7f;
1755bccae6eSSangbeom Kim 	if (rtc_24hr_mode) {
1765bccae6eSSangbeom Kim 		tm->tm_hour = data[RTC_HOUR] & 0x1f;
1775bccae6eSSangbeom Kim 	} else {
1785bccae6eSSangbeom Kim 		tm->tm_hour = data[RTC_HOUR] & 0x0f;
1795bccae6eSSangbeom Kim 		if (data[RTC_HOUR] & HOUR_PM_MASK)
1805bccae6eSSangbeom Kim 			tm->tm_hour += 12;
1815bccae6eSSangbeom Kim 	}
1825bccae6eSSangbeom Kim 
1835bccae6eSSangbeom Kim 	tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f);
1845bccae6eSSangbeom Kim 	tm->tm_mday = data[RTC_DATE] & 0x1f;
1855bccae6eSSangbeom Kim 	tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
1865bccae6eSSangbeom Kim 	tm->tm_year = (data[RTC_YEAR1] & 0x7f) + 100;
1875bccae6eSSangbeom Kim 	tm->tm_yday = 0;
1885bccae6eSSangbeom Kim 	tm->tm_isdst = 0;
1895bccae6eSSangbeom Kim }
1905bccae6eSSangbeom Kim 
1915bccae6eSSangbeom Kim static int s5m8767_tm_to_data(struct rtc_time *tm, u8 *data)
1925bccae6eSSangbeom Kim {
1935bccae6eSSangbeom Kim 	data[RTC_SEC] = tm->tm_sec;
1945bccae6eSSangbeom Kim 	data[RTC_MIN] = tm->tm_min;
1955bccae6eSSangbeom Kim 
1965bccae6eSSangbeom Kim 	if (tm->tm_hour >= 12)
1975bccae6eSSangbeom Kim 		data[RTC_HOUR] = tm->tm_hour | HOUR_PM_MASK;
1985bccae6eSSangbeom Kim 	else
1995bccae6eSSangbeom Kim 		data[RTC_HOUR] = tm->tm_hour & ~HOUR_PM_MASK;
2005bccae6eSSangbeom Kim 
2015bccae6eSSangbeom Kim 	data[RTC_WEEKDAY] = 1 << tm->tm_wday;
2025bccae6eSSangbeom Kim 	data[RTC_DATE] = tm->tm_mday;
2035bccae6eSSangbeom Kim 	data[RTC_MONTH] = tm->tm_mon + 1;
2045bccae6eSSangbeom Kim 	data[RTC_YEAR1] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
2055bccae6eSSangbeom Kim 
2065bccae6eSSangbeom Kim 	if (tm->tm_year < 100) {
207a737e835SJoe Perches 		pr_err("RTC cannot handle the year %d\n",
2085bccae6eSSangbeom Kim 		       1900 + tm->tm_year);
2095bccae6eSSangbeom Kim 		return -EINVAL;
2105bccae6eSSangbeom Kim 	} else {
2115bccae6eSSangbeom Kim 		return 0;
2125bccae6eSSangbeom Kim 	}
2135bccae6eSSangbeom Kim }
2145bccae6eSSangbeom Kim 
215d73238d4SKrzysztof Kozlowski /*
216d73238d4SKrzysztof Kozlowski  * Read RTC_UDR_CON register and wait till UDR field is cleared.
217d73238d4SKrzysztof Kozlowski  * This indicates that time/alarm update ended.
218d73238d4SKrzysztof Kozlowski  */
219d73238d4SKrzysztof Kozlowski static inline int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info)
220d73238d4SKrzysztof Kozlowski {
221d73238d4SKrzysztof Kozlowski 	int ret, retry = UDR_READ_RETRY_CNT;
222d73238d4SKrzysztof Kozlowski 	unsigned int data;
223d73238d4SKrzysztof Kozlowski 
224d73238d4SKrzysztof Kozlowski 	do {
225a83a793aSKrzysztof Kozlowski 		ret = regmap_read(info->regmap, info->regs->udr_update, &data);
22667a6025aSKrzysztof Kozlowski 	} while (--retry && (data & info->regs->autoclear_udr_mask) && !ret);
227d73238d4SKrzysztof Kozlowski 
228d73238d4SKrzysztof Kozlowski 	if (!retry)
229d73238d4SKrzysztof Kozlowski 		dev_err(info->dev, "waiting for UDR update, reached max number of retries\n");
230d73238d4SKrzysztof Kozlowski 
231d73238d4SKrzysztof Kozlowski 	return ret;
232d73238d4SKrzysztof Kozlowski }
233d73238d4SKrzysztof Kozlowski 
234f8b23bbdSKrzysztof Kozlowski static inline int s5m_check_peding_alarm_interrupt(struct s5m_rtc_info *info,
235f8b23bbdSKrzysztof Kozlowski 		struct rtc_wkalrm *alarm)
236f8b23bbdSKrzysztof Kozlowski {
237f8b23bbdSKrzysztof Kozlowski 	int ret;
238f8b23bbdSKrzysztof Kozlowski 	unsigned int val;
239f8b23bbdSKrzysztof Kozlowski 
240f8b23bbdSKrzysztof Kozlowski 	switch (info->device_type) {
241f8b23bbdSKrzysztof Kozlowski 	case S5M8767X:
242f8b23bbdSKrzysztof Kozlowski 	case S5M8763X:
243f8b23bbdSKrzysztof Kozlowski 		ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val);
244f8b23bbdSKrzysztof Kozlowski 		val &= S5M_ALARM0_STATUS;
245f8b23bbdSKrzysztof Kozlowski 		break;
246a65e5efaSAlim Akhtar 	case S2MPS15X:
2470c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
2485281f94aSKrzysztof Kozlowski 	case S2MPS13X:
2490c5deb1eSKrzysztof Kozlowski 		ret = regmap_read(info->s5m87xx->regmap_pmic, S2MPS14_REG_ST2,
2500c5deb1eSKrzysztof Kozlowski 				&val);
2510c5deb1eSKrzysztof Kozlowski 		val &= S2MPS_ALARM0_STATUS;
2520c5deb1eSKrzysztof Kozlowski 		break;
253f8b23bbdSKrzysztof Kozlowski 	default:
254f8b23bbdSKrzysztof Kozlowski 		return -EINVAL;
255f8b23bbdSKrzysztof Kozlowski 	}
256f8b23bbdSKrzysztof Kozlowski 	if (ret < 0)
257f8b23bbdSKrzysztof Kozlowski 		return ret;
258f8b23bbdSKrzysztof Kozlowski 
259f8b23bbdSKrzysztof Kozlowski 	if (val)
260f8b23bbdSKrzysztof Kozlowski 		alarm->pending = 1;
261f8b23bbdSKrzysztof Kozlowski 	else
262f8b23bbdSKrzysztof Kozlowski 		alarm->pending = 0;
263f8b23bbdSKrzysztof Kozlowski 
264f8b23bbdSKrzysztof Kozlowski 	return 0;
265f8b23bbdSKrzysztof Kozlowski }
266f8b23bbdSKrzysztof Kozlowski 
2675bccae6eSSangbeom Kim static inline int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info)
2685bccae6eSSangbeom Kim {
2695bccae6eSSangbeom Kim 	int ret;
2705bccae6eSSangbeom Kim 	unsigned int data;
2715bccae6eSSangbeom Kim 
272a83a793aSKrzysztof Kozlowski 	ret = regmap_read(info->regmap, info->regs->udr_update, &data);
2735bccae6eSSangbeom Kim 	if (ret < 0) {
2745bccae6eSSangbeom Kim 		dev_err(info->dev, "failed to read update reg(%d)\n", ret);
2755bccae6eSSangbeom Kim 		return ret;
2765bccae6eSSangbeom Kim 	}
2775bccae6eSSangbeom Kim 
278*8ae83b6fSKrzysztof Kozlowski 	data |= info->regs->write_time_udr_mask;
279a65e5efaSAlim Akhtar 
280a83a793aSKrzysztof Kozlowski 	ret = regmap_write(info->regmap, info->regs->udr_update, data);
2815bccae6eSSangbeom Kim 	if (ret < 0) {
2825bccae6eSSangbeom Kim 		dev_err(info->dev, "failed to write update reg(%d)\n", ret);
2835bccae6eSSangbeom Kim 		return ret;
2845bccae6eSSangbeom Kim 	}
2855bccae6eSSangbeom Kim 
286d73238d4SKrzysztof Kozlowski 	ret = s5m8767_wait_for_udr_update(info);
2875bccae6eSSangbeom Kim 
2885bccae6eSSangbeom Kim 	return ret;
2895bccae6eSSangbeom Kim }
2905bccae6eSSangbeom Kim 
2915bccae6eSSangbeom Kim static inline int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info)
2925bccae6eSSangbeom Kim {
2935bccae6eSSangbeom Kim 	int ret;
2945bccae6eSSangbeom Kim 	unsigned int data;
2955bccae6eSSangbeom Kim 
296a83a793aSKrzysztof Kozlowski 	ret = regmap_read(info->regmap, info->regs->udr_update, &data);
2975bccae6eSSangbeom Kim 	if (ret < 0) {
2985bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to read update reg(%d)\n",
2995bccae6eSSangbeom Kim 			__func__, ret);
3005bccae6eSSangbeom Kim 		return ret;
3015bccae6eSSangbeom Kim 	}
3025bccae6eSSangbeom Kim 
303*8ae83b6fSKrzysztof Kozlowski 	data |= info->regs->write_alarm_udr_mask;
3040c5deb1eSKrzysztof Kozlowski 	switch (info->device_type) {
3050c5deb1eSKrzysztof Kozlowski 	case S5M8763X:
3060c5deb1eSKrzysztof Kozlowski 	case S5M8767X:
3070c5deb1eSKrzysztof Kozlowski 		data &= ~S5M_RTC_TIME_EN_MASK;
3080c5deb1eSKrzysztof Kozlowski 		break;
309a65e5efaSAlim Akhtar 	case S2MPS15X:
3100c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
3115281f94aSKrzysztof Kozlowski 	case S2MPS13X:
312*8ae83b6fSKrzysztof Kozlowski 		/* No exceptions needed */
3135281f94aSKrzysztof Kozlowski 		break;
3140c5deb1eSKrzysztof Kozlowski 	default:
3150c5deb1eSKrzysztof Kozlowski 		return -EINVAL;
3160c5deb1eSKrzysztof Kozlowski 	}
3175bccae6eSSangbeom Kim 
318a83a793aSKrzysztof Kozlowski 	ret = regmap_write(info->regmap, info->regs->udr_update, data);
3195bccae6eSSangbeom Kim 	if (ret < 0) {
3205bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to write update reg(%d)\n",
3215bccae6eSSangbeom Kim 			__func__, ret);
3225bccae6eSSangbeom Kim 		return ret;
3235bccae6eSSangbeom Kim 	}
3245bccae6eSSangbeom Kim 
325d73238d4SKrzysztof Kozlowski 	ret = s5m8767_wait_for_udr_update(info);
3265bccae6eSSangbeom Kim 
3275281f94aSKrzysztof Kozlowski 	/* On S2MPS13 the AUDR is not auto-cleared */
3285281f94aSKrzysztof Kozlowski 	if (info->device_type == S2MPS13X)
329a83a793aSKrzysztof Kozlowski 		regmap_update_bits(info->regmap, info->regs->udr_update,
3305281f94aSKrzysztof Kozlowski 				   S2MPS13_RTC_AUDR_MASK, 0);
3315281f94aSKrzysztof Kozlowski 
3325bccae6eSSangbeom Kim 	return ret;
3335bccae6eSSangbeom Kim }
3345bccae6eSSangbeom Kim 
3355bccae6eSSangbeom Kim static void s5m8763_data_to_tm(u8 *data, struct rtc_time *tm)
3365bccae6eSSangbeom Kim {
3375bccae6eSSangbeom Kim 	tm->tm_sec = bcd2bin(data[RTC_SEC]);
3385bccae6eSSangbeom Kim 	tm->tm_min = bcd2bin(data[RTC_MIN]);
3395bccae6eSSangbeom Kim 
3405bccae6eSSangbeom Kim 	if (data[RTC_HOUR] & HOUR_12) {
3415bccae6eSSangbeom Kim 		tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
3425bccae6eSSangbeom Kim 		if (data[RTC_HOUR] & HOUR_PM)
3435bccae6eSSangbeom Kim 			tm->tm_hour += 12;
3445bccae6eSSangbeom Kim 	} else {
3455bccae6eSSangbeom Kim 		tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
3465bccae6eSSangbeom Kim 	}
3475bccae6eSSangbeom Kim 
3485bccae6eSSangbeom Kim 	tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
3495bccae6eSSangbeom Kim 	tm->tm_mday = bcd2bin(data[RTC_DATE]);
3505bccae6eSSangbeom Kim 	tm->tm_mon = bcd2bin(data[RTC_MONTH]);
3515bccae6eSSangbeom Kim 	tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
3525bccae6eSSangbeom Kim 	tm->tm_year -= 1900;
3535bccae6eSSangbeom Kim }
3545bccae6eSSangbeom Kim 
3555bccae6eSSangbeom Kim static void s5m8763_tm_to_data(struct rtc_time *tm, u8 *data)
3565bccae6eSSangbeom Kim {
3575bccae6eSSangbeom Kim 	data[RTC_SEC] = bin2bcd(tm->tm_sec);
3585bccae6eSSangbeom Kim 	data[RTC_MIN] = bin2bcd(tm->tm_min);
3595bccae6eSSangbeom Kim 	data[RTC_HOUR] = bin2bcd(tm->tm_hour);
3605bccae6eSSangbeom Kim 	data[RTC_WEEKDAY] = tm->tm_wday;
3615bccae6eSSangbeom Kim 	data[RTC_DATE] = bin2bcd(tm->tm_mday);
3625bccae6eSSangbeom Kim 	data[RTC_MONTH] = bin2bcd(tm->tm_mon);
3635bccae6eSSangbeom Kim 	data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
3645bccae6eSSangbeom Kim 	data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
3655bccae6eSSangbeom Kim }
3665bccae6eSSangbeom Kim 
3675bccae6eSSangbeom Kim static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm)
3685bccae6eSSangbeom Kim {
3695bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
370f8b23bbdSKrzysztof Kozlowski 	u8 data[info->regs->regs_count];
3715bccae6eSSangbeom Kim 	int ret;
3725bccae6eSSangbeom Kim 
373*8ae83b6fSKrzysztof Kozlowski 	if (info->regs->read_time_udr_mask) {
3740c5deb1eSKrzysztof Kozlowski 		ret = regmap_update_bits(info->regmap,
375a83a793aSKrzysztof Kozlowski 				info->regs->udr_update,
376*8ae83b6fSKrzysztof Kozlowski 				info->regs->read_time_udr_mask,
377*8ae83b6fSKrzysztof Kozlowski 				info->regs->read_time_udr_mask);
3780c5deb1eSKrzysztof Kozlowski 		if (ret) {
3790c5deb1eSKrzysztof Kozlowski 			dev_err(dev,
3800c5deb1eSKrzysztof Kozlowski 				"Failed to prepare registers for time reading: %d\n",
3810c5deb1eSKrzysztof Kozlowski 				ret);
3820c5deb1eSKrzysztof Kozlowski 			return ret;
3830c5deb1eSKrzysztof Kozlowski 		}
3840c5deb1eSKrzysztof Kozlowski 	}
385f8b23bbdSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, info->regs->time, data,
386f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
3875bccae6eSSangbeom Kim 	if (ret < 0)
3885bccae6eSSangbeom Kim 		return ret;
3895bccae6eSSangbeom Kim 
3905bccae6eSSangbeom Kim 	switch (info->device_type) {
3915bccae6eSSangbeom Kim 	case S5M8763X:
3925bccae6eSSangbeom Kim 		s5m8763_data_to_tm(data, tm);
3935bccae6eSSangbeom Kim 		break;
3945bccae6eSSangbeom Kim 
3955bccae6eSSangbeom Kim 	case S5M8767X:
396a65e5efaSAlim Akhtar 	case S2MPS15X:
3970c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
3985281f94aSKrzysztof Kozlowski 	case S2MPS13X:
3995bccae6eSSangbeom Kim 		s5m8767_data_to_tm(data, tm, info->rtc_24hr_mode);
4005bccae6eSSangbeom Kim 		break;
4015bccae6eSSangbeom Kim 
4025bccae6eSSangbeom Kim 	default:
4035bccae6eSSangbeom Kim 		return -EINVAL;
4045bccae6eSSangbeom Kim 	}
4055bccae6eSSangbeom Kim 
4065bccae6eSSangbeom Kim 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
4075bccae6eSSangbeom Kim 		1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
4085bccae6eSSangbeom Kim 		tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
4095bccae6eSSangbeom Kim 
4105bccae6eSSangbeom Kim 	return rtc_valid_tm(tm);
4115bccae6eSSangbeom Kim }
4125bccae6eSSangbeom Kim 
4135bccae6eSSangbeom Kim static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm)
4145bccae6eSSangbeom Kim {
4155bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
416f8b23bbdSKrzysztof Kozlowski 	u8 data[info->regs->regs_count];
4175bccae6eSSangbeom Kim 	int ret = 0;
4185bccae6eSSangbeom Kim 
4195bccae6eSSangbeom Kim 	switch (info->device_type) {
4205bccae6eSSangbeom Kim 	case S5M8763X:
4215bccae6eSSangbeom Kim 		s5m8763_tm_to_data(tm, data);
4225bccae6eSSangbeom Kim 		break;
4235bccae6eSSangbeom Kim 	case S5M8767X:
424a65e5efaSAlim Akhtar 	case S2MPS15X:
4250c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
4265281f94aSKrzysztof Kozlowski 	case S2MPS13X:
4275bccae6eSSangbeom Kim 		ret = s5m8767_tm_to_data(tm, data);
4285bccae6eSSangbeom Kim 		break;
4295bccae6eSSangbeom Kim 	default:
4305bccae6eSSangbeom Kim 		return -EINVAL;
4315bccae6eSSangbeom Kim 	}
4325bccae6eSSangbeom Kim 
4335bccae6eSSangbeom Kim 	if (ret < 0)
4345bccae6eSSangbeom Kim 		return ret;
4355bccae6eSSangbeom Kim 
4365bccae6eSSangbeom Kim 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
4375bccae6eSSangbeom Kim 		1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
4385bccae6eSSangbeom Kim 		tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
4395bccae6eSSangbeom Kim 
440f8b23bbdSKrzysztof Kozlowski 	ret = regmap_raw_write(info->regmap, info->regs->time, data,
441f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
4425bccae6eSSangbeom Kim 	if (ret < 0)
4435bccae6eSSangbeom Kim 		return ret;
4445bccae6eSSangbeom Kim 
4455bccae6eSSangbeom Kim 	ret = s5m8767_rtc_set_time_reg(info);
4465bccae6eSSangbeom Kim 
4475bccae6eSSangbeom Kim 	return ret;
4485bccae6eSSangbeom Kim }
4495bccae6eSSangbeom Kim 
4505bccae6eSSangbeom Kim static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
4515bccae6eSSangbeom Kim {
4525bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
453f8b23bbdSKrzysztof Kozlowski 	u8 data[info->regs->regs_count];
4545bccae6eSSangbeom Kim 	unsigned int val;
4555bccae6eSSangbeom Kim 	int ret, i;
4565bccae6eSSangbeom Kim 
457f8b23bbdSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
458f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
4595bccae6eSSangbeom Kim 	if (ret < 0)
4605bccae6eSSangbeom Kim 		return ret;
4615bccae6eSSangbeom Kim 
4625bccae6eSSangbeom Kim 	switch (info->device_type) {
4635bccae6eSSangbeom Kim 	case S5M8763X:
4645bccae6eSSangbeom Kim 		s5m8763_data_to_tm(data, &alrm->time);
465602cb5bbSKrzysztof Kozlowski 		ret = regmap_read(info->regmap, S5M_ALARM0_CONF, &val);
4665bccae6eSSangbeom Kim 		if (ret < 0)
4675bccae6eSSangbeom Kim 			return ret;
4685bccae6eSSangbeom Kim 
4695bccae6eSSangbeom Kim 		alrm->enabled = !!val;
4705bccae6eSSangbeom Kim 		break;
4715bccae6eSSangbeom Kim 
4725bccae6eSSangbeom Kim 	case S5M8767X:
473a65e5efaSAlim Akhtar 	case S2MPS15X:
4740c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
4755281f94aSKrzysztof Kozlowski 	case S2MPS13X:
4765bccae6eSSangbeom Kim 		s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
4775bccae6eSSangbeom Kim 		alrm->enabled = 0;
478f8b23bbdSKrzysztof Kozlowski 		for (i = 0; i < info->regs->regs_count; i++) {
4795bccae6eSSangbeom Kim 			if (data[i] & ALARM_ENABLE_MASK) {
4805bccae6eSSangbeom Kim 				alrm->enabled = 1;
4815bccae6eSSangbeom Kim 				break;
4825bccae6eSSangbeom Kim 			}
4835bccae6eSSangbeom Kim 		}
4845bccae6eSSangbeom Kim 		break;
4855bccae6eSSangbeom Kim 
4865bccae6eSSangbeom Kim 	default:
4875bccae6eSSangbeom Kim 		return -EINVAL;
4885bccae6eSSangbeom Kim 	}
4895bccae6eSSangbeom Kim 
490f8b23bbdSKrzysztof Kozlowski 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
491f8b23bbdSKrzysztof Kozlowski 		1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
492f8b23bbdSKrzysztof Kozlowski 		alrm->time.tm_mday, alrm->time.tm_hour,
493f8b23bbdSKrzysztof Kozlowski 		alrm->time.tm_min, alrm->time.tm_sec,
494f8b23bbdSKrzysztof Kozlowski 		alrm->time.tm_wday);
495f8b23bbdSKrzysztof Kozlowski 
496f8b23bbdSKrzysztof Kozlowski 	ret = s5m_check_peding_alarm_interrupt(info, alrm);
4975bccae6eSSangbeom Kim 
4985bccae6eSSangbeom Kim 	return 0;
4995bccae6eSSangbeom Kim }
5005bccae6eSSangbeom Kim 
5015bccae6eSSangbeom Kim static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info)
5025bccae6eSSangbeom Kim {
503f8b23bbdSKrzysztof Kozlowski 	u8 data[info->regs->regs_count];
5045bccae6eSSangbeom Kim 	int ret, i;
5055bccae6eSSangbeom Kim 	struct rtc_time tm;
5065bccae6eSSangbeom Kim 
507f8b23bbdSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
508f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
5095bccae6eSSangbeom Kim 	if (ret < 0)
5105bccae6eSSangbeom Kim 		return ret;
5115bccae6eSSangbeom Kim 
5125bccae6eSSangbeom Kim 	s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
5135bccae6eSSangbeom Kim 	dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
5145bccae6eSSangbeom Kim 		1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
5155bccae6eSSangbeom Kim 		tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday);
5165bccae6eSSangbeom Kim 
5175bccae6eSSangbeom Kim 	switch (info->device_type) {
5185bccae6eSSangbeom Kim 	case S5M8763X:
519602cb5bbSKrzysztof Kozlowski 		ret = regmap_write(info->regmap, S5M_ALARM0_CONF, 0);
5205bccae6eSSangbeom Kim 		break;
5215bccae6eSSangbeom Kim 
5225bccae6eSSangbeom Kim 	case S5M8767X:
523a65e5efaSAlim Akhtar 	case S2MPS15X:
5240c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
5255281f94aSKrzysztof Kozlowski 	case S2MPS13X:
526f8b23bbdSKrzysztof Kozlowski 		for (i = 0; i < info->regs->regs_count; i++)
5275bccae6eSSangbeom Kim 			data[i] &= ~ALARM_ENABLE_MASK;
5285bccae6eSSangbeom Kim 
529f8b23bbdSKrzysztof Kozlowski 		ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
530f8b23bbdSKrzysztof Kozlowski 				info->regs->regs_count);
5315bccae6eSSangbeom Kim 		if (ret < 0)
5325bccae6eSSangbeom Kim 			return ret;
5335bccae6eSSangbeom Kim 
5345bccae6eSSangbeom Kim 		ret = s5m8767_rtc_set_alarm_reg(info);
5355bccae6eSSangbeom Kim 
5365bccae6eSSangbeom Kim 		break;
5375bccae6eSSangbeom Kim 
5385bccae6eSSangbeom Kim 	default:
5395bccae6eSSangbeom Kim 		return -EINVAL;
5405bccae6eSSangbeom Kim 	}
5415bccae6eSSangbeom Kim 
5425bccae6eSSangbeom Kim 	return ret;
5435bccae6eSSangbeom Kim }
5445bccae6eSSangbeom Kim 
5455bccae6eSSangbeom Kim static int s5m_rtc_start_alarm(struct s5m_rtc_info *info)
5465bccae6eSSangbeom Kim {
5475bccae6eSSangbeom Kim 	int ret;
548f8b23bbdSKrzysztof Kozlowski 	u8 data[info->regs->regs_count];
5495bccae6eSSangbeom Kim 	u8 alarm0_conf;
5505bccae6eSSangbeom Kim 	struct rtc_time tm;
5515bccae6eSSangbeom Kim 
552f8b23bbdSKrzysztof Kozlowski 	ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
553f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
5545bccae6eSSangbeom Kim 	if (ret < 0)
5555bccae6eSSangbeom Kim 		return ret;
5565bccae6eSSangbeom Kim 
5575bccae6eSSangbeom Kim 	s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
5585bccae6eSSangbeom Kim 	dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
5595bccae6eSSangbeom Kim 		1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
5605bccae6eSSangbeom Kim 		tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday);
5615bccae6eSSangbeom Kim 
5625bccae6eSSangbeom Kim 	switch (info->device_type) {
5635bccae6eSSangbeom Kim 	case S5M8763X:
5645bccae6eSSangbeom Kim 		alarm0_conf = 0x77;
565602cb5bbSKrzysztof Kozlowski 		ret = regmap_write(info->regmap, S5M_ALARM0_CONF, alarm0_conf);
5665bccae6eSSangbeom Kim 		break;
5675bccae6eSSangbeom Kim 
5685bccae6eSSangbeom Kim 	case S5M8767X:
569a65e5efaSAlim Akhtar 	case S2MPS15X:
5700c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
5715281f94aSKrzysztof Kozlowski 	case S2MPS13X:
5725bccae6eSSangbeom Kim 		data[RTC_SEC] |= ALARM_ENABLE_MASK;
5735bccae6eSSangbeom Kim 		data[RTC_MIN] |= ALARM_ENABLE_MASK;
5745bccae6eSSangbeom Kim 		data[RTC_HOUR] |= ALARM_ENABLE_MASK;
5755bccae6eSSangbeom Kim 		data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
5765bccae6eSSangbeom Kim 		if (data[RTC_DATE] & 0x1f)
5775bccae6eSSangbeom Kim 			data[RTC_DATE] |= ALARM_ENABLE_MASK;
5785bccae6eSSangbeom Kim 		if (data[RTC_MONTH] & 0xf)
5795bccae6eSSangbeom Kim 			data[RTC_MONTH] |= ALARM_ENABLE_MASK;
5805bccae6eSSangbeom Kim 		if (data[RTC_YEAR1] & 0x7f)
5815bccae6eSSangbeom Kim 			data[RTC_YEAR1] |= ALARM_ENABLE_MASK;
5825bccae6eSSangbeom Kim 
583f8b23bbdSKrzysztof Kozlowski 		ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
584f8b23bbdSKrzysztof Kozlowski 				info->regs->regs_count);
5855bccae6eSSangbeom Kim 		if (ret < 0)
5865bccae6eSSangbeom Kim 			return ret;
5875bccae6eSSangbeom Kim 		ret = s5m8767_rtc_set_alarm_reg(info);
5885bccae6eSSangbeom Kim 
5895bccae6eSSangbeom Kim 		break;
5905bccae6eSSangbeom Kim 
5915bccae6eSSangbeom Kim 	default:
5925bccae6eSSangbeom Kim 		return -EINVAL;
5935bccae6eSSangbeom Kim 	}
5945bccae6eSSangbeom Kim 
5955bccae6eSSangbeom Kim 	return ret;
5965bccae6eSSangbeom Kim }
5975bccae6eSSangbeom Kim 
5985bccae6eSSangbeom Kim static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
5995bccae6eSSangbeom Kim {
6005bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
601f8b23bbdSKrzysztof Kozlowski 	u8 data[info->regs->regs_count];
6025bccae6eSSangbeom Kim 	int ret;
6035bccae6eSSangbeom Kim 
6045bccae6eSSangbeom Kim 	switch (info->device_type) {
6055bccae6eSSangbeom Kim 	case S5M8763X:
6065bccae6eSSangbeom Kim 		s5m8763_tm_to_data(&alrm->time, data);
6075bccae6eSSangbeom Kim 		break;
6085bccae6eSSangbeom Kim 
6095bccae6eSSangbeom Kim 	case S5M8767X:
610a65e5efaSAlim Akhtar 	case S2MPS15X:
6110c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
6125281f94aSKrzysztof Kozlowski 	case S2MPS13X:
6135bccae6eSSangbeom Kim 		s5m8767_tm_to_data(&alrm->time, data);
6145bccae6eSSangbeom Kim 		break;
6155bccae6eSSangbeom Kim 
6165bccae6eSSangbeom Kim 	default:
6175bccae6eSSangbeom Kim 		return -EINVAL;
6185bccae6eSSangbeom Kim 	}
6195bccae6eSSangbeom Kim 
6205bccae6eSSangbeom Kim 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
6215bccae6eSSangbeom Kim 		1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
6225bccae6eSSangbeom Kim 		alrm->time.tm_mday, alrm->time.tm_hour, alrm->time.tm_min,
6235bccae6eSSangbeom Kim 		alrm->time.tm_sec, alrm->time.tm_wday);
6245bccae6eSSangbeom Kim 
6255bccae6eSSangbeom Kim 	ret = s5m_rtc_stop_alarm(info);
6265bccae6eSSangbeom Kim 	if (ret < 0)
6275bccae6eSSangbeom Kim 		return ret;
6285bccae6eSSangbeom Kim 
629f8b23bbdSKrzysztof Kozlowski 	ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
630f8b23bbdSKrzysztof Kozlowski 			info->regs->regs_count);
6315bccae6eSSangbeom Kim 	if (ret < 0)
6325bccae6eSSangbeom Kim 		return ret;
6335bccae6eSSangbeom Kim 
6345bccae6eSSangbeom Kim 	ret = s5m8767_rtc_set_alarm_reg(info);
6355bccae6eSSangbeom Kim 	if (ret < 0)
6365bccae6eSSangbeom Kim 		return ret;
6375bccae6eSSangbeom Kim 
6385bccae6eSSangbeom Kim 	if (alrm->enabled)
6395bccae6eSSangbeom Kim 		ret = s5m_rtc_start_alarm(info);
6405bccae6eSSangbeom Kim 
6415bccae6eSSangbeom Kim 	return ret;
6425bccae6eSSangbeom Kim }
6435bccae6eSSangbeom Kim 
6445bccae6eSSangbeom Kim static int s5m_rtc_alarm_irq_enable(struct device *dev,
6455bccae6eSSangbeom Kim 				    unsigned int enabled)
6465bccae6eSSangbeom Kim {
6475bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
6485bccae6eSSangbeom Kim 
6495bccae6eSSangbeom Kim 	if (enabled)
6505bccae6eSSangbeom Kim 		return s5m_rtc_start_alarm(info);
6515bccae6eSSangbeom Kim 	else
6525bccae6eSSangbeom Kim 		return s5m_rtc_stop_alarm(info);
6535bccae6eSSangbeom Kim }
6545bccae6eSSangbeom Kim 
6555bccae6eSSangbeom Kim static irqreturn_t s5m_rtc_alarm_irq(int irq, void *data)
6565bccae6eSSangbeom Kim {
6575bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = data;
6585bccae6eSSangbeom Kim 
6595bccae6eSSangbeom Kim 	rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
6605bccae6eSSangbeom Kim 
6615bccae6eSSangbeom Kim 	return IRQ_HANDLED;
6625bccae6eSSangbeom Kim }
6635bccae6eSSangbeom Kim 
6645bccae6eSSangbeom Kim static const struct rtc_class_ops s5m_rtc_ops = {
6655bccae6eSSangbeom Kim 	.read_time = s5m_rtc_read_time,
6665bccae6eSSangbeom Kim 	.set_time = s5m_rtc_set_time,
6675bccae6eSSangbeom Kim 	.read_alarm = s5m_rtc_read_alarm,
6685bccae6eSSangbeom Kim 	.set_alarm = s5m_rtc_set_alarm,
6695bccae6eSSangbeom Kim 	.alarm_irq_enable = s5m_rtc_alarm_irq_enable,
6705bccae6eSSangbeom Kim };
6715bccae6eSSangbeom Kim 
6725bccae6eSSangbeom Kim static int s5m8767_rtc_init_reg(struct s5m_rtc_info *info)
6735bccae6eSSangbeom Kim {
6745bccae6eSSangbeom Kim 	u8 data[2];
6755bccae6eSSangbeom Kim 	int ret;
6765bccae6eSSangbeom Kim 
6770c5deb1eSKrzysztof Kozlowski 	switch (info->device_type) {
6780c5deb1eSKrzysztof Kozlowski 	case S5M8763X:
6790c5deb1eSKrzysztof Kozlowski 	case S5M8767X:
6800c5f5d9aSKrzysztof Kozlowski 		/* UDR update time. Default of 7.32 ms is too long. */
6810c5f5d9aSKrzysztof Kozlowski 		ret = regmap_update_bits(info->regmap, S5M_RTC_UDR_CON,
6820c5f5d9aSKrzysztof Kozlowski 				S5M_RTC_UDR_T_MASK, S5M_RTC_UDR_T_450_US);
6830c5f5d9aSKrzysztof Kozlowski 		if (ret < 0)
6840c5f5d9aSKrzysztof Kozlowski 			dev_err(info->dev, "%s: fail to change UDR time: %d\n",
6850c5f5d9aSKrzysztof Kozlowski 					__func__, ret);
6860c5f5d9aSKrzysztof Kozlowski 
6875bccae6eSSangbeom Kim 		/* Set RTC control register : Binary mode, 24hour mode */
6885bccae6eSSangbeom Kim 		data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
6895bccae6eSSangbeom Kim 		data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
6905bccae6eSSangbeom Kim 
691602cb5bbSKrzysztof Kozlowski 		ret = regmap_raw_write(info->regmap, S5M_ALARM0_CONF, data, 2);
6920c5deb1eSKrzysztof Kozlowski 		break;
6930c5deb1eSKrzysztof Kozlowski 
694a65e5efaSAlim Akhtar 	case S2MPS15X:
6950c5deb1eSKrzysztof Kozlowski 	case S2MPS14X:
6965281f94aSKrzysztof Kozlowski 	case S2MPS13X:
6970c5deb1eSKrzysztof Kozlowski 		data[0] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
6980c5deb1eSKrzysztof Kozlowski 		ret = regmap_write(info->regmap, info->regs->ctrl, data[0]);
699ff02c044SJoonyoung Shim 		if (ret < 0)
700ff02c044SJoonyoung Shim 			break;
701ff02c044SJoonyoung Shim 
702ff02c044SJoonyoung Shim 		/*
703ff02c044SJoonyoung Shim 		 * Should set WUDR & (RUDR or AUDR) bits to high after writing
704ff02c044SJoonyoung Shim 		 * RTC_CTRL register like writing Alarm registers. We can't find
705ff02c044SJoonyoung Shim 		 * the description from datasheet but vendor code does that
706ff02c044SJoonyoung Shim 		 * really.
707ff02c044SJoonyoung Shim 		 */
708ff02c044SJoonyoung Shim 		ret = s5m8767_rtc_set_alarm_reg(info);
7090c5deb1eSKrzysztof Kozlowski 		break;
7100c5deb1eSKrzysztof Kozlowski 
7110c5deb1eSKrzysztof Kozlowski 	default:
7120c5deb1eSKrzysztof Kozlowski 		return -EINVAL;
7130c5deb1eSKrzysztof Kozlowski 	}
7140c5deb1eSKrzysztof Kozlowski 
7150c5deb1eSKrzysztof Kozlowski 	info->rtc_24hr_mode = 1;
7165bccae6eSSangbeom Kim 	if (ret < 0) {
7175bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
7185bccae6eSSangbeom Kim 			__func__, ret);
7195bccae6eSSangbeom Kim 		return ret;
7205bccae6eSSangbeom Kim 	}
7215bccae6eSSangbeom Kim 
7225bccae6eSSangbeom Kim 	return ret;
7235bccae6eSSangbeom Kim }
7245bccae6eSSangbeom Kim 
7255bccae6eSSangbeom Kim static int s5m_rtc_probe(struct platform_device *pdev)
7265bccae6eSSangbeom Kim {
7275bccae6eSSangbeom Kim 	struct sec_pmic_dev *s5m87xx = dev_get_drvdata(pdev->dev.parent);
7285bccae6eSSangbeom Kim 	struct sec_platform_data *pdata = s5m87xx->pdata;
7295bccae6eSSangbeom Kim 	struct s5m_rtc_info *info;
730e349c910SKrzysztof Kozlowski 	const struct regmap_config *regmap_cfg;
731a0347f20SKrzysztof Kozlowski 	int ret, alarm_irq;
7325bccae6eSSangbeom Kim 
7335bccae6eSSangbeom Kim 	if (!pdata) {
7345bccae6eSSangbeom Kim 		dev_err(pdev->dev.parent, "Platform data not supplied\n");
7355bccae6eSSangbeom Kim 		return -ENODEV;
7365bccae6eSSangbeom Kim 	}
7375bccae6eSSangbeom Kim 
7385bccae6eSSangbeom Kim 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
7395bccae6eSSangbeom Kim 	if (!info)
7405bccae6eSSangbeom Kim 		return -ENOMEM;
7415bccae6eSSangbeom Kim 
74294f91922SKrzysztof Kozlowski 	switch (platform_get_device_id(pdev)->driver_data) {
743a65e5efaSAlim Akhtar 	case S2MPS15X:
744*8ae83b6fSKrzysztof Kozlowski 		regmap_cfg = &s2mps14_rtc_regmap_config;
745*8ae83b6fSKrzysztof Kozlowski 		info->regs = &s2mps15_rtc_regs;
746*8ae83b6fSKrzysztof Kozlowski 		alarm_irq = S2MPS14_IRQ_RTCA0;
747*8ae83b6fSKrzysztof Kozlowski 		break;
748e349c910SKrzysztof Kozlowski 	case S2MPS14X:
749*8ae83b6fSKrzysztof Kozlowski 		regmap_cfg = &s2mps14_rtc_regmap_config;
750*8ae83b6fSKrzysztof Kozlowski 		info->regs = &s2mps14_rtc_regs;
751*8ae83b6fSKrzysztof Kozlowski 		alarm_irq = S2MPS14_IRQ_RTCA0;
752*8ae83b6fSKrzysztof Kozlowski 		break;
7535281f94aSKrzysztof Kozlowski 	case S2MPS13X:
754e349c910SKrzysztof Kozlowski 		regmap_cfg = &s2mps14_rtc_regmap_config;
755*8ae83b6fSKrzysztof Kozlowski 		info->regs = &s2mps13_rtc_regs;
756a0347f20SKrzysztof Kozlowski 		alarm_irq = S2MPS14_IRQ_RTCA0;
757e349c910SKrzysztof Kozlowski 		break;
758e349c910SKrzysztof Kozlowski 	case S5M8763X:
759e349c910SKrzysztof Kozlowski 		regmap_cfg = &s5m_rtc_regmap_config;
760f8b23bbdSKrzysztof Kozlowski 		info->regs = &s5m_rtc_regs;
761a0347f20SKrzysztof Kozlowski 		alarm_irq = S5M8763_IRQ_ALARM0;
762e349c910SKrzysztof Kozlowski 		break;
763e349c910SKrzysztof Kozlowski 	case S5M8767X:
764e349c910SKrzysztof Kozlowski 		regmap_cfg = &s5m_rtc_regmap_config;
765f8b23bbdSKrzysztof Kozlowski 		info->regs = &s5m_rtc_regs;
766a0347f20SKrzysztof Kozlowski 		alarm_irq = S5M8767_IRQ_RTCA1;
767e349c910SKrzysztof Kozlowski 		break;
768e349c910SKrzysztof Kozlowski 	default:
76994f91922SKrzysztof Kozlowski 		dev_err(&pdev->dev,
77094f91922SKrzysztof Kozlowski 				"Device type %lu is not supported by RTC driver\n",
77194f91922SKrzysztof Kozlowski 				platform_get_device_id(pdev)->driver_data);
772e349c910SKrzysztof Kozlowski 		return -ENODEV;
773e349c910SKrzysztof Kozlowski 	}
774e349c910SKrzysztof Kozlowski 
775e349c910SKrzysztof Kozlowski 	info->i2c = i2c_new_dummy(s5m87xx->i2c->adapter, RTC_I2C_ADDR);
776e349c910SKrzysztof Kozlowski 	if (!info->i2c) {
777e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Failed to allocate I2C for RTC\n");
778e349c910SKrzysztof Kozlowski 		return -ENODEV;
779e349c910SKrzysztof Kozlowski 	}
780e349c910SKrzysztof Kozlowski 
781e349c910SKrzysztof Kozlowski 	info->regmap = devm_regmap_init_i2c(info->i2c, regmap_cfg);
782e349c910SKrzysztof Kozlowski 	if (IS_ERR(info->regmap)) {
783e349c910SKrzysztof Kozlowski 		ret = PTR_ERR(info->regmap);
784e349c910SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Failed to allocate RTC register map: %d\n",
785e349c910SKrzysztof Kozlowski 				ret);
786e349c910SKrzysztof Kozlowski 		goto err;
787e349c910SKrzysztof Kozlowski 	}
788e349c910SKrzysztof Kozlowski 
7895bccae6eSSangbeom Kim 	info->dev = &pdev->dev;
7905bccae6eSSangbeom Kim 	info->s5m87xx = s5m87xx;
79194f91922SKrzysztof Kozlowski 	info->device_type = platform_get_device_id(pdev)->driver_data;
7925bccae6eSSangbeom Kim 
793b7d5b9a9SBartlomiej Zolnierkiewicz 	if (s5m87xx->irq_data) {
794a0347f20SKrzysztof Kozlowski 		info->irq = regmap_irq_get_virq(s5m87xx->irq_data, alarm_irq);
795a0347f20SKrzysztof Kozlowski 		if (info->irq <= 0) {
7965bccae6eSSangbeom Kim 			ret = -EINVAL;
797a0347f20SKrzysztof Kozlowski 			dev_err(&pdev->dev, "Failed to get virtual IRQ %d\n",
798a0347f20SKrzysztof Kozlowski 				alarm_irq);
799e349c910SKrzysztof Kozlowski 			goto err;
8005bccae6eSSangbeom Kim 		}
801b7d5b9a9SBartlomiej Zolnierkiewicz 	}
8025bccae6eSSangbeom Kim 
8035bccae6eSSangbeom Kim 	platform_set_drvdata(pdev, info);
8045bccae6eSSangbeom Kim 
8055bccae6eSSangbeom Kim 	ret = s5m8767_rtc_init_reg(info);
8065bccae6eSSangbeom Kim 
8075bccae6eSSangbeom Kim 	device_init_wakeup(&pdev->dev, 1);
8085bccae6eSSangbeom Kim 
8095bccae6eSSangbeom Kim 	info->rtc_dev = devm_rtc_device_register(&pdev->dev, "s5m-rtc",
8105bccae6eSSangbeom Kim 						 &s5m_rtc_ops, THIS_MODULE);
8115bccae6eSSangbeom Kim 
812e349c910SKrzysztof Kozlowski 	if (IS_ERR(info->rtc_dev)) {
813e349c910SKrzysztof Kozlowski 		ret = PTR_ERR(info->rtc_dev);
814e349c910SKrzysztof Kozlowski 		goto err;
815e349c910SKrzysztof Kozlowski 	}
8165bccae6eSSangbeom Kim 
817b7d5b9a9SBartlomiej Zolnierkiewicz 	if (!info->irq) {
818b7d5b9a9SBartlomiej Zolnierkiewicz 		dev_info(&pdev->dev, "Alarm IRQ not available\n");
819b7d5b9a9SBartlomiej Zolnierkiewicz 		return 0;
820b7d5b9a9SBartlomiej Zolnierkiewicz 	}
821b7d5b9a9SBartlomiej Zolnierkiewicz 
8225bccae6eSSangbeom Kim 	ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
8235bccae6eSSangbeom Kim 					s5m_rtc_alarm_irq, 0, "rtc-alarm0",
8245bccae6eSSangbeom Kim 					info);
825e349c910SKrzysztof Kozlowski 	if (ret < 0) {
8265bccae6eSSangbeom Kim 		dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
8275bccae6eSSangbeom Kim 			info->irq, ret);
828e349c910SKrzysztof Kozlowski 		goto err;
829e349c910SKrzysztof Kozlowski 	}
830e349c910SKrzysztof Kozlowski 
831e349c910SKrzysztof Kozlowski 	return 0;
832e349c910SKrzysztof Kozlowski 
833e349c910SKrzysztof Kozlowski err:
834e349c910SKrzysztof Kozlowski 	i2c_unregister_device(info->i2c);
8355bccae6eSSangbeom Kim 
8365bccae6eSSangbeom Kim 	return ret;
8375bccae6eSSangbeom Kim }
8385bccae6eSSangbeom Kim 
839e349c910SKrzysztof Kozlowski static int s5m_rtc_remove(struct platform_device *pdev)
840e349c910SKrzysztof Kozlowski {
841e349c910SKrzysztof Kozlowski 	struct s5m_rtc_info *info = platform_get_drvdata(pdev);
842e349c910SKrzysztof Kozlowski 
843e349c910SKrzysztof Kozlowski 	i2c_unregister_device(info->i2c);
844e349c910SKrzysztof Kozlowski 
845e349c910SKrzysztof Kozlowski 	return 0;
846e349c910SKrzysztof Kozlowski }
847e349c910SKrzysztof Kozlowski 
84811ba5a1eSGeert Uytterhoeven #ifdef CONFIG_PM_SLEEP
849222ead7fSKrzysztof Kozlowski static int s5m_rtc_resume(struct device *dev)
850222ead7fSKrzysztof Kozlowski {
851222ead7fSKrzysztof Kozlowski 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
852222ead7fSKrzysztof Kozlowski 	int ret = 0;
853222ead7fSKrzysztof Kozlowski 
854b7d5b9a9SBartlomiej Zolnierkiewicz 	if (info->irq && device_may_wakeup(dev))
855222ead7fSKrzysztof Kozlowski 		ret = disable_irq_wake(info->irq);
856222ead7fSKrzysztof Kozlowski 
857222ead7fSKrzysztof Kozlowski 	return ret;
858222ead7fSKrzysztof Kozlowski }
859222ead7fSKrzysztof Kozlowski 
860222ead7fSKrzysztof Kozlowski static int s5m_rtc_suspend(struct device *dev)
861222ead7fSKrzysztof Kozlowski {
862222ead7fSKrzysztof Kozlowski 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
863222ead7fSKrzysztof Kozlowski 	int ret = 0;
864222ead7fSKrzysztof Kozlowski 
865b7d5b9a9SBartlomiej Zolnierkiewicz 	if (info->irq && device_may_wakeup(dev))
866222ead7fSKrzysztof Kozlowski 		ret = enable_irq_wake(info->irq);
867222ead7fSKrzysztof Kozlowski 
868222ead7fSKrzysztof Kozlowski 	return ret;
869222ead7fSKrzysztof Kozlowski }
87011ba5a1eSGeert Uytterhoeven #endif /* CONFIG_PM_SLEEP */
871222ead7fSKrzysztof Kozlowski 
872222ead7fSKrzysztof Kozlowski static SIMPLE_DEV_PM_OPS(s5m_rtc_pm_ops, s5m_rtc_suspend, s5m_rtc_resume);
873222ead7fSKrzysztof Kozlowski 
8745bccae6eSSangbeom Kim static const struct platform_device_id s5m_rtc_id[] = {
8750c5deb1eSKrzysztof Kozlowski 	{ "s5m-rtc",		S5M8767X },
8765281f94aSKrzysztof Kozlowski 	{ "s2mps13-rtc",	S2MPS13X },
8770c5deb1eSKrzysztof Kozlowski 	{ "s2mps14-rtc",	S2MPS14X },
878a65e5efaSAlim Akhtar 	{ "s2mps15-rtc",	S2MPS15X },
87945cd15e6SAndrey Ryabinin 	{ },
8805bccae6eSSangbeom Kim };
88163074cc3SJavier Martinez Canillas MODULE_DEVICE_TABLE(platform, s5m_rtc_id);
8825bccae6eSSangbeom Kim 
8835bccae6eSSangbeom Kim static struct platform_driver s5m_rtc_driver = {
8845bccae6eSSangbeom Kim 	.driver		= {
8855bccae6eSSangbeom Kim 		.name	= "s5m-rtc",
886222ead7fSKrzysztof Kozlowski 		.pm	= &s5m_rtc_pm_ops,
8875bccae6eSSangbeom Kim 	},
8885bccae6eSSangbeom Kim 	.probe		= s5m_rtc_probe,
889e349c910SKrzysztof Kozlowski 	.remove		= s5m_rtc_remove,
8905bccae6eSSangbeom Kim 	.id_table	= s5m_rtc_id,
8915bccae6eSSangbeom Kim };
8925bccae6eSSangbeom Kim 
8935bccae6eSSangbeom Kim module_platform_driver(s5m_rtc_driver);
8945bccae6eSSangbeom Kim 
8955bccae6eSSangbeom Kim /* Module information */
8965bccae6eSSangbeom Kim MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
8970c5deb1eSKrzysztof Kozlowski MODULE_DESCRIPTION("Samsung S5M/S2MPS14 RTC driver");
8985bccae6eSSangbeom Kim MODULE_LICENSE("GPL");
8995bccae6eSSangbeom Kim MODULE_ALIAS("platform:s5m-rtc");
900