• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Device driver for regulators in Hisi IC
4  *
5  * Copyright (c) 2013 Linaro Ltd.
6  * Copyright (c) 2011 Hisilicon.
7  *
8  * Guodong Xu <guodong.xu@linaro.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19 
20 #include <linux/delay.h>
21 #include <linux/device.h>
22 #include <linux/err.h>
23 #include <linux/io.h>
24 #include <linux/mfd/hi6421-spmi-pmic.h>
25 #include <linux/module.h>
26 #include <linux/of_address.h>
27 #include <linux/of_device.h>
28 #include <linux/of.h>
29 #include <linux/platform_device.h>
30 #include <linux/regmap.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/regulator/of_regulator.h>
34 #include <linux/seq_file.h>
35 #include <linux/slab.h>
36 #include <linux/spmi.h>
37 #include <linux/time.h>
38 #include <linux/uaccess.h>
39 
40 #define rdev_dbg(rdev, fmt, arg...)	\
41 		 pr_debug("%s: %s: " fmt, (rdev)->desc->name, __func__, ##arg)
42 
43 struct hi6421v600_regulator {
44 	struct regulator_desc rdesc;
45 	struct hi6421_spmi_pmic *pmic;
46 	u32 eco_mode_mask, eco_uA;
47 };
48 
49 static DEFINE_MUTEX(enable_mutex);
50 
51 /*
52  * helper function to ensure when it returns it is at least 'delay_us'
53  * microseconds after 'since'.
54  */
55 
hi6421_spmi_regulator_is_enabled(struct regulator_dev * rdev)56 static int hi6421_spmi_regulator_is_enabled(struct regulator_dev *rdev)
57 {
58 	struct hi6421v600_regulator *sreg = rdev_get_drvdata(rdev);
59 	struct hi6421_spmi_pmic *pmic = sreg->pmic;
60 	u32 reg_val;
61 
62 	reg_val = hi6421_spmi_pmic_read(pmic, rdev->desc->enable_reg);
63 
64 	rdev_dbg(rdev,
65 		 "enable_reg=0x%x, val= 0x%x, enable_state=%d\n",
66 		 rdev->desc->enable_reg,
67 		 reg_val, (reg_val & rdev->desc->enable_mask));
68 
69 	return ((reg_val & rdev->desc->enable_mask) != 0);
70 }
71 
hi6421_spmi_regulator_enable(struct regulator_dev * rdev)72 static int hi6421_spmi_regulator_enable(struct regulator_dev *rdev)
73 {
74 	struct hi6421v600_regulator *sreg = rdev_get_drvdata(rdev);
75 	struct hi6421_spmi_pmic *pmic = sreg->pmic;
76 
77 	/* cannot enable more than one regulator at one time */
78 	mutex_lock(&enable_mutex);
79 	usleep_range(HISI_REGS_ENA_PROTECT_TIME,
80 		     HISI_REGS_ENA_PROTECT_TIME + 1000);
81 
82 	/* set enable register */
83 	rdev_dbg(rdev,
84 		 "off_on_delay=%d us, enable_reg=0x%x, enable_mask=0x%x\n",
85 		 rdev->desc->off_on_delay, rdev->desc->enable_reg,
86 		 rdev->desc->enable_mask);
87 
88 	hi6421_spmi_pmic_rmw(pmic, rdev->desc->enable_reg,
89 			     rdev->desc->enable_mask,
90 			     rdev->desc->enable_mask);
91 
92 	mutex_unlock(&enable_mutex);
93 
94 	return 0;
95 }
96 
hi6421_spmi_regulator_disable(struct regulator_dev * rdev)97 static int hi6421_spmi_regulator_disable(struct regulator_dev *rdev)
98 {
99 	struct hi6421v600_regulator *sreg = rdev_get_drvdata(rdev);
100 	struct hi6421_spmi_pmic *pmic = sreg->pmic;
101 
102 	/* set enable register to 0 */
103 	rdev_dbg(rdev, "enable_reg=0x%x, enable_mask=0x%x\n",
104 		 rdev->desc->enable_reg, rdev->desc->enable_mask);
105 
106 	hi6421_spmi_pmic_rmw(pmic, rdev->desc->enable_reg,
107 			     rdev->desc->enable_mask, 0);
108 
109 	return 0;
110 }
111 
hi6421_spmi_regulator_get_voltage_sel(struct regulator_dev * rdev)112 static int hi6421_spmi_regulator_get_voltage_sel(struct regulator_dev *rdev)
113 {
114 	struct hi6421v600_regulator *sreg = rdev_get_drvdata(rdev);
115 	struct hi6421_spmi_pmic *pmic = sreg->pmic;
116 	u32 reg_val, selector;
117 
118 	/* get voltage selector */
119 	reg_val = hi6421_spmi_pmic_read(pmic, rdev->desc->vsel_reg);
120 
121 	selector = (reg_val & rdev->desc->vsel_mask) >>	(ffs(rdev->desc->vsel_mask) - 1);
122 
123 	rdev_dbg(rdev,
124 		 "vsel_reg=0x%x, value=0x%x, entry=0x%x, voltage=%d mV\n",
125 		 rdev->desc->vsel_reg, reg_val, selector,
126 		rdev->desc->ops->list_voltage(rdev, selector) / 1000);
127 
128 	return selector;
129 }
130 
hi6421_spmi_regulator_set_voltage_sel(struct regulator_dev * rdev,unsigned int selector)131 static int hi6421_spmi_regulator_set_voltage_sel(struct regulator_dev *rdev,
132 						 unsigned int selector)
133 {
134 	struct hi6421v600_regulator *sreg = rdev_get_drvdata(rdev);
135 	struct hi6421_spmi_pmic *pmic = sreg->pmic;
136 	u32 reg_val;
137 
138 	if (unlikely(selector >= rdev->desc->n_voltages))
139 		return -EINVAL;
140 
141 	reg_val = selector << (ffs(rdev->desc->vsel_mask) - 1);
142 
143 	/* set voltage selector */
144 	rdev_dbg(rdev,
145 		 "vsel_reg=0x%x, mask=0x%x, value=0x%x, voltage=%d mV\n",
146 		 rdev->desc->vsel_reg, rdev->desc->vsel_mask, reg_val,
147 		 rdev->desc->ops->list_voltage(rdev, selector) / 1000);
148 
149 	hi6421_spmi_pmic_rmw(pmic, rdev->desc->vsel_reg,
150 			     rdev->desc->vsel_mask, reg_val);
151 
152 	return 0;
153 }
154 
hi6421_spmi_regulator_get_mode(struct regulator_dev * rdev)155 static unsigned int hi6421_spmi_regulator_get_mode(struct regulator_dev *rdev)
156 {
157 	struct hi6421v600_regulator *sreg = rdev_get_drvdata(rdev);
158 	struct hi6421_spmi_pmic *pmic = sreg->pmic;
159 	unsigned int mode;
160 	u32 reg_val;
161 
162 	reg_val = hi6421_spmi_pmic_read(pmic, rdev->desc->enable_reg);
163 
164 	if (reg_val & sreg->eco_mode_mask)
165 		mode = REGULATOR_MODE_IDLE;
166 	else
167 		mode = REGULATOR_MODE_NORMAL;
168 
169 	rdev_dbg(rdev,
170 		 "enable_reg=0x%x, eco_mode_mask=0x%x, reg_val=0x%x, %s mode\n",
171 		 rdev->desc->enable_reg, sreg->eco_mode_mask, reg_val,
172 		 mode == REGULATOR_MODE_IDLE ? "idle" : "normal");
173 
174 	return mode;
175 }
176 
hi6421_spmi_regulator_set_mode(struct regulator_dev * rdev,unsigned int mode)177 static int hi6421_spmi_regulator_set_mode(struct regulator_dev *rdev,
178 					  unsigned int mode)
179 {
180 	struct hi6421v600_regulator *sreg = rdev_get_drvdata(rdev);
181 	struct hi6421_spmi_pmic *pmic = sreg->pmic;
182 	u32 val;
183 
184 	switch (mode) {
185 	case REGULATOR_MODE_NORMAL:
186 		val = 0;
187 		break;
188 	case REGULATOR_MODE_IDLE:
189 		val = sreg->eco_mode_mask << (ffs(sreg->eco_mode_mask) - 1);
190 		break;
191 	default:
192 		return -EINVAL;
193 	}
194 
195 	/* set mode */
196 	rdev_dbg(rdev, "enable_reg=0x%x, eco_mode_mask=0x%x, value=0x%x\n",
197 		 rdev->desc->enable_reg, sreg->eco_mode_mask, val);
198 
199 	hi6421_spmi_pmic_rmw(pmic, rdev->desc->enable_reg,
200 			     sreg->eco_mode_mask, val);
201 
202 	return 0;
203 }
204 
205 static unsigned int
hi6421_spmi_regulator_get_optimum_mode(struct regulator_dev * rdev,int input_uV,int output_uV,int load_uA)206 hi6421_spmi_regulator_get_optimum_mode(struct regulator_dev *rdev,
207 				       int input_uV, int output_uV,
208 				       int load_uA)
209 {
210 	struct hi6421v600_regulator *sreg = rdev_get_drvdata(rdev);
211 
212 	if (load_uA || ((unsigned int)load_uA > sreg->eco_uA))
213 		return REGULATOR_MODE_NORMAL;
214 
215 	return REGULATOR_MODE_IDLE;
216 }
217 
hi6421_spmi_dt_parse(struct platform_device * pdev,struct hi6421v600_regulator * sreg,struct regulator_desc * rdesc)218 static int hi6421_spmi_dt_parse(struct platform_device *pdev,
219 				struct hi6421v600_regulator *sreg,
220 			 struct regulator_desc *rdesc)
221 {
222 	struct device *dev = &pdev->dev;
223 	struct device_node *np = dev->of_node;
224 	unsigned int *v_table;
225 	int ret;
226 
227 	ret = of_property_read_u32(np, "reg", &rdesc->enable_reg);
228 	if (ret) {
229 		dev_err(dev, "missing reg property\n");
230 		return ret;
231 	}
232 
233 	ret = of_property_read_u32(np, "vsel-reg", &rdesc->vsel_reg);
234 	if (ret) {
235 		dev_err(dev, "missing vsel-reg property\n");
236 		return ret;
237 	}
238 
239 	ret = of_property_read_u32(np, "enable-mask", &rdesc->enable_mask);
240 	if (ret) {
241 		dev_err(dev, "missing enable-mask property\n");
242 		return ret;
243 	}
244 
245 	/*
246 	 * Not all regulators work on idle mode
247 	 */
248 	ret = of_property_read_u32(np, "idle-mode-mask", &sreg->eco_mode_mask);
249 	if (ret) {
250 		dev_dbg(dev, "LDO doesn't support economy mode.\n");
251 		sreg->eco_mode_mask = 0;
252 		sreg->eco_uA = 0;
253 	} else {
254 		ret = of_property_read_u32(np, "eco-microamp", &sreg->eco_uA);
255 		if (ret) {
256 			dev_err(dev, "missing eco-microamp property\n");
257 			return ret;
258 		}
259 	}
260 
261 	/* parse .off-on-delay */
262 	ret = of_property_read_u32(np, "off-on-delay-us",
263 				   &rdesc->off_on_delay);
264 	if (ret) {
265 		dev_err(dev, "missing off-on-delay-us property\n");
266 		return ret;
267 	}
268 
269 	/* parse .enable_time */
270 	ret = of_property_read_u32(np, "startup-delay-us",
271 				   &rdesc->enable_time);
272 	if (ret) {
273 		dev_err(dev, "missing startup-delay-us property\n");
274 		return ret;
275 	}
276 
277 	/* FIXME: are there a better value for this? */
278 	rdesc->ramp_delay = rdesc->enable_time;
279 
280 	/* parse volt_table */
281 
282 	rdesc->n_voltages = of_property_count_u32_elems(np, "voltage-table");
283 
284 	v_table = devm_kzalloc(dev, sizeof(unsigned int) * rdesc->n_voltages,
285 			       GFP_KERNEL);
286 	if (unlikely(!v_table))
287 		return  -ENOMEM;
288 	rdesc->volt_table = v_table;
289 
290 	ret = of_property_read_u32_array(np, "voltage-table",
291 					 v_table, rdesc->n_voltages);
292 	if (ret) {
293 		dev_err(dev, "missing voltage-table property\n");
294 		return ret;
295 	}
296 
297 	/*
298 	 * Instead of explicitly requiring a mask for the voltage selector,
299 	 * as they all start from bit zero (at least on the known LDOs),
300 	 * just use the number of voltages at the voltage table, getting the
301 	 * minimal mask that would pick everything.
302 	 */
303 	rdesc->vsel_mask = (1 << (fls(rdesc->n_voltages) - 1)) - 1;
304 
305 	dev_dbg(dev, "voltage selector settings: reg: 0x%x, mask: 0x%x\n",
306 		rdesc->vsel_reg, rdesc->vsel_mask);
307 
308 	return 0;
309 }
310 
311 static const struct regulator_ops hi6421_spmi_ldo_rops = {
312 	.is_enabled = hi6421_spmi_regulator_is_enabled,
313 	.enable = hi6421_spmi_regulator_enable,
314 	.disable = hi6421_spmi_regulator_disable,
315 	.list_voltage = regulator_list_voltage_table,
316 	.map_voltage = regulator_map_voltage_iterate,
317 	.get_voltage_sel = hi6421_spmi_regulator_get_voltage_sel,
318 	.set_voltage_sel = hi6421_spmi_regulator_set_voltage_sel,
319 	.get_mode = hi6421_spmi_regulator_get_mode,
320 	.set_mode = hi6421_spmi_regulator_set_mode,
321 	.get_optimum_mode = hi6421_spmi_regulator_get_optimum_mode,
322 };
323 
hi6421_spmi_regulator_probe_ldo(struct platform_device * pdev,struct device_node * np,struct hi6421_spmi_pmic * pmic)324 static int hi6421_spmi_regulator_probe_ldo(struct platform_device *pdev,
325 					   struct device_node *np,
326 					   struct hi6421_spmi_pmic *pmic)
327 {
328 	struct regulation_constraints *constraint;
329 	struct regulator_init_data *initdata;
330 	struct regulator_config config = { };
331 	struct hi6421v600_regulator *sreg;
332 	struct device *dev = &pdev->dev;
333 	struct regulator_desc *rdesc;
334 	struct regulator_dev *rdev;
335 	const char *supplyname;
336 	int ret;
337 
338 	initdata = of_get_regulator_init_data(dev, np, NULL);
339 	if (!initdata) {
340 		dev_err(dev, "failed to get regulator data\n");
341 		return -EINVAL;
342 	}
343 
344 	sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
345 	if (!sreg)
346 		return -ENOMEM;
347 
348 	sreg->pmic = pmic;
349 	rdesc = &sreg->rdesc;
350 
351 	rdesc->name = initdata->constraints.name;
352 	rdesc->ops = &hi6421_spmi_ldo_rops;
353 	rdesc->type = REGULATOR_VOLTAGE;
354 	rdesc->min_uV = initdata->constraints.min_uV;
355 
356 	supplyname = of_get_property(np, "supply_name", NULL);
357 	if (supplyname)
358 		initdata->supply_regulator = supplyname;
359 
360 	/* parse device tree data for regulator specific */
361 	ret = hi6421_spmi_dt_parse(pdev, sreg, rdesc);
362 	if (ret)
363 		return ret;
364 
365 	/* hisi regulator supports two modes */
366 	constraint = &initdata->constraints;
367 
368 	constraint->valid_modes_mask = REGULATOR_MODE_NORMAL;
369 	if (sreg->eco_mode_mask) {
370 		constraint->valid_modes_mask |= REGULATOR_MODE_IDLE;
371 		constraint->valid_ops_mask |= REGULATOR_CHANGE_MODE;
372 	}
373 
374 	config.dev = &pdev->dev;
375 	config.init_data = initdata;
376 	config.driver_data = sreg;
377 	config.of_node = pdev->dev.of_node;
378 
379 	/* register regulator */
380 	rdev = regulator_register(rdesc, &config);
381 	if (IS_ERR(rdev)) {
382 		dev_err(dev, "failed to register %s\n",
383 			rdesc->name);
384 		return PTR_ERR(rdev);
385 	}
386 
387 	rdev_dbg(rdev, "valid_modes_mask: 0x%x, valid_ops_mask: 0x%x\n",
388 		 constraint->valid_modes_mask, constraint->valid_ops_mask);
389 
390 	dev_set_drvdata(dev, rdev);
391 
392 	return 0;
393 }
394 
hi6421_spmi_regulator_probe(struct platform_device * pdev)395 static int hi6421_spmi_regulator_probe(struct platform_device *pdev)
396 {
397 	struct device *pmic_dev = pdev->dev.parent;
398 	struct device_node *np = pmic_dev->of_node;
399 	struct device_node *regulators, *child;
400 	struct platform_device *new_pdev;
401 	struct hi6421_spmi_pmic *pmic;
402 	int ret;
403 
404 	/*
405 	 * This driver is meant to be called by hi6421-spmi-core,
406 	 * which should first set drvdata. If this doesn't happen, hit
407 	 * a warn on and return.
408 	 */
409 	pmic = dev_get_drvdata(pmic_dev);
410 	if (WARN_ON(!pmic))
411 		return -ENODEV;
412 
413 	regulators = of_get_child_by_name(np, "regulators");
414 	if (!regulators) {
415 		dev_err(&pdev->dev, "regulator node not found\n");
416 		return -ENODEV;
417 	}
418 
419 	/*
420 	 * Parse all LDO regulator nodes
421 	 */
422 	for_each_child_of_node(regulators, child) {
423 		dev_dbg(&pdev->dev, "adding child %pOF\n", child);
424 
425 		new_pdev = platform_device_alloc(child->name, -1);
426 		new_pdev->dev.parent = pmic_dev;
427 		new_pdev->dev.of_node = of_node_get(child);
428 
429 		ret = platform_device_add(new_pdev);
430 		if (ret < 0) {
431 			platform_device_put(new_pdev);
432 			continue;
433 		}
434 
435 		ret = hi6421_spmi_regulator_probe_ldo(new_pdev, child, pmic);
436 		if (ret < 0)
437 			platform_device_put(new_pdev);
438 	}
439 
440 	of_node_put(regulators);
441 
442 	return 0;
443 }
444 
hi6421_spmi_regulator_remove(struct platform_device * pdev)445 static int hi6421_spmi_regulator_remove(struct platform_device *pdev)
446 {
447 	struct regulator_dev *rdev = dev_get_drvdata(&pdev->dev);
448 	struct hi6421v600_regulator *sreg = rdev_get_drvdata(rdev);
449 
450 	regulator_unregister(rdev);
451 
452 	if (rdev->desc->volt_table)
453 		devm_kfree(&pdev->dev, (unsigned int *)rdev->desc->volt_table);
454 
455 	kfree(sreg);
456 
457 	return 0;
458 }
459 
460 static const struct platform_device_id hi6421v600_regulator_table[] = {
461 	{ .name = "hi6421v600-regulator" },
462 	{},
463 };
464 MODULE_DEVICE_TABLE(platform, hi6421v600_regulator_table);
465 
466 static struct platform_driver hi6421v600_regulator_driver = {
467 	.id_table = hi6421v600_regulator_table,
468 	.driver = {
469 		.name	= "hi6421v600-regulator",
470 	},
471 	.probe	= hi6421_spmi_regulator_probe,
472 	.remove	= hi6421_spmi_regulator_remove,
473 };
474 module_platform_driver(hi6421v600_regulator_driver);
475 
476 MODULE_DESCRIPTION("Hi6421v600 regulator driver");
477 MODULE_LICENSE("GPL v2");
478 
479