blob: 20fef1d6cf90753f0733e49f3d78808514036d37 [file] [log] [blame]
Laxman Dewangan62199292012-01-09 20:27:41 +05301/*
2 * tps62360.c -- TI tps62360
3 *
Axel Lind1cf4f62012-04-02 18:19:28 +08004 * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363.
Laxman Dewangan62199292012-01-09 20:27:41 +05305 *
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>
Laxman Dewangan62199292012-01-09 20:27:41 +053035#include <linux/slab.h>
36#include <linux/regmap.h>
37
38/* Register definitions */
39#define REG_VSET0 0
40#define REG_VSET1 1
41#define REG_VSET2 2
42#define REG_VSET3 3
43#define REG_CONTROL 4
44#define REG_TEMP 5
45#define REG_RAMPCTRL 6
46#define REG_CHIPID 8
47
Axel Lind1cf4f62012-04-02 18:19:28 +080048enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
Laxman Dewangan62199292012-01-09 20:27:41 +053049
50#define TPS62360_BASE_VOLTAGE 770
51#define TPS62360_N_VOLTAGES 64
52
53#define TPS62361_BASE_VOLTAGE 500
54#define TPS62361_N_VOLTAGES 128
55
56/* tps 62360 chip information */
57struct tps62360_chip {
Laxman Dewangan62199292012-01-09 20:27:41 +053058 struct device *dev;
59 struct regulator_desc desc;
Laxman Dewangan62199292012-01-09 20:27:41 +053060 struct regulator_dev *rdev;
61 struct regmap *regmap;
62 int chip_id;
63 int vsel0_gpio;
64 int vsel1_gpio;
65 int voltage_base;
66 u8 voltage_reg_mask;
67 bool en_internal_pulldn;
68 bool en_force_pwm;
69 bool en_discharge;
70 bool valid_gpios;
71 int lru_index[4];
72 int curr_vset_vsel[4];
73 int curr_vset_id;
74};
75
76/*
77 * find_voltage_set_register: Find new voltage configuration register
78 * (VSET) id.
79 * The finding of the new VSET register will be based on the LRU mechanism.
80 * Each VSET register will have different voltage configured . This
81 * Function will look if any of the VSET register have requested voltage set
82 * or not.
83 * - If it is already there then it will make that register as most
84 * recently used and return as found so that caller need not to set
85 * the VSET register but need to set the proper gpios to select this
86 * VSET register.
87 * - If requested voltage is not found then it will use the least
88 * recently mechanism to get new VSET register for new configuration
89 * and will return not_found so that caller need to set new VSET
90 * register and then gpios (both).
91 */
92static bool find_voltage_set_register(struct tps62360_chip *tps,
93 int req_vsel, int *vset_reg_id)
94{
95 int i;
96 bool found = false;
97 int new_vset_reg = tps->lru_index[3];
98 int found_index = 3;
99 for (i = 0; i < 4; ++i) {
100 if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
101 new_vset_reg = tps->lru_index[i];
102 found_index = i;
103 found = true;
104 goto update_lru_index;
105 }
106 }
107
108update_lru_index:
109 for (i = found_index; i > 0; i--)
110 tps->lru_index[i] = tps->lru_index[i - 1];
111
112 tps->lru_index[0] = new_vset_reg;
113 *vset_reg_id = new_vset_reg;
114 return found;
115}
116
117static int tps62360_dcdc_get_voltage(struct regulator_dev *dev)
118{
119 struct tps62360_chip *tps = rdev_get_drvdata(dev);
120 int vsel;
121 unsigned int data;
122 int ret;
123
124 ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
125 if (ret < 0) {
126 dev_err(tps->dev, "%s: Error in reading register %d\n",
127 __func__, REG_VSET0 + tps->curr_vset_id);
128 return ret;
129 }
130 vsel = (int)data & tps->voltage_reg_mask;
131 return (tps->voltage_base + vsel * 10) * 1000;
132}
133
134static int tps62360_dcdc_set_voltage(struct regulator_dev *dev,
135 int min_uV, int max_uV, unsigned *selector)
136{
137 struct tps62360_chip *tps = rdev_get_drvdata(dev);
138 int vsel;
139 int ret;
140 bool found = false;
141 int new_vset_id = tps->curr_vset_id;
142
143 if (max_uV < min_uV)
144 return -EINVAL;
145
146 if (min_uV >
147 ((tps->voltage_base + (tps->desc.n_voltages - 1) * 10) * 1000))
148 return -EINVAL;
149
150 if (max_uV < tps->voltage_base * 1000)
151 return -EINVAL;
152
153 vsel = DIV_ROUND_UP(min_uV - (tps->voltage_base * 1000), 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, "%s: Error in updating register %d\n",
169 __func__, REG_VSET0 + new_vset_id);
170 return ret;
171 }
172 tps->curr_vset_id = new_vset_id;
173 tps->curr_vset_vsel[new_vset_id] = vsel;
174 }
175
176 /* Select proper VSET register vio gpios */
177 if (tps->valid_gpios) {
178 gpio_set_value_cansleep(tps->vsel0_gpio,
179 new_vset_id & 0x1);
180 gpio_set_value_cansleep(tps->vsel1_gpio,
181 (new_vset_id >> 1) & 0x1);
182 }
183 return 0;
184}
185
186static int tps62360_dcdc_list_voltage(struct regulator_dev *dev,
187 unsigned selector)
188{
189 struct tps62360_chip *tps = rdev_get_drvdata(dev);
190
Axel Lin46783a02012-02-07 11:06:20 +0800191 if (selector >= tps->desc.n_voltages)
Laxman Dewangan62199292012-01-09 20:27:41 +0530192 return -EINVAL;
193 return (tps->voltage_base + selector * 10) * 1000;
194}
195
196static struct regulator_ops tps62360_dcdc_ops = {
197 .get_voltage = tps62360_dcdc_get_voltage,
198 .set_voltage = tps62360_dcdc_set_voltage,
199 .list_voltage = tps62360_dcdc_list_voltage,
200};
201
202static int tps62360_init_force_pwm(struct tps62360_chip *tps,
203 struct tps62360_regulator_platform_data *pdata,
204 int vset_id)
205{
206 unsigned int data;
207 int ret;
208 ret = regmap_read(tps->regmap, REG_VSET0 + vset_id, &data);
209 if (ret < 0) {
210 dev_err(tps->dev, "%s() fails in writing reg %d\n",
211 __func__, REG_VSET0 + vset_id);
212 return ret;
213 }
214 tps->curr_vset_vsel[vset_id] = data & tps->voltage_reg_mask;
215 if (pdata->en_force_pwm)
216 data |= BIT(7);
217 else
218 data &= ~BIT(7);
219 ret = regmap_write(tps->regmap, REG_VSET0 + vset_id, data);
220 if (ret < 0)
221 dev_err(tps->dev, "%s() fails in writing reg %d\n",
222 __func__, REG_VSET0 + vset_id);
223 return ret;
224}
225
226static int tps62360_init_dcdc(struct tps62360_chip *tps,
227 struct tps62360_regulator_platform_data *pdata)
228{
229 int ret;
230 int i;
231
232 /* Initailize internal pull up/down control */
233 if (tps->en_internal_pulldn)
234 ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
235 else
236 ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
237 if (ret < 0) {
238 dev_err(tps->dev, "%s() fails in writing reg %d\n",
239 __func__, REG_CONTROL);
240 return ret;
241 }
242
243 /* Initailize force PWM mode */
244 if (tps->valid_gpios) {
245 for (i = 0; i < 4; ++i) {
246 ret = tps62360_init_force_pwm(tps, pdata, i);
247 if (ret < 0)
248 return ret;
249 }
250 } else {
251 ret = tps62360_init_force_pwm(tps, pdata, tps->curr_vset_id);
252 if (ret < 0)
253 return ret;
254 }
255
256 /* Reset output discharge path to reduce power consumption */
257 ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
258 if (ret < 0)
259 dev_err(tps->dev, "%s() fails in updating reg %d\n",
260 __func__, REG_RAMPCTRL);
261 return ret;
262}
263
264static const struct regmap_config tps62360_regmap_config = {
265 .reg_bits = 8,
266 .val_bits = 8,
267};
268
269static int __devinit tps62360_probe(struct i2c_client *client,
270 const struct i2c_device_id *id)
271{
Mark Brownc1727082012-04-04 00:50:22 +0100272 struct regulator_config config = { };
Laxman Dewangan62199292012-01-09 20:27:41 +0530273 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;
Laxman Dewangan62199292012-01-09 20:27:41 +0530298 tps->dev = &client->dev;
Axel Lind1cf4f62012-04-02 18:19:28 +0800299
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 }
Laxman Dewangan62199292012-01-09 20:27:41 +0530316
317 tps->desc.name = id->name;
318 tps->desc.id = 0;
Laxman Dewangan62199292012-01-09 20:27:41 +0530319 tps->desc.ops = &tps62360_dcdc_ops;
320 tps->desc.type = REGULATOR_VOLTAGE;
321 tps->desc.owner = THIS_MODULE;
Axel Lin9a4bdd82012-04-07 23:29:56 +0800322 tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
Laxman Dewangan62199292012-01-09 20:27:41 +0530323 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
Mark Brownc1727082012-04-04 00:50:22 +0100387 config.dev = &client->dev;
388 config.init_data = &pdata->reg_init_data;
389 config.driver_data = tps;
390
Laxman Dewangan62199292012-01-09 20:27:41 +0530391 /* Register the regulators */
Mark Brownc1727082012-04-04 00:50:22 +0100392 rdev = regulator_register(&tps->desc, &config);
Laxman Dewangan62199292012-01-09 20:27:41 +0530393 if (IS_ERR(rdev)) {
394 dev_err(tps->dev, "%s() Err: Failed to register %s\n",
395 __func__, id->name);
396 ret = PTR_ERR(rdev);
397 goto err_init;
398 }
399
400 tps->rdev = rdev;
401 return 0;
402
403err_init:
404 if (gpio_is_valid(tps->vsel1_gpio))
405 gpio_free(tps->vsel1_gpio);
406err_gpio1:
407 if (gpio_is_valid(tps->vsel0_gpio))
408 gpio_free(tps->vsel0_gpio);
409err_gpio0:
Laxman Dewangan62199292012-01-09 20:27:41 +0530410 return ret;
411}
412
413/**
414 * tps62360_remove - tps62360 driver i2c remove handler
415 * @client: i2c driver client device structure
416 *
417 * Unregister TPS driver as an i2c client device driver
418 */
419static int __devexit tps62360_remove(struct i2c_client *client)
420{
421 struct tps62360_chip *tps = i2c_get_clientdata(client);
422
423 if (gpio_is_valid(tps->vsel1_gpio))
424 gpio_free(tps->vsel1_gpio);
425
426 if (gpio_is_valid(tps->vsel0_gpio))
427 gpio_free(tps->vsel0_gpio);
428
429 regulator_unregister(tps->rdev);
Laxman Dewangan62199292012-01-09 20:27:41 +0530430 return 0;
431}
432
433static void tps62360_shutdown(struct i2c_client *client)
434{
435 struct tps62360_chip *tps = i2c_get_clientdata(client);
436 int st;
437
438 if (!tps->en_discharge)
439 return;
440
441 /* Configure the output discharge path */
442 st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
443 if (st < 0)
444 dev_err(tps->dev, "%s() fails in updating reg %d\n",
445 __func__, REG_RAMPCTRL);
446}
447
448static const struct i2c_device_id tps62360_id[] = {
449 {.name = "tps62360", .driver_data = TPS62360},
450 {.name = "tps62361", .driver_data = TPS62361},
Axel Lind1cf4f62012-04-02 18:19:28 +0800451 {.name = "tps62362", .driver_data = TPS62362},
452 {.name = "tps62363", .driver_data = TPS62363},
Laxman Dewangan62199292012-01-09 20:27:41 +0530453 {},
454};
455
456MODULE_DEVICE_TABLE(i2c, tps62360_id);
457
458static struct i2c_driver tps62360_i2c_driver = {
459 .driver = {
460 .name = "tps62360",
461 .owner = THIS_MODULE,
462 },
463 .probe = tps62360_probe,
464 .remove = __devexit_p(tps62360_remove),
465 .shutdown = tps62360_shutdown,
466 .id_table = tps62360_id,
467};
468
469static int __init tps62360_init(void)
470{
471 return i2c_add_driver(&tps62360_i2c_driver);
472}
473subsys_initcall(tps62360_init);
474
475static void __exit tps62360_cleanup(void)
476{
477 i2c_del_driver(&tps62360_i2c_driver);
478}
479module_exit(tps62360_cleanup);
480
481MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
Axel Lind1cf4f62012-04-02 18:19:28 +0800482MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
Laxman Dewangan62199292012-01-09 20:27:41 +0530483MODULE_LICENSE("GPL v2");