xref: /linux/drivers/rtc/rtc-s5m.c (revision 5ccb7d718e1551ed672b7e2e39fb626dc869594b)
15bccae6eSSangbeom Kim /*
25bccae6eSSangbeom Kim  * Copyright (c) 2013 Samsung Electronics Co., Ltd
35bccae6eSSangbeom Kim  *	http://www.samsung.com
45bccae6eSSangbeom Kim  *
55bccae6eSSangbeom Kim  *  Copyright (C) 2013 Google, Inc
65bccae6eSSangbeom Kim  *
75bccae6eSSangbeom Kim  *  This program is free software; you can redistribute it and/or modify
85bccae6eSSangbeom Kim  *  it under the terms of the GNU General Public License as published by
95bccae6eSSangbeom Kim  *  the Free Software Foundation; either version 2 of the License, or
105bccae6eSSangbeom Kim  *  (at your option) any later version.
115bccae6eSSangbeom Kim  *
125bccae6eSSangbeom Kim  *  This program is distributed in the hope that it will be useful,
135bccae6eSSangbeom Kim  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
145bccae6eSSangbeom Kim  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155bccae6eSSangbeom Kim  *  GNU General Public License for more details.
165bccae6eSSangbeom Kim  */
175bccae6eSSangbeom Kim 
185bccae6eSSangbeom Kim #include <linux/module.h>
195bccae6eSSangbeom Kim #include <linux/i2c.h>
205bccae6eSSangbeom Kim #include <linux/slab.h>
215bccae6eSSangbeom Kim #include <linux/bcd.h>
225bccae6eSSangbeom Kim #include <linux/bitops.h>
235bccae6eSSangbeom Kim #include <linux/regmap.h>
245bccae6eSSangbeom Kim #include <linux/rtc.h>
255bccae6eSSangbeom Kim #include <linux/delay.h>
265bccae6eSSangbeom Kim #include <linux/platform_device.h>
275bccae6eSSangbeom Kim #include <linux/mfd/samsung/core.h>
285bccae6eSSangbeom Kim #include <linux/mfd/samsung/irq.h>
295bccae6eSSangbeom Kim #include <linux/mfd/samsung/rtc.h>
305bccae6eSSangbeom Kim 
315bccae6eSSangbeom Kim struct s5m_rtc_info {
325bccae6eSSangbeom Kim 	struct device *dev;
335bccae6eSSangbeom Kim 	struct sec_pmic_dev *s5m87xx;
34*5ccb7d71SGeert Uytterhoeven 	struct regmap *regmap;
355bccae6eSSangbeom Kim 	struct rtc_device *rtc_dev;
365bccae6eSSangbeom Kim 	int irq;
375bccae6eSSangbeom Kim 	int device_type;
385bccae6eSSangbeom Kim 	int rtc_24hr_mode;
395bccae6eSSangbeom Kim 	bool wtsr_smpl;
405bccae6eSSangbeom Kim };
415bccae6eSSangbeom Kim 
425bccae6eSSangbeom Kim static void s5m8767_data_to_tm(u8 *data, struct rtc_time *tm,
435bccae6eSSangbeom Kim 			       int rtc_24hr_mode)
445bccae6eSSangbeom Kim {
455bccae6eSSangbeom Kim 	tm->tm_sec = data[RTC_SEC] & 0x7f;
465bccae6eSSangbeom Kim 	tm->tm_min = data[RTC_MIN] & 0x7f;
475bccae6eSSangbeom Kim 	if (rtc_24hr_mode) {
485bccae6eSSangbeom Kim 		tm->tm_hour = data[RTC_HOUR] & 0x1f;
495bccae6eSSangbeom Kim 	} else {
505bccae6eSSangbeom Kim 		tm->tm_hour = data[RTC_HOUR] & 0x0f;
515bccae6eSSangbeom Kim 		if (data[RTC_HOUR] & HOUR_PM_MASK)
525bccae6eSSangbeom Kim 			tm->tm_hour += 12;
535bccae6eSSangbeom Kim 	}
545bccae6eSSangbeom Kim 
555bccae6eSSangbeom Kim 	tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f);
565bccae6eSSangbeom Kim 	tm->tm_mday = data[RTC_DATE] & 0x1f;
575bccae6eSSangbeom Kim 	tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
585bccae6eSSangbeom Kim 	tm->tm_year = (data[RTC_YEAR1] & 0x7f) + 100;
595bccae6eSSangbeom Kim 	tm->tm_yday = 0;
605bccae6eSSangbeom Kim 	tm->tm_isdst = 0;
615bccae6eSSangbeom Kim }
625bccae6eSSangbeom Kim 
635bccae6eSSangbeom Kim static int s5m8767_tm_to_data(struct rtc_time *tm, u8 *data)
645bccae6eSSangbeom Kim {
655bccae6eSSangbeom Kim 	data[RTC_SEC] = tm->tm_sec;
665bccae6eSSangbeom Kim 	data[RTC_MIN] = tm->tm_min;
675bccae6eSSangbeom Kim 
685bccae6eSSangbeom Kim 	if (tm->tm_hour >= 12)
695bccae6eSSangbeom Kim 		data[RTC_HOUR] = tm->tm_hour | HOUR_PM_MASK;
705bccae6eSSangbeom Kim 	else
715bccae6eSSangbeom Kim 		data[RTC_HOUR] = tm->tm_hour & ~HOUR_PM_MASK;
725bccae6eSSangbeom Kim 
735bccae6eSSangbeom Kim 	data[RTC_WEEKDAY] = 1 << tm->tm_wday;
745bccae6eSSangbeom Kim 	data[RTC_DATE] = tm->tm_mday;
755bccae6eSSangbeom Kim 	data[RTC_MONTH] = tm->tm_mon + 1;
765bccae6eSSangbeom Kim 	data[RTC_YEAR1] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
775bccae6eSSangbeom Kim 
785bccae6eSSangbeom Kim 	if (tm->tm_year < 100) {
795bccae6eSSangbeom Kim 		pr_err("s5m8767 RTC cannot handle the year %d.\n",
805bccae6eSSangbeom Kim 		       1900 + tm->tm_year);
815bccae6eSSangbeom Kim 		return -EINVAL;
825bccae6eSSangbeom Kim 	} else {
835bccae6eSSangbeom Kim 		return 0;
845bccae6eSSangbeom Kim 	}
855bccae6eSSangbeom Kim }
865bccae6eSSangbeom Kim 
875bccae6eSSangbeom Kim static inline int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info)
885bccae6eSSangbeom Kim {
895bccae6eSSangbeom Kim 	int ret;
905bccae6eSSangbeom Kim 	unsigned int data;
915bccae6eSSangbeom Kim 
92*5ccb7d71SGeert Uytterhoeven 	ret = regmap_read(info->regmap, SEC_RTC_UDR_CON, &data);
935bccae6eSSangbeom Kim 	if (ret < 0) {
945bccae6eSSangbeom Kim 		dev_err(info->dev, "failed to read update reg(%d)\n", ret);
955bccae6eSSangbeom Kim 		return ret;
965bccae6eSSangbeom Kim 	}
975bccae6eSSangbeom Kim 
985bccae6eSSangbeom Kim 	data |= RTC_TIME_EN_MASK;
995bccae6eSSangbeom Kim 	data |= RTC_UDR_MASK;
1005bccae6eSSangbeom Kim 
101*5ccb7d71SGeert Uytterhoeven 	ret = regmap_write(info->regmap, SEC_RTC_UDR_CON, data);
1025bccae6eSSangbeom Kim 	if (ret < 0) {
1035bccae6eSSangbeom Kim 		dev_err(info->dev, "failed to write update reg(%d)\n", ret);
1045bccae6eSSangbeom Kim 		return ret;
1055bccae6eSSangbeom Kim 	}
1065bccae6eSSangbeom Kim 
1075bccae6eSSangbeom Kim 	do {
108*5ccb7d71SGeert Uytterhoeven 		ret = regmap_read(info->regmap, SEC_RTC_UDR_CON, &data);
1095bccae6eSSangbeom Kim 	} while ((data & RTC_UDR_MASK) && !ret);
1105bccae6eSSangbeom Kim 
1115bccae6eSSangbeom Kim 	return ret;
1125bccae6eSSangbeom Kim }
1135bccae6eSSangbeom Kim 
1145bccae6eSSangbeom Kim static inline int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info)
1155bccae6eSSangbeom Kim {
1165bccae6eSSangbeom Kim 	int ret;
1175bccae6eSSangbeom Kim 	unsigned int data;
1185bccae6eSSangbeom Kim 
119*5ccb7d71SGeert Uytterhoeven 	ret = regmap_read(info->regmap, SEC_RTC_UDR_CON, &data);
1205bccae6eSSangbeom Kim 	if (ret < 0) {
1215bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to read update reg(%d)\n",
1225bccae6eSSangbeom Kim 			__func__, ret);
1235bccae6eSSangbeom Kim 		return ret;
1245bccae6eSSangbeom Kim 	}
1255bccae6eSSangbeom Kim 
1265bccae6eSSangbeom Kim 	data &= ~RTC_TIME_EN_MASK;
1275bccae6eSSangbeom Kim 	data |= RTC_UDR_MASK;
1285bccae6eSSangbeom Kim 
129*5ccb7d71SGeert Uytterhoeven 	ret = regmap_write(info->regmap, SEC_RTC_UDR_CON, data);
1305bccae6eSSangbeom Kim 	if (ret < 0) {
1315bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to write update reg(%d)\n",
1325bccae6eSSangbeom Kim 			__func__, ret);
1335bccae6eSSangbeom Kim 		return ret;
1345bccae6eSSangbeom Kim 	}
1355bccae6eSSangbeom Kim 
1365bccae6eSSangbeom Kim 	do {
137*5ccb7d71SGeert Uytterhoeven 		ret = regmap_read(info->regmap, SEC_RTC_UDR_CON, &data);
1385bccae6eSSangbeom Kim 	} while ((data & RTC_UDR_MASK) && !ret);
1395bccae6eSSangbeom Kim 
1405bccae6eSSangbeom Kim 	return ret;
1415bccae6eSSangbeom Kim }
1425bccae6eSSangbeom Kim 
1435bccae6eSSangbeom Kim static void s5m8763_data_to_tm(u8 *data, struct rtc_time *tm)
1445bccae6eSSangbeom Kim {
1455bccae6eSSangbeom Kim 	tm->tm_sec = bcd2bin(data[RTC_SEC]);
1465bccae6eSSangbeom Kim 	tm->tm_min = bcd2bin(data[RTC_MIN]);
1475bccae6eSSangbeom Kim 
1485bccae6eSSangbeom Kim 	if (data[RTC_HOUR] & HOUR_12) {
1495bccae6eSSangbeom Kim 		tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
1505bccae6eSSangbeom Kim 		if (data[RTC_HOUR] & HOUR_PM)
1515bccae6eSSangbeom Kim 			tm->tm_hour += 12;
1525bccae6eSSangbeom Kim 	} else {
1535bccae6eSSangbeom Kim 		tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
1545bccae6eSSangbeom Kim 	}
1555bccae6eSSangbeom Kim 
1565bccae6eSSangbeom Kim 	tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
1575bccae6eSSangbeom Kim 	tm->tm_mday = bcd2bin(data[RTC_DATE]);
1585bccae6eSSangbeom Kim 	tm->tm_mon = bcd2bin(data[RTC_MONTH]);
1595bccae6eSSangbeom Kim 	tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
1605bccae6eSSangbeom Kim 	tm->tm_year -= 1900;
1615bccae6eSSangbeom Kim }
1625bccae6eSSangbeom Kim 
1635bccae6eSSangbeom Kim static void s5m8763_tm_to_data(struct rtc_time *tm, u8 *data)
1645bccae6eSSangbeom Kim {
1655bccae6eSSangbeom Kim 	data[RTC_SEC] = bin2bcd(tm->tm_sec);
1665bccae6eSSangbeom Kim 	data[RTC_MIN] = bin2bcd(tm->tm_min);
1675bccae6eSSangbeom Kim 	data[RTC_HOUR] = bin2bcd(tm->tm_hour);
1685bccae6eSSangbeom Kim 	data[RTC_WEEKDAY] = tm->tm_wday;
1695bccae6eSSangbeom Kim 	data[RTC_DATE] = bin2bcd(tm->tm_mday);
1705bccae6eSSangbeom Kim 	data[RTC_MONTH] = bin2bcd(tm->tm_mon);
1715bccae6eSSangbeom Kim 	data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
1725bccae6eSSangbeom Kim 	data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
1735bccae6eSSangbeom Kim }
1745bccae6eSSangbeom Kim 
1755bccae6eSSangbeom Kim static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm)
1765bccae6eSSangbeom Kim {
1775bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
1785bccae6eSSangbeom Kim 	u8 data[8];
1795bccae6eSSangbeom Kim 	int ret;
1805bccae6eSSangbeom Kim 
181*5ccb7d71SGeert Uytterhoeven 	ret = regmap_bulk_read(info->regmap, SEC_RTC_SEC, data, 8);
1825bccae6eSSangbeom Kim 	if (ret < 0)
1835bccae6eSSangbeom Kim 		return ret;
1845bccae6eSSangbeom Kim 
1855bccae6eSSangbeom Kim 	switch (info->device_type) {
1865bccae6eSSangbeom Kim 	case S5M8763X:
1875bccae6eSSangbeom Kim 		s5m8763_data_to_tm(data, tm);
1885bccae6eSSangbeom Kim 		break;
1895bccae6eSSangbeom Kim 
1905bccae6eSSangbeom Kim 	case S5M8767X:
1915bccae6eSSangbeom Kim 		s5m8767_data_to_tm(data, tm, info->rtc_24hr_mode);
1925bccae6eSSangbeom Kim 		break;
1935bccae6eSSangbeom Kim 
1945bccae6eSSangbeom Kim 	default:
1955bccae6eSSangbeom Kim 		return -EINVAL;
1965bccae6eSSangbeom Kim 	}
1975bccae6eSSangbeom Kim 
1985bccae6eSSangbeom Kim 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
1995bccae6eSSangbeom Kim 		1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
2005bccae6eSSangbeom Kim 		tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
2015bccae6eSSangbeom Kim 
2025bccae6eSSangbeom Kim 	return rtc_valid_tm(tm);
2035bccae6eSSangbeom Kim }
2045bccae6eSSangbeom Kim 
2055bccae6eSSangbeom Kim static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm)
2065bccae6eSSangbeom Kim {
2075bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
2085bccae6eSSangbeom Kim 	u8 data[8];
2095bccae6eSSangbeom Kim 	int ret = 0;
2105bccae6eSSangbeom Kim 
2115bccae6eSSangbeom Kim 	switch (info->device_type) {
2125bccae6eSSangbeom Kim 	case S5M8763X:
2135bccae6eSSangbeom Kim 		s5m8763_tm_to_data(tm, data);
2145bccae6eSSangbeom Kim 		break;
2155bccae6eSSangbeom Kim 	case S5M8767X:
2165bccae6eSSangbeom Kim 		ret = s5m8767_tm_to_data(tm, data);
2175bccae6eSSangbeom Kim 		break;
2185bccae6eSSangbeom Kim 	default:
2195bccae6eSSangbeom Kim 		return -EINVAL;
2205bccae6eSSangbeom Kim 	}
2215bccae6eSSangbeom Kim 
2225bccae6eSSangbeom Kim 	if (ret < 0)
2235bccae6eSSangbeom Kim 		return ret;
2245bccae6eSSangbeom Kim 
2255bccae6eSSangbeom Kim 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
2265bccae6eSSangbeom Kim 		1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
2275bccae6eSSangbeom Kim 		tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
2285bccae6eSSangbeom Kim 
229*5ccb7d71SGeert Uytterhoeven 	ret = regmap_raw_write(info->regmap, SEC_RTC_SEC, data, 8);
2305bccae6eSSangbeom Kim 	if (ret < 0)
2315bccae6eSSangbeom Kim 		return ret;
2325bccae6eSSangbeom Kim 
2335bccae6eSSangbeom Kim 	ret = s5m8767_rtc_set_time_reg(info);
2345bccae6eSSangbeom Kim 
2355bccae6eSSangbeom Kim 	return ret;
2365bccae6eSSangbeom Kim }
2375bccae6eSSangbeom Kim 
2385bccae6eSSangbeom Kim static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
2395bccae6eSSangbeom Kim {
2405bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
2415bccae6eSSangbeom Kim 	u8 data[8];
2425bccae6eSSangbeom Kim 	unsigned int val;
2435bccae6eSSangbeom Kim 	int ret, i;
2445bccae6eSSangbeom Kim 
245*5ccb7d71SGeert Uytterhoeven 	ret = regmap_bulk_read(info->regmap, SEC_ALARM0_SEC, data, 8);
2465bccae6eSSangbeom Kim 	if (ret < 0)
2475bccae6eSSangbeom Kim 		return ret;
2485bccae6eSSangbeom Kim 
2495bccae6eSSangbeom Kim 	switch (info->device_type) {
2505bccae6eSSangbeom Kim 	case S5M8763X:
2515bccae6eSSangbeom Kim 		s5m8763_data_to_tm(data, &alrm->time);
252*5ccb7d71SGeert Uytterhoeven 		ret = regmap_read(info->regmap, SEC_ALARM0_CONF, &val);
2535bccae6eSSangbeom Kim 		if (ret < 0)
2545bccae6eSSangbeom Kim 			return ret;
2555bccae6eSSangbeom Kim 
2565bccae6eSSangbeom Kim 		alrm->enabled = !!val;
2575bccae6eSSangbeom Kim 
258*5ccb7d71SGeert Uytterhoeven 		ret = regmap_read(info->regmap, SEC_RTC_STATUS, &val);
2595bccae6eSSangbeom Kim 		if (ret < 0)
2605bccae6eSSangbeom Kim 			return ret;
2615bccae6eSSangbeom Kim 
2625bccae6eSSangbeom Kim 		break;
2635bccae6eSSangbeom Kim 
2645bccae6eSSangbeom Kim 	case S5M8767X:
2655bccae6eSSangbeom Kim 		s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
2665bccae6eSSangbeom Kim 		dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
2675bccae6eSSangbeom Kim 			1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
2685bccae6eSSangbeom Kim 			alrm->time.tm_mday, alrm->time.tm_hour,
2695bccae6eSSangbeom Kim 			alrm->time.tm_min, alrm->time.tm_sec,
2705bccae6eSSangbeom Kim 			alrm->time.tm_wday);
2715bccae6eSSangbeom Kim 
2725bccae6eSSangbeom Kim 		alrm->enabled = 0;
2735bccae6eSSangbeom Kim 		for (i = 0; i < 7; i++) {
2745bccae6eSSangbeom Kim 			if (data[i] & ALARM_ENABLE_MASK) {
2755bccae6eSSangbeom Kim 				alrm->enabled = 1;
2765bccae6eSSangbeom Kim 				break;
2775bccae6eSSangbeom Kim 			}
2785bccae6eSSangbeom Kim 		}
2795bccae6eSSangbeom Kim 
2805bccae6eSSangbeom Kim 		alrm->pending = 0;
281*5ccb7d71SGeert Uytterhoeven 		ret = regmap_read(info->regmap, SEC_RTC_STATUS, &val);
2825bccae6eSSangbeom Kim 		if (ret < 0)
2835bccae6eSSangbeom Kim 			return ret;
2845bccae6eSSangbeom Kim 		break;
2855bccae6eSSangbeom Kim 
2865bccae6eSSangbeom Kim 	default:
2875bccae6eSSangbeom Kim 		return -EINVAL;
2885bccae6eSSangbeom Kim 	}
2895bccae6eSSangbeom Kim 
2905bccae6eSSangbeom Kim 	if (val & ALARM0_STATUS)
2915bccae6eSSangbeom Kim 		alrm->pending = 1;
2925bccae6eSSangbeom Kim 	else
2935bccae6eSSangbeom Kim 		alrm->pending = 0;
2945bccae6eSSangbeom Kim 
2955bccae6eSSangbeom Kim 	return 0;
2965bccae6eSSangbeom Kim }
2975bccae6eSSangbeom Kim 
2985bccae6eSSangbeom Kim static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info)
2995bccae6eSSangbeom Kim {
3005bccae6eSSangbeom Kim 	u8 data[8];
3015bccae6eSSangbeom Kim 	int ret, i;
3025bccae6eSSangbeom Kim 	struct rtc_time tm;
3035bccae6eSSangbeom Kim 
304*5ccb7d71SGeert Uytterhoeven 	ret = regmap_bulk_read(info->regmap, SEC_ALARM0_SEC, data, 8);
3055bccae6eSSangbeom Kim 	if (ret < 0)
3065bccae6eSSangbeom Kim 		return ret;
3075bccae6eSSangbeom Kim 
3085bccae6eSSangbeom Kim 	s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
3095bccae6eSSangbeom Kim 	dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
3105bccae6eSSangbeom Kim 		1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
3115bccae6eSSangbeom Kim 		tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday);
3125bccae6eSSangbeom Kim 
3135bccae6eSSangbeom Kim 	switch (info->device_type) {
3145bccae6eSSangbeom Kim 	case S5M8763X:
315*5ccb7d71SGeert Uytterhoeven 		ret = regmap_write(info->regmap, SEC_ALARM0_CONF, 0);
3165bccae6eSSangbeom Kim 		break;
3175bccae6eSSangbeom Kim 
3185bccae6eSSangbeom Kim 	case S5M8767X:
3195bccae6eSSangbeom Kim 		for (i = 0; i < 7; i++)
3205bccae6eSSangbeom Kim 			data[i] &= ~ALARM_ENABLE_MASK;
3215bccae6eSSangbeom Kim 
322*5ccb7d71SGeert Uytterhoeven 		ret = regmap_raw_write(info->regmap, SEC_ALARM0_SEC, data, 8);
3235bccae6eSSangbeom Kim 		if (ret < 0)
3245bccae6eSSangbeom Kim 			return ret;
3255bccae6eSSangbeom Kim 
3265bccae6eSSangbeom Kim 		ret = s5m8767_rtc_set_alarm_reg(info);
3275bccae6eSSangbeom Kim 
3285bccae6eSSangbeom Kim 		break;
3295bccae6eSSangbeom Kim 
3305bccae6eSSangbeom Kim 	default:
3315bccae6eSSangbeom Kim 		return -EINVAL;
3325bccae6eSSangbeom Kim 	}
3335bccae6eSSangbeom Kim 
3345bccae6eSSangbeom Kim 	return ret;
3355bccae6eSSangbeom Kim }
3365bccae6eSSangbeom Kim 
3375bccae6eSSangbeom Kim static int s5m_rtc_start_alarm(struct s5m_rtc_info *info)
3385bccae6eSSangbeom Kim {
3395bccae6eSSangbeom Kim 	int ret;
3405bccae6eSSangbeom Kim 	u8 data[8];
3415bccae6eSSangbeom Kim 	u8 alarm0_conf;
3425bccae6eSSangbeom Kim 	struct rtc_time tm;
3435bccae6eSSangbeom Kim 
344*5ccb7d71SGeert Uytterhoeven 	ret = regmap_bulk_read(info->regmap, SEC_ALARM0_SEC, data, 8);
3455bccae6eSSangbeom Kim 	if (ret < 0)
3465bccae6eSSangbeom Kim 		return ret;
3475bccae6eSSangbeom Kim 
3485bccae6eSSangbeom Kim 	s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
3495bccae6eSSangbeom Kim 	dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
3505bccae6eSSangbeom Kim 		1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
3515bccae6eSSangbeom Kim 		tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday);
3525bccae6eSSangbeom Kim 
3535bccae6eSSangbeom Kim 	switch (info->device_type) {
3545bccae6eSSangbeom Kim 	case S5M8763X:
3555bccae6eSSangbeom Kim 		alarm0_conf = 0x77;
356*5ccb7d71SGeert Uytterhoeven 		ret = regmap_write(info->regmap, SEC_ALARM0_CONF, alarm0_conf);
3575bccae6eSSangbeom Kim 		break;
3585bccae6eSSangbeom Kim 
3595bccae6eSSangbeom Kim 	case S5M8767X:
3605bccae6eSSangbeom Kim 		data[RTC_SEC] |= ALARM_ENABLE_MASK;
3615bccae6eSSangbeom Kim 		data[RTC_MIN] |= ALARM_ENABLE_MASK;
3625bccae6eSSangbeom Kim 		data[RTC_HOUR] |= ALARM_ENABLE_MASK;
3635bccae6eSSangbeom Kim 		data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
3645bccae6eSSangbeom Kim 		if (data[RTC_DATE] & 0x1f)
3655bccae6eSSangbeom Kim 			data[RTC_DATE] |= ALARM_ENABLE_MASK;
3665bccae6eSSangbeom Kim 		if (data[RTC_MONTH] & 0xf)
3675bccae6eSSangbeom Kim 			data[RTC_MONTH] |= ALARM_ENABLE_MASK;
3685bccae6eSSangbeom Kim 		if (data[RTC_YEAR1] & 0x7f)
3695bccae6eSSangbeom Kim 			data[RTC_YEAR1] |= ALARM_ENABLE_MASK;
3705bccae6eSSangbeom Kim 
371*5ccb7d71SGeert Uytterhoeven 		ret = regmap_raw_write(info->regmap, SEC_ALARM0_SEC, data, 8);
3725bccae6eSSangbeom Kim 		if (ret < 0)
3735bccae6eSSangbeom Kim 			return ret;
3745bccae6eSSangbeom Kim 		ret = s5m8767_rtc_set_alarm_reg(info);
3755bccae6eSSangbeom Kim 
3765bccae6eSSangbeom Kim 		break;
3775bccae6eSSangbeom Kim 
3785bccae6eSSangbeom Kim 	default:
3795bccae6eSSangbeom Kim 		return -EINVAL;
3805bccae6eSSangbeom Kim 	}
3815bccae6eSSangbeom Kim 
3825bccae6eSSangbeom Kim 	return ret;
3835bccae6eSSangbeom Kim }
3845bccae6eSSangbeom Kim 
3855bccae6eSSangbeom Kim static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
3865bccae6eSSangbeom Kim {
3875bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
3885bccae6eSSangbeom Kim 	u8 data[8];
3895bccae6eSSangbeom Kim 	int ret;
3905bccae6eSSangbeom Kim 
3915bccae6eSSangbeom Kim 	switch (info->device_type) {
3925bccae6eSSangbeom Kim 	case S5M8763X:
3935bccae6eSSangbeom Kim 		s5m8763_tm_to_data(&alrm->time, data);
3945bccae6eSSangbeom Kim 		break;
3955bccae6eSSangbeom Kim 
3965bccae6eSSangbeom Kim 	case S5M8767X:
3975bccae6eSSangbeom Kim 		s5m8767_tm_to_data(&alrm->time, data);
3985bccae6eSSangbeom Kim 		break;
3995bccae6eSSangbeom Kim 
4005bccae6eSSangbeom Kim 	default:
4015bccae6eSSangbeom Kim 		return -EINVAL;
4025bccae6eSSangbeom Kim 	}
4035bccae6eSSangbeom Kim 
4045bccae6eSSangbeom Kim 	dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
4055bccae6eSSangbeom Kim 		1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
4065bccae6eSSangbeom Kim 		alrm->time.tm_mday, alrm->time.tm_hour, alrm->time.tm_min,
4075bccae6eSSangbeom Kim 		alrm->time.tm_sec, alrm->time.tm_wday);
4085bccae6eSSangbeom Kim 
4095bccae6eSSangbeom Kim 	ret = s5m_rtc_stop_alarm(info);
4105bccae6eSSangbeom Kim 	if (ret < 0)
4115bccae6eSSangbeom Kim 		return ret;
4125bccae6eSSangbeom Kim 
413*5ccb7d71SGeert Uytterhoeven 	ret = regmap_raw_write(info->regmap, SEC_ALARM0_SEC, data, 8);
4145bccae6eSSangbeom Kim 	if (ret < 0)
4155bccae6eSSangbeom Kim 		return ret;
4165bccae6eSSangbeom Kim 
4175bccae6eSSangbeom Kim 	ret = s5m8767_rtc_set_alarm_reg(info);
4185bccae6eSSangbeom Kim 	if (ret < 0)
4195bccae6eSSangbeom Kim 		return ret;
4205bccae6eSSangbeom Kim 
4215bccae6eSSangbeom Kim 	if (alrm->enabled)
4225bccae6eSSangbeom Kim 		ret = s5m_rtc_start_alarm(info);
4235bccae6eSSangbeom Kim 
4245bccae6eSSangbeom Kim 	return ret;
4255bccae6eSSangbeom Kim }
4265bccae6eSSangbeom Kim 
4275bccae6eSSangbeom Kim static int s5m_rtc_alarm_irq_enable(struct device *dev,
4285bccae6eSSangbeom Kim 				    unsigned int enabled)
4295bccae6eSSangbeom Kim {
4305bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = dev_get_drvdata(dev);
4315bccae6eSSangbeom Kim 
4325bccae6eSSangbeom Kim 	if (enabled)
4335bccae6eSSangbeom Kim 		return s5m_rtc_start_alarm(info);
4345bccae6eSSangbeom Kim 	else
4355bccae6eSSangbeom Kim 		return s5m_rtc_stop_alarm(info);
4365bccae6eSSangbeom Kim }
4375bccae6eSSangbeom Kim 
4385bccae6eSSangbeom Kim static irqreturn_t s5m_rtc_alarm_irq(int irq, void *data)
4395bccae6eSSangbeom Kim {
4405bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = data;
4415bccae6eSSangbeom Kim 
4425bccae6eSSangbeom Kim 	rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
4435bccae6eSSangbeom Kim 
4445bccae6eSSangbeom Kim 	return IRQ_HANDLED;
4455bccae6eSSangbeom Kim }
4465bccae6eSSangbeom Kim 
4475bccae6eSSangbeom Kim static const struct rtc_class_ops s5m_rtc_ops = {
4485bccae6eSSangbeom Kim 	.read_time = s5m_rtc_read_time,
4495bccae6eSSangbeom Kim 	.set_time = s5m_rtc_set_time,
4505bccae6eSSangbeom Kim 	.read_alarm = s5m_rtc_read_alarm,
4515bccae6eSSangbeom Kim 	.set_alarm = s5m_rtc_set_alarm,
4525bccae6eSSangbeom Kim 	.alarm_irq_enable = s5m_rtc_alarm_irq_enable,
4535bccae6eSSangbeom Kim };
4545bccae6eSSangbeom Kim 
4555bccae6eSSangbeom Kim static void s5m_rtc_enable_wtsr(struct s5m_rtc_info *info, bool enable)
4565bccae6eSSangbeom Kim {
4575bccae6eSSangbeom Kim 	int ret;
458*5ccb7d71SGeert Uytterhoeven 	ret = regmap_update_bits(info->regmap, SEC_WTSR_SMPL_CNTL,
4595bccae6eSSangbeom Kim 				 WTSR_ENABLE_MASK,
4605bccae6eSSangbeom Kim 				 enable ? WTSR_ENABLE_MASK : 0);
4615bccae6eSSangbeom Kim 	if (ret < 0)
4625bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to update WTSR reg(%d)\n",
4635bccae6eSSangbeom Kim 			__func__, ret);
4645bccae6eSSangbeom Kim }
4655bccae6eSSangbeom Kim 
4665bccae6eSSangbeom Kim static void s5m_rtc_enable_smpl(struct s5m_rtc_info *info, bool enable)
4675bccae6eSSangbeom Kim {
4685bccae6eSSangbeom Kim 	int ret;
469*5ccb7d71SGeert Uytterhoeven 	ret = regmap_update_bits(info->regmap, SEC_WTSR_SMPL_CNTL,
4705bccae6eSSangbeom Kim 				 SMPL_ENABLE_MASK,
4715bccae6eSSangbeom Kim 				 enable ? SMPL_ENABLE_MASK : 0);
4725bccae6eSSangbeom Kim 	if (ret < 0)
4735bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to update SMPL reg(%d)\n",
4745bccae6eSSangbeom Kim 			__func__, ret);
4755bccae6eSSangbeom Kim }
4765bccae6eSSangbeom Kim 
4775bccae6eSSangbeom Kim static int s5m8767_rtc_init_reg(struct s5m_rtc_info *info)
4785bccae6eSSangbeom Kim {
4795bccae6eSSangbeom Kim 	u8 data[2];
4805bccae6eSSangbeom Kim 	unsigned int tp_read;
4815bccae6eSSangbeom Kim 	int ret;
4825bccae6eSSangbeom Kim 	struct rtc_time tm;
4835bccae6eSSangbeom Kim 
484*5ccb7d71SGeert Uytterhoeven 	ret = regmap_read(info->regmap, SEC_RTC_UDR_CON, &tp_read);
4855bccae6eSSangbeom Kim 	if (ret < 0) {
4865bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to read control reg(%d)\n",
4875bccae6eSSangbeom Kim 			__func__, ret);
4885bccae6eSSangbeom Kim 		return ret;
4895bccae6eSSangbeom Kim 	}
4905bccae6eSSangbeom Kim 
4915bccae6eSSangbeom Kim 	/* Set RTC control register : Binary mode, 24hour mode */
4925bccae6eSSangbeom Kim 	data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
4935bccae6eSSangbeom Kim 	data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
4945bccae6eSSangbeom Kim 
4955bccae6eSSangbeom Kim 	info->rtc_24hr_mode = 1;
496*5ccb7d71SGeert Uytterhoeven 	ret = regmap_raw_write(info->regmap, SEC_ALARM0_CONF, data, 2);
4975bccae6eSSangbeom Kim 	if (ret < 0) {
4985bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
4995bccae6eSSangbeom Kim 			__func__, ret);
5005bccae6eSSangbeom Kim 		return ret;
5015bccae6eSSangbeom Kim 	}
5025bccae6eSSangbeom Kim 
5035bccae6eSSangbeom Kim 	/* In first boot time, Set rtc time to 1/1/2012 00:00:00(SUN) */
5045bccae6eSSangbeom Kim 	if ((tp_read & RTC_TCON_MASK) == 0) {
5055bccae6eSSangbeom Kim 		dev_dbg(info->dev, "rtc init\n");
5065bccae6eSSangbeom Kim 		tm.tm_sec = 0;
5075bccae6eSSangbeom Kim 		tm.tm_min = 0;
5085bccae6eSSangbeom Kim 		tm.tm_hour = 0;
5095bccae6eSSangbeom Kim 		tm.tm_wday = 0;
5105bccae6eSSangbeom Kim 		tm.tm_mday = 1;
5115bccae6eSSangbeom Kim 		tm.tm_mon = 0;
5125bccae6eSSangbeom Kim 		tm.tm_year = 112;
5135bccae6eSSangbeom Kim 		tm.tm_yday = 0;
5145bccae6eSSangbeom Kim 		tm.tm_isdst = 0;
5155bccae6eSSangbeom Kim 		ret = s5m_rtc_set_time(info->dev, &tm);
5165bccae6eSSangbeom Kim 	}
5175bccae6eSSangbeom Kim 
518*5ccb7d71SGeert Uytterhoeven 	ret = regmap_update_bits(info->regmap, SEC_RTC_UDR_CON,
5195bccae6eSSangbeom Kim 				 RTC_TCON_MASK, tp_read | RTC_TCON_MASK);
5205bccae6eSSangbeom Kim 	if (ret < 0)
5215bccae6eSSangbeom Kim 		dev_err(info->dev, "%s: fail to update TCON reg(%d)\n",
5225bccae6eSSangbeom Kim 			__func__, ret);
5235bccae6eSSangbeom Kim 
5245bccae6eSSangbeom Kim 	return ret;
5255bccae6eSSangbeom Kim }
5265bccae6eSSangbeom Kim 
5275bccae6eSSangbeom Kim static int s5m_rtc_probe(struct platform_device *pdev)
5285bccae6eSSangbeom Kim {
5295bccae6eSSangbeom Kim 	struct sec_pmic_dev *s5m87xx = dev_get_drvdata(pdev->dev.parent);
5305bccae6eSSangbeom Kim 	struct sec_platform_data *pdata = s5m87xx->pdata;
5315bccae6eSSangbeom Kim 	struct s5m_rtc_info *info;
5325bccae6eSSangbeom Kim 	int ret;
5335bccae6eSSangbeom Kim 
5345bccae6eSSangbeom Kim 	if (!pdata) {
5355bccae6eSSangbeom Kim 		dev_err(pdev->dev.parent, "Platform data not supplied\n");
5365bccae6eSSangbeom Kim 		return -ENODEV;
5375bccae6eSSangbeom Kim 	}
5385bccae6eSSangbeom Kim 
5395bccae6eSSangbeom Kim 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
5405bccae6eSSangbeom Kim 	if (!info)
5415bccae6eSSangbeom Kim 		return -ENOMEM;
5425bccae6eSSangbeom Kim 
5435bccae6eSSangbeom Kim 	info->dev = &pdev->dev;
5445bccae6eSSangbeom Kim 	info->s5m87xx = s5m87xx;
545*5ccb7d71SGeert Uytterhoeven 	info->regmap = s5m87xx->regmap;
5465bccae6eSSangbeom Kim 	info->device_type = s5m87xx->device_type;
5475bccae6eSSangbeom Kim 	info->wtsr_smpl = s5m87xx->wtsr_smpl;
5485bccae6eSSangbeom Kim 
5495bccae6eSSangbeom Kim 	switch (pdata->device_type) {
5505bccae6eSSangbeom Kim 	case S5M8763X:
5515bccae6eSSangbeom Kim 		info->irq = s5m87xx->irq_base + S5M8763_IRQ_ALARM0;
5525bccae6eSSangbeom Kim 		break;
5535bccae6eSSangbeom Kim 
5545bccae6eSSangbeom Kim 	case S5M8767X:
5555bccae6eSSangbeom Kim 		info->irq = s5m87xx->irq_base + S5M8767_IRQ_RTCA1;
5565bccae6eSSangbeom Kim 		break;
5575bccae6eSSangbeom Kim 
5585bccae6eSSangbeom Kim 	default:
5595bccae6eSSangbeom Kim 		ret = -EINVAL;
5605bccae6eSSangbeom Kim 		dev_err(&pdev->dev, "Unsupported device type: %d\n", ret);
5615bccae6eSSangbeom Kim 		return ret;
5625bccae6eSSangbeom Kim 	}
5635bccae6eSSangbeom Kim 
5645bccae6eSSangbeom Kim 	platform_set_drvdata(pdev, info);
5655bccae6eSSangbeom Kim 
5665bccae6eSSangbeom Kim 	ret = s5m8767_rtc_init_reg(info);
5675bccae6eSSangbeom Kim 
5685bccae6eSSangbeom Kim 	if (info->wtsr_smpl) {
5695bccae6eSSangbeom Kim 		s5m_rtc_enable_wtsr(info, true);
5705bccae6eSSangbeom Kim 		s5m_rtc_enable_smpl(info, true);
5715bccae6eSSangbeom Kim 	}
5725bccae6eSSangbeom Kim 
5735bccae6eSSangbeom Kim 	device_init_wakeup(&pdev->dev, 1);
5745bccae6eSSangbeom Kim 
5755bccae6eSSangbeom Kim 	info->rtc_dev = devm_rtc_device_register(&pdev->dev, "s5m-rtc",
5765bccae6eSSangbeom Kim 						 &s5m_rtc_ops, THIS_MODULE);
5775bccae6eSSangbeom Kim 
5785bccae6eSSangbeom Kim 	if (IS_ERR(info->rtc_dev))
5795bccae6eSSangbeom Kim 		return PTR_ERR(info->rtc_dev);
5805bccae6eSSangbeom Kim 
5815bccae6eSSangbeom Kim 	ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
5825bccae6eSSangbeom Kim 					s5m_rtc_alarm_irq, 0, "rtc-alarm0",
5835bccae6eSSangbeom Kim 					info);
5845bccae6eSSangbeom Kim 	if (ret < 0)
5855bccae6eSSangbeom Kim 		dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
5865bccae6eSSangbeom Kim 			info->irq, ret);
5875bccae6eSSangbeom Kim 
5885bccae6eSSangbeom Kim 	return ret;
5895bccae6eSSangbeom Kim }
5905bccae6eSSangbeom Kim 
5915bccae6eSSangbeom Kim static void s5m_rtc_shutdown(struct platform_device *pdev)
5925bccae6eSSangbeom Kim {
5935bccae6eSSangbeom Kim 	struct s5m_rtc_info *info = platform_get_drvdata(pdev);
5945bccae6eSSangbeom Kim 	int i;
5955bccae6eSSangbeom Kim 	unsigned int val = 0;
5965bccae6eSSangbeom Kim 	if (info->wtsr_smpl) {
5975bccae6eSSangbeom Kim 		for (i = 0; i < 3; i++) {
5985bccae6eSSangbeom Kim 			s5m_rtc_enable_wtsr(info, false);
599*5ccb7d71SGeert Uytterhoeven 			regmap_read(info->regmap, SEC_WTSR_SMPL_CNTL, &val);
6005bccae6eSSangbeom Kim 			pr_debug("%s: WTSR_SMPL reg(0x%02x)\n", __func__, val);
6015bccae6eSSangbeom Kim 			if (val & WTSR_ENABLE_MASK)
6025bccae6eSSangbeom Kim 				pr_emerg("%s: fail to disable WTSR\n",
6035bccae6eSSangbeom Kim 					 __func__);
6045bccae6eSSangbeom Kim 			else {
6055bccae6eSSangbeom Kim 				pr_info("%s: success to disable WTSR\n",
6065bccae6eSSangbeom Kim 					__func__);
6075bccae6eSSangbeom Kim 				break;
6085bccae6eSSangbeom Kim 			}
6095bccae6eSSangbeom Kim 		}
6105bccae6eSSangbeom Kim 	}
6115bccae6eSSangbeom Kim 	/* Disable SMPL when power off */
6125bccae6eSSangbeom Kim 	s5m_rtc_enable_smpl(info, false);
6135bccae6eSSangbeom Kim }
6145bccae6eSSangbeom Kim 
6155bccae6eSSangbeom Kim static const struct platform_device_id s5m_rtc_id[] = {
6165bccae6eSSangbeom Kim 	{ "s5m-rtc", 0 },
6175bccae6eSSangbeom Kim };
6185bccae6eSSangbeom Kim 
6195bccae6eSSangbeom Kim static struct platform_driver s5m_rtc_driver = {
6205bccae6eSSangbeom Kim 	.driver		= {
6215bccae6eSSangbeom Kim 		.name	= "s5m-rtc",
6225bccae6eSSangbeom Kim 		.owner	= THIS_MODULE,
6235bccae6eSSangbeom Kim 	},
6245bccae6eSSangbeom Kim 	.probe		= s5m_rtc_probe,
6255bccae6eSSangbeom Kim 	.shutdown	= s5m_rtc_shutdown,
6265bccae6eSSangbeom Kim 	.id_table	= s5m_rtc_id,
6275bccae6eSSangbeom Kim };
6285bccae6eSSangbeom Kim 
6295bccae6eSSangbeom Kim module_platform_driver(s5m_rtc_driver);
6305bccae6eSSangbeom Kim 
6315bccae6eSSangbeom Kim /* Module information */
6325bccae6eSSangbeom Kim MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
6335bccae6eSSangbeom Kim MODULE_DESCRIPTION("Samsung S5M RTC driver");
6345bccae6eSSangbeom Kim MODULE_LICENSE("GPL");
6355bccae6eSSangbeom Kim MODULE_ALIAS("platform:s5m-rtc");
636