regulator: tps65023: Set missing bit for update core-voltage
[deliverable/linux.git] / drivers / regulator / tps65023-regulator.c
CommitLineData
30e6599d
AA
1/*
2 * tps65023-regulator.c
3 *
4 * Supports TPS65023 Regulator
5 *
6 * Copyright (C) 2009 Texas Instrument Incorporated - http://www.ti.com/
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
11 *
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
13 * whether express or implied; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/err.h>
22#include <linux/platform_device.h>
23#include <linux/regulator/driver.h>
24#include <linux/regulator/machine.h>
25#include <linux/i2c.h>
26#include <linux/delay.h>
5a0e3ad6 27#include <linux/slab.h>
90923351 28#include <linux/regmap.h>
30e6599d
AA
29
30/* Register definitions */
31#define TPS65023_REG_VERSION 0
32#define TPS65023_REG_PGOODZ 1
33#define TPS65023_REG_MASK 2
34#define TPS65023_REG_REG_CTRL 3
35#define TPS65023_REG_CON_CTRL 4
36#define TPS65023_REG_CON_CTRL2 5
37#define TPS65023_REG_DEF_CORE 6
38#define TPS65023_REG_DEFSLEW 7
39#define TPS65023_REG_LDO_CTRL 8
40
41/* PGOODZ bitfields */
42#define TPS65023_PGOODZ_PWRFAILZ BIT(7)
43#define TPS65023_PGOODZ_LOWBATTZ BIT(6)
44#define TPS65023_PGOODZ_VDCDC1 BIT(5)
45#define TPS65023_PGOODZ_VDCDC2 BIT(4)
46#define TPS65023_PGOODZ_VDCDC3 BIT(3)
47#define TPS65023_PGOODZ_LDO2 BIT(2)
48#define TPS65023_PGOODZ_LDO1 BIT(1)
49
50/* MASK bitfields */
51#define TPS65023_MASK_PWRFAILZ BIT(7)
52#define TPS65023_MASK_LOWBATTZ BIT(6)
53#define TPS65023_MASK_VDCDC1 BIT(5)
54#define TPS65023_MASK_VDCDC2 BIT(4)
55#define TPS65023_MASK_VDCDC3 BIT(3)
56#define TPS65023_MASK_LDO2 BIT(2)
57#define TPS65023_MASK_LDO1 BIT(1)
58
59/* REG_CTRL bitfields */
60#define TPS65023_REG_CTRL_VDCDC1_EN BIT(5)
61#define TPS65023_REG_CTRL_VDCDC2_EN BIT(4)
62#define TPS65023_REG_CTRL_VDCDC3_EN BIT(3)
63#define TPS65023_REG_CTRL_LDO2_EN BIT(2)
64#define TPS65023_REG_CTRL_LDO1_EN BIT(1)
65
fc999b83
MF
66/* REG_CTRL2 bitfields */
67#define TPS65023_REG_CTRL2_GO BIT(7)
68#define TPS65023_REG_CTRL2_CORE_ADJ BIT(6)
69#define TPS65023_REG_CTRL2_DCDC2 BIT(2)
70#define TPS65023_REG_CTRL2_DCDC1 BIT(2)
71#define TPS65023_REG_CTRL2_DCDC3 BIT(0)
72
f068ad8c
MF
73/* REG_CTRL2 bitfields */
74#define TPS65023_REG_CTRL2_GO BIT(7)
75#define TPS65023_REG_CTRL2_CORE_ADJ BIT(6)
76#define TPS65023_REG_CTRL2_DCDC2 BIT(2)
77#define TPS65023_REG_CTRL2_DCDC1 BIT(1)
78#define TPS65023_REG_CTRL2_DCDC3 BIT(0)
79
30e6599d
AA
80/* LDO_CTRL bitfields */
81#define TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_id) ((ldo_id)*4)
82#define TPS65023_LDO_CTRL_LDOx_MASK(ldo_id) (0xF0 >> ((ldo_id)*4))
83
84/* Number of step-down converters available */
85#define TPS65023_NUM_DCDC 3
86/* Number of LDO voltage regulators available */
87#define TPS65023_NUM_LDO 2
88/* Number of total regulators available */
89#define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
90
91/* DCDCs */
92#define TPS65023_DCDC_1 0
93#define TPS65023_DCDC_2 1
94#define TPS65023_DCDC_3 2
95/* LDOs */
96#define TPS65023_LDO_1 3
97#define TPS65023_LDO_2 4
98
99#define TPS65023_MAX_REG_ID TPS65023_LDO_2
100
101/* Supported voltage values for regulators */
102static const u16 VDCDC1_VSEL_table[] = {
103 800, 825, 850, 875,
104 900, 925, 950, 975,
105 1000, 1025, 1050, 1075,
106 1100, 1125, 1150, 1175,
107 1200, 1225, 1250, 1275,
108 1300, 1325, 1350, 1375,
109 1400, 1425, 1450, 1475,
110 1500, 1525, 1550, 1600,
111};
112
113static const u16 LDO1_VSEL_table[] = {
114 1000, 1100, 1300, 1800,
115 2200, 2600, 2800, 3150,
116};
117
118static const u16 LDO2_VSEL_table[] = {
119 1050, 1200, 1300, 1800,
120 2500, 2800, 3000, 3300,
121};
122
123static unsigned int num_voltages[] = {ARRAY_SIZE(VDCDC1_VSEL_table),
124 0, 0, ARRAY_SIZE(LDO1_VSEL_table),
125 ARRAY_SIZE(LDO2_VSEL_table)};
126
127/* Regulator specific details */
128struct tps_info {
129 const char *name;
130 unsigned min_uV;
131 unsigned max_uV;
132 bool fixed;
133 u8 table_len;
134 const u16 *table;
135};
136
137/* PMIC details */
138struct tps_pmic {
139 struct regulator_desc desc[TPS65023_NUM_REGULATOR];
140 struct i2c_client *client;
141 struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
142 const struct tps_info *info[TPS65023_NUM_REGULATOR];
90923351 143 struct regmap *regmap;
30e6599d
AA
144};
145
30e6599d
AA
146static int tps_65023_set_bits(struct tps_pmic *tps, u8 reg, u8 mask)
147{
90923351 148 return regmap_update_bits(tps->regmap, reg, mask, mask);
30e6599d
AA
149}
150
151static int tps_65023_clear_bits(struct tps_pmic *tps, u8 reg, u8 mask)
152{
90923351 153 return regmap_update_bits(tps->regmap, reg, mask, 0);
30e6599d
AA
154}
155
156static int tps_65023_reg_read(struct tps_pmic *tps, u8 reg)
157{
90923351
MB
158 unsigned int val;
159 int ret;
30e6599d 160
90923351 161 ret = regmap_read(tps->regmap, reg, &val);
30e6599d 162
90923351
MB
163 if (ret != 0)
164 return ret;
165 else
166 return val;
30e6599d
AA
167}
168
169static int tps_65023_reg_write(struct tps_pmic *tps, u8 reg, u8 val)
170{
90923351 171 return regmap_write(tps->regmap, reg, val);
30e6599d
AA
172}
173
174static int tps65023_dcdc_is_enabled(struct regulator_dev *dev)
175{
176 struct tps_pmic *tps = rdev_get_drvdata(dev);
177 int data, dcdc = rdev_get_id(dev);
178 u8 shift;
179
180 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
181 return -EINVAL;
182
183 shift = TPS65023_NUM_REGULATOR - dcdc;
184 data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
185
186 if (data < 0)
187 return data;
188 else
189 return (data & 1<<shift) ? 1 : 0;
190}
191
192static int tps65023_ldo_is_enabled(struct regulator_dev *dev)
193{
194 struct tps_pmic *tps = rdev_get_drvdata(dev);
195 int data, ldo = rdev_get_id(dev);
196 u8 shift;
197
198 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
199 return -EINVAL;
200
201 shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
202 data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
203
204 if (data < 0)
205 return data;
206 else
207 return (data & 1<<shift) ? 1 : 0;
208}
209
210static int tps65023_dcdc_enable(struct regulator_dev *dev)
211{
212 struct tps_pmic *tps = rdev_get_drvdata(dev);
213 int dcdc = rdev_get_id(dev);
214 u8 shift;
215
216 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
217 return -EINVAL;
218
219 shift = TPS65023_NUM_REGULATOR - dcdc;
220 return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
221}
222
223static int tps65023_dcdc_disable(struct regulator_dev *dev)
224{
225 struct tps_pmic *tps = rdev_get_drvdata(dev);
226 int dcdc = rdev_get_id(dev);
227 u8 shift;
228
229 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
230 return -EINVAL;
231
232 shift = TPS65023_NUM_REGULATOR - dcdc;
233 return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
234}
235
236static int tps65023_ldo_enable(struct regulator_dev *dev)
237{
238 struct tps_pmic *tps = rdev_get_drvdata(dev);
239 int ldo = rdev_get_id(dev);
240 u8 shift;
241
242 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
243 return -EINVAL;
244
245 shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
246 return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
247}
248
249static int tps65023_ldo_disable(struct regulator_dev *dev)
250{
251 struct tps_pmic *tps = rdev_get_drvdata(dev);
252 int ldo = rdev_get_id(dev);
253 u8 shift;
254
255 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
256 return -EINVAL;
257
258 shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
259 return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
260}
261
262static int tps65023_dcdc_get_voltage(struct regulator_dev *dev)
263{
264 struct tps_pmic *tps = rdev_get_drvdata(dev);
265 int data, dcdc = rdev_get_id(dev);
266
267 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
268 return -EINVAL;
269
270 if (dcdc == TPS65023_DCDC_1) {
271 data = tps_65023_reg_read(tps, TPS65023_REG_DEF_CORE);
272 if (data < 0)
273 return data;
274 data &= (tps->info[dcdc]->table_len - 1);
275 return tps->info[dcdc]->table[data] * 1000;
276 } else
277 return tps->info[dcdc]->min_uV;
278}
279
280static int tps65023_dcdc_set_voltage(struct regulator_dev *dev,
3a93f2a9
MB
281 int min_uV, int max_uV,
282 unsigned *selector)
30e6599d
AA
283{
284 struct tps_pmic *tps = rdev_get_drvdata(dev);
285 int dcdc = rdev_get_id(dev);
286 int vsel;
cc17ef3f 287 int ret;
30e6599d
AA
288
289 if (dcdc != TPS65023_DCDC_1)
290 return -EINVAL;
291
292 if (min_uV < tps->info[dcdc]->min_uV
293 || min_uV > tps->info[dcdc]->max_uV)
294 return -EINVAL;
295 if (max_uV < tps->info[dcdc]->min_uV
296 || max_uV > tps->info[dcdc]->max_uV)
297 return -EINVAL;
298
299 for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
300 int mV = tps->info[dcdc]->table[vsel];
301 int uV = mV * 1000;
302
303 /* Break at the first in-range value */
304 if (min_uV <= uV && uV <= max_uV)
305 break;
306 }
307
3a93f2a9
MB
308 *selector = vsel;
309
30e6599d 310 if (vsel == tps->info[dcdc]->table_len)
cc17ef3f
MF
311 goto failed;
312
313 ret = tps_65023_reg_write(tps, TPS65023_REG_DEF_CORE, vsel);
314
315 /* Tell the chip that we have changed the value in DEFCORE
316 * and its time to update the core voltage
317 */
318 tps_65023_set_bits(tps, TPS65023_REG_CON_CTRL2,
319 TPS65023_REG_CTRL2_GO);
320
321 return ret;
322
323failed:
324 return -EINVAL;
30e6599d
AA
325}
326
327static int tps65023_ldo_get_voltage(struct regulator_dev *dev)
328{
329 struct tps_pmic *tps = rdev_get_drvdata(dev);
330 int data, ldo = rdev_get_id(dev);
331
332 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
333 return -EINVAL;
334
335 data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
336 if (data < 0)
337 return data;
338
339 data >>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1));
340 data &= (tps->info[ldo]->table_len - 1);
341 return tps->info[ldo]->table[data] * 1000;
342}
343
344static int tps65023_ldo_set_voltage(struct regulator_dev *dev,
3a93f2a9 345 int min_uV, int max_uV, unsigned *selector)
30e6599d
AA
346{
347 struct tps_pmic *tps = rdev_get_drvdata(dev);
348 int data, vsel, ldo = rdev_get_id(dev);
349
350 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
351 return -EINVAL;
352
353 if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
354 return -EINVAL;
355 if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
356 return -EINVAL;
357
358 for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
359 int mV = tps->info[ldo]->table[vsel];
360 int uV = mV * 1000;
361
362 /* Break at the first in-range value */
363 if (min_uV <= uV && uV <= max_uV)
364 break;
365 }
366
367 if (vsel == tps->info[ldo]->table_len)
368 return -EINVAL;
369
3a93f2a9
MB
370 *selector = vsel;
371
30e6599d
AA
372 data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
373 if (data < 0)
374 return data;
375
376 data &= TPS65023_LDO_CTRL_LDOx_MASK(ldo - TPS65023_LDO_1);
377 data |= (vsel << (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1)));
378 return tps_65023_reg_write(tps, TPS65023_REG_LDO_CTRL, data);
379}
380
381static int tps65023_dcdc_list_voltage(struct regulator_dev *dev,
382 unsigned selector)
383{
384 struct tps_pmic *tps = rdev_get_drvdata(dev);
385 int dcdc = rdev_get_id(dev);
386
387 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
388 return -EINVAL;
389
390 if (dcdc == TPS65023_DCDC_1) {
391 if (selector >= tps->info[dcdc]->table_len)
392 return -EINVAL;
393 else
394 return tps->info[dcdc]->table[selector] * 1000;
395 } else
396 return tps->info[dcdc]->min_uV;
397}
398
399static int tps65023_ldo_list_voltage(struct regulator_dev *dev,
400 unsigned selector)
401{
402 struct tps_pmic *tps = rdev_get_drvdata(dev);
403 int ldo = rdev_get_id(dev);
404
405 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
406 return -EINVAL;
407
408 if (selector >= tps->info[ldo]->table_len)
409 return -EINVAL;
410 else
411 return tps->info[ldo]->table[selector] * 1000;
412}
413
414/* Operations permitted on VDCDCx */
415static struct regulator_ops tps65023_dcdc_ops = {
416 .is_enabled = tps65023_dcdc_is_enabled,
417 .enable = tps65023_dcdc_enable,
418 .disable = tps65023_dcdc_disable,
419 .get_voltage = tps65023_dcdc_get_voltage,
420 .set_voltage = tps65023_dcdc_set_voltage,
421 .list_voltage = tps65023_dcdc_list_voltage,
422};
423
424/* Operations permitted on LDOx */
425static struct regulator_ops tps65023_ldo_ops = {
426 .is_enabled = tps65023_ldo_is_enabled,
427 .enable = tps65023_ldo_enable,
428 .disable = tps65023_ldo_disable,
429 .get_voltage = tps65023_ldo_get_voltage,
430 .set_voltage = tps65023_ldo_set_voltage,
431 .list_voltage = tps65023_ldo_list_voltage,
432};
433
90923351
MB
434static struct regmap_config tps65023_regmap_config = {
435 .reg_bits = 8,
436 .val_bits = 8,
437};
438
54d13ab1
DT
439static int __devinit tps_65023_probe(struct i2c_client *client,
440 const struct i2c_device_id *id)
30e6599d 441{
30e6599d
AA
442 const struct tps_info *info = (void *)id->driver_data;
443 struct regulator_init_data *init_data;
444 struct regulator_dev *rdev;
445 struct tps_pmic *tps;
446 int i;
54d13ab1 447 int error;
30e6599d
AA
448
449 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
450 return -EIO;
451
452 /**
453 * init_data points to array of regulator_init structures
454 * coming from the board-evm file.
455 */
456 init_data = client->dev.platform_data;
30e6599d
AA
457 if (!init_data)
458 return -EIO;
459
460 tps = kzalloc(sizeof(*tps), GFP_KERNEL);
461 if (!tps)
462 return -ENOMEM;
463
90923351
MB
464 tps->regmap = regmap_init_i2c(client, &tps65023_regmap_config);
465 if (IS_ERR(tps->regmap)) {
466 error = PTR_ERR(tps->regmap);
467 dev_err(&client->dev, "Failed to allocate register map: %d\n",
468 error);
469 goto fail_alloc;
470 }
30e6599d
AA
471
472 /* common for all regulators */
473 tps->client = client;
474
475 for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) {
476 /* Store regulator specific information */
477 tps->info[i] = info;
478
479 tps->desc[i].name = info->name;
77fa44d0 480 tps->desc[i].id = i;
30e6599d
AA
481 tps->desc[i].n_voltages = num_voltages[i];
482 tps->desc[i].ops = (i > TPS65023_DCDC_3 ?
483 &tps65023_ldo_ops : &tps65023_dcdc_ops);
484 tps->desc[i].type = REGULATOR_VOLTAGE;
485 tps->desc[i].owner = THIS_MODULE;
486
487 /* Register the regulators */
488 rdev = regulator_register(&tps->desc[i], &client->dev,
54d13ab1 489 init_data, tps);
30e6599d
AA
490 if (IS_ERR(rdev)) {
491 dev_err(&client->dev, "failed to register %s\n",
492 id->name);
54d13ab1
DT
493 error = PTR_ERR(rdev);
494 goto fail;
30e6599d
AA
495 }
496
497 /* Save regulator for cleanup */
498 tps->rdev[i] = rdev;
499 }
500
501 i2c_set_clientdata(client, tps);
502
f068ad8c
MF
503 /* Enable setting output voltage by I2C */
504 tps_65023_clear_bits(tps, TPS65023_REG_CON_CTRL2,
505 TPS65023_REG_CTRL2_CORE_ADJ);
506
fc999b83
MF
507 /* Enable setting output voltage by I2C */
508 tps_65023_clear_bits(tps, TPS65023_REG_CON_CTRL2,
509 TPS65023_REG_CTRL2_CORE_ADJ);
510
30e6599d 511 return 0;
54d13ab1
DT
512
513 fail:
514 while (--i >= 0)
515 regulator_unregister(tps->rdev[i]);
516
90923351
MB
517 regmap_exit(tps->regmap);
518 fail_alloc:
54d13ab1
DT
519 kfree(tps);
520 return error;
30e6599d
AA
521}
522
523/**
524 * tps_65023_remove - TPS65023 driver i2c remove handler
525 * @client: i2c driver client device structure
526 *
527 * Unregister TPS driver as an i2c client device driver
528 */
529static int __devexit tps_65023_remove(struct i2c_client *client)
530{
531 struct tps_pmic *tps = i2c_get_clientdata(client);
532 int i;
533
534 for (i = 0; i < TPS65023_NUM_REGULATOR; i++)
535 regulator_unregister(tps->rdev[i]);
536
90923351 537 regmap_exit(tps->regmap);
30e6599d
AA
538 kfree(tps);
539
540 return 0;
541}
542
543static const struct tps_info tps65023_regs[] = {
544 {
545 .name = "VDCDC1",
546 .min_uV = 800000,
547 .max_uV = 1600000,
548 .table_len = ARRAY_SIZE(VDCDC1_VSEL_table),
549 .table = VDCDC1_VSEL_table,
550 },
551 {
552 .name = "VDCDC2",
553 .min_uV = 3300000,
554 .max_uV = 3300000,
555 .fixed = 1,
556 },
557 {
558 .name = "VDCDC3",
559 .min_uV = 1800000,
560 .max_uV = 1800000,
561 .fixed = 1,
562 },
563 {
564 .name = "LDO1",
565 .min_uV = 1000000,
566 .max_uV = 3150000,
567 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
568 .table = LDO1_VSEL_table,
569 },
570 {
571 .name = "LDO2",
572 .min_uV = 1050000,
573 .max_uV = 3300000,
574 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
575 .table = LDO2_VSEL_table,
576 },
577};
578
9e108d33
LG
579static const struct i2c_device_id tps_65023_id[] = {
580 {.name = "tps65023",
581 .driver_data = (unsigned long) tps65023_regs,},
1880a2fc
MV
582 {.name = "tps65021",
583 .driver_data = (unsigned long) tps65023_regs,},
9e108d33 584 { },
30e6599d
AA
585};
586
587MODULE_DEVICE_TABLE(i2c, tps_65023_id);
588
589static struct i2c_driver tps_65023_i2c_driver = {
590 .driver = {
591 .name = "tps65023",
592 .owner = THIS_MODULE,
593 },
594 .probe = tps_65023_probe,
595 .remove = __devexit_p(tps_65023_remove),
9e108d33 596 .id_table = tps_65023_id,
30e6599d
AA
597};
598
599/**
600 * tps_65023_init
601 *
602 * Module init function
603 */
604static int __init tps_65023_init(void)
605{
606 return i2c_add_driver(&tps_65023_i2c_driver);
607}
608subsys_initcall(tps_65023_init);
609
610/**
611 * tps_65023_cleanup
612 *
613 * Module exit function
614 */
615static void __exit tps_65023_cleanup(void)
616{
617 i2c_del_driver(&tps_65023_i2c_driver);
618}
619module_exit(tps_65023_cleanup);
620
621MODULE_AUTHOR("Texas Instruments");
622MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
9e108d33 623MODULE_LICENSE("GPL v2");
This page took 0.203645 seconds and 5 git commands to generate.