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 inline void msm6242_set(struct msm6242_priv *priv, unsigned int val, 924f9b9bbaSGeert Uytterhoeven unsigned int reg) 934f9b9bbaSGeert Uytterhoeven { 944f9b9bbaSGeert Uytterhoeven msm6242_write(priv, msm6242_read(priv, reg) | val, reg); 954f9b9bbaSGeert Uytterhoeven } 964f9b9bbaSGeert Uytterhoeven 974f9b9bbaSGeert Uytterhoeven static inline void msm6242_clear(struct msm6242_priv *priv, unsigned int val, 984f9b9bbaSGeert Uytterhoeven unsigned int reg) 994f9b9bbaSGeert Uytterhoeven { 1004f9b9bbaSGeert Uytterhoeven msm6242_write(priv, msm6242_read(priv, reg) & ~val, reg); 1014f9b9bbaSGeert Uytterhoeven } 1024f9b9bbaSGeert Uytterhoeven 1034f9b9bbaSGeert Uytterhoeven static void msm6242_lock(struct msm6242_priv *priv) 1044f9b9bbaSGeert Uytterhoeven { 1054f9b9bbaSGeert Uytterhoeven int cnt = 5; 1064f9b9bbaSGeert Uytterhoeven 1074f9b9bbaSGeert Uytterhoeven msm6242_set(priv, MSM6242_CD_HOLD, MSM6242_CD); 1084f9b9bbaSGeert Uytterhoeven 1094f9b9bbaSGeert Uytterhoeven while ((msm6242_read(priv, MSM6242_CD) & MSM6242_CD_BUSY) && cnt) { 1104f9b9bbaSGeert Uytterhoeven msm6242_clear(priv, MSM6242_CD_HOLD, MSM6242_CD); 1114f9b9bbaSGeert Uytterhoeven udelay(70); 1124f9b9bbaSGeert Uytterhoeven msm6242_set(priv, MSM6242_CD_HOLD, MSM6242_CD); 1134f9b9bbaSGeert Uytterhoeven cnt--; 1144f9b9bbaSGeert Uytterhoeven } 1154f9b9bbaSGeert Uytterhoeven 1164f9b9bbaSGeert Uytterhoeven if (!cnt) 117a737e835SJoe Perches pr_warn("timed out waiting for RTC (0x%x)\n", 1184f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_CD)); 1194f9b9bbaSGeert Uytterhoeven } 1204f9b9bbaSGeert Uytterhoeven 1214f9b9bbaSGeert Uytterhoeven static void msm6242_unlock(struct msm6242_priv *priv) 1224f9b9bbaSGeert Uytterhoeven { 1234f9b9bbaSGeert Uytterhoeven msm6242_clear(priv, MSM6242_CD_HOLD, MSM6242_CD); 1244f9b9bbaSGeert Uytterhoeven } 1254f9b9bbaSGeert Uytterhoeven 1264f9b9bbaSGeert Uytterhoeven static int msm6242_read_time(struct device *dev, struct rtc_time *tm) 1274f9b9bbaSGeert Uytterhoeven { 1284f9b9bbaSGeert Uytterhoeven struct msm6242_priv *priv = dev_get_drvdata(dev); 1294f9b9bbaSGeert Uytterhoeven 1304f9b9bbaSGeert Uytterhoeven msm6242_lock(priv); 1314f9b9bbaSGeert Uytterhoeven 1324f9b9bbaSGeert Uytterhoeven tm->tm_sec = msm6242_read(priv, MSM6242_SECOND10) * 10 + 1334f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_SECOND1); 1344f9b9bbaSGeert Uytterhoeven tm->tm_min = msm6242_read(priv, MSM6242_MINUTE10) * 10 + 1354f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_MINUTE1); 136*e34494c8SKars de Jong tm->tm_hour = (msm6242_read(priv, MSM6242_HOUR10) & 137*e34494c8SKars de Jong MSM6242_HOUR10_HR_MASK) * 10 + 1384f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_HOUR1); 1394f9b9bbaSGeert Uytterhoeven tm->tm_mday = msm6242_read(priv, MSM6242_DAY10) * 10 + 1404f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_DAY1); 1414f9b9bbaSGeert Uytterhoeven tm->tm_wday = msm6242_read(priv, MSM6242_WEEK); 1424f9b9bbaSGeert Uytterhoeven tm->tm_mon = msm6242_read(priv, MSM6242_MONTH10) * 10 + 1434f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_MONTH1) - 1; 1444f9b9bbaSGeert Uytterhoeven tm->tm_year = msm6242_read(priv, MSM6242_YEAR10) * 10 + 1454f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_YEAR1); 1464f9b9bbaSGeert Uytterhoeven if (tm->tm_year <= 69) 1474f9b9bbaSGeert Uytterhoeven tm->tm_year += 100; 1484f9b9bbaSGeert Uytterhoeven 1494f9b9bbaSGeert Uytterhoeven if (!(msm6242_read(priv, MSM6242_CF) & MSM6242_CF_24H)) { 1504f9b9bbaSGeert Uytterhoeven unsigned int pm = msm6242_read(priv, MSM6242_HOUR10) & 1514f9b9bbaSGeert Uytterhoeven MSM6242_HOUR10_PM; 1524f9b9bbaSGeert Uytterhoeven if (!pm && tm->tm_hour == 12) 1534f9b9bbaSGeert Uytterhoeven tm->tm_hour = 0; 1544f9b9bbaSGeert Uytterhoeven else if (pm && tm->tm_hour != 12) 1554f9b9bbaSGeert Uytterhoeven tm->tm_hour += 12; 1564f9b9bbaSGeert Uytterhoeven } 1574f9b9bbaSGeert Uytterhoeven 1584f9b9bbaSGeert Uytterhoeven msm6242_unlock(priv); 1594f9b9bbaSGeert Uytterhoeven 16022652ba7SAlexandre Belloni return 0; 1614f9b9bbaSGeert Uytterhoeven } 1624f9b9bbaSGeert Uytterhoeven 1634f9b9bbaSGeert Uytterhoeven static int msm6242_set_time(struct device *dev, struct rtc_time *tm) 1644f9b9bbaSGeert Uytterhoeven { 1654f9b9bbaSGeert Uytterhoeven struct msm6242_priv *priv = dev_get_drvdata(dev); 1664f9b9bbaSGeert Uytterhoeven 1674f9b9bbaSGeert Uytterhoeven msm6242_lock(priv); 1684f9b9bbaSGeert Uytterhoeven 1694f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_sec / 10, MSM6242_SECOND10); 1704f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_sec % 10, MSM6242_SECOND1); 1714f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_min / 10, MSM6242_MINUTE10); 1724f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_min % 10, MSM6242_MINUTE1); 1734f9b9bbaSGeert Uytterhoeven if (msm6242_read(priv, MSM6242_CF) & MSM6242_CF_24H) 1744f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_hour / 10, MSM6242_HOUR10); 1754f9b9bbaSGeert Uytterhoeven else if (tm->tm_hour >= 12) 1764f9b9bbaSGeert Uytterhoeven msm6242_write(priv, MSM6242_HOUR10_PM + (tm->tm_hour - 12) / 10, 1774f9b9bbaSGeert Uytterhoeven MSM6242_HOUR10); 1784f9b9bbaSGeert Uytterhoeven else 1794f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_hour / 10, MSM6242_HOUR10); 1804f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_hour % 10, MSM6242_HOUR1); 1814f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_mday / 10, MSM6242_DAY10); 1824f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_mday % 10, MSM6242_DAY1); 1834f9b9bbaSGeert Uytterhoeven if (tm->tm_wday != -1) 1844f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_wday, MSM6242_WEEK); 1854f9b9bbaSGeert Uytterhoeven msm6242_write(priv, (tm->tm_mon + 1) / 10, MSM6242_MONTH10); 1864f9b9bbaSGeert Uytterhoeven msm6242_write(priv, (tm->tm_mon + 1) % 10, MSM6242_MONTH1); 1874f9b9bbaSGeert Uytterhoeven if (tm->tm_year >= 100) 1884f9b9bbaSGeert Uytterhoeven tm->tm_year -= 100; 1894f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_year / 10, MSM6242_YEAR10); 1904f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_year % 10, MSM6242_YEAR1); 1914f9b9bbaSGeert Uytterhoeven 1924f9b9bbaSGeert Uytterhoeven msm6242_unlock(priv); 1934f9b9bbaSGeert Uytterhoeven return 0; 1944f9b9bbaSGeert Uytterhoeven } 1954f9b9bbaSGeert Uytterhoeven 1964f9b9bbaSGeert Uytterhoeven static const struct rtc_class_ops msm6242_rtc_ops = { 1974f9b9bbaSGeert Uytterhoeven .read_time = msm6242_read_time, 1984f9b9bbaSGeert Uytterhoeven .set_time = msm6242_set_time, 1994f9b9bbaSGeert Uytterhoeven }; 2004f9b9bbaSGeert Uytterhoeven 201c40dcf6eSJingoo Han static int __init msm6242_rtc_probe(struct platform_device *pdev) 2024f9b9bbaSGeert Uytterhoeven { 2034f9b9bbaSGeert Uytterhoeven struct resource *res; 2044f9b9bbaSGeert Uytterhoeven struct msm6242_priv *priv; 2054f9b9bbaSGeert Uytterhoeven struct rtc_device *rtc; 2064f9b9bbaSGeert Uytterhoeven 207c40dcf6eSJingoo Han res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2084f9b9bbaSGeert Uytterhoeven if (!res) 2094f9b9bbaSGeert Uytterhoeven return -ENODEV; 2104f9b9bbaSGeert Uytterhoeven 211c40dcf6eSJingoo Han priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 2124f9b9bbaSGeert Uytterhoeven if (!priv) 2134f9b9bbaSGeert Uytterhoeven return -ENOMEM; 2144f9b9bbaSGeert Uytterhoeven 215c40dcf6eSJingoo Han priv->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res)); 216c40dcf6eSJingoo Han if (!priv->regs) 217c40dcf6eSJingoo Han return -ENOMEM; 218c40dcf6eSJingoo Han platform_set_drvdata(pdev, priv); 2194f9b9bbaSGeert Uytterhoeven 220c40dcf6eSJingoo Han rtc = devm_rtc_device_register(&pdev->dev, "rtc-msm6242", 221c40dcf6eSJingoo Han &msm6242_rtc_ops, THIS_MODULE); 222e3aa7526SJingoo Han if (IS_ERR(rtc)) 223e3aa7526SJingoo Han return PTR_ERR(rtc); 2244f9b9bbaSGeert Uytterhoeven 2254f9b9bbaSGeert Uytterhoeven priv->rtc = rtc; 2264f9b9bbaSGeert Uytterhoeven return 0; 2274f9b9bbaSGeert Uytterhoeven } 2284f9b9bbaSGeert Uytterhoeven 2294f9b9bbaSGeert Uytterhoeven static struct platform_driver msm6242_rtc_driver = { 2304f9b9bbaSGeert Uytterhoeven .driver = { 2314f9b9bbaSGeert Uytterhoeven .name = "rtc-msm6242", 2324f9b9bbaSGeert Uytterhoeven }, 2334f9b9bbaSGeert Uytterhoeven }; 2344f9b9bbaSGeert Uytterhoeven 23519cccfb5SJingoo Han module_platform_driver_probe(msm6242_rtc_driver, msm6242_rtc_probe); 2364f9b9bbaSGeert Uytterhoeven 2374f9b9bbaSGeert Uytterhoeven MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>"); 2384f9b9bbaSGeert Uytterhoeven MODULE_LICENSE("GPL"); 2394f9b9bbaSGeert Uytterhoeven MODULE_DESCRIPTION("Oki MSM6242 RTC driver"); 2404f9b9bbaSGeert Uytterhoeven MODULE_ALIAS("platform:rtc-msm6242"); 241