Orange Pi5 kernel

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

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Real time clock driver for DA9052
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright(c) 2012 Dialog Semiconductor Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Dajun Dajun Chen <dajun.chen@diasemi.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/mfd/da9052/da9052.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mfd/da9052/reg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define rtc_err(rtc, fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 		dev_err(rtc->da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define DA9052_GET_TIME_RETRIES 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) struct da9052_rtc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct da9052 *da9052;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static int da9052_rtc_enable_alarm(struct da9052_rtc *rtc, bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 				DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 				DA9052_ALARM_Y_ALARM_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			rtc_err(rtc, "Failed to enable ALM: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 			DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			rtc_err(rtc, "Write error: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static irqreturn_t da9052_rtc_irq(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct da9052_rtc *rtc = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static int da9052_read_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	uint8_t v[2][5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	int idx = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int timeout = DA9052_GET_TIME_RETRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	ret = da9052_group_read(rtc->da9052, DA9052_ALARM_MI_REG, 5, &v[0][0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		rtc_err(rtc, "Failed to group read ALM: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		ret = da9052_group_read(rtc->da9052,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 					DA9052_ALARM_MI_REG, 5, &v[idx][0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			rtc_err(rtc, "Failed to group read ALM: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		if (memcmp(&v[0][0], &v[1][0], 5) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			rtc_tm->tm_year = (v[0][4] & DA9052_RTC_YEAR) + 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			rtc_tm->tm_mon  = (v[0][3] & DA9052_RTC_MONTH) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			rtc_tm->tm_mday = v[0][2] & DA9052_RTC_DAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			rtc_tm->tm_hour = v[0][1] & DA9052_RTC_HOUR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			rtc_tm->tm_min  = v[0][0] & DA9052_RTC_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			rtc_tm->tm_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			ret = rtc_valid_tm(rtc_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		idx = (1-idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	} while (timeout--);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	rtc_err(rtc, "Timed out reading alarm time\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static int da9052_set_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct da9052 *da9052 = rtc->da9052;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	unsigned long alm_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	uint8_t v[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	alm_time = rtc_tm_to_time64(rtc_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (rtc_tm->tm_sec > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		alm_time += 60 - rtc_tm->tm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		rtc_time64_to_tm(alm_time, rtc_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	BUG_ON(rtc_tm->tm_sec); /* it will cause repeated irqs if not zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	rtc_tm->tm_year -= 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	rtc_tm->tm_mon += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	ret = da9052_reg_update(da9052, DA9052_ALARM_MI_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 				DA9052_RTC_MIN, rtc_tm->tm_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		rtc_err(rtc, "Failed to write ALRM MIN: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	v[0] = rtc_tm->tm_hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	v[1] = rtc_tm->tm_mday;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	v[2] = rtc_tm->tm_mon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	ret = da9052_group_write(da9052, DA9052_ALARM_H_REG, 3, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	ret = da9052_reg_update(da9052, DA9052_ALARM_Y_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				DA9052_RTC_YEAR, rtc_tm->tm_year);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		rtc_err(rtc, "Failed to write ALRM YEAR: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int da9052_rtc_get_alarm_status(struct da9052_rtc *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	ret = da9052_reg_read(rtc->da9052, DA9052_ALARM_Y_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		rtc_err(rtc, "Failed to read ALM: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return !!(ret&DA9052_ALARM_Y_ALARM_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static int da9052_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct da9052_rtc *rtc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	uint8_t v[2][6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	int idx = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	int timeout = DA9052_GET_TIME_RETRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	ret = da9052_group_read(rtc->da9052, DA9052_COUNT_S_REG, 6, &v[0][0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		rtc_err(rtc, "Failed to read RTC time : %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		ret = da9052_group_read(rtc->da9052,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 					DA9052_COUNT_S_REG, 6, &v[idx][0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			rtc_err(rtc, "Failed to read RTC time : %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		if (memcmp(&v[0][0], &v[1][0], 6) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			rtc_tm->tm_year = (v[0][5] & DA9052_RTC_YEAR) + 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			rtc_tm->tm_mon  = (v[0][4] & DA9052_RTC_MONTH) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			rtc_tm->tm_mday = v[0][3] & DA9052_RTC_DAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			rtc_tm->tm_hour = v[0][2] & DA9052_RTC_HOUR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			rtc_tm->tm_min  = v[0][1] & DA9052_RTC_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			rtc_tm->tm_sec  = v[0][0] & DA9052_RTC_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		idx = (1-idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	} while (timeout--);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	rtc_err(rtc, "Timed out reading time\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static int da9052_rtc_set_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	struct da9052_rtc *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	uint8_t v[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	/* DA9052 only has 6 bits for year - to represent 2000-2063 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if ((tm->tm_year < 100) || (tm->tm_year > 163))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	rtc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	v[0] = tm->tm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	v[1] = tm->tm_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	v[2] = tm->tm_hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	v[3] = tm->tm_mday;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	v[4] = tm->tm_mon + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	v[5] = tm->tm_year - 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	ret = da9052_group_write(rtc->da9052, DA9052_COUNT_S_REG, 6, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		rtc_err(rtc, "failed to set RTC time: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static int da9052_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	struct rtc_time *tm = &alrm->time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	struct da9052_rtc *rtc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	ret = da9052_read_alarm(rtc, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		rtc_err(rtc, "failed to read RTC alarm: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	alrm->enabled = da9052_rtc_get_alarm_status(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static int da9052_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct rtc_time *tm = &alrm->time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct da9052_rtc *rtc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	/* DA9052 only has 6 bits for year - to represent 2000-2063 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if ((tm->tm_year < 100) || (tm->tm_year > 163))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	ret = da9052_rtc_enable_alarm(rtc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	ret = da9052_set_alarm(rtc, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	ret = da9052_rtc_enable_alarm(rtc, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static int da9052_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	struct da9052_rtc *rtc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	return da9052_rtc_enable_alarm(rtc, enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static const struct rtc_class_ops da9052_rtc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	.read_time	= da9052_rtc_read_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	.set_time	= da9052_rtc_set_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	.read_alarm	= da9052_rtc_read_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	.set_alarm	= da9052_rtc_set_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	.alarm_irq_enable = da9052_rtc_alarm_irq_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static int da9052_rtc_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct da9052_rtc *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	rtc = devm_kzalloc(&pdev->dev, sizeof(struct da9052_rtc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (!rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	rtc->da9052 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	platform_set_drvdata(pdev, rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	ret = da9052_reg_write(rtc->da9052, DA9052_BBAT_CONT_REG, 0xFE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		rtc_err(rtc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			"Failed to setup RTC battery charging: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 				DA9052_ALARM_Y_TICK_ON, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		rtc_err(rtc, "Failed to disable TICKS: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	device_init_wakeup(&pdev->dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (IS_ERR(rtc->rtc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		return PTR_ERR(rtc->rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	rtc->rtc->ops = &da9052_rtc_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	rtc->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	rtc->rtc->range_max = RTC_TIMESTAMP_END_2063;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	ret = rtc_register_device(rtc->rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	ret = da9052_request_irq(rtc->da9052, DA9052_IRQ_ALARM, "ALM",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 				da9052_rtc_irq, rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		rtc_err(rtc, "irq registration failed: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static struct platform_driver da9052_rtc_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	.probe	= da9052_rtc_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		.name	= "da9052-rtc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) module_platform_driver(da9052_rtc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) MODULE_ALIAS("platform:da9052-rtc");