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