1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RTC driver for the SD2405AL Real-Time Clock
6 * https://image.dfrobot.com/image/data/TOY0021/SD2405AL%20datasheet%20(Angelo%20v0.1).pdf
8 * Copyright (C) 2024 Tóth János <gomba007@gmail.com>
11 #include <linux/bcd.h>
12 #include <linux/i2c.h>
13 #include <linux/regmap.h>
14 #include <linux/rtc.h>
16 /* Real time clock registers */
17 #define SD2405AL_REG_T_SEC 0x00
18 #define SD2405AL_REG_T_MIN 0x01
19 #define SD2405AL_REG_T_HOUR 0x02
20 # define SD2405AL_BIT_12H_PM BIT(5)
21 # define SD2405AL_BIT_24H BIT(7)
22 #define SD2405AL_REG_T_WEEK 0x03
23 #define SD2405AL_REG_T_DAY 0x04
24 #define SD2405AL_REG_T_MON 0x05
25 #define SD2405AL_REG_T_YEAR 0x06
27 #define SD2405AL_NUM_T_REGS (SD2405AL_REG_T_YEAR - SD2405AL_REG_T_SEC + 1)
29 /* Control registers */
30 #define SD2405AL_REG_CTR1 0x0F
31 # define SD2405AL_BIT_WRTC2 BIT(2)
32 # define SD2405AL_BIT_WRTC3 BIT(7)
33 #define SD2405AL_REG_CTR2 0x10
34 # define SD2405AL_BIT_WRTC1 BIT(7)
35 #define SD2405AL_REG_CTR3 0x11
36 #define SD2405AL_REG_TTF 0x12
37 #define SD2405AL_REG_CNTDWN 0x13
40 #define SD2405AL_REG_M_START 0x14
41 #define SD2405AL_REG_M_END 0x1F
45 struct rtc_device
*rtc
;
46 struct regmap
*regmap
;
49 static int sd2405al_enable_reg_write(struct sd2405al
*sd2405al
)
53 /* order of writes is important */
54 ret
= regmap_update_bits(sd2405al
->regmap
, SD2405AL_REG_CTR2
,
55 SD2405AL_BIT_WRTC1
, SD2405AL_BIT_WRTC1
);
59 ret
= regmap_update_bits(sd2405al
->regmap
, SD2405AL_REG_CTR1
,
60 SD2405AL_BIT_WRTC2
| SD2405AL_BIT_WRTC3
,
61 SD2405AL_BIT_WRTC2
| SD2405AL_BIT_WRTC3
);
68 static int sd2405al_disable_reg_write(struct sd2405al
*sd2405al
)
72 /* order of writes is important */
73 ret
= regmap_update_bits(sd2405al
->regmap
, SD2405AL_REG_CTR1
,
74 SD2405AL_BIT_WRTC2
| SD2405AL_BIT_WRTC3
, 0x00);
78 ret
= regmap_update_bits(sd2405al
->regmap
, SD2405AL_REG_CTR2
,
79 SD2405AL_BIT_WRTC1
, 0x00);
86 static int sd2405al_read_time(struct device
*dev
, struct rtc_time
*time
)
88 u8 data
[SD2405AL_NUM_T_REGS
] = { 0 };
89 struct sd2405al
*sd2405al
= dev_get_drvdata(dev
);
92 ret
= regmap_bulk_read(sd2405al
->regmap
, SD2405AL_REG_T_SEC
, data
,
97 time
->tm_sec
= bcd2bin(data
[SD2405AL_REG_T_SEC
] & 0x7F);
98 time
->tm_min
= bcd2bin(data
[SD2405AL_REG_T_MIN
] & 0x7F);
100 if (data
[SD2405AL_REG_T_HOUR
] & SD2405AL_BIT_24H
)
101 time
->tm_hour
= bcd2bin(data
[SD2405AL_REG_T_HOUR
] & 0x3F);
103 if (data
[SD2405AL_REG_T_HOUR
] & SD2405AL_BIT_12H_PM
)
104 time
->tm_hour
= bcd2bin(data
[SD2405AL_REG_T_HOUR
]
106 else /* 12 hour mode, AM */
107 time
->tm_hour
= bcd2bin(data
[SD2405AL_REG_T_HOUR
]
110 time
->tm_wday
= bcd2bin(data
[SD2405AL_REG_T_WEEK
] & 0x07);
111 time
->tm_mday
= bcd2bin(data
[SD2405AL_REG_T_DAY
] & 0x3F);
112 time
->tm_mon
= bcd2bin(data
[SD2405AL_REG_T_MON
] & 0x1F) - 1;
113 time
->tm_year
= bcd2bin(data
[SD2405AL_REG_T_YEAR
]) + 100;
115 dev_dbg(sd2405al
->dev
, "read time: %ptR (%d)\n", time
, time
->tm_wday
);
120 static int sd2405al_set_time(struct device
*dev
, struct rtc_time
*time
)
122 u8 data
[SD2405AL_NUM_T_REGS
];
123 struct sd2405al
*sd2405al
= dev_get_drvdata(dev
);
126 data
[SD2405AL_REG_T_SEC
] = bin2bcd(time
->tm_sec
);
127 data
[SD2405AL_REG_T_MIN
] = bin2bcd(time
->tm_min
);
128 data
[SD2405AL_REG_T_HOUR
] = bin2bcd(time
->tm_hour
) | SD2405AL_BIT_24H
;
129 data
[SD2405AL_REG_T_DAY
] = bin2bcd(time
->tm_mday
);
130 data
[SD2405AL_REG_T_WEEK
] = bin2bcd(time
->tm_wday
);
131 data
[SD2405AL_REG_T_MON
] = bin2bcd(time
->tm_mon
) + 1;
132 data
[SD2405AL_REG_T_YEAR
] = bin2bcd(time
->tm_year
- 100);
134 ret
= sd2405al_enable_reg_write(sd2405al
);
138 ret
= regmap_bulk_write(sd2405al
->regmap
, SD2405AL_REG_T_SEC
, data
,
139 SD2405AL_NUM_T_REGS
);
143 ret
= regmap_write(sd2405al
->regmap
, SD2405AL_REG_TTF
, 0x00);
147 ret
= sd2405al_disable_reg_write(sd2405al
);
151 dev_dbg(sd2405al
->dev
, "set time: %ptR (%d)\n", time
, time
->tm_wday
);
156 static const struct rtc_class_ops sd2405al_rtc_ops
= {
157 .read_time
= sd2405al_read_time
,
158 .set_time
= sd2405al_set_time
,
161 static const struct regmap_config sd2405al_regmap_conf
= {
164 .max_register
= SD2405AL_REG_M_END
,
167 static int sd2405al_probe(struct i2c_client
*client
)
169 struct sd2405al
*sd2405al
;
172 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
175 sd2405al
= devm_kzalloc(&client
->dev
, sizeof(*sd2405al
), GFP_KERNEL
);
179 sd2405al
->dev
= &client
->dev
;
181 sd2405al
->regmap
= devm_regmap_init_i2c(client
, &sd2405al_regmap_conf
);
182 if (IS_ERR(sd2405al
->regmap
))
183 return PTR_ERR(sd2405al
->regmap
);
185 sd2405al
->rtc
= devm_rtc_allocate_device(&client
->dev
);
186 if (IS_ERR(sd2405al
->rtc
))
187 return PTR_ERR(sd2405al
->rtc
);
189 sd2405al
->rtc
->ops
= &sd2405al_rtc_ops
;
190 sd2405al
->rtc
->range_min
= RTC_TIMESTAMP_BEGIN_2000
;
191 sd2405al
->rtc
->range_max
= RTC_TIMESTAMP_END_2099
;
193 dev_set_drvdata(&client
->dev
, sd2405al
);
195 ret
= devm_rtc_register_device(sd2405al
->rtc
);
202 static const struct i2c_device_id sd2405al_id
[] = {
206 MODULE_DEVICE_TABLE(i2c
, sd2405al_id
);
208 static const __maybe_unused
struct of_device_id sd2405al_of_match
[] = {
209 { .compatible
= "dfrobot,sd2405al" },
212 MODULE_DEVICE_TABLE(of
, sd2405al_of_match
);
214 static struct i2c_driver sd2405al_driver
= {
217 .of_match_table
= of_match_ptr(sd2405al_of_match
),
219 .probe
= sd2405al_probe
,
220 .id_table
= sd2405al_id
,
223 module_i2c_driver(sd2405al_driver
);
225 MODULE_AUTHOR("Tóth János <gomba007@gmail.com>");
226 MODULE_LICENSE("GPL");
227 MODULE_DESCRIPTION("SD2405AL RTC driver");