Revert "Merge commit 'main-jb-2012.08.03-B4' into t114-0806"
[linux-2.6.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/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
49 #define FORCE_PWM_ENABLE        BIT(7)
50
51 enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
52
53 #define TPS62360_BASE_VOLTAGE   770000
54 #define TPS62360_N_VOLTAGES     64
55
56 #define TPS62361_BASE_VOLTAGE   500000
57 #define TPS62361_N_VOLTAGES     128
58
59 /* tps 62360 chip information */
60 struct tps62360_chip {
61         struct device *dev;
62         struct regulator_desc desc;
63         struct regulator_dev *rdev;
64         struct regmap *regmap;
65         int chip_id;
66         int vsel0_gpio;
67         int vsel1_gpio;
68         int voltage_base;
69         u8 voltage_reg_mask;
70         bool en_internal_pulldn;
71         bool en_discharge;
72         bool valid_gpios;
73         int lru_index[4];
74         int curr_vset_vsel[4];
75         int curr_vset_id;
76         int change_uv_per_us;
77 };
78
79 /*
80  * find_voltage_set_register: Find new voltage configuration register
81  * (VSET) id.
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
85  * or not.
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
89  *       VSET register.
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).
94  */
95 static bool find_voltage_set_register(struct tps62360_chip *tps,
96                 int req_vsel, int *vset_reg_id)
97 {
98         int i;
99         bool found = false;
100         int new_vset_reg = tps->lru_index[3];
101         int found_index = 3;
102
103         for (i = 0; i < 4; ++i) {
104                 if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
105                         new_vset_reg = tps->lru_index[i];
106                         found_index = i;
107                         found = true;
108                         goto update_lru_index;
109                 }
110         }
111
112 update_lru_index:
113         for (i = found_index; i > 0; i--)
114                 tps->lru_index[i] = tps->lru_index[i - 1];
115
116         tps->lru_index[0] = new_vset_reg;
117         *vset_reg_id = new_vset_reg;
118         return found;
119 }
120
121 static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
122 {
123         struct tps62360_chip *tps = rdev_get_drvdata(dev);
124         int vsel;
125         unsigned int data;
126         int ret;
127
128         ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
129         if (ret < 0) {
130                 dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
131                         __func__, REG_VSET0 + tps->curr_vset_id, ret);
132                 return ret;
133         }
134         vsel = (int)data & tps->voltage_reg_mask;
135         return vsel;
136 }
137
138 static int tps62360_dcdc_set_voltage(struct regulator_dev *dev,
139              int min_uV, int max_uV, unsigned *selector)
140 {
141         struct tps62360_chip *tps = rdev_get_drvdata(dev);
142         int vsel;
143         int ret;
144         bool found = false;
145         int new_vset_id = tps->curr_vset_id;
146
147         if ((max_uV < min_uV) || (max_uV < tps->voltage_base))
148                 return -EINVAL;
149
150         if (min_uV > (tps->voltage_base + (tps->desc.n_voltages - 1) * 10000))
151                 return -EINVAL;
152
153         vsel = DIV_ROUND_UP(min_uV - tps->voltage_base, 10000);
154         if (selector)
155                 *selector = (vsel & tps->voltage_reg_mask);
156
157         /*
158          * If gpios are available to select the VSET register then least
159          * recently used register for new configuration.
160          */
161         if (tps->valid_gpios)
162                 found = find_voltage_set_register(tps, vsel, &new_vset_id);
163
164         if (!found) {
165                 ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
166                                 tps->voltage_reg_mask, vsel);
167                 if (ret < 0) {
168                         dev_err(tps->dev,
169                                 "%s(): register %d update failed with err %d\n",
170                                  __func__, REG_VSET0 + new_vset_id, ret);
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, new_vset_id & 0x1);
180                 gpio_set_value_cansleep(tps->vsel1_gpio,
181                                         (new_vset_id >> 1) & 0x1);
182         }
183         return 0;
184 }
185
186 static int tps62360_dcdc_list_voltage(struct regulator_dev *dev,
187                                         unsigned selector)
188 {
189         struct tps62360_chip *tps = rdev_get_drvdata(dev);
190
191         if (selector >= tps->desc.n_voltages)
192                 return -EINVAL;
193
194         return tps->voltage_base + selector * 10000;
195 }
196
197 static int tps62360_set_voltage_time_sel(struct regulator_dev *rdev,
198                 unsigned int old_selector, unsigned int new_selector)
199 {
200         struct tps62360_chip *tps = rdev_get_drvdata(rdev);
201         int old_uV, new_uV;
202
203         old_uV = tps62360_dcdc_list_voltage(rdev, old_selector);
204         if (old_uV < 0)
205                 return old_uV;
206
207         new_uV = tps62360_dcdc_list_voltage(rdev, new_selector);
208         if (new_uV < 0)
209                 return new_uV;
210
211         return DIV_ROUND_UP(abs(old_uV - new_uV), tps->change_uv_per_us);
212 }
213
214 static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode)
215 {
216         struct tps62360_chip *tps = rdev_get_drvdata(rdev);
217         int i;
218         int val;
219         int ret;
220
221         /* Enable force PWM mode in FAST mode only. */
222         switch (mode) {
223         case REGULATOR_MODE_FAST:
224                 val = FORCE_PWM_ENABLE;
225                 break;
226
227         case REGULATOR_MODE_NORMAL:
228                 val = 0;
229                 break;
230
231         default:
232                 return -EINVAL;
233         }
234
235         if (!tps->valid_gpios) {
236                 ret = regmap_update_bits(tps->regmap,
237                         REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val);
238                 if (ret < 0)
239                         dev_err(tps->dev,
240                                 "%s(): register %d update failed with err %d\n",
241                                 __func__, REG_VSET0 + tps->curr_vset_id, ret);
242                 return ret;
243         }
244
245         /* If gpios are valid then all register set need to be control */
246         for (i = 0; i < 4; ++i) {
247                 ret = regmap_update_bits(tps->regmap,
248                                         REG_VSET0 + i, FORCE_PWM_ENABLE, val);
249                 if (ret < 0) {
250                         dev_err(tps->dev,
251                                 "%s(): register %d update failed with err %d\n",
252                                 __func__, REG_VSET0 + i, ret);
253                         return ret;
254                 }
255         }
256         return ret;
257 }
258
259 static unsigned int tps62360_get_mode(struct regulator_dev *rdev)
260 {
261         struct tps62360_chip *tps = rdev_get_drvdata(rdev);
262         unsigned int data;
263         int ret;
264
265         ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
266         if (ret < 0) {
267                 dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
268                         __func__, REG_VSET0 + tps->curr_vset_id, ret);
269                 return ret;
270         }
271         return (data & FORCE_PWM_ENABLE) ?
272                                 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
273 }
274
275 static struct regulator_ops tps62360_dcdc_ops = {
276         .get_voltage_sel        = tps62360_dcdc_get_voltage_sel,
277         .set_voltage            = tps62360_dcdc_set_voltage,
278         .list_voltage           = tps62360_dcdc_list_voltage,
279         .set_voltage_time_sel   = tps62360_set_voltage_time_sel,
280         .set_mode               = tps62360_set_mode,
281         .get_mode               = tps62360_get_mode,
282 };
283
284 static int __devinit tps62360_init_dcdc(struct tps62360_chip *tps,
285                 struct tps62360_regulator_platform_data *pdata)
286 {
287         int ret;
288         unsigned int ramp_ctrl;
289
290         /* Initialize internal pull up/down control */
291         if (tps->en_internal_pulldn)
292                 ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
293         else
294                 ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
295         if (ret < 0) {
296                 dev_err(tps->dev,
297                         "%s(): register %d write failed with err %d\n",
298                         __func__, REG_CONTROL, ret);
299                 return ret;
300         }
301
302         /* Reset output discharge path to reduce power consumption */
303         ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
304         if (ret < 0) {
305                 dev_err(tps->dev,
306                         "%s(): register %d update failed with err %d\n",
307                         __func__, REG_RAMPCTRL, ret);
308                 return ret;
309         }
310
311         /* Get ramp value from ramp control register */
312         ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
313         if (ret < 0) {
314                 dev_err(tps->dev,
315                         "%s(): register %d read failed with err %d\n",
316                         __func__, REG_RAMPCTRL, ret);
317                 return ret;
318         }
319         ramp_ctrl = (ramp_ctrl >> 4) & 0x7;
320
321         /* ramp mV/us = 32/(2^ramp_ctrl) */
322         tps->change_uv_per_us = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
323         return ret;
324 }
325
326 static const struct regmap_config tps62360_regmap_config = {
327         .reg_bits               = 8,
328         .val_bits               = 8,
329         .max_register           = REG_CHIPID,
330         .cache_type             = REGCACHE_RBTREE,
331 };
332
333 static int __devinit tps62360_probe(struct i2c_client *client,
334                                      const struct i2c_device_id *id)
335 {
336         struct tps62360_regulator_platform_data *pdata;
337         struct regulator_dev *rdev;
338         struct tps62360_chip *tps;
339         int ret;
340         int i;
341
342         pdata = client->dev.platform_data;
343         if (!pdata) {
344                 dev_err(&client->dev, "%s(): Platform data not found\n",
345                                                 __func__);
346                 return -EIO;
347         }
348
349         tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
350         if (!tps) {
351                 dev_err(&client->dev, "%s(): Memory allocation failed\n",
352                                                 __func__);
353                 return -ENOMEM;
354         }
355
356         tps->en_discharge = pdata->en_discharge;
357         tps->en_internal_pulldn = pdata->en_internal_pulldn;
358         tps->vsel0_gpio = pdata->vsel0_gpio;
359         tps->vsel1_gpio = pdata->vsel1_gpio;
360         tps->dev = &client->dev;
361
362         switch (id->driver_data) {
363         case TPS62360:
364         case TPS62362:
365                 tps->voltage_base = TPS62360_BASE_VOLTAGE;
366                 tps->voltage_reg_mask = 0x3F;
367                 tps->desc.n_voltages = TPS62360_N_VOLTAGES;
368                 break;
369         case TPS62361:
370         case TPS62363:
371                 tps->voltage_base = TPS62361_BASE_VOLTAGE;
372                 tps->voltage_reg_mask = 0x7F;
373                 tps->desc.n_voltages = TPS62361_N_VOLTAGES;
374                 break;
375         default:
376                 return -ENODEV;
377         }
378
379         tps->desc.name = id->name;
380         tps->desc.id = 0;
381         tps->desc.ops = &tps62360_dcdc_ops;
382         tps->desc.type = REGULATOR_VOLTAGE;
383         tps->desc.owner = THIS_MODULE;
384         tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
385         if (IS_ERR(tps->regmap)) {
386                 ret = PTR_ERR(tps->regmap);
387                 dev_err(&client->dev,
388                         "%s(): regmap allocation failed with err %d\n",
389                         __func__, ret);
390                 return ret;
391         }
392         i2c_set_clientdata(client, tps);
393
394         tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
395                                 (pdata->vsel0_def_state & 1);
396         tps->lru_index[0] = tps->curr_vset_id;
397         tps->valid_gpios = false;
398
399         if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
400                 int gpio_flags;
401                 gpio_flags = (pdata->vsel0_def_state) ?
402                                 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
403                 ret = gpio_request_one(tps->vsel0_gpio,
404                                 gpio_flags, "tps62360-vsel0");
405                 if (ret) {
406                         dev_err(&client->dev,
407                                 "%s(): Could not obtain vsel0 GPIO %d: %d\n",
408                                 __func__, tps->vsel0_gpio, ret);
409                         goto err_gpio0;
410                 }
411
412                 gpio_flags = (pdata->vsel1_def_state) ?
413                                 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
414                 ret = gpio_request_one(tps->vsel1_gpio,
415                                 gpio_flags, "tps62360-vsel1");
416                 if (ret) {
417                         dev_err(&client->dev,
418                                 "%s(): Could not obtain vsel1 GPIO %d: %d\n",
419                                 __func__, tps->vsel1_gpio, ret);
420                         goto err_gpio1;
421                 }
422                 tps->valid_gpios = true;
423
424                 /*
425                  * Initialize the lru index with vset_reg id
426                  * The index 0 will be most recently used and
427                  * set with the tps->curr_vset_id */
428                 for (i = 0; i < 4; ++i)
429                         tps->lru_index[i] = i;
430                 tps->lru_index[0] = tps->curr_vset_id;
431                 tps->lru_index[tps->curr_vset_id] = 0;
432         }
433
434         ret = tps62360_init_dcdc(tps, pdata);
435         if (ret < 0) {
436                 dev_err(tps->dev, "%s(): Init failed with err = %d\n",
437                                 __func__, ret);
438                 goto err_init;
439         }
440
441         /* Register the regulators */
442         rdev = regulator_register(&tps->desc, &client->dev,
443                                 &pdata->reg_init_data, tps, NULL);
444         if (IS_ERR(rdev)) {
445                 dev_err(tps->dev,
446                         "%s(): regulator register failed with err %s\n",
447                         __func__, id->name);
448                 ret = PTR_ERR(rdev);
449                 goto err_init;
450         }
451
452         tps->rdev = rdev;
453         return 0;
454
455 err_init:
456         if (gpio_is_valid(tps->vsel1_gpio))
457                 gpio_free(tps->vsel1_gpio);
458 err_gpio1:
459         if (gpio_is_valid(tps->vsel0_gpio))
460                 gpio_free(tps->vsel0_gpio);
461 err_gpio0:
462         return ret;
463 }
464
465 /**
466  * tps62360_remove - tps62360 driver i2c remove handler
467  * @client: i2c driver client device structure
468  *
469  * Unregister TPS driver as an i2c client device driver
470  */
471 static int __devexit tps62360_remove(struct i2c_client *client)
472 {
473         struct tps62360_chip *tps = i2c_get_clientdata(client);
474
475         if (gpio_is_valid(tps->vsel1_gpio))
476                 gpio_free(tps->vsel1_gpio);
477
478         if (gpio_is_valid(tps->vsel0_gpio))
479                 gpio_free(tps->vsel0_gpio);
480
481         regulator_unregister(tps->rdev);
482         return 0;
483 }
484
485 static void tps62360_shutdown(struct i2c_client *client)
486 {
487         struct tps62360_chip *tps = i2c_get_clientdata(client);
488         int st;
489
490         if (!tps->en_discharge)
491                 return;
492
493         /* Configure the output discharge path */
494         st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
495         if (st < 0)
496                 dev_err(tps->dev,
497                         "%s(): register %d update failed with err %d\n",
498                         __func__, REG_RAMPCTRL, st);
499 }
500
501 static const struct i2c_device_id tps62360_id[] = {
502         {.name = "tps62360", .driver_data = TPS62360},
503         {.name = "tps62361", .driver_data = TPS62361},
504         {.name = "tps62362", .driver_data = TPS62362},
505         {.name = "tps62363", .driver_data = TPS62363},
506         {},
507 };
508
509 MODULE_DEVICE_TABLE(i2c, tps62360_id);
510
511 static struct i2c_driver tps62360_i2c_driver = {
512         .driver = {
513                 .name = "tps62360",
514                 .owner = THIS_MODULE,
515         },
516         .probe = tps62360_probe,
517         .remove = __devexit_p(tps62360_remove),
518         .shutdown = tps62360_shutdown,
519         .id_table = tps62360_id,
520 };
521
522 static int __init tps62360_init(void)
523 {
524         return i2c_add_driver(&tps62360_i2c_driver);
525 }
526 subsys_initcall(tps62360_init);
527
528 static void __exit tps62360_cleanup(void)
529 {
530         i2c_del_driver(&tps62360_i2c_driver);
531 }
532 module_exit(tps62360_cleanup);
533
534 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
535 MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
536 MODULE_LICENSE("GPL v2");