Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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)  * rtc-mrst.c: Driver for Moorestown virtual RTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * (C) Copyright 2009 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Jacob Pan (jacob.jun.pan@intel.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *	   Feng Tang (feng.tang@intel.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Note:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * VRTC is emulated by system controller firmware, the real HW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * RTC is located in the PMIC device. SCU FW shadows PMIC RTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * in a memory mapped IO space that is visible to the host IA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * processor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * This driver is based upon drivers/rtc/rtc-cmos.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * Note:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *  * vRTC only supports binary mode and 24H mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *  * vRTC only support PIE and AIE, no UIE, and its PIE only happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *    at 23:59:59pm everyday, no support for adjustable frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *  * Alarm function is also limited to hr/min/sec.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/mod_devicetable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/mc146818rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/sfi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <asm/intel_scu_ipc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <asm/intel-mid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <asm/intel_mid_vrtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) struct mrst_rtc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct rtc_device	*rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct device		*dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int			irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	u8			enabled_wake;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	u8			suspend_ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static const char driver_name[] = "rtc_mrst";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define	RTC_IRQMASK	(RTC_PF | RTC_AF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static inline int is_intr(u8 rtc_intr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (!(rtc_intr & RTC_IRQF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return rtc_intr & RTC_IRQMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static inline unsigned char vrtc_is_updating(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	unsigned char uip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	spin_lock_irqsave(&rtc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	uip = (vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	spin_unlock_irqrestore(&rtc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return uip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^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)  * rtc_time's year contains the increment over 1900, but vRTC's YEAR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * register can't be programmed to value larger than 0x64, so vRTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * driver chose to use 1972 (1970 is UNIX time start point) as the base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * and does the translation at read/write time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * Why not just use 1970 as the offset? it's because using 1972 will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * make it consistent in leap year setting for both vrtc and low-level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  * physical rtc devices. Then why not use 1960 as the offset? If we use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  * 1960, for a device's first use, its YEAR register is 0 and the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * year will be parsed as 1960 which is not a valid UNIX time and will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * cause many applications to fail mysteriously.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static int mrst_read_time(struct device *dev, struct rtc_time *time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (vrtc_is_updating())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	spin_lock_irqsave(&rtc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	time->tm_sec = vrtc_cmos_read(RTC_SECONDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	time->tm_min = vrtc_cmos_read(RTC_MINUTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	time->tm_hour = vrtc_cmos_read(RTC_HOURS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	time->tm_mday = vrtc_cmos_read(RTC_DAY_OF_MONTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	time->tm_mon = vrtc_cmos_read(RTC_MONTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	time->tm_year = vrtc_cmos_read(RTC_YEAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	spin_unlock_irqrestore(&rtc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	/* Adjust for the 1972/1900 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	time->tm_year += 72;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	time->tm_mon--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int mrst_set_time(struct device *dev, struct rtc_time *time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	unsigned char mon, day, hrs, min, sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	unsigned int yrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	yrs = time->tm_year;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	mon = time->tm_mon + 1;   /* tm_mon starts at zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	day = time->tm_mday;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	hrs = time->tm_hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	min = time->tm_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	sec = time->tm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (yrs < 72 || yrs > 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	yrs -= 72;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	spin_lock_irqsave(&rtc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	vrtc_cmos_write(yrs, RTC_YEAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	vrtc_cmos_write(mon, RTC_MONTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	vrtc_cmos_write(day, RTC_DAY_OF_MONTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	vrtc_cmos_write(hrs, RTC_HOURS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	vrtc_cmos_write(min, RTC_MINUTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	vrtc_cmos_write(sec, RTC_SECONDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	spin_unlock_irqrestore(&rtc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETTIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int mrst_read_alarm(struct device *dev, struct rtc_wkalrm *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct mrst_rtc	*mrst = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	unsigned char rtc_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (mrst->irq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	/* vRTC only supports binary mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	spin_lock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	t->time.tm_sec = vrtc_cmos_read(RTC_SECONDS_ALARM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	t->time.tm_min = vrtc_cmos_read(RTC_MINUTES_ALARM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	t->time.tm_hour = vrtc_cmos_read(RTC_HOURS_ALARM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	rtc_control = vrtc_cmos_read(RTC_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	spin_unlock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	t->enabled = !!(rtc_control & RTC_AIE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	t->pending = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static void mrst_checkintr(struct mrst_rtc *mrst, unsigned char rtc_control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	unsigned char	rtc_intr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	 * allegedly some older rtcs need that to handle irqs properly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	rtc_intr = vrtc_cmos_read(RTC_INTR_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (is_intr(rtc_intr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		rtc_update_irq(mrst->rtc, 1, rtc_intr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static void mrst_irq_enable(struct mrst_rtc *mrst, unsigned char mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	unsigned char	rtc_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	 * Flush any pending IRQ status, notably for update irqs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	 * before we enable new IRQs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	rtc_control = vrtc_cmos_read(RTC_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	mrst_checkintr(mrst, rtc_control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	rtc_control |= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	vrtc_cmos_write(rtc_control, RTC_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	mrst_checkintr(mrst, rtc_control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static void mrst_irq_disable(struct mrst_rtc *mrst, unsigned char mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	unsigned char	rtc_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	rtc_control = vrtc_cmos_read(RTC_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	rtc_control &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	vrtc_cmos_write(rtc_control, RTC_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	mrst_checkintr(mrst, rtc_control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int mrst_set_alarm(struct device *dev, struct rtc_wkalrm *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	struct mrst_rtc	*mrst = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	unsigned char hrs, min, sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	if (!mrst->irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	hrs = t->time.tm_hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	min = t->time.tm_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	sec = t->time.tm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	spin_lock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	/* Next rtc irq must not be from previous alarm setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	mrst_irq_disable(mrst, RTC_AIE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	/* Update alarm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	vrtc_cmos_write(hrs, RTC_HOURS_ALARM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	vrtc_cmos_write(min, RTC_MINUTES_ALARM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	vrtc_cmos_write(sec, RTC_SECONDS_ALARM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	spin_unlock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETALARM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	spin_lock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (t->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		mrst_irq_enable(mrst, RTC_AIE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	spin_unlock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /* Currently, the vRTC doesn't support UIE ON/OFF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static int mrst_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct mrst_rtc	*mrst = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	unsigned long	flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	spin_lock_irqsave(&rtc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		mrst_irq_enable(mrst, RTC_AIE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		mrst_irq_disable(mrst, RTC_AIE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	spin_unlock_irqrestore(&rtc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) #if IS_ENABLED(CONFIG_RTC_INTF_PROC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static int mrst_procfs(struct device *dev, struct seq_file *seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	unsigned char	rtc_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	spin_lock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	rtc_control = vrtc_cmos_read(RTC_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	spin_unlock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	seq_printf(seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		   "periodic_IRQ\t: %s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		   "alarm\t\t: %s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		   "BCD\t\t: no\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		   "periodic_freq\t: daily (not adjustable)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		   (rtc_control & RTC_PIE) ? "on" : "off",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		   (rtc_control & RTC_AIE) ? "on" : "off");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) #define	mrst_procfs	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static const struct rtc_class_ops mrst_rtc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	.read_time	= mrst_read_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	.set_time	= mrst_set_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	.read_alarm	= mrst_read_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	.set_alarm	= mrst_set_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	.proc		= mrst_procfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	.alarm_irq_enable = mrst_rtc_alarm_irq_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static struct mrst_rtc	mrst_rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  * Reg B, so no need for this driver to clear it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static irqreturn_t mrst_rtc_irq(int irq, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	u8 irqstat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	spin_lock(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	/* This read will clear all IRQ flags inside Reg C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	irqstat = vrtc_cmos_read(RTC_INTR_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	spin_unlock(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	irqstat &= RTC_IRQMASK | RTC_IRQF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (is_intr(irqstat)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		rtc_update_irq(p, 1, irqstat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static int vrtc_mrst_do_probe(struct device *dev, struct resource *iomem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			      int rtc_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	unsigned char rtc_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	/* There can be only one ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (mrst_rtc.dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (!iomem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	iomem = devm_request_mem_region(dev, iomem->start, resource_size(iomem),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 					driver_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	if (!iomem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		dev_dbg(dev, "i/o mem already in use.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		return -EBUSY;
^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) 	mrst_rtc.irq = rtc_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	mrst_rtc.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	dev_set_drvdata(dev, &mrst_rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	mrst_rtc.rtc = devm_rtc_allocate_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (IS_ERR(mrst_rtc.rtc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		return PTR_ERR(mrst_rtc.rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	mrst_rtc.rtc->ops = &mrst_rtc_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	rename_region(iomem, dev_name(&mrst_rtc.rtc->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	spin_lock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	mrst_irq_disable(&mrst_rtc, RTC_PIE | RTC_AIE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	rtc_control = vrtc_cmos_read(RTC_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	spin_unlock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		dev_dbg(dev, "TODO: support more than 24-hr BCD mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if (rtc_irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		retval = devm_request_irq(dev, rtc_irq, mrst_rtc_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 					  0, dev_name(&mrst_rtc.rtc->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 					  mrst_rtc.rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			dev_dbg(dev, "IRQ %d is already in use, err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 				rtc_irq, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			goto cleanup0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	retval = rtc_register_device(mrst_rtc.rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		goto cleanup0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	dev_dbg(dev, "initialised\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) cleanup0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	mrst_rtc.dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	dev_err(dev, "rtc-mrst: unable to initialise\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static void rtc_mrst_do_shutdown(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	spin_lock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	mrst_irq_disable(&mrst_rtc, RTC_IRQMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	spin_unlock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) static void rtc_mrst_do_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	struct mrst_rtc	*mrst = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	rtc_mrst_do_shutdown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	mrst->rtc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	mrst->dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static int mrst_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	struct mrst_rtc	*mrst = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	unsigned char	tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	/* Only the alarm might be a wakeup event source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	spin_lock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	mrst->suspend_ctrl = tmp = vrtc_cmos_read(RTC_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	if (tmp & (RTC_PIE | RTC_AIE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		unsigned char	mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			mask = RTC_IRQMASK & ~RTC_AIE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			mask = RTC_IRQMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		tmp &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		vrtc_cmos_write(tmp, RTC_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		mrst_checkintr(mrst, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	spin_unlock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	if (tmp & RTC_AIE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		mrst->enabled_wake = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		enable_irq_wake(mrst->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	dev_dbg(&mrst_rtc.rtc->dev, "suspend%s, ctrl %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 			(tmp & RTC_AIE) ? ", alarm may wake" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 			tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)  * We want RTC alarms to wake us from the deep power saving state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static inline int mrst_poweroff(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	return mrst_suspend(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static int mrst_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	struct mrst_rtc	*mrst = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	unsigned char tmp = mrst->suspend_ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	/* Re-enable any irqs previously active */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (tmp & RTC_IRQMASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		unsigned char	mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		if (mrst->enabled_wake) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			disable_irq_wake(mrst->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			mrst->enabled_wake = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		spin_lock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			vrtc_cmos_write(tmp, RTC_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			mask = vrtc_cmos_read(RTC_INTR_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			if (!is_intr(mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			rtc_update_irq(mrst->rtc, 1, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			tmp &= ~RTC_AIE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		} while (mask & RTC_AIE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		spin_unlock_irq(&rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	dev_dbg(&mrst_rtc.rtc->dev, "resume, ctrl %02x\n", tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static SIMPLE_DEV_PM_OPS(mrst_pm_ops, mrst_suspend, mrst_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) #define MRST_PM_OPS (&mrst_pm_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) #define MRST_PM_OPS NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static inline int mrst_poweroff(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static int vrtc_mrst_platform_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	return vrtc_mrst_do_probe(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			platform_get_resource(pdev, IORESOURCE_MEM, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			platform_get_irq(pdev, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) static int vrtc_mrst_platform_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	rtc_mrst_do_remove(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static void vrtc_mrst_platform_shutdown(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	if (system_state == SYSTEM_POWER_OFF && !mrst_poweroff(&pdev->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	rtc_mrst_do_shutdown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) MODULE_ALIAS("platform:vrtc_mrst");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) static struct platform_driver vrtc_mrst_platform_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	.probe		= vrtc_mrst_platform_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	.remove		= vrtc_mrst_platform_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	.shutdown	= vrtc_mrst_platform_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		.name	= driver_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		.pm	= MRST_PM_OPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) module_platform_driver(vrtc_mrst_platform_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) MODULE_AUTHOR("Jacob Pan; Feng Tang");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) MODULE_DESCRIPTION("Driver for Moorestown virtual RTC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) MODULE_LICENSE("GPL");