4 * Regulator driver for TPS65073 PMIC
6 * Copyright (C) 2009 Texas Instrument Incorporated - http://www.ti.com/
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.
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.
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>
27 #include <linux/slab.h>
29 /* Register definitions */
30 #define TPS6507X_REG_PPATH1 0X01
31 #define TPS6507X_REG_INT 0X02
32 #define TPS6507X_REG_CHGCONFIG0 0X03
33 #define TPS6507X_REG_CHGCONFIG1 0X04
34 #define TPS6507X_REG_CHGCONFIG2 0X05
35 #define TPS6507X_REG_CHGCONFIG3 0X06
36 #define TPS6507X_REG_REG_ADCONFIG 0X07
37 #define TPS6507X_REG_TSCMODE 0X08
38 #define TPS6507X_REG_ADRESULT_1 0X09
39 #define TPS6507X_REG_ADRESULT_2 0X0A
40 #define TPS6507X_REG_PGOOD 0X0B
41 #define TPS6507X_REG_PGOODMASK 0X0C
42 #define TPS6507X_REG_CON_CTRL1 0X0D
43 #define TPS6507X_REG_CON_CTRL2 0X0E
44 #define TPS6507X_REG_CON_CTRL3 0X0F
45 #define TPS6507X_REG_DEFDCDC1 0X10
46 #define TPS6507X_REG_DEFDCDC2_LOW 0X11
47 #define TPS6507X_REG_DEFDCDC2_HIGH 0X12
48 #define TPS6507X_REG_DEFDCDC3_LOW 0X13
49 #define TPS6507X_REG_DEFDCDC3_HIGH 0X14
50 #define TPS6507X_REG_DEFSLEW 0X15
51 #define TPS6507X_REG_LDO_CTRL1 0X16
52 #define TPS6507X_REG_DEFLDO2 0X17
53 #define TPS6507X_REG_WLED_CTRL1 0X18
54 #define TPS6507X_REG_WLED_CTRL2 0X19
56 /* CON_CTRL1 bitfields */
57 #define TPS6507X_CON_CTRL1_DCDC1_ENABLE BIT(4)
58 #define TPS6507X_CON_CTRL1_DCDC2_ENABLE BIT(3)
59 #define TPS6507X_CON_CTRL1_DCDC3_ENABLE BIT(2)
60 #define TPS6507X_CON_CTRL1_LDO1_ENABLE BIT(1)
61 #define TPS6507X_CON_CTRL1_LDO2_ENABLE BIT(0)
63 /* DEFDCDC1 bitfields */
64 #define TPS6507X_DEFDCDC1_DCDC1_EXT_ADJ_EN BIT(7)
65 #define TPS6507X_DEFDCDC1_DCDC1_MASK 0X3F
67 /* DEFDCDC2_LOW bitfields */
68 #define TPS6507X_DEFDCDC2_LOW_DCDC2_MASK 0X3F
70 /* DEFDCDC2_HIGH bitfields */
71 #define TPS6507X_DEFDCDC2_HIGH_DCDC2_MASK 0X3F
73 /* DEFDCDC3_LOW bitfields */
74 #define TPS6507X_DEFDCDC3_LOW_DCDC3_MASK 0X3F
76 /* DEFDCDC3_HIGH bitfields */
77 #define TPS6507X_DEFDCDC3_HIGH_DCDC3_MASK 0X3F
79 /* TPS6507X_REG_LDO_CTRL1 bitfields */
80 #define TPS6507X_REG_LDO_CTRL1_LDO1_MASK 0X0F
82 /* TPS6507X_REG_DEFLDO2 bitfields */
83 #define TPS6507X_REG_DEFLDO2_LDO2_MASK 0X3F
86 #define TPS6507X_DEFDCDCX_DCDC_MASK 0X3F
89 #define TPS6507X_DCDC_1 0
90 #define TPS6507X_DCDC_2 1
91 #define TPS6507X_DCDC_3 2
93 #define TPS6507X_LDO_1 3
94 #define TPS6507X_LDO_2 4
96 #define TPS6507X_MAX_REG_ID TPS6507X_LDO_2
98 /* Number of step-down converters available */
99 #define TPS6507X_NUM_DCDC 3
100 /* Number of LDO voltage regulators available */
101 #define TPS6507X_NUM_LDO 2
102 /* Number of total regulators available */
103 #define TPS6507X_NUM_REGULATOR (TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)
105 /* Supported voltage values for regulators (in milliVolts) */
106 static const u16 VDCDCx_VSEL_table
[] = {
110 1025, 1050, 1075, 1100,
111 1125, 1150, 1175, 1200,
112 1225, 1250, 1275, 1300,
113 1325, 1350, 1375, 1400,
114 1425, 1450, 1475, 1500,
115 1550, 1600, 1650, 1700,
116 1750, 1800, 1850, 1900,
117 1950, 2000, 2050, 2100,
118 2150, 2200, 2250, 2300,
119 2350, 2400, 2450, 2500,
120 2550, 2600, 2650, 2700,
121 2750, 2800, 2850, 2900,
122 3000, 3100, 3200, 3300,
125 static const u16 LDO1_VSEL_table
[] = {
126 1000, 1100, 1200, 1250,
127 1300, 1350, 1400, 1500,
128 1600, 1800, 2500, 2750,
129 2800, 3000, 3100, 3300,
132 static const u16 LDO2_VSEL_table
[] = {
136 1025, 1050, 1075, 1100,
137 1125, 1150, 1175, 1200,
138 1225, 1250, 1275, 1300,
139 1325, 1350, 1375, 1400,
140 1425, 1450, 1475, 1500,
141 1550, 1600, 1650, 1700,
142 1750, 1800, 1850, 1900,
143 1950, 2000, 2050, 2100,
144 2150, 2200, 2250, 2300,
145 2350, 2400, 2450, 2500,
146 2550, 2600, 2650, 2700,
147 2750, 2800, 2850, 2900,
148 3000, 3100, 3200, 3300,
151 static unsigned int num_voltages
[] = {ARRAY_SIZE(VDCDCx_VSEL_table
),
152 ARRAY_SIZE(VDCDCx_VSEL_table
),
153 ARRAY_SIZE(VDCDCx_VSEL_table
),
154 ARRAY_SIZE(LDO1_VSEL_table
),
155 ARRAY_SIZE(LDO2_VSEL_table
)};
166 struct regulator_desc desc
[TPS6507X_NUM_REGULATOR
];
167 struct i2c_client
*client
;
168 struct regulator_dev
*rdev
[TPS6507X_NUM_REGULATOR
];
169 const struct tps_info
*info
[TPS6507X_NUM_REGULATOR
];
170 struct mutex io_lock
;
173 static inline int tps_6507x_read(struct tps_pmic
*tps
, u8 reg
)
175 return i2c_smbus_read_byte_data(tps
->client
, reg
);
178 static inline int tps_6507x_write(struct tps_pmic
*tps
, u8 reg
, u8 val
)
180 return i2c_smbus_write_byte_data(tps
->client
, reg
, val
);
183 static int tps_6507x_set_bits(struct tps_pmic
*tps
, u8 reg
, u8 mask
)
187 mutex_lock(&tps
->io_lock
);
189 data
= tps_6507x_read(tps
, reg
);
191 dev_err(&tps
->client
->dev
, "Read from reg 0x%x failed\n", reg
);
197 err
= tps_6507x_write(tps
, reg
, data
);
199 dev_err(&tps
->client
->dev
, "Write for reg 0x%x failed\n", reg
);
202 mutex_unlock(&tps
->io_lock
);
206 static int tps_6507x_clear_bits(struct tps_pmic
*tps
, u8 reg
, u8 mask
)
210 mutex_lock(&tps
->io_lock
);
212 data
= tps_6507x_read(tps
, reg
);
214 dev_err(&tps
->client
->dev
, "Read from reg 0x%x failed\n", reg
);
220 err
= tps_6507x_write(tps
, reg
, data
);
222 dev_err(&tps
->client
->dev
, "Write for reg 0x%x failed\n", reg
);
225 mutex_unlock(&tps
->io_lock
);
229 static int tps_6507x_reg_read(struct tps_pmic
*tps
, u8 reg
)
233 mutex_lock(&tps
->io_lock
);
235 data
= tps_6507x_read(tps
, reg
);
237 dev_err(&tps
->client
->dev
, "Read from reg 0x%x failed\n", reg
);
239 mutex_unlock(&tps
->io_lock
);
243 static int tps_6507x_reg_write(struct tps_pmic
*tps
, u8 reg
, u8 val
)
247 mutex_lock(&tps
->io_lock
);
249 err
= tps_6507x_write(tps
, reg
, val
);
251 dev_err(&tps
->client
->dev
, "Write for reg 0x%x failed\n", reg
);
253 mutex_unlock(&tps
->io_lock
);
257 static int tps6507x_dcdc_is_enabled(struct regulator_dev
*dev
)
259 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
260 int data
, dcdc
= rdev_get_id(dev
);
263 if (dcdc
< TPS6507X_DCDC_1
|| dcdc
> TPS6507X_DCDC_3
)
266 shift
= TPS6507X_MAX_REG_ID
- dcdc
;
267 data
= tps_6507x_reg_read(tps
, TPS6507X_REG_CON_CTRL1
);
272 return (data
& 1<<shift
) ? 1 : 0;
275 static int tps6507x_ldo_is_enabled(struct regulator_dev
*dev
)
277 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
278 int data
, ldo
= rdev_get_id(dev
);
281 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
284 shift
= TPS6507X_MAX_REG_ID
- ldo
;
285 data
= tps_6507x_reg_read(tps
, TPS6507X_REG_CON_CTRL1
);
290 return (data
& 1<<shift
) ? 1 : 0;
293 static int tps6507x_dcdc_enable(struct regulator_dev
*dev
)
295 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
296 int dcdc
= rdev_get_id(dev
);
299 if (dcdc
< TPS6507X_DCDC_1
|| dcdc
> TPS6507X_DCDC_3
)
302 shift
= TPS6507X_MAX_REG_ID
- dcdc
;
303 return tps_6507x_set_bits(tps
, TPS6507X_REG_CON_CTRL1
, 1 << shift
);
306 static int tps6507x_dcdc_disable(struct regulator_dev
*dev
)
308 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
309 int dcdc
= rdev_get_id(dev
);
312 if (dcdc
< TPS6507X_DCDC_1
|| dcdc
> TPS6507X_DCDC_3
)
315 shift
= TPS6507X_MAX_REG_ID
- dcdc
;
316 return tps_6507x_clear_bits(tps
, TPS6507X_REG_CON_CTRL1
, 1 << shift
);
319 static int tps6507x_ldo_enable(struct regulator_dev
*dev
)
321 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
322 int ldo
= rdev_get_id(dev
);
325 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
328 shift
= TPS6507X_MAX_REG_ID
- ldo
;
329 return tps_6507x_set_bits(tps
, TPS6507X_REG_CON_CTRL1
, 1 << shift
);
332 static int tps6507x_ldo_disable(struct regulator_dev
*dev
)
334 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
335 int ldo
= rdev_get_id(dev
);
338 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
341 shift
= TPS6507X_MAX_REG_ID
- ldo
;
342 return tps_6507x_clear_bits(tps
, TPS6507X_REG_CON_CTRL1
, 1 << shift
);
345 static int tps6507x_dcdc_get_voltage(struct regulator_dev
*dev
)
347 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
348 int data
, dcdc
= rdev_get_id(dev
);
352 case TPS6507X_DCDC_1
:
353 reg
= TPS6507X_REG_DEFDCDC1
;
355 case TPS6507X_DCDC_2
:
356 reg
= TPS6507X_REG_DEFDCDC2_LOW
;
358 case TPS6507X_DCDC_3
:
359 reg
= TPS6507X_REG_DEFDCDC3_LOW
;
365 data
= tps_6507x_reg_read(tps
, reg
);
369 data
&= TPS6507X_DEFDCDCX_DCDC_MASK
;
370 return tps
->info
[dcdc
]->table
[data
] * 1000;
373 static int tps6507x_dcdc_set_voltage(struct regulator_dev
*dev
,
374 int min_uV
, int max_uV
)
376 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
377 int data
, vsel
, dcdc
= rdev_get_id(dev
);
381 case TPS6507X_DCDC_1
:
382 reg
= TPS6507X_REG_DEFDCDC1
;
384 case TPS6507X_DCDC_2
:
385 reg
= TPS6507X_REG_DEFDCDC2_LOW
;
387 case TPS6507X_DCDC_3
:
388 reg
= TPS6507X_REG_DEFDCDC3_LOW
;
394 if (min_uV
< tps
->info
[dcdc
]->min_uV
395 || min_uV
> tps
->info
[dcdc
]->max_uV
)
397 if (max_uV
< tps
->info
[dcdc
]->min_uV
398 || max_uV
> tps
->info
[dcdc
]->max_uV
)
401 for (vsel
= 0; vsel
< tps
->info
[dcdc
]->table_len
; vsel
++) {
402 int mV
= tps
->info
[dcdc
]->table
[vsel
];
405 /* Break at the first in-range value */
406 if (min_uV
<= uV
&& uV
<= max_uV
)
410 /* write to the register in case we found a match */
411 if (vsel
== tps
->info
[dcdc
]->table_len
)
414 data
= tps_6507x_reg_read(tps
, reg
);
418 data
&= ~TPS6507X_DEFDCDCX_DCDC_MASK
;
421 return tps_6507x_reg_write(tps
, reg
, data
);
424 static int tps6507x_ldo_get_voltage(struct regulator_dev
*dev
)
426 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
427 int data
, ldo
= rdev_get_id(dev
);
430 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
433 reg
= (ldo
== TPS6507X_LDO_1
?
434 TPS6507X_REG_LDO_CTRL1
: TPS6507X_REG_DEFLDO2
);
435 mask
= (ldo
== TPS6507X_LDO_1
?
436 TPS6507X_REG_LDO_CTRL1_LDO1_MASK
:
437 TPS6507X_REG_DEFLDO2_LDO2_MASK
);
440 data
= tps_6507x_reg_read(tps
, reg
);
445 return tps
->info
[ldo
]->table
[data
] * 1000;
448 static int tps6507x_ldo_set_voltage(struct regulator_dev
*dev
,
449 int min_uV
, int max_uV
)
451 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
452 int data
, vsel
, ldo
= rdev_get_id(dev
);
455 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
458 reg
= (ldo
== TPS6507X_LDO_1
?
459 TPS6507X_REG_LDO_CTRL1
: TPS6507X_REG_DEFLDO2
);
460 mask
= (ldo
== TPS6507X_LDO_1
?
461 TPS6507X_REG_LDO_CTRL1_LDO1_MASK
:
462 TPS6507X_REG_DEFLDO2_LDO2_MASK
);
465 if (min_uV
< tps
->info
[ldo
]->min_uV
|| min_uV
> tps
->info
[ldo
]->max_uV
)
467 if (max_uV
< tps
->info
[ldo
]->min_uV
|| max_uV
> tps
->info
[ldo
]->max_uV
)
470 for (vsel
= 0; vsel
< tps
->info
[ldo
]->table_len
; vsel
++) {
471 int mV
= tps
->info
[ldo
]->table
[vsel
];
474 /* Break at the first in-range value */
475 if (min_uV
<= uV
&& uV
<= max_uV
)
479 if (vsel
== tps
->info
[ldo
]->table_len
)
482 data
= tps_6507x_reg_read(tps
, reg
);
489 return tps_6507x_reg_write(tps
, reg
, data
);
492 static int tps6507x_dcdc_list_voltage(struct regulator_dev
*dev
,
495 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
496 int dcdc
= rdev_get_id(dev
);
498 if (dcdc
< TPS6507X_DCDC_1
|| dcdc
> TPS6507X_DCDC_3
)
501 if (selector
>= tps
->info
[dcdc
]->table_len
)
504 return tps
->info
[dcdc
]->table
[selector
] * 1000;
507 static int tps6507x_ldo_list_voltage(struct regulator_dev
*dev
,
510 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
511 int ldo
= rdev_get_id(dev
);
513 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
516 if (selector
>= tps
->info
[ldo
]->table_len
)
519 return tps
->info
[ldo
]->table
[selector
] * 1000;
522 /* Operations permitted on VDCDCx */
523 static struct regulator_ops tps6507x_dcdc_ops
= {
524 .is_enabled
= tps6507x_dcdc_is_enabled
,
525 .enable
= tps6507x_dcdc_enable
,
526 .disable
= tps6507x_dcdc_disable
,
527 .get_voltage
= tps6507x_dcdc_get_voltage
,
528 .set_voltage
= tps6507x_dcdc_set_voltage
,
529 .list_voltage
= tps6507x_dcdc_list_voltage
,
532 /* Operations permitted on LDOx */
533 static struct regulator_ops tps6507x_ldo_ops
= {
534 .is_enabled
= tps6507x_ldo_is_enabled
,
535 .enable
= tps6507x_ldo_enable
,
536 .disable
= tps6507x_ldo_disable
,
537 .get_voltage
= tps6507x_ldo_get_voltage
,
538 .set_voltage
= tps6507x_ldo_set_voltage
,
539 .list_voltage
= tps6507x_ldo_list_voltage
,
542 static int __devinit
tps_6507x_probe(struct i2c_client
*client
,
543 const struct i2c_device_id
*id
)
546 const struct tps_info
*info
= (void *)id
->driver_data
;
547 struct regulator_init_data
*init_data
;
548 struct regulator_dev
*rdev
;
549 struct tps_pmic
*tps
;
553 if (!i2c_check_functionality(client
->adapter
,
554 I2C_FUNC_SMBUS_BYTE_DATA
))
558 * init_data points to array of regulator_init structures
559 * coming from the board-evm file.
561 init_data
= client
->dev
.platform_data
;
565 tps
= kzalloc(sizeof(*tps
), GFP_KERNEL
);
569 mutex_init(&tps
->io_lock
);
571 /* common for all regulators */
572 tps
->client
= client
;
574 for (i
= 0; i
< TPS6507X_NUM_REGULATOR
; i
++, info
++, init_data
++) {
575 /* Register the regulators */
577 tps
->desc
[i
].name
= info
->name
;
578 tps
->desc
[i
].id
= desc_id
++;
579 tps
->desc
[i
].n_voltages
= num_voltages
[i
];
580 tps
->desc
[i
].ops
= (i
> TPS6507X_DCDC_3
?
581 &tps6507x_ldo_ops
: &tps6507x_dcdc_ops
);
582 tps
->desc
[i
].type
= REGULATOR_VOLTAGE
;
583 tps
->desc
[i
].owner
= THIS_MODULE
;
585 rdev
= regulator_register(&tps
->desc
[i
],
586 &client
->dev
, init_data
, tps
);
588 dev_err(&client
->dev
, "failed to register %s\n",
590 error
= PTR_ERR(rdev
);
594 /* Save regulator for cleanup */
598 i2c_set_clientdata(client
, tps
);
604 regulator_unregister(tps
->rdev
[i
]);
611 * tps_6507x_remove - TPS6507x driver i2c remove handler
612 * @client: i2c driver client device structure
614 * Unregister TPS driver as an i2c client device driver
616 static int __devexit
tps_6507x_remove(struct i2c_client
*client
)
618 struct tps_pmic
*tps
= i2c_get_clientdata(client
);
621 /* clear the client data in i2c */
622 i2c_set_clientdata(client
, NULL
);
624 for (i
= 0; i
< TPS6507X_NUM_REGULATOR
; i
++)
625 regulator_unregister(tps
->rdev
[i
]);
632 static const struct tps_info tps6507x_regs
[] = {
637 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
638 .table
= VDCDCx_VSEL_table
,
644 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
645 .table
= VDCDCx_VSEL_table
,
651 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
652 .table
= VDCDCx_VSEL_table
,
658 .table_len
= ARRAY_SIZE(LDO1_VSEL_table
),
659 .table
= LDO1_VSEL_table
,
665 .table_len
= ARRAY_SIZE(LDO2_VSEL_table
),
666 .table
= LDO2_VSEL_table
,
670 static const struct i2c_device_id tps_6507x_id
[] = {
672 .driver_data
= (unsigned long) tps6507x_regs
,},
675 MODULE_DEVICE_TABLE(i2c
, tps_6507x_id
);
677 static struct i2c_driver tps_6507x_i2c_driver
= {
680 .owner
= THIS_MODULE
,
682 .probe
= tps_6507x_probe
,
683 .remove
= __devexit_p(tps_6507x_remove
),
684 .id_table
= tps_6507x_id
,
690 * Module init function
692 static int __init
tps_6507x_init(void)
694 return i2c_add_driver(&tps_6507x_i2c_driver
);
696 subsys_initcall(tps_6507x_init
);
701 * Module exit function
703 static void __exit
tps_6507x_cleanup(void)
705 i2c_del_driver(&tps_6507x_i2c_driver
);
707 module_exit(tps_6507x_cleanup
);
709 MODULE_AUTHOR("Texas Instruments");
710 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
711 MODULE_LICENSE("GPL v2");