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