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+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // RTC driver for Maxim MAX8998
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) // Copyright (C) 2010 Samsung Electronics Co.Ltd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) // Author: Minkyu Kang <mk7.kang@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) // Author: Joonyoung Shim <jy0922.shim@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/bcd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/irqdomain.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/mfd/max8998.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mfd/max8998-private.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define MAX8998_RTC_SEC			0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define MAX8998_RTC_MIN			0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define MAX8998_RTC_HOUR		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define MAX8998_RTC_WEEKDAY		0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define MAX8998_RTC_DATE		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define MAX8998_RTC_MONTH		0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define MAX8998_RTC_YEAR1		0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define MAX8998_RTC_YEAR2		0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define MAX8998_ALARM0_SEC		0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define MAX8998_ALARM0_MIN		0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define MAX8998_ALARM0_HOUR		0x0a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define MAX8998_ALARM0_WEEKDAY		0x0b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define MAX8998_ALARM0_DATE		0x0c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define MAX8998_ALARM0_MONTH		0x0d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define MAX8998_ALARM0_YEAR1		0x0e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define MAX8998_ALARM0_YEAR2		0x0f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define MAX8998_ALARM1_SEC		0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define MAX8998_ALARM1_MIN		0x11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define MAX8998_ALARM1_HOUR		0x12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define MAX8998_ALARM1_WEEKDAY		0x13
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define MAX8998_ALARM1_DATE		0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define MAX8998_ALARM1_MONTH		0x15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define MAX8998_ALARM1_YEAR1		0x16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define MAX8998_ALARM1_YEAR2		0x17
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define MAX8998_ALARM0_CONF		0x18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define MAX8998_ALARM1_CONF		0x19
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define MAX8998_RTC_STATUS		0x1a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define MAX8998_WTSR_SMPL_CNTL		0x1b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define MAX8998_TEST			0x1f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define HOUR_12				(1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define HOUR_PM				(1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define ALARM0_STATUS			(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define ALARM1_STATUS			(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	RTC_SEC = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	RTC_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	RTC_HOUR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	RTC_WEEKDAY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	RTC_DATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	RTC_MONTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	RTC_YEAR1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	RTC_YEAR2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) struct max8998_rtc_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct device		*dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct max8998_dev	*max8998;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct i2c_client	*rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct rtc_device	*rtc_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	bool lp3974_bug_workaround;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static void max8998_data_to_tm(u8 *data, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	tm->tm_sec = bcd2bin(data[RTC_SEC]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	tm->tm_min = bcd2bin(data[RTC_MIN]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (data[RTC_HOUR] & HOUR_12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		if (data[RTC_HOUR] & HOUR_PM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			tm->tm_hour += 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	tm->tm_mday = bcd2bin(data[RTC_DATE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	tm->tm_mon = bcd2bin(data[RTC_MONTH]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	tm->tm_year -= 1900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static void max8998_tm_to_data(struct rtc_time *tm, u8 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	data[RTC_SEC] = bin2bcd(tm->tm_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	data[RTC_MIN] = bin2bcd(tm->tm_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	data[RTC_HOUR] = bin2bcd(tm->tm_hour);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	data[RTC_WEEKDAY] = tm->tm_wday;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	data[RTC_DATE] = bin2bcd(tm->tm_mday);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	data[RTC_MONTH] = bin2bcd(tm->tm_mon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int max8998_rtc_read_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct max8998_rtc_info *info = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	u8 data[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	ret = max8998_bulk_read(info->rtc, MAX8998_RTC_SEC, 8, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	max8998_data_to_tm(data, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int max8998_rtc_set_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	struct max8998_rtc_info *info = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	u8 data[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	max8998_tm_to_data(tm, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	ret = max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (info->lp3974_bug_workaround)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		msleep(2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct max8998_rtc_info *info = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	u8 data[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	ret = max8998_bulk_read(info->rtc, MAX8998_ALARM0_SEC, 8, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	max8998_data_to_tm(data, &alrm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	ret = max8998_read_reg(info->rtc, MAX8998_ALARM0_CONF, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	alrm->enabled = !!val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	ret = max8998_read_reg(info->rtc, MAX8998_RTC_STATUS, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (val & ALARM0_STATUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		alrm->pending = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		alrm->pending = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return 0;
^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) static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	int ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (info->lp3974_bug_workaround)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		msleep(2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static int max8998_rtc_start_alarm(struct max8998_rtc_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	u8 alarm0_conf = 0x77;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	/* LP3974 with delay bug chips has rtc alarm bugs with "MONTH" field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (info->lp3974_bug_workaround)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		alarm0_conf = 0x57;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, alarm0_conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (info->lp3974_bug_workaround)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		msleep(2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct max8998_rtc_info *info = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	u8 data[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	max8998_tm_to_data(&alrm->time, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	ret = max8998_rtc_stop_alarm(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	ret = max8998_bulk_write(info->rtc, MAX8998_ALARM0_SEC, 8, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (info->lp3974_bug_workaround)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		msleep(2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (alrm->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		ret = max8998_rtc_start_alarm(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static int max8998_rtc_alarm_irq_enable(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 					unsigned int enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	struct max8998_rtc_info *info = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		return max8998_rtc_start_alarm(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return max8998_rtc_stop_alarm(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static irqreturn_t max8998_rtc_alarm_irq(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	struct max8998_rtc_info *info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static const struct rtc_class_ops max8998_rtc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	.read_time = max8998_rtc_read_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	.set_time = max8998_rtc_set_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	.read_alarm = max8998_rtc_read_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	.set_alarm = max8998_rtc_set_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	.alarm_irq_enable = max8998_rtc_alarm_irq_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static int max8998_rtc_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	struct max8998_platform_data *pdata = max8998->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	struct max8998_rtc_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	info = devm_kzalloc(&pdev->dev, sizeof(struct max8998_rtc_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	info->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	info->max8998 = max8998;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	info->rtc = max8998->rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	platform_set_drvdata(pdev, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8998-rtc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			&max8998_rtc_ops, THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (IS_ERR(info->rtc_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		ret = PTR_ERR(info->rtc_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		return ret;
^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) 	if (!max8998->irq_domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		goto no_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	info->irq = irq_create_mapping(max8998->irq_domain, MAX8998_IRQ_ALARM0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (!info->irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		dev_warn(&pdev->dev, "Failed to map alarm IRQ\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		goto no_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 				max8998_rtc_alarm_irq, 0, "rtc-alarm0", info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			info->irq, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) no_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	dev_info(&pdev->dev, "RTC CHIP NAME: %s\n", pdev->id_entry->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (pdata && pdata->rtc_delay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		info->lp3974_bug_workaround = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		dev_warn(&pdev->dev, "LP3974 with RTC REGERR option."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 				" RTC updates will be extremely slow.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static const struct platform_device_id max8998_rtc_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	{ "max8998-rtc", TYPE_MAX8998 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	{ "lp3974-rtc", TYPE_LP3974 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) MODULE_DEVICE_TABLE(platform, max8998_rtc_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static struct platform_driver max8998_rtc_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		.name	= "max8998-rtc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	.probe		= max8998_rtc_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	.id_table	= max8998_rtc_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) module_platform_driver(max8998_rtc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) MODULE_DESCRIPTION("Maxim MAX8998 RTC driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) MODULE_LICENSE("GPL");