xref: /linux/drivers/rtc/rtc-msm6242.c (revision 32c4d9e8a4eb286674dc1a3b93d51d791c7f645e)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24f9b9bbaSGeert Uytterhoeven /*
34f9b9bbaSGeert Uytterhoeven  *  Oki MSM6242 RTC Driver
44f9b9bbaSGeert Uytterhoeven  *
54f9b9bbaSGeert Uytterhoeven  *  Copyright 2009 Geert Uytterhoeven
64f9b9bbaSGeert Uytterhoeven  *
74f9b9bbaSGeert Uytterhoeven  *  Based on the A2000 TOD code in arch/m68k/amiga/config.c
84f9b9bbaSGeert Uytterhoeven  *  Copyright (C) 1993 Hamish Macdonald
94f9b9bbaSGeert Uytterhoeven  */
104f9b9bbaSGeert Uytterhoeven 
11a737e835SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12a737e835SJoe Perches 
134f9b9bbaSGeert Uytterhoeven #include <linux/delay.h>
144f9b9bbaSGeert Uytterhoeven #include <linux/io.h>
154f9b9bbaSGeert Uytterhoeven #include <linux/kernel.h>
164f9b9bbaSGeert Uytterhoeven #include <linux/module.h>
174f9b9bbaSGeert Uytterhoeven #include <linux/platform_device.h>
184f9b9bbaSGeert Uytterhoeven #include <linux/rtc.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
204f9b9bbaSGeert Uytterhoeven 
214f9b9bbaSGeert Uytterhoeven 
224f9b9bbaSGeert Uytterhoeven enum {
234f9b9bbaSGeert Uytterhoeven 	MSM6242_SECOND1		= 0x0,	/* 1-second digit register */
244f9b9bbaSGeert Uytterhoeven 	MSM6242_SECOND10	= 0x1,	/* 10-second digit register */
254f9b9bbaSGeert Uytterhoeven 	MSM6242_MINUTE1		= 0x2,	/* 1-minute digit register */
264f9b9bbaSGeert Uytterhoeven 	MSM6242_MINUTE10	= 0x3,	/* 10-minute digit register */
274f9b9bbaSGeert Uytterhoeven 	MSM6242_HOUR1		= 0x4,	/* 1-hour digit register */
284f9b9bbaSGeert Uytterhoeven 	MSM6242_HOUR10		= 0x5,	/* PM/AM, 10-hour digit register */
294f9b9bbaSGeert Uytterhoeven 	MSM6242_DAY1		= 0x6,	/* 1-day digit register */
304f9b9bbaSGeert Uytterhoeven 	MSM6242_DAY10		= 0x7,	/* 10-day digit register */
314f9b9bbaSGeert Uytterhoeven 	MSM6242_MONTH1		= 0x8,	/* 1-month digit register */
324f9b9bbaSGeert Uytterhoeven 	MSM6242_MONTH10		= 0x9,	/* 10-month digit register */
334f9b9bbaSGeert Uytterhoeven 	MSM6242_YEAR1		= 0xa,	/* 1-year digit register */
344f9b9bbaSGeert Uytterhoeven 	MSM6242_YEAR10		= 0xb,	/* 10-year digit register */
354f9b9bbaSGeert Uytterhoeven 	MSM6242_WEEK		= 0xc,	/* Week register */
364f9b9bbaSGeert Uytterhoeven 	MSM6242_CD		= 0xd,	/* Control Register D */
374f9b9bbaSGeert Uytterhoeven 	MSM6242_CE		= 0xe,	/* Control Register E */
384f9b9bbaSGeert Uytterhoeven 	MSM6242_CF		= 0xf,	/* Control Register F */
394f9b9bbaSGeert Uytterhoeven };
404f9b9bbaSGeert Uytterhoeven 
414f9b9bbaSGeert Uytterhoeven #define MSM6242_HOUR10_AM	(0 << 2)
424f9b9bbaSGeert Uytterhoeven #define MSM6242_HOUR10_PM	(1 << 2)
434f9b9bbaSGeert Uytterhoeven #define MSM6242_HOUR10_HR_MASK	(3 << 0)
444f9b9bbaSGeert Uytterhoeven 
454f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_SUNDAY	0
464f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_MONDAY	1
474f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_TUESDAY	2
484f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_WEDNESDAY	3
494f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_THURSDAY	4
504f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_FRIDAY	5
514f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_SATURDAY	6
524f9b9bbaSGeert Uytterhoeven 
534f9b9bbaSGeert Uytterhoeven #define MSM6242_CD_30_S_ADJ	(1 << 3)	/* 30-second adjustment */
544f9b9bbaSGeert Uytterhoeven #define MSM6242_CD_IRQ_FLAG	(1 << 2)
554f9b9bbaSGeert Uytterhoeven #define MSM6242_CD_BUSY		(1 << 1)
564f9b9bbaSGeert Uytterhoeven #define MSM6242_CD_HOLD		(1 << 0)
574f9b9bbaSGeert Uytterhoeven 
584f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_T_MASK	(3 << 2)
594f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_T_64HZ	(0 << 2)	/* period 1/64 second */
604f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_T_1HZ	(1 << 2)	/* period 1 second */
614f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_T_1MINUTE	(2 << 2)	/* period 1 minute */
624f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_T_1HOUR	(3 << 2)	/* period 1 hour */
634f9b9bbaSGeert Uytterhoeven 
644f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_ITRPT_STND	(1 << 1)
654f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_MASK		(1 << 0)	/* STD.P output control */
664f9b9bbaSGeert Uytterhoeven 
674f9b9bbaSGeert Uytterhoeven #define MSM6242_CF_TEST		(1 << 3)
684f9b9bbaSGeert Uytterhoeven #define MSM6242_CF_12H		(0 << 2)
694f9b9bbaSGeert Uytterhoeven #define MSM6242_CF_24H		(1 << 2)
704f9b9bbaSGeert Uytterhoeven #define MSM6242_CF_STOP		(1 << 1)
714f9b9bbaSGeert Uytterhoeven #define MSM6242_CF_REST		(1 << 0)	/* reset */
724f9b9bbaSGeert Uytterhoeven 
734f9b9bbaSGeert Uytterhoeven 
744f9b9bbaSGeert Uytterhoeven struct msm6242_priv {
754f9b9bbaSGeert Uytterhoeven 	u32 __iomem *regs;
764f9b9bbaSGeert Uytterhoeven 	struct rtc_device *rtc;
774f9b9bbaSGeert Uytterhoeven };
784f9b9bbaSGeert Uytterhoeven 
794f9b9bbaSGeert Uytterhoeven static inline unsigned int msm6242_read(struct msm6242_priv *priv,
804f9b9bbaSGeert Uytterhoeven 				       unsigned int reg)
814f9b9bbaSGeert Uytterhoeven {
824f9b9bbaSGeert Uytterhoeven 	return __raw_readl(&priv->regs[reg]) & 0xf;
834f9b9bbaSGeert Uytterhoeven }
844f9b9bbaSGeert Uytterhoeven 
854f9b9bbaSGeert Uytterhoeven static inline void msm6242_write(struct msm6242_priv *priv, unsigned int val,
864f9b9bbaSGeert Uytterhoeven 				unsigned int reg)
874f9b9bbaSGeert Uytterhoeven {
88d8ce1481SJohn Stultz 	__raw_writel(val, &priv->regs[reg]);
894f9b9bbaSGeert Uytterhoeven }
904f9b9bbaSGeert Uytterhoeven 
914f9b9bbaSGeert Uytterhoeven static void msm6242_lock(struct msm6242_priv *priv)
924f9b9bbaSGeert Uytterhoeven {
934f9b9bbaSGeert Uytterhoeven 	int cnt = 5;
944f9b9bbaSGeert Uytterhoeven 
95*32c4d9e8SKars de Jong 	msm6242_write(priv, MSM6242_CD_HOLD|MSM6242_CD_IRQ_FLAG, MSM6242_CD);
964f9b9bbaSGeert Uytterhoeven 
974f9b9bbaSGeert Uytterhoeven 	while ((msm6242_read(priv, MSM6242_CD) & MSM6242_CD_BUSY) && cnt) {
98*32c4d9e8SKars de Jong 		msm6242_write(priv, MSM6242_CD_IRQ_FLAG, MSM6242_CD);
994f9b9bbaSGeert Uytterhoeven 		udelay(70);
100*32c4d9e8SKars de Jong 		msm6242_write(priv, MSM6242_CD_HOLD|MSM6242_CD_IRQ_FLAG, MSM6242_CD);
1014f9b9bbaSGeert Uytterhoeven 		cnt--;
1024f9b9bbaSGeert Uytterhoeven 	}
1034f9b9bbaSGeert Uytterhoeven 
1044f9b9bbaSGeert Uytterhoeven 	if (!cnt)
105a737e835SJoe Perches 		pr_warn("timed out waiting for RTC (0x%x)\n",
1064f9b9bbaSGeert Uytterhoeven 			msm6242_read(priv, MSM6242_CD));
1074f9b9bbaSGeert Uytterhoeven }
1084f9b9bbaSGeert Uytterhoeven 
1094f9b9bbaSGeert Uytterhoeven static void msm6242_unlock(struct msm6242_priv *priv)
1104f9b9bbaSGeert Uytterhoeven {
111*32c4d9e8SKars de Jong 	msm6242_write(priv, MSM6242_CD_IRQ_FLAG, MSM6242_CD);
1124f9b9bbaSGeert Uytterhoeven }
1134f9b9bbaSGeert Uytterhoeven 
1144f9b9bbaSGeert Uytterhoeven static int msm6242_read_time(struct device *dev, struct rtc_time *tm)
1154f9b9bbaSGeert Uytterhoeven {
1164f9b9bbaSGeert Uytterhoeven 	struct msm6242_priv *priv = dev_get_drvdata(dev);
1174f9b9bbaSGeert Uytterhoeven 
1184f9b9bbaSGeert Uytterhoeven 	msm6242_lock(priv);
1194f9b9bbaSGeert Uytterhoeven 
1204f9b9bbaSGeert Uytterhoeven 	tm->tm_sec  = msm6242_read(priv, MSM6242_SECOND10) * 10 +
1214f9b9bbaSGeert Uytterhoeven 		      msm6242_read(priv, MSM6242_SECOND1);
1224f9b9bbaSGeert Uytterhoeven 	tm->tm_min  = msm6242_read(priv, MSM6242_MINUTE10) * 10 +
1234f9b9bbaSGeert Uytterhoeven 		      msm6242_read(priv, MSM6242_MINUTE1);
124e34494c8SKars de Jong 	tm->tm_hour = (msm6242_read(priv, MSM6242_HOUR10) &
125e34494c8SKars de Jong 		       MSM6242_HOUR10_HR_MASK) * 10 +
1264f9b9bbaSGeert Uytterhoeven 		      msm6242_read(priv, MSM6242_HOUR1);
1274f9b9bbaSGeert Uytterhoeven 	tm->tm_mday = msm6242_read(priv, MSM6242_DAY10) * 10 +
1284f9b9bbaSGeert Uytterhoeven 		      msm6242_read(priv, MSM6242_DAY1);
1294f9b9bbaSGeert Uytterhoeven 	tm->tm_wday = msm6242_read(priv, MSM6242_WEEK);
1304f9b9bbaSGeert Uytterhoeven 	tm->tm_mon  = msm6242_read(priv, MSM6242_MONTH10) * 10 +
1314f9b9bbaSGeert Uytterhoeven 		      msm6242_read(priv, MSM6242_MONTH1) - 1;
1324f9b9bbaSGeert Uytterhoeven 	tm->tm_year = msm6242_read(priv, MSM6242_YEAR10) * 10 +
1334f9b9bbaSGeert Uytterhoeven 		      msm6242_read(priv, MSM6242_YEAR1);
1344f9b9bbaSGeert Uytterhoeven 	if (tm->tm_year <= 69)
1354f9b9bbaSGeert Uytterhoeven 		tm->tm_year += 100;
1364f9b9bbaSGeert Uytterhoeven 
1374f9b9bbaSGeert Uytterhoeven 	if (!(msm6242_read(priv, MSM6242_CF) & MSM6242_CF_24H)) {
1384f9b9bbaSGeert Uytterhoeven 		unsigned int pm = msm6242_read(priv, MSM6242_HOUR10) &
1394f9b9bbaSGeert Uytterhoeven 				  MSM6242_HOUR10_PM;
1404f9b9bbaSGeert Uytterhoeven 		if (!pm && tm->tm_hour == 12)
1414f9b9bbaSGeert Uytterhoeven 			tm->tm_hour = 0;
1424f9b9bbaSGeert Uytterhoeven 		else if (pm && tm->tm_hour != 12)
1434f9b9bbaSGeert Uytterhoeven 			tm->tm_hour += 12;
1444f9b9bbaSGeert Uytterhoeven 	}
1454f9b9bbaSGeert Uytterhoeven 
1464f9b9bbaSGeert Uytterhoeven 	msm6242_unlock(priv);
1474f9b9bbaSGeert Uytterhoeven 
14822652ba7SAlexandre Belloni 	return 0;
1494f9b9bbaSGeert Uytterhoeven }
1504f9b9bbaSGeert Uytterhoeven 
1514f9b9bbaSGeert Uytterhoeven static int msm6242_set_time(struct device *dev, struct rtc_time *tm)
1524f9b9bbaSGeert Uytterhoeven {
1534f9b9bbaSGeert Uytterhoeven 	struct msm6242_priv *priv = dev_get_drvdata(dev);
1544f9b9bbaSGeert Uytterhoeven 
1554f9b9bbaSGeert Uytterhoeven 	msm6242_lock(priv);
1564f9b9bbaSGeert Uytterhoeven 
1574f9b9bbaSGeert Uytterhoeven 	msm6242_write(priv, tm->tm_sec / 10, MSM6242_SECOND10);
1584f9b9bbaSGeert Uytterhoeven 	msm6242_write(priv, tm->tm_sec % 10, MSM6242_SECOND1);
1594f9b9bbaSGeert Uytterhoeven 	msm6242_write(priv, tm->tm_min / 10, MSM6242_MINUTE10);
1604f9b9bbaSGeert Uytterhoeven 	msm6242_write(priv, tm->tm_min % 10, MSM6242_MINUTE1);
1614f9b9bbaSGeert Uytterhoeven 	if (msm6242_read(priv, MSM6242_CF) & MSM6242_CF_24H)
1624f9b9bbaSGeert Uytterhoeven 		msm6242_write(priv, tm->tm_hour / 10, MSM6242_HOUR10);
1634f9b9bbaSGeert Uytterhoeven 	else if (tm->tm_hour >= 12)
1644f9b9bbaSGeert Uytterhoeven 		msm6242_write(priv, MSM6242_HOUR10_PM + (tm->tm_hour - 12) / 10,
1654f9b9bbaSGeert Uytterhoeven 			      MSM6242_HOUR10);
1664f9b9bbaSGeert Uytterhoeven 	else
1674f9b9bbaSGeert Uytterhoeven 		msm6242_write(priv, tm->tm_hour / 10, MSM6242_HOUR10);
1684f9b9bbaSGeert Uytterhoeven 	msm6242_write(priv, tm->tm_hour % 10, MSM6242_HOUR1);
1694f9b9bbaSGeert Uytterhoeven 	msm6242_write(priv, tm->tm_mday / 10, MSM6242_DAY10);
1704f9b9bbaSGeert Uytterhoeven 	msm6242_write(priv, tm->tm_mday % 10, MSM6242_DAY1);
1714f9b9bbaSGeert Uytterhoeven 	if (tm->tm_wday != -1)
1724f9b9bbaSGeert Uytterhoeven 		msm6242_write(priv, tm->tm_wday, MSM6242_WEEK);
1734f9b9bbaSGeert Uytterhoeven 	msm6242_write(priv, (tm->tm_mon + 1) / 10, MSM6242_MONTH10);
1744f9b9bbaSGeert Uytterhoeven 	msm6242_write(priv, (tm->tm_mon + 1) % 10, MSM6242_MONTH1);
1754f9b9bbaSGeert Uytterhoeven 	if (tm->tm_year >= 100)
1764f9b9bbaSGeert Uytterhoeven 		tm->tm_year -= 100;
1774f9b9bbaSGeert Uytterhoeven 	msm6242_write(priv, tm->tm_year / 10, MSM6242_YEAR10);
1784f9b9bbaSGeert Uytterhoeven 	msm6242_write(priv, tm->tm_year % 10, MSM6242_YEAR1);
1794f9b9bbaSGeert Uytterhoeven 
1804f9b9bbaSGeert Uytterhoeven 	msm6242_unlock(priv);
1814f9b9bbaSGeert Uytterhoeven 	return 0;
1824f9b9bbaSGeert Uytterhoeven }
1834f9b9bbaSGeert Uytterhoeven 
1844f9b9bbaSGeert Uytterhoeven static const struct rtc_class_ops msm6242_rtc_ops = {
1854f9b9bbaSGeert Uytterhoeven 	.read_time	= msm6242_read_time,
1864f9b9bbaSGeert Uytterhoeven 	.set_time	= msm6242_set_time,
1874f9b9bbaSGeert Uytterhoeven };
1884f9b9bbaSGeert Uytterhoeven 
189c40dcf6eSJingoo Han static int __init msm6242_rtc_probe(struct platform_device *pdev)
1904f9b9bbaSGeert Uytterhoeven {
1914f9b9bbaSGeert Uytterhoeven 	struct resource *res;
1924f9b9bbaSGeert Uytterhoeven 	struct msm6242_priv *priv;
1934f9b9bbaSGeert Uytterhoeven 	struct rtc_device *rtc;
1944f9b9bbaSGeert Uytterhoeven 
195c40dcf6eSJingoo Han 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1964f9b9bbaSGeert Uytterhoeven 	if (!res)
1974f9b9bbaSGeert Uytterhoeven 		return -ENODEV;
1984f9b9bbaSGeert Uytterhoeven 
199c40dcf6eSJingoo Han 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
2004f9b9bbaSGeert Uytterhoeven 	if (!priv)
2014f9b9bbaSGeert Uytterhoeven 		return -ENOMEM;
2024f9b9bbaSGeert Uytterhoeven 
203c40dcf6eSJingoo Han 	priv->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
204c40dcf6eSJingoo Han 	if (!priv->regs)
205c40dcf6eSJingoo Han 		return -ENOMEM;
206c40dcf6eSJingoo Han 	platform_set_drvdata(pdev, priv);
2074f9b9bbaSGeert Uytterhoeven 
208c40dcf6eSJingoo Han 	rtc = devm_rtc_device_register(&pdev->dev, "rtc-msm6242",
209c40dcf6eSJingoo Han 				&msm6242_rtc_ops, THIS_MODULE);
210e3aa7526SJingoo Han 	if (IS_ERR(rtc))
211e3aa7526SJingoo Han 		return PTR_ERR(rtc);
2124f9b9bbaSGeert Uytterhoeven 
2134f9b9bbaSGeert Uytterhoeven 	priv->rtc = rtc;
2144f9b9bbaSGeert Uytterhoeven 	return 0;
2154f9b9bbaSGeert Uytterhoeven }
2164f9b9bbaSGeert Uytterhoeven 
2174f9b9bbaSGeert Uytterhoeven static struct platform_driver msm6242_rtc_driver = {
2184f9b9bbaSGeert Uytterhoeven 	.driver	= {
2194f9b9bbaSGeert Uytterhoeven 		.name	= "rtc-msm6242",
2204f9b9bbaSGeert Uytterhoeven 	},
2214f9b9bbaSGeert Uytterhoeven };
2224f9b9bbaSGeert Uytterhoeven 
22319cccfb5SJingoo Han module_platform_driver_probe(msm6242_rtc_driver, msm6242_rtc_probe);
2244f9b9bbaSGeert Uytterhoeven 
2254f9b9bbaSGeert Uytterhoeven MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
2264f9b9bbaSGeert Uytterhoeven MODULE_LICENSE("GPL");
2274f9b9bbaSGeert Uytterhoeven MODULE_DESCRIPTION("Oki MSM6242 RTC driver");
2284f9b9bbaSGeert Uytterhoeven MODULE_ALIAS("platform:rtc-msm6242");
229