]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/regulator/tps62360-regulator.c
regulator: tps62360: make init_data of platform data to pointer.
[mirror_ubuntu-zesty-kernel.git] / drivers / regulator / tps62360-regulator.c
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/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>
37
38 /* Register definitions */
39 #define REG_VSET0 0
40 #define REG_VSET1 1
41 #define REG_VSET2 2
42 #define REG_VSET3 3
43 #define REG_CONTROL 4
44 #define REG_TEMP 5
45 #define REG_RAMPCTRL 6
46 #define REG_CHIPID 8
47
48 enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
49
50 #define TPS62360_BASE_VOLTAGE 770000
51 #define TPS62360_N_VOLTAGES 64
52
53 #define TPS62361_BASE_VOLTAGE 500000
54 #define TPS62361_N_VOLTAGES 128
55
56 /* tps 62360 chip information */
57 struct tps62360_chip {
58 struct device *dev;
59 struct regulator_desc desc;
60 struct regulator_dev *rdev;
61 struct regmap *regmap;
62 int chip_id;
63 int vsel0_gpio;
64 int vsel1_gpio;
65 int voltage_base;
66 u8 voltage_reg_mask;
67 bool en_internal_pulldn;
68 bool en_force_pwm;
69 bool en_discharge;
70 bool valid_gpios;
71 int lru_index[4];
72 int curr_vset_vsel[4];
73 int curr_vset_id;
74 int change_uv_per_us;
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 */
93 static 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
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];
104 found_index = i;
105 found = true;
106 goto update_lru_index;
107 }
108 }
109
110 update_lru_index:
111 for (i = found_index; i > 0; i--)
112 tps->lru_index[i] = tps->lru_index[i - 1];
113
114 tps->lru_index[0] = new_vset_reg;
115 *vset_reg_id = new_vset_reg;
116 return found;
117 }
118
119 static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
120 {
121 struct tps62360_chip *tps = rdev_get_drvdata(dev);
122 int vsel;
123 unsigned int data;
124 int ret;
125
126 ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
127 if (ret < 0) {
128 dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
129 __func__, REG_VSET0 + tps->curr_vset_id, ret);
130 return ret;
131 }
132 vsel = (int)data & tps->voltage_reg_mask;
133 return vsel;
134 }
135
136 static int tps62360_dcdc_set_voltage(struct regulator_dev *dev,
137 int min_uV, int max_uV, unsigned *selector)
138 {
139 struct tps62360_chip *tps = rdev_get_drvdata(dev);
140 int vsel;
141 int ret;
142 bool found = false;
143 int new_vset_id = tps->curr_vset_id;
144
145 if ((max_uV < min_uV) || (max_uV < tps->voltage_base))
146 return -EINVAL;
147
148 if (min_uV > (tps->voltage_base + (tps->desc.n_voltages - 1) * 10000))
149 return -EINVAL;
150
151 vsel = DIV_ROUND_UP(min_uV - tps->voltage_base, 10000);
152 if (selector)
153 *selector = (vsel & tps->voltage_reg_mask);
154
155 /*
156 * If gpios are available to select the VSET register then least
157 * recently used register for new configuration.
158 */
159 if (tps->valid_gpios)
160 found = find_voltage_set_register(tps, vsel, &new_vset_id);
161
162 if (!found) {
163 ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
164 tps->voltage_reg_mask, vsel);
165 if (ret < 0) {
166 dev_err(tps->dev,
167 "%s(): register %d update failed with err %d\n",
168 __func__, REG_VSET0 + new_vset_id, ret);
169 return ret;
170 }
171 tps->curr_vset_id = new_vset_id;
172 tps->curr_vset_vsel[new_vset_id] = vsel;
173 }
174
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);
180 }
181 return 0;
182 }
183
184 static int tps62360_dcdc_list_voltage(struct regulator_dev *dev,
185 unsigned selector)
186 {
187 struct tps62360_chip *tps = rdev_get_drvdata(dev);
188
189 if (selector >= tps->desc.n_voltages)
190 return -EINVAL;
191
192 return tps->voltage_base + selector * 10000;
193 }
194
195 static int tps62360_set_voltage_time_sel(struct regulator_dev *rdev,
196 unsigned int old_selector, unsigned int new_selector)
197 {
198 struct tps62360_chip *tps = rdev_get_drvdata(rdev);
199 int old_uV, new_uV;
200
201 old_uV = tps62360_dcdc_list_voltage(rdev, old_selector);
202 if (old_uV < 0)
203 return old_uV;
204
205 new_uV = tps62360_dcdc_list_voltage(rdev, new_selector);
206 if (new_uV < 0)
207 return new_uV;
208
209 return DIV_ROUND_UP(abs(old_uV - new_uV), tps->change_uv_per_us);
210 }
211
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,
217 };
218
219 static int __devinit tps62360_init_force_pwm(struct tps62360_chip *tps,
220 struct tps62360_regulator_platform_data *pdata,
221 int vset_id)
222 {
223 int ret;
224 int bit = 0;
225
226 if (pdata->en_force_pwm)
227 bit = BIT(7);
228
229 ret = regmap_update_bits(tps->regmap, REG_VSET0 + vset_id, BIT(7), bit);
230 if (ret < 0)
231 dev_err(tps->dev,
232 "%s(): register %d update failed with err %d\n",
233 __func__, REG_VSET0 + vset_id, ret);
234 return ret;
235 }
236
237 static int __devinit tps62360_init_dcdc(struct tps62360_chip *tps,
238 struct tps62360_regulator_platform_data *pdata)
239 {
240 int ret;
241 int i;
242 unsigned int ramp_ctrl;
243
244 /* Initialize internal pull up/down control */
245 if (tps->en_internal_pulldn)
246 ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
247 else
248 ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
249 if (ret < 0) {
250 dev_err(tps->dev,
251 "%s(): register %d write failed with err %d\n",
252 __func__, REG_CONTROL, ret);
253 return ret;
254 }
255
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);
260 if (ret < 0)
261 return ret;
262 }
263 } else {
264 ret = tps62360_init_force_pwm(tps, pdata, tps->curr_vset_id);
265 if (ret < 0)
266 return ret;
267 }
268
269 /* Reset output discharge path to reduce power consumption */
270 ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
271 if (ret < 0) {
272 dev_err(tps->dev,
273 "%s(): register %d update failed with err %d\n",
274 __func__, REG_RAMPCTRL, ret);
275 return ret;
276 }
277
278 /* Get ramp value from ramp control register */
279 ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
280 if (ret < 0) {
281 dev_err(tps->dev,
282 "%s(): register %d read failed with err %d\n",
283 __func__, REG_RAMPCTRL, ret);
284 return ret;
285 }
286 ramp_ctrl = (ramp_ctrl >> 4) & 0x7;
287
288 /* ramp mV/us = 32/(2^ramp_ctrl) */
289 tps->change_uv_per_us = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
290 return ret;
291 }
292
293 static const struct regmap_config tps62360_regmap_config = {
294 .reg_bits = 8,
295 .val_bits = 8,
296 .max_register = REG_CHIPID,
297 .cache_type = REGCACHE_RBTREE,
298 };
299
300 static int __devinit tps62360_probe(struct i2c_client *client,
301 const struct i2c_device_id *id)
302 {
303 struct regulator_config config = { };
304 struct tps62360_regulator_platform_data *pdata;
305 struct regulator_dev *rdev;
306 struct tps62360_chip *tps;
307 int ret;
308 int i;
309
310 pdata = client->dev.platform_data;
311 if (!pdata) {
312 dev_err(&client->dev, "%s(): Platform data not found\n",
313 __func__);
314 return -EIO;
315 }
316
317 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
318 if (!tps) {
319 dev_err(&client->dev, "%s(): Memory allocation failed\n",
320 __func__);
321 return -ENOMEM;
322 }
323
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;
330
331 switch (id->driver_data) {
332 case TPS62360:
333 case TPS62362:
334 tps->voltage_base = TPS62360_BASE_VOLTAGE;
335 tps->voltage_reg_mask = 0x3F;
336 tps->desc.n_voltages = TPS62360_N_VOLTAGES;
337 break;
338 case TPS62361:
339 case TPS62363:
340 tps->voltage_base = TPS62361_BASE_VOLTAGE;
341 tps->voltage_reg_mask = 0x7F;
342 tps->desc.n_voltages = TPS62361_N_VOLTAGES;
343 break;
344 default:
345 return -ENODEV;
346 }
347
348 tps->desc.name = id->name;
349 tps->desc.id = 0;
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",
358 __func__, ret);
359 return ret;
360 }
361 i2c_set_clientdata(client, tps);
362
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;
367
368 if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
369 int gpio_flags;
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");
374 if (ret) {
375 dev_err(&client->dev,
376 "%s(): Could not obtain vsel0 GPIO %d: %d\n",
377 __func__, tps->vsel0_gpio, ret);
378 goto err_gpio0;
379 }
380
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");
385 if (ret) {
386 dev_err(&client->dev,
387 "%s(): Could not obtain vsel1 GPIO %d: %d\n",
388 __func__, tps->vsel1_gpio, ret);
389 goto err_gpio1;
390 }
391 tps->valid_gpios = true;
392
393 /*
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;
401 }
402
403 ret = tps62360_init_dcdc(tps, pdata);
404 if (ret < 0) {
405 dev_err(tps->dev, "%s(): Init failed with err = %d\n",
406 __func__, ret);
407 goto err_init;
408 }
409
410 config.dev = &client->dev;
411 config.init_data = pdata->reg_init_data;
412 config.driver_data = tps;
413
414 /* Register the regulators */
415 rdev = regulator_register(&tps->desc, &config);
416 if (IS_ERR(rdev)) {
417 dev_err(tps->dev,
418 "%s(): regulator register failed with err %s\n",
419 __func__, id->name);
420 ret = PTR_ERR(rdev);
421 goto err_init;
422 }
423
424 tps->rdev = rdev;
425 return 0;
426
427 err_init:
428 if (gpio_is_valid(tps->vsel1_gpio))
429 gpio_free(tps->vsel1_gpio);
430 err_gpio1:
431 if (gpio_is_valid(tps->vsel0_gpio))
432 gpio_free(tps->vsel0_gpio);
433 err_gpio0:
434 return ret;
435 }
436
437 /**
438 * tps62360_remove - tps62360 driver i2c remove handler
439 * @client: i2c driver client device structure
440 *
441 * Unregister TPS driver as an i2c client device driver
442 */
443 static int __devexit tps62360_remove(struct i2c_client *client)
444 {
445 struct tps62360_chip *tps = i2c_get_clientdata(client);
446
447 if (gpio_is_valid(tps->vsel1_gpio))
448 gpio_free(tps->vsel1_gpio);
449
450 if (gpio_is_valid(tps->vsel0_gpio))
451 gpio_free(tps->vsel0_gpio);
452
453 regulator_unregister(tps->rdev);
454 return 0;
455 }
456
457 static void tps62360_shutdown(struct i2c_client *client)
458 {
459 struct tps62360_chip *tps = i2c_get_clientdata(client);
460 int st;
461
462 if (!tps->en_discharge)
463 return;
464
465 /* Configure the output discharge path */
466 st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
467 if (st < 0)
468 dev_err(tps->dev,
469 "%s(): register %d update failed with err %d\n",
470 __func__, REG_RAMPCTRL, st);
471 }
472
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},
478 {},
479 };
480
481 MODULE_DEVICE_TABLE(i2c, tps62360_id);
482
483 static struct i2c_driver tps62360_i2c_driver = {
484 .driver = {
485 .name = "tps62360",
486 .owner = THIS_MODULE,
487 },
488 .probe = tps62360_probe,
489 .remove = __devexit_p(tps62360_remove),
490 .shutdown = tps62360_shutdown,
491 .id_table = tps62360_id,
492 };
493
494 static int __init tps62360_init(void)
495 {
496 return i2c_add_driver(&tps62360_i2c_driver);
497 }
498 subsys_initcall(tps62360_init);
499
500 static void __exit tps62360_cleanup(void)
501 {
502 i2c_del_driver(&tps62360_i2c_driver);
503 }
504 module_exit(tps62360_cleanup);
505
506 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
507 MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
508 MODULE_LICENSE("GPL v2");