10101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan/*
20101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * rtc-palmas.c -- Palmas Real Time Clock driver.
30101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
40101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * RTC driver for TI Palma series devices like TPS65913,
50101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * TPS65914 power management IC.
60101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan *
70101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * Copyright (c) 2012, NVIDIA Corporation.
80101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan *
90101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * Author: Laxman Dewangan <ldewangan@nvidia.com>
100101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan *
110101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * This program is free software; you can redistribute it and/or
120101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * modify it under the terms of the GNU General Public License as
130101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * published by the Free Software Foundation version 2.
140101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan *
150101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
160101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * whether express or implied; without even the implied warranty of
170101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
180101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * General Public License for more details.
190101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan *
200101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * You should have received a copy of the GNU General Public License
210101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * along with this program; if not, write to the Free Software
220101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
230101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan * 02111-1307, USA
240101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan */
250101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
260101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#include <linux/bcd.h>
270101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#include <linux/errno.h>
280101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#include <linux/init.h>
290101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#include <linux/interrupt.h>
300101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#include <linux/kernel.h>
310101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#include <linux/mfd/palmas.h>
320101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#include <linux/module.h>
330288d2e32653efc55943768bd12e2bbf20ce9750Laxman Dewangan#include <linux/of.h>
340101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#include <linux/rtc.h>
350101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#include <linux/types.h>
360101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#include <linux/platform_device.h>
370101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#include <linux/pm.h>
380101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
390101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstruct palmas_rtc {
400101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct rtc_device	*rtc;
410101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct device		*dev;
420101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	unsigned int		irq;
430101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan};
440101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
450101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan/* Total number of RTC registers needed to set time*/
460101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#define PALMAS_NUM_TIME_REGS	(PALMAS_YEARS_REG - PALMAS_SECONDS_REG + 1)
470101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
480101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic int palmas_rtc_read_time(struct device *dev, struct rtc_time *tm)
490101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan{
500101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	unsigned char rtc_data[PALMAS_NUM_TIME_REGS];
510101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct palmas *palmas = dev_get_drvdata(dev->parent);
520101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	int ret;
530101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
540101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	/* Copy RTC counting registers to static registers or latches */
550101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
560101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		PALMAS_RTC_CTRL_REG_GET_TIME, PALMAS_RTC_CTRL_REG_GET_TIME);
570101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
580101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(dev, "RTC CTRL reg update failed, err: %d\n", ret);
590101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
600101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
610101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
620101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_bulk_read(palmas, PALMAS_RTC_BASE, PALMAS_SECONDS_REG,
630101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan			rtc_data, PALMAS_NUM_TIME_REGS);
640101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
650101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(dev, "RTC_SECONDS reg read failed, err = %d\n", ret);
660101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
670101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
680101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
690101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	tm->tm_sec = bcd2bin(rtc_data[0]);
700101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	tm->tm_min = bcd2bin(rtc_data[1]);
710101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	tm->tm_hour = bcd2bin(rtc_data[2]);
720101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	tm->tm_mday = bcd2bin(rtc_data[3]);
730101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
740101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	tm->tm_year = bcd2bin(rtc_data[5]) + 100;
750101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
760101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	return ret;
770101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan}
780101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
790101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic int palmas_rtc_set_time(struct device *dev, struct rtc_time *tm)
800101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan{
810101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	unsigned char rtc_data[PALMAS_NUM_TIME_REGS];
820101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct palmas *palmas = dev_get_drvdata(dev->parent);
830101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	int ret;
840101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
850101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	rtc_data[0] = bin2bcd(tm->tm_sec);
860101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	rtc_data[1] = bin2bcd(tm->tm_min);
870101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	rtc_data[2] = bin2bcd(tm->tm_hour);
880101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	rtc_data[3] = bin2bcd(tm->tm_mday);
890101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	rtc_data[4] = bin2bcd(tm->tm_mon + 1);
900101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	rtc_data[5] = bin2bcd(tm->tm_year - 100);
910101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
920101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	/* Stop RTC while updating the RTC time registers */
930101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
940101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		PALMAS_RTC_CTRL_REG_STOP_RTC, 0);
950101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
960101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(dev, "RTC stop failed, err = %d\n", ret);
970101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
980101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
990101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1000101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_bulk_write(palmas, PALMAS_RTC_BASE, PALMAS_SECONDS_REG,
1010101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		rtc_data, PALMAS_NUM_TIME_REGS);
1020101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
1030101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(dev, "RTC_SECONDS reg write failed, err = %d\n", ret);
1040101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
1050101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
1060101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1070101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	/* Start back RTC */
1080101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
1090101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		PALMAS_RTC_CTRL_REG_STOP_RTC, PALMAS_RTC_CTRL_REG_STOP_RTC);
1100101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0)
1110101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(dev, "RTC start failed, err = %d\n", ret);
1120101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	return ret;
1130101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan}
1140101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1150101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic int palmas_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
1160101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan{
1170101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct palmas *palmas = dev_get_drvdata(dev->parent);
1180101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	u8 val;
1190101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1200101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	val = enabled ? PALMAS_RTC_INTERRUPTS_REG_IT_ALARM : 0;
1210101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	return palmas_write(palmas, PALMAS_RTC_BASE,
1220101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		PALMAS_RTC_INTERRUPTS_REG, val);
1230101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan}
1240101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1250101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic int palmas_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
1260101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan{
1270101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	unsigned char alarm_data[PALMAS_NUM_TIME_REGS];
1280101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	u32 int_val;
1290101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct palmas *palmas = dev_get_drvdata(dev->parent);
1300101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	int ret;
1310101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1320101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_bulk_read(palmas, PALMAS_RTC_BASE,
1330101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan			PALMAS_ALARM_SECONDS_REG,
1340101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan			alarm_data, PALMAS_NUM_TIME_REGS);
1350101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
1360101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(dev, "RTC_ALARM_SECONDS read failed, err = %d\n", ret);
1370101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
1380101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
1390101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1400101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	alm->time.tm_sec = bcd2bin(alarm_data[0]);
1410101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	alm->time.tm_min = bcd2bin(alarm_data[1]);
1420101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	alm->time.tm_hour = bcd2bin(alarm_data[2]);
1430101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	alm->time.tm_mday = bcd2bin(alarm_data[3]);
1440101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	alm->time.tm_mon = bcd2bin(alarm_data[4]) - 1;
1450101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	alm->time.tm_year = bcd2bin(alarm_data[5]) + 100;
1460101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1470101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_read(palmas, PALMAS_RTC_BASE, PALMAS_RTC_INTERRUPTS_REG,
1480101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan			&int_val);
1490101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
1500101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(dev, "RTC_INTERRUPTS reg read failed, err = %d\n", ret);
1510101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
1520101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
1530101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1540101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (int_val & PALMAS_RTC_INTERRUPTS_REG_IT_ALARM)
1550101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		alm->enabled = 1;
1560101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	return ret;
1570101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan}
1580101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1590101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic int palmas_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
1600101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan{
1610101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	unsigned char alarm_data[PALMAS_NUM_TIME_REGS];
1620101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct palmas *palmas = dev_get_drvdata(dev->parent);
1630101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	int ret;
1640101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1650101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_rtc_alarm_irq_enable(dev, 0);
1660101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
1670101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(dev, "Disable RTC alarm failed\n");
1680101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
1690101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
1700101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1710101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	alarm_data[0] = bin2bcd(alm->time.tm_sec);
1720101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	alarm_data[1] = bin2bcd(alm->time.tm_min);
1730101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	alarm_data[2] = bin2bcd(alm->time.tm_hour);
1740101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	alarm_data[3] = bin2bcd(alm->time.tm_mday);
1750101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	alarm_data[4] = bin2bcd(alm->time.tm_mon + 1);
1760101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	alarm_data[5] = bin2bcd(alm->time.tm_year - 100);
1770101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1780101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_bulk_write(palmas, PALMAS_RTC_BASE,
1790101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		PALMAS_ALARM_SECONDS_REG, alarm_data, PALMAS_NUM_TIME_REGS);
1800101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
1810101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(dev, "ALARM_SECONDS_REG write failed, err = %d\n", ret);
1820101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
1830101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
1840101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1850101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (alm->enabled)
1860101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		ret = palmas_rtc_alarm_irq_enable(dev, 1);
1870101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	return ret;
1880101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan}
1890101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1900101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic int palmas_clear_interrupts(struct device *dev)
1910101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan{
1920101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct palmas *palmas = dev_get_drvdata(dev->parent);
1930101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	unsigned int rtc_reg;
1940101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	int ret;
1950101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
1960101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_read(palmas, PALMAS_RTC_BASE, PALMAS_RTC_STATUS_REG,
1970101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan				&rtc_reg);
1980101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
1990101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(dev, "RTC_STATUS read failed, err = %d\n", ret);
2000101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
2010101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
2020101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
2030101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_write(palmas, PALMAS_RTC_BASE, PALMAS_RTC_STATUS_REG,
2040101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan			rtc_reg);
2050101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
2060101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(dev, "RTC_STATUS write failed, err = %d\n", ret);
2070101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
2080101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
2090101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	return 0;
2100101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan}
2110101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
2120101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic irqreturn_t palmas_rtc_interrupt(int irq, void *context)
2130101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan{
2140101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct palmas_rtc *palmas_rtc = context;
2150101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct device *dev = palmas_rtc->dev;
2160101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	int ret;
2170101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
2180101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_clear_interrupts(dev);
2190101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
2200101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(dev, "RTC interrupt clear failed, err = %d\n", ret);
2210101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return IRQ_NONE;
2220101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
2230101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
2240101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	rtc_update_irq(palmas_rtc->rtc, 1, RTC_IRQF | RTC_AF);
2250101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	return IRQ_HANDLED;
2260101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan}
2270101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
2280101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic struct rtc_class_ops palmas_rtc_ops = {
2290101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	.read_time	= palmas_rtc_read_time,
2300101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	.set_time	= palmas_rtc_set_time,
2310101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	.read_alarm	= palmas_rtc_read_alarm,
2320101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	.set_alarm	= palmas_rtc_set_alarm,
2330101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	.alarm_irq_enable = palmas_rtc_alarm_irq_enable,
2340101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan};
2350101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
2360101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic int palmas_rtc_probe(struct platform_device *pdev)
2370101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan{
2380101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
2390101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct palmas_rtc *palmas_rtc = NULL;
2400101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	int ret;
241666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan	bool enable_bb_charging = false;
242666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan	bool high_bb_charging;
243666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan
244666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan	if (pdev->dev.of_node) {
245666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan		enable_bb_charging = of_property_read_bool(pdev->dev.of_node,
246666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan					"ti,backup-battery-chargeable");
247666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan		high_bb_charging = of_property_read_bool(pdev->dev.of_node,
248666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan					"ti,backup-battery-charge-high-current");
249666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan	}
2500101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
2510101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	palmas_rtc = devm_kzalloc(&pdev->dev, sizeof(struct palmas_rtc),
2520101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan			GFP_KERNEL);
2530101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (!palmas_rtc)
2540101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return -ENOMEM;
2550101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
2560101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	/* Clear pending interrupts */
2570101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_clear_interrupts(&pdev->dev);
2580101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
2590101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(&pdev->dev, "clear RTC int failed, err = %d\n", ret);
2600101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
2610101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
2620101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
2630101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	palmas_rtc->dev = &pdev->dev;
2640101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	platform_set_drvdata(pdev, palmas_rtc);
2650101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
266666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan	if (enable_bb_charging) {
267666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan		unsigned reg = PALMAS_BACKUP_BATTERY_CTRL_BBS_BBC_LOW_ICHRG;
268666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan
269666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan		if (high_bb_charging)
270666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan			reg = 0;
271666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan
272666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan		ret = palmas_update_bits(palmas, PALMAS_PMU_CONTROL_BASE,
273666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan			PALMAS_BACKUP_BATTERY_CTRL,
274666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan			PALMAS_BACKUP_BATTERY_CTRL_BBS_BBC_LOW_ICHRG, reg);
275666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan		if (ret < 0) {
276666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan			dev_err(&pdev->dev,
277666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan				"BACKUP_BATTERY_CTRL update failed, %d\n", ret);
278666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan			return ret;
279666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan		}
280666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan
281666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan		ret = palmas_update_bits(palmas, PALMAS_PMU_CONTROL_BASE,
282666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan			PALMAS_BACKUP_BATTERY_CTRL,
283666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan			PALMAS_BACKUP_BATTERY_CTRL_BB_CHG_EN,
284666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan			PALMAS_BACKUP_BATTERY_CTRL_BB_CHG_EN);
285666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan		if (ret < 0) {
286666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan			dev_err(&pdev->dev,
287666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan				"BACKUP_BATTERY_CTRL update failed, %d\n", ret);
288666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan			return ret;
289666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan		}
290666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan	}
291666a584d3a765a914642f80deef7a33fb309df5dLaxman Dewangan
2920101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	/* Start RTC */
2930101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
2940101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan			PALMAS_RTC_CTRL_REG_STOP_RTC,
2950101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan			PALMAS_RTC_CTRL_REG_STOP_RTC);
2960101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
2970101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(&pdev->dev, "RTC_CTRL write failed, err = %d\n", ret);
2980101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
2990101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
3000101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
3010101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	palmas_rtc->irq = platform_get_irq(pdev, 0);
3020101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
3032c5a5b30917b725ea95fa189e52fc8412f6a2814Wei Ni	device_init_wakeup(&pdev->dev, 1);
30427971335ce666feb08344ca7c5fdd25f13985f62Jingoo Han	palmas_rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
3050101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan				&palmas_rtc_ops, THIS_MODULE);
3060101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (IS_ERR(palmas_rtc->rtc)) {
3070101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		ret = PTR_ERR(palmas_rtc->rtc);
3080101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(&pdev->dev, "RTC register failed, err = %d\n", ret);
3090101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
3100101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
3110101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
31227971335ce666feb08344ca7c5fdd25f13985f62Jingoo Han	ret = devm_request_threaded_irq(&pdev->dev, palmas_rtc->irq, NULL,
3130101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan			palmas_rtc_interrupt,
3140101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan			IRQF_TRIGGER_LOW | IRQF_ONESHOT |
3150101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan			IRQF_EARLY_RESUME,
3160101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan			dev_name(&pdev->dev), palmas_rtc);
3170101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (ret < 0) {
3180101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		dev_err(&pdev->dev, "IRQ request failed, err = %d\n", ret);
3190101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		return ret;
3200101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	}
3210101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
3220101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	return 0;
3230101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan}
3240101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
3250101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic int palmas_rtc_remove(struct platform_device *pdev)
3260101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan{
3270101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	palmas_rtc_alarm_irq_enable(&pdev->dev, 0);
3280101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	return 0;
3290101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan}
3300101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
3310101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#ifdef CONFIG_PM_SLEEP
3320101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic int palmas_rtc_suspend(struct device *dev)
3330101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan{
3340101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct palmas_rtc *palmas_rtc = dev_get_drvdata(dev);
3350101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
3360101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (device_may_wakeup(dev))
3370101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		enable_irq_wake(palmas_rtc->irq);
3380101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	return 0;
3390101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan}
3400101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
3410101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic int palmas_rtc_resume(struct device *dev)
3420101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan{
3430101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	struct palmas_rtc *palmas_rtc = dev_get_drvdata(dev);
3440101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
3450101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	if (device_may_wakeup(dev))
3460101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		disable_irq_wake(palmas_rtc->irq);
3470101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	return 0;
3480101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan}
3490101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan#endif
3500101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
3513f93822d36fc8413658a67f5fb853bbe1b2a1f4dJingoo Hanstatic SIMPLE_DEV_PM_OPS(palmas_rtc_pm_ops, palmas_rtc_suspend,
3523f93822d36fc8413658a67f5fb853bbe1b2a1f4dJingoo Han			 palmas_rtc_resume);
3530101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
3540288d2e32653efc55943768bd12e2bbf20ce9750Laxman Dewangan#ifdef CONFIG_OF
3557abe3f5404fae84cecd851e140a2218ba9d24e4fJingoo Hanstatic const struct of_device_id of_palmas_rtc_match[] = {
3560288d2e32653efc55943768bd12e2bbf20ce9750Laxman Dewangan	{ .compatible = "ti,palmas-rtc"},
3570288d2e32653efc55943768bd12e2bbf20ce9750Laxman Dewangan	{ },
3580288d2e32653efc55943768bd12e2bbf20ce9750Laxman Dewangan};
3590288d2e32653efc55943768bd12e2bbf20ce9750Laxman DewanganMODULE_DEVICE_TABLE(of, of_palmas_rtc_match);
3600288d2e32653efc55943768bd12e2bbf20ce9750Laxman Dewangan#endif
3610288d2e32653efc55943768bd12e2bbf20ce9750Laxman Dewangan
3620101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganstatic struct platform_driver palmas_rtc_driver = {
3630101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	.probe		= palmas_rtc_probe,
3640101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	.remove		= palmas_rtc_remove,
3650101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	.driver		= {
3660101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		.owner	= THIS_MODULE,
3670101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		.name	= "palmas-rtc",
3680101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan		.pm	= &palmas_rtc_pm_ops,
3690288d2e32653efc55943768bd12e2bbf20ce9750Laxman Dewangan		.of_match_table = of_match_ptr(of_palmas_rtc_match),
3700101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan	},
3710101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan};
3720101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
3730101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewanganmodule_platform_driver(palmas_rtc_driver);
3740101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman Dewangan
3750101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman DewanganMODULE_ALIAS("platform:palmas_rtc");
3760101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman DewanganMODULE_DESCRIPTION("TI PALMAS series RTC driver");
3770101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman DewanganMODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
3780101e53cbb1c653738de8dfddc80c0faa5a4fd39Laxman DewanganMODULE_LICENSE("GPL v2");
379