Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / rtc / rtc-sd3078.c
blobf6bee69ba017c5aaa647483ccbe1f2ee37b31af5
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Real Time Clock (RTC) Driver for sd3078
4 * Copyright (C) 2018 Zoro Li
5 */
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>
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
22 #define SD3078_REG_CTRL1 0x0f
23 #define SD3078_REG_CTRL2 0x10
24 #define SD3078_REG_CTRL3 0x11
26 #define KEY_WRITE1 0x80
27 #define KEY_WRITE2 0x04
28 #define KEY_WRITE3 0x80
30 #define NUM_TIME_REGS (SD3078_REG_YR - SD3078_REG_SC + 1)
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.
37 #define WRITE_PROTECT_EN 0
39 struct sd3078 {
40 struct rtc_device *rtc;
41 struct regmap *regmap;
45 * In order to prevent arbitrary modification of the time register,
46 * when modification of the register,
47 * the "write" bit needs to be written in a certain order.
48 * 1. set WRITE1 bit
49 * 2. set WRITE2 bit
50 * 3. set WRITE3 bit
52 static void sd3078_enable_reg_write(struct sd3078 *sd3078)
54 regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL2,
55 KEY_WRITE1, KEY_WRITE1);
56 regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
57 KEY_WRITE2, KEY_WRITE2);
58 regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
59 KEY_WRITE3, KEY_WRITE3);
62 #if WRITE_PROTECT_EN
64 * In order to prevent arbitrary modification of the time register,
65 * we should disable the write function.
66 * when disable write,
67 * the "write" bit needs to be clear in a certain order.
68 * 1. clear WRITE2 bit
69 * 2. clear WRITE3 bit
70 * 3. clear WRITE1 bit
72 static void sd3078_disable_reg_write(struct sd3078 *sd3078)
74 regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
75 KEY_WRITE2, 0);
76 regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
77 KEY_WRITE3, 0);
78 regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL2,
79 KEY_WRITE1, 0);
81 #endif
83 static int sd3078_rtc_read_time(struct device *dev, struct rtc_time *tm)
85 unsigned char hour;
86 unsigned char rtc_data[NUM_TIME_REGS] = {0};
87 struct i2c_client *client = to_i2c_client(dev);
88 struct sd3078 *sd3078 = i2c_get_clientdata(client);
89 int ret;
91 ret = regmap_bulk_read(sd3078->regmap, SD3078_REG_SC, rtc_data,
92 NUM_TIME_REGS);
93 if (ret < 0) {
94 dev_err(dev, "reading from RTC failed with err:%d\n", ret);
95 return ret;
98 tm->tm_sec = bcd2bin(rtc_data[SD3078_REG_SC] & 0x7F);
99 tm->tm_min = bcd2bin(rtc_data[SD3078_REG_MN] & 0x7F);
102 * The sd3078 supports 12/24 hour mode.
103 * When getting time,
104 * we need to convert the 12 hour mode to the 24 hour mode.
106 hour = rtc_data[SD3078_REG_HR];
107 if (hour & 0x80) /* 24H MODE */
108 tm->tm_hour = bcd2bin(rtc_data[SD3078_REG_HR] & 0x3F);
109 else if (hour & 0x20) /* 12H MODE PM */
110 tm->tm_hour = bcd2bin(rtc_data[SD3078_REG_HR] & 0x1F) + 12;
111 else /* 12H MODE AM */
112 tm->tm_hour = bcd2bin(rtc_data[SD3078_REG_HR] & 0x1F);
114 tm->tm_mday = bcd2bin(rtc_data[SD3078_REG_DM] & 0x3F);
115 tm->tm_wday = rtc_data[SD3078_REG_DW] & 0x07;
116 tm->tm_mon = bcd2bin(rtc_data[SD3078_REG_MO] & 0x1F) - 1;
117 tm->tm_year = bcd2bin(rtc_data[SD3078_REG_YR]) + 100;
119 return 0;
122 static int sd3078_rtc_set_time(struct device *dev, struct rtc_time *tm)
124 unsigned char rtc_data[NUM_TIME_REGS];
125 struct i2c_client *client = to_i2c_client(dev);
126 struct sd3078 *sd3078 = i2c_get_clientdata(client);
127 int ret;
129 rtc_data[SD3078_REG_SC] = bin2bcd(tm->tm_sec);
130 rtc_data[SD3078_REG_MN] = bin2bcd(tm->tm_min);
131 rtc_data[SD3078_REG_HR] = bin2bcd(tm->tm_hour) | 0x80;
132 rtc_data[SD3078_REG_DM] = bin2bcd(tm->tm_mday);
133 rtc_data[SD3078_REG_DW] = tm->tm_wday & 0x07;
134 rtc_data[SD3078_REG_MO] = bin2bcd(tm->tm_mon) + 1;
135 rtc_data[SD3078_REG_YR] = bin2bcd(tm->tm_year - 100);
137 #if WRITE_PROTECT_EN
138 sd3078_enable_reg_write(sd3078);
139 #endif
141 ret = regmap_bulk_write(sd3078->regmap, SD3078_REG_SC, rtc_data,
142 NUM_TIME_REGS);
143 if (ret < 0) {
144 dev_err(dev, "writing to RTC failed with err:%d\n", ret);
145 return ret;
148 #if WRITE_PROTECT_EN
149 sd3078_disable_reg_write(sd3078);
150 #endif
152 return 0;
155 static const struct rtc_class_ops sd3078_rtc_ops = {
156 .read_time = sd3078_rtc_read_time,
157 .set_time = sd3078_rtc_set_time,
160 static const struct regmap_config regmap_config = {
161 .reg_bits = 8,
162 .val_bits = 8,
163 .max_register = 0x11,
166 static int sd3078_probe(struct i2c_client *client,
167 const struct i2c_device_id *id)
169 int ret;
170 struct sd3078 *sd3078;
172 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
173 return -ENODEV;
175 sd3078 = devm_kzalloc(&client->dev, sizeof(*sd3078), GFP_KERNEL);
176 if (!sd3078)
177 return -ENOMEM;
179 sd3078->regmap = devm_regmap_init_i2c(client, &regmap_config);
180 if (IS_ERR(sd3078->regmap)) {
181 dev_err(&client->dev, "regmap allocation failed\n");
182 return PTR_ERR(sd3078->regmap);
185 i2c_set_clientdata(client, sd3078);
187 sd3078->rtc = devm_rtc_allocate_device(&client->dev);
188 if (IS_ERR(sd3078->rtc))
189 return PTR_ERR(sd3078->rtc);
191 sd3078->rtc->ops = &sd3078_rtc_ops;
192 sd3078->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
193 sd3078->rtc->range_max = RTC_TIMESTAMP_END_2099;
195 ret = devm_rtc_register_device(sd3078->rtc);
196 if (ret)
197 return ret;
199 sd3078_enable_reg_write(sd3078);
201 return 0;
204 static const struct i2c_device_id sd3078_id[] = {
205 {"sd3078", 0},
208 MODULE_DEVICE_TABLE(i2c, sd3078_id);
210 static const struct of_device_id rtc_dt_match[] = {
211 { .compatible = "whwave,sd3078" },
214 MODULE_DEVICE_TABLE(of, rtc_dt_match);
216 static struct i2c_driver sd3078_driver = {
217 .driver = {
218 .name = "sd3078",
219 .of_match_table = of_match_ptr(rtc_dt_match),
221 .probe = sd3078_probe,
222 .id_table = sd3078_id,
225 module_i2c_driver(sd3078_driver);
227 MODULE_AUTHOR("Dianlong Li <long17.cool@163.com>");
228 MODULE_DESCRIPTION("SD3078 RTC driver");
229 MODULE_LICENSE("GPL v2");