18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * drivers/rtc/rtc-vt8500.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on rtc-pxa.c
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/rtc.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/io.h>
158c2ecf20Sopenharmony_ci#include <linux/bcd.h>
168c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include <linux/of.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * Register definitions
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#define VT8500_RTC_TS		0x00	/* Time set */
248c2ecf20Sopenharmony_ci#define VT8500_RTC_DS		0x04	/* Date set */
258c2ecf20Sopenharmony_ci#define VT8500_RTC_AS		0x08	/* Alarm set */
268c2ecf20Sopenharmony_ci#define VT8500_RTC_CR		0x0c	/* Control */
278c2ecf20Sopenharmony_ci#define VT8500_RTC_TR		0x10	/* Time read */
288c2ecf20Sopenharmony_ci#define VT8500_RTC_DR		0x14	/* Date read */
298c2ecf20Sopenharmony_ci#define VT8500_RTC_WS		0x18	/* Write status */
308c2ecf20Sopenharmony_ci#define VT8500_RTC_CL		0x20	/* Calibration */
318c2ecf20Sopenharmony_ci#define VT8500_RTC_IS		0x24	/* Interrupt status */
328c2ecf20Sopenharmony_ci#define VT8500_RTC_ST		0x28	/* Status */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define INVALID_TIME_BIT	(1 << 31)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define DATE_CENTURY_S		19
378c2ecf20Sopenharmony_ci#define DATE_YEAR_S		11
388c2ecf20Sopenharmony_ci#define DATE_YEAR_MASK		(0xff << DATE_YEAR_S)
398c2ecf20Sopenharmony_ci#define DATE_MONTH_S		6
408c2ecf20Sopenharmony_ci#define DATE_MONTH_MASK		(0x1f << DATE_MONTH_S)
418c2ecf20Sopenharmony_ci#define DATE_DAY_MASK		0x3f
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define TIME_DOW_S		20
448c2ecf20Sopenharmony_ci#define TIME_DOW_MASK		(0x07 << TIME_DOW_S)
458c2ecf20Sopenharmony_ci#define TIME_HOUR_S		14
468c2ecf20Sopenharmony_ci#define TIME_HOUR_MASK		(0x3f << TIME_HOUR_S)
478c2ecf20Sopenharmony_ci#define TIME_MIN_S		7
488c2ecf20Sopenharmony_ci#define TIME_MIN_MASK		(0x7f << TIME_MIN_S)
498c2ecf20Sopenharmony_ci#define TIME_SEC_MASK		0x7f
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define ALARM_DAY_S		20
528c2ecf20Sopenharmony_ci#define ALARM_DAY_MASK		(0x3f << ALARM_DAY_S)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define ALARM_DAY_BIT		(1 << 29)
558c2ecf20Sopenharmony_ci#define ALARM_HOUR_BIT		(1 << 28)
568c2ecf20Sopenharmony_ci#define ALARM_MIN_BIT		(1 << 27)
578c2ecf20Sopenharmony_ci#define ALARM_SEC_BIT		(1 << 26)
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define ALARM_ENABLE_MASK	(ALARM_DAY_BIT \
608c2ecf20Sopenharmony_ci				| ALARM_HOUR_BIT \
618c2ecf20Sopenharmony_ci				| ALARM_MIN_BIT \
628c2ecf20Sopenharmony_ci				| ALARM_SEC_BIT)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define VT8500_RTC_CR_ENABLE	(1 << 0)	/* Enable RTC */
658c2ecf20Sopenharmony_ci#define VT8500_RTC_CR_12H	(1 << 1)	/* 12h time format */
668c2ecf20Sopenharmony_ci#define VT8500_RTC_CR_SM_ENABLE	(1 << 2)	/* Enable periodic irqs */
678c2ecf20Sopenharmony_ci#define VT8500_RTC_CR_SM_SEC	(1 << 3)	/* 0: 1Hz/60, 1: 1Hz */
688c2ecf20Sopenharmony_ci#define VT8500_RTC_CR_CALIB	(1 << 4)	/* Enable calibration */
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define VT8500_RTC_IS_ALARM	(1 << 0)	/* Alarm interrupt status */
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistruct vt8500_rtc {
738c2ecf20Sopenharmony_ci	void __iomem		*regbase;
748c2ecf20Sopenharmony_ci	int			irq_alarm;
758c2ecf20Sopenharmony_ci	struct rtc_device	*rtc;
768c2ecf20Sopenharmony_ci	spinlock_t		lock;		/* Protects this structure */
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic irqreturn_t vt8500_rtc_irq(int irq, void *dev_id)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct vt8500_rtc *vt8500_rtc = dev_id;
828c2ecf20Sopenharmony_ci	u32 isr;
838c2ecf20Sopenharmony_ci	unsigned long events = 0;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	spin_lock(&vt8500_rtc->lock);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/* clear interrupt sources */
888c2ecf20Sopenharmony_ci	isr = readl(vt8500_rtc->regbase + VT8500_RTC_IS);
898c2ecf20Sopenharmony_ci	writel(isr, vt8500_rtc->regbase + VT8500_RTC_IS);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	spin_unlock(&vt8500_rtc->lock);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	if (isr & VT8500_RTC_IS_ALARM)
948c2ecf20Sopenharmony_ci		events |= RTC_AF | RTC_IRQF;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	rtc_update_irq(vt8500_rtc->rtc, 1, events);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic int vt8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
1048c2ecf20Sopenharmony_ci	u32 date, time;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	date = readl(vt8500_rtc->regbase + VT8500_RTC_DR);
1078c2ecf20Sopenharmony_ci	time = readl(vt8500_rtc->regbase + VT8500_RTC_TR);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	tm->tm_sec = bcd2bin(time & TIME_SEC_MASK);
1108c2ecf20Sopenharmony_ci	tm->tm_min = bcd2bin((time & TIME_MIN_MASK) >> TIME_MIN_S);
1118c2ecf20Sopenharmony_ci	tm->tm_hour = bcd2bin((time & TIME_HOUR_MASK) >> TIME_HOUR_S);
1128c2ecf20Sopenharmony_ci	tm->tm_mday = bcd2bin(date & DATE_DAY_MASK);
1138c2ecf20Sopenharmony_ci	tm->tm_mon = bcd2bin((date & DATE_MONTH_MASK) >> DATE_MONTH_S) - 1;
1148c2ecf20Sopenharmony_ci	tm->tm_year = bcd2bin((date & DATE_YEAR_MASK) >> DATE_YEAR_S)
1158c2ecf20Sopenharmony_ci			+ ((date >> DATE_CENTURY_S) & 1 ? 200 : 100);
1168c2ecf20Sopenharmony_ci	tm->tm_wday = (time & TIME_DOW_MASK) >> TIME_DOW_S;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	return 0;
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic int vt8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	writel((bin2bcd(tm->tm_year % 100) << DATE_YEAR_S)
1268c2ecf20Sopenharmony_ci		| (bin2bcd(tm->tm_mon + 1) << DATE_MONTH_S)
1278c2ecf20Sopenharmony_ci		| (bin2bcd(tm->tm_mday))
1288c2ecf20Sopenharmony_ci		| ((tm->tm_year >= 200) << DATE_CENTURY_S),
1298c2ecf20Sopenharmony_ci		vt8500_rtc->regbase + VT8500_RTC_DS);
1308c2ecf20Sopenharmony_ci	writel((bin2bcd(tm->tm_wday) << TIME_DOW_S)
1318c2ecf20Sopenharmony_ci		| (bin2bcd(tm->tm_hour) << TIME_HOUR_S)
1328c2ecf20Sopenharmony_ci		| (bin2bcd(tm->tm_min) << TIME_MIN_S)
1338c2ecf20Sopenharmony_ci		| (bin2bcd(tm->tm_sec)),
1348c2ecf20Sopenharmony_ci		vt8500_rtc->regbase + VT8500_RTC_TS);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	return 0;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic int vt8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
1428c2ecf20Sopenharmony_ci	u32 isr, alarm;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	alarm = readl(vt8500_rtc->regbase + VT8500_RTC_AS);
1458c2ecf20Sopenharmony_ci	isr = readl(vt8500_rtc->regbase + VT8500_RTC_IS);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	alrm->time.tm_mday = bcd2bin((alarm & ALARM_DAY_MASK) >> ALARM_DAY_S);
1488c2ecf20Sopenharmony_ci	alrm->time.tm_hour = bcd2bin((alarm & TIME_HOUR_MASK) >> TIME_HOUR_S);
1498c2ecf20Sopenharmony_ci	alrm->time.tm_min = bcd2bin((alarm & TIME_MIN_MASK) >> TIME_MIN_S);
1508c2ecf20Sopenharmony_ci	alrm->time.tm_sec = bcd2bin((alarm & TIME_SEC_MASK));
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	alrm->enabled = (alarm & ALARM_ENABLE_MASK) ? 1 : 0;
1538c2ecf20Sopenharmony_ci	alrm->pending = (isr & VT8500_RTC_IS_ALARM) ? 1 : 0;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	return rtc_valid_tm(&alrm->time);
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic int vt8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	writel((alrm->enabled ? ALARM_ENABLE_MASK : 0)
1638c2ecf20Sopenharmony_ci		| (bin2bcd(alrm->time.tm_mday) << ALARM_DAY_S)
1648c2ecf20Sopenharmony_ci		| (bin2bcd(alrm->time.tm_hour) << TIME_HOUR_S)
1658c2ecf20Sopenharmony_ci		| (bin2bcd(alrm->time.tm_min) << TIME_MIN_S)
1668c2ecf20Sopenharmony_ci		| (bin2bcd(alrm->time.tm_sec)),
1678c2ecf20Sopenharmony_ci		vt8500_rtc->regbase + VT8500_RTC_AS);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return 0;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic int vt8500_alarm_irq_enable(struct device *dev, unsigned int enabled)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
1758c2ecf20Sopenharmony_ci	unsigned long tmp = readl(vt8500_rtc->regbase + VT8500_RTC_AS);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	if (enabled)
1788c2ecf20Sopenharmony_ci		tmp |= ALARM_ENABLE_MASK;
1798c2ecf20Sopenharmony_ci	else
1808c2ecf20Sopenharmony_ci		tmp &= ~ALARM_ENABLE_MASK;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	writel(tmp, vt8500_rtc->regbase + VT8500_RTC_AS);
1838c2ecf20Sopenharmony_ci	return 0;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic const struct rtc_class_ops vt8500_rtc_ops = {
1878c2ecf20Sopenharmony_ci	.read_time = vt8500_rtc_read_time,
1888c2ecf20Sopenharmony_ci	.set_time = vt8500_rtc_set_time,
1898c2ecf20Sopenharmony_ci	.read_alarm = vt8500_rtc_read_alarm,
1908c2ecf20Sopenharmony_ci	.set_alarm = vt8500_rtc_set_alarm,
1918c2ecf20Sopenharmony_ci	.alarm_irq_enable = vt8500_alarm_irq_enable,
1928c2ecf20Sopenharmony_ci};
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic int vt8500_rtc_probe(struct platform_device *pdev)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	struct vt8500_rtc *vt8500_rtc;
1978c2ecf20Sopenharmony_ci	int ret;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	vt8500_rtc = devm_kzalloc(&pdev->dev,
2008c2ecf20Sopenharmony_ci			   sizeof(struct vt8500_rtc), GFP_KERNEL);
2018c2ecf20Sopenharmony_ci	if (!vt8500_rtc)
2028c2ecf20Sopenharmony_ci		return -ENOMEM;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	spin_lock_init(&vt8500_rtc->lock);
2058c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, vt8500_rtc);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	vt8500_rtc->irq_alarm = platform_get_irq(pdev, 0);
2088c2ecf20Sopenharmony_ci	if (vt8500_rtc->irq_alarm < 0)
2098c2ecf20Sopenharmony_ci		return vt8500_rtc->irq_alarm;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	vt8500_rtc->regbase = devm_platform_ioremap_resource(pdev, 0);
2128c2ecf20Sopenharmony_ci	if (IS_ERR(vt8500_rtc->regbase))
2138c2ecf20Sopenharmony_ci		return PTR_ERR(vt8500_rtc->regbase);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	/* Enable RTC and set it to 24-hour mode */
2168c2ecf20Sopenharmony_ci	writel(VT8500_RTC_CR_ENABLE,
2178c2ecf20Sopenharmony_ci	       vt8500_rtc->regbase + VT8500_RTC_CR);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	vt8500_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
2208c2ecf20Sopenharmony_ci	if (IS_ERR(vt8500_rtc->rtc))
2218c2ecf20Sopenharmony_ci		return PTR_ERR(vt8500_rtc->rtc);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	vt8500_rtc->rtc->ops = &vt8500_rtc_ops;
2248c2ecf20Sopenharmony_ci	vt8500_rtc->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
2258c2ecf20Sopenharmony_ci	vt8500_rtc->rtc->range_max = RTC_TIMESTAMP_END_2199;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	ret = devm_request_irq(&pdev->dev, vt8500_rtc->irq_alarm,
2288c2ecf20Sopenharmony_ci				vt8500_rtc_irq, 0, "rtc alarm", vt8500_rtc);
2298c2ecf20Sopenharmony_ci	if (ret < 0) {
2308c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "can't get irq %i, err %d\n",
2318c2ecf20Sopenharmony_ci			vt8500_rtc->irq_alarm, ret);
2328c2ecf20Sopenharmony_ci		return ret;
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	return rtc_register_device(vt8500_rtc->rtc);
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic int vt8500_rtc_remove(struct platform_device *pdev)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	struct vt8500_rtc *vt8500_rtc = platform_get_drvdata(pdev);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/* Disable alarm matching */
2438c2ecf20Sopenharmony_ci	writel(0, vt8500_rtc->regbase + VT8500_RTC_IS);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	return 0;
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic const struct of_device_id wmt_dt_ids[] = {
2498c2ecf20Sopenharmony_ci	{ .compatible = "via,vt8500-rtc", },
2508c2ecf20Sopenharmony_ci	{}
2518c2ecf20Sopenharmony_ci};
2528c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, wmt_dt_ids);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic struct platform_driver vt8500_rtc_driver = {
2558c2ecf20Sopenharmony_ci	.probe		= vt8500_rtc_probe,
2568c2ecf20Sopenharmony_ci	.remove		= vt8500_rtc_remove,
2578c2ecf20Sopenharmony_ci	.driver		= {
2588c2ecf20Sopenharmony_ci		.name	= "vt8500-rtc",
2598c2ecf20Sopenharmony_ci		.of_match_table = wmt_dt_ids,
2608c2ecf20Sopenharmony_ci	},
2618c2ecf20Sopenharmony_ci};
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cimodule_platform_driver(vt8500_rtc_driver);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
2668c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("VIA VT8500 SoC Realtime Clock Driver (RTC)");
2678c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
2688c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:vt8500-rtc");
269