blob: 23c0597ab1f5abe3e59c65cd152e18f2f377d45f [file] [log] [blame]
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +05301/*
2 * tps6507x-regulator.c
3 *
4 * Regulator driver for TPS65073 PMIC
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Todd Fischerd183fcc2010-04-05 20:23:56 -060028#include <linux/mfd/tps6507x.h>
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +053029
30/* DCDC's */
31#define TPS6507X_DCDC_1 0
32#define TPS6507X_DCDC_2 1
33#define TPS6507X_DCDC_3 2
34/* LDOs */
35#define TPS6507X_LDO_1 3
36#define TPS6507X_LDO_2 4
37
38#define TPS6507X_MAX_REG_ID TPS6507X_LDO_2
39
40/* Number of step-down converters available */
41#define TPS6507X_NUM_DCDC 3
42/* Number of LDO voltage regulators available */
43#define TPS6507X_NUM_LDO 2
44/* Number of total regulators available */
45#define TPS6507X_NUM_REGULATOR (TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)
46
47/* Supported voltage values for regulators (in milliVolts) */
48static const u16 VDCDCx_VSEL_table[] = {
49 725, 750, 775, 800,
50 825, 850, 875, 900,
51 925, 950, 975, 1000,
52 1025, 1050, 1075, 1100,
53 1125, 1150, 1175, 1200,
54 1225, 1250, 1275, 1300,
55 1325, 1350, 1375, 1400,
56 1425, 1450, 1475, 1500,
57 1550, 1600, 1650, 1700,
58 1750, 1800, 1850, 1900,
59 1950, 2000, 2050, 2100,
60 2150, 2200, 2250, 2300,
61 2350, 2400, 2450, 2500,
62 2550, 2600, 2650, 2700,
63 2750, 2800, 2850, 2900,
64 3000, 3100, 3200, 3300,
65};
66
67static const u16 LDO1_VSEL_table[] = {
68 1000, 1100, 1200, 1250,
69 1300, 1350, 1400, 1500,
70 1600, 1800, 2500, 2750,
71 2800, 3000, 3100, 3300,
72};
73
74static const u16 LDO2_VSEL_table[] = {
75 725, 750, 775, 800,
76 825, 850, 875, 900,
77 925, 950, 975, 1000,
78 1025, 1050, 1075, 1100,
79 1125, 1150, 1175, 1200,
80 1225, 1250, 1275, 1300,
81 1325, 1350, 1375, 1400,
82 1425, 1450, 1475, 1500,
83 1550, 1600, 1650, 1700,
84 1750, 1800, 1850, 1900,
85 1950, 2000, 2050, 2100,
86 2150, 2200, 2250, 2300,
87 2350, 2400, 2450, 2500,
88 2550, 2600, 2650, 2700,
89 2750, 2800, 2850, 2900,
90 3000, 3100, 3200, 3300,
91};
92
93static unsigned int num_voltages[] = {ARRAY_SIZE(VDCDCx_VSEL_table),
94 ARRAY_SIZE(VDCDCx_VSEL_table),
95 ARRAY_SIZE(VDCDCx_VSEL_table),
96 ARRAY_SIZE(LDO1_VSEL_table),
97 ARRAY_SIZE(LDO2_VSEL_table)};
98
99struct tps_info {
100 const char *name;
101 unsigned min_uV;
102 unsigned max_uV;
103 u8 table_len;
104 const u16 *table;
105};
106
107struct tps_pmic {
108 struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
109 struct i2c_client *client;
110 struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
111 const struct tps_info *info[TPS6507X_NUM_REGULATOR];
112 struct mutex io_lock;
113};
114
115static inline int tps_6507x_read(struct tps_pmic *tps, u8 reg)
116{
117 return i2c_smbus_read_byte_data(tps->client, reg);
118}
119
120static inline int tps_6507x_write(struct tps_pmic *tps, u8 reg, u8 val)
121{
122 return i2c_smbus_write_byte_data(tps->client, reg, val);
123}
124
125static int tps_6507x_set_bits(struct tps_pmic *tps, u8 reg, u8 mask)
126{
127 int err, data;
128
129 mutex_lock(&tps->io_lock);
130
131 data = tps_6507x_read(tps, reg);
132 if (data < 0) {
133 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
134 err = data;
135 goto out;
136 }
137
138 data |= mask;
139 err = tps_6507x_write(tps, reg, data);
140 if (err)
141 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
142
143out:
144 mutex_unlock(&tps->io_lock);
145 return err;
146}
147
148static int tps_6507x_clear_bits(struct tps_pmic *tps, u8 reg, u8 mask)
149{
150 int err, data;
151
152 mutex_lock(&tps->io_lock);
153
154 data = tps_6507x_read(tps, reg);
155 if (data < 0) {
156 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
157 err = data;
158 goto out;
159 }
160
161 data &= ~mask;
162 err = tps_6507x_write(tps, reg, data);
163 if (err)
164 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
165
166out:
167 mutex_unlock(&tps->io_lock);
168 return err;
169}
170
171static int tps_6507x_reg_read(struct tps_pmic *tps, u8 reg)
172{
173 int data;
174
175 mutex_lock(&tps->io_lock);
176
177 data = tps_6507x_read(tps, reg);
178 if (data < 0)
179 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
180
181 mutex_unlock(&tps->io_lock);
182 return data;
183}
184
185static int tps_6507x_reg_write(struct tps_pmic *tps, u8 reg, u8 val)
186{
187 int err;
188
189 mutex_lock(&tps->io_lock);
190
191 err = tps_6507x_write(tps, reg, val);
192 if (err < 0)
193 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
194
195 mutex_unlock(&tps->io_lock);
196 return err;
197}
198
199static int tps6507x_dcdc_is_enabled(struct regulator_dev *dev)
200{
201 struct tps_pmic *tps = rdev_get_drvdata(dev);
202 int data, dcdc = rdev_get_id(dev);
203 u8 shift;
204
205 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
206 return -EINVAL;
207
208 shift = TPS6507X_MAX_REG_ID - dcdc;
209 data = tps_6507x_reg_read(tps, TPS6507X_REG_CON_CTRL1);
210
211 if (data < 0)
212 return data;
213 else
214 return (data & 1<<shift) ? 1 : 0;
215}
216
217static int tps6507x_ldo_is_enabled(struct regulator_dev *dev)
218{
219 struct tps_pmic *tps = rdev_get_drvdata(dev);
220 int data, ldo = rdev_get_id(dev);
221 u8 shift;
222
223 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
224 return -EINVAL;
225
226 shift = TPS6507X_MAX_REG_ID - ldo;
227 data = tps_6507x_reg_read(tps, TPS6507X_REG_CON_CTRL1);
228
229 if (data < 0)
230 return data;
231 else
232 return (data & 1<<shift) ? 1 : 0;
233}
234
235static int tps6507x_dcdc_enable(struct regulator_dev *dev)
236{
237 struct tps_pmic *tps = rdev_get_drvdata(dev);
238 int dcdc = rdev_get_id(dev);
239 u8 shift;
240
241 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
242 return -EINVAL;
243
244 shift = TPS6507X_MAX_REG_ID - dcdc;
245 return tps_6507x_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
246}
247
248static int tps6507x_dcdc_disable(struct regulator_dev *dev)
249{
250 struct tps_pmic *tps = rdev_get_drvdata(dev);
251 int dcdc = rdev_get_id(dev);
252 u8 shift;
253
254 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
255 return -EINVAL;
256
257 shift = TPS6507X_MAX_REG_ID - dcdc;
258 return tps_6507x_clear_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
259}
260
261static int tps6507x_ldo_enable(struct regulator_dev *dev)
262{
263 struct tps_pmic *tps = rdev_get_drvdata(dev);
264 int ldo = rdev_get_id(dev);
265 u8 shift;
266
267 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
268 return -EINVAL;
269
270 shift = TPS6507X_MAX_REG_ID - ldo;
271 return tps_6507x_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
272}
273
274static int tps6507x_ldo_disable(struct regulator_dev *dev)
275{
276 struct tps_pmic *tps = rdev_get_drvdata(dev);
277 int ldo = rdev_get_id(dev);
278 u8 shift;
279
280 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
281 return -EINVAL;
282
283 shift = TPS6507X_MAX_REG_ID - ldo;
284 return tps_6507x_clear_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
285}
286
287static int tps6507x_dcdc_get_voltage(struct regulator_dev *dev)
288{
289 struct tps_pmic *tps = rdev_get_drvdata(dev);
290 int data, dcdc = rdev_get_id(dev);
291 u8 reg;
292
293 switch (dcdc) {
294 case TPS6507X_DCDC_1:
295 reg = TPS6507X_REG_DEFDCDC1;
296 break;
297 case TPS6507X_DCDC_2:
298 reg = TPS6507X_REG_DEFDCDC2_LOW;
299 break;
300 case TPS6507X_DCDC_3:
301 reg = TPS6507X_REG_DEFDCDC3_LOW;
302 break;
303 default:
304 return -EINVAL;
305 }
306
307 data = tps_6507x_reg_read(tps, reg);
308 if (data < 0)
309 return data;
310
311 data &= TPS6507X_DEFDCDCX_DCDC_MASK;
312 return tps->info[dcdc]->table[data] * 1000;
313}
314
315static int tps6507x_dcdc_set_voltage(struct regulator_dev *dev,
316 int min_uV, int max_uV)
317{
318 struct tps_pmic *tps = rdev_get_drvdata(dev);
319 int data, vsel, dcdc = rdev_get_id(dev);
320 u8 reg;
321
322 switch (dcdc) {
323 case TPS6507X_DCDC_1:
324 reg = TPS6507X_REG_DEFDCDC1;
325 break;
326 case TPS6507X_DCDC_2:
327 reg = TPS6507X_REG_DEFDCDC2_LOW;
328 break;
329 case TPS6507X_DCDC_3:
330 reg = TPS6507X_REG_DEFDCDC3_LOW;
331 break;
332 default:
333 return -EINVAL;
334 }
335
336 if (min_uV < tps->info[dcdc]->min_uV
337 || min_uV > tps->info[dcdc]->max_uV)
338 return -EINVAL;
339 if (max_uV < tps->info[dcdc]->min_uV
340 || max_uV > tps->info[dcdc]->max_uV)
341 return -EINVAL;
342
343 for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
344 int mV = tps->info[dcdc]->table[vsel];
345 int uV = mV * 1000;
346
347 /* Break at the first in-range value */
348 if (min_uV <= uV && uV <= max_uV)
349 break;
350 }
351
352 /* write to the register in case we found a match */
353 if (vsel == tps->info[dcdc]->table_len)
354 return -EINVAL;
355
356 data = tps_6507x_reg_read(tps, reg);
357 if (data < 0)
358 return data;
359
360 data &= ~TPS6507X_DEFDCDCX_DCDC_MASK;
361 data |= vsel;
362
363 return tps_6507x_reg_write(tps, reg, data);
364}
365
366static int tps6507x_ldo_get_voltage(struct regulator_dev *dev)
367{
368 struct tps_pmic *tps = rdev_get_drvdata(dev);
369 int data, ldo = rdev_get_id(dev);
370 u8 reg, mask;
371
372 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
373 return -EINVAL;
374 else {
375 reg = (ldo == TPS6507X_LDO_1 ?
376 TPS6507X_REG_LDO_CTRL1 : TPS6507X_REG_DEFLDO2);
377 mask = (ldo == TPS6507X_LDO_1 ?
378 TPS6507X_REG_LDO_CTRL1_LDO1_MASK :
379 TPS6507X_REG_DEFLDO2_LDO2_MASK);
380 }
381
382 data = tps_6507x_reg_read(tps, reg);
383 if (data < 0)
384 return data;
385
386 data &= mask;
387 return tps->info[ldo]->table[data] * 1000;
388}
389
390static int tps6507x_ldo_set_voltage(struct regulator_dev *dev,
391 int min_uV, int max_uV)
392{
393 struct tps_pmic *tps = rdev_get_drvdata(dev);
394 int data, vsel, ldo = rdev_get_id(dev);
395 u8 reg, mask;
396
397 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
398 return -EINVAL;
399 else {
400 reg = (ldo == TPS6507X_LDO_1 ?
401 TPS6507X_REG_LDO_CTRL1 : TPS6507X_REG_DEFLDO2);
402 mask = (ldo == TPS6507X_LDO_1 ?
403 TPS6507X_REG_LDO_CTRL1_LDO1_MASK :
404 TPS6507X_REG_DEFLDO2_LDO2_MASK);
405 }
406
407 if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
408 return -EINVAL;
409 if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
410 return -EINVAL;
411
412 for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
413 int mV = tps->info[ldo]->table[vsel];
414 int uV = mV * 1000;
415
416 /* Break at the first in-range value */
417 if (min_uV <= uV && uV <= max_uV)
418 break;
419 }
420
421 if (vsel == tps->info[ldo]->table_len)
422 return -EINVAL;
423
424 data = tps_6507x_reg_read(tps, reg);
425 if (data < 0)
426 return data;
427
428 data &= ~mask;
429 data |= vsel;
430
431 return tps_6507x_reg_write(tps, reg, data);
432}
433
434static int tps6507x_dcdc_list_voltage(struct regulator_dev *dev,
435 unsigned selector)
436{
437 struct tps_pmic *tps = rdev_get_drvdata(dev);
438 int dcdc = rdev_get_id(dev);
439
440 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
441 return -EINVAL;
442
443 if (selector >= tps->info[dcdc]->table_len)
444 return -EINVAL;
445 else
446 return tps->info[dcdc]->table[selector] * 1000;
447}
448
449static int tps6507x_ldo_list_voltage(struct regulator_dev *dev,
450 unsigned selector)
451{
452 struct tps_pmic *tps = rdev_get_drvdata(dev);
453 int ldo = rdev_get_id(dev);
454
455 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
456 return -EINVAL;
457
458 if (selector >= tps->info[ldo]->table_len)
459 return -EINVAL;
460 else
461 return tps->info[ldo]->table[selector] * 1000;
462}
463
464/* Operations permitted on VDCDCx */
465static struct regulator_ops tps6507x_dcdc_ops = {
466 .is_enabled = tps6507x_dcdc_is_enabled,
467 .enable = tps6507x_dcdc_enable,
468 .disable = tps6507x_dcdc_disable,
469 .get_voltage = tps6507x_dcdc_get_voltage,
470 .set_voltage = tps6507x_dcdc_set_voltage,
471 .list_voltage = tps6507x_dcdc_list_voltage,
472};
473
474/* Operations permitted on LDOx */
475static struct regulator_ops tps6507x_ldo_ops = {
476 .is_enabled = tps6507x_ldo_is_enabled,
477 .enable = tps6507x_ldo_enable,
478 .disable = tps6507x_ldo_disable,
479 .get_voltage = tps6507x_ldo_get_voltage,
480 .set_voltage = tps6507x_ldo_set_voltage,
481 .list_voltage = tps6507x_ldo_list_voltage,
482};
483
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800484static int __devinit tps_6507x_probe(struct i2c_client *client,
485 const struct i2c_device_id *id)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530486{
487 static int desc_id;
488 const struct tps_info *info = (void *)id->driver_data;
489 struct regulator_init_data *init_data;
490 struct regulator_dev *rdev;
491 struct tps_pmic *tps;
492 int i;
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800493 int error;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530494
495 if (!i2c_check_functionality(client->adapter,
496 I2C_FUNC_SMBUS_BYTE_DATA))
497 return -EIO;
498
499 /**
500 * init_data points to array of regulator_init structures
501 * coming from the board-evm file.
502 */
503 init_data = client->dev.platform_data;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530504 if (!init_data)
505 return -EIO;
506
507 tps = kzalloc(sizeof(*tps), GFP_KERNEL);
508 if (!tps)
509 return -ENOMEM;
510
511 mutex_init(&tps->io_lock);
512
513 /* common for all regulators */
514 tps->client = client;
515
516 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
517 /* Register the regulators */
518 tps->info[i] = info;
519 tps->desc[i].name = info->name;
520 tps->desc[i].id = desc_id++;
521 tps->desc[i].n_voltages = num_voltages[i];
522 tps->desc[i].ops = (i > TPS6507X_DCDC_3 ?
523 &tps6507x_ldo_ops : &tps6507x_dcdc_ops);
524 tps->desc[i].type = REGULATOR_VOLTAGE;
525 tps->desc[i].owner = THIS_MODULE;
526
527 rdev = regulator_register(&tps->desc[i],
528 &client->dev, init_data, tps);
529 if (IS_ERR(rdev)) {
530 dev_err(&client->dev, "failed to register %s\n",
531 id->name);
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800532 error = PTR_ERR(rdev);
533 goto fail;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530534 }
535
536 /* Save regulator for cleanup */
537 tps->rdev[i] = rdev;
538 }
539
540 i2c_set_clientdata(client, tps);
541
542 return 0;
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800543
544fail:
545 while (--i >= 0)
546 regulator_unregister(tps->rdev[i]);
547
548 kfree(tps);
549 return error;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530550}
551
552/**
553 * tps_6507x_remove - TPS6507x driver i2c remove handler
554 * @client: i2c driver client device structure
555 *
556 * Unregister TPS driver as an i2c client device driver
557 */
558static int __devexit tps_6507x_remove(struct i2c_client *client)
559{
560 struct tps_pmic *tps = i2c_get_clientdata(client);
561 int i;
562
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800563 /* clear the client data in i2c */
564 i2c_set_clientdata(client, NULL);
565
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530566 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
567 regulator_unregister(tps->rdev[i]);
568
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530569 kfree(tps);
570
571 return 0;
572}
573
574static const struct tps_info tps6507x_regs[] = {
575 {
576 .name = "VDCDC1",
577 .min_uV = 725000,
578 .max_uV = 3300000,
579 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
580 .table = VDCDCx_VSEL_table,
581 },
582 {
583 .name = "VDCDC2",
584 .min_uV = 725000,
585 .max_uV = 3300000,
586 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
587 .table = VDCDCx_VSEL_table,
588 },
589 {
590 .name = "VDCDC3",
591 .min_uV = 725000,
592 .max_uV = 3300000,
593 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
594 .table = VDCDCx_VSEL_table,
595 },
596 {
597 .name = "LDO1",
598 .min_uV = 1000000,
599 .max_uV = 3300000,
600 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
601 .table = LDO1_VSEL_table,
602 },
603 {
604 .name = "LDO2",
605 .min_uV = 725000,
606 .max_uV = 3300000,
607 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
608 .table = LDO2_VSEL_table,
609 },
610};
611
Liam Girdwood9e108d32009-08-24 10:31:34 +0100612static const struct i2c_device_id tps_6507x_id[] = {
613 {.name = "tps6507x",
614 .driver_data = (unsigned long) tps6507x_regs,},
615 { },
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530616};
617MODULE_DEVICE_TABLE(i2c, tps_6507x_id);
618
619static struct i2c_driver tps_6507x_i2c_driver = {
620 .driver = {
621 .name = "tps6507x",
622 .owner = THIS_MODULE,
623 },
624 .probe = tps_6507x_probe,
625 .remove = __devexit_p(tps_6507x_remove),
Liam Girdwood9e108d32009-08-24 10:31:34 +0100626 .id_table = tps_6507x_id,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530627};
628
629/**
630 * tps_6507x_init
631 *
632 * Module init function
633 */
634static int __init tps_6507x_init(void)
635{
636 return i2c_add_driver(&tps_6507x_i2c_driver);
637}
638subsys_initcall(tps_6507x_init);
639
640/**
641 * tps_6507x_cleanup
642 *
643 * Module exit function
644 */
645static void __exit tps_6507x_cleanup(void)
646{
647 i2c_del_driver(&tps_6507x_i2c_driver);
648}
649module_exit(tps_6507x_cleanup);
650
651MODULE_AUTHOR("Texas Instruments");
652MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
Liam Girdwood9e108d32009-08-24 10:31:34 +0100653MODULE_LICENSE("GPL v2");