1 /*
2  * tps62360.c -- TI tps62360
3  *
4  * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363.
5  *
6  * Copyright (c) 2012, NVIDIA Corporation.
7  *
8  * Author: Laxman Dewangan <ldewangan@nvidia.com>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation version 2.
13  *
14  * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
15  * whether express or implied; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22  * 02111-1307, USA
23  */
24 
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/err.h>
29 #include <linux/of.h>
30 #include <linux/of_device.h>
31 #include <linux/regulator/of_regulator.h>
32 #include <linux/platform_device.h>
33 #include <linux/regulator/driver.h>
34 #include <linux/regulator/machine.h>
35 #include <linux/regulator/tps62360.h>
36 #include <linux/gpio/consumer.h>
37 #include <linux/i2c.h>
38 #include <linux/slab.h>
39 #include <linux/regmap.h>
40 
41 /* Register definitions */
42 #define REG_VSET0		0
43 #define REG_VSET1		1
44 #define REG_VSET2		2
45 #define REG_VSET3		3
46 #define REG_CONTROL		4
47 #define REG_TEMP		5
48 #define REG_RAMPCTRL		6
49 #define REG_CHIPID		8
50 
51 #define FORCE_PWM_ENABLE	BIT(7)
52 
53 enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
54 
55 #define TPS62360_BASE_VOLTAGE	770000
56 #define TPS62360_N_VOLTAGES	64
57 
58 #define TPS62361_BASE_VOLTAGE	500000
59 #define TPS62361_N_VOLTAGES	128
60 
61 /* tps 62360 chip information */
62 struct tps62360_chip {
63 	struct device *dev;
64 	struct regulator_desc desc;
65 	struct regulator_dev *rdev;
66 	struct regmap *regmap;
67 	struct gpio_desc *vsel0_gpio;
68 	struct gpio_desc *vsel1_gpio;
69 	u8 voltage_reg_mask;
70 	bool en_internal_pulldn;
71 	bool en_discharge;
72 	bool valid_gpios;
73 	int lru_index[4];
74 	int curr_vset_vsel[4];
75 	int curr_vset_id;
76 };
77 
78 /*
79  * find_voltage_set_register: Find new voltage configuration register
80  * (VSET) id.
81  * The finding of the new VSET register will be based on the LRU mechanism.
82  * Each VSET register will have different voltage configured . This
83  * Function will look if any of the VSET register have requested voltage set
84  * or not.
85  *     - If it is already there then it will make that register as most
86  *       recently used and return as found so that caller need not to set
87  *       the VSET register but need to set the proper gpios to select this
88  *       VSET register.
89  *     - If requested voltage is not found then it will use the least
90  *       recently mechanism to get new VSET register for new configuration
91  *       and will return not_found so that caller need to set new VSET
92  *       register and then gpios (both).
93  */
94 static bool find_voltage_set_register(struct tps62360_chip *tps,
95 		int req_vsel, int *vset_reg_id)
96 {
97 	int i;
98 	bool found = false;
99 	int new_vset_reg = tps->lru_index[3];
100 	int found_index = 3;
101 
102 	for (i = 0; i < 4; ++i) {
103 		if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
104 			new_vset_reg = tps->lru_index[i];
105 			found_index = i;
106 			found = true;
107 			goto update_lru_index;
108 		}
109 	}
110 
111 update_lru_index:
112 	for (i = found_index; i > 0; i--)
113 		tps->lru_index[i] = tps->lru_index[i - 1];
114 
115 	tps->lru_index[0] = new_vset_reg;
116 	*vset_reg_id = new_vset_reg;
117 	return found;
118 }
119 
120 static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
121 {
122 	struct tps62360_chip *tps = rdev_get_drvdata(dev);
123 	int vsel;
124 	unsigned int data;
125 	int ret;
126 
127 	ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
128 	if (ret < 0) {
129 		dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
130 			__func__, REG_VSET0 + tps->curr_vset_id, ret);
131 		return ret;
132 	}
133 	vsel = (int)data & tps->voltage_reg_mask;
134 	return vsel;
135 }
136 
137 static int tps62360_dcdc_set_voltage_sel(struct regulator_dev *dev,
138 					 unsigned selector)
139 {
140 	struct tps62360_chip *tps = rdev_get_drvdata(dev);
141 	int ret;
142 	bool found = false;
143 	int new_vset_id = tps->curr_vset_id;
144 
145 	/*
146 	 * If gpios are available to select the VSET register then least
147 	 * recently used register for new configuration.
148 	 */
149 	if (tps->valid_gpios)
150 		found = find_voltage_set_register(tps, selector, &new_vset_id);
151 
152 	if (!found) {
153 		ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
154 				tps->voltage_reg_mask, selector);
155 		if (ret < 0) {
156 			dev_err(tps->dev,
157 				"%s(): register %d update failed with err %d\n",
158 				 __func__, REG_VSET0 + new_vset_id, ret);
159 			return ret;
160 		}
161 		tps->curr_vset_id = new_vset_id;
162 		tps->curr_vset_vsel[new_vset_id] = selector;
163 	}
164 
165 	/* Select proper VSET register vio gpios */
166 	if (tps->valid_gpios) {
167 		gpiod_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1);
168 		gpiod_set_value_cansleep(tps->vsel1_gpio,
169 					(new_vset_id >> 1) & 0x1);
170 	}
171 	return 0;
172 }
173 
174 static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode)
175 {
176 	struct tps62360_chip *tps = rdev_get_drvdata(rdev);
177 	int i;
178 	int val;
179 	int ret;
180 
181 	/* Enable force PWM mode in FAST mode only. */
182 	switch (mode) {
183 	case REGULATOR_MODE_FAST:
184 		val = FORCE_PWM_ENABLE;
185 		break;
186 
187 	case REGULATOR_MODE_NORMAL:
188 		val = 0;
189 		break;
190 
191 	default:
192 		return -EINVAL;
193 	}
194 
195 	if (!tps->valid_gpios) {
196 		ret = regmap_update_bits(tps->regmap,
197 			REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val);
198 		if (ret < 0)
199 			dev_err(tps->dev,
200 				"%s(): register %d update failed with err %d\n",
201 				__func__, REG_VSET0 + tps->curr_vset_id, ret);
202 		return ret;
203 	}
204 
205 	/* If gpios are valid then all register set need to be control */
206 	for (i = 0; i < 4; ++i) {
207 		ret = regmap_update_bits(tps->regmap,
208 					REG_VSET0 + i, FORCE_PWM_ENABLE, val);
209 		if (ret < 0) {
210 			dev_err(tps->dev,
211 				"%s(): register %d update failed with err %d\n",
212 				__func__, REG_VSET0 + i, ret);
213 			return ret;
214 		}
215 	}
216 	return ret;
217 }
218 
219 static unsigned int tps62360_get_mode(struct regulator_dev *rdev)
220 {
221 	struct tps62360_chip *tps = rdev_get_drvdata(rdev);
222 	unsigned int data;
223 	int ret;
224 
225 	ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
226 	if (ret < 0) {
227 		dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
228 			__func__, REG_VSET0 + tps->curr_vset_id, ret);
229 		return ret;
230 	}
231 	return (data & FORCE_PWM_ENABLE) ?
232 				REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
233 }
234 
235 static const struct regulator_ops tps62360_dcdc_ops = {
236 	.get_voltage_sel	= tps62360_dcdc_get_voltage_sel,
237 	.set_voltage_sel	= tps62360_dcdc_set_voltage_sel,
238 	.list_voltage		= regulator_list_voltage_linear,
239 	.map_voltage		= regulator_map_voltage_linear,
240 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
241 	.set_mode		= tps62360_set_mode,
242 	.get_mode		= tps62360_get_mode,
243 };
244 
245 static int tps62360_init_dcdc(struct tps62360_chip *tps,
246 		struct tps62360_regulator_platform_data *pdata)
247 {
248 	int ret;
249 	unsigned int ramp_ctrl;
250 
251 	/* Initialize internal pull up/down control */
252 	if (tps->en_internal_pulldn)
253 		ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
254 	else
255 		ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
256 	if (ret < 0) {
257 		dev_err(tps->dev,
258 			"%s(): register %d write failed with err %d\n",
259 			__func__, REG_CONTROL, ret);
260 		return ret;
261 	}
262 
263 	/* Reset output discharge path to reduce power consumption */
264 	ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
265 	if (ret < 0) {
266 		dev_err(tps->dev,
267 			"%s(): register %d update failed with err %d\n",
268 			__func__, REG_RAMPCTRL, ret);
269 		return ret;
270 	}
271 
272 	/* Get ramp value from ramp control register */
273 	ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
274 	if (ret < 0) {
275 		dev_err(tps->dev,
276 			"%s(): register %d read failed with err %d\n",
277 			__func__, REG_RAMPCTRL, ret);
278 		return ret;
279 	}
280 	ramp_ctrl = (ramp_ctrl >> 5) & 0x7;
281 
282 	/* ramp mV/us = 32/(2^ramp_ctrl) */
283 	tps->desc.ramp_delay = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
284 	return ret;
285 }
286 
287 static const struct regmap_config tps62360_regmap_config = {
288 	.reg_bits		= 8,
289 	.val_bits		= 8,
290 	.max_register		= REG_CHIPID,
291 	.cache_type		= REGCACHE_RBTREE,
292 };
293 
294 static struct tps62360_regulator_platform_data *
295 	of_get_tps62360_platform_data(struct device *dev,
296 				      const struct regulator_desc *desc)
297 {
298 	struct tps62360_regulator_platform_data *pdata;
299 	struct device_node *np = dev->of_node;
300 
301 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
302 	if (!pdata)
303 		return NULL;
304 
305 	pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node,
306 							  desc);
307 	if (!pdata->reg_init_data) {
308 		dev_err(dev, "Not able to get OF regulator init data\n");
309 		return NULL;
310 	}
311 
312 	if (of_find_property(np, "ti,vsel0-state-high", NULL))
313 		pdata->vsel0_def_state = 1;
314 
315 	if (of_find_property(np, "ti,vsel1-state-high", NULL))
316 		pdata->vsel1_def_state = 1;
317 
318 	if (of_find_property(np, "ti,enable-pull-down", NULL))
319 		pdata->en_internal_pulldn = true;
320 
321 	if (of_find_property(np, "ti,enable-vout-discharge", NULL))
322 		pdata->en_discharge = true;
323 
324 	return pdata;
325 }
326 
327 #if defined(CONFIG_OF)
328 static const struct of_device_id tps62360_of_match[] = {
329 	 { .compatible = "ti,tps62360", .data = (void *)TPS62360},
330 	 { .compatible = "ti,tps62361", .data = (void *)TPS62361},
331 	 { .compatible = "ti,tps62362", .data = (void *)TPS62362},
332 	 { .compatible = "ti,tps62363", .data = (void *)TPS62363},
333 	{},
334 };
335 MODULE_DEVICE_TABLE(of, tps62360_of_match);
336 #endif
337 
338 static int tps62360_probe(struct i2c_client *client,
339 				     const struct i2c_device_id *id)
340 {
341 	struct regulator_config config = { };
342 	struct tps62360_regulator_platform_data *pdata;
343 	struct regulator_dev *rdev;
344 	struct tps62360_chip *tps;
345 	int ret;
346 	int i;
347 	int chip_id;
348 	int gpio_flags;
349 
350 	pdata = dev_get_platdata(&client->dev);
351 
352 	tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
353 	if (!tps)
354 		return -ENOMEM;
355 
356 	tps->desc.name = client->name;
357 	tps->desc.id = 0;
358 	tps->desc.ops = &tps62360_dcdc_ops;
359 	tps->desc.type = REGULATOR_VOLTAGE;
360 	tps->desc.owner = THIS_MODULE;
361 	tps->desc.uV_step = 10000;
362 
363 	if (client->dev.of_node) {
364 		const struct of_device_id *match;
365 		match = of_match_device(of_match_ptr(tps62360_of_match),
366 				&client->dev);
367 		if (!match) {
368 			dev_err(&client->dev, "Error: No device match found\n");
369 			return -ENODEV;
370 		}
371 		chip_id = (int)(long)match->data;
372 		if (!pdata)
373 			pdata = of_get_tps62360_platform_data(&client->dev,
374 							      &tps->desc);
375 	} else if (id) {
376 		chip_id = id->driver_data;
377 	} else {
378 		dev_err(&client->dev, "No device tree match or id table match found\n");
379 		return -ENODEV;
380 	}
381 
382 	if (!pdata) {
383 		dev_err(&client->dev, "%s(): Platform data not found\n",
384 						__func__);
385 		return -EIO;
386 	}
387 
388 	tps->en_discharge = pdata->en_discharge;
389 	tps->en_internal_pulldn = pdata->en_internal_pulldn;
390 	tps->dev = &client->dev;
391 
392 	switch (chip_id) {
393 	case TPS62360:
394 	case TPS62362:
395 		tps->desc.min_uV = TPS62360_BASE_VOLTAGE;
396 		tps->voltage_reg_mask = 0x3F;
397 		tps->desc.n_voltages = TPS62360_N_VOLTAGES;
398 		break;
399 	case TPS62361:
400 	case TPS62363:
401 		tps->desc.min_uV = TPS62361_BASE_VOLTAGE;
402 		tps->voltage_reg_mask = 0x7F;
403 		tps->desc.n_voltages = TPS62361_N_VOLTAGES;
404 		break;
405 	default:
406 		return -ENODEV;
407 	}
408 
409 	tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
410 	if (IS_ERR(tps->regmap)) {
411 		ret = PTR_ERR(tps->regmap);
412 		dev_err(&client->dev,
413 			"%s(): regmap allocation failed with err %d\n",
414 			__func__, ret);
415 		return ret;
416 	}
417 	i2c_set_clientdata(client, tps);
418 
419 	tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
420 				(pdata->vsel0_def_state & 1);
421 	tps->lru_index[0] = tps->curr_vset_id;
422 	tps->valid_gpios = false;
423 
424 	gpio_flags = (pdata->vsel0_def_state) ?
425 			GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
426 	tps->vsel0_gpio = devm_gpiod_get_optional(&client->dev, "vsel0", gpio_flags);
427 	if (IS_ERR(tps->vsel0_gpio)) {
428 		dev_err(&client->dev,
429 			"%s(): Could not obtain vsel0 GPIO: %ld\n",
430 			__func__, PTR_ERR(tps->vsel0_gpio));
431 		return PTR_ERR(tps->vsel0_gpio);
432 	}
433 
434 	gpio_flags = (pdata->vsel1_def_state) ?
435 			GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
436 	tps->vsel1_gpio = devm_gpiod_get_optional(&client->dev, "vsel1", gpio_flags);
437 	if (IS_ERR(tps->vsel1_gpio)) {
438 		dev_err(&client->dev,
439 			"%s(): Could not obtain vsel1 GPIO: %ld\n",
440 			__func__, PTR_ERR(tps->vsel1_gpio));
441 		return PTR_ERR(tps->vsel1_gpio);
442 	}
443 
444 	if (tps->vsel0_gpio != NULL && tps->vsel1_gpio != NULL) {
445 		tps->valid_gpios = true;
446 
447 		/*
448 		 * Initialize the lru index with vset_reg id
449 		 * The index 0 will be most recently used and
450 		 * set with the tps->curr_vset_id */
451 		for (i = 0; i < 4; ++i)
452 			tps->lru_index[i] = i;
453 		tps->lru_index[0] = tps->curr_vset_id;
454 		tps->lru_index[tps->curr_vset_id] = 0;
455 	}
456 
457 	ret = tps62360_init_dcdc(tps, pdata);
458 	if (ret < 0) {
459 		dev_err(tps->dev, "%s(): Init failed with err = %d\n",
460 				__func__, ret);
461 		return ret;
462 	}
463 
464 	config.dev = &client->dev;
465 	config.init_data = pdata->reg_init_data;
466 	config.driver_data = tps;
467 	config.of_node = client->dev.of_node;
468 
469 	/* Register the regulators */
470 	rdev = devm_regulator_register(&client->dev, &tps->desc, &config);
471 	if (IS_ERR(rdev)) {
472 		dev_err(tps->dev,
473 			"%s(): regulator register failed with err %s\n",
474 			__func__, id->name);
475 		return PTR_ERR(rdev);
476 	}
477 
478 	tps->rdev = rdev;
479 	return 0;
480 }
481 
482 static void tps62360_shutdown(struct i2c_client *client)
483 {
484 	struct tps62360_chip *tps = i2c_get_clientdata(client);
485 	int st;
486 
487 	if (!tps->en_discharge)
488 		return;
489 
490 	/* Configure the output discharge path */
491 	st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
492 	if (st < 0)
493 		dev_err(tps->dev,
494 			"%s(): register %d update failed with err %d\n",
495 			__func__, REG_RAMPCTRL, st);
496 }
497 
498 static const struct i2c_device_id tps62360_id[] = {
499 	{.name = "tps62360", .driver_data = TPS62360},
500 	{.name = "tps62361", .driver_data = TPS62361},
501 	{.name = "tps62362", .driver_data = TPS62362},
502 	{.name = "tps62363", .driver_data = TPS62363},
503 	{},
504 };
505 
506 MODULE_DEVICE_TABLE(i2c, tps62360_id);
507 
508 static struct i2c_driver tps62360_i2c_driver = {
509 	.driver = {
510 		.name = "tps62360",
511 		.of_match_table = of_match_ptr(tps62360_of_match),
512 	},
513 	.probe = tps62360_probe,
514 	.shutdown = tps62360_shutdown,
515 	.id_table = tps62360_id,
516 };
517 
518 static int __init tps62360_init(void)
519 {
520 	return i2c_add_driver(&tps62360_i2c_driver);
521 }
522 subsys_initcall(tps62360_init);
523 
524 static void __exit tps62360_cleanup(void)
525 {
526 	i2c_del_driver(&tps62360_i2c_driver);
527 }
528 module_exit(tps62360_cleanup);
529 
530 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
531 MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
532 MODULE_LICENSE("GPL v2");
533