Commit | Line | Data |
---|---|---|
6219929f LD |
1 | /* |
2 | * tps62360.c -- TI tps62360 | |
3 | * | |
d1cf4f65 | 4 | * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363. |
6219929f LD |
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> | |
6219929f LD |
35 | #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 | ||
d1cf4f65 | 48 | enum chips {TPS62360, TPS62361, TPS62362, TPS62363}; |
6219929f LD |
49 | |
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 */ | |
57 | struct tps62360_chip { | |
6219929f LD |
58 | struct device *dev; |
59 | struct regulator_desc desc; | |
6219929f LD |
60 | 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; | |
a60cfce0 | 74 | int change_uv_per_us; |
6219929f LD |
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 | ||
a60cfce0 | 118 | static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev) |
6219929f LD |
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; | |
a60cfce0 | 132 | return vsel; |
6219929f LD |
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 | ||
46783a04 | 192 | if (selector >= tps->desc.n_voltages) |
6219929f LD |
193 | return -EINVAL; |
194 | return (tps->voltage_base + selector * 10) * 1000; | |
195 | } | |
196 | ||
a60cfce0 LD |
197 | static int tps62360_set_voltage_time_sel(struct regulator_dev *rdev, |
198 | unsigned int old_selector, unsigned int new_selector) | |
199 | { | |
200 | struct tps62360_chip *tps = rdev_get_drvdata(rdev); | |
201 | int old_uV, new_uV; | |
202 | ||
203 | old_uV = tps62360_dcdc_list_voltage(rdev, old_selector); | |
204 | if (old_uV < 0) | |
205 | return old_uV; | |
206 | ||
207 | new_uV = tps62360_dcdc_list_voltage(rdev, new_selector); | |
208 | if (new_uV < 0) | |
209 | return new_uV; | |
210 | ||
211 | return DIV_ROUND_UP(abs(old_uV - new_uV), tps->change_uv_per_us); | |
212 | } | |
213 | ||
6219929f | 214 | static struct regulator_ops tps62360_dcdc_ops = { |
a60cfce0 LD |
215 | .get_voltage_sel = tps62360_dcdc_get_voltage_sel, |
216 | .set_voltage = tps62360_dcdc_set_voltage, | |
217 | .list_voltage = tps62360_dcdc_list_voltage, | |
218 | .set_voltage_time_sel = tps62360_set_voltage_time_sel, | |
6219929f LD |
219 | }; |
220 | ||
221 | static int tps62360_init_force_pwm(struct tps62360_chip *tps, | |
222 | struct tps62360_regulator_platform_data *pdata, | |
223 | int vset_id) | |
224 | { | |
225 | unsigned int data; | |
226 | int ret; | |
227 | ret = regmap_read(tps->regmap, REG_VSET0 + vset_id, &data); | |
228 | if (ret < 0) { | |
229 | dev_err(tps->dev, "%s() fails in writing reg %d\n", | |
230 | __func__, REG_VSET0 + vset_id); | |
231 | return ret; | |
232 | } | |
233 | tps->curr_vset_vsel[vset_id] = data & tps->voltage_reg_mask; | |
234 | if (pdata->en_force_pwm) | |
235 | data |= BIT(7); | |
236 | else | |
237 | data &= ~BIT(7); | |
238 | ret = regmap_write(tps->regmap, REG_VSET0 + vset_id, data); | |
239 | if (ret < 0) | |
240 | dev_err(tps->dev, "%s() fails in writing reg %d\n", | |
241 | __func__, REG_VSET0 + vset_id); | |
242 | return ret; | |
243 | } | |
244 | ||
245 | static int tps62360_init_dcdc(struct tps62360_chip *tps, | |
246 | struct tps62360_regulator_platform_data *pdata) | |
247 | { | |
248 | int ret; | |
249 | int i; | |
a60cfce0 | 250 | unsigned int ramp_ctrl; |
6219929f LD |
251 | |
252 | /* Initailize internal pull up/down control */ | |
253 | if (tps->en_internal_pulldn) | |
254 | ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0); | |
255 | else | |
256 | ret = regmap_write(tps->regmap, REG_CONTROL, 0x0); | |
257 | if (ret < 0) { | |
258 | dev_err(tps->dev, "%s() fails in writing reg %d\n", | |
259 | __func__, REG_CONTROL); | |
260 | return ret; | |
261 | } | |
262 | ||
263 | /* Initailize force PWM mode */ | |
264 | if (tps->valid_gpios) { | |
265 | for (i = 0; i < 4; ++i) { | |
266 | ret = tps62360_init_force_pwm(tps, pdata, i); | |
267 | if (ret < 0) | |
268 | return ret; | |
269 | } | |
270 | } else { | |
271 | ret = tps62360_init_force_pwm(tps, pdata, tps->curr_vset_id); | |
272 | if (ret < 0) | |
273 | return ret; | |
274 | } | |
275 | ||
276 | /* Reset output discharge path to reduce power consumption */ | |
277 | ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0); | |
a60cfce0 | 278 | if (ret < 0) { |
6219929f LD |
279 | dev_err(tps->dev, "%s() fails in updating reg %d\n", |
280 | __func__, REG_RAMPCTRL); | |
a60cfce0 LD |
281 | return ret; |
282 | } | |
283 | ||
284 | /* Get ramp value from ramp control register */ | |
285 | ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl); | |
286 | if (ret < 0) { | |
287 | dev_err(tps->dev, "%s() fails in reading reg %d\n", | |
288 | __func__, REG_RAMPCTRL); | |
289 | return ret; | |
290 | } | |
291 | ramp_ctrl = (ramp_ctrl >> 4) & 0x7; | |
292 | ||
293 | /* ramp mV/us = 32/(2^ramp_ctrl) */ | |
294 | tps->change_uv_per_us = DIV_ROUND_UP(32000, BIT(ramp_ctrl)); | |
6219929f LD |
295 | return ret; |
296 | } | |
297 | ||
298 | static const struct regmap_config tps62360_regmap_config = { | |
16ea003b LD |
299 | .reg_bits = 8, |
300 | .val_bits = 8, | |
301 | .max_register = REG_CHIPID, | |
302 | .cache_type = REGCACHE_RBTREE, | |
6219929f LD |
303 | }; |
304 | ||
305 | static int __devinit tps62360_probe(struct i2c_client *client, | |
306 | const struct i2c_device_id *id) | |
307 | { | |
c172708d | 308 | struct regulator_config config = { }; |
6219929f LD |
309 | struct tps62360_regulator_platform_data *pdata; |
310 | struct regulator_dev *rdev; | |
311 | struct tps62360_chip *tps; | |
312 | int ret; | |
313 | int i; | |
314 | ||
315 | pdata = client->dev.platform_data; | |
316 | if (!pdata) { | |
317 | dev_err(&client->dev, "%s() Err: Platform data not found\n", | |
318 | __func__); | |
319 | return -EIO; | |
320 | } | |
321 | ||
322 | tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL); | |
323 | if (!tps) { | |
324 | dev_err(&client->dev, "%s() Err: Memory allocation fails\n", | |
325 | __func__); | |
326 | return -ENOMEM; | |
327 | } | |
328 | ||
329 | tps->en_force_pwm = pdata->en_force_pwm; | |
330 | tps->en_discharge = pdata->en_discharge; | |
331 | tps->en_internal_pulldn = pdata->en_internal_pulldn; | |
332 | tps->vsel0_gpio = pdata->vsel0_gpio; | |
333 | tps->vsel1_gpio = pdata->vsel1_gpio; | |
6219929f | 334 | tps->dev = &client->dev; |
d1cf4f65 AL |
335 | |
336 | switch (id->driver_data) { | |
337 | case TPS62360: | |
338 | case TPS62362: | |
339 | tps->voltage_base = TPS62360_BASE_VOLTAGE; | |
340 | tps->voltage_reg_mask = 0x3F; | |
341 | tps->desc.n_voltages = TPS62360_N_VOLTAGES; | |
342 | break; | |
343 | case TPS62361: | |
344 | case TPS62363: | |
345 | tps->voltage_base = TPS62361_BASE_VOLTAGE; | |
346 | tps->voltage_reg_mask = 0x7F; | |
347 | tps->desc.n_voltages = TPS62361_N_VOLTAGES; | |
348 | break; | |
349 | default: | |
350 | return -ENODEV; | |
351 | } | |
6219929f LD |
352 | |
353 | tps->desc.name = id->name; | |
354 | tps->desc.id = 0; | |
6219929f LD |
355 | tps->desc.ops = &tps62360_dcdc_ops; |
356 | tps->desc.type = REGULATOR_VOLTAGE; | |
357 | tps->desc.owner = THIS_MODULE; | |
9a4bdd87 | 358 | tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config); |
6219929f LD |
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->vsel1_def_state & 1) * 2 + | |
368 | (pdata->vsel0_def_state & 1); | |
369 | tps->lru_index[0] = tps->curr_vset_id; | |
370 | tps->valid_gpios = false; | |
371 | ||
372 | if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) { | |
373 | ret = gpio_request(tps->vsel0_gpio, "tps62360-vsel0"); | |
374 | if (ret) { | |
375 | dev_err(&client->dev, | |
376 | "Err: Could not obtain vsel0 GPIO %d: %d\n", | |
377 | tps->vsel0_gpio, ret); | |
378 | goto err_gpio0; | |
379 | } | |
380 | ret = gpio_direction_output(tps->vsel0_gpio, | |
381 | pdata->vsel0_def_state); | |
382 | if (ret) { | |
383 | dev_err(&client->dev, "Err: Could not set direction of" | |
384 | "vsel0 GPIO %d: %d\n", tps->vsel0_gpio, ret); | |
385 | gpio_free(tps->vsel0_gpio); | |
386 | goto err_gpio0; | |
387 | } | |
388 | ||
389 | ret = gpio_request(tps->vsel1_gpio, "tps62360-vsel1"); | |
390 | if (ret) { | |
391 | dev_err(&client->dev, | |
392 | "Err: Could not obtain vsel1 GPIO %d: %d\n", | |
393 | tps->vsel1_gpio, ret); | |
394 | goto err_gpio1; | |
395 | } | |
396 | ret = gpio_direction_output(tps->vsel1_gpio, | |
397 | pdata->vsel1_def_state); | |
398 | if (ret) { | |
399 | dev_err(&client->dev, "Err: Could not set direction of" | |
400 | "vsel1 GPIO %d: %d\n", tps->vsel1_gpio, ret); | |
401 | gpio_free(tps->vsel1_gpio); | |
402 | goto err_gpio1; | |
403 | } | |
404 | tps->valid_gpios = true; | |
405 | ||
406 | /* | |
407 | * Initialize the lru index with vset_reg id | |
408 | * The index 0 will be most recently used and | |
409 | * set with the tps->curr_vset_id */ | |
410 | for (i = 0; i < 4; ++i) | |
411 | tps->lru_index[i] = i; | |
412 | tps->lru_index[0] = tps->curr_vset_id; | |
413 | tps->lru_index[tps->curr_vset_id] = 0; | |
414 | } | |
415 | ||
416 | ret = tps62360_init_dcdc(tps, pdata); | |
417 | if (ret < 0) { | |
418 | dev_err(tps->dev, "%s() Err: Init fails with = %d\n", | |
419 | __func__, ret); | |
420 | goto err_init; | |
421 | } | |
422 | ||
c172708d MB |
423 | config.dev = &client->dev; |
424 | config.init_data = &pdata->reg_init_data; | |
425 | config.driver_data = tps; | |
426 | ||
6219929f | 427 | /* Register the regulators */ |
c172708d | 428 | rdev = regulator_register(&tps->desc, &config); |
6219929f LD |
429 | if (IS_ERR(rdev)) { |
430 | dev_err(tps->dev, "%s() Err: Failed to register %s\n", | |
431 | __func__, id->name); | |
432 | ret = PTR_ERR(rdev); | |
433 | goto err_init; | |
434 | } | |
435 | ||
436 | tps->rdev = rdev; | |
437 | return 0; | |
438 | ||
439 | err_init: | |
440 | if (gpio_is_valid(tps->vsel1_gpio)) | |
441 | gpio_free(tps->vsel1_gpio); | |
442 | err_gpio1: | |
443 | if (gpio_is_valid(tps->vsel0_gpio)) | |
444 | gpio_free(tps->vsel0_gpio); | |
445 | err_gpio0: | |
6219929f LD |
446 | return ret; |
447 | } | |
448 | ||
449 | /** | |
450 | * tps62360_remove - tps62360 driver i2c remove handler | |
451 | * @client: i2c driver client device structure | |
452 | * | |
453 | * Unregister TPS driver as an i2c client device driver | |
454 | */ | |
455 | static int __devexit tps62360_remove(struct i2c_client *client) | |
456 | { | |
457 | struct tps62360_chip *tps = i2c_get_clientdata(client); | |
458 | ||
459 | if (gpio_is_valid(tps->vsel1_gpio)) | |
460 | gpio_free(tps->vsel1_gpio); | |
461 | ||
462 | if (gpio_is_valid(tps->vsel0_gpio)) | |
463 | gpio_free(tps->vsel0_gpio); | |
464 | ||
465 | regulator_unregister(tps->rdev); | |
6219929f LD |
466 | return 0; |
467 | } | |
468 | ||
469 | static void tps62360_shutdown(struct i2c_client *client) | |
470 | { | |
471 | struct tps62360_chip *tps = i2c_get_clientdata(client); | |
472 | int st; | |
473 | ||
474 | if (!tps->en_discharge) | |
475 | return; | |
476 | ||
477 | /* Configure the output discharge path */ | |
478 | st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2)); | |
479 | if (st < 0) | |
480 | dev_err(tps->dev, "%s() fails in updating reg %d\n", | |
481 | __func__, REG_RAMPCTRL); | |
482 | } | |
483 | ||
484 | static const struct i2c_device_id tps62360_id[] = { | |
485 | {.name = "tps62360", .driver_data = TPS62360}, | |
486 | {.name = "tps62361", .driver_data = TPS62361}, | |
d1cf4f65 AL |
487 | {.name = "tps62362", .driver_data = TPS62362}, |
488 | {.name = "tps62363", .driver_data = TPS62363}, | |
6219929f LD |
489 | {}, |
490 | }; | |
491 | ||
492 | MODULE_DEVICE_TABLE(i2c, tps62360_id); | |
493 | ||
494 | static struct i2c_driver tps62360_i2c_driver = { | |
495 | .driver = { | |
496 | .name = "tps62360", | |
497 | .owner = THIS_MODULE, | |
498 | }, | |
499 | .probe = tps62360_probe, | |
500 | .remove = __devexit_p(tps62360_remove), | |
501 | .shutdown = tps62360_shutdown, | |
502 | .id_table = tps62360_id, | |
503 | }; | |
504 | ||
505 | static int __init tps62360_init(void) | |
506 | { | |
507 | return i2c_add_driver(&tps62360_i2c_driver); | |
508 | } | |
509 | subsys_initcall(tps62360_init); | |
510 | ||
511 | static void __exit tps62360_cleanup(void) | |
512 | { | |
513 | i2c_del_driver(&tps62360_i2c_driver); | |
514 | } | |
515 | module_exit(tps62360_cleanup); | |
516 | ||
517 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); | |
d1cf4f65 | 518 | MODULE_DESCRIPTION("TPS6236x voltage regulator driver"); |
6219929f | 519 | MODULE_LICENSE("GPL v2"); |