regulator: Add missing n_voltages setting for max8925
[deliverable/linux.git] / drivers / regulator / tps62360-regulator.c
CommitLineData
6219929f
LD
1/*
2 * tps62360.c -- TI tps62360
3 *
4 * Driver for processor core supply tps62360 and tps62361B
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/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>
38
39/* Register definitions */
40#define REG_VSET0 0
41#define REG_VSET1 1
42#define REG_VSET2 2
43#define REG_VSET3 3
44#define REG_CONTROL 4
45#define REG_TEMP 5
46#define REG_RAMPCTRL 6
47#define REG_CHIPID 8
48
49enum chips {TPS62360, TPS62361};
50
51#define TPS62360_BASE_VOLTAGE 770
52#define TPS62360_N_VOLTAGES 64
53
54#define TPS62361_BASE_VOLTAGE 500
55#define TPS62361_N_VOLTAGES 128
56
57/* tps 62360 chip information */
58struct tps62360_chip {
6219929f
LD
59 struct device *dev;
60 struct regulator_desc desc;
6219929f
LD
61 struct regulator_dev *rdev;
62 struct regmap *regmap;
63 int chip_id;
64 int vsel0_gpio;
65 int vsel1_gpio;
66 int voltage_base;
67 u8 voltage_reg_mask;
68 bool en_internal_pulldn;
69 bool en_force_pwm;
70 bool en_discharge;
71 bool valid_gpios;
72 int lru_index[4];
73 int curr_vset_vsel[4];
74 int curr_vset_id;
75};
76
77/*
78 * find_voltage_set_register: Find new voltage configuration register
79 * (VSET) id.
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
83 * or not.
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
87 * VSET register.
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).
92 */
93static bool find_voltage_set_register(struct tps62360_chip *tps,
94 int req_vsel, int *vset_reg_id)
95{
96 int i;
97 bool found = false;
98 int new_vset_reg = tps->lru_index[3];
99 int found_index = 3;
100 for (i = 0; i < 4; ++i) {
101 if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
102 new_vset_reg = tps->lru_index[i];
103 found_index = i;
104 found = true;
105 goto update_lru_index;
106 }
107 }
108
109update_lru_index:
110 for (i = found_index; i > 0; i--)
111 tps->lru_index[i] = tps->lru_index[i - 1];
112
113 tps->lru_index[0] = new_vset_reg;
114 *vset_reg_id = new_vset_reg;
115 return found;
116}
117
118static int tps62360_dcdc_get_voltage(struct regulator_dev *dev)
119{
120 struct tps62360_chip *tps = rdev_get_drvdata(dev);
121 int vsel;
122 unsigned int data;
123 int ret;
124
125 ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
126 if (ret < 0) {
127 dev_err(tps->dev, "%s: Error in reading register %d\n",
128 __func__, REG_VSET0 + tps->curr_vset_id);
129 return ret;
130 }
131 vsel = (int)data & tps->voltage_reg_mask;
132 return (tps->voltage_base + vsel * 10) * 1000;
133}
134
135static int tps62360_dcdc_set_voltage(struct regulator_dev *dev,
136 int min_uV, int max_uV, unsigned *selector)
137{
138 struct tps62360_chip *tps = rdev_get_drvdata(dev);
139 int vsel;
140 int ret;
141 bool found = false;
142 int new_vset_id = tps->curr_vset_id;
143
144 if (max_uV < min_uV)
145 return -EINVAL;
146
147 if (min_uV >
148 ((tps->voltage_base + (tps->desc.n_voltages - 1) * 10) * 1000))
149 return -EINVAL;
150
151 if (max_uV < tps->voltage_base * 1000)
152 return -EINVAL;
153
154 vsel = DIV_ROUND_UP(min_uV - (tps->voltage_base * 1000), 10000);
155 if (selector)
156 *selector = (vsel & tps->voltage_reg_mask);
157
158 /*
159 * If gpios are available to select the VSET register then least
160 * recently used register for new configuration.
161 */
162 if (tps->valid_gpios)
163 found = find_voltage_set_register(tps, vsel, &new_vset_id);
164
165 if (!found) {
166 ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
167 tps->voltage_reg_mask, vsel);
168 if (ret < 0) {
169 dev_err(tps->dev, "%s: Error in updating register %d\n",
170 __func__, REG_VSET0 + new_vset_id);
171 return ret;
172 }
173 tps->curr_vset_id = new_vset_id;
174 tps->curr_vset_vsel[new_vset_id] = vsel;
175 }
176
177 /* Select proper VSET register vio gpios */
178 if (tps->valid_gpios) {
179 gpio_set_value_cansleep(tps->vsel0_gpio,
180 new_vset_id & 0x1);
181 gpio_set_value_cansleep(tps->vsel1_gpio,
182 (new_vset_id >> 1) & 0x1);
183 }
184 return 0;
185}
186
187static int tps62360_dcdc_list_voltage(struct regulator_dev *dev,
188 unsigned selector)
189{
190 struct tps62360_chip *tps = rdev_get_drvdata(dev);
191
46783a04 192 if (selector >= tps->desc.n_voltages)
6219929f
LD
193 return -EINVAL;
194 return (tps->voltage_base + selector * 10) * 1000;
195}
196
197static struct regulator_ops tps62360_dcdc_ops = {
198 .get_voltage = tps62360_dcdc_get_voltage,
199 .set_voltage = tps62360_dcdc_set_voltage,
200 .list_voltage = tps62360_dcdc_list_voltage,
201};
202
203static int tps62360_init_force_pwm(struct tps62360_chip *tps,
204 struct tps62360_regulator_platform_data *pdata,
205 int vset_id)
206{
207 unsigned int data;
208 int ret;
209 ret = regmap_read(tps->regmap, REG_VSET0 + vset_id, &data);
210 if (ret < 0) {
211 dev_err(tps->dev, "%s() fails in writing reg %d\n",
212 __func__, REG_VSET0 + vset_id);
213 return ret;
214 }
215 tps->curr_vset_vsel[vset_id] = data & tps->voltage_reg_mask;
216 if (pdata->en_force_pwm)
217 data |= BIT(7);
218 else
219 data &= ~BIT(7);
220 ret = regmap_write(tps->regmap, REG_VSET0 + vset_id, data);
221 if (ret < 0)
222 dev_err(tps->dev, "%s() fails in writing reg %d\n",
223 __func__, REG_VSET0 + vset_id);
224 return ret;
225}
226
227static int tps62360_init_dcdc(struct tps62360_chip *tps,
228 struct tps62360_regulator_platform_data *pdata)
229{
230 int ret;
231 int i;
232
233 /* Initailize internal pull up/down control */
234 if (tps->en_internal_pulldn)
235 ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
236 else
237 ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
238 if (ret < 0) {
239 dev_err(tps->dev, "%s() fails in writing reg %d\n",
240 __func__, REG_CONTROL);
241 return ret;
242 }
243
244 /* Initailize force PWM mode */
245 if (tps->valid_gpios) {
246 for (i = 0; i < 4; ++i) {
247 ret = tps62360_init_force_pwm(tps, pdata, i);
248 if (ret < 0)
249 return ret;
250 }
251 } else {
252 ret = tps62360_init_force_pwm(tps, pdata, tps->curr_vset_id);
253 if (ret < 0)
254 return ret;
255 }
256
257 /* Reset output discharge path to reduce power consumption */
258 ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
259 if (ret < 0)
260 dev_err(tps->dev, "%s() fails in updating reg %d\n",
261 __func__, REG_RAMPCTRL);
262 return ret;
263}
264
265static const struct regmap_config tps62360_regmap_config = {
266 .reg_bits = 8,
267 .val_bits = 8,
268};
269
270static int __devinit tps62360_probe(struct i2c_client *client,
271 const struct i2c_device_id *id)
272{
273 struct tps62360_regulator_platform_data *pdata;
274 struct regulator_dev *rdev;
275 struct tps62360_chip *tps;
276 int ret;
277 int i;
278
279 pdata = client->dev.platform_data;
280 if (!pdata) {
281 dev_err(&client->dev, "%s() Err: Platform data not found\n",
282 __func__);
283 return -EIO;
284 }
285
286 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
287 if (!tps) {
288 dev_err(&client->dev, "%s() Err: Memory allocation fails\n",
289 __func__);
290 return -ENOMEM;
291 }
292
293 tps->en_force_pwm = pdata->en_force_pwm;
294 tps->en_discharge = pdata->en_discharge;
295 tps->en_internal_pulldn = pdata->en_internal_pulldn;
296 tps->vsel0_gpio = pdata->vsel0_gpio;
297 tps->vsel1_gpio = pdata->vsel1_gpio;
6219929f 298 tps->dev = &client->dev;
6219929f
LD
299 tps->voltage_base = (id->driver_data == TPS62360) ?
300 TPS62360_BASE_VOLTAGE : TPS62361_BASE_VOLTAGE;
301 tps->voltage_reg_mask = (id->driver_data == TPS62360) ? 0x3F : 0x7F;
302
303 tps->desc.name = id->name;
304 tps->desc.id = 0;
305 tps->desc.n_voltages = (id->driver_data == TPS62360) ?
306 TPS62360_N_VOLTAGES : TPS62361_N_VOLTAGES;
307 tps->desc.ops = &tps62360_dcdc_ops;
308 tps->desc.type = REGULATOR_VOLTAGE;
309 tps->desc.owner = THIS_MODULE;
310 tps->regmap = regmap_init_i2c(client, &tps62360_regmap_config);
311 if (IS_ERR(tps->regmap)) {
312 ret = PTR_ERR(tps->regmap);
313 dev_err(&client->dev, "%s() Err: Failed to allocate register"
314 "map: %d\n", __func__, ret);
315 return ret;
316 }
317 i2c_set_clientdata(client, tps);
318
319 tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
320 (pdata->vsel0_def_state & 1);
321 tps->lru_index[0] = tps->curr_vset_id;
322 tps->valid_gpios = false;
323
324 if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
325 ret = gpio_request(tps->vsel0_gpio, "tps62360-vsel0");
326 if (ret) {
327 dev_err(&client->dev,
328 "Err: Could not obtain vsel0 GPIO %d: %d\n",
329 tps->vsel0_gpio, ret);
330 goto err_gpio0;
331 }
332 ret = gpio_direction_output(tps->vsel0_gpio,
333 pdata->vsel0_def_state);
334 if (ret) {
335 dev_err(&client->dev, "Err: Could not set direction of"
336 "vsel0 GPIO %d: %d\n", tps->vsel0_gpio, ret);
337 gpio_free(tps->vsel0_gpio);
338 goto err_gpio0;
339 }
340
341 ret = gpio_request(tps->vsel1_gpio, "tps62360-vsel1");
342 if (ret) {
343 dev_err(&client->dev,
344 "Err: Could not obtain vsel1 GPIO %d: %d\n",
345 tps->vsel1_gpio, ret);
346 goto err_gpio1;
347 }
348 ret = gpio_direction_output(tps->vsel1_gpio,
349 pdata->vsel1_def_state);
350 if (ret) {
351 dev_err(&client->dev, "Err: Could not set direction of"
352 "vsel1 GPIO %d: %d\n", tps->vsel1_gpio, ret);
353 gpio_free(tps->vsel1_gpio);
354 goto err_gpio1;
355 }
356 tps->valid_gpios = true;
357
358 /*
359 * Initialize the lru index with vset_reg id
360 * The index 0 will be most recently used and
361 * set with the tps->curr_vset_id */
362 for (i = 0; i < 4; ++i)
363 tps->lru_index[i] = i;
364 tps->lru_index[0] = tps->curr_vset_id;
365 tps->lru_index[tps->curr_vset_id] = 0;
366 }
367
368 ret = tps62360_init_dcdc(tps, pdata);
369 if (ret < 0) {
370 dev_err(tps->dev, "%s() Err: Init fails with = %d\n",
371 __func__, ret);
372 goto err_init;
373 }
374
375 /* Register the regulators */
376 rdev = regulator_register(&tps->desc, &client->dev,
377 &pdata->reg_init_data, tps, NULL);
378 if (IS_ERR(rdev)) {
379 dev_err(tps->dev, "%s() Err: Failed to register %s\n",
380 __func__, id->name);
381 ret = PTR_ERR(rdev);
382 goto err_init;
383 }
384
385 tps->rdev = rdev;
386 return 0;
387
388err_init:
389 if (gpio_is_valid(tps->vsel1_gpio))
390 gpio_free(tps->vsel1_gpio);
391err_gpio1:
392 if (gpio_is_valid(tps->vsel0_gpio))
393 gpio_free(tps->vsel0_gpio);
394err_gpio0:
395 regmap_exit(tps->regmap);
396 return ret;
397}
398
399/**
400 * tps62360_remove - tps62360 driver i2c remove handler
401 * @client: i2c driver client device structure
402 *
403 * Unregister TPS driver as an i2c client device driver
404 */
405static int __devexit tps62360_remove(struct i2c_client *client)
406{
407 struct tps62360_chip *tps = i2c_get_clientdata(client);
408
409 if (gpio_is_valid(tps->vsel1_gpio))
410 gpio_free(tps->vsel1_gpio);
411
412 if (gpio_is_valid(tps->vsel0_gpio))
413 gpio_free(tps->vsel0_gpio);
414
415 regulator_unregister(tps->rdev);
416 regmap_exit(tps->regmap);
417 return 0;
418}
419
420static void tps62360_shutdown(struct i2c_client *client)
421{
422 struct tps62360_chip *tps = i2c_get_clientdata(client);
423 int st;
424
425 if (!tps->en_discharge)
426 return;
427
428 /* Configure the output discharge path */
429 st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
430 if (st < 0)
431 dev_err(tps->dev, "%s() fails in updating reg %d\n",
432 __func__, REG_RAMPCTRL);
433}
434
435static const struct i2c_device_id tps62360_id[] = {
436 {.name = "tps62360", .driver_data = TPS62360},
437 {.name = "tps62361", .driver_data = TPS62361},
438 {},
439};
440
441MODULE_DEVICE_TABLE(i2c, tps62360_id);
442
443static struct i2c_driver tps62360_i2c_driver = {
444 .driver = {
445 .name = "tps62360",
446 .owner = THIS_MODULE,
447 },
448 .probe = tps62360_probe,
449 .remove = __devexit_p(tps62360_remove),
450 .shutdown = tps62360_shutdown,
451 .id_table = tps62360_id,
452};
453
454static int __init tps62360_init(void)
455{
456 return i2c_add_driver(&tps62360_i2c_driver);
457}
458subsys_initcall(tps62360_init);
459
460static void __exit tps62360_cleanup(void)
461{
462 i2c_del_driver(&tps62360_i2c_driver);
463}
464module_exit(tps62360_cleanup);
465
466MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
467MODULE_DESCRIPTION("TPS62360 voltage regulator driver");
468MODULE_LICENSE("GPL v2");
This page took 0.068216 seconds and 5 git commands to generate.