1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/bits.h>
5 #include <linux/kernel.h>
6 #include <linux/module.h>
8 #include <linux/regmap.h>
9 #include <linux/regulator/driver.h>
10 #include <linux/regulator/of_regulator.h>
12 #define RT5759_REG_VENDORINFO 0x00
13 #define RT5759_REG_FREQ 0x01
14 #define RT5759_REG_VSEL 0x02
15 #define RT5759_REG_DCDCCTRL 0x03
16 #define RT5759_REG_STATUS 0x04
17 #define RT5759_REG_DCDCSET 0x05
18 #define RT5759A_REG_WDTEN 0x42
20 #define RT5759_TSTEP_MASK GENMASK(3, 2)
21 #define RT5759_VSEL_MASK GENMASK(6, 0)
22 #define RT5759_DISCHARGE_MASK BIT(3)
23 #define RT5759_FPWM_MASK BIT(2)
24 #define RT5759_ENABLE_MASK BIT(1)
25 #define RT5759_OT_MASK BIT(1)
26 #define RT5759_UV_MASK BIT(0)
27 #define RT5957_OCLVL_MASK GENMASK(7, 6)
28 #define RT5759_OCLVL_SHIFT 6
29 #define RT5957_OTLVL_MASK GENMASK(5, 4)
30 #define RT5759_OTLVL_SHIFT 4
31 #define RT5759A_WDTEN_MASK BIT(1)
33 #define RT5759_MANUFACTURER_ID 0x82
34 /* vsel range 0x00 ~ 0x5A */
35 #define RT5759_NUM_VOLTS 91
36 #define RT5759_MIN_UV 600000
37 #define RT5759_STEP_UV 10000
38 #define RT5759A_STEP_UV 12500
39 #define RT5759_MINSS_TIMEUS 1500
41 #define RT5759_PSKIP_MODE 0
42 #define RT5759_FPWM_MODE 1
52 struct regmap
*regmap
;
53 struct regulator_desc desc
;
54 unsigned long chip_type
;
57 static int rt5759_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
59 struct regmap
*regmap
= rdev_get_regmap(rdev
);
60 unsigned int mode_val
;
63 case REGULATOR_MODE_NORMAL
:
66 case REGULATOR_MODE_FAST
:
67 mode_val
= RT5759_FPWM_MASK
;
73 return regmap_update_bits(regmap
, RT5759_REG_STATUS
, RT5759_FPWM_MASK
,
77 static unsigned int rt5759_get_mode(struct regulator_dev
*rdev
)
79 struct regmap
*regmap
= rdev_get_regmap(rdev
);
83 ret
= regmap_read(regmap
, RT5759_REG_DCDCCTRL
, ®val
);
85 return REGULATOR_MODE_INVALID
;
87 if (regval
& RT5759_FPWM_MASK
)
88 return REGULATOR_MODE_FAST
;
90 return REGULATOR_MODE_NORMAL
;
93 static int rt5759_get_error_flags(struct regulator_dev
*rdev
,
96 struct regmap
*regmap
= rdev_get_regmap(rdev
);
97 unsigned int status
, events
= 0;
100 ret
= regmap_read(regmap
, RT5759_REG_STATUS
, &status
);
104 if (status
& RT5759_OT_MASK
)
105 events
|= REGULATOR_ERROR_OVER_TEMP
;
107 if (status
& RT5759_UV_MASK
)
108 events
|= REGULATOR_ERROR_UNDER_VOLTAGE
;
114 static int rt5759_set_ocp(struct regulator_dev
*rdev
, int lim_uA
, int severity
,
117 struct regmap
*regmap
= rdev_get_regmap(rdev
);
118 int ocp_lvl
[] = { 9800000, 10800000, 11800000 };
119 unsigned int ocp_regval
;
122 /* Only support over current protection parameter */
123 if (severity
!= REGULATOR_SEVERITY_PROT
)
127 /* Default ocp level is 10.8A */
131 for (i
= 0; i
< ARRAY_SIZE(ocp_lvl
); i
++) {
132 if (lim_uA
<= ocp_lvl
[i
])
136 if (i
== ARRAY_SIZE(ocp_lvl
))
137 i
= ARRAY_SIZE(ocp_lvl
) - 1;
143 return regmap_update_bits(regmap
, RT5759_REG_DCDCSET
, RT5957_OCLVL_MASK
,
144 ocp_regval
<< RT5759_OCLVL_SHIFT
);
147 static int rt5759_set_otp(struct regulator_dev
*rdev
, int lim
, int severity
,
150 struct regmap
*regmap
= rdev_get_regmap(rdev
);
151 int otp_lvl
[] = { 140, 150, 170 };
152 unsigned int otp_regval
;
155 /* Only support over temperature protection parameter */
156 if (severity
!= REGULATOR_SEVERITY_PROT
)
160 /* Default otp level is 150'c */
164 for (i
= 0; i
< ARRAY_SIZE(otp_lvl
); i
++) {
165 if (lim
<= otp_lvl
[i
])
169 if (i
== ARRAY_SIZE(otp_lvl
))
170 i
= ARRAY_SIZE(otp_lvl
) - 1;
176 return regmap_update_bits(regmap
, RT5759_REG_DCDCSET
, RT5957_OTLVL_MASK
,
177 otp_regval
<< RT5759_OTLVL_SHIFT
);
180 static const struct regulator_ops rt5759_regulator_ops
= {
181 .list_voltage
= regulator_list_voltage_linear
,
182 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
183 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
184 .enable
= regulator_enable_regmap
,
185 .disable
= regulator_disable_regmap
,
186 .is_enabled
= regulator_is_enabled_regmap
,
187 .set_active_discharge
= regulator_set_active_discharge_regmap
,
188 .set_mode
= rt5759_set_mode
,
189 .get_mode
= rt5759_get_mode
,
190 .set_ramp_delay
= regulator_set_ramp_delay_regmap
,
191 .get_error_flags
= rt5759_get_error_flags
,
192 .set_over_current_protection
= rt5759_set_ocp
,
193 .set_thermal_protection
= rt5759_set_otp
,
196 static unsigned int rt5759_of_map_mode(unsigned int mode
)
199 case RT5759_FPWM_MODE
:
200 return REGULATOR_MODE_FAST
;
201 case RT5759_PSKIP_MODE
:
202 return REGULATOR_MODE_NORMAL
;
204 return REGULATOR_MODE_INVALID
;
208 static const unsigned int rt5759_ramp_table
[] = { 20000, 15000, 10000, 5000 };
210 static int rt5759_regulator_register(struct rt5759_priv
*priv
)
212 struct device_node
*np
= priv
->dev
->of_node
;
213 struct regulator_desc
*reg_desc
= &priv
->desc
;
214 struct regulator_config reg_cfg
;
215 struct regulator_dev
*rdev
;
218 reg_desc
->name
= "rt5759-buck";
219 reg_desc
->type
= REGULATOR_VOLTAGE
;
220 reg_desc
->owner
= THIS_MODULE
;
221 reg_desc
->ops
= &rt5759_regulator_ops
;
222 reg_desc
->n_voltages
= RT5759_NUM_VOLTS
;
223 reg_desc
->min_uV
= RT5759_MIN_UV
;
224 reg_desc
->uV_step
= RT5759_STEP_UV
;
225 reg_desc
->vsel_reg
= RT5759_REG_VSEL
;
226 reg_desc
->vsel_mask
= RT5759_VSEL_MASK
;
227 reg_desc
->enable_reg
= RT5759_REG_DCDCCTRL
;
228 reg_desc
->enable_mask
= RT5759_ENABLE_MASK
;
229 reg_desc
->active_discharge_reg
= RT5759_REG_DCDCCTRL
;
230 reg_desc
->active_discharge_mask
= RT5759_DISCHARGE_MASK
;
231 reg_desc
->active_discharge_on
= RT5759_DISCHARGE_MASK
;
232 reg_desc
->ramp_reg
= RT5759_REG_FREQ
;
233 reg_desc
->ramp_mask
= RT5759_TSTEP_MASK
;
234 reg_desc
->ramp_delay_table
= rt5759_ramp_table
;
235 reg_desc
->n_ramp_values
= ARRAY_SIZE(rt5759_ramp_table
);
236 reg_desc
->enable_time
= RT5759_MINSS_TIMEUS
;
237 reg_desc
->of_map_mode
= rt5759_of_map_mode
;
240 * RT5759 step uV = 10000
241 * RT5759A step uV = 12500
243 if (priv
->chip_type
== CHIP_TYPE_RT5759A
)
244 reg_desc
->uV_step
= RT5759A_STEP_UV
;
246 memset(®_cfg
, 0, sizeof(reg_cfg
));
247 reg_cfg
.dev
= priv
->dev
;
248 reg_cfg
.of_node
= np
;
249 reg_cfg
.init_data
= of_get_regulator_init_data(priv
->dev
, np
, reg_desc
);
250 reg_cfg
.regmap
= priv
->regmap
;
252 rdev
= devm_regulator_register(priv
->dev
, reg_desc
, ®_cfg
);
255 dev_err(priv
->dev
, "Failed to register regulator (%d)\n", ret
);
262 static int rt5759_init_device_property(struct rt5759_priv
*priv
)
264 unsigned int val
= 0;
267 * Only RT5759A support external watchdog input
269 if (priv
->chip_type
!= CHIP_TYPE_RT5759A
)
272 if (device_property_read_bool(priv
->dev
, "richtek,watchdog-enable"))
273 val
= RT5759A_WDTEN_MASK
;
275 return regmap_update_bits(priv
->regmap
, RT5759A_REG_WDTEN
,
276 RT5759A_WDTEN_MASK
, val
);
279 static int rt5759_manufacturer_check(struct rt5759_priv
*priv
)
284 ret
= regmap_read(priv
->regmap
, RT5759_REG_VENDORINFO
, &vendor
);
288 if (vendor
!= RT5759_MANUFACTURER_ID
) {
289 dev_err(priv
->dev
, "vendor info not correct (%d)\n", vendor
);
296 static bool rt5759_is_accessible_reg(struct device
*dev
, unsigned int reg
)
298 struct rt5759_priv
*priv
= dev_get_drvdata(dev
);
300 if (reg
<= RT5759_REG_DCDCSET
)
303 if (priv
->chip_type
== CHIP_TYPE_RT5759A
&& reg
== RT5759A_REG_WDTEN
)
309 static const struct regmap_config rt5759_regmap_config
= {
312 .max_register
= RT5759A_REG_WDTEN
,
313 .readable_reg
= rt5759_is_accessible_reg
,
314 .writeable_reg
= rt5759_is_accessible_reg
,
317 static int rt5759_probe(struct i2c_client
*i2c
)
319 struct rt5759_priv
*priv
;
322 priv
= devm_kzalloc(&i2c
->dev
, sizeof(*priv
), GFP_KERNEL
);
326 priv
->dev
= &i2c
->dev
;
327 priv
->chip_type
= (unsigned long)of_device_get_match_data(&i2c
->dev
);
328 i2c_set_clientdata(i2c
, priv
);
330 priv
->regmap
= devm_regmap_init_i2c(i2c
, &rt5759_regmap_config
);
331 if (IS_ERR(priv
->regmap
)) {
332 ret
= PTR_ERR(priv
->regmap
);
333 dev_err(&i2c
->dev
, "Failed to allocate regmap (%d)\n", ret
);
337 ret
= rt5759_manufacturer_check(priv
);
339 dev_err(&i2c
->dev
, "Failed to check device (%d)\n", ret
);
343 ret
= rt5759_init_device_property(priv
);
345 dev_err(&i2c
->dev
, "Failed to init device (%d)\n", ret
);
349 return rt5759_regulator_register(priv
);
352 static const struct of_device_id __maybe_unused rt5759_device_table
[] = {
353 { .compatible
= "richtek,rt5759", .data
= (void *)CHIP_TYPE_RT5759
},
354 { .compatible
= "richtek,rt5759a", .data
= (void *)CHIP_TYPE_RT5759A
},
357 MODULE_DEVICE_TABLE(of
, rt5759_device_table
);
359 static struct i2c_driver rt5759_driver
= {
362 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
363 .of_match_table
= of_match_ptr(rt5759_device_table
),
365 .probe
= rt5759_probe
,
367 module_i2c_driver(rt5759_driver
);
369 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
370 MODULE_DESCRIPTION("Richtek RT5759 Regulator Driver");
371 MODULE_LICENSE("GPL v2");