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 	reg_cfg.dev = priv->dev;
247 	reg_cfg.of_node = np;
248 	reg_cfg.init_data = of_get_regulator_init_data(priv->dev, np, reg_desc);
249 	reg_cfg.regmap = priv->regmap;
250 
251 	rdev = devm_regulator_register(priv->dev, reg_desc, &reg_cfg);
252 	if (IS_ERR(rdev)) {
253 		ret = PTR_ERR(rdev);
254 		dev_err(priv->dev, "Failed to register regulator (%d)\n", ret);
255 		return ret;
256 	}
257 
258 	return 0;
259 }
260 
261 static int rt5759_init_device_property(struct rt5759_priv *priv)
262 {
263 	unsigned int val = 0;
264 
265 	/*
266 	 * Only RT5759A support external watchdog input
267 	 */
268 	if (priv->chip_type != CHIP_TYPE_RT5759A)
269 		return 0;
270 
271 	if (device_property_read_bool(priv->dev, "richtek,watchdog-enable"))
272 		val = RT5759A_WDTEN_MASK;
273 
274 	return regmap_update_bits(priv->regmap, RT5759A_REG_WDTEN,
275 				  RT5759A_WDTEN_MASK, val);
276 }
277 
278 static int rt5759_manufacturer_check(struct rt5759_priv *priv)
279 {
280 	unsigned int vendor;
281 	int ret;
282 
283 	ret = regmap_read(priv->regmap, RT5759_REG_VENDORINFO, &vendor);
284 	if (ret)
285 		return ret;
286 
287 	if (vendor != RT5759_MANUFACTURER_ID) {
288 		dev_err(priv->dev, "vendor info not correct (%d)\n", vendor);
289 		return -EINVAL;
290 	}
291 
292 	return 0;
293 }
294 
295 static bool rt5759_is_accessible_reg(struct device *dev, unsigned int reg)
296 {
297 	struct rt5759_priv *priv = dev_get_drvdata(dev);
298 
299 	if (reg <= RT5759_REG_DCDCSET)
300 		return true;
301 
302 	if (priv->chip_type == CHIP_TYPE_RT5759A && reg == RT5759A_REG_WDTEN)
303 		return true;
304 
305 	return false;
306 }
307 
308 static const struct regmap_config rt5759_regmap_config = {
309 	.reg_bits = 8,
310 	.val_bits = 8,
311 	.max_register = RT5759A_REG_WDTEN,
312 	.readable_reg = rt5759_is_accessible_reg,
313 	.writeable_reg = rt5759_is_accessible_reg,
314 };
315 
316 static int rt5759_probe(struct i2c_client *i2c)
317 {
318 	struct rt5759_priv *priv;
319 	int ret;
320 
321 	priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
322 	if (!priv)
323 		return -ENOMEM;
324 
325 	priv->dev = &i2c->dev;
326 	priv->chip_type = (unsigned long)of_device_get_match_data(&i2c->dev);
327 	i2c_set_clientdata(i2c, priv);
328 
329 	priv->regmap = devm_regmap_init_i2c(i2c, &rt5759_regmap_config);
330 	if (IS_ERR(priv->regmap)) {
331 		ret = PTR_ERR(priv->regmap);
332 		dev_err(&i2c->dev, "Failed to allocate regmap (%d)\n", ret);
333 		return ret;
334 	}
335 
336 	ret = rt5759_manufacturer_check(priv);
337 	if (ret) {
338 		dev_err(&i2c->dev, "Failed to check device (%d)\n", ret);
339 		return ret;
340 	}
341 
342 	ret = rt5759_init_device_property(priv);
343 	if (ret) {
344 		dev_err(&i2c->dev, "Failed to init device (%d)\n", ret);
345 		return ret;
346 	}
347 
348 	return rt5759_regulator_register(priv);
349 }
350 
351 static const struct of_device_id __maybe_unused rt5759_device_table[] = {
352 	{ .compatible = "richtek,rt5759", .data = (void *)CHIP_TYPE_RT5759 },
353 	{ .compatible = "richtek,rt5759a", .data = (void *)CHIP_TYPE_RT5759A },
354 	{}
355 };
356 MODULE_DEVICE_TABLE(of, rt5759_device_table);
357 
358 static struct i2c_driver rt5759_driver = {
359 	.driver = {
360 		.name = "rt5759",
361 		.of_match_table = of_match_ptr(rt5759_device_table),
362 	},
363 	.probe_new = rt5759_probe,
364 };
365 module_i2c_driver(rt5759_driver);
366 
367 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
368 MODULE_DESCRIPTION("Richtek RT5759 Regulator Driver");
369 MODULE_LICENSE("GPL v2");
370