1 // SPDX-License-Identifier: GPL-2.0
 
   2 /* drivers/rtc/rtc-goldfish.c
 
   4  * Copyright (C) 2007 Google, Inc.
 
   5  * Copyright (C) 2017 Imagination Technologies Ltd.
 
   9 #include <linux/module.h>
 
  11 #include <linux/platform_device.h>
 
  12 #include <linux/rtc.h>
 
  14 #define TIMER_TIME_LOW          0x00    /* get low bits of current time  */
 
  15                                         /*   and update TIMER_TIME_HIGH  */
 
  16 #define TIMER_TIME_HIGH 0x04    /* get high bits of time at last */
 
  17                                         /*   TIMER_TIME_LOW read         */
 
  18 #define TIMER_ALARM_LOW 0x08    /* set low bits of alarm and     */
 
  20 #define TIMER_ALARM_HIGH        0x0c    /* set high bits of next alarm   */
 
  21 #define TIMER_IRQ_ENABLED       0x10
 
  22 #define TIMER_CLEAR_ALARM       0x14
 
  23 #define TIMER_ALARM_STATUS      0x18
 
  24 #define TIMER_CLEAR_INTERRUPT   0x1c
 
  29         struct rtc_device *rtc;
 
  32 static int goldfish_rtc_read_alarm(struct device *dev,
 
  33                                    struct rtc_wkalrm *alrm)
 
  39         struct goldfish_rtc *rtcdrv;
 
  41         rtcdrv = dev_get_drvdata(dev);
 
  44         rtc_alarm_low = readl(base + TIMER_ALARM_LOW);
 
  45         rtc_alarm_high = readl(base + TIMER_ALARM_HIGH);
 
  46         rtc_alarm = (rtc_alarm_high << 32) | rtc_alarm_low;
 
  48         do_div(rtc_alarm, NSEC_PER_SEC);
 
  49         memset(alrm, 0, sizeof(struct rtc_wkalrm));
 
  51         rtc_time64_to_tm(rtc_alarm, &alrm->time);
 
  53         if (readl(base + TIMER_ALARM_STATUS))
 
  61 static int goldfish_rtc_set_alarm(struct device *dev,
 
  62                                   struct rtc_wkalrm *alrm)
 
  64         struct goldfish_rtc *rtcdrv;
 
  69         rtcdrv = dev_get_drvdata(dev);
 
  73                 rtc_alarm64 = rtc_tm_to_time64(&alrm->time) * NSEC_PER_SEC;
 
  74                 writel((rtc_alarm64 >> 32), base + TIMER_ALARM_HIGH);
 
  75                 writel(rtc_alarm64, base + TIMER_ALARM_LOW);
 
  78                  * if this function was called with enabled=0
 
  79                  * then it could mean that the application is
 
  80                  * trying to cancel an ongoing alarm
 
  82                 rtc_status_reg = readl(base + TIMER_ALARM_STATUS);
 
  84                         writel(1, base + TIMER_CLEAR_ALARM);
 
  90 static int goldfish_rtc_alarm_irq_enable(struct device *dev,
 
  94         struct goldfish_rtc *rtcdrv;
 
  96         rtcdrv = dev_get_drvdata(dev);
 
 100                 writel(1, base + TIMER_IRQ_ENABLED);
 
 102                 writel(0, base + TIMER_IRQ_ENABLED);
 
 107 static irqreturn_t goldfish_rtc_interrupt(int irq, void *dev_id)
 
 109         struct goldfish_rtc *rtcdrv = dev_id;
 
 110         void __iomem *base = rtcdrv->base;
 
 112         writel(1, base + TIMER_CLEAR_INTERRUPT);
 
 114         rtc_update_irq(rtcdrv->rtc, 1, RTC_IRQF | RTC_AF);
 
 119 static int goldfish_rtc_read_time(struct device *dev, struct rtc_time *tm)
 
 121         struct goldfish_rtc *rtcdrv;
 
 127         rtcdrv = dev_get_drvdata(dev);
 
 130         time_low = readl(base + TIMER_TIME_LOW);
 
 131         time_high = readl(base + TIMER_TIME_HIGH);
 
 132         time = (time_high << 32) | time_low;
 
 134         do_div(time, NSEC_PER_SEC);
 
 136         rtc_time64_to_tm(time, tm);
 
 141 static int goldfish_rtc_set_time(struct device *dev, struct rtc_time *tm)
 
 143         struct goldfish_rtc *rtcdrv;
 
 147         rtcdrv = dev_get_drvdata(dev);
 
 150         now64 = rtc_tm_to_time64(tm) * NSEC_PER_SEC;
 
 151         writel((now64 >> 32), base + TIMER_TIME_HIGH);
 
 152         writel(now64, base + TIMER_TIME_LOW);
 
 157 static const struct rtc_class_ops goldfish_rtc_ops = {
 
 158         .read_time      = goldfish_rtc_read_time,
 
 159         .set_time       = goldfish_rtc_set_time,
 
 160         .read_alarm     = goldfish_rtc_read_alarm,
 
 161         .set_alarm      = goldfish_rtc_set_alarm,
 
 162         .alarm_irq_enable = goldfish_rtc_alarm_irq_enable
 
 165 static int goldfish_rtc_probe(struct platform_device *pdev)
 
 167         struct goldfish_rtc *rtcdrv;
 
 170         rtcdrv = devm_kzalloc(&pdev->dev, sizeof(*rtcdrv), GFP_KERNEL);
 
 174         platform_set_drvdata(pdev, rtcdrv);
 
 175         rtcdrv->base = devm_platform_ioremap_resource(pdev, 0);
 
 176         if (IS_ERR(rtcdrv->base))
 
 177                 return PTR_ERR(rtcdrv->base);
 
 179         rtcdrv->irq = platform_get_irq(pdev, 0);
 
 183         rtcdrv->rtc = devm_rtc_allocate_device(&pdev->dev);
 
 184         if (IS_ERR(rtcdrv->rtc))
 
 185                 return PTR_ERR(rtcdrv->rtc);
 
 187         rtcdrv->rtc->ops = &goldfish_rtc_ops;
 
 188         rtcdrv->rtc->range_max = U64_MAX / NSEC_PER_SEC;
 
 190         err = devm_request_irq(&pdev->dev, rtcdrv->irq,
 
 191                                goldfish_rtc_interrupt,
 
 192                                0, pdev->name, rtcdrv);
 
 196         return rtc_register_device(rtcdrv->rtc);
 
 199 static const struct of_device_id goldfish_rtc_of_match[] = {
 
 200         { .compatible = "google,goldfish-rtc", },
 
 203 MODULE_DEVICE_TABLE(of, goldfish_rtc_of_match);
 
 205 static struct platform_driver goldfish_rtc = {
 
 206         .probe = goldfish_rtc_probe,
 
 208                 .name = "goldfish_rtc",
 
 209                 .of_match_table = goldfish_rtc_of_match,
 
 213 module_platform_driver(goldfish_rtc);
 
 215 MODULE_LICENSE("GPL v2");