1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Real Time Clock (RTC) Driver for sd3078
4  * Copyright (C) 2018 Zoro Li
5  */
6 
7 #include <linux/bcd.h>
8 #include <linux/i2c.h>
9 #include <linux/module.h>
10 #include <linux/regmap.h>
11 #include <linux/rtc.h>
12 #include <linux/slab.h>
13 
14 #define SD3078_REG_SC			0x00
15 #define SD3078_REG_MN			0x01
16 #define SD3078_REG_HR			0x02
17 #define SD3078_REG_DW			0x03
18 #define SD3078_REG_DM			0x04
19 #define SD3078_REG_MO			0x05
20 #define SD3078_REG_YR			0x06
21 
22 #define SD3078_REG_CTRL1		0x0f
23 #define SD3078_REG_CTRL2		0x10
24 #define SD3078_REG_CTRL3		0x11
25 
26 #define KEY_WRITE1		0x80
27 #define KEY_WRITE2		0x04
28 #define KEY_WRITE3		0x80
29 
30 #define NUM_TIME_REGS   (SD3078_REG_YR - SD3078_REG_SC + 1)
31 
32 /*
33  * The sd3078 has write protection
34  * and we can choose whether or not to use it.
35  * Write protection is turned off by default.
36  */
37 #define WRITE_PROTECT_EN	0
38 
39 /*
40  * In order to prevent arbitrary modification of the time register,
41  * when modification of the register,
42  * the "write" bit needs to be written in a certain order.
43  * 1. set WRITE1 bit
44  * 2. set WRITE2 bit
45  * 3. set WRITE3 bit
46  */
47 static void sd3078_enable_reg_write(struct regmap *regmap)
48 {
49 	regmap_update_bits(regmap, SD3078_REG_CTRL2, KEY_WRITE1, KEY_WRITE1);
50 	regmap_update_bits(regmap, SD3078_REG_CTRL1, KEY_WRITE2, KEY_WRITE2);
51 	regmap_update_bits(regmap, SD3078_REG_CTRL1, KEY_WRITE3, KEY_WRITE3);
52 }
53 
54 #if WRITE_PROTECT_EN
55 /*
56  * In order to prevent arbitrary modification of the time register,
57  * we should disable the write function.
58  * when disable write,
59  * the "write" bit needs to be clear in a certain order.
60  * 1. clear WRITE2 bit
61  * 2. clear WRITE3 bit
62  * 3. clear WRITE1 bit
63  */
64 static void sd3078_disable_reg_write(struct regmap *regmap)
65 {
66 	regmap_update_bits(regmap, SD3078_REG_CTRL1, KEY_WRITE2, 0);
67 	regmap_update_bits(regmap, SD3078_REG_CTRL1, KEY_WRITE3, 0);
68 	regmap_update_bits(regmap, SD3078_REG_CTRL2, KEY_WRITE1, 0);
69 }
70 #endif
71 
72 static int sd3078_rtc_read_time(struct device *dev, struct rtc_time *tm)
73 {
74 	unsigned char hour;
75 	unsigned char rtc_data[NUM_TIME_REGS] = {0};
76 	struct i2c_client *client = to_i2c_client(dev);
77 	struct regmap *regmap = i2c_get_clientdata(client);
78 	int ret;
79 
80 	ret = regmap_bulk_read(regmap, SD3078_REG_SC, rtc_data, NUM_TIME_REGS);
81 	if (ret < 0) {
82 		dev_err(dev, "reading from RTC failed with err:%d\n", ret);
83 		return ret;
84 	}
85 
86 	tm->tm_sec	= bcd2bin(rtc_data[SD3078_REG_SC] & 0x7F);
87 	tm->tm_min	= bcd2bin(rtc_data[SD3078_REG_MN] & 0x7F);
88 
89 	/*
90 	 * The sd3078 supports 12/24 hour mode.
91 	 * When getting time,
92 	 * we need to convert the 12 hour mode to the 24 hour mode.
93 	 */
94 	hour = rtc_data[SD3078_REG_HR];
95 	if (hour & 0x80) /* 24H MODE */
96 		tm->tm_hour = bcd2bin(rtc_data[SD3078_REG_HR] & 0x3F);
97 	else if (hour & 0x20) /* 12H MODE PM */
98 		tm->tm_hour = bcd2bin(rtc_data[SD3078_REG_HR] & 0x1F) + 12;
99 	else /* 12H MODE AM */
100 		tm->tm_hour = bcd2bin(rtc_data[SD3078_REG_HR] & 0x1F);
101 
102 	tm->tm_mday = bcd2bin(rtc_data[SD3078_REG_DM] & 0x3F);
103 	tm->tm_wday = rtc_data[SD3078_REG_DW] & 0x07;
104 	tm->tm_mon	= bcd2bin(rtc_data[SD3078_REG_MO] & 0x1F) - 1;
105 	tm->tm_year = bcd2bin(rtc_data[SD3078_REG_YR]) + 100;
106 
107 	return 0;
108 }
109 
110 static int sd3078_rtc_set_time(struct device *dev, struct rtc_time *tm)
111 {
112 	unsigned char rtc_data[NUM_TIME_REGS];
113 	struct i2c_client *client = to_i2c_client(dev);
114 	struct regmap *regmap = i2c_get_clientdata(client);
115 	int ret;
116 
117 	rtc_data[SD3078_REG_SC] = bin2bcd(tm->tm_sec);
118 	rtc_data[SD3078_REG_MN] = bin2bcd(tm->tm_min);
119 	rtc_data[SD3078_REG_HR] = bin2bcd(tm->tm_hour) | 0x80;
120 	rtc_data[SD3078_REG_DM] = bin2bcd(tm->tm_mday);
121 	rtc_data[SD3078_REG_DW] = tm->tm_wday & 0x07;
122 	rtc_data[SD3078_REG_MO] = bin2bcd(tm->tm_mon) + 1;
123 	rtc_data[SD3078_REG_YR] = bin2bcd(tm->tm_year - 100);
124 
125 #if WRITE_PROTECT_EN
126 	sd3078_enable_reg_write(regmap);
127 #endif
128 
129 	ret = regmap_bulk_write(regmap, SD3078_REG_SC, rtc_data,
130 				NUM_TIME_REGS);
131 	if (ret < 0) {
132 		dev_err(dev, "writing to RTC failed with err:%d\n", ret);
133 		return ret;
134 	}
135 
136 #if WRITE_PROTECT_EN
137 	sd3078_disable_reg_write(regmap);
138 #endif
139 
140 	return 0;
141 }
142 
143 static const struct rtc_class_ops sd3078_rtc_ops = {
144 	.read_time	= sd3078_rtc_read_time,
145 	.set_time	= sd3078_rtc_set_time,
146 };
147 
148 static const struct regmap_config regmap_config = {
149 	.reg_bits = 8,
150 	.val_bits = 8,
151 	.max_register = 0x11,
152 };
153 
154 static int sd3078_probe(struct i2c_client *client)
155 {
156 	int ret;
157 	struct regmap *regmap;
158 	struct rtc_device *rtc;
159 
160 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
161 		return -ENODEV;
162 
163 	regmap = devm_regmap_init_i2c(client, &regmap_config);
164 	if (IS_ERR(regmap)) {
165 		dev_err(&client->dev, "regmap allocation failed\n");
166 		return PTR_ERR(regmap);
167 	}
168 
169 	i2c_set_clientdata(client, regmap);
170 
171 	rtc = devm_rtc_allocate_device(&client->dev);
172 	if (IS_ERR(rtc))
173 		return PTR_ERR(rtc);
174 
175 	rtc->ops = &sd3078_rtc_ops;
176 	rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
177 	rtc->range_max = RTC_TIMESTAMP_END_2099;
178 
179 	ret = devm_rtc_register_device(rtc);
180 	if (ret)
181 		return ret;
182 
183 	sd3078_enable_reg_write(regmap);
184 
185 	return 0;
186 }
187 
188 static const struct i2c_device_id sd3078_id[] = {
189 	{ "sd3078" },
190 	{ }
191 };
192 MODULE_DEVICE_TABLE(i2c, sd3078_id);
193 
194 static const __maybe_unused struct of_device_id rtc_dt_match[] = {
195 	{ .compatible = "whwave,sd3078" },
196 	{},
197 };
198 MODULE_DEVICE_TABLE(of, rtc_dt_match);
199 
200 static struct i2c_driver sd3078_driver = {
201 	.driver     = {
202 		.name   = "sd3078",
203 		.of_match_table = of_match_ptr(rtc_dt_match),
204 	},
205 	.probe      = sd3078_probe,
206 	.id_table   = sd3078_id,
207 };
208 
209 module_i2c_driver(sd3078_driver);
210 
211 MODULE_AUTHOR("Dianlong Li <long17.cool@163.com>");
212 MODULE_DESCRIPTION("SD3078 RTC driver");
213 MODULE_LICENSE("GPL v2");
214