^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * A RTC driver for the Simtek STK17TA8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * By Thomas Hommel <thomas.hommel@ge.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Based on the DS1553 driver from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Atsushi Nemoto <anemo@mba.ocn.ne.jp>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/bcd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define RTC_REG_SIZE 0x20000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define RTC_OFFSET 0x1fff0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define RTC_FLAGS (RTC_OFFSET + 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define RTC_CENTURY (RTC_OFFSET + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define RTC_SECONDS_ALARM (RTC_OFFSET + 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define RTC_MINUTES_ALARM (RTC_OFFSET + 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define RTC_HOURS_ALARM (RTC_OFFSET + 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define RTC_DATE_ALARM (RTC_OFFSET + 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define RTC_INTERRUPTS (RTC_OFFSET + 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define RTC_WATCHDOG (RTC_OFFSET + 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define RTC_CALIBRATION (RTC_OFFSET + 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define RTC_SECONDS (RTC_OFFSET + 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define RTC_MINUTES (RTC_OFFSET + 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define RTC_HOURS (RTC_OFFSET + 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define RTC_DAY (RTC_OFFSET + 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define RTC_DATE (RTC_OFFSET + 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define RTC_MONTH (RTC_OFFSET + 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define RTC_YEAR (RTC_OFFSET + 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define RTC_SECONDS_MASK 0x7f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define RTC_DAY_MASK 0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define RTC_CAL_MASK 0x3f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Bits in the Calibration register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define RTC_STOP 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Bits in the Flags register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define RTC_FLAGS_AF 0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define RTC_FLAGS_PF 0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define RTC_WRITE 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define RTC_READ 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* Bits in the Interrupts register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define RTC_INTS_AIE 0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct rtc_plat_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) void __iomem *ioaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) unsigned long last_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) unsigned int irqen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int alrm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int alrm_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int alrm_hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int alrm_mday;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int stk17ta8_rtc_set_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct rtc_plat_data *pdata = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) void __iomem *ioaddr = pdata->ioaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) u8 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) flags = readb(pdata->ioaddr + RTC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) writeb(flags | RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) writeb(bin2bcd((tm->tm_year + 1900) / 100), ioaddr + RTC_CENTURY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) writeb(flags & ~RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int stk17ta8_rtc_read_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct rtc_plat_data *pdata = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) void __iomem *ioaddr = pdata->ioaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) unsigned int year, month, day, hour, minute, second, week;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) unsigned int century;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) u8 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* give enough time to update RTC in case of continuous read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (pdata->last_jiffies == jiffies)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) msleep(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) pdata->last_jiffies = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) flags = readb(pdata->ioaddr + RTC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) writeb(flags | RTC_READ, ioaddr + RTC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) minute = readb(ioaddr + RTC_MINUTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) hour = readb(ioaddr + RTC_HOURS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) day = readb(ioaddr + RTC_DATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) month = readb(ioaddr + RTC_MONTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) year = readb(ioaddr + RTC_YEAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) century = readb(ioaddr + RTC_CENTURY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) writeb(flags & ~RTC_READ, ioaddr + RTC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) tm->tm_sec = bcd2bin(second);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) tm->tm_min = bcd2bin(minute);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) tm->tm_hour = bcd2bin(hour);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) tm->tm_mday = bcd2bin(day);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) tm->tm_wday = bcd2bin(week);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) tm->tm_mon = bcd2bin(month) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* year is 1900 + tm->tm_year */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void stk17ta8_rtc_update_alarm(struct rtc_plat_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) void __iomem *ioaddr = pdata->ioaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) unsigned long irqflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) u8 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) spin_lock_irqsave(&pdata->lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) flags = readb(ioaddr + RTC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 0x80 : bin2bcd(pdata->alrm_mday),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ioaddr + RTC_DATE_ALARM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 0x80 : bin2bcd(pdata->alrm_hour),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) ioaddr + RTC_HOURS_ALARM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 0x80 : bin2bcd(pdata->alrm_min),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) ioaddr + RTC_MINUTES_ALARM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 0x80 : bin2bcd(pdata->alrm_sec),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) ioaddr + RTC_SECONDS_ALARM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) writeb(pdata->irqen ? RTC_INTS_AIE : 0, ioaddr + RTC_INTERRUPTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) readb(ioaddr + RTC_FLAGS); /* clear interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) spin_unlock_irqrestore(&pdata->lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int stk17ta8_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct rtc_plat_data *pdata = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (pdata->irq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) pdata->alrm_mday = alrm->time.tm_mday;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) pdata->alrm_hour = alrm->time.tm_hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) pdata->alrm_min = alrm->time.tm_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) pdata->alrm_sec = alrm->time.tm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (alrm->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) pdata->irqen |= RTC_AF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) stk17ta8_rtc_update_alarm(pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int stk17ta8_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct rtc_plat_data *pdata = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (pdata->irq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static irqreturn_t stk17ta8_rtc_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct platform_device *pdev = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) void __iomem *ioaddr = pdata->ioaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) unsigned long events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) spin_lock(&pdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /* read and clear interrupt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) events = RTC_IRQF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) events |= RTC_UF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) events |= RTC_AF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) rtc_update_irq(pdata->rtc, 1, events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) spin_unlock(&pdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return events ? IRQ_HANDLED : IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static int stk17ta8_rtc_alarm_irq_enable(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) unsigned int enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct rtc_plat_data *pdata = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (pdata->irq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) pdata->irqen |= RTC_AF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) pdata->irqen &= ~RTC_AF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) stk17ta8_rtc_update_alarm(pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static const struct rtc_class_ops stk17ta8_rtc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) .read_time = stk17ta8_rtc_read_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) .set_time = stk17ta8_rtc_set_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) .read_alarm = stk17ta8_rtc_read_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) .set_alarm = stk17ta8_rtc_set_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) .alarm_irq_enable = stk17ta8_rtc_alarm_irq_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int stk17ta8_nvram_read(void *priv, unsigned int pos, void *val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) size_t bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct rtc_plat_data *pdata = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) void __iomem *ioaddr = pdata->ioaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) u8 *buf = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) for (; bytes; bytes--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) *buf++ = readb(ioaddr + pos++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static int stk17ta8_nvram_write(void *priv, unsigned int pos, void *val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) size_t bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct rtc_plat_data *pdata = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) void __iomem *ioaddr = pdata->ioaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) u8 *buf = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) for (; bytes; bytes--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) writeb(*buf++, ioaddr + pos++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static int stk17ta8_rtc_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) unsigned int cal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct rtc_plat_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) void __iomem *ioaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct nvmem_config nvmem_cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) .name = "stk17ta8_nvram",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) .word_size = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) .stride = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) .size = RTC_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) .reg_read = stk17ta8_nvram_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) .reg_write = stk17ta8_nvram_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) ioaddr = devm_platform_ioremap_resource(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (IS_ERR(ioaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return PTR_ERR(ioaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) pdata->ioaddr = ioaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) pdata->irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /* turn RTC on if it was not on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) cal = readb(ioaddr + RTC_CALIBRATION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (cal & RTC_STOP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) cal &= RTC_CAL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) flags = readb(ioaddr + RTC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) writeb(cal, ioaddr + RTC_CALIBRATION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_PF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) dev_warn(&pdev->dev, "voltage-low detected.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) spin_lock_init(&pdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) pdata->last_jiffies = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) platform_set_drvdata(pdev, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (pdata->irq > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) writeb(0, ioaddr + RTC_INTERRUPTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (devm_request_irq(&pdev->dev, pdata->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) stk17ta8_rtc_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) IRQF_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) pdev->name, pdev) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) dev_warn(&pdev->dev, "interrupt not available.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) pdata->irq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) pdata->rtc = devm_rtc_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (IS_ERR(pdata->rtc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return PTR_ERR(pdata->rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) pdata->rtc->ops = &stk17ta8_rtc_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) pdata->rtc->nvram_old_abi = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) nvmem_cfg.priv = pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) ret = rtc_nvmem_register(pdata->rtc, &nvmem_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return rtc_register_device(pdata->rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /* work with hotplug and coldplug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) MODULE_ALIAS("platform:stk17ta8");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static struct platform_driver stk17ta8_rtc_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) .probe = stk17ta8_rtc_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) .name = "stk17ta8",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) module_platform_driver(stk17ta8_rtc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) MODULE_AUTHOR("Thomas Hommel <thomas.hommel@ge.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) MODULE_DESCRIPTION("Simtek STK17TA8 RTC driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) MODULE_LICENSE("GPL");