regulator: TPS6238X0: Add tps6238X0 regulator driver
[linux-2.6.git] / drivers / regulator / tps6238x0-regulator.c
1 /*
2  * tps6238x0-regulator.c -- TI tps623850/tps623860/tps623870
3  *
4  * Driver for processor core supply tps623850, tps623860 and tps623870
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/tps6238x0-regulator.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_MODE0               2
43 #define REG_MODE1               3
44 #define REG_CONTROL             4
45 #define REG_EXCEPTION           5
46 #define REG_RAMPCTRL            6
47 #define REG_IOUT                7
48 #define REG_CHIPID              8
49
50 #define TPS6238X0_BASE_VOLTAGE  500000
51 #define TPS6238X0_N_VOLTAGES    128
52 #define TPS6238X0_MAX_VSET      2
53 #define TPS6238X0_VOUT_MASK     0x7F
54
55 /* tps 6238x0 chip information */
56 struct tps6238x0_chip {
57         const char *name;
58         struct device *dev;
59         struct regulator_desc desc;
60         struct regulator_dev *rdev;
61         struct regmap *regmap;
62         int vsel_gpio;
63         int change_uv_per_us;
64         bool en_internal_pulldn;
65         bool en_discharge;
66         bool valid_gpios;
67         int lru_index[TPS6238X0_MAX_VSET];
68         int curr_vset_vsel[TPS6238X0_MAX_VSET];
69         int curr_vset_id;
70 };
71
72 /*
73  * find_voltage_set_register: Find new voltage configuration register
74  * (VSET) id.
75  * The finding of the new VSET register will be based on the LRU mechanism.
76  * Each VSET register will have different voltage configured . This
77  * Function will look if any of the VSET register have requested voltage set
78  * or not.
79  *     - If it is already there then it will make that register as most
80  *       recently used and return as found so that caller need not to set
81  *       the VSET register but need to set the proper gpios to select this
82  *       VSET register.
83  *     - If requested voltage is not found then it will use the least
84  *       recently mechanism to get new VSET register for new configuration
85  *       and will return not_found so that caller need to set new VSET
86  *       register and then gpios (both).
87  */
88 static bool find_voltage_set_register(struct tps6238x0_chip *tps,
89                 int req_vsel, int *vset_reg_id)
90 {
91         int i;
92         bool found = false;
93         int new_vset_reg = tps->lru_index[1];
94         int found_index = 1;
95         for (i = 0; i < TPS6238X0_MAX_VSET; ++i) {
96                 if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
97                         new_vset_reg = tps->lru_index[i];
98                         found_index = i;
99                         found = true;
100                         goto update_lru_index;
101                 }
102         }
103
104 update_lru_index:
105         for (i = found_index; i > 0; i--)
106                 tps->lru_index[i] = tps->lru_index[i - 1];
107
108         tps->lru_index[0] = new_vset_reg;
109         *vset_reg_id = new_vset_reg;
110         return found;
111 }
112
113 static int tps6238x0_get_voltage_sel(struct regulator_dev *dev)
114 {
115         struct tps6238x0_chip *tps = rdev_get_drvdata(dev);
116         unsigned int data;
117         int ret;
118
119         ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
120         if (ret < 0) {
121                 dev_err(tps->dev, "%s: Error in reading register %d\n",
122                         __func__, REG_VSET0 + tps->curr_vset_id);
123                 return ret;
124         }
125         return data & TPS6238X0_VOUT_MASK;
126 }
127
128 static int tps6238x0_set_voltage(struct regulator_dev *dev,
129              int min_uV, int max_uV, unsigned *selector)
130 {
131         struct tps6238x0_chip *tps = rdev_get_drvdata(dev);
132         int vsel;
133         int ret;
134         bool found = false;
135         int new_vset_id = tps->curr_vset_id;
136
137         if (min_uV >
138                 ((TPS6238X0_BASE_VOLTAGE + (TPS6238X0_N_VOLTAGES - 1) * 10000)))
139                 return -EINVAL;
140
141         if ((max_uV < min_uV) || (max_uV < TPS6238X0_BASE_VOLTAGE))
142                 return -EINVAL;
143
144         vsel = DIV_ROUND_UP(min_uV - TPS6238X0_BASE_VOLTAGE, 10000);
145         if (selector)
146                 *selector = (vsel & TPS6238X0_VOUT_MASK);
147
148         /*
149          * If gpios are available to select the VSET register then least
150          * recently used register for new configuration.
151          */
152         if (tps->valid_gpios)
153                 found = find_voltage_set_register(tps, vsel, &new_vset_id);
154
155         if (!found) {
156                 ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
157                                 TPS6238X0_VOUT_MASK, vsel);
158                 if (ret < 0) {
159                         dev_err(tps->dev, "%s: Error in updating register %d\n",
160                                  __func__, REG_VSET0 + new_vset_id);
161                         return ret;
162                 }
163                 tps->curr_vset_id = new_vset_id;
164                 tps->curr_vset_vsel[new_vset_id] = vsel;
165         }
166
167         /* Select proper VSET register vio gpios */
168         if (tps->valid_gpios)
169                 gpio_set_value_cansleep(tps->vsel_gpio, new_vset_id & 0x1);
170         return 0;
171 }
172
173 static int tps6238x0_list_voltage(struct regulator_dev *dev,
174                         unsigned selector)
175 {
176         struct tps6238x0_chip *tps = rdev_get_drvdata(dev);
177
178         if ((selector < 0) || (selector >= tps->desc.n_voltages))
179                 return -EINVAL;
180
181         return TPS6238X0_BASE_VOLTAGE + selector * 10000;
182 }
183
184 static int tps6238x0_regulator_enable_time(struct regulator_dev *rdev)
185 {
186         return 300;
187 }
188
189 static int tps6238x0_set_voltage_time_sel(struct regulator_dev *rdev,
190                 unsigned int old_selector, unsigned int new_selector)
191 {
192         struct tps6238x0_chip *tps = rdev_get_drvdata(rdev);
193         int old_uV, new_uV;
194         old_uV = tps6238x0_list_voltage(rdev, old_selector);
195
196         if (old_uV < 0)
197                 return old_uV;
198
199         new_uV = tps6238x0_list_voltage(rdev, new_selector);
200         if (new_uV < 0)
201                 return new_uV;
202
203         return DIV_ROUND_UP(abs(old_uV - new_uV),
204                                 tps->change_uv_per_us);
205 }
206
207 static int tps6238x0_is_enable(struct regulator_dev *rdev)
208 {
209         struct tps6238x0_chip *tps = rdev_get_drvdata(rdev);
210         unsigned int data;
211         int ret;
212
213         ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
214         if (ret < 0) {
215                 dev_err(tps->dev, "%s: Error in reading register %d\n",
216                         __func__, REG_VSET0 + tps->curr_vset_id);
217                 return ret;
218         }
219         return !!(data & BIT(7));
220 }
221
222 static int tps6238x0_enable(struct regulator_dev *rdev)
223 {
224         struct tps6238x0_chip *tps = rdev_get_drvdata(rdev);
225         int ret;
226         int i;
227
228         /* Enable required VSET configuration */
229         for (i = 0; i < TPS6238X0_MAX_VSET; ++i) {
230                 unsigned int en = 0;
231                 if (tps->valid_gpios || (i == tps->curr_vset_id))
232                         en = BIT(7);
233
234                 ret = regmap_update_bits(tps->regmap, REG_VSET0 + i,
235                                 BIT(7), en);
236                 if (ret < 0) {
237                         dev_err(tps->dev, "%s() fails in updating reg %d\n",
238                                         __func__, REG_VSET0 + i);
239                         return ret;
240                 }
241         }
242         return ret;
243 }
244
245 static int tps6238x0_disable(struct regulator_dev *rdev)
246 {
247         struct tps6238x0_chip *tps = rdev_get_drvdata(rdev);
248         int ret;
249         int i;
250
251         /* Disable required VSET configuration */
252         for (i = 0; i < TPS6238X0_MAX_VSET; ++i) {
253                 ret = regmap_update_bits(tps->regmap, REG_VSET0 + i,
254                                 BIT(7), 0);
255                 if (ret < 0) {
256                         dev_err(tps->dev, "%s() fails in updating reg %d\n",
257                                         __func__, REG_VSET0 + i);
258                         return ret;
259                 }
260         }
261         return ret;
262 }
263
264 static struct regulator_ops tps6238x0_ops = {
265         .is_enabled             = tps6238x0_is_enable,
266         .enable                 = tps6238x0_enable,
267         .disable                = tps6238x0_disable,
268         .enable_time            = tps6238x0_regulator_enable_time,
269         .set_voltage_time_sel   = tps6238x0_set_voltage_time_sel,
270         .get_voltage_sel        = tps6238x0_get_voltage_sel,
271         .set_voltage            = tps6238x0_set_voltage,
272         .list_voltage           = tps6238x0_list_voltage,
273 };
274
275 static int __devinit tps6238x0_configure(struct tps6238x0_chip *tps,
276                 struct tps6238x0_regulator_platform_data *pdata)
277 {
278         int ret;
279         int i;
280
281         /* Initailize internal pull up/down control */
282         if (tps->en_internal_pulldn)
283                 ret = regmap_write(tps->regmap, REG_CONTROL, 0xC0);
284         else
285                 ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
286         if (ret < 0) {
287                 dev_err(tps->dev, "%s() fails in writing reg %d\n",
288                         __func__, REG_CONTROL);
289                 return ret;
290         }
291
292         /* Enable required VSET configuration */
293         for (i = 0; i < TPS6238X0_MAX_VSET; ++i) {
294                 unsigned int en = 0;
295                 if (tps->valid_gpios || (i == tps->curr_vset_id))
296                         en = BIT(7);
297
298                 ret = regmap_update_bits(tps->regmap, REG_VSET0 + i,
299                                 BIT(7), en);
300                 if (ret < 0) {
301                         dev_err(tps->dev, "%s() fails in updating reg %d\n",
302                                         __func__, REG_VSET0 + i);
303                         return ret;
304                 }
305         }
306
307         /* Reset output discharge path to reduce power consumption */
308         ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
309         if (ret < 0)
310                 dev_err(tps->dev, "%s() fails in updating reg %d\n",
311                         __func__, REG_RAMPCTRL);
312         tps->change_uv_per_us = 312;
313         return ret;
314 }
315
316 static const struct regmap_config tps6238x0_regmap_config = {
317         .reg_bits = 8,
318         .val_bits = 8,
319         .max_register = REG_CHIPID,
320         .num_reg_defaults_raw = REG_CHIPID + 1,
321         .cache_type = REGCACHE_RBTREE,
322 };
323
324 static int __devinit tps6238x0_probe(struct i2c_client *client,
325                                      const struct i2c_device_id *id)
326 {
327         struct tps6238x0_regulator_platform_data *pdata;
328         struct regulator_dev *rdev;
329         struct tps6238x0_chip *tps;
330         int ret;
331         int i;
332
333         pdata = client->dev.platform_data;
334         if (!pdata) {
335                 dev_err(&client->dev, "%s() Err: Platform data not found\n",
336                                                 __func__);
337                 return -EIO;
338         }
339
340         tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
341         if (!tps) {
342                 dev_err(&client->dev, "%s() Err: Memory allocation fails\n",
343                                                 __func__);
344                 return -ENOMEM;
345         }
346
347         tps->en_discharge = pdata->en_discharge;
348         tps->en_internal_pulldn = pdata->en_internal_pulldn;
349         tps->vsel_gpio = pdata->vsel_gpio;
350         tps->dev = &client->dev;
351
352         tps->desc.name = id->name;
353         tps->desc.id = 0;
354         tps->desc.n_voltages = TPS6238X0_N_VOLTAGES;
355         tps->desc.ops = &tps6238x0_ops;
356         tps->desc.type = REGULATOR_VOLTAGE;
357         tps->desc.owner = THIS_MODULE;
358         tps->regmap = devm_regmap_init_i2c(client, &tps6238x0_regmap_config);
359         if (IS_ERR(tps->regmap)) {
360                 ret = PTR_ERR(tps->regmap);
361                 dev_err(&client->dev, "%s() Err: Failed to allocate register"
362                         "map: %d\n", __func__, ret);
363                 return ret;
364         }
365         i2c_set_clientdata(client, tps);
366
367         tps->curr_vset_id = (pdata->vsel_def_state & 1);
368         tps->lru_index[0] = tps->curr_vset_id;
369         tps->valid_gpios = false;
370
371         if (gpio_is_valid(tps->vsel_gpio)) {
372                 int gpio_flag;
373                 gpio_flag = (tps->curr_vset_id) ?
374                                 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
375                 ret = gpio_request_one(tps->vsel_gpio,
376                                         gpio_flag, "tps6238x0-vsel0");
377                 if (ret) {
378                         dev_err(&client->dev,
379                                 "Err: Could not obtain vsel GPIO %d: %d\n",
380                                                 tps->vsel_gpio, ret);
381                         return ret;
382                 }
383                 tps->valid_gpios = true;
384
385                 /*
386                  * Initialize the lru index with vset_reg id
387                  * The index 0 will be most recently used and
388                  * set with the tps->curr_vset_id */
389                 for (i = 0; i < TPS6238X0_MAX_VSET; ++i)
390                         tps->lru_index[i] = i;
391                 tps->lru_index[0] = tps->curr_vset_id;
392                 tps->lru_index[tps->curr_vset_id] = 0;
393         }
394
395         ret = tps6238x0_configure(tps, pdata);
396         if (ret < 0) {
397                 dev_err(tps->dev, "%s() Err: Init fails with = %d\n",
398                                 __func__, ret);
399                 goto err_init;
400         }
401
402         /* Register the regulators */
403         rdev = regulator_register(&tps->desc, &client->dev,
404                                 pdata->init_data, tps);
405         if (IS_ERR(rdev)) {
406                 dev_err(tps->dev, "%s() Err: Failed to register %s\n",
407                                 __func__, id->name);
408                 ret = PTR_ERR(rdev);
409                 goto err_init;
410         }
411
412         tps->rdev = rdev;
413         return 0;
414
415 err_init:
416         if (gpio_is_valid(tps->vsel_gpio))
417                 gpio_free(tps->vsel_gpio);
418
419         return ret;
420 }
421
422 /**
423  * tps6238x0_remove - tps62360 driver i2c remove handler
424  * @client: i2c driver client device structure
425  *
426  * Unregister TPS driver as an i2c client device driver
427  */
428 static int __devexit tps6238x0_remove(struct i2c_client *client)
429 {
430         struct tps6238x0_chip *tps = i2c_get_clientdata(client);
431
432         if (gpio_is_valid(tps->vsel_gpio))
433                 gpio_free(tps->vsel_gpio);
434
435         regulator_unregister(tps->rdev);
436         return 0;
437 }
438
439 static void tps6238x0_shutdown(struct i2c_client *client)
440 {
441         struct tps6238x0_chip *tps = i2c_get_clientdata(client);
442         int st;
443
444         if (!tps->en_discharge)
445                 return;
446
447         /* Configure the output discharge path */
448         st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
449         if (st < 0)
450                 dev_err(tps->dev, "%s() fails in updating reg %d\n",
451                         __func__, REG_RAMPCTRL);
452 }
453
454 static const struct i2c_device_id tps6238x0_id[] = {
455         {.name = "tps623850", },
456         {.name = "tps623860", },
457         {.name = "tps623870", },
458         {},
459 };
460
461 MODULE_DEVICE_TABLE(i2c, tps6238x0_id);
462
463 static struct i2c_driver tps6238x0_i2c_driver = {
464         .driver = {
465                 .name = "tps6238x0",
466                 .owner = THIS_MODULE,
467         },
468         .probe = tps6238x0_probe,
469         .remove = __devexit_p(tps6238x0_remove),
470         .shutdown = tps6238x0_shutdown,
471         .id_table = tps6238x0_id,
472 };
473
474 static int __init tps6238x0_init(void)
475 {
476         return i2c_add_driver(&tps6238x0_i2c_driver);
477 }
478 subsys_initcall(tps6238x0_init);
479
480 static void __exit tps6238x0_cleanup(void)
481 {
482         i2c_del_driver(&tps6238x0_i2c_driver);
483 }
484 module_exit(tps6238x0_cleanup);
485
486 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
487 MODULE_DESCRIPTION("TPS623850/TPS623860/TPS623870 voltage regulator driver");
488 MODULE_LICENSE("GPL v2");