18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2007-2009 ST-Ericsson AB 48c2ecf20Sopenharmony_ci * Real Time Clock interface for ST-Ericsson AB COH 901 331 RTC. 58c2ecf20Sopenharmony_ci * Author: Linus Walleij <linus.walleij@stericsson.com> 68c2ecf20Sopenharmony_ci * Based on rtc-pl031.c by Deepak Saxena <dsaxena@plexity.net> 78c2ecf20Sopenharmony_ci * Copyright 2006 (c) MontaVista Software, Inc. 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci#include <linux/init.h> 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h> 128c2ecf20Sopenharmony_ci#include <linux/rtc.h> 138c2ecf20Sopenharmony_ci#include <linux/clk.h> 148c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 158c2ecf20Sopenharmony_ci#include <linux/pm.h> 168c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 178c2ecf20Sopenharmony_ci#include <linux/io.h> 188c2ecf20Sopenharmony_ci#include <linux/slab.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci/* 218c2ecf20Sopenharmony_ci * Registers in the COH 901 331 228c2ecf20Sopenharmony_ci */ 238c2ecf20Sopenharmony_ci/* Alarm value 32bit (R/W) */ 248c2ecf20Sopenharmony_ci#define COH901331_ALARM 0x00U 258c2ecf20Sopenharmony_ci/* Used to set current time 32bit (R/W) */ 268c2ecf20Sopenharmony_ci#define COH901331_SET_TIME 0x04U 278c2ecf20Sopenharmony_ci/* Indication if current time is valid 32bit (R/-) */ 288c2ecf20Sopenharmony_ci#define COH901331_VALID 0x08U 298c2ecf20Sopenharmony_ci/* Read the current time 32bit (R/-) */ 308c2ecf20Sopenharmony_ci#define COH901331_CUR_TIME 0x0cU 318c2ecf20Sopenharmony_ci/* Event register for the "alarm" interrupt */ 328c2ecf20Sopenharmony_ci#define COH901331_IRQ_EVENT 0x10U 338c2ecf20Sopenharmony_ci/* Mask register for the "alarm" interrupt */ 348c2ecf20Sopenharmony_ci#define COH901331_IRQ_MASK 0x14U 358c2ecf20Sopenharmony_ci/* Force register for the "alarm" interrupt */ 368c2ecf20Sopenharmony_ci#define COH901331_IRQ_FORCE 0x18U 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci/* 398c2ecf20Sopenharmony_ci * Reference to RTC block clock 408c2ecf20Sopenharmony_ci * Notice that the frequent clk_enable()/clk_disable() on this 418c2ecf20Sopenharmony_ci * clock is mainly to be able to turn on/off other clocks in the 428c2ecf20Sopenharmony_ci * hierarchy as needed, the RTC clock is always on anyway. 438c2ecf20Sopenharmony_ci */ 448c2ecf20Sopenharmony_cistruct coh901331_port { 458c2ecf20Sopenharmony_ci struct rtc_device *rtc; 468c2ecf20Sopenharmony_ci struct clk *clk; 478c2ecf20Sopenharmony_ci void __iomem *virtbase; 488c2ecf20Sopenharmony_ci int irq; 498c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 508c2ecf20Sopenharmony_ci u32 irqmaskstore; 518c2ecf20Sopenharmony_ci#endif 528c2ecf20Sopenharmony_ci}; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_cistatic irqreturn_t coh901331_interrupt(int irq, void *data) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci struct coh901331_port *rtap = data; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci clk_enable(rtap->clk); 598c2ecf20Sopenharmony_ci /* Ack IRQ */ 608c2ecf20Sopenharmony_ci writel(1, rtap->virtbase + COH901331_IRQ_EVENT); 618c2ecf20Sopenharmony_ci /* 628c2ecf20Sopenharmony_ci * Disable the interrupt. This is necessary because 638c2ecf20Sopenharmony_ci * the RTC lives on a lower-clocked line and will 648c2ecf20Sopenharmony_ci * not release the IRQ line until after a few (slower) 658c2ecf20Sopenharmony_ci * clock cycles. The interrupt will be re-enabled when 668c2ecf20Sopenharmony_ci * a new alarm is set anyway. 678c2ecf20Sopenharmony_ci */ 688c2ecf20Sopenharmony_ci writel(0, rtap->virtbase + COH901331_IRQ_MASK); 698c2ecf20Sopenharmony_ci clk_disable(rtap->clk); 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci /* Set alarm flag */ 728c2ecf20Sopenharmony_ci rtc_update_irq(rtap->rtc, 1, RTC_AF); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci return IRQ_HANDLED; 758c2ecf20Sopenharmony_ci} 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistatic int coh901331_read_time(struct device *dev, struct rtc_time *tm) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci struct coh901331_port *rtap = dev_get_drvdata(dev); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci clk_enable(rtap->clk); 828c2ecf20Sopenharmony_ci /* Check if the time is valid */ 838c2ecf20Sopenharmony_ci if (!readl(rtap->virtbase + COH901331_VALID)) { 848c2ecf20Sopenharmony_ci clk_disable(rtap->clk); 858c2ecf20Sopenharmony_ci return -EINVAL; 868c2ecf20Sopenharmony_ci } 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci rtc_time64_to_tm(readl(rtap->virtbase + COH901331_CUR_TIME), tm); 898c2ecf20Sopenharmony_ci clk_disable(rtap->clk); 908c2ecf20Sopenharmony_ci return 0; 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic int coh901331_set_time(struct device *dev, struct rtc_time *tm) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci struct coh901331_port *rtap = dev_get_drvdata(dev); 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci clk_enable(rtap->clk); 988c2ecf20Sopenharmony_ci writel(rtc_tm_to_time64(tm), rtap->virtbase + COH901331_SET_TIME); 998c2ecf20Sopenharmony_ci clk_disable(rtap->clk); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci return 0; 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic int coh901331_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) 1058c2ecf20Sopenharmony_ci{ 1068c2ecf20Sopenharmony_ci struct coh901331_port *rtap = dev_get_drvdata(dev); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci clk_enable(rtap->clk); 1098c2ecf20Sopenharmony_ci rtc_time64_to_tm(readl(rtap->virtbase + COH901331_ALARM), &alarm->time); 1108c2ecf20Sopenharmony_ci alarm->pending = readl(rtap->virtbase + COH901331_IRQ_EVENT) & 1U; 1118c2ecf20Sopenharmony_ci alarm->enabled = readl(rtap->virtbase + COH901331_IRQ_MASK) & 1U; 1128c2ecf20Sopenharmony_ci clk_disable(rtap->clk); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci return 0; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic int coh901331_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci struct coh901331_port *rtap = dev_get_drvdata(dev); 1208c2ecf20Sopenharmony_ci unsigned long time = rtc_tm_to_time64(&alarm->time); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci clk_enable(rtap->clk); 1238c2ecf20Sopenharmony_ci writel(time, rtap->virtbase + COH901331_ALARM); 1248c2ecf20Sopenharmony_ci writel(alarm->enabled, rtap->virtbase + COH901331_IRQ_MASK); 1258c2ecf20Sopenharmony_ci clk_disable(rtap->clk); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci return 0; 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic int coh901331_alarm_irq_enable(struct device *dev, unsigned int enabled) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci struct coh901331_port *rtap = dev_get_drvdata(dev); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci clk_enable(rtap->clk); 1358c2ecf20Sopenharmony_ci if (enabled) 1368c2ecf20Sopenharmony_ci writel(1, rtap->virtbase + COH901331_IRQ_MASK); 1378c2ecf20Sopenharmony_ci else 1388c2ecf20Sopenharmony_ci writel(0, rtap->virtbase + COH901331_IRQ_MASK); 1398c2ecf20Sopenharmony_ci clk_disable(rtap->clk); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci return 0; 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic const struct rtc_class_ops coh901331_ops = { 1458c2ecf20Sopenharmony_ci .read_time = coh901331_read_time, 1468c2ecf20Sopenharmony_ci .set_time = coh901331_set_time, 1478c2ecf20Sopenharmony_ci .read_alarm = coh901331_read_alarm, 1488c2ecf20Sopenharmony_ci .set_alarm = coh901331_set_alarm, 1498c2ecf20Sopenharmony_ci .alarm_irq_enable = coh901331_alarm_irq_enable, 1508c2ecf20Sopenharmony_ci}; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic int __exit coh901331_remove(struct platform_device *pdev) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci struct coh901331_port *rtap = platform_get_drvdata(pdev); 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci if (rtap) 1578c2ecf20Sopenharmony_ci clk_unprepare(rtap->clk); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci return 0; 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_cistatic int __init coh901331_probe(struct platform_device *pdev) 1648c2ecf20Sopenharmony_ci{ 1658c2ecf20Sopenharmony_ci int ret; 1668c2ecf20Sopenharmony_ci struct coh901331_port *rtap; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci rtap = devm_kzalloc(&pdev->dev, 1698c2ecf20Sopenharmony_ci sizeof(struct coh901331_port), GFP_KERNEL); 1708c2ecf20Sopenharmony_ci if (!rtap) 1718c2ecf20Sopenharmony_ci return -ENOMEM; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci rtap->virtbase = devm_platform_ioremap_resource(pdev, 0); 1748c2ecf20Sopenharmony_ci if (IS_ERR(rtap->virtbase)) 1758c2ecf20Sopenharmony_ci return PTR_ERR(rtap->virtbase); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci rtap->irq = platform_get_irq(pdev, 0); 1788c2ecf20Sopenharmony_ci if (devm_request_irq(&pdev->dev, rtap->irq, coh901331_interrupt, 0, 1798c2ecf20Sopenharmony_ci "RTC COH 901 331 Alarm", rtap)) 1808c2ecf20Sopenharmony_ci return -EIO; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci rtap->clk = devm_clk_get(&pdev->dev, NULL); 1838c2ecf20Sopenharmony_ci if (IS_ERR(rtap->clk)) { 1848c2ecf20Sopenharmony_ci ret = PTR_ERR(rtap->clk); 1858c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "could not get clock\n"); 1868c2ecf20Sopenharmony_ci return ret; 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci rtap->rtc = devm_rtc_allocate_device(&pdev->dev); 1908c2ecf20Sopenharmony_ci if (IS_ERR(rtap->rtc)) 1918c2ecf20Sopenharmony_ci return PTR_ERR(rtap->rtc); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci rtap->rtc->ops = &coh901331_ops; 1948c2ecf20Sopenharmony_ci rtap->rtc->range_max = U32_MAX; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci /* We enable/disable the clock only to assure it works */ 1978c2ecf20Sopenharmony_ci ret = clk_prepare_enable(rtap->clk); 1988c2ecf20Sopenharmony_ci if (ret) { 1998c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "could not enable clock\n"); 2008c2ecf20Sopenharmony_ci return ret; 2018c2ecf20Sopenharmony_ci } 2028c2ecf20Sopenharmony_ci clk_disable(rtap->clk); 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, rtap); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci ret = rtc_register_device(rtap->rtc); 2078c2ecf20Sopenharmony_ci if (ret) 2088c2ecf20Sopenharmony_ci goto out_no_rtc; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci return 0; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci out_no_rtc: 2138c2ecf20Sopenharmony_ci clk_unprepare(rtap->clk); 2148c2ecf20Sopenharmony_ci return ret; 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 2188c2ecf20Sopenharmony_cistatic int coh901331_suspend(struct device *dev) 2198c2ecf20Sopenharmony_ci{ 2208c2ecf20Sopenharmony_ci struct coh901331_port *rtap = dev_get_drvdata(dev); 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci /* 2238c2ecf20Sopenharmony_ci * If this RTC alarm will be used for waking the system up, 2248c2ecf20Sopenharmony_ci * don't disable it of course. Else we just disable the alarm 2258c2ecf20Sopenharmony_ci * and await suspension. 2268c2ecf20Sopenharmony_ci */ 2278c2ecf20Sopenharmony_ci if (device_may_wakeup(dev)) { 2288c2ecf20Sopenharmony_ci enable_irq_wake(rtap->irq); 2298c2ecf20Sopenharmony_ci } else { 2308c2ecf20Sopenharmony_ci clk_enable(rtap->clk); 2318c2ecf20Sopenharmony_ci rtap->irqmaskstore = readl(rtap->virtbase + COH901331_IRQ_MASK); 2328c2ecf20Sopenharmony_ci writel(0, rtap->virtbase + COH901331_IRQ_MASK); 2338c2ecf20Sopenharmony_ci clk_disable(rtap->clk); 2348c2ecf20Sopenharmony_ci } 2358c2ecf20Sopenharmony_ci clk_unprepare(rtap->clk); 2368c2ecf20Sopenharmony_ci return 0; 2378c2ecf20Sopenharmony_ci} 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_cistatic int coh901331_resume(struct device *dev) 2408c2ecf20Sopenharmony_ci{ 2418c2ecf20Sopenharmony_ci int ret; 2428c2ecf20Sopenharmony_ci struct coh901331_port *rtap = dev_get_drvdata(dev); 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci ret = clk_prepare(rtap->clk); 2458c2ecf20Sopenharmony_ci if (ret) 2468c2ecf20Sopenharmony_ci return ret; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci if (device_may_wakeup(dev)) { 2498c2ecf20Sopenharmony_ci disable_irq_wake(rtap->irq); 2508c2ecf20Sopenharmony_ci } else { 2518c2ecf20Sopenharmony_ci clk_enable(rtap->clk); 2528c2ecf20Sopenharmony_ci writel(rtap->irqmaskstore, rtap->virtbase + COH901331_IRQ_MASK); 2538c2ecf20Sopenharmony_ci clk_disable(rtap->clk); 2548c2ecf20Sopenharmony_ci } 2558c2ecf20Sopenharmony_ci return 0; 2568c2ecf20Sopenharmony_ci} 2578c2ecf20Sopenharmony_ci#endif 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(coh901331_pm_ops, coh901331_suspend, coh901331_resume); 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_cistatic void coh901331_shutdown(struct platform_device *pdev) 2628c2ecf20Sopenharmony_ci{ 2638c2ecf20Sopenharmony_ci struct coh901331_port *rtap = platform_get_drvdata(pdev); 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci clk_enable(rtap->clk); 2668c2ecf20Sopenharmony_ci writel(0, rtap->virtbase + COH901331_IRQ_MASK); 2678c2ecf20Sopenharmony_ci clk_disable_unprepare(rtap->clk); 2688c2ecf20Sopenharmony_ci} 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_cistatic const struct of_device_id coh901331_dt_match[] = { 2718c2ecf20Sopenharmony_ci { .compatible = "stericsson,coh901331" }, 2728c2ecf20Sopenharmony_ci {}, 2738c2ecf20Sopenharmony_ci}; 2748c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, coh901331_dt_match); 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_cistatic struct platform_driver coh901331_driver = { 2778c2ecf20Sopenharmony_ci .driver = { 2788c2ecf20Sopenharmony_ci .name = "rtc-coh901331", 2798c2ecf20Sopenharmony_ci .pm = &coh901331_pm_ops, 2808c2ecf20Sopenharmony_ci .of_match_table = coh901331_dt_match, 2818c2ecf20Sopenharmony_ci }, 2828c2ecf20Sopenharmony_ci .remove = __exit_p(coh901331_remove), 2838c2ecf20Sopenharmony_ci .shutdown = coh901331_shutdown, 2848c2ecf20Sopenharmony_ci}; 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_cimodule_platform_driver_probe(coh901331_driver, coh901331_probe); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ciMODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>"); 2898c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ST-Ericsson AB COH 901 331 RTC Driver"); 2908c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 291