18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * rtc-twl.c -- TWL Real Time Clock interface
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2007 MontaVista Software, Inc
68c2ecf20Sopenharmony_ci * Author: Alexandre Rusev <source@mvista.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Based on original TI driver twl4030-rtc.c
98c2ecf20Sopenharmony_ci *   Copyright (C) 2006 Texas Instruments, Inc.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Based on rtc-omap.c
128c2ecf20Sopenharmony_ci *   Copyright (C) 2003 MontaVista Software, Inc.
138c2ecf20Sopenharmony_ci *   Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
148c2ecf20Sopenharmony_ci *   Copyright (C) 2006 David Brownell
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/errno.h>
218c2ecf20Sopenharmony_ci#include <linux/init.h>
228c2ecf20Sopenharmony_ci#include <linux/module.h>
238c2ecf20Sopenharmony_ci#include <linux/types.h>
248c2ecf20Sopenharmony_ci#include <linux/rtc.h>
258c2ecf20Sopenharmony_ci#include <linux/bcd.h>
268c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
278c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
288c2ecf20Sopenharmony_ci#include <linux/of.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <linux/mfd/twl.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cienum twl_class {
338c2ecf20Sopenharmony_ci	TWL_4030 = 0,
348c2ecf20Sopenharmony_ci	TWL_6030,
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * RTC block register offsets (use TWL_MODULE_RTC)
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_cienum {
418c2ecf20Sopenharmony_ci	REG_SECONDS_REG = 0,
428c2ecf20Sopenharmony_ci	REG_MINUTES_REG,
438c2ecf20Sopenharmony_ci	REG_HOURS_REG,
448c2ecf20Sopenharmony_ci	REG_DAYS_REG,
458c2ecf20Sopenharmony_ci	REG_MONTHS_REG,
468c2ecf20Sopenharmony_ci	REG_YEARS_REG,
478c2ecf20Sopenharmony_ci	REG_WEEKS_REG,
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	REG_ALARM_SECONDS_REG,
508c2ecf20Sopenharmony_ci	REG_ALARM_MINUTES_REG,
518c2ecf20Sopenharmony_ci	REG_ALARM_HOURS_REG,
528c2ecf20Sopenharmony_ci	REG_ALARM_DAYS_REG,
538c2ecf20Sopenharmony_ci	REG_ALARM_MONTHS_REG,
548c2ecf20Sopenharmony_ci	REG_ALARM_YEARS_REG,
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	REG_RTC_CTRL_REG,
578c2ecf20Sopenharmony_ci	REG_RTC_STATUS_REG,
588c2ecf20Sopenharmony_ci	REG_RTC_INTERRUPTS_REG,
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	REG_RTC_COMP_LSB_REG,
618c2ecf20Sopenharmony_ci	REG_RTC_COMP_MSB_REG,
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_cistatic const u8 twl4030_rtc_reg_map[] = {
648c2ecf20Sopenharmony_ci	[REG_SECONDS_REG] = 0x00,
658c2ecf20Sopenharmony_ci	[REG_MINUTES_REG] = 0x01,
668c2ecf20Sopenharmony_ci	[REG_HOURS_REG] = 0x02,
678c2ecf20Sopenharmony_ci	[REG_DAYS_REG] = 0x03,
688c2ecf20Sopenharmony_ci	[REG_MONTHS_REG] = 0x04,
698c2ecf20Sopenharmony_ci	[REG_YEARS_REG] = 0x05,
708c2ecf20Sopenharmony_ci	[REG_WEEKS_REG] = 0x06,
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	[REG_ALARM_SECONDS_REG] = 0x07,
738c2ecf20Sopenharmony_ci	[REG_ALARM_MINUTES_REG] = 0x08,
748c2ecf20Sopenharmony_ci	[REG_ALARM_HOURS_REG] = 0x09,
758c2ecf20Sopenharmony_ci	[REG_ALARM_DAYS_REG] = 0x0A,
768c2ecf20Sopenharmony_ci	[REG_ALARM_MONTHS_REG] = 0x0B,
778c2ecf20Sopenharmony_ci	[REG_ALARM_YEARS_REG] = 0x0C,
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	[REG_RTC_CTRL_REG] = 0x0D,
808c2ecf20Sopenharmony_ci	[REG_RTC_STATUS_REG] = 0x0E,
818c2ecf20Sopenharmony_ci	[REG_RTC_INTERRUPTS_REG] = 0x0F,
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	[REG_RTC_COMP_LSB_REG] = 0x10,
848c2ecf20Sopenharmony_ci	[REG_RTC_COMP_MSB_REG] = 0x11,
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_cistatic const u8 twl6030_rtc_reg_map[] = {
878c2ecf20Sopenharmony_ci	[REG_SECONDS_REG] = 0x00,
888c2ecf20Sopenharmony_ci	[REG_MINUTES_REG] = 0x01,
898c2ecf20Sopenharmony_ci	[REG_HOURS_REG] = 0x02,
908c2ecf20Sopenharmony_ci	[REG_DAYS_REG] = 0x03,
918c2ecf20Sopenharmony_ci	[REG_MONTHS_REG] = 0x04,
928c2ecf20Sopenharmony_ci	[REG_YEARS_REG] = 0x05,
938c2ecf20Sopenharmony_ci	[REG_WEEKS_REG] = 0x06,
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	[REG_ALARM_SECONDS_REG] = 0x08,
968c2ecf20Sopenharmony_ci	[REG_ALARM_MINUTES_REG] = 0x09,
978c2ecf20Sopenharmony_ci	[REG_ALARM_HOURS_REG] = 0x0A,
988c2ecf20Sopenharmony_ci	[REG_ALARM_DAYS_REG] = 0x0B,
998c2ecf20Sopenharmony_ci	[REG_ALARM_MONTHS_REG] = 0x0C,
1008c2ecf20Sopenharmony_ci	[REG_ALARM_YEARS_REG] = 0x0D,
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	[REG_RTC_CTRL_REG] = 0x10,
1038c2ecf20Sopenharmony_ci	[REG_RTC_STATUS_REG] = 0x11,
1048c2ecf20Sopenharmony_ci	[REG_RTC_INTERRUPTS_REG] = 0x12,
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	[REG_RTC_COMP_LSB_REG] = 0x13,
1078c2ecf20Sopenharmony_ci	[REG_RTC_COMP_MSB_REG] = 0x14,
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/* RTC_CTRL_REG bitfields */
1118c2ecf20Sopenharmony_ci#define BIT_RTC_CTRL_REG_STOP_RTC_M              0x01
1128c2ecf20Sopenharmony_ci#define BIT_RTC_CTRL_REG_ROUND_30S_M             0x02
1138c2ecf20Sopenharmony_ci#define BIT_RTC_CTRL_REG_AUTO_COMP_M             0x04
1148c2ecf20Sopenharmony_ci#define BIT_RTC_CTRL_REG_MODE_12_24_M            0x08
1158c2ecf20Sopenharmony_ci#define BIT_RTC_CTRL_REG_TEST_MODE_M             0x10
1168c2ecf20Sopenharmony_ci#define BIT_RTC_CTRL_REG_SET_32_COUNTER_M        0x20
1178c2ecf20Sopenharmony_ci#define BIT_RTC_CTRL_REG_GET_TIME_M              0x40
1188c2ecf20Sopenharmony_ci#define BIT_RTC_CTRL_REG_RTC_V_OPT               0x80
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/* RTC_STATUS_REG bitfields */
1218c2ecf20Sopenharmony_ci#define BIT_RTC_STATUS_REG_RUN_M                 0x02
1228c2ecf20Sopenharmony_ci#define BIT_RTC_STATUS_REG_1S_EVENT_M            0x04
1238c2ecf20Sopenharmony_ci#define BIT_RTC_STATUS_REG_1M_EVENT_M            0x08
1248c2ecf20Sopenharmony_ci#define BIT_RTC_STATUS_REG_1H_EVENT_M            0x10
1258c2ecf20Sopenharmony_ci#define BIT_RTC_STATUS_REG_1D_EVENT_M            0x20
1268c2ecf20Sopenharmony_ci#define BIT_RTC_STATUS_REG_ALARM_M               0x40
1278c2ecf20Sopenharmony_ci#define BIT_RTC_STATUS_REG_POWER_UP_M            0x80
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/* RTC_INTERRUPTS_REG bitfields */
1308c2ecf20Sopenharmony_ci#define BIT_RTC_INTERRUPTS_REG_EVERY_M           0x03
1318c2ecf20Sopenharmony_ci#define BIT_RTC_INTERRUPTS_REG_IT_TIMER_M        0x04
1328c2ecf20Sopenharmony_ci#define BIT_RTC_INTERRUPTS_REG_IT_ALARM_M        0x08
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/* REG_SECONDS_REG through REG_YEARS_REG is how many registers? */
1368c2ecf20Sopenharmony_ci#define ALL_TIME_REGS		6
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci/*----------------------------------------------------------------------*/
1398c2ecf20Sopenharmony_cistruct twl_rtc {
1408c2ecf20Sopenharmony_ci	struct device *dev;
1418c2ecf20Sopenharmony_ci	struct rtc_device *rtc;
1428c2ecf20Sopenharmony_ci	u8 *reg_map;
1438c2ecf20Sopenharmony_ci	/*
1448c2ecf20Sopenharmony_ci	 * Cache the value for timer/alarm interrupts register; this is
1458c2ecf20Sopenharmony_ci	 * only changed by callers holding rtc ops lock (or resume).
1468c2ecf20Sopenharmony_ci	 */
1478c2ecf20Sopenharmony_ci	unsigned char rtc_irq_bits;
1488c2ecf20Sopenharmony_ci	bool wake_enabled;
1498c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
1508c2ecf20Sopenharmony_ci	unsigned char irqstat;
1518c2ecf20Sopenharmony_ci#endif
1528c2ecf20Sopenharmony_ci	enum twl_class class;
1538c2ecf20Sopenharmony_ci};
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/*
1568c2ecf20Sopenharmony_ci * Supports 1 byte read from TWL RTC register.
1578c2ecf20Sopenharmony_ci */
1588c2ecf20Sopenharmony_cistatic int twl_rtc_read_u8(struct twl_rtc *twl_rtc, u8 *data, u8 reg)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	int ret;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	ret = twl_i2c_read_u8(TWL_MODULE_RTC, data, (twl_rtc->reg_map[reg]));
1638c2ecf20Sopenharmony_ci	if (ret < 0)
1648c2ecf20Sopenharmony_ci		pr_err("Could not read TWL register %X - error %d\n", reg, ret);
1658c2ecf20Sopenharmony_ci	return ret;
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/*
1698c2ecf20Sopenharmony_ci * Supports 1 byte write to TWL RTC registers.
1708c2ecf20Sopenharmony_ci */
1718c2ecf20Sopenharmony_cistatic int twl_rtc_write_u8(struct twl_rtc *twl_rtc, u8 data, u8 reg)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	int ret;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	ret = twl_i2c_write_u8(TWL_MODULE_RTC, data, (twl_rtc->reg_map[reg]));
1768c2ecf20Sopenharmony_ci	if (ret < 0)
1778c2ecf20Sopenharmony_ci		pr_err("Could not write TWL register %X - error %d\n",
1788c2ecf20Sopenharmony_ci		       reg, ret);
1798c2ecf20Sopenharmony_ci	return ret;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/*
1838c2ecf20Sopenharmony_ci * Enable 1/second update and/or alarm interrupts.
1848c2ecf20Sopenharmony_ci */
1858c2ecf20Sopenharmony_cistatic int set_rtc_irq_bit(struct twl_rtc *twl_rtc, unsigned char bit)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	unsigned char val;
1888c2ecf20Sopenharmony_ci	int ret;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	/* if the bit is set, return from here */
1918c2ecf20Sopenharmony_ci	if (twl_rtc->rtc_irq_bits & bit)
1928c2ecf20Sopenharmony_ci		return 0;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	val = twl_rtc->rtc_irq_bits | bit;
1958c2ecf20Sopenharmony_ci	val &= ~BIT_RTC_INTERRUPTS_REG_EVERY_M;
1968c2ecf20Sopenharmony_ci	ret = twl_rtc_write_u8(twl_rtc, val, REG_RTC_INTERRUPTS_REG);
1978c2ecf20Sopenharmony_ci	if (ret == 0)
1988c2ecf20Sopenharmony_ci		twl_rtc->rtc_irq_bits = val;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	return ret;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci/*
2048c2ecf20Sopenharmony_ci * Disable update and/or alarm interrupts.
2058c2ecf20Sopenharmony_ci */
2068c2ecf20Sopenharmony_cistatic int mask_rtc_irq_bit(struct twl_rtc *twl_rtc, unsigned char bit)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	unsigned char val;
2098c2ecf20Sopenharmony_ci	int ret;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	/* if the bit is clear, return from here */
2128c2ecf20Sopenharmony_ci	if (!(twl_rtc->rtc_irq_bits & bit))
2138c2ecf20Sopenharmony_ci		return 0;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	val = twl_rtc->rtc_irq_bits & ~bit;
2168c2ecf20Sopenharmony_ci	ret = twl_rtc_write_u8(twl_rtc, val, REG_RTC_INTERRUPTS_REG);
2178c2ecf20Sopenharmony_ci	if (ret == 0)
2188c2ecf20Sopenharmony_ci		twl_rtc->rtc_irq_bits = val;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	return ret;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic int twl_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	struct platform_device *pdev = to_platform_device(dev);
2268c2ecf20Sopenharmony_ci	struct twl_rtc *twl_rtc = dev_get_drvdata(dev);
2278c2ecf20Sopenharmony_ci	int irq = platform_get_irq(pdev, 0);
2288c2ecf20Sopenharmony_ci	int ret;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	if (enabled) {
2318c2ecf20Sopenharmony_ci		ret = set_rtc_irq_bit(twl_rtc,
2328c2ecf20Sopenharmony_ci				      BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
2338c2ecf20Sopenharmony_ci		if (device_can_wakeup(dev) && !twl_rtc->wake_enabled) {
2348c2ecf20Sopenharmony_ci			enable_irq_wake(irq);
2358c2ecf20Sopenharmony_ci			twl_rtc->wake_enabled = true;
2368c2ecf20Sopenharmony_ci		}
2378c2ecf20Sopenharmony_ci	} else {
2388c2ecf20Sopenharmony_ci		ret = mask_rtc_irq_bit(twl_rtc,
2398c2ecf20Sopenharmony_ci				       BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
2408c2ecf20Sopenharmony_ci		if (twl_rtc->wake_enabled) {
2418c2ecf20Sopenharmony_ci			disable_irq_wake(irq);
2428c2ecf20Sopenharmony_ci			twl_rtc->wake_enabled = false;
2438c2ecf20Sopenharmony_ci		}
2448c2ecf20Sopenharmony_ci	}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	return ret;
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci/*
2508c2ecf20Sopenharmony_ci * Gets current TWL RTC time and date parameters.
2518c2ecf20Sopenharmony_ci *
2528c2ecf20Sopenharmony_ci * The RTC's time/alarm representation is not what gmtime(3) requires
2538c2ecf20Sopenharmony_ci * Linux to use:
2548c2ecf20Sopenharmony_ci *
2558c2ecf20Sopenharmony_ci *  - Months are 1..12 vs Linux 0-11
2568c2ecf20Sopenharmony_ci *  - Years are 0..99 vs Linux 1900..N (we assume 21st century)
2578c2ecf20Sopenharmony_ci */
2588c2ecf20Sopenharmony_cistatic int twl_rtc_read_time(struct device *dev, struct rtc_time *tm)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	struct twl_rtc *twl_rtc = dev_get_drvdata(dev);
2618c2ecf20Sopenharmony_ci	unsigned char rtc_data[ALL_TIME_REGS];
2628c2ecf20Sopenharmony_ci	int ret;
2638c2ecf20Sopenharmony_ci	u8 save_control;
2648c2ecf20Sopenharmony_ci	u8 rtc_control;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	ret = twl_rtc_read_u8(twl_rtc, &save_control, REG_RTC_CTRL_REG);
2678c2ecf20Sopenharmony_ci	if (ret < 0) {
2688c2ecf20Sopenharmony_ci		dev_err(dev, "%s: reading CTRL_REG, error %d\n", __func__, ret);
2698c2ecf20Sopenharmony_ci		return ret;
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci	/* for twl6030/32 make sure BIT_RTC_CTRL_REG_GET_TIME_M is clear */
2728c2ecf20Sopenharmony_ci	if (twl_rtc->class == TWL_6030) {
2738c2ecf20Sopenharmony_ci		if (save_control & BIT_RTC_CTRL_REG_GET_TIME_M) {
2748c2ecf20Sopenharmony_ci			save_control &= ~BIT_RTC_CTRL_REG_GET_TIME_M;
2758c2ecf20Sopenharmony_ci			ret = twl_rtc_write_u8(twl_rtc, save_control,
2768c2ecf20Sopenharmony_ci					       REG_RTC_CTRL_REG);
2778c2ecf20Sopenharmony_ci			if (ret < 0) {
2788c2ecf20Sopenharmony_ci				dev_err(dev, "%s clr GET_TIME, error %d\n",
2798c2ecf20Sopenharmony_ci					__func__, ret);
2808c2ecf20Sopenharmony_ci				return ret;
2818c2ecf20Sopenharmony_ci			}
2828c2ecf20Sopenharmony_ci		}
2838c2ecf20Sopenharmony_ci	}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	/* Copy RTC counting registers to static registers or latches */
2868c2ecf20Sopenharmony_ci	rtc_control = save_control | BIT_RTC_CTRL_REG_GET_TIME_M;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	/* for twl6030/32 enable read access to static shadowed registers */
2898c2ecf20Sopenharmony_ci	if (twl_rtc->class == TWL_6030)
2908c2ecf20Sopenharmony_ci		rtc_control |= BIT_RTC_CTRL_REG_RTC_V_OPT;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	ret = twl_rtc_write_u8(twl_rtc, rtc_control, REG_RTC_CTRL_REG);
2938c2ecf20Sopenharmony_ci	if (ret < 0) {
2948c2ecf20Sopenharmony_ci		dev_err(dev, "%s: writing CTRL_REG, error %d\n", __func__, ret);
2958c2ecf20Sopenharmony_ci		return ret;
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	ret = twl_i2c_read(TWL_MODULE_RTC, rtc_data,
2998c2ecf20Sopenharmony_ci			(twl_rtc->reg_map[REG_SECONDS_REG]), ALL_TIME_REGS);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	if (ret < 0) {
3028c2ecf20Sopenharmony_ci		dev_err(dev, "%s: reading data, error %d\n", __func__, ret);
3038c2ecf20Sopenharmony_ci		return ret;
3048c2ecf20Sopenharmony_ci	}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	/* for twl6030 restore original state of rtc control register */
3078c2ecf20Sopenharmony_ci	if (twl_rtc->class == TWL_6030) {
3088c2ecf20Sopenharmony_ci		ret = twl_rtc_write_u8(twl_rtc, save_control, REG_RTC_CTRL_REG);
3098c2ecf20Sopenharmony_ci		if (ret < 0) {
3108c2ecf20Sopenharmony_ci			dev_err(dev, "%s: restore CTRL_REG, error %d\n",
3118c2ecf20Sopenharmony_ci				__func__, ret);
3128c2ecf20Sopenharmony_ci			return ret;
3138c2ecf20Sopenharmony_ci		}
3148c2ecf20Sopenharmony_ci	}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	tm->tm_sec = bcd2bin(rtc_data[0]);
3178c2ecf20Sopenharmony_ci	tm->tm_min = bcd2bin(rtc_data[1]);
3188c2ecf20Sopenharmony_ci	tm->tm_hour = bcd2bin(rtc_data[2]);
3198c2ecf20Sopenharmony_ci	tm->tm_mday = bcd2bin(rtc_data[3]);
3208c2ecf20Sopenharmony_ci	tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
3218c2ecf20Sopenharmony_ci	tm->tm_year = bcd2bin(rtc_data[5]) + 100;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	return ret;
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic int twl_rtc_set_time(struct device *dev, struct rtc_time *tm)
3278c2ecf20Sopenharmony_ci{
3288c2ecf20Sopenharmony_ci	struct twl_rtc *twl_rtc = dev_get_drvdata(dev);
3298c2ecf20Sopenharmony_ci	unsigned char save_control;
3308c2ecf20Sopenharmony_ci	unsigned char rtc_data[ALL_TIME_REGS];
3318c2ecf20Sopenharmony_ci	int ret;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	rtc_data[0] = bin2bcd(tm->tm_sec);
3348c2ecf20Sopenharmony_ci	rtc_data[1] = bin2bcd(tm->tm_min);
3358c2ecf20Sopenharmony_ci	rtc_data[2] = bin2bcd(tm->tm_hour);
3368c2ecf20Sopenharmony_ci	rtc_data[3] = bin2bcd(tm->tm_mday);
3378c2ecf20Sopenharmony_ci	rtc_data[4] = bin2bcd(tm->tm_mon + 1);
3388c2ecf20Sopenharmony_ci	rtc_data[5] = bin2bcd(tm->tm_year - 100);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	/* Stop RTC while updating the TC registers */
3418c2ecf20Sopenharmony_ci	ret = twl_rtc_read_u8(twl_rtc, &save_control, REG_RTC_CTRL_REG);
3428c2ecf20Sopenharmony_ci	if (ret < 0)
3438c2ecf20Sopenharmony_ci		goto out;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	save_control &= ~BIT_RTC_CTRL_REG_STOP_RTC_M;
3468c2ecf20Sopenharmony_ci	ret = twl_rtc_write_u8(twl_rtc, save_control, REG_RTC_CTRL_REG);
3478c2ecf20Sopenharmony_ci	if (ret < 0)
3488c2ecf20Sopenharmony_ci		goto out;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	/* update all the time registers in one shot */
3518c2ecf20Sopenharmony_ci	ret = twl_i2c_write(TWL_MODULE_RTC, rtc_data,
3528c2ecf20Sopenharmony_ci		(twl_rtc->reg_map[REG_SECONDS_REG]), ALL_TIME_REGS);
3538c2ecf20Sopenharmony_ci	if (ret < 0) {
3548c2ecf20Sopenharmony_ci		dev_err(dev, "rtc_set_time error %d\n", ret);
3558c2ecf20Sopenharmony_ci		goto out;
3568c2ecf20Sopenharmony_ci	}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	/* Start back RTC */
3598c2ecf20Sopenharmony_ci	save_control |= BIT_RTC_CTRL_REG_STOP_RTC_M;
3608c2ecf20Sopenharmony_ci	ret = twl_rtc_write_u8(twl_rtc, save_control, REG_RTC_CTRL_REG);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ciout:
3638c2ecf20Sopenharmony_ci	return ret;
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci/*
3678c2ecf20Sopenharmony_ci * Gets current TWL RTC alarm time.
3688c2ecf20Sopenharmony_ci */
3698c2ecf20Sopenharmony_cistatic int twl_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	struct twl_rtc *twl_rtc = dev_get_drvdata(dev);
3728c2ecf20Sopenharmony_ci	unsigned char rtc_data[ALL_TIME_REGS];
3738c2ecf20Sopenharmony_ci	int ret;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	ret = twl_i2c_read(TWL_MODULE_RTC, rtc_data,
3768c2ecf20Sopenharmony_ci			twl_rtc->reg_map[REG_ALARM_SECONDS_REG], ALL_TIME_REGS);
3778c2ecf20Sopenharmony_ci	if (ret < 0) {
3788c2ecf20Sopenharmony_ci		dev_err(dev, "rtc_read_alarm error %d\n", ret);
3798c2ecf20Sopenharmony_ci		return ret;
3808c2ecf20Sopenharmony_ci	}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	/* some of these fields may be wildcard/"match all" */
3838c2ecf20Sopenharmony_ci	alm->time.tm_sec = bcd2bin(rtc_data[0]);
3848c2ecf20Sopenharmony_ci	alm->time.tm_min = bcd2bin(rtc_data[1]);
3858c2ecf20Sopenharmony_ci	alm->time.tm_hour = bcd2bin(rtc_data[2]);
3868c2ecf20Sopenharmony_ci	alm->time.tm_mday = bcd2bin(rtc_data[3]);
3878c2ecf20Sopenharmony_ci	alm->time.tm_mon = bcd2bin(rtc_data[4]) - 1;
3888c2ecf20Sopenharmony_ci	alm->time.tm_year = bcd2bin(rtc_data[5]) + 100;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	/* report cached alarm enable state */
3918c2ecf20Sopenharmony_ci	if (twl_rtc->rtc_irq_bits & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M)
3928c2ecf20Sopenharmony_ci		alm->enabled = 1;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	return ret;
3958c2ecf20Sopenharmony_ci}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistatic int twl_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
3988c2ecf20Sopenharmony_ci{
3998c2ecf20Sopenharmony_ci	struct twl_rtc *twl_rtc = dev_get_drvdata(dev);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	unsigned char alarm_data[ALL_TIME_REGS];
4028c2ecf20Sopenharmony_ci	int ret;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	ret = twl_rtc_alarm_irq_enable(dev, 0);
4058c2ecf20Sopenharmony_ci	if (ret)
4068c2ecf20Sopenharmony_ci		goto out;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	alarm_data[0] = bin2bcd(alm->time.tm_sec);
4098c2ecf20Sopenharmony_ci	alarm_data[1] = bin2bcd(alm->time.tm_min);
4108c2ecf20Sopenharmony_ci	alarm_data[2] = bin2bcd(alm->time.tm_hour);
4118c2ecf20Sopenharmony_ci	alarm_data[3] = bin2bcd(alm->time.tm_mday);
4128c2ecf20Sopenharmony_ci	alarm_data[4] = bin2bcd(alm->time.tm_mon + 1);
4138c2ecf20Sopenharmony_ci	alarm_data[5] = bin2bcd(alm->time.tm_year - 100);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	/* update all the alarm registers in one shot */
4168c2ecf20Sopenharmony_ci	ret = twl_i2c_write(TWL_MODULE_RTC, alarm_data,
4178c2ecf20Sopenharmony_ci			twl_rtc->reg_map[REG_ALARM_SECONDS_REG], ALL_TIME_REGS);
4188c2ecf20Sopenharmony_ci	if (ret) {
4198c2ecf20Sopenharmony_ci		dev_err(dev, "rtc_set_alarm error %d\n", ret);
4208c2ecf20Sopenharmony_ci		goto out;
4218c2ecf20Sopenharmony_ci	}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	if (alm->enabled)
4248c2ecf20Sopenharmony_ci		ret = twl_rtc_alarm_irq_enable(dev, 1);
4258c2ecf20Sopenharmony_ciout:
4268c2ecf20Sopenharmony_ci	return ret;
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_cistatic irqreturn_t twl_rtc_interrupt(int irq, void *data)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	struct twl_rtc *twl_rtc = data;
4328c2ecf20Sopenharmony_ci	unsigned long events;
4338c2ecf20Sopenharmony_ci	int ret = IRQ_NONE;
4348c2ecf20Sopenharmony_ci	int res;
4358c2ecf20Sopenharmony_ci	u8 rd_reg;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	res = twl_rtc_read_u8(twl_rtc, &rd_reg, REG_RTC_STATUS_REG);
4388c2ecf20Sopenharmony_ci	if (res)
4398c2ecf20Sopenharmony_ci		goto out;
4408c2ecf20Sopenharmony_ci	/*
4418c2ecf20Sopenharmony_ci	 * Figure out source of interrupt: ALARM or TIMER in RTC_STATUS_REG.
4428c2ecf20Sopenharmony_ci	 * only one (ALARM or RTC) interrupt source may be enabled
4438c2ecf20Sopenharmony_ci	 * at time, we also could check our results
4448c2ecf20Sopenharmony_ci	 * by reading RTS_INTERRUPTS_REGISTER[IT_TIMER,IT_ALARM]
4458c2ecf20Sopenharmony_ci	 */
4468c2ecf20Sopenharmony_ci	if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
4478c2ecf20Sopenharmony_ci		events = RTC_IRQF | RTC_AF;
4488c2ecf20Sopenharmony_ci	else
4498c2ecf20Sopenharmony_ci		events = RTC_IRQF | RTC_PF;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	res = twl_rtc_write_u8(twl_rtc, BIT_RTC_STATUS_REG_ALARM_M,
4528c2ecf20Sopenharmony_ci			       REG_RTC_STATUS_REG);
4538c2ecf20Sopenharmony_ci	if (res)
4548c2ecf20Sopenharmony_ci		goto out;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	if (twl_rtc->class == TWL_4030) {
4578c2ecf20Sopenharmony_ci		/* Clear on Read enabled. RTC_IT bit of TWL4030_INT_PWR_ISR1
4588c2ecf20Sopenharmony_ci		 * needs 2 reads to clear the interrupt. One read is done in
4598c2ecf20Sopenharmony_ci		 * do_twl_pwrirq(). Doing the second read, to clear
4608c2ecf20Sopenharmony_ci		 * the bit.
4618c2ecf20Sopenharmony_ci		 *
4628c2ecf20Sopenharmony_ci		 * FIXME the reason PWR_ISR1 needs an extra read is that
4638c2ecf20Sopenharmony_ci		 * RTC_IF retriggered until we cleared REG_ALARM_M above.
4648c2ecf20Sopenharmony_ci		 * But re-reading like this is a bad hack; by doing so we
4658c2ecf20Sopenharmony_ci		 * risk wrongly clearing status for some other IRQ (losing
4668c2ecf20Sopenharmony_ci		 * the interrupt).  Be smarter about handling RTC_UF ...
4678c2ecf20Sopenharmony_ci		 */
4688c2ecf20Sopenharmony_ci		res = twl_i2c_read_u8(TWL4030_MODULE_INT,
4698c2ecf20Sopenharmony_ci			&rd_reg, TWL4030_INT_PWR_ISR1);
4708c2ecf20Sopenharmony_ci		if (res)
4718c2ecf20Sopenharmony_ci			goto out;
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	/* Notify RTC core on event */
4758c2ecf20Sopenharmony_ci	rtc_update_irq(twl_rtc->rtc, 1, events);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	ret = IRQ_HANDLED;
4788c2ecf20Sopenharmony_ciout:
4798c2ecf20Sopenharmony_ci	return ret;
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cistatic const struct rtc_class_ops twl_rtc_ops = {
4838c2ecf20Sopenharmony_ci	.read_time	= twl_rtc_read_time,
4848c2ecf20Sopenharmony_ci	.set_time	= twl_rtc_set_time,
4858c2ecf20Sopenharmony_ci	.read_alarm	= twl_rtc_read_alarm,
4868c2ecf20Sopenharmony_ci	.set_alarm	= twl_rtc_set_alarm,
4878c2ecf20Sopenharmony_ci	.alarm_irq_enable = twl_rtc_alarm_irq_enable,
4888c2ecf20Sopenharmony_ci};
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci/*----------------------------------------------------------------------*/
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic int twl_rtc_probe(struct platform_device *pdev)
4938c2ecf20Sopenharmony_ci{
4948c2ecf20Sopenharmony_ci	struct twl_rtc *twl_rtc;
4958c2ecf20Sopenharmony_ci	struct device_node *np = pdev->dev.of_node;
4968c2ecf20Sopenharmony_ci	int ret = -EINVAL;
4978c2ecf20Sopenharmony_ci	int irq = platform_get_irq(pdev, 0);
4988c2ecf20Sopenharmony_ci	u8 rd_reg;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	if (!np) {
5018c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "no DT info\n");
5028c2ecf20Sopenharmony_ci		return -EINVAL;
5038c2ecf20Sopenharmony_ci	}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	if (irq <= 0)
5068c2ecf20Sopenharmony_ci		return ret;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	twl_rtc = devm_kzalloc(&pdev->dev, sizeof(*twl_rtc), GFP_KERNEL);
5098c2ecf20Sopenharmony_ci	if (!twl_rtc)
5108c2ecf20Sopenharmony_ci		return -ENOMEM;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	if (twl_class_is_4030()) {
5138c2ecf20Sopenharmony_ci		twl_rtc->class = TWL_4030;
5148c2ecf20Sopenharmony_ci		twl_rtc->reg_map = (u8 *)twl4030_rtc_reg_map;
5158c2ecf20Sopenharmony_ci	} else if (twl_class_is_6030()) {
5168c2ecf20Sopenharmony_ci		twl_rtc->class = TWL_6030;
5178c2ecf20Sopenharmony_ci		twl_rtc->reg_map = (u8 *)twl6030_rtc_reg_map;
5188c2ecf20Sopenharmony_ci	} else {
5198c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "TWL Class not supported.\n");
5208c2ecf20Sopenharmony_ci		return -EINVAL;
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	ret = twl_rtc_read_u8(twl_rtc, &rd_reg, REG_RTC_STATUS_REG);
5248c2ecf20Sopenharmony_ci	if (ret < 0)
5258c2ecf20Sopenharmony_ci		return ret;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (rd_reg & BIT_RTC_STATUS_REG_POWER_UP_M)
5288c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev, "Power up reset detected.\n");
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
5318c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev, "Pending Alarm interrupt detected.\n");
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	/* Clear RTC Power up reset and pending alarm interrupts */
5348c2ecf20Sopenharmony_ci	ret = twl_rtc_write_u8(twl_rtc, rd_reg, REG_RTC_STATUS_REG);
5358c2ecf20Sopenharmony_ci	if (ret < 0)
5368c2ecf20Sopenharmony_ci		return ret;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	if (twl_rtc->class == TWL_6030) {
5398c2ecf20Sopenharmony_ci		twl6030_interrupt_unmask(TWL6030_RTC_INT_MASK,
5408c2ecf20Sopenharmony_ci			REG_INT_MSK_LINE_A);
5418c2ecf20Sopenharmony_ci		twl6030_interrupt_unmask(TWL6030_RTC_INT_MASK,
5428c2ecf20Sopenharmony_ci			REG_INT_MSK_STS_A);
5438c2ecf20Sopenharmony_ci	}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	dev_info(&pdev->dev, "Enabling TWL-RTC\n");
5468c2ecf20Sopenharmony_ci	ret = twl_rtc_write_u8(twl_rtc, BIT_RTC_CTRL_REG_STOP_RTC_M,
5478c2ecf20Sopenharmony_ci			       REG_RTC_CTRL_REG);
5488c2ecf20Sopenharmony_ci	if (ret < 0)
5498c2ecf20Sopenharmony_ci		return ret;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	/* ensure interrupts are disabled, bootloaders can be strange */
5528c2ecf20Sopenharmony_ci	ret = twl_rtc_write_u8(twl_rtc, 0, REG_RTC_INTERRUPTS_REG);
5538c2ecf20Sopenharmony_ci	if (ret < 0)
5548c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev, "unable to disable interrupt\n");
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	/* init cached IRQ enable bits */
5578c2ecf20Sopenharmony_ci	ret = twl_rtc_read_u8(twl_rtc, &twl_rtc->rtc_irq_bits,
5588c2ecf20Sopenharmony_ci			      REG_RTC_INTERRUPTS_REG);
5598c2ecf20Sopenharmony_ci	if (ret < 0)
5608c2ecf20Sopenharmony_ci		return ret;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, twl_rtc);
5638c2ecf20Sopenharmony_ci	device_init_wakeup(&pdev->dev, 1);
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	twl_rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
5668c2ecf20Sopenharmony_ci					&twl_rtc_ops, THIS_MODULE);
5678c2ecf20Sopenharmony_ci	if (IS_ERR(twl_rtc->rtc)) {
5688c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
5698c2ecf20Sopenharmony_ci			PTR_ERR(twl_rtc->rtc));
5708c2ecf20Sopenharmony_ci		return PTR_ERR(twl_rtc->rtc);
5718c2ecf20Sopenharmony_ci	}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
5748c2ecf20Sopenharmony_ci					twl_rtc_interrupt,
5758c2ecf20Sopenharmony_ci					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
5768c2ecf20Sopenharmony_ci					dev_name(&twl_rtc->rtc->dev), twl_rtc);
5778c2ecf20Sopenharmony_ci	if (ret < 0) {
5788c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "IRQ is not free.\n");
5798c2ecf20Sopenharmony_ci		return ret;
5808c2ecf20Sopenharmony_ci	}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	return 0;
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci/*
5868c2ecf20Sopenharmony_ci * Disable all TWL RTC module interrupts.
5878c2ecf20Sopenharmony_ci * Sets status flag to free.
5888c2ecf20Sopenharmony_ci */
5898c2ecf20Sopenharmony_cistatic int twl_rtc_remove(struct platform_device *pdev)
5908c2ecf20Sopenharmony_ci{
5918c2ecf20Sopenharmony_ci	struct twl_rtc *twl_rtc = platform_get_drvdata(pdev);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	/* leave rtc running, but disable irqs */
5948c2ecf20Sopenharmony_ci	mask_rtc_irq_bit(twl_rtc, BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
5958c2ecf20Sopenharmony_ci	mask_rtc_irq_bit(twl_rtc, BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
5968c2ecf20Sopenharmony_ci	if (twl_rtc->class == TWL_6030) {
5978c2ecf20Sopenharmony_ci		twl6030_interrupt_mask(TWL6030_RTC_INT_MASK,
5988c2ecf20Sopenharmony_ci			REG_INT_MSK_LINE_A);
5998c2ecf20Sopenharmony_ci		twl6030_interrupt_mask(TWL6030_RTC_INT_MASK,
6008c2ecf20Sopenharmony_ci			REG_INT_MSK_STS_A);
6018c2ecf20Sopenharmony_ci	}
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	return 0;
6048c2ecf20Sopenharmony_ci}
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_cistatic void twl_rtc_shutdown(struct platform_device *pdev)
6078c2ecf20Sopenharmony_ci{
6088c2ecf20Sopenharmony_ci	struct twl_rtc *twl_rtc = platform_get_drvdata(pdev);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	/* mask timer interrupts, but leave alarm interrupts on to enable
6118c2ecf20Sopenharmony_ci	   power-on when alarm is triggered */
6128c2ecf20Sopenharmony_ci	mask_rtc_irq_bit(twl_rtc, BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
6138c2ecf20Sopenharmony_ci}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
6168c2ecf20Sopenharmony_cistatic int twl_rtc_suspend(struct device *dev)
6178c2ecf20Sopenharmony_ci{
6188c2ecf20Sopenharmony_ci	struct twl_rtc *twl_rtc = dev_get_drvdata(dev);
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	twl_rtc->irqstat = twl_rtc->rtc_irq_bits;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	mask_rtc_irq_bit(twl_rtc, BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
6238c2ecf20Sopenharmony_ci	return 0;
6248c2ecf20Sopenharmony_ci}
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_cistatic int twl_rtc_resume(struct device *dev)
6278c2ecf20Sopenharmony_ci{
6288c2ecf20Sopenharmony_ci	struct twl_rtc *twl_rtc = dev_get_drvdata(dev);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	set_rtc_irq_bit(twl_rtc, twl_rtc->irqstat);
6318c2ecf20Sopenharmony_ci	return 0;
6328c2ecf20Sopenharmony_ci}
6338c2ecf20Sopenharmony_ci#endif
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(twl_rtc_pm_ops, twl_rtc_suspend, twl_rtc_resume);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_cistatic const struct of_device_id twl_rtc_of_match[] = {
6388c2ecf20Sopenharmony_ci	{.compatible = "ti,twl4030-rtc", },
6398c2ecf20Sopenharmony_ci	{ },
6408c2ecf20Sopenharmony_ci};
6418c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, twl_rtc_of_match);
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_cistatic struct platform_driver twl4030rtc_driver = {
6448c2ecf20Sopenharmony_ci	.probe		= twl_rtc_probe,
6458c2ecf20Sopenharmony_ci	.remove		= twl_rtc_remove,
6468c2ecf20Sopenharmony_ci	.shutdown	= twl_rtc_shutdown,
6478c2ecf20Sopenharmony_ci	.driver		= {
6488c2ecf20Sopenharmony_ci		.name		= "twl_rtc",
6498c2ecf20Sopenharmony_ci		.pm		= &twl_rtc_pm_ops,
6508c2ecf20Sopenharmony_ci		.of_match_table = twl_rtc_of_match,
6518c2ecf20Sopenharmony_ci	},
6528c2ecf20Sopenharmony_ci};
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_cimodule_platform_driver(twl4030rtc_driver);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ciMODULE_AUTHOR("Texas Instruments, MontaVista Software");
6578c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
658