14f9b9bbaSGeert Uytterhoeven /* 24f9b9bbaSGeert Uytterhoeven * Oki MSM6242 RTC Driver 34f9b9bbaSGeert Uytterhoeven * 44f9b9bbaSGeert Uytterhoeven * Copyright 2009 Geert Uytterhoeven 54f9b9bbaSGeert Uytterhoeven * 64f9b9bbaSGeert Uytterhoeven * Based on the A2000 TOD code in arch/m68k/amiga/config.c 74f9b9bbaSGeert Uytterhoeven * Copyright (C) 1993 Hamish Macdonald 84f9b9bbaSGeert Uytterhoeven */ 94f9b9bbaSGeert Uytterhoeven 104f9b9bbaSGeert Uytterhoeven #include <linux/delay.h> 114f9b9bbaSGeert Uytterhoeven #include <linux/io.h> 124f9b9bbaSGeert Uytterhoeven #include <linux/kernel.h> 134f9b9bbaSGeert Uytterhoeven #include <linux/module.h> 144f9b9bbaSGeert Uytterhoeven #include <linux/platform_device.h> 154f9b9bbaSGeert Uytterhoeven #include <linux/rtc.h> 165a0e3ad6STejun Heo #include <linux/slab.h> 174f9b9bbaSGeert Uytterhoeven 184f9b9bbaSGeert Uytterhoeven 194f9b9bbaSGeert Uytterhoeven enum { 204f9b9bbaSGeert Uytterhoeven MSM6242_SECOND1 = 0x0, /* 1-second digit register */ 214f9b9bbaSGeert Uytterhoeven MSM6242_SECOND10 = 0x1, /* 10-second digit register */ 224f9b9bbaSGeert Uytterhoeven MSM6242_MINUTE1 = 0x2, /* 1-minute digit register */ 234f9b9bbaSGeert Uytterhoeven MSM6242_MINUTE10 = 0x3, /* 10-minute digit register */ 244f9b9bbaSGeert Uytterhoeven MSM6242_HOUR1 = 0x4, /* 1-hour digit register */ 254f9b9bbaSGeert Uytterhoeven MSM6242_HOUR10 = 0x5, /* PM/AM, 10-hour digit register */ 264f9b9bbaSGeert Uytterhoeven MSM6242_DAY1 = 0x6, /* 1-day digit register */ 274f9b9bbaSGeert Uytterhoeven MSM6242_DAY10 = 0x7, /* 10-day digit register */ 284f9b9bbaSGeert Uytterhoeven MSM6242_MONTH1 = 0x8, /* 1-month digit register */ 294f9b9bbaSGeert Uytterhoeven MSM6242_MONTH10 = 0x9, /* 10-month digit register */ 304f9b9bbaSGeert Uytterhoeven MSM6242_YEAR1 = 0xa, /* 1-year digit register */ 314f9b9bbaSGeert Uytterhoeven MSM6242_YEAR10 = 0xb, /* 10-year digit register */ 324f9b9bbaSGeert Uytterhoeven MSM6242_WEEK = 0xc, /* Week register */ 334f9b9bbaSGeert Uytterhoeven MSM6242_CD = 0xd, /* Control Register D */ 344f9b9bbaSGeert Uytterhoeven MSM6242_CE = 0xe, /* Control Register E */ 354f9b9bbaSGeert Uytterhoeven MSM6242_CF = 0xf, /* Control Register F */ 364f9b9bbaSGeert Uytterhoeven }; 374f9b9bbaSGeert Uytterhoeven 384f9b9bbaSGeert Uytterhoeven #define MSM6242_HOUR10_AM (0 << 2) 394f9b9bbaSGeert Uytterhoeven #define MSM6242_HOUR10_PM (1 << 2) 404f9b9bbaSGeert Uytterhoeven #define MSM6242_HOUR10_HR_MASK (3 << 0) 414f9b9bbaSGeert Uytterhoeven 424f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_SUNDAY 0 434f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_MONDAY 1 444f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_TUESDAY 2 454f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_WEDNESDAY 3 464f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_THURSDAY 4 474f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_FRIDAY 5 484f9b9bbaSGeert Uytterhoeven #define MSM6242_WEEK_SATURDAY 6 494f9b9bbaSGeert Uytterhoeven 504f9b9bbaSGeert Uytterhoeven #define MSM6242_CD_30_S_ADJ (1 << 3) /* 30-second adjustment */ 514f9b9bbaSGeert Uytterhoeven #define MSM6242_CD_IRQ_FLAG (1 << 2) 524f9b9bbaSGeert Uytterhoeven #define MSM6242_CD_BUSY (1 << 1) 534f9b9bbaSGeert Uytterhoeven #define MSM6242_CD_HOLD (1 << 0) 544f9b9bbaSGeert Uytterhoeven 554f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_T_MASK (3 << 2) 564f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_T_64HZ (0 << 2) /* period 1/64 second */ 574f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_T_1HZ (1 << 2) /* period 1 second */ 584f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_T_1MINUTE (2 << 2) /* period 1 minute */ 594f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_T_1HOUR (3 << 2) /* period 1 hour */ 604f9b9bbaSGeert Uytterhoeven 614f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_ITRPT_STND (1 << 1) 624f9b9bbaSGeert Uytterhoeven #define MSM6242_CE_MASK (1 << 0) /* STD.P output control */ 634f9b9bbaSGeert Uytterhoeven 644f9b9bbaSGeert Uytterhoeven #define MSM6242_CF_TEST (1 << 3) 654f9b9bbaSGeert Uytterhoeven #define MSM6242_CF_12H (0 << 2) 664f9b9bbaSGeert Uytterhoeven #define MSM6242_CF_24H (1 << 2) 674f9b9bbaSGeert Uytterhoeven #define MSM6242_CF_STOP (1 << 1) 684f9b9bbaSGeert Uytterhoeven #define MSM6242_CF_REST (1 << 0) /* reset */ 694f9b9bbaSGeert Uytterhoeven 704f9b9bbaSGeert Uytterhoeven 714f9b9bbaSGeert Uytterhoeven struct msm6242_priv { 724f9b9bbaSGeert Uytterhoeven u32 __iomem *regs; 734f9b9bbaSGeert Uytterhoeven struct rtc_device *rtc; 744f9b9bbaSGeert Uytterhoeven }; 754f9b9bbaSGeert Uytterhoeven 764f9b9bbaSGeert Uytterhoeven static inline unsigned int msm6242_read(struct msm6242_priv *priv, 774f9b9bbaSGeert Uytterhoeven unsigned int reg) 784f9b9bbaSGeert Uytterhoeven { 794f9b9bbaSGeert Uytterhoeven return __raw_readl(&priv->regs[reg]) & 0xf; 804f9b9bbaSGeert Uytterhoeven } 814f9b9bbaSGeert Uytterhoeven 824f9b9bbaSGeert Uytterhoeven static inline void msm6242_write(struct msm6242_priv *priv, unsigned int val, 834f9b9bbaSGeert Uytterhoeven unsigned int reg) 844f9b9bbaSGeert Uytterhoeven { 85*d8ce1481SJohn Stultz __raw_writel(val, &priv->regs[reg]); 864f9b9bbaSGeert Uytterhoeven } 874f9b9bbaSGeert Uytterhoeven 884f9b9bbaSGeert Uytterhoeven static inline void msm6242_set(struct msm6242_priv *priv, unsigned int val, 894f9b9bbaSGeert Uytterhoeven unsigned int reg) 904f9b9bbaSGeert Uytterhoeven { 914f9b9bbaSGeert Uytterhoeven msm6242_write(priv, msm6242_read(priv, reg) | val, reg); 924f9b9bbaSGeert Uytterhoeven } 934f9b9bbaSGeert Uytterhoeven 944f9b9bbaSGeert Uytterhoeven static inline void msm6242_clear(struct msm6242_priv *priv, unsigned int val, 954f9b9bbaSGeert Uytterhoeven unsigned int reg) 964f9b9bbaSGeert Uytterhoeven { 974f9b9bbaSGeert Uytterhoeven msm6242_write(priv, msm6242_read(priv, reg) & ~val, reg); 984f9b9bbaSGeert Uytterhoeven } 994f9b9bbaSGeert Uytterhoeven 1004f9b9bbaSGeert Uytterhoeven static void msm6242_lock(struct msm6242_priv *priv) 1014f9b9bbaSGeert Uytterhoeven { 1024f9b9bbaSGeert Uytterhoeven int cnt = 5; 1034f9b9bbaSGeert Uytterhoeven 1044f9b9bbaSGeert Uytterhoeven msm6242_set(priv, MSM6242_CD_HOLD, MSM6242_CD); 1054f9b9bbaSGeert Uytterhoeven 1064f9b9bbaSGeert Uytterhoeven while ((msm6242_read(priv, MSM6242_CD) & MSM6242_CD_BUSY) && cnt) { 1074f9b9bbaSGeert Uytterhoeven msm6242_clear(priv, MSM6242_CD_HOLD, MSM6242_CD); 1084f9b9bbaSGeert Uytterhoeven udelay(70); 1094f9b9bbaSGeert Uytterhoeven msm6242_set(priv, MSM6242_CD_HOLD, MSM6242_CD); 1104f9b9bbaSGeert Uytterhoeven cnt--; 1114f9b9bbaSGeert Uytterhoeven } 1124f9b9bbaSGeert Uytterhoeven 1134f9b9bbaSGeert Uytterhoeven if (!cnt) 1144f9b9bbaSGeert Uytterhoeven pr_warning("msm6242: timed out waiting for RTC (0x%x)\n", 1154f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_CD)); 1164f9b9bbaSGeert Uytterhoeven } 1174f9b9bbaSGeert Uytterhoeven 1184f9b9bbaSGeert Uytterhoeven static void msm6242_unlock(struct msm6242_priv *priv) 1194f9b9bbaSGeert Uytterhoeven { 1204f9b9bbaSGeert Uytterhoeven msm6242_clear(priv, MSM6242_CD_HOLD, MSM6242_CD); 1214f9b9bbaSGeert Uytterhoeven } 1224f9b9bbaSGeert Uytterhoeven 1234f9b9bbaSGeert Uytterhoeven static int msm6242_read_time(struct device *dev, struct rtc_time *tm) 1244f9b9bbaSGeert Uytterhoeven { 1254f9b9bbaSGeert Uytterhoeven struct msm6242_priv *priv = dev_get_drvdata(dev); 1264f9b9bbaSGeert Uytterhoeven 1274f9b9bbaSGeert Uytterhoeven msm6242_lock(priv); 1284f9b9bbaSGeert Uytterhoeven 1294f9b9bbaSGeert Uytterhoeven tm->tm_sec = msm6242_read(priv, MSM6242_SECOND10) * 10 + 1304f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_SECOND1); 1314f9b9bbaSGeert Uytterhoeven tm->tm_min = msm6242_read(priv, MSM6242_MINUTE10) * 10 + 1324f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_MINUTE1); 1334f9b9bbaSGeert Uytterhoeven tm->tm_hour = (msm6242_read(priv, MSM6242_HOUR10 & 3)) * 10 + 1344f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_HOUR1); 1354f9b9bbaSGeert Uytterhoeven tm->tm_mday = msm6242_read(priv, MSM6242_DAY10) * 10 + 1364f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_DAY1); 1374f9b9bbaSGeert Uytterhoeven tm->tm_wday = msm6242_read(priv, MSM6242_WEEK); 1384f9b9bbaSGeert Uytterhoeven tm->tm_mon = msm6242_read(priv, MSM6242_MONTH10) * 10 + 1394f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_MONTH1) - 1; 1404f9b9bbaSGeert Uytterhoeven tm->tm_year = msm6242_read(priv, MSM6242_YEAR10) * 10 + 1414f9b9bbaSGeert Uytterhoeven msm6242_read(priv, MSM6242_YEAR1); 1424f9b9bbaSGeert Uytterhoeven if (tm->tm_year <= 69) 1434f9b9bbaSGeert Uytterhoeven tm->tm_year += 100; 1444f9b9bbaSGeert Uytterhoeven 1454f9b9bbaSGeert Uytterhoeven if (!(msm6242_read(priv, MSM6242_CF) & MSM6242_CF_24H)) { 1464f9b9bbaSGeert Uytterhoeven unsigned int pm = msm6242_read(priv, MSM6242_HOUR10) & 1474f9b9bbaSGeert Uytterhoeven MSM6242_HOUR10_PM; 1484f9b9bbaSGeert Uytterhoeven if (!pm && tm->tm_hour == 12) 1494f9b9bbaSGeert Uytterhoeven tm->tm_hour = 0; 1504f9b9bbaSGeert Uytterhoeven else if (pm && tm->tm_hour != 12) 1514f9b9bbaSGeert Uytterhoeven tm->tm_hour += 12; 1524f9b9bbaSGeert Uytterhoeven } 1534f9b9bbaSGeert Uytterhoeven 1544f9b9bbaSGeert Uytterhoeven msm6242_unlock(priv); 1554f9b9bbaSGeert Uytterhoeven 1564f9b9bbaSGeert Uytterhoeven return rtc_valid_tm(tm); 1574f9b9bbaSGeert Uytterhoeven } 1584f9b9bbaSGeert Uytterhoeven 1594f9b9bbaSGeert Uytterhoeven static int msm6242_set_time(struct device *dev, struct rtc_time *tm) 1604f9b9bbaSGeert Uytterhoeven { 1614f9b9bbaSGeert Uytterhoeven struct msm6242_priv *priv = dev_get_drvdata(dev); 1624f9b9bbaSGeert Uytterhoeven 1634f9b9bbaSGeert Uytterhoeven msm6242_lock(priv); 1644f9b9bbaSGeert Uytterhoeven 1654f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_sec / 10, MSM6242_SECOND10); 1664f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_sec % 10, MSM6242_SECOND1); 1674f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_min / 10, MSM6242_MINUTE10); 1684f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_min % 10, MSM6242_MINUTE1); 1694f9b9bbaSGeert Uytterhoeven if (msm6242_read(priv, MSM6242_CF) & MSM6242_CF_24H) 1704f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_hour / 10, MSM6242_HOUR10); 1714f9b9bbaSGeert Uytterhoeven else if (tm->tm_hour >= 12) 1724f9b9bbaSGeert Uytterhoeven msm6242_write(priv, MSM6242_HOUR10_PM + (tm->tm_hour - 12) / 10, 1734f9b9bbaSGeert Uytterhoeven MSM6242_HOUR10); 1744f9b9bbaSGeert Uytterhoeven else 1754f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_hour / 10, MSM6242_HOUR10); 1764f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_hour % 10, MSM6242_HOUR1); 1774f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_mday / 10, MSM6242_DAY10); 1784f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_mday % 10, MSM6242_DAY1); 1794f9b9bbaSGeert Uytterhoeven if (tm->tm_wday != -1) 1804f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_wday, MSM6242_WEEK); 1814f9b9bbaSGeert Uytterhoeven msm6242_write(priv, (tm->tm_mon + 1) / 10, MSM6242_MONTH10); 1824f9b9bbaSGeert Uytterhoeven msm6242_write(priv, (tm->tm_mon + 1) % 10, MSM6242_MONTH1); 1834f9b9bbaSGeert Uytterhoeven if (tm->tm_year >= 100) 1844f9b9bbaSGeert Uytterhoeven tm->tm_year -= 100; 1854f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_year / 10, MSM6242_YEAR10); 1864f9b9bbaSGeert Uytterhoeven msm6242_write(priv, tm->tm_year % 10, MSM6242_YEAR1); 1874f9b9bbaSGeert Uytterhoeven 1884f9b9bbaSGeert Uytterhoeven msm6242_unlock(priv); 1894f9b9bbaSGeert Uytterhoeven return 0; 1904f9b9bbaSGeert Uytterhoeven } 1914f9b9bbaSGeert Uytterhoeven 1924f9b9bbaSGeert Uytterhoeven static const struct rtc_class_ops msm6242_rtc_ops = { 1934f9b9bbaSGeert Uytterhoeven .read_time = msm6242_read_time, 1944f9b9bbaSGeert Uytterhoeven .set_time = msm6242_set_time, 1954f9b9bbaSGeert Uytterhoeven }; 1964f9b9bbaSGeert Uytterhoeven 1974f9b9bbaSGeert Uytterhoeven static int __init msm6242_rtc_probe(struct platform_device *dev) 1984f9b9bbaSGeert Uytterhoeven { 1994f9b9bbaSGeert Uytterhoeven struct resource *res; 2004f9b9bbaSGeert Uytterhoeven struct msm6242_priv *priv; 2014f9b9bbaSGeert Uytterhoeven struct rtc_device *rtc; 2024f9b9bbaSGeert Uytterhoeven int error; 2034f9b9bbaSGeert Uytterhoeven 2044f9b9bbaSGeert Uytterhoeven res = platform_get_resource(dev, IORESOURCE_MEM, 0); 2054f9b9bbaSGeert Uytterhoeven if (!res) 2064f9b9bbaSGeert Uytterhoeven return -ENODEV; 2074f9b9bbaSGeert Uytterhoeven 2084f9b9bbaSGeert Uytterhoeven priv = kzalloc(sizeof(*priv), GFP_KERNEL); 2094f9b9bbaSGeert Uytterhoeven if (!priv) 2104f9b9bbaSGeert Uytterhoeven return -ENOMEM; 2114f9b9bbaSGeert Uytterhoeven 2124f9b9bbaSGeert Uytterhoeven priv->regs = ioremap(res->start, resource_size(res)); 2134f9b9bbaSGeert Uytterhoeven if (!priv->regs) { 2144f9b9bbaSGeert Uytterhoeven error = -ENOMEM; 2154f9b9bbaSGeert Uytterhoeven goto out_free_priv; 2164f9b9bbaSGeert Uytterhoeven } 2174f9b9bbaSGeert Uytterhoeven 2184f9b9bbaSGeert Uytterhoeven rtc = rtc_device_register("rtc-msm6242", &dev->dev, &msm6242_rtc_ops, 2194f9b9bbaSGeert Uytterhoeven THIS_MODULE); 2204f9b9bbaSGeert Uytterhoeven if (IS_ERR(rtc)) { 2214f9b9bbaSGeert Uytterhoeven error = PTR_ERR(rtc); 2224f9b9bbaSGeert Uytterhoeven goto out_unmap; 2234f9b9bbaSGeert Uytterhoeven } 2244f9b9bbaSGeert Uytterhoeven 2254f9b9bbaSGeert Uytterhoeven priv->rtc = rtc; 2264f9b9bbaSGeert Uytterhoeven platform_set_drvdata(dev, priv); 2274f9b9bbaSGeert Uytterhoeven return 0; 2284f9b9bbaSGeert Uytterhoeven 2294f9b9bbaSGeert Uytterhoeven out_unmap: 2304f9b9bbaSGeert Uytterhoeven iounmap(priv->regs); 2314f9b9bbaSGeert Uytterhoeven out_free_priv: 2324f9b9bbaSGeert Uytterhoeven kfree(priv); 2334f9b9bbaSGeert Uytterhoeven return error; 2344f9b9bbaSGeert Uytterhoeven } 2354f9b9bbaSGeert Uytterhoeven 2364f9b9bbaSGeert Uytterhoeven static int __exit msm6242_rtc_remove(struct platform_device *dev) 2374f9b9bbaSGeert Uytterhoeven { 2384f9b9bbaSGeert Uytterhoeven struct msm6242_priv *priv = platform_get_drvdata(dev); 2394f9b9bbaSGeert Uytterhoeven 2404f9b9bbaSGeert Uytterhoeven rtc_device_unregister(priv->rtc); 2414f9b9bbaSGeert Uytterhoeven iounmap(priv->regs); 2424f9b9bbaSGeert Uytterhoeven kfree(priv); 2434f9b9bbaSGeert Uytterhoeven return 0; 2444f9b9bbaSGeert Uytterhoeven } 2454f9b9bbaSGeert Uytterhoeven 2464f9b9bbaSGeert Uytterhoeven static struct platform_driver msm6242_rtc_driver = { 2474f9b9bbaSGeert Uytterhoeven .driver = { 2484f9b9bbaSGeert Uytterhoeven .name = "rtc-msm6242", 2494f9b9bbaSGeert Uytterhoeven .owner = THIS_MODULE, 2504f9b9bbaSGeert Uytterhoeven }, 2514f9b9bbaSGeert Uytterhoeven .remove = __exit_p(msm6242_rtc_remove), 2524f9b9bbaSGeert Uytterhoeven }; 2534f9b9bbaSGeert Uytterhoeven 2544f9b9bbaSGeert Uytterhoeven static int __init msm6242_rtc_init(void) 2554f9b9bbaSGeert Uytterhoeven { 2564f9b9bbaSGeert Uytterhoeven return platform_driver_probe(&msm6242_rtc_driver, msm6242_rtc_probe); 2574f9b9bbaSGeert Uytterhoeven } 2584f9b9bbaSGeert Uytterhoeven 2594f9b9bbaSGeert Uytterhoeven static void __exit msm6242_rtc_fini(void) 2604f9b9bbaSGeert Uytterhoeven { 2614f9b9bbaSGeert Uytterhoeven platform_driver_unregister(&msm6242_rtc_driver); 2624f9b9bbaSGeert Uytterhoeven } 2634f9b9bbaSGeert Uytterhoeven 2644f9b9bbaSGeert Uytterhoeven module_init(msm6242_rtc_init); 2654f9b9bbaSGeert Uytterhoeven module_exit(msm6242_rtc_fini); 2664f9b9bbaSGeert Uytterhoeven 2674f9b9bbaSGeert Uytterhoeven MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>"); 2684f9b9bbaSGeert Uytterhoeven MODULE_LICENSE("GPL"); 2694f9b9bbaSGeert Uytterhoeven MODULE_DESCRIPTION("Oki MSM6242 RTC driver"); 2704f9b9bbaSGeert Uytterhoeven MODULE_ALIAS("platform:rtc-msm6242"); 271