]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/regulator/tps62360-regulator.c
regulator: max8649: Use devm_* APIs
[mirror_ubuntu-artful-kernel.git] / drivers / regulator / tps62360-regulator.c
CommitLineData
6219929f
LD
1/*
2 * tps62360.c -- TI tps62360
3 *
d1cf4f65 4 * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363.
6219929f
LD
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
d1cf4f65 49enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
6219929f
LD
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{
c172708d 273 struct regulator_config config = { };
6219929f
LD
274 struct tps62360_regulator_platform_data *pdata;
275 struct regulator_dev *rdev;
276 struct tps62360_chip *tps;
277 int ret;
278 int i;
279
280 pdata = client->dev.platform_data;
281 if (!pdata) {
282 dev_err(&client->dev, "%s() Err: Platform data not found\n",
283 __func__);
284 return -EIO;
285 }
286
287 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
288 if (!tps) {
289 dev_err(&client->dev, "%s() Err: Memory allocation fails\n",
290 __func__);
291 return -ENOMEM;
292 }
293
294 tps->en_force_pwm = pdata->en_force_pwm;
295 tps->en_discharge = pdata->en_discharge;
296 tps->en_internal_pulldn = pdata->en_internal_pulldn;
297 tps->vsel0_gpio = pdata->vsel0_gpio;
298 tps->vsel1_gpio = pdata->vsel1_gpio;
6219929f 299 tps->dev = &client->dev;
d1cf4f65
AL
300
301 switch (id->driver_data) {
302 case TPS62360:
303 case TPS62362:
304 tps->voltage_base = TPS62360_BASE_VOLTAGE;
305 tps->voltage_reg_mask = 0x3F;
306 tps->desc.n_voltages = TPS62360_N_VOLTAGES;
307 break;
308 case TPS62361:
309 case TPS62363:
310 tps->voltage_base = TPS62361_BASE_VOLTAGE;
311 tps->voltage_reg_mask = 0x7F;
312 tps->desc.n_voltages = TPS62361_N_VOLTAGES;
313 break;
314 default:
315 return -ENODEV;
316 }
6219929f
LD
317
318 tps->desc.name = id->name;
319 tps->desc.id = 0;
6219929f
LD
320 tps->desc.ops = &tps62360_dcdc_ops;
321 tps->desc.type = REGULATOR_VOLTAGE;
322 tps->desc.owner = THIS_MODULE;
323 tps->regmap = regmap_init_i2c(client, &tps62360_regmap_config);
324 if (IS_ERR(tps->regmap)) {
325 ret = PTR_ERR(tps->regmap);
326 dev_err(&client->dev, "%s() Err: Failed to allocate register"
327 "map: %d\n", __func__, ret);
328 return ret;
329 }
330 i2c_set_clientdata(client, tps);
331
332 tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
333 (pdata->vsel0_def_state & 1);
334 tps->lru_index[0] = tps->curr_vset_id;
335 tps->valid_gpios = false;
336
337 if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
338 ret = gpio_request(tps->vsel0_gpio, "tps62360-vsel0");
339 if (ret) {
340 dev_err(&client->dev,
341 "Err: Could not obtain vsel0 GPIO %d: %d\n",
342 tps->vsel0_gpio, ret);
343 goto err_gpio0;
344 }
345 ret = gpio_direction_output(tps->vsel0_gpio,
346 pdata->vsel0_def_state);
347 if (ret) {
348 dev_err(&client->dev, "Err: Could not set direction of"
349 "vsel0 GPIO %d: %d\n", tps->vsel0_gpio, ret);
350 gpio_free(tps->vsel0_gpio);
351 goto err_gpio0;
352 }
353
354 ret = gpio_request(tps->vsel1_gpio, "tps62360-vsel1");
355 if (ret) {
356 dev_err(&client->dev,
357 "Err: Could not obtain vsel1 GPIO %d: %d\n",
358 tps->vsel1_gpio, ret);
359 goto err_gpio1;
360 }
361 ret = gpio_direction_output(tps->vsel1_gpio,
362 pdata->vsel1_def_state);
363 if (ret) {
364 dev_err(&client->dev, "Err: Could not set direction of"
365 "vsel1 GPIO %d: %d\n", tps->vsel1_gpio, ret);
366 gpio_free(tps->vsel1_gpio);
367 goto err_gpio1;
368 }
369 tps->valid_gpios = true;
370
371 /*
372 * Initialize the lru index with vset_reg id
373 * The index 0 will be most recently used and
374 * set with the tps->curr_vset_id */
375 for (i = 0; i < 4; ++i)
376 tps->lru_index[i] = i;
377 tps->lru_index[0] = tps->curr_vset_id;
378 tps->lru_index[tps->curr_vset_id] = 0;
379 }
380
381 ret = tps62360_init_dcdc(tps, pdata);
382 if (ret < 0) {
383 dev_err(tps->dev, "%s() Err: Init fails with = %d\n",
384 __func__, ret);
385 goto err_init;
386 }
387
c172708d
MB
388 config.dev = &client->dev;
389 config.init_data = &pdata->reg_init_data;
390 config.driver_data = tps;
391
6219929f 392 /* Register the regulators */
c172708d 393 rdev = regulator_register(&tps->desc, &config);
6219929f
LD
394 if (IS_ERR(rdev)) {
395 dev_err(tps->dev, "%s() Err: Failed to register %s\n",
396 __func__, id->name);
397 ret = PTR_ERR(rdev);
398 goto err_init;
399 }
400
401 tps->rdev = rdev;
402 return 0;
403
404err_init:
405 if (gpio_is_valid(tps->vsel1_gpio))
406 gpio_free(tps->vsel1_gpio);
407err_gpio1:
408 if (gpio_is_valid(tps->vsel0_gpio))
409 gpio_free(tps->vsel0_gpio);
410err_gpio0:
411 regmap_exit(tps->regmap);
412 return ret;
413}
414
415/**
416 * tps62360_remove - tps62360 driver i2c remove handler
417 * @client: i2c driver client device structure
418 *
419 * Unregister TPS driver as an i2c client device driver
420 */
421static int __devexit tps62360_remove(struct i2c_client *client)
422{
423 struct tps62360_chip *tps = i2c_get_clientdata(client);
424
425 if (gpio_is_valid(tps->vsel1_gpio))
426 gpio_free(tps->vsel1_gpio);
427
428 if (gpio_is_valid(tps->vsel0_gpio))
429 gpio_free(tps->vsel0_gpio);
430
431 regulator_unregister(tps->rdev);
432 regmap_exit(tps->regmap);
433 return 0;
434}
435
436static void tps62360_shutdown(struct i2c_client *client)
437{
438 struct tps62360_chip *tps = i2c_get_clientdata(client);
439 int st;
440
441 if (!tps->en_discharge)
442 return;
443
444 /* Configure the output discharge path */
445 st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
446 if (st < 0)
447 dev_err(tps->dev, "%s() fails in updating reg %d\n",
448 __func__, REG_RAMPCTRL);
449}
450
451static const struct i2c_device_id tps62360_id[] = {
452 {.name = "tps62360", .driver_data = TPS62360},
453 {.name = "tps62361", .driver_data = TPS62361},
d1cf4f65
AL
454 {.name = "tps62362", .driver_data = TPS62362},
455 {.name = "tps62363", .driver_data = TPS62363},
6219929f
LD
456 {},
457};
458
459MODULE_DEVICE_TABLE(i2c, tps62360_id);
460
461static struct i2c_driver tps62360_i2c_driver = {
462 .driver = {
463 .name = "tps62360",
464 .owner = THIS_MODULE,
465 },
466 .probe = tps62360_probe,
467 .remove = __devexit_p(tps62360_remove),
468 .shutdown = tps62360_shutdown,
469 .id_table = tps62360_id,
470};
471
472static int __init tps62360_init(void)
473{
474 return i2c_add_driver(&tps62360_i2c_driver);
475}
476subsys_initcall(tps62360_init);
477
478static void __exit tps62360_cleanup(void)
479{
480 i2c_del_driver(&tps62360_i2c_driver);
481}
482module_exit(tps62360_cleanup);
483
484MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
d1cf4f65 485MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
6219929f 486MODULE_LICENSE("GPL v2");