Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / regulator / tps62360-regulator.c
1 /*
2  * tps62360.c -- TI tps62360
3  *
4  * Driver for processor core supply tps62360 and tps62361B
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 enum chips {TPS62360, TPS62361};
50
51 #define TPS62360_BASE_VOLTAGE   770
52 #define TPS62360_N_VOLTAGES     64
53
54 #define TPS62361_BASE_VOLTAGE   500
55 #define TPS62361_N_VOLTAGES     128
56
57 /* tps 62360 chip information */
58 struct tps62360_chip {
59         const char *name;
60         struct device *dev;
61         struct regulator_desc desc;
62         struct i2c_client *client;
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_force_pwm;
72         bool en_discharge;
73         bool valid_gpios;
74         int lru_index[4];
75         int curr_vset_vsel[4];
76         int curr_vset_id;
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         for (i = 0; i < 4; ++i) {
103                 if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
104                         new_vset_reg = tps->lru_index[i];
105                         found_index = i;
106                         found = true;
107                         goto update_lru_index;
108                 }
109         }
110
111 update_lru_index:
112         for (i = found_index; i > 0; i--)
113                 tps->lru_index[i] = tps->lru_index[i - 1];
114
115         tps->lru_index[0] = new_vset_reg;
116         *vset_reg_id = new_vset_reg;
117         return found;
118 }
119
120 static int tps62360_dcdc_get_voltage(struct regulator_dev *dev)
121 {
122         struct tps62360_chip *tps = rdev_get_drvdata(dev);
123         int vsel;
124         unsigned int data;
125         int ret;
126
127         ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
128         if (ret < 0) {
129                 dev_err(tps->dev, "%s: Error in reading register %d\n",
130                         __func__, REG_VSET0 + tps->curr_vset_id);
131                 return ret;
132         }
133         vsel = (int)data & tps->voltage_reg_mask;
134         return (tps->voltage_base + vsel * 10) * 1000;
135 }
136
137 static int tps62360_dcdc_set_voltage(struct regulator_dev *dev,
138              int min_uV, int max_uV, unsigned *selector)
139 {
140         struct tps62360_chip *tps = rdev_get_drvdata(dev);
141         int vsel;
142         int ret;
143         bool found = false;
144         int new_vset_id = tps->curr_vset_id;
145
146         if (max_uV < min_uV)
147                 return -EINVAL;
148
149         if (min_uV >
150                 ((tps->voltage_base + (tps->desc.n_voltages - 1) * 10) * 1000))
151                 return -EINVAL;
152
153         if (max_uV < tps->voltage_base * 1000)
154                 return -EINVAL;
155
156         vsel = DIV_ROUND_UP(min_uV - (tps->voltage_base * 1000), 10000);
157         if (selector)
158                 *selector = (vsel & tps->voltage_reg_mask);
159
160         /*
161          * If gpios are available to select the VSET register then least
162          * recently used register for new configuration.
163          */
164         if (tps->valid_gpios)
165                 found = find_voltage_set_register(tps, vsel, &new_vset_id);
166
167         if (!found) {
168                 ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
169                                 tps->voltage_reg_mask, vsel);
170                 if (ret < 0) {
171                         dev_err(tps->dev, "%s: Error in updating register %d\n",
172                                  __func__, REG_VSET0 + new_vset_id);
173                         return ret;
174                 }
175                 tps->curr_vset_id = new_vset_id;
176                 tps->curr_vset_vsel[new_vset_id] = vsel;
177         }
178
179         /* Select proper VSET register vio gpios */
180         if (tps->valid_gpios) {
181                 gpio_set_value_cansleep(tps->vsel0_gpio,
182                                         new_vset_id & 0x1);
183                 gpio_set_value_cansleep(tps->vsel1_gpio,
184                                         (new_vset_id >> 1) & 0x1);
185         }
186         return 0;
187 }
188
189 static int tps62360_dcdc_list_voltage(struct regulator_dev *dev,
190                                         unsigned selector)
191 {
192         struct tps62360_chip *tps = rdev_get_drvdata(dev);
193
194         if (selector >= tps->desc.n_voltages)
195                 return -EINVAL;
196         return (tps->voltage_base + selector * 10) * 1000;
197 }
198
199 static struct regulator_ops tps62360_dcdc_ops = {
200         .get_voltage = tps62360_dcdc_get_voltage,
201         .set_voltage = tps62360_dcdc_set_voltage,
202         .list_voltage = tps62360_dcdc_list_voltage,
203 };
204
205 static int tps62360_init_force_pwm(struct tps62360_chip *tps,
206         struct tps62360_regulator_platform_data *pdata,
207         int vset_id)
208 {
209         unsigned int data;
210         int ret;
211         ret = regmap_read(tps->regmap, REG_VSET0 + vset_id, &data);
212         if (ret < 0) {
213                 dev_err(tps->dev, "%s() fails in writing reg %d\n",
214                         __func__, REG_VSET0 + vset_id);
215                 return ret;
216         }
217         tps->curr_vset_vsel[vset_id] = data & tps->voltage_reg_mask;
218         if (pdata->en_force_pwm)
219                 data |= BIT(7);
220         else
221                 data &= ~BIT(7);
222         ret = regmap_write(tps->regmap, REG_VSET0 + vset_id, data);
223         if (ret < 0)
224                 dev_err(tps->dev, "%s() fails in writing reg %d\n",
225                                 __func__, REG_VSET0 + vset_id);
226         return ret;
227 }
228
229 static int tps62360_init_dcdc(struct tps62360_chip *tps,
230                 struct tps62360_regulator_platform_data *pdata)
231 {
232         int ret;
233         int i;
234
235         /* Initailize internal pull up/down control */
236         if (tps->en_internal_pulldn)
237                 ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
238         else
239                 ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
240         if (ret < 0) {
241                 dev_err(tps->dev, "%s() fails in writing reg %d\n",
242                         __func__, REG_CONTROL);
243                 return ret;
244         }
245
246         /* Initailize force PWM mode */
247         if (tps->valid_gpios) {
248                 for (i = 0; i < 4; ++i) {
249                         ret = tps62360_init_force_pwm(tps, pdata, i);
250                         if (ret < 0)
251                                 return ret;
252                 }
253         } else {
254                 ret = tps62360_init_force_pwm(tps, pdata, tps->curr_vset_id);
255                 if (ret < 0)
256                         return ret;
257         }
258
259         /* Reset output discharge path to reduce power consumption */
260         ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
261         if (ret < 0)
262                 dev_err(tps->dev, "%s() fails in updating reg %d\n",
263                         __func__, REG_RAMPCTRL);
264         return ret;
265 }
266
267 static const struct regmap_config tps62360_regmap_config = {
268         .reg_bits = 8,
269         .val_bits = 8,
270 };
271
272 static int __devinit tps62360_probe(struct i2c_client *client,
273                                      const struct i2c_device_id *id)
274 {
275         struct tps62360_regulator_platform_data *pdata;
276         struct regulator_dev *rdev;
277         struct tps62360_chip *tps;
278         int ret;
279         int i;
280
281         pdata = client->dev.platform_data;
282         if (!pdata) {
283                 dev_err(&client->dev, "%s() Err: Platform data not found\n",
284                                                 __func__);
285                 return -EIO;
286         }
287
288         tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
289         if (!tps) {
290                 dev_err(&client->dev, "%s() Err: Memory allocation fails\n",
291                                                 __func__);
292                 return -ENOMEM;
293         }
294
295         tps->en_force_pwm = pdata->en_force_pwm;
296         tps->en_discharge = pdata->en_discharge;
297         tps->en_internal_pulldn = pdata->en_internal_pulldn;
298         tps->vsel0_gpio = pdata->vsel0_gpio;
299         tps->vsel1_gpio = pdata->vsel1_gpio;
300         tps->client = client;
301         tps->dev = &client->dev;
302         tps->name = id->name;
303         tps->voltage_base = (id->driver_data == TPS62360) ?
304                                 TPS62360_BASE_VOLTAGE : TPS62361_BASE_VOLTAGE;
305         tps->voltage_reg_mask = (id->driver_data == TPS62360) ? 0x3F : 0x7F;
306
307         tps->desc.name = id->name;
308         tps->desc.id = 0;
309         tps->desc.n_voltages = (id->driver_data == TPS62360) ?
310                                 TPS62360_N_VOLTAGES : TPS62361_N_VOLTAGES;
311         tps->desc.ops = &tps62360_dcdc_ops;
312         tps->desc.type = REGULATOR_VOLTAGE;
313         tps->desc.owner = THIS_MODULE;
314         tps->regmap = regmap_init_i2c(client, &tps62360_regmap_config);
315         if (IS_ERR(tps->regmap)) {
316                 ret = PTR_ERR(tps->regmap);
317                 dev_err(&client->dev, "%s() Err: Failed to allocate register"
318                         "map: %d\n", __func__, ret);
319                 return ret;
320         }
321         i2c_set_clientdata(client, tps);
322
323         tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
324                                 (pdata->vsel0_def_state & 1);
325         tps->lru_index[0] = tps->curr_vset_id;
326         tps->valid_gpios = false;
327
328         if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
329                 ret = gpio_request(tps->vsel0_gpio, "tps62360-vsel0");
330                 if (ret) {
331                         dev_err(&client->dev,
332                                 "Err: Could not obtain vsel0 GPIO %d: %d\n",
333                                                 tps->vsel0_gpio, ret);
334                         goto err_gpio0;
335                 }
336                 ret = gpio_direction_output(tps->vsel0_gpio,
337                                         pdata->vsel0_def_state);
338                 if (ret) {
339                         dev_err(&client->dev, "Err: Could not set direction of"
340                                 "vsel0 GPIO %d: %d\n", tps->vsel0_gpio, ret);
341                         gpio_free(tps->vsel0_gpio);
342                         goto err_gpio0;
343                 }
344
345                 ret = gpio_request(tps->vsel1_gpio, "tps62360-vsel1");
346                 if (ret) {
347                         dev_err(&client->dev,
348                                 "Err: Could not obtain vsel1 GPIO %d: %d\n",
349                                                 tps->vsel1_gpio, ret);
350                         goto err_gpio1;
351                 }
352                 ret = gpio_direction_output(tps->vsel1_gpio,
353                                         pdata->vsel1_def_state);
354                 if (ret) {
355                         dev_err(&client->dev, "Err: Could not set direction of"
356                                 "vsel1 GPIO %d: %d\n", tps->vsel1_gpio, ret);
357                         gpio_free(tps->vsel1_gpio);
358                         goto err_gpio1;
359                 }
360                 tps->valid_gpios = true;
361
362                 /*
363                  * Initialize the lru index with vset_reg id
364                  * The index 0 will be most recently used and
365                  * set with the tps->curr_vset_id */
366                 for (i = 0; i < 4; ++i)
367                         tps->lru_index[i] = i;
368                 tps->lru_index[0] = tps->curr_vset_id;
369                 tps->lru_index[tps->curr_vset_id] = 0;
370         }
371
372         ret = tps62360_init_dcdc(tps, pdata);
373         if (ret < 0) {
374                 dev_err(tps->dev, "%s() Err: Init fails with = %d\n",
375                                 __func__, ret);
376                 goto err_init;
377         }
378
379         /* Register the regulators */
380         rdev = regulator_register(&tps->desc, &client->dev,
381                                 &pdata->reg_init_data, tps, NULL);
382         if (IS_ERR(rdev)) {
383                 dev_err(tps->dev, "%s() Err: Failed to register %s\n",
384                                 __func__, id->name);
385                 ret = PTR_ERR(rdev);
386                 goto err_init;
387         }
388
389         tps->rdev = rdev;
390         return 0;
391
392 err_init:
393         if (gpio_is_valid(tps->vsel1_gpio))
394                 gpio_free(tps->vsel1_gpio);
395 err_gpio1:
396         if (gpio_is_valid(tps->vsel0_gpio))
397                 gpio_free(tps->vsel0_gpio);
398 err_gpio0:
399         regmap_exit(tps->regmap);
400         return ret;
401 }
402
403 /**
404  * tps62360_remove - tps62360 driver i2c remove handler
405  * @client: i2c driver client device structure
406  *
407  * Unregister TPS driver as an i2c client device driver
408  */
409 static int __devexit tps62360_remove(struct i2c_client *client)
410 {
411         struct tps62360_chip *tps = i2c_get_clientdata(client);
412
413         if (gpio_is_valid(tps->vsel1_gpio))
414                 gpio_free(tps->vsel1_gpio);
415
416         if (gpio_is_valid(tps->vsel0_gpio))
417                 gpio_free(tps->vsel0_gpio);
418
419         regulator_unregister(tps->rdev);
420         regmap_exit(tps->regmap);
421         return 0;
422 }
423
424 static void tps62360_shutdown(struct i2c_client *client)
425 {
426         struct tps62360_chip *tps = i2c_get_clientdata(client);
427         int st;
428
429         if (!tps->en_discharge)
430                 return;
431
432         /* Configure the output discharge path */
433         st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
434         if (st < 0)
435                 dev_err(tps->dev, "%s() fails in updating reg %d\n",
436                         __func__, REG_RAMPCTRL);
437 }
438
439 static const struct i2c_device_id tps62360_id[] = {
440         {.name = "tps62360", .driver_data = TPS62360},
441         {.name = "tps62361", .driver_data = TPS62361},
442         {},
443 };
444
445 MODULE_DEVICE_TABLE(i2c, tps62360_id);
446
447 static struct i2c_driver tps62360_i2c_driver = {
448         .driver = {
449                 .name = "tps62360",
450                 .owner = THIS_MODULE,
451         },
452         .probe = tps62360_probe,
453         .remove = __devexit_p(tps62360_remove),
454         .shutdown = tps62360_shutdown,
455         .id_table = tps62360_id,
456 };
457
458 static int __init tps62360_init(void)
459 {
460         return i2c_add_driver(&tps62360_i2c_driver);
461 }
462 subsys_initcall(tps62360_init);
463
464 static void __exit tps62360_cleanup(void)
465 {
466         i2c_del_driver(&tps62360_i2c_driver);
467 }
468 module_exit(tps62360_cleanup);
469
470 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
471 MODULE_DESCRIPTION("TPS62360 voltage regulator driver");
472 MODULE_LICENSE("GPL v2");