18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci* Copyright (c) 2014-2015 MediaTek Inc.
48c2ecf20Sopenharmony_ci* Author: Tianping.Fang <tianping.fang@mediatek.com>
58c2ecf20Sopenharmony_ci*/
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/err.h>
88c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
98c2ecf20Sopenharmony_ci#include <linux/mfd/mt6397/core.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/mutex.h>
128c2ecf20Sopenharmony_ci#include <linux/of_device.h>
138c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
148c2ecf20Sopenharmony_ci#include <linux/regmap.h>
158c2ecf20Sopenharmony_ci#include <linux/rtc.h>
168c2ecf20Sopenharmony_ci#include <linux/mfd/mt6397/rtc.h>
178c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic int mtk_rtc_write_trigger(struct mt6397_rtc *rtc)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	int ret;
228c2ecf20Sopenharmony_ci	u32 data;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	ret = regmap_write(rtc->regmap, rtc->addr_base + rtc->data->wrtgr, 1);
258c2ecf20Sopenharmony_ci	if (ret < 0)
268c2ecf20Sopenharmony_ci		return ret;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	ret = regmap_read_poll_timeout(rtc->regmap,
298c2ecf20Sopenharmony_ci					rtc->addr_base + RTC_BBPU, data,
308c2ecf20Sopenharmony_ci					!(data & RTC_BBPU_CBUSY),
318c2ecf20Sopenharmony_ci					MTK_RTC_POLL_DELAY_US,
328c2ecf20Sopenharmony_ci					MTK_RTC_POLL_TIMEOUT);
338c2ecf20Sopenharmony_ci	if (ret < 0)
348c2ecf20Sopenharmony_ci		dev_err(rtc->rtc_dev->dev.parent,
358c2ecf20Sopenharmony_ci			"failed to write WRTGR: %d\n", ret);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	return ret;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic irqreturn_t mtk_rtc_irq_handler_thread(int irq, void *data)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	struct mt6397_rtc *rtc = data;
438c2ecf20Sopenharmony_ci	u32 irqsta, irqen;
448c2ecf20Sopenharmony_ci	int ret;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_IRQ_STA, &irqsta);
478c2ecf20Sopenharmony_ci	if ((ret >= 0) && (irqsta & RTC_IRQ_STA_AL)) {
488c2ecf20Sopenharmony_ci		rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
498c2ecf20Sopenharmony_ci		irqen = irqsta & ~RTC_IRQ_EN_AL;
508c2ecf20Sopenharmony_ci		mutex_lock(&rtc->lock);
518c2ecf20Sopenharmony_ci		if (regmap_write(rtc->regmap, rtc->addr_base + RTC_IRQ_EN,
528c2ecf20Sopenharmony_ci				 irqen) == 0)
538c2ecf20Sopenharmony_ci			mtk_rtc_write_trigger(rtc);
548c2ecf20Sopenharmony_ci		mutex_unlock(&rtc->lock);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	return IRQ_NONE;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic int __mtk_rtc_read_time(struct mt6397_rtc *rtc,
638c2ecf20Sopenharmony_ci			       struct rtc_time *tm, int *sec)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	int ret;
668c2ecf20Sopenharmony_ci	u16 data[RTC_OFFSET_COUNT];
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	mutex_lock(&rtc->lock);
698c2ecf20Sopenharmony_ci	ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_TC_SEC,
708c2ecf20Sopenharmony_ci			       data, RTC_OFFSET_COUNT);
718c2ecf20Sopenharmony_ci	if (ret < 0)
728c2ecf20Sopenharmony_ci		goto exit;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	tm->tm_sec = data[RTC_OFFSET_SEC];
758c2ecf20Sopenharmony_ci	tm->tm_min = data[RTC_OFFSET_MIN];
768c2ecf20Sopenharmony_ci	tm->tm_hour = data[RTC_OFFSET_HOUR];
778c2ecf20Sopenharmony_ci	tm->tm_mday = data[RTC_OFFSET_DOM];
788c2ecf20Sopenharmony_ci	tm->tm_mon = data[RTC_OFFSET_MTH];
798c2ecf20Sopenharmony_ci	tm->tm_year = data[RTC_OFFSET_YEAR];
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_TC_SEC, sec);
828c2ecf20Sopenharmony_ciexit:
838c2ecf20Sopenharmony_ci	mutex_unlock(&rtc->lock);
848c2ecf20Sopenharmony_ci	return ret;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int mtk_rtc_read_time(struct device *dev, struct rtc_time *tm)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	time64_t time;
908c2ecf20Sopenharmony_ci	struct mt6397_rtc *rtc = dev_get_drvdata(dev);
918c2ecf20Sopenharmony_ci	int days, sec, ret;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	do {
948c2ecf20Sopenharmony_ci		ret = __mtk_rtc_read_time(rtc, tm, &sec);
958c2ecf20Sopenharmony_ci		if (ret < 0)
968c2ecf20Sopenharmony_ci			goto exit;
978c2ecf20Sopenharmony_ci	} while (sec < tm->tm_sec);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/* HW register use 7 bits to store year data, minus
1008c2ecf20Sopenharmony_ci	 * RTC_MIN_YEAR_OFFSET before write year data to register, and plus
1018c2ecf20Sopenharmony_ci	 * RTC_MIN_YEAR_OFFSET back after read year from register
1028c2ecf20Sopenharmony_ci	 */
1038c2ecf20Sopenharmony_ci	tm->tm_year += RTC_MIN_YEAR_OFFSET;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	/* HW register start mon from one, but tm_mon start from zero. */
1068c2ecf20Sopenharmony_ci	tm->tm_mon--;
1078c2ecf20Sopenharmony_ci	time = rtc_tm_to_time64(tm);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/* rtc_tm_to_time64 covert Gregorian date to seconds since
1108c2ecf20Sopenharmony_ci	 * 01-01-1970 00:00:00, and this date is Thursday.
1118c2ecf20Sopenharmony_ci	 */
1128c2ecf20Sopenharmony_ci	days = div_s64(time, 86400);
1138c2ecf20Sopenharmony_ci	tm->tm_wday = (days + 4) % 7;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciexit:
1168c2ecf20Sopenharmony_ci	return ret;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int mtk_rtc_set_time(struct device *dev, struct rtc_time *tm)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct mt6397_rtc *rtc = dev_get_drvdata(dev);
1228c2ecf20Sopenharmony_ci	int ret;
1238c2ecf20Sopenharmony_ci	u16 data[RTC_OFFSET_COUNT];
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	tm->tm_year -= RTC_MIN_YEAR_OFFSET;
1268c2ecf20Sopenharmony_ci	tm->tm_mon++;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	data[RTC_OFFSET_SEC] = tm->tm_sec;
1298c2ecf20Sopenharmony_ci	data[RTC_OFFSET_MIN] = tm->tm_min;
1308c2ecf20Sopenharmony_ci	data[RTC_OFFSET_HOUR] = tm->tm_hour;
1318c2ecf20Sopenharmony_ci	data[RTC_OFFSET_DOM] = tm->tm_mday;
1328c2ecf20Sopenharmony_ci	data[RTC_OFFSET_MTH] = tm->tm_mon;
1338c2ecf20Sopenharmony_ci	data[RTC_OFFSET_YEAR] = tm->tm_year;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	mutex_lock(&rtc->lock);
1368c2ecf20Sopenharmony_ci	ret = regmap_bulk_write(rtc->regmap, rtc->addr_base + RTC_TC_SEC,
1378c2ecf20Sopenharmony_ci				data, RTC_OFFSET_COUNT);
1388c2ecf20Sopenharmony_ci	if (ret < 0)
1398c2ecf20Sopenharmony_ci		goto exit;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	/* Time register write to hardware after call trigger function */
1428c2ecf20Sopenharmony_ci	ret = mtk_rtc_write_trigger(rtc);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ciexit:
1458c2ecf20Sopenharmony_ci	mutex_unlock(&rtc->lock);
1468c2ecf20Sopenharmony_ci	return ret;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic int mtk_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	struct rtc_time *tm = &alm->time;
1528c2ecf20Sopenharmony_ci	struct mt6397_rtc *rtc = dev_get_drvdata(dev);
1538c2ecf20Sopenharmony_ci	u32 irqen, pdn2;
1548c2ecf20Sopenharmony_ci	int ret;
1558c2ecf20Sopenharmony_ci	u16 data[RTC_OFFSET_COUNT];
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	mutex_lock(&rtc->lock);
1588c2ecf20Sopenharmony_ci	ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_IRQ_EN, &irqen);
1598c2ecf20Sopenharmony_ci	if (ret < 0)
1608c2ecf20Sopenharmony_ci		goto err_exit;
1618c2ecf20Sopenharmony_ci	ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_PDN2, &pdn2);
1628c2ecf20Sopenharmony_ci	if (ret < 0)
1638c2ecf20Sopenharmony_ci		goto err_exit;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_AL_SEC,
1668c2ecf20Sopenharmony_ci			       data, RTC_OFFSET_COUNT);
1678c2ecf20Sopenharmony_ci	if (ret < 0)
1688c2ecf20Sopenharmony_ci		goto err_exit;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	alm->enabled = !!(irqen & RTC_IRQ_EN_AL);
1718c2ecf20Sopenharmony_ci	alm->pending = !!(pdn2 & RTC_PDN2_PWRON_ALARM);
1728c2ecf20Sopenharmony_ci	mutex_unlock(&rtc->lock);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	tm->tm_sec = data[RTC_OFFSET_SEC] & RTC_AL_SEC_MASK;
1758c2ecf20Sopenharmony_ci	tm->tm_min = data[RTC_OFFSET_MIN] & RTC_AL_MIN_MASK;
1768c2ecf20Sopenharmony_ci	tm->tm_hour = data[RTC_OFFSET_HOUR] & RTC_AL_HOU_MASK;
1778c2ecf20Sopenharmony_ci	tm->tm_mday = data[RTC_OFFSET_DOM] & RTC_AL_DOM_MASK;
1788c2ecf20Sopenharmony_ci	tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_AL_MTH_MASK;
1798c2ecf20Sopenharmony_ci	tm->tm_year = data[RTC_OFFSET_YEAR] & RTC_AL_YEA_MASK;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	tm->tm_year += RTC_MIN_YEAR_OFFSET;
1828c2ecf20Sopenharmony_ci	tm->tm_mon--;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	return 0;
1858c2ecf20Sopenharmony_cierr_exit:
1868c2ecf20Sopenharmony_ci	mutex_unlock(&rtc->lock);
1878c2ecf20Sopenharmony_ci	return ret;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic int mtk_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	struct rtc_time *tm = &alm->time;
1938c2ecf20Sopenharmony_ci	struct mt6397_rtc *rtc = dev_get_drvdata(dev);
1948c2ecf20Sopenharmony_ci	int ret;
1958c2ecf20Sopenharmony_ci	u16 data[RTC_OFFSET_COUNT];
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	tm->tm_year -= RTC_MIN_YEAR_OFFSET;
1988c2ecf20Sopenharmony_ci	tm->tm_mon++;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	mutex_lock(&rtc->lock);
2018c2ecf20Sopenharmony_ci	ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_AL_SEC,
2028c2ecf20Sopenharmony_ci			       data, RTC_OFFSET_COUNT);
2038c2ecf20Sopenharmony_ci	if (ret < 0)
2048c2ecf20Sopenharmony_ci		goto exit;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	data[RTC_OFFSET_SEC] = ((data[RTC_OFFSET_SEC] & ~(RTC_AL_SEC_MASK)) |
2078c2ecf20Sopenharmony_ci				(tm->tm_sec & RTC_AL_SEC_MASK));
2088c2ecf20Sopenharmony_ci	data[RTC_OFFSET_MIN] = ((data[RTC_OFFSET_MIN] & ~(RTC_AL_MIN_MASK)) |
2098c2ecf20Sopenharmony_ci				(tm->tm_min & RTC_AL_MIN_MASK));
2108c2ecf20Sopenharmony_ci	data[RTC_OFFSET_HOUR] = ((data[RTC_OFFSET_HOUR] & ~(RTC_AL_HOU_MASK)) |
2118c2ecf20Sopenharmony_ci				(tm->tm_hour & RTC_AL_HOU_MASK));
2128c2ecf20Sopenharmony_ci	data[RTC_OFFSET_DOM] = ((data[RTC_OFFSET_DOM] & ~(RTC_AL_DOM_MASK)) |
2138c2ecf20Sopenharmony_ci				(tm->tm_mday & RTC_AL_DOM_MASK));
2148c2ecf20Sopenharmony_ci	data[RTC_OFFSET_MTH] = ((data[RTC_OFFSET_MTH] & ~(RTC_AL_MTH_MASK)) |
2158c2ecf20Sopenharmony_ci				(tm->tm_mon & RTC_AL_MTH_MASK));
2168c2ecf20Sopenharmony_ci	data[RTC_OFFSET_YEAR] = ((data[RTC_OFFSET_YEAR] & ~(RTC_AL_YEA_MASK)) |
2178c2ecf20Sopenharmony_ci				(tm->tm_year & RTC_AL_YEA_MASK));
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	if (alm->enabled) {
2208c2ecf20Sopenharmony_ci		ret = regmap_bulk_write(rtc->regmap,
2218c2ecf20Sopenharmony_ci					rtc->addr_base + RTC_AL_SEC,
2228c2ecf20Sopenharmony_ci					data, RTC_OFFSET_COUNT);
2238c2ecf20Sopenharmony_ci		if (ret < 0)
2248c2ecf20Sopenharmony_ci			goto exit;
2258c2ecf20Sopenharmony_ci		ret = regmap_write(rtc->regmap, rtc->addr_base + RTC_AL_MASK,
2268c2ecf20Sopenharmony_ci				   RTC_AL_MASK_DOW);
2278c2ecf20Sopenharmony_ci		if (ret < 0)
2288c2ecf20Sopenharmony_ci			goto exit;
2298c2ecf20Sopenharmony_ci		ret = regmap_update_bits(rtc->regmap,
2308c2ecf20Sopenharmony_ci					 rtc->addr_base + RTC_IRQ_EN,
2318c2ecf20Sopenharmony_ci					 RTC_IRQ_EN_ONESHOT_AL,
2328c2ecf20Sopenharmony_ci					 RTC_IRQ_EN_ONESHOT_AL);
2338c2ecf20Sopenharmony_ci		if (ret < 0)
2348c2ecf20Sopenharmony_ci			goto exit;
2358c2ecf20Sopenharmony_ci	} else {
2368c2ecf20Sopenharmony_ci		ret = regmap_update_bits(rtc->regmap,
2378c2ecf20Sopenharmony_ci					 rtc->addr_base + RTC_IRQ_EN,
2388c2ecf20Sopenharmony_ci					 RTC_IRQ_EN_ONESHOT_AL, 0);
2398c2ecf20Sopenharmony_ci		if (ret < 0)
2408c2ecf20Sopenharmony_ci			goto exit;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/* All alarm time register write to hardware after calling
2448c2ecf20Sopenharmony_ci	 * mtk_rtc_write_trigger. This can avoid race condition if alarm
2458c2ecf20Sopenharmony_ci	 * occur happen during writing alarm time register.
2468c2ecf20Sopenharmony_ci	 */
2478c2ecf20Sopenharmony_ci	ret = mtk_rtc_write_trigger(rtc);
2488c2ecf20Sopenharmony_ciexit:
2498c2ecf20Sopenharmony_ci	mutex_unlock(&rtc->lock);
2508c2ecf20Sopenharmony_ci	return ret;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic const struct rtc_class_ops mtk_rtc_ops = {
2548c2ecf20Sopenharmony_ci	.read_time  = mtk_rtc_read_time,
2558c2ecf20Sopenharmony_ci	.set_time   = mtk_rtc_set_time,
2568c2ecf20Sopenharmony_ci	.read_alarm = mtk_rtc_read_alarm,
2578c2ecf20Sopenharmony_ci	.set_alarm  = mtk_rtc_set_alarm,
2588c2ecf20Sopenharmony_ci};
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cistatic int mtk_rtc_probe(struct platform_device *pdev)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	struct resource *res;
2638c2ecf20Sopenharmony_ci	struct mt6397_chip *mt6397_chip = dev_get_drvdata(pdev->dev.parent);
2648c2ecf20Sopenharmony_ci	struct mt6397_rtc *rtc;
2658c2ecf20Sopenharmony_ci	int ret;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	rtc = devm_kzalloc(&pdev->dev, sizeof(struct mt6397_rtc), GFP_KERNEL);
2688c2ecf20Sopenharmony_ci	if (!rtc)
2698c2ecf20Sopenharmony_ci		return -ENOMEM;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2728c2ecf20Sopenharmony_ci	if (!res)
2738c2ecf20Sopenharmony_ci		return -EINVAL;
2748c2ecf20Sopenharmony_ci	rtc->addr_base = res->start;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	rtc->data = of_device_get_match_data(&pdev->dev);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	rtc->irq = platform_get_irq(pdev, 0);
2798c2ecf20Sopenharmony_ci	if (rtc->irq < 0)
2808c2ecf20Sopenharmony_ci		return rtc->irq;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	rtc->regmap = mt6397_chip->regmap;
2838c2ecf20Sopenharmony_ci	mutex_init(&rtc->lock);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, rtc);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
2888c2ecf20Sopenharmony_ci	if (IS_ERR(rtc->rtc_dev))
2898c2ecf20Sopenharmony_ci		return PTR_ERR(rtc->rtc_dev);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	ret = devm_request_threaded_irq(&pdev->dev, rtc->irq, NULL,
2928c2ecf20Sopenharmony_ci					mtk_rtc_irq_handler_thread,
2938c2ecf20Sopenharmony_ci					IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
2948c2ecf20Sopenharmony_ci					"mt6397-rtc", rtc);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	if (ret) {
2978c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
2988c2ecf20Sopenharmony_ci			rtc->irq, ret);
2998c2ecf20Sopenharmony_ci		return ret;
3008c2ecf20Sopenharmony_ci	}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	device_init_wakeup(&pdev->dev, 1);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	rtc->rtc_dev->ops = &mtk_rtc_ops;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	return rtc_register_device(rtc->rtc_dev);
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
3108c2ecf20Sopenharmony_cistatic int mt6397_rtc_suspend(struct device *dev)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	struct mt6397_rtc *rtc = dev_get_drvdata(dev);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	if (device_may_wakeup(dev))
3158c2ecf20Sopenharmony_ci		enable_irq_wake(rtc->irq);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	return 0;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cistatic int mt6397_rtc_resume(struct device *dev)
3218c2ecf20Sopenharmony_ci{
3228c2ecf20Sopenharmony_ci	struct mt6397_rtc *rtc = dev_get_drvdata(dev);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (device_may_wakeup(dev))
3258c2ecf20Sopenharmony_ci		disable_irq_wake(rtc->irq);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	return 0;
3288c2ecf20Sopenharmony_ci}
3298c2ecf20Sopenharmony_ci#endif
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(mt6397_pm_ops, mt6397_rtc_suspend,
3328c2ecf20Sopenharmony_ci			mt6397_rtc_resume);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic const struct mtk_rtc_data mt6358_rtc_data = {
3358c2ecf20Sopenharmony_ci	.wrtgr = RTC_WRTGR_MT6358,
3368c2ecf20Sopenharmony_ci};
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic const struct mtk_rtc_data mt6397_rtc_data = {
3398c2ecf20Sopenharmony_ci	.wrtgr = RTC_WRTGR_MT6397,
3408c2ecf20Sopenharmony_ci};
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_cistatic const struct of_device_id mt6397_rtc_of_match[] = {
3438c2ecf20Sopenharmony_ci	{ .compatible = "mediatek,mt6323-rtc", .data = &mt6397_rtc_data },
3448c2ecf20Sopenharmony_ci	{ .compatible = "mediatek,mt6358-rtc", .data = &mt6358_rtc_data },
3458c2ecf20Sopenharmony_ci	{ .compatible = "mediatek,mt6397-rtc", .data = &mt6397_rtc_data },
3468c2ecf20Sopenharmony_ci	{ }
3478c2ecf20Sopenharmony_ci};
3488c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mt6397_rtc_of_match);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic struct platform_driver mtk_rtc_driver = {
3518c2ecf20Sopenharmony_ci	.driver = {
3528c2ecf20Sopenharmony_ci		.name = "mt6397-rtc",
3538c2ecf20Sopenharmony_ci		.of_match_table = mt6397_rtc_of_match,
3548c2ecf20Sopenharmony_ci		.pm = &mt6397_pm_ops,
3558c2ecf20Sopenharmony_ci	},
3568c2ecf20Sopenharmony_ci	.probe	= mtk_rtc_probe,
3578c2ecf20Sopenharmony_ci};
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_cimodule_platform_driver(mtk_rtc_driver);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
3628c2ecf20Sopenharmony_ciMODULE_AUTHOR("Tianping Fang <tianping.fang@mediatek.com>");
3638c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("RTC Driver for MediaTek MT6397 PMIC");
364