2 * tps62360.c -- TI tps62360
4 * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363.
6 * Copyright (c) 2012, NVIDIA Corporation.
8 * Author: Laxman Dewangan <ldewangan@nvidia.com>
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.
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.
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
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/err.h>
29 #include <linux/platform_device.h>
30 #include <linux/regulator/driver.h>
31 #include <linux/regulator/machine.h>
32 #include <linux/regulator/tps62360.h>
33 #include <linux/gpio.h>
34 #include <linux/i2c.h>
35 #include <linux/slab.h>
36 #include <linux/regmap.h>
38 /* Register definitions */
45 #define REG_RAMPCTRL 6
48 enum chips
{TPS62360
, TPS62361
, TPS62362
, TPS62363
};
50 #define TPS62360_BASE_VOLTAGE 770000
51 #define TPS62360_N_VOLTAGES 64
53 #define TPS62361_BASE_VOLTAGE 500000
54 #define TPS62361_N_VOLTAGES 128
56 /* tps 62360 chip information */
57 struct tps62360_chip
{
59 struct regulator_desc desc
;
60 struct regulator_dev
*rdev
;
61 struct regmap
*regmap
;
67 bool en_internal_pulldn
;
72 int curr_vset_vsel
[4];
78 * find_voltage_set_register: Find new voltage configuration register
80 * The finding of the new VSET register will be based on the LRU mechanism.
81 * Each VSET register will have different voltage configured . This
82 * Function will look if any of the VSET register have requested voltage set
84 * - If it is already there then it will make that register as most
85 * recently used and return as found so that caller need not to set
86 * the VSET register but need to set the proper gpios to select this
88 * - If requested voltage is not found then it will use the least
89 * recently mechanism to get new VSET register for new configuration
90 * and will return not_found so that caller need to set new VSET
91 * register and then gpios (both).
93 static bool find_voltage_set_register(struct tps62360_chip
*tps
,
94 int req_vsel
, int *vset_reg_id
)
98 int new_vset_reg
= tps
->lru_index
[3];
101 for (i
= 0; i
< 4; ++i
) {
102 if (tps
->curr_vset_vsel
[tps
->lru_index
[i
]] == req_vsel
) {
103 new_vset_reg
= tps
->lru_index
[i
];
106 goto update_lru_index
;
111 for (i
= found_index
; i
> 0; i
--)
112 tps
->lru_index
[i
] = tps
->lru_index
[i
- 1];
114 tps
->lru_index
[0] = new_vset_reg
;
115 *vset_reg_id
= new_vset_reg
;
119 static int tps62360_dcdc_get_voltage_sel(struct regulator_dev
*dev
)
121 struct tps62360_chip
*tps
= rdev_get_drvdata(dev
);
126 ret
= regmap_read(tps
->regmap
, REG_VSET0
+ tps
->curr_vset_id
, &data
);
128 dev_err(tps
->dev
, "%s(): register %d read failed with err %d\n",
129 __func__
, REG_VSET0
+ tps
->curr_vset_id
, ret
);
132 vsel
= (int)data
& tps
->voltage_reg_mask
;
136 static int tps62360_dcdc_set_voltage(struct regulator_dev
*dev
,
137 int min_uV
, int max_uV
, unsigned *selector
)
139 struct tps62360_chip
*tps
= rdev_get_drvdata(dev
);
143 int new_vset_id
= tps
->curr_vset_id
;
145 if ((max_uV
< min_uV
) || (max_uV
< tps
->voltage_base
))
148 if (min_uV
> (tps
->voltage_base
+ (tps
->desc
.n_voltages
- 1) * 10000))
151 vsel
= DIV_ROUND_UP(min_uV
- tps
->voltage_base
, 10000);
153 *selector
= (vsel
& tps
->voltage_reg_mask
);
156 * If gpios are available to select the VSET register then least
157 * recently used register for new configuration.
159 if (tps
->valid_gpios
)
160 found
= find_voltage_set_register(tps
, vsel
, &new_vset_id
);
163 ret
= regmap_update_bits(tps
->regmap
, REG_VSET0
+ new_vset_id
,
164 tps
->voltage_reg_mask
, vsel
);
167 "%s(): register %d update failed with err %d\n",
168 __func__
, REG_VSET0
+ new_vset_id
, ret
);
171 tps
->curr_vset_id
= new_vset_id
;
172 tps
->curr_vset_vsel
[new_vset_id
] = vsel
;
175 /* Select proper VSET register vio gpios */
176 if (tps
->valid_gpios
) {
177 gpio_set_value_cansleep(tps
->vsel0_gpio
, new_vset_id
& 0x1);
178 gpio_set_value_cansleep(tps
->vsel1_gpio
,
179 (new_vset_id
>> 1) & 0x1);
184 static int tps62360_dcdc_list_voltage(struct regulator_dev
*dev
,
187 struct tps62360_chip
*tps
= rdev_get_drvdata(dev
);
189 if (selector
>= tps
->desc
.n_voltages
)
192 return tps
->voltage_base
+ selector
* 10000;
195 static int tps62360_set_voltage_time_sel(struct regulator_dev
*rdev
,
196 unsigned int old_selector
, unsigned int new_selector
)
198 struct tps62360_chip
*tps
= rdev_get_drvdata(rdev
);
201 old_uV
= tps62360_dcdc_list_voltage(rdev
, old_selector
);
205 new_uV
= tps62360_dcdc_list_voltage(rdev
, new_selector
);
209 return DIV_ROUND_UP(abs(old_uV
- new_uV
), tps
->change_uv_per_us
);
212 static struct regulator_ops tps62360_dcdc_ops
= {
213 .get_voltage_sel
= tps62360_dcdc_get_voltage_sel
,
214 .set_voltage
= tps62360_dcdc_set_voltage
,
215 .list_voltage
= tps62360_dcdc_list_voltage
,
216 .set_voltage_time_sel
= tps62360_set_voltage_time_sel
,
219 static int __devinit
tps62360_init_force_pwm(struct tps62360_chip
*tps
,
220 struct tps62360_regulator_platform_data
*pdata
,
226 if (pdata
->en_force_pwm
)
229 ret
= regmap_update_bits(tps
->regmap
, REG_VSET0
+ vset_id
, BIT(7), bit
);
232 "%s(): register %d update failed with err %d\n",
233 __func__
, REG_VSET0
+ vset_id
, ret
);
237 static int __devinit
tps62360_init_dcdc(struct tps62360_chip
*tps
,
238 struct tps62360_regulator_platform_data
*pdata
)
242 unsigned int ramp_ctrl
;
244 /* Initialize internal pull up/down control */
245 if (tps
->en_internal_pulldn
)
246 ret
= regmap_write(tps
->regmap
, REG_CONTROL
, 0xE0);
248 ret
= regmap_write(tps
->regmap
, REG_CONTROL
, 0x0);
251 "%s(): register %d write failed with err %d\n",
252 __func__
, REG_CONTROL
, ret
);
256 /* Initialize force PWM mode */
257 if (tps
->valid_gpios
) {
258 for (i
= 0; i
< 4; ++i
) {
259 ret
= tps62360_init_force_pwm(tps
, pdata
, i
);
264 ret
= tps62360_init_force_pwm(tps
, pdata
, tps
->curr_vset_id
);
269 /* Reset output discharge path to reduce power consumption */
270 ret
= regmap_update_bits(tps
->regmap
, REG_RAMPCTRL
, BIT(2), 0);
273 "%s(): register %d update failed with err %d\n",
274 __func__
, REG_RAMPCTRL
, ret
);
278 /* Get ramp value from ramp control register */
279 ret
= regmap_read(tps
->regmap
, REG_RAMPCTRL
, &ramp_ctrl
);
282 "%s(): register %d read failed with err %d\n",
283 __func__
, REG_RAMPCTRL
, ret
);
286 ramp_ctrl
= (ramp_ctrl
>> 4) & 0x7;
288 /* ramp mV/us = 32/(2^ramp_ctrl) */
289 tps
->change_uv_per_us
= DIV_ROUND_UP(32000, BIT(ramp_ctrl
));
293 static const struct regmap_config tps62360_regmap_config
= {
296 .max_register
= REG_CHIPID
,
297 .cache_type
= REGCACHE_RBTREE
,
300 static int __devinit
tps62360_probe(struct i2c_client
*client
,
301 const struct i2c_device_id
*id
)
303 struct regulator_config config
= { };
304 struct tps62360_regulator_platform_data
*pdata
;
305 struct regulator_dev
*rdev
;
306 struct tps62360_chip
*tps
;
310 pdata
= client
->dev
.platform_data
;
312 dev_err(&client
->dev
, "%s(): Platform data not found\n",
317 tps
= devm_kzalloc(&client
->dev
, sizeof(*tps
), GFP_KERNEL
);
319 dev_err(&client
->dev
, "%s(): Memory allocation failed\n",
324 tps
->en_force_pwm
= pdata
->en_force_pwm
;
325 tps
->en_discharge
= pdata
->en_discharge
;
326 tps
->en_internal_pulldn
= pdata
->en_internal_pulldn
;
327 tps
->vsel0_gpio
= pdata
->vsel0_gpio
;
328 tps
->vsel1_gpio
= pdata
->vsel1_gpio
;
329 tps
->dev
= &client
->dev
;
331 switch (id
->driver_data
) {
334 tps
->voltage_base
= TPS62360_BASE_VOLTAGE
;
335 tps
->voltage_reg_mask
= 0x3F;
336 tps
->desc
.n_voltages
= TPS62360_N_VOLTAGES
;
340 tps
->voltage_base
= TPS62361_BASE_VOLTAGE
;
341 tps
->voltage_reg_mask
= 0x7F;
342 tps
->desc
.n_voltages
= TPS62361_N_VOLTAGES
;
348 tps
->desc
.name
= id
->name
;
350 tps
->desc
.ops
= &tps62360_dcdc_ops
;
351 tps
->desc
.type
= REGULATOR_VOLTAGE
;
352 tps
->desc
.owner
= THIS_MODULE
;
353 tps
->regmap
= devm_regmap_init_i2c(client
, &tps62360_regmap_config
);
354 if (IS_ERR(tps
->regmap
)) {
355 ret
= PTR_ERR(tps
->regmap
);
356 dev_err(&client
->dev
,
357 "%s(): regmap allocation failed with err %d\n",
361 i2c_set_clientdata(client
, tps
);
363 tps
->curr_vset_id
= (pdata
->vsel1_def_state
& 1) * 2 +
364 (pdata
->vsel0_def_state
& 1);
365 tps
->lru_index
[0] = tps
->curr_vset_id
;
366 tps
->valid_gpios
= false;
368 if (gpio_is_valid(tps
->vsel0_gpio
) && gpio_is_valid(tps
->vsel1_gpio
)) {
370 gpio_flags
= (pdata
->vsel0_def_state
) ?
371 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
;
372 ret
= gpio_request_one(tps
->vsel0_gpio
,
373 gpio_flags
, "tps62360-vsel0");
375 dev_err(&client
->dev
,
376 "%s(): Could not obtain vsel0 GPIO %d: %d\n",
377 __func__
, tps
->vsel0_gpio
, ret
);
381 gpio_flags
= (pdata
->vsel1_def_state
) ?
382 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
;
383 ret
= gpio_request_one(tps
->vsel1_gpio
,
384 gpio_flags
, "tps62360-vsel1");
386 dev_err(&client
->dev
,
387 "%s(): Could not obtain vsel1 GPIO %d: %d\n",
388 __func__
, tps
->vsel1_gpio
, ret
);
391 tps
->valid_gpios
= true;
394 * Initialize the lru index with vset_reg id
395 * The index 0 will be most recently used and
396 * set with the tps->curr_vset_id */
397 for (i
= 0; i
< 4; ++i
)
398 tps
->lru_index
[i
] = i
;
399 tps
->lru_index
[0] = tps
->curr_vset_id
;
400 tps
->lru_index
[tps
->curr_vset_id
] = 0;
403 ret
= tps62360_init_dcdc(tps
, pdata
);
405 dev_err(tps
->dev
, "%s(): Init failed with err = %d\n",
410 config
.dev
= &client
->dev
;
411 config
.init_data
= pdata
->reg_init_data
;
412 config
.driver_data
= tps
;
414 /* Register the regulators */
415 rdev
= regulator_register(&tps
->desc
, &config
);
418 "%s(): regulator register failed with err %s\n",
428 if (gpio_is_valid(tps
->vsel1_gpio
))
429 gpio_free(tps
->vsel1_gpio
);
431 if (gpio_is_valid(tps
->vsel0_gpio
))
432 gpio_free(tps
->vsel0_gpio
);
438 * tps62360_remove - tps62360 driver i2c remove handler
439 * @client: i2c driver client device structure
441 * Unregister TPS driver as an i2c client device driver
443 static int __devexit
tps62360_remove(struct i2c_client
*client
)
445 struct tps62360_chip
*tps
= i2c_get_clientdata(client
);
447 if (gpio_is_valid(tps
->vsel1_gpio
))
448 gpio_free(tps
->vsel1_gpio
);
450 if (gpio_is_valid(tps
->vsel0_gpio
))
451 gpio_free(tps
->vsel0_gpio
);
453 regulator_unregister(tps
->rdev
);
457 static void tps62360_shutdown(struct i2c_client
*client
)
459 struct tps62360_chip
*tps
= i2c_get_clientdata(client
);
462 if (!tps
->en_discharge
)
465 /* Configure the output discharge path */
466 st
= regmap_update_bits(tps
->regmap
, REG_RAMPCTRL
, BIT(2), BIT(2));
469 "%s(): register %d update failed with err %d\n",
470 __func__
, REG_RAMPCTRL
, st
);
473 static const struct i2c_device_id tps62360_id
[] = {
474 {.name
= "tps62360", .driver_data
= TPS62360
},
475 {.name
= "tps62361", .driver_data
= TPS62361
},
476 {.name
= "tps62362", .driver_data
= TPS62362
},
477 {.name
= "tps62363", .driver_data
= TPS62363
},
481 MODULE_DEVICE_TABLE(i2c
, tps62360_id
);
483 static struct i2c_driver tps62360_i2c_driver
= {
486 .owner
= THIS_MODULE
,
488 .probe
= tps62360_probe
,
489 .remove
= __devexit_p(tps62360_remove
),
490 .shutdown
= tps62360_shutdown
,
491 .id_table
= tps62360_id
,
494 static int __init
tps62360_init(void)
496 return i2c_add_driver(&tps62360_i2c_driver
);
498 subsys_initcall(tps62360_init
);
500 static void __exit
tps62360_cleanup(void)
502 i2c_del_driver(&tps62360_i2c_driver
);
504 module_exit(tps62360_cleanup
);
506 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
507 MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
508 MODULE_LICENSE("GPL v2");