regulator: palmas: Fix warning message
[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 bool is_volatile_reg(struct device *dev, unsigned int reg)
327 {
328         return false;
329 }
330
331 static const struct regmap_config tps62360_regmap_config = {
332         .reg_bits               = 8,
333         .val_bits               = 8,
334         .volatile_reg           = is_volatile_reg,
335         .max_register           = REG_CHIPID,
336         .cache_type             = REGCACHE_RBTREE,
337 };
338
339 static int __devinit tps62360_probe(struct i2c_client *client,
340                                      const struct i2c_device_id *id)
341 {
342         struct tps62360_regulator_platform_data *pdata;
343         struct regulator_dev *rdev;
344         struct tps62360_chip *tps;
345         int ret;
346         int i;
347
348         pdata = client->dev.platform_data;
349         if (!pdata) {
350                 dev_err(&client->dev, "%s(): Platform data not found\n",
351                                                 __func__);
352                 return -EIO;
353         }
354
355         tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
356         if (!tps) {
357                 dev_err(&client->dev, "%s(): Memory allocation failed\n",
358                                                 __func__);
359                 return -ENOMEM;
360         }
361
362         tps->en_discharge = pdata->en_discharge;
363         tps->en_internal_pulldn = pdata->en_internal_pulldn;
364         tps->vsel0_gpio = pdata->vsel0_gpio;
365         tps->vsel1_gpio = pdata->vsel1_gpio;
366         tps->dev = &client->dev;
367
368         switch (id->driver_data) {
369         case TPS62360:
370         case TPS62362:
371                 tps->voltage_base = TPS62360_BASE_VOLTAGE;
372                 tps->voltage_reg_mask = 0x3F;
373                 tps->desc.n_voltages = TPS62360_N_VOLTAGES;
374                 break;
375         case TPS62361:
376         case TPS62363:
377                 tps->voltage_base = TPS62361_BASE_VOLTAGE;
378                 tps->voltage_reg_mask = 0x7F;
379                 tps->desc.n_voltages = TPS62361_N_VOLTAGES;
380                 break;
381         default:
382                 return -ENODEV;
383         }
384
385         tps->desc.name = id->name;
386         tps->desc.id = 0;
387         tps->desc.ops = &tps62360_dcdc_ops;
388         tps->desc.type = REGULATOR_VOLTAGE;
389         tps->desc.owner = THIS_MODULE;
390         tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
391         if (IS_ERR(tps->regmap)) {
392                 ret = PTR_ERR(tps->regmap);
393                 dev_err(&client->dev,
394                         "%s(): regmap allocation failed with err %d\n",
395                         __func__, ret);
396                 return ret;
397         }
398         i2c_set_clientdata(client, tps);
399
400         tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
401                                 (pdata->vsel0_def_state & 1);
402         tps->lru_index[0] = tps->curr_vset_id;
403         tps->valid_gpios = false;
404
405         if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
406                 int gpio_flags;
407                 gpio_flags = (pdata->vsel0_def_state) ?
408                                 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
409                 ret = gpio_request_one(tps->vsel0_gpio,
410                                 gpio_flags, "tps62360-vsel0");
411                 if (ret) {
412                         dev_err(&client->dev,
413                                 "%s(): Could not obtain vsel0 GPIO %d: %d\n",
414                                 __func__, tps->vsel0_gpio, ret);
415                         goto err_gpio0;
416                 }
417
418                 gpio_flags = (pdata->vsel1_def_state) ?
419                                 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
420                 ret = gpio_request_one(tps->vsel1_gpio,
421                                 gpio_flags, "tps62360-vsel1");
422                 if (ret) {
423                         dev_err(&client->dev,
424                                 "%s(): Could not obtain vsel1 GPIO %d: %d\n",
425                                 __func__, tps->vsel1_gpio, ret);
426                         goto err_gpio1;
427                 }
428                 tps->valid_gpios = true;
429
430                 /*
431                  * Initialize the lru index with vset_reg id
432                  * The index 0 will be most recently used and
433                  * set with the tps->curr_vset_id */
434                 for (i = 0; i < 4; ++i)
435                         tps->lru_index[i] = i;
436                 tps->lru_index[0] = tps->curr_vset_id;
437                 tps->lru_index[tps->curr_vset_id] = 0;
438         }
439
440         ret = tps62360_init_dcdc(tps, pdata);
441         if (ret < 0) {
442                 dev_err(tps->dev, "%s(): Init failed with err = %d\n",
443                                 __func__, ret);
444                 goto err_init;
445         }
446
447         /* Register the regulators */
448         rdev = regulator_register(&tps->desc, &client->dev,
449                                 &pdata->reg_init_data, tps, NULL);
450         if (IS_ERR(rdev)) {
451                 dev_err(tps->dev,
452                         "%s(): regulator register failed with err %s\n",
453                         __func__, id->name);
454                 ret = PTR_ERR(rdev);
455                 goto err_init;
456         }
457
458         tps->rdev = rdev;
459         return 0;
460
461 err_init:
462         if (gpio_is_valid(tps->vsel1_gpio))
463                 gpio_free(tps->vsel1_gpio);
464 err_gpio1:
465         if (gpio_is_valid(tps->vsel0_gpio))
466                 gpio_free(tps->vsel0_gpio);
467 err_gpio0:
468         return ret;
469 }
470
471 /**
472  * tps62360_remove - tps62360 driver i2c remove handler
473  * @client: i2c driver client device structure
474  *
475  * Unregister TPS driver as an i2c client device driver
476  */
477 static int __devexit tps62360_remove(struct i2c_client *client)
478 {
479         struct tps62360_chip *tps = i2c_get_clientdata(client);
480
481         if (gpio_is_valid(tps->vsel1_gpio))
482                 gpio_free(tps->vsel1_gpio);
483
484         if (gpio_is_valid(tps->vsel0_gpio))
485                 gpio_free(tps->vsel0_gpio);
486
487         regulator_unregister(tps->rdev);
488         return 0;
489 }
490
491 static void tps62360_shutdown(struct i2c_client *client)
492 {
493         struct tps62360_chip *tps = i2c_get_clientdata(client);
494         int st;
495
496         if (!tps->en_discharge)
497                 return;
498
499         /* Configure the output discharge path */
500         st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
501         if (st < 0)
502                 dev_err(tps->dev,
503                         "%s(): register %d update failed with err %d\n",
504                         __func__, REG_RAMPCTRL, st);
505 }
506
507 static const struct i2c_device_id tps62360_id[] = {
508         {.name = "tps62360", .driver_data = TPS62360},
509         {.name = "tps62361", .driver_data = TPS62361},
510         {.name = "tps62362", .driver_data = TPS62362},
511         {.name = "tps62363", .driver_data = TPS62363},
512         {},
513 };
514
515 MODULE_DEVICE_TABLE(i2c, tps62360_id);
516
517 static struct i2c_driver tps62360_i2c_driver = {
518         .driver = {
519                 .name = "tps62360",
520                 .owner = THIS_MODULE,
521         },
522         .probe = tps62360_probe,
523         .remove = __devexit_p(tps62360_remove),
524         .shutdown = tps62360_shutdown,
525         .id_table = tps62360_id,
526 };
527
528 static int __init tps62360_init(void)
529 {
530         return i2c_add_driver(&tps62360_i2c_driver);
531 }
532 subsys_initcall(tps62360_init);
533
534 static void __exit tps62360_cleanup(void)
535 {
536         i2c_del_driver(&tps62360_i2c_driver);
537 }
538 module_exit(tps62360_cleanup);
539
540 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
541 MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
542 MODULE_LICENSE("GPL v2");