2 * tps62360.c -- TI tps62360
4 * Driver for processor core supply tps62360 and tps62361B
6 * Copyright (c) 2012, NVIDIA Corporation.
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/delay.h>
36 #include <linux/slab.h>
37 #include <linux/regmap.h>
39 /* Register definitions */
46 #define REG_RAMPCTRL 6
49 enum chips {TPS62360, TPS62361};
51 #define TPS62360_BASE_VOLTAGE 770
52 #define TPS62360_N_VOLTAGES 64
54 #define TPS62361_BASE_VOLTAGE 500
55 #define TPS62361_N_VOLTAGES 128
57 /* tps 62360 chip information */
58 struct tps62360_chip {
61 struct regulator_desc desc;
62 struct i2c_client *client;
63 struct regulator_dev *rdev;
64 struct regmap *regmap;
70 bool en_internal_pulldn;
75 int curr_vset_vsel[4];
80 * find_voltage_set_register: Find new voltage configuration register
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
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
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).
95 static bool find_voltage_set_register(struct tps62360_chip *tps,
96 int req_vsel, int *vset_reg_id)
100 int new_vset_reg = tps->lru_index[3];
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];
107 goto update_lru_index;
112 for (i = found_index; i > 0; i--)
113 tps->lru_index[i] = tps->lru_index[i - 1];
115 tps->lru_index[0] = new_vset_reg;
116 *vset_reg_id = new_vset_reg;
120 static int tps62360_dcdc_get_voltage(struct regulator_dev *dev)
122 struct tps62360_chip *tps = rdev_get_drvdata(dev);
127 ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
129 dev_err(tps->dev, "%s: Error in reading register %d\n",
130 __func__, REG_VSET0 + tps->curr_vset_id);
133 vsel = (int)data & tps->voltage_reg_mask;
134 return (tps->voltage_base + vsel * 10) * 1000;
137 static int tps62360_dcdc_set_voltage(struct regulator_dev *dev,
138 int min_uV, int max_uV, unsigned *selector)
140 struct tps62360_chip *tps = rdev_get_drvdata(dev);
144 int new_vset_id = tps->curr_vset_id;
150 ((tps->voltage_base + (tps->desc.n_voltages - 1) * 10) * 1000))
153 if (max_uV < tps->voltage_base * 1000)
156 vsel = DIV_ROUND_UP(min_uV - (tps->voltage_base * 1000), 10000);
158 *selector = (vsel & tps->voltage_reg_mask);
161 * If gpios are available to select the VSET register then least
162 * recently used register for new configuration.
164 if (tps->valid_gpios)
165 found = find_voltage_set_register(tps, vsel, &new_vset_id);
168 ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
169 tps->voltage_reg_mask, vsel);
171 dev_err(tps->dev, "%s: Error in updating register %d\n",
172 __func__, REG_VSET0 + new_vset_id);
175 tps->curr_vset_id = new_vset_id;
176 tps->curr_vset_vsel[new_vset_id] = vsel;
179 /* Select proper VSET register vio gpios */
180 if (tps->valid_gpios) {
181 gpio_set_value_cansleep(tps->vsel0_gpio,
183 gpio_set_value_cansleep(tps->vsel1_gpio,
184 (new_vset_id >> 1) & 0x1);
189 static int tps62360_dcdc_list_voltage(struct regulator_dev *dev,
192 struct tps62360_chip *tps = rdev_get_drvdata(dev);
194 if (selector >= tps->desc.n_voltages)
196 return (tps->voltage_base + selector * 10) * 1000;
199 static struct regulator_ops tps62360_dcdc_ops = {
200 .get_voltage = tps62360_dcdc_get_voltage,
201 .set_voltage = tps62360_dcdc_set_voltage,
202 .list_voltage = tps62360_dcdc_list_voltage,
205 static int tps62360_init_force_pwm(struct tps62360_chip *tps,
206 struct tps62360_regulator_platform_data *pdata,
211 ret = regmap_read(tps->regmap, REG_VSET0 + vset_id, &data);
213 dev_err(tps->dev, "%s() fails in writing reg %d\n",
214 __func__, REG_VSET0 + vset_id);
217 tps->curr_vset_vsel[vset_id] = data & tps->voltage_reg_mask;
218 if (pdata->en_force_pwm)
222 ret = regmap_write(tps->regmap, REG_VSET0 + vset_id, data);
224 dev_err(tps->dev, "%s() fails in writing reg %d\n",
225 __func__, REG_VSET0 + vset_id);
229 static int tps62360_init_dcdc(struct tps62360_chip *tps,
230 struct tps62360_regulator_platform_data *pdata)
235 /* Initailize internal pull up/down control */
236 if (tps->en_internal_pulldn)
237 ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
239 ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
241 dev_err(tps->dev, "%s() fails in writing reg %d\n",
242 __func__, REG_CONTROL);
246 /* Initailize force PWM mode */
247 if (tps->valid_gpios) {
248 for (i = 0; i < 4; ++i) {
249 ret = tps62360_init_force_pwm(tps, pdata, i);
254 ret = tps62360_init_force_pwm(tps, pdata, tps->curr_vset_id);
259 /* Reset output discharge path to reduce power consumption */
260 ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
262 dev_err(tps->dev, "%s() fails in updating reg %d\n",
263 __func__, REG_RAMPCTRL);
267 static const struct regmap_config tps62360_regmap_config = {
272 static int __devinit tps62360_probe(struct i2c_client *client,
273 const struct i2c_device_id *id)
275 struct tps62360_regulator_platform_data *pdata;
276 struct regulator_dev *rdev;
277 struct tps62360_chip *tps;
281 pdata = client->dev.platform_data;
283 dev_err(&client->dev, "%s() Err: Platform data not found\n",
288 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
290 dev_err(&client->dev, "%s() Err: Memory allocation fails\n",
295 tps->en_force_pwm = pdata->en_force_pwm;
296 tps->en_discharge = pdata->en_discharge;
297 tps->en_internal_pulldn = pdata->en_internal_pulldn;
298 tps->vsel0_gpio = pdata->vsel0_gpio;
299 tps->vsel1_gpio = pdata->vsel1_gpio;
300 tps->client = client;
301 tps->dev = &client->dev;
302 tps->name = id->name;
303 tps->voltage_base = (id->driver_data == TPS62360) ?
304 TPS62360_BASE_VOLTAGE : TPS62361_BASE_VOLTAGE;
305 tps->voltage_reg_mask = (id->driver_data == TPS62360) ? 0x3F : 0x7F;
307 tps->desc.name = id->name;
309 tps->desc.n_voltages = (id->driver_data == TPS62360) ?
310 TPS62360_N_VOLTAGES : TPS62361_N_VOLTAGES;
311 tps->desc.ops = &tps62360_dcdc_ops;
312 tps->desc.type = REGULATOR_VOLTAGE;
313 tps->desc.owner = THIS_MODULE;
314 tps->regmap = regmap_init_i2c(client, &tps62360_regmap_config);
315 if (IS_ERR(tps->regmap)) {
316 ret = PTR_ERR(tps->regmap);
317 dev_err(&client->dev, "%s() Err: Failed to allocate register"
318 "map: %d\n", __func__, ret);
321 i2c_set_clientdata(client, tps);
323 tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
324 (pdata->vsel0_def_state & 1);
325 tps->lru_index[0] = tps->curr_vset_id;
326 tps->valid_gpios = false;
328 if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
329 ret = gpio_request(tps->vsel0_gpio, "tps62360-vsel0");
331 dev_err(&client->dev,
332 "Err: Could not obtain vsel0 GPIO %d: %d\n",
333 tps->vsel0_gpio, ret);
336 ret = gpio_direction_output(tps->vsel0_gpio,
337 pdata->vsel0_def_state);
339 dev_err(&client->dev, "Err: Could not set direction of"
340 "vsel0 GPIO %d: %d\n", tps->vsel0_gpio, ret);
341 gpio_free(tps->vsel0_gpio);
345 ret = gpio_request(tps->vsel1_gpio, "tps62360-vsel1");
347 dev_err(&client->dev,
348 "Err: Could not obtain vsel1 GPIO %d: %d\n",
349 tps->vsel1_gpio, ret);
352 ret = gpio_direction_output(tps->vsel1_gpio,
353 pdata->vsel1_def_state);
355 dev_err(&client->dev, "Err: Could not set direction of"
356 "vsel1 GPIO %d: %d\n", tps->vsel1_gpio, ret);
357 gpio_free(tps->vsel1_gpio);
360 tps->valid_gpios = true;
363 * Initialize the lru index with vset_reg id
364 * The index 0 will be most recently used and
365 * set with the tps->curr_vset_id */
366 for (i = 0; i < 4; ++i)
367 tps->lru_index[i] = i;
368 tps->lru_index[0] = tps->curr_vset_id;
369 tps->lru_index[tps->curr_vset_id] = 0;
372 ret = tps62360_init_dcdc(tps, pdata);
374 dev_err(tps->dev, "%s() Err: Init fails with = %d\n",
379 /* Register the regulators */
380 rdev = regulator_register(&tps->desc, &client->dev,
381 &pdata->reg_init_data, tps, NULL);
383 dev_err(tps->dev, "%s() Err: Failed to register %s\n",
393 if (gpio_is_valid(tps->vsel1_gpio))
394 gpio_free(tps->vsel1_gpio);
396 if (gpio_is_valid(tps->vsel0_gpio))
397 gpio_free(tps->vsel0_gpio);
399 regmap_exit(tps->regmap);
404 * tps62360_remove - tps62360 driver i2c remove handler
405 * @client: i2c driver client device structure
407 * Unregister TPS driver as an i2c client device driver
409 static int __devexit tps62360_remove(struct i2c_client *client)
411 struct tps62360_chip *tps = i2c_get_clientdata(client);
413 if (gpio_is_valid(tps->vsel1_gpio))
414 gpio_free(tps->vsel1_gpio);
416 if (gpio_is_valid(tps->vsel0_gpio))
417 gpio_free(tps->vsel0_gpio);
419 regulator_unregister(tps->rdev);
420 regmap_exit(tps->regmap);
424 static void tps62360_shutdown(struct i2c_client *client)
426 struct tps62360_chip *tps = i2c_get_clientdata(client);
429 if (!tps->en_discharge)
432 /* Configure the output discharge path */
433 st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
435 dev_err(tps->dev, "%s() fails in updating reg %d\n",
436 __func__, REG_RAMPCTRL);
439 static const struct i2c_device_id tps62360_id[] = {
440 {.name = "tps62360", .driver_data = TPS62360},
441 {.name = "tps62361", .driver_data = TPS62361},
445 MODULE_DEVICE_TABLE(i2c, tps62360_id);
447 static struct i2c_driver tps62360_i2c_driver = {
450 .owner = THIS_MODULE,
452 .probe = tps62360_probe,
453 .remove = __devexit_p(tps62360_remove),
454 .shutdown = tps62360_shutdown,
455 .id_table = tps62360_id,
458 static int __init tps62360_init(void)
460 return i2c_add_driver(&tps62360_i2c_driver);
462 subsys_initcall(tps62360_init);
464 static void __exit tps62360_cleanup(void)
466 i2c_del_driver(&tps62360_i2c_driver);
468 module_exit(tps62360_cleanup);
471 MODULE_DESCRIPTION("TPS62360 voltage regulator driver");
472 MODULE_LICENSE("GPL v2");