0001
0002
0003
0004
0005
0006
0007 #include <linux/bcd.h>
0008 #include <linux/i2c.h>
0009 #include <linux/module.h>
0010 #include <linux/regmap.h>
0011 #include <linux/rtc.h>
0012 #include <linux/slab.h>
0013
0014 #define SD3078_REG_SC 0x00
0015 #define SD3078_REG_MN 0x01
0016 #define SD3078_REG_HR 0x02
0017 #define SD3078_REG_DW 0x03
0018 #define SD3078_REG_DM 0x04
0019 #define SD3078_REG_MO 0x05
0020 #define SD3078_REG_YR 0x06
0021
0022 #define SD3078_REG_CTRL1 0x0f
0023 #define SD3078_REG_CTRL2 0x10
0024 #define SD3078_REG_CTRL3 0x11
0025
0026 #define KEY_WRITE1 0x80
0027 #define KEY_WRITE2 0x04
0028 #define KEY_WRITE3 0x80
0029
0030 #define NUM_TIME_REGS (SD3078_REG_YR - SD3078_REG_SC + 1)
0031
0032
0033
0034
0035
0036
0037 #define WRITE_PROTECT_EN 0
0038
0039 struct sd3078 {
0040 struct rtc_device *rtc;
0041 struct regmap *regmap;
0042 };
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 static void sd3078_enable_reg_write(struct sd3078 *sd3078)
0053 {
0054 regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL2,
0055 KEY_WRITE1, KEY_WRITE1);
0056 regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
0057 KEY_WRITE2, KEY_WRITE2);
0058 regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
0059 KEY_WRITE3, KEY_WRITE3);
0060 }
0061
0062 #if WRITE_PROTECT_EN
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072 static void sd3078_disable_reg_write(struct sd3078 *sd3078)
0073 {
0074 regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
0075 KEY_WRITE2, 0);
0076 regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
0077 KEY_WRITE3, 0);
0078 regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL2,
0079 KEY_WRITE1, 0);
0080 }
0081 #endif
0082
0083 static int sd3078_rtc_read_time(struct device *dev, struct rtc_time *tm)
0084 {
0085 unsigned char hour;
0086 unsigned char rtc_data[NUM_TIME_REGS] = {0};
0087 struct i2c_client *client = to_i2c_client(dev);
0088 struct sd3078 *sd3078 = i2c_get_clientdata(client);
0089 int ret;
0090
0091 ret = regmap_bulk_read(sd3078->regmap, SD3078_REG_SC, rtc_data,
0092 NUM_TIME_REGS);
0093 if (ret < 0) {
0094 dev_err(dev, "reading from RTC failed with err:%d\n", ret);
0095 return ret;
0096 }
0097
0098 tm->tm_sec = bcd2bin(rtc_data[SD3078_REG_SC] & 0x7F);
0099 tm->tm_min = bcd2bin(rtc_data[SD3078_REG_MN] & 0x7F);
0100
0101
0102
0103
0104
0105
0106 hour = rtc_data[SD3078_REG_HR];
0107 if (hour & 0x80)
0108 tm->tm_hour = bcd2bin(rtc_data[SD3078_REG_HR] & 0x3F);
0109 else if (hour & 0x20)
0110 tm->tm_hour = bcd2bin(rtc_data[SD3078_REG_HR] & 0x1F) + 12;
0111 else
0112 tm->tm_hour = bcd2bin(rtc_data[SD3078_REG_HR] & 0x1F);
0113
0114 tm->tm_mday = bcd2bin(rtc_data[SD3078_REG_DM] & 0x3F);
0115 tm->tm_wday = rtc_data[SD3078_REG_DW] & 0x07;
0116 tm->tm_mon = bcd2bin(rtc_data[SD3078_REG_MO] & 0x1F) - 1;
0117 tm->tm_year = bcd2bin(rtc_data[SD3078_REG_YR]) + 100;
0118
0119 return 0;
0120 }
0121
0122 static int sd3078_rtc_set_time(struct device *dev, struct rtc_time *tm)
0123 {
0124 unsigned char rtc_data[NUM_TIME_REGS];
0125 struct i2c_client *client = to_i2c_client(dev);
0126 struct sd3078 *sd3078 = i2c_get_clientdata(client);
0127 int ret;
0128
0129 rtc_data[SD3078_REG_SC] = bin2bcd(tm->tm_sec);
0130 rtc_data[SD3078_REG_MN] = bin2bcd(tm->tm_min);
0131 rtc_data[SD3078_REG_HR] = bin2bcd(tm->tm_hour) | 0x80;
0132 rtc_data[SD3078_REG_DM] = bin2bcd(tm->tm_mday);
0133 rtc_data[SD3078_REG_DW] = tm->tm_wday & 0x07;
0134 rtc_data[SD3078_REG_MO] = bin2bcd(tm->tm_mon) + 1;
0135 rtc_data[SD3078_REG_YR] = bin2bcd(tm->tm_year - 100);
0136
0137 #if WRITE_PROTECT_EN
0138 sd3078_enable_reg_write(sd3078);
0139 #endif
0140
0141 ret = regmap_bulk_write(sd3078->regmap, SD3078_REG_SC, rtc_data,
0142 NUM_TIME_REGS);
0143 if (ret < 0) {
0144 dev_err(dev, "writing to RTC failed with err:%d\n", ret);
0145 return ret;
0146 }
0147
0148 #if WRITE_PROTECT_EN
0149 sd3078_disable_reg_write(sd3078);
0150 #endif
0151
0152 return 0;
0153 }
0154
0155 static const struct rtc_class_ops sd3078_rtc_ops = {
0156 .read_time = sd3078_rtc_read_time,
0157 .set_time = sd3078_rtc_set_time,
0158 };
0159
0160 static const struct regmap_config regmap_config = {
0161 .reg_bits = 8,
0162 .val_bits = 8,
0163 .max_register = 0x11,
0164 };
0165
0166 static int sd3078_probe(struct i2c_client *client)
0167 {
0168 int ret;
0169 struct sd3078 *sd3078;
0170
0171 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0172 return -ENODEV;
0173
0174 sd3078 = devm_kzalloc(&client->dev, sizeof(*sd3078), GFP_KERNEL);
0175 if (!sd3078)
0176 return -ENOMEM;
0177
0178 sd3078->regmap = devm_regmap_init_i2c(client, ®map_config);
0179 if (IS_ERR(sd3078->regmap)) {
0180 dev_err(&client->dev, "regmap allocation failed\n");
0181 return PTR_ERR(sd3078->regmap);
0182 }
0183
0184 i2c_set_clientdata(client, sd3078);
0185
0186 sd3078->rtc = devm_rtc_allocate_device(&client->dev);
0187 if (IS_ERR(sd3078->rtc))
0188 return PTR_ERR(sd3078->rtc);
0189
0190 sd3078->rtc->ops = &sd3078_rtc_ops;
0191 sd3078->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
0192 sd3078->rtc->range_max = RTC_TIMESTAMP_END_2099;
0193
0194 ret = devm_rtc_register_device(sd3078->rtc);
0195 if (ret)
0196 return ret;
0197
0198 sd3078_enable_reg_write(sd3078);
0199
0200 return 0;
0201 }
0202
0203 static const struct i2c_device_id sd3078_id[] = {
0204 {"sd3078", 0},
0205 { }
0206 };
0207 MODULE_DEVICE_TABLE(i2c, sd3078_id);
0208
0209 static const __maybe_unused struct of_device_id rtc_dt_match[] = {
0210 { .compatible = "whwave,sd3078" },
0211 {},
0212 };
0213 MODULE_DEVICE_TABLE(of, rtc_dt_match);
0214
0215 static struct i2c_driver sd3078_driver = {
0216 .driver = {
0217 .name = "sd3078",
0218 .of_match_table = of_match_ptr(rtc_dt_match),
0219 },
0220 .probe_new = sd3078_probe,
0221 .id_table = sd3078_id,
0222 };
0223
0224 module_i2c_driver(sd3078_driver);
0225
0226 MODULE_AUTHOR("Dianlong Li <long17.cool@163.com>");
0227 MODULE_DESCRIPTION("SD3078 RTC driver");
0228 MODULE_LICENSE("GPL v2");