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