xref: /linux/drivers/regulator/rt5759-regulator.c (revision 41cff178e3d6df28acd8490519a656c509b4496f)
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <linux/bits.h>
4 #include <linux/i2c.h>
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/of_device.h>
8 #include <linux/regmap.h>
9 #include <linux/regulator/driver.h>
10 #include <linux/regulator/of_regulator.h>
11 
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
19 
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)
32 
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
40 
41 #define RT5759_PSKIP_MODE	0
42 #define RT5759_FPWM_MODE	1
43 
44 enum {
45 	CHIP_TYPE_RT5759 = 0,
46 	CHIP_TYPE_RT5759A,
47 	CHIP_TYPE_MAX
48 };
49 
50 struct rt5759_priv {
51 	struct device *dev;
52 	struct regmap *regmap;
53 	struct regulator_desc desc;
54 	unsigned long chip_type;
55 };
56 
57 static int rt5759_set_mode(struct regulator_dev *rdev, unsigned int mode)
58 {
59 	struct regmap *regmap = rdev_get_regmap(rdev);
60 	unsigned int mode_val;
61 
62 	switch (mode) {
63 	case REGULATOR_MODE_NORMAL:
64 		mode_val = 0;
65 		break;
66 	case REGULATOR_MODE_FAST:
67 		mode_val = RT5759_FPWM_MASK;
68 		break;
69 	default:
70 		return -EINVAL;
71 	}
72 
73 	return regmap_update_bits(regmap, RT5759_REG_STATUS, RT5759_FPWM_MASK,
74 				  mode_val);
75 }
76 
77 static unsigned int rt5759_get_mode(struct regulator_dev *rdev)
78 {
79 	struct regmap *regmap = rdev_get_regmap(rdev);
80 	unsigned int regval;
81 	int ret;
82 
83 	ret = regmap_read(regmap, RT5759_REG_DCDCCTRL, &regval);
84 	if (ret)
85 		return REGULATOR_MODE_INVALID;
86 
87 	if (regval & RT5759_FPWM_MASK)
88 		return REGULATOR_MODE_FAST;
89 
90 	return REGULATOR_MODE_NORMAL;
91 }
92 
93 static int rt5759_get_error_flags(struct regulator_dev *rdev,
94 				  unsigned int *flags)
95 {
96 	struct regmap *regmap = rdev_get_regmap(rdev);
97 	unsigned int status, events = 0;
98 	int ret;
99 
100 	ret = regmap_read(regmap, RT5759_REG_STATUS, &status);
101 	if (ret)
102 		return ret;
103 
104 	if (status & RT5759_OT_MASK)
105 		events |= REGULATOR_ERROR_OVER_TEMP;
106 
107 	if (status & RT5759_UV_MASK)
108 		events |= REGULATOR_ERROR_UNDER_VOLTAGE;
109 
110 	*flags = events;
111 	return 0;
112 }
113 
114 static int rt5759_set_ocp(struct regulator_dev *rdev, int lim_uA, int severity,
115 			  bool enable)
116 {
117 	struct regmap *regmap = rdev_get_regmap(rdev);
118 	int ocp_lvl[] = { 9800000, 10800000, 11800000 };
119 	unsigned int ocp_regval;
120 	int i;
121 
122 	/* Only support over current protection parameter */
123 	if (severity != REGULATOR_SEVERITY_PROT)
124 		return 0;
125 
126 	if (enable) {
127 		/* Default ocp level is 10.8A */
128 		if (lim_uA == 0)
129 			lim_uA = 10800000;
130 
131 		for (i = 0; i < ARRAY_SIZE(ocp_lvl); i++) {
132 			if (lim_uA <= ocp_lvl[i])
133 				break;
134 		}
135 
136 		if (i == ARRAY_SIZE(ocp_lvl))
137 			i = ARRAY_SIZE(ocp_lvl) - 1;
138 
139 		ocp_regval = i + 1;
140 	} else
141 		ocp_regval = 0;
142 
143 	return regmap_update_bits(regmap, RT5759_REG_DCDCSET, RT5957_OCLVL_MASK,
144 				  ocp_regval << RT5759_OCLVL_SHIFT);
145 }
146 
147 static int rt5759_set_otp(struct regulator_dev *rdev, int lim, int severity,
148 			  bool enable)
149 {
150 	struct regmap *regmap = rdev_get_regmap(rdev);
151 	int otp_lvl[] = { 140, 150, 170 };
152 	unsigned int otp_regval;
153 	int i;
154 
155 	/* Only support over temperature protection parameter */
156 	if (severity != REGULATOR_SEVERITY_PROT)
157 		return 0;
158 
159 	if (enable) {
160 		/* Default otp level is 150'c */
161 		if (lim == 0)
162 			lim = 150;
163 
164 		for (i = 0; i < ARRAY_SIZE(otp_lvl); i++) {
165 			if (lim <= otp_lvl[i])
166 				break;
167 		}
168 
169 		if (i == ARRAY_SIZE(otp_lvl))
170 			i = ARRAY_SIZE(otp_lvl) - 1;
171 
172 		otp_regval = i + 1;
173 	} else
174 		otp_regval = 0;
175 
176 	return regmap_update_bits(regmap, RT5759_REG_DCDCSET, RT5957_OTLVL_MASK,
177 				  otp_regval << RT5759_OTLVL_SHIFT);
178 }
179 
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,
194 };
195 
196 static unsigned int rt5759_of_map_mode(unsigned int mode)
197 {
198 	switch (mode) {
199 	case RT5759_FPWM_MODE:
200 		return REGULATOR_MODE_FAST;
201 	case RT5759_PSKIP_MODE:
202 		return REGULATOR_MODE_NORMAL;
203 	default:
204 		return REGULATOR_MODE_INVALID;
205 	}
206 }
207 
208 static const unsigned int rt5759_ramp_table[] = { 20000, 15000, 10000, 5000 };
209 
210 static int rt5759_regulator_register(struct rt5759_priv *priv)
211 {
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;
216 	int ret;
217 
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;
238 
239 	/*
240 	 * RT5759 step uV = 10000
241 	 * RT5759A step uV = 12500
242 	 */
243 	if (priv->chip_type == CHIP_TYPE_RT5759A)
244 		reg_desc->uV_step = RT5759A_STEP_UV;
245 
246 	memset(&reg_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;
251 
252 	rdev = devm_regulator_register(priv->dev, reg_desc, &reg_cfg);
253 	if (IS_ERR(rdev)) {
254 		ret = PTR_ERR(rdev);
255 		dev_err(priv->dev, "Failed to register regulator (%d)\n", ret);
256 		return ret;
257 	}
258 
259 	return 0;
260 }
261 
262 static int rt5759_init_device_property(struct rt5759_priv *priv)
263 {
264 	unsigned int val = 0;
265 
266 	/*
267 	 * Only RT5759A support external watchdog input
268 	 */
269 	if (priv->chip_type != CHIP_TYPE_RT5759A)
270 		return 0;
271 
272 	if (device_property_read_bool(priv->dev, "richtek,watchdog-enable"))
273 		val = RT5759A_WDTEN_MASK;
274 
275 	return regmap_update_bits(priv->regmap, RT5759A_REG_WDTEN,
276 				  RT5759A_WDTEN_MASK, val);
277 }
278 
279 static int rt5759_manufacturer_check(struct rt5759_priv *priv)
280 {
281 	unsigned int vendor;
282 	int ret;
283 
284 	ret = regmap_read(priv->regmap, RT5759_REG_VENDORINFO, &vendor);
285 	if (ret)
286 		return ret;
287 
288 	if (vendor != RT5759_MANUFACTURER_ID) {
289 		dev_err(priv->dev, "vendor info not correct (%d)\n", vendor);
290 		return -EINVAL;
291 	}
292 
293 	return 0;
294 }
295 
296 static bool rt5759_is_accessible_reg(struct device *dev, unsigned int reg)
297 {
298 	struct rt5759_priv *priv = dev_get_drvdata(dev);
299 
300 	if (reg <= RT5759_REG_DCDCSET)
301 		return true;
302 
303 	if (priv->chip_type == CHIP_TYPE_RT5759A && reg == RT5759A_REG_WDTEN)
304 		return true;
305 
306 	return false;
307 }
308 
309 static const struct regmap_config rt5759_regmap_config = {
310 	.reg_bits = 8,
311 	.val_bits = 8,
312 	.max_register = RT5759A_REG_WDTEN,
313 	.readable_reg = rt5759_is_accessible_reg,
314 	.writeable_reg = rt5759_is_accessible_reg,
315 };
316 
317 static int rt5759_probe(struct i2c_client *i2c)
318 {
319 	struct rt5759_priv *priv;
320 	int ret;
321 
322 	priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
323 	if (!priv)
324 		return -ENOMEM;
325 
326 	priv->dev = &i2c->dev;
327 	priv->chip_type = (unsigned long)of_device_get_match_data(&i2c->dev);
328 	i2c_set_clientdata(i2c, priv);
329 
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);
334 		return ret;
335 	}
336 
337 	ret = rt5759_manufacturer_check(priv);
338 	if (ret) {
339 		dev_err(&i2c->dev, "Failed to check device (%d)\n", ret);
340 		return ret;
341 	}
342 
343 	ret = rt5759_init_device_property(priv);
344 	if (ret) {
345 		dev_err(&i2c->dev, "Failed to init device (%d)\n", ret);
346 		return ret;
347 	}
348 
349 	return rt5759_regulator_register(priv);
350 }
351 
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 },
355 	{}
356 };
357 MODULE_DEVICE_TABLE(of, rt5759_device_table);
358 
359 static struct i2c_driver rt5759_driver = {
360 	.driver = {
361 		.name = "rt5759",
362 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
363 		.of_match_table = of_match_ptr(rt5759_device_table),
364 	},
365 	.probe_new = rt5759_probe,
366 };
367 module_i2c_driver(rt5759_driver);
368 
369 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
370 MODULE_DESCRIPTION("Richtek RT5759 Regulator Driver");
371 MODULE_LICENSE("GPL v2");
372