18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * DaVinci Power Management and Real Time Clock Driver for TI platforms
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2009 Texas Instruments, Inc
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/ioport.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
158c2ecf20Sopenharmony_ci#include <linux/rtc.h>
168c2ecf20Sopenharmony_ci#include <linux/bcd.h>
178c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
188c2ecf20Sopenharmony_ci#include <linux/io.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * The DaVinci RTC is a simple RTC with the following
238c2ecf20Sopenharmony_ci * Sec: 0 - 59 : BCD count
248c2ecf20Sopenharmony_ci * Min: 0 - 59 : BCD count
258c2ecf20Sopenharmony_ci * Hour: 0 - 23 : BCD count
268c2ecf20Sopenharmony_ci * Day: 0 - 0x7FFF(32767) : Binary count ( Over 89 years )
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* PRTC interface registers */
308c2ecf20Sopenharmony_ci#define DAVINCI_PRTCIF_PID		0x00
318c2ecf20Sopenharmony_ci#define PRTCIF_CTLR			0x04
328c2ecf20Sopenharmony_ci#define PRTCIF_LDATA			0x08
338c2ecf20Sopenharmony_ci#define PRTCIF_UDATA			0x0C
348c2ecf20Sopenharmony_ci#define PRTCIF_INTEN			0x10
358c2ecf20Sopenharmony_ci#define PRTCIF_INTFLG			0x14
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* PRTCIF_CTLR bit fields */
388c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_BUSY		BIT(31)
398c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_SIZE		BIT(25)
408c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_DIR			BIT(24)
418c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_BENU_MSB		BIT(23)
428c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_BENU_3RD_BYTE	BIT(22)
438c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_BENU_2ND_BYTE	BIT(21)
448c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_BENU_LSB		BIT(20)
458c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_BENU_MASK		(0x00F00000)
468c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_BENL_MSB		BIT(19)
478c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_BENL_3RD_BYTE	BIT(18)
488c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_BENL_2ND_BYTE	BIT(17)
498c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_BENL_LSB		BIT(16)
508c2ecf20Sopenharmony_ci#define PRTCIF_CTLR_BENL_MASK		(0x000F0000)
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* PRTCIF_INTEN bit fields */
538c2ecf20Sopenharmony_ci#define PRTCIF_INTEN_RTCSS		BIT(1)
548c2ecf20Sopenharmony_ci#define PRTCIF_INTEN_RTCIF		BIT(0)
558c2ecf20Sopenharmony_ci#define PRTCIF_INTEN_MASK		(PRTCIF_INTEN_RTCSS \
568c2ecf20Sopenharmony_ci					| PRTCIF_INTEN_RTCIF)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* PRTCIF_INTFLG bit fields */
598c2ecf20Sopenharmony_ci#define PRTCIF_INTFLG_RTCSS		BIT(1)
608c2ecf20Sopenharmony_ci#define PRTCIF_INTFLG_RTCIF		BIT(0)
618c2ecf20Sopenharmony_ci#define PRTCIF_INTFLG_MASK		(PRTCIF_INTFLG_RTCSS \
628c2ecf20Sopenharmony_ci					| PRTCIF_INTFLG_RTCIF)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/* PRTC subsystem registers */
658c2ecf20Sopenharmony_ci#define PRTCSS_RTC_INTC_EXTENA1		(0x0C)
668c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CTRL			(0x10)
678c2ecf20Sopenharmony_ci#define PRTCSS_RTC_WDT			(0x11)
688c2ecf20Sopenharmony_ci#define PRTCSS_RTC_TMR0			(0x12)
698c2ecf20Sopenharmony_ci#define PRTCSS_RTC_TMR1			(0x13)
708c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CCTRL		(0x14)
718c2ecf20Sopenharmony_ci#define PRTCSS_RTC_SEC			(0x15)
728c2ecf20Sopenharmony_ci#define PRTCSS_RTC_MIN			(0x16)
738c2ecf20Sopenharmony_ci#define PRTCSS_RTC_HOUR			(0x17)
748c2ecf20Sopenharmony_ci#define PRTCSS_RTC_DAY0			(0x18)
758c2ecf20Sopenharmony_ci#define PRTCSS_RTC_DAY1			(0x19)
768c2ecf20Sopenharmony_ci#define PRTCSS_RTC_AMIN			(0x1A)
778c2ecf20Sopenharmony_ci#define PRTCSS_RTC_AHOUR		(0x1B)
788c2ecf20Sopenharmony_ci#define PRTCSS_RTC_ADAY0		(0x1C)
798c2ecf20Sopenharmony_ci#define PRTCSS_RTC_ADAY1		(0x1D)
808c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CLKC_CNT		(0x20)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/* PRTCSS_RTC_INTC_EXTENA1 */
838c2ecf20Sopenharmony_ci#define PRTCSS_RTC_INTC_EXTENA1_MASK	(0x07)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/* PRTCSS_RTC_CTRL bit fields */
868c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CTRL_WDTBUS		BIT(7)
878c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CTRL_WEN		BIT(6)
888c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CTRL_WDRT		BIT(5)
898c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CTRL_WDTFLG		BIT(4)
908c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CTRL_TE		BIT(3)
918c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CTRL_TIEN		BIT(2)
928c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CTRL_TMRFLG		BIT(1)
938c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CTRL_TMMD		BIT(0)
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/* PRTCSS_RTC_CCTRL bit fields */
968c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CCTRL_CALBUSY	BIT(7)
978c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CCTRL_DAEN		BIT(5)
988c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CCTRL_HAEN		BIT(4)
998c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CCTRL_MAEN		BIT(3)
1008c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CCTRL_ALMFLG		BIT(2)
1018c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CCTRL_AIEN		BIT(1)
1028c2ecf20Sopenharmony_ci#define PRTCSS_RTC_CCTRL_CAEN		BIT(0)
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(davinci_rtc_lock);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistruct davinci_rtc {
1078c2ecf20Sopenharmony_ci	struct rtc_device		*rtc;
1088c2ecf20Sopenharmony_ci	void __iomem			*base;
1098c2ecf20Sopenharmony_ci	int				irq;
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic inline void rtcif_write(struct davinci_rtc *davinci_rtc,
1138c2ecf20Sopenharmony_ci			       u32 val, u32 addr)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	writel(val, davinci_rtc->base + addr);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic inline u32 rtcif_read(struct davinci_rtc *davinci_rtc, u32 addr)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	return readl(davinci_rtc->base + addr);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic inline void rtcif_wait(struct davinci_rtc *davinci_rtc)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	while (rtcif_read(davinci_rtc, PRTCIF_CTLR) & PRTCIF_CTLR_BUSY)
1268c2ecf20Sopenharmony_ci		cpu_relax();
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic inline void rtcss_write(struct davinci_rtc *davinci_rtc,
1308c2ecf20Sopenharmony_ci			       unsigned long val, u8 addr)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	rtcif_wait(davinci_rtc);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	rtcif_write(davinci_rtc, PRTCIF_CTLR_BENL_LSB | addr, PRTCIF_CTLR);
1358c2ecf20Sopenharmony_ci	rtcif_write(davinci_rtc, val, PRTCIF_LDATA);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	rtcif_wait(davinci_rtc);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic inline u8 rtcss_read(struct davinci_rtc *davinci_rtc, u8 addr)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	rtcif_wait(davinci_rtc);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	rtcif_write(davinci_rtc, PRTCIF_CTLR_DIR | PRTCIF_CTLR_BENL_LSB | addr,
1458c2ecf20Sopenharmony_ci		    PRTCIF_CTLR);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	rtcif_wait(davinci_rtc);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	return rtcif_read(davinci_rtc, PRTCIF_LDATA);
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic inline void davinci_rtcss_calendar_wait(struct davinci_rtc *davinci_rtc)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	while (rtcss_read(davinci_rtc, PRTCSS_RTC_CCTRL) &
1558c2ecf20Sopenharmony_ci	       PRTCSS_RTC_CCTRL_CALBUSY)
1568c2ecf20Sopenharmony_ci		cpu_relax();
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic irqreturn_t davinci_rtc_interrupt(int irq, void *class_dev)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	struct davinci_rtc *davinci_rtc = class_dev;
1628c2ecf20Sopenharmony_ci	unsigned long events = 0;
1638c2ecf20Sopenharmony_ci	u32 irq_flg;
1648c2ecf20Sopenharmony_ci	u8 alm_irq, tmr_irq;
1658c2ecf20Sopenharmony_ci	u8 rtc_ctrl, rtc_cctrl;
1668c2ecf20Sopenharmony_ci	int ret = IRQ_NONE;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	irq_flg = rtcif_read(davinci_rtc, PRTCIF_INTFLG) &
1698c2ecf20Sopenharmony_ci		  PRTCIF_INTFLG_RTCSS;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	alm_irq = rtcss_read(davinci_rtc, PRTCSS_RTC_CCTRL) &
1728c2ecf20Sopenharmony_ci		  PRTCSS_RTC_CCTRL_ALMFLG;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	tmr_irq = rtcss_read(davinci_rtc, PRTCSS_RTC_CTRL) &
1758c2ecf20Sopenharmony_ci		  PRTCSS_RTC_CTRL_TMRFLG;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	if (irq_flg) {
1788c2ecf20Sopenharmony_ci		if (alm_irq) {
1798c2ecf20Sopenharmony_ci			events |= RTC_IRQF | RTC_AF;
1808c2ecf20Sopenharmony_ci			rtc_cctrl = rtcss_read(davinci_rtc, PRTCSS_RTC_CCTRL);
1818c2ecf20Sopenharmony_ci			rtc_cctrl |=  PRTCSS_RTC_CCTRL_ALMFLG;
1828c2ecf20Sopenharmony_ci			rtcss_write(davinci_rtc, rtc_cctrl, PRTCSS_RTC_CCTRL);
1838c2ecf20Sopenharmony_ci		} else if (tmr_irq) {
1848c2ecf20Sopenharmony_ci			events |= RTC_IRQF | RTC_PF;
1858c2ecf20Sopenharmony_ci			rtc_ctrl = rtcss_read(davinci_rtc, PRTCSS_RTC_CTRL);
1868c2ecf20Sopenharmony_ci			rtc_ctrl |=  PRTCSS_RTC_CTRL_TMRFLG;
1878c2ecf20Sopenharmony_ci			rtcss_write(davinci_rtc, rtc_ctrl, PRTCSS_RTC_CTRL);
1888c2ecf20Sopenharmony_ci		}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci		rtcif_write(davinci_rtc, PRTCIF_INTFLG_RTCSS,
1918c2ecf20Sopenharmony_ci				    PRTCIF_INTFLG);
1928c2ecf20Sopenharmony_ci		rtc_update_irq(davinci_rtc->rtc, 1, events);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci		ret = IRQ_HANDLED;
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	return ret;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic int
2018c2ecf20Sopenharmony_cidavinci_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	struct davinci_rtc *davinci_rtc = dev_get_drvdata(dev);
2048c2ecf20Sopenharmony_ci	u8 rtc_ctrl;
2058c2ecf20Sopenharmony_ci	unsigned long flags;
2068c2ecf20Sopenharmony_ci	int ret = 0;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	spin_lock_irqsave(&davinci_rtc_lock, flags);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	rtc_ctrl = rtcss_read(davinci_rtc, PRTCSS_RTC_CTRL);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	switch (cmd) {
2138c2ecf20Sopenharmony_ci	case RTC_WIE_ON:
2148c2ecf20Sopenharmony_ci		rtc_ctrl |= PRTCSS_RTC_CTRL_WEN | PRTCSS_RTC_CTRL_WDTFLG;
2158c2ecf20Sopenharmony_ci		break;
2168c2ecf20Sopenharmony_ci	case RTC_WIE_OFF:
2178c2ecf20Sopenharmony_ci		rtc_ctrl &= ~PRTCSS_RTC_CTRL_WEN;
2188c2ecf20Sopenharmony_ci		break;
2198c2ecf20Sopenharmony_ci	default:
2208c2ecf20Sopenharmony_ci		ret = -ENOIOCTLCMD;
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, rtc_ctrl, PRTCSS_RTC_CTRL);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&davinci_rtc_lock, flags);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	return ret;
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic void convertfromdays(u16 days, struct rtc_time *tm)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	int tmp_days, year, mon;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	for (year = 2000;; year++) {
2358c2ecf20Sopenharmony_ci		tmp_days = rtc_year_days(1, 12, year);
2368c2ecf20Sopenharmony_ci		if (days >= tmp_days)
2378c2ecf20Sopenharmony_ci			days -= tmp_days;
2388c2ecf20Sopenharmony_ci		else {
2398c2ecf20Sopenharmony_ci			for (mon = 0;; mon++) {
2408c2ecf20Sopenharmony_ci				tmp_days = rtc_month_days(mon, year);
2418c2ecf20Sopenharmony_ci				if (days >= tmp_days) {
2428c2ecf20Sopenharmony_ci					days -= tmp_days;
2438c2ecf20Sopenharmony_ci				} else {
2448c2ecf20Sopenharmony_ci					tm->tm_year = year - 1900;
2458c2ecf20Sopenharmony_ci					tm->tm_mon = mon;
2468c2ecf20Sopenharmony_ci					tm->tm_mday = days + 1;
2478c2ecf20Sopenharmony_ci					break;
2488c2ecf20Sopenharmony_ci				}
2498c2ecf20Sopenharmony_ci			}
2508c2ecf20Sopenharmony_ci			break;
2518c2ecf20Sopenharmony_ci		}
2528c2ecf20Sopenharmony_ci	}
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic void convert2days(u16 *days, struct rtc_time *tm)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	int i;
2588c2ecf20Sopenharmony_ci	*days = 0;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	for (i = 2000; i < 1900 + tm->tm_year; i++)
2618c2ecf20Sopenharmony_ci		*days += rtc_year_days(1, 12, i);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	*days += rtc_year_days(tm->tm_mday, tm->tm_mon, 1900 + tm->tm_year);
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic int davinci_rtc_read_time(struct device *dev, struct rtc_time *tm)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	struct davinci_rtc *davinci_rtc = dev_get_drvdata(dev);
2698c2ecf20Sopenharmony_ci	u16 days = 0;
2708c2ecf20Sopenharmony_ci	u8 day0, day1;
2718c2ecf20Sopenharmony_ci	unsigned long flags;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	spin_lock_irqsave(&davinci_rtc_lock, flags);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
2768c2ecf20Sopenharmony_ci	tm->tm_sec = bcd2bin(rtcss_read(davinci_rtc, PRTCSS_RTC_SEC));
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
2798c2ecf20Sopenharmony_ci	tm->tm_min = bcd2bin(rtcss_read(davinci_rtc, PRTCSS_RTC_MIN));
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
2828c2ecf20Sopenharmony_ci	tm->tm_hour = bcd2bin(rtcss_read(davinci_rtc, PRTCSS_RTC_HOUR));
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
2858c2ecf20Sopenharmony_ci	day0 = rtcss_read(davinci_rtc, PRTCSS_RTC_DAY0);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
2888c2ecf20Sopenharmony_ci	day1 = rtcss_read(davinci_rtc, PRTCSS_RTC_DAY1);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&davinci_rtc_lock, flags);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	days |= day1;
2938c2ecf20Sopenharmony_ci	days <<= 8;
2948c2ecf20Sopenharmony_ci	days |= day0;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	convertfromdays(days, tm);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	return 0;
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic int davinci_rtc_set_time(struct device *dev, struct rtc_time *tm)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	struct davinci_rtc *davinci_rtc = dev_get_drvdata(dev);
3048c2ecf20Sopenharmony_ci	u16 days;
3058c2ecf20Sopenharmony_ci	u8 rtc_cctrl;
3068c2ecf20Sopenharmony_ci	unsigned long flags;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	convert2days(&days, tm);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	spin_lock_irqsave(&davinci_rtc_lock, flags);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
3138c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, bin2bcd(tm->tm_sec), PRTCSS_RTC_SEC);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
3168c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, bin2bcd(tm->tm_min), PRTCSS_RTC_MIN);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
3198c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, bin2bcd(tm->tm_hour), PRTCSS_RTC_HOUR);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
3228c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, days & 0xFF, PRTCSS_RTC_DAY0);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
3258c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, (days & 0xFF00) >> 8, PRTCSS_RTC_DAY1);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	rtc_cctrl = rtcss_read(davinci_rtc, PRTCSS_RTC_CCTRL);
3288c2ecf20Sopenharmony_ci	rtc_cctrl |= PRTCSS_RTC_CCTRL_CAEN;
3298c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, rtc_cctrl, PRTCSS_RTC_CCTRL);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&davinci_rtc_lock, flags);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	return 0;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic int davinci_rtc_alarm_irq_enable(struct device *dev,
3378c2ecf20Sopenharmony_ci					unsigned int enabled)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	struct davinci_rtc *davinci_rtc = dev_get_drvdata(dev);
3408c2ecf20Sopenharmony_ci	unsigned long flags;
3418c2ecf20Sopenharmony_ci	u8 rtc_cctrl = rtcss_read(davinci_rtc, PRTCSS_RTC_CCTRL);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	spin_lock_irqsave(&davinci_rtc_lock, flags);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	if (enabled)
3468c2ecf20Sopenharmony_ci		rtc_cctrl |= PRTCSS_RTC_CCTRL_DAEN |
3478c2ecf20Sopenharmony_ci			     PRTCSS_RTC_CCTRL_HAEN |
3488c2ecf20Sopenharmony_ci			     PRTCSS_RTC_CCTRL_MAEN |
3498c2ecf20Sopenharmony_ci			     PRTCSS_RTC_CCTRL_ALMFLG |
3508c2ecf20Sopenharmony_ci			     PRTCSS_RTC_CCTRL_AIEN;
3518c2ecf20Sopenharmony_ci	else
3528c2ecf20Sopenharmony_ci		rtc_cctrl &= ~PRTCSS_RTC_CCTRL_AIEN;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
3558c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, rtc_cctrl, PRTCSS_RTC_CCTRL);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&davinci_rtc_lock, flags);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	return 0;
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic int davinci_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
3638c2ecf20Sopenharmony_ci{
3648c2ecf20Sopenharmony_ci	struct davinci_rtc *davinci_rtc = dev_get_drvdata(dev);
3658c2ecf20Sopenharmony_ci	u16 days = 0;
3668c2ecf20Sopenharmony_ci	u8 day0, day1;
3678c2ecf20Sopenharmony_ci	unsigned long flags;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	alm->time.tm_sec = 0;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	spin_lock_irqsave(&davinci_rtc_lock, flags);
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
3748c2ecf20Sopenharmony_ci	alm->time.tm_min = bcd2bin(rtcss_read(davinci_rtc, PRTCSS_RTC_AMIN));
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
3778c2ecf20Sopenharmony_ci	alm->time.tm_hour = bcd2bin(rtcss_read(davinci_rtc, PRTCSS_RTC_AHOUR));
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
3808c2ecf20Sopenharmony_ci	day0 = rtcss_read(davinci_rtc, PRTCSS_RTC_ADAY0);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
3838c2ecf20Sopenharmony_ci	day1 = rtcss_read(davinci_rtc, PRTCSS_RTC_ADAY1);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&davinci_rtc_lock, flags);
3868c2ecf20Sopenharmony_ci	days |= day1;
3878c2ecf20Sopenharmony_ci	days <<= 8;
3888c2ecf20Sopenharmony_ci	days |= day0;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	convertfromdays(days, &alm->time);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	alm->pending = !!(rtcss_read(davinci_rtc,
3938c2ecf20Sopenharmony_ci			  PRTCSS_RTC_CCTRL) &
3948c2ecf20Sopenharmony_ci			PRTCSS_RTC_CCTRL_AIEN);
3958c2ecf20Sopenharmony_ci	alm->enabled = alm->pending && device_may_wakeup(dev);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	return 0;
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic int davinci_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	struct davinci_rtc *davinci_rtc = dev_get_drvdata(dev);
4038c2ecf20Sopenharmony_ci	unsigned long flags;
4048c2ecf20Sopenharmony_ci	u16 days;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	convert2days(&days, &alm->time);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	spin_lock_irqsave(&davinci_rtc_lock, flags);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
4118c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, bin2bcd(alm->time.tm_min), PRTCSS_RTC_AMIN);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
4148c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, bin2bcd(alm->time.tm_hour), PRTCSS_RTC_AHOUR);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
4178c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, days & 0xFF, PRTCSS_RTC_ADAY0);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	davinci_rtcss_calendar_wait(davinci_rtc);
4208c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, (days & 0xFF00) >> 8, PRTCSS_RTC_ADAY1);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&davinci_rtc_lock, flags);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	return 0;
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic const struct rtc_class_ops davinci_rtc_ops = {
4288c2ecf20Sopenharmony_ci	.ioctl			= davinci_rtc_ioctl,
4298c2ecf20Sopenharmony_ci	.read_time		= davinci_rtc_read_time,
4308c2ecf20Sopenharmony_ci	.set_time		= davinci_rtc_set_time,
4318c2ecf20Sopenharmony_ci	.alarm_irq_enable	= davinci_rtc_alarm_irq_enable,
4328c2ecf20Sopenharmony_ci	.read_alarm		= davinci_rtc_read_alarm,
4338c2ecf20Sopenharmony_ci	.set_alarm		= davinci_rtc_set_alarm,
4348c2ecf20Sopenharmony_ci};
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_cistatic int __init davinci_rtc_probe(struct platform_device *pdev)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
4398c2ecf20Sopenharmony_ci	struct davinci_rtc *davinci_rtc;
4408c2ecf20Sopenharmony_ci	int ret = 0;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	davinci_rtc = devm_kzalloc(&pdev->dev, sizeof(struct davinci_rtc), GFP_KERNEL);
4438c2ecf20Sopenharmony_ci	if (!davinci_rtc)
4448c2ecf20Sopenharmony_ci		return -ENOMEM;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	davinci_rtc->irq = platform_get_irq(pdev, 0);
4478c2ecf20Sopenharmony_ci	if (davinci_rtc->irq < 0)
4488c2ecf20Sopenharmony_ci		return davinci_rtc->irq;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	davinci_rtc->base = devm_platform_ioremap_resource(pdev, 0);
4518c2ecf20Sopenharmony_ci	if (IS_ERR(davinci_rtc->base))
4528c2ecf20Sopenharmony_ci		return PTR_ERR(davinci_rtc->base);
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, davinci_rtc);
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	davinci_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
4578c2ecf20Sopenharmony_ci	if (IS_ERR(davinci_rtc->rtc))
4588c2ecf20Sopenharmony_ci		return PTR_ERR(davinci_rtc->rtc);
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	davinci_rtc->rtc->ops = &davinci_rtc_ops;
4618c2ecf20Sopenharmony_ci	davinci_rtc->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
4628c2ecf20Sopenharmony_ci	davinci_rtc->rtc->range_max = RTC_TIMESTAMP_BEGIN_2000 + (1 << 16) * 86400ULL - 1;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	rtcif_write(davinci_rtc, PRTCIF_INTFLG_RTCSS, PRTCIF_INTFLG);
4658c2ecf20Sopenharmony_ci	rtcif_write(davinci_rtc, 0, PRTCIF_INTEN);
4668c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, 0, PRTCSS_RTC_INTC_EXTENA1);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, 0, PRTCSS_RTC_CTRL);
4698c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, 0, PRTCSS_RTC_CCTRL);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	ret = devm_request_irq(dev, davinci_rtc->irq, davinci_rtc_interrupt,
4728c2ecf20Sopenharmony_ci			  0, "davinci_rtc", davinci_rtc);
4738c2ecf20Sopenharmony_ci	if (ret < 0) {
4748c2ecf20Sopenharmony_ci		dev_err(dev, "unable to register davinci RTC interrupt\n");
4758c2ecf20Sopenharmony_ci		return ret;
4768c2ecf20Sopenharmony_ci	}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	/* Enable interrupts */
4798c2ecf20Sopenharmony_ci	rtcif_write(davinci_rtc, PRTCIF_INTEN_RTCSS, PRTCIF_INTEN);
4808c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, PRTCSS_RTC_INTC_EXTENA1_MASK,
4818c2ecf20Sopenharmony_ci			    PRTCSS_RTC_INTC_EXTENA1);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	rtcss_write(davinci_rtc, PRTCSS_RTC_CCTRL_CAEN, PRTCSS_RTC_CCTRL);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	device_init_wakeup(&pdev->dev, 0);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	return rtc_register_device(davinci_rtc->rtc);
4888c2ecf20Sopenharmony_ci}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_cistatic int __exit davinci_rtc_remove(struct platform_device *pdev)
4918c2ecf20Sopenharmony_ci{
4928c2ecf20Sopenharmony_ci	struct davinci_rtc *davinci_rtc = platform_get_drvdata(pdev);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	device_init_wakeup(&pdev->dev, 0);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	rtcif_write(davinci_rtc, 0, PRTCIF_INTEN);
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	return 0;
4998c2ecf20Sopenharmony_ci}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_cistatic struct platform_driver davinci_rtc_driver = {
5028c2ecf20Sopenharmony_ci	.remove		= __exit_p(davinci_rtc_remove),
5038c2ecf20Sopenharmony_ci	.driver		= {
5048c2ecf20Sopenharmony_ci		.name = "rtc_davinci",
5058c2ecf20Sopenharmony_ci	},
5068c2ecf20Sopenharmony_ci};
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cimodule_platform_driver_probe(davinci_rtc_driver, davinci_rtc_probe);
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ciMODULE_AUTHOR("Miguel Aguilar <miguel.aguilar@ridgerun.com>");
5118c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Texas Instruments DaVinci PRTC Driver");
5128c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
513