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>
28 /* Register definitions */
29 #define TPS6507X_REG_PPATH1 0X01
30 #define TPS6507X_REG_INT 0X02
31 #define TPS6507X_REG_CHGCONFIG0 0X03
32 #define TPS6507X_REG_CHGCONFIG1 0X04
33 #define TPS6507X_REG_CHGCONFIG2 0X05
34 #define TPS6507X_REG_CHGCONFIG3 0X06
35 #define TPS6507X_REG_REG_ADCONFIG 0X07
36 #define TPS6507X_REG_TSCMODE 0X08
37 #define TPS6507X_REG_ADRESULT_1 0X09
38 #define TPS6507X_REG_ADRESULT_2 0X0A
39 #define TPS6507X_REG_PGOOD 0X0B
40 #define TPS6507X_REG_PGOODMASK 0X0C
41 #define TPS6507X_REG_CON_CTRL1 0X0D
42 #define TPS6507X_REG_CON_CTRL2 0X0E
43 #define TPS6507X_REG_CON_CTRL3 0X0F
44 #define TPS6507X_REG_DEFDCDC1 0X10
45 #define TPS6507X_REG_DEFDCDC2_LOW 0X11
46 #define TPS6507X_REG_DEFDCDC2_HIGH 0X12
47 #define TPS6507X_REG_DEFDCDC3_LOW 0X13
48 #define TPS6507X_REG_DEFDCDC3_HIGH 0X14
49 #define TPS6507X_REG_DEFSLEW 0X15
50 #define TPS6507X_REG_LDO_CTRL1 0X16
51 #define TPS6507X_REG_DEFLDO2 0X17
52 #define TPS6507X_REG_WLED_CTRL1 0X18
53 #define TPS6507X_REG_WLED_CTRL2 0X19
55 /* CON_CTRL1 bitfields */
56 #define TPS6507X_CON_CTRL1_DCDC1_ENABLE BIT(4)
57 #define TPS6507X_CON_CTRL1_DCDC2_ENABLE BIT(3)
58 #define TPS6507X_CON_CTRL1_DCDC3_ENABLE BIT(2)
59 #define TPS6507X_CON_CTRL1_LDO1_ENABLE BIT(1)
60 #define TPS6507X_CON_CTRL1_LDO2_ENABLE BIT(0)
62 /* DEFDCDC1 bitfields */
63 #define TPS6507X_DEFDCDC1_DCDC1_EXT_ADJ_EN BIT(7)
64 #define TPS6507X_DEFDCDC1_DCDC1_MASK 0X3F
66 /* DEFDCDC2_LOW bitfields */
67 #define TPS6507X_DEFDCDC2_LOW_DCDC2_MASK 0X3F
69 /* DEFDCDC2_HIGH bitfields */
70 #define TPS6507X_DEFDCDC2_HIGH_DCDC2_MASK 0X3F
72 /* DEFDCDC3_LOW bitfields */
73 #define TPS6507X_DEFDCDC3_LOW_DCDC3_MASK 0X3F
75 /* DEFDCDC3_HIGH bitfields */
76 #define TPS6507X_DEFDCDC3_HIGH_DCDC3_MASK 0X3F
78 /* TPS6507X_REG_LDO_CTRL1 bitfields */
79 #define TPS6507X_REG_LDO_CTRL1_LDO1_MASK 0X0F
81 /* TPS6507X_REG_DEFLDO2 bitfields */
82 #define TPS6507X_REG_DEFLDO2_LDO2_MASK 0X3F
85 #define TPS6507X_DEFDCDCX_DCDC_MASK 0X3F
88 #define TPS6507X_DCDC_1 0
89 #define TPS6507X_DCDC_2 1
90 #define TPS6507X_DCDC_3 2
92 #define TPS6507X_LDO_1 3
93 #define TPS6507X_LDO_2 4
95 #define TPS6507X_MAX_REG_ID TPS6507X_LDO_2
97 /* Number of step-down converters available */
98 #define TPS6507X_NUM_DCDC 3
99 /* Number of LDO voltage regulators available */
100 #define TPS6507X_NUM_LDO 2
101 /* Number of total regulators available */
102 #define TPS6507X_NUM_REGULATOR (TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)
104 /* Supported voltage values for regulators (in milliVolts) */
105 static const u16 VDCDCx_VSEL_table
[] = {
109 1025, 1050, 1075, 1100,
110 1125, 1150, 1175, 1200,
111 1225, 1250, 1275, 1300,
112 1325, 1350, 1375, 1400,
113 1425, 1450, 1475, 1500,
114 1550, 1600, 1650, 1700,
115 1750, 1800, 1850, 1900,
116 1950, 2000, 2050, 2100,
117 2150, 2200, 2250, 2300,
118 2350, 2400, 2450, 2500,
119 2550, 2600, 2650, 2700,
120 2750, 2800, 2850, 2900,
121 3000, 3100, 3200, 3300,
124 static const u16 LDO1_VSEL_table
[] = {
125 1000, 1100, 1200, 1250,
126 1300, 1350, 1400, 1500,
127 1600, 1800, 2500, 2750,
128 2800, 3000, 3100, 3300,
131 static const u16 LDO2_VSEL_table
[] = {
135 1025, 1050, 1075, 1100,
136 1125, 1150, 1175, 1200,
137 1225, 1250, 1275, 1300,
138 1325, 1350, 1375, 1400,
139 1425, 1450, 1475, 1500,
140 1550, 1600, 1650, 1700,
141 1750, 1800, 1850, 1900,
142 1950, 2000, 2050, 2100,
143 2150, 2200, 2250, 2300,
144 2350, 2400, 2450, 2500,
145 2550, 2600, 2650, 2700,
146 2750, 2800, 2850, 2900,
147 3000, 3100, 3200, 3300,
150 static unsigned int num_voltages
[] = {ARRAY_SIZE(VDCDCx_VSEL_table
),
151 ARRAY_SIZE(VDCDCx_VSEL_table
),
152 ARRAY_SIZE(VDCDCx_VSEL_table
),
153 ARRAY_SIZE(LDO1_VSEL_table
),
154 ARRAY_SIZE(LDO2_VSEL_table
)};
165 struct regulator_desc desc
[TPS6507X_NUM_REGULATOR
];
166 struct i2c_client
*client
;
167 struct regulator_dev
*rdev
[TPS6507X_NUM_REGULATOR
];
168 const struct tps_info
*info
[TPS6507X_NUM_REGULATOR
];
169 struct mutex io_lock
;
172 static inline int tps_6507x_read(struct tps_pmic
*tps
, u8 reg
)
174 return i2c_smbus_read_byte_data(tps
->client
, reg
);
177 static inline int tps_6507x_write(struct tps_pmic
*tps
, u8 reg
, u8 val
)
179 return i2c_smbus_write_byte_data(tps
->client
, reg
, val
);
182 static int tps_6507x_set_bits(struct tps_pmic
*tps
, u8 reg
, u8 mask
)
186 mutex_lock(&tps
->io_lock
);
188 data
= tps_6507x_read(tps
, reg
);
190 dev_err(&tps
->client
->dev
, "Read from reg 0x%x failed\n", reg
);
196 err
= tps_6507x_write(tps
, reg
, data
);
198 dev_err(&tps
->client
->dev
, "Write for reg 0x%x failed\n", reg
);
201 mutex_unlock(&tps
->io_lock
);
205 static int tps_6507x_clear_bits(struct tps_pmic
*tps
, u8 reg
, u8 mask
)
209 mutex_lock(&tps
->io_lock
);
211 data
= tps_6507x_read(tps
, reg
);
213 dev_err(&tps
->client
->dev
, "Read from reg 0x%x failed\n", reg
);
219 err
= tps_6507x_write(tps
, reg
, data
);
221 dev_err(&tps
->client
->dev
, "Write for reg 0x%x failed\n", reg
);
224 mutex_unlock(&tps
->io_lock
);
228 static int tps_6507x_reg_read(struct tps_pmic
*tps
, u8 reg
)
232 mutex_lock(&tps
->io_lock
);
234 data
= tps_6507x_read(tps
, reg
);
236 dev_err(&tps
->client
->dev
, "Read from reg 0x%x failed\n", reg
);
238 mutex_unlock(&tps
->io_lock
);
242 static int tps_6507x_reg_write(struct tps_pmic
*tps
, u8 reg
, u8 val
)
246 mutex_lock(&tps
->io_lock
);
248 err
= tps_6507x_write(tps
, reg
, val
);
250 dev_err(&tps
->client
->dev
, "Write for reg 0x%x failed\n", reg
);
252 mutex_unlock(&tps
->io_lock
);
256 static int tps6507x_dcdc_is_enabled(struct regulator_dev
*dev
)
258 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
259 int data
, dcdc
= rdev_get_id(dev
);
262 if (dcdc
< TPS6507X_DCDC_1
|| dcdc
> TPS6507X_DCDC_3
)
265 shift
= TPS6507X_MAX_REG_ID
- dcdc
;
266 data
= tps_6507x_reg_read(tps
, TPS6507X_REG_CON_CTRL1
);
271 return (data
& 1<<shift
) ? 1 : 0;
274 static int tps6507x_ldo_is_enabled(struct regulator_dev
*dev
)
276 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
277 int data
, ldo
= rdev_get_id(dev
);
280 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
283 shift
= TPS6507X_MAX_REG_ID
- ldo
;
284 data
= tps_6507x_reg_read(tps
, TPS6507X_REG_CON_CTRL1
);
289 return (data
& 1<<shift
) ? 1 : 0;
292 static int tps6507x_dcdc_enable(struct regulator_dev
*dev
)
294 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
295 int dcdc
= rdev_get_id(dev
);
298 if (dcdc
< TPS6507X_DCDC_1
|| dcdc
> TPS6507X_DCDC_3
)
301 shift
= TPS6507X_MAX_REG_ID
- dcdc
;
302 return tps_6507x_set_bits(tps
, TPS6507X_REG_CON_CTRL1
, 1 << shift
);
305 static int tps6507x_dcdc_disable(struct regulator_dev
*dev
)
307 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
308 int dcdc
= rdev_get_id(dev
);
311 if (dcdc
< TPS6507X_DCDC_1
|| dcdc
> TPS6507X_DCDC_3
)
314 shift
= TPS6507X_MAX_REG_ID
- dcdc
;
315 return tps_6507x_clear_bits(tps
, TPS6507X_REG_CON_CTRL1
, 1 << shift
);
318 static int tps6507x_ldo_enable(struct regulator_dev
*dev
)
320 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
321 int ldo
= rdev_get_id(dev
);
324 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
327 shift
= TPS6507X_MAX_REG_ID
- ldo
;
328 return tps_6507x_set_bits(tps
, TPS6507X_REG_CON_CTRL1
, 1 << shift
);
331 static int tps6507x_ldo_disable(struct regulator_dev
*dev
)
333 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
334 int ldo
= rdev_get_id(dev
);
337 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
340 shift
= TPS6507X_MAX_REG_ID
- ldo
;
341 return tps_6507x_clear_bits(tps
, TPS6507X_REG_CON_CTRL1
, 1 << shift
);
344 static int tps6507x_dcdc_get_voltage(struct regulator_dev
*dev
)
346 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
347 int data
, dcdc
= rdev_get_id(dev
);
351 case TPS6507X_DCDC_1
:
352 reg
= TPS6507X_REG_DEFDCDC1
;
354 case TPS6507X_DCDC_2
:
355 reg
= TPS6507X_REG_DEFDCDC2_LOW
;
357 case TPS6507X_DCDC_3
:
358 reg
= TPS6507X_REG_DEFDCDC3_LOW
;
364 data
= tps_6507x_reg_read(tps
, reg
);
368 data
&= TPS6507X_DEFDCDCX_DCDC_MASK
;
369 return tps
->info
[dcdc
]->table
[data
] * 1000;
372 static int tps6507x_dcdc_set_voltage(struct regulator_dev
*dev
,
373 int min_uV
, int max_uV
)
375 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
376 int data
, vsel
, dcdc
= rdev_get_id(dev
);
380 case TPS6507X_DCDC_1
:
381 reg
= TPS6507X_REG_DEFDCDC1
;
383 case TPS6507X_DCDC_2
:
384 reg
= TPS6507X_REG_DEFDCDC2_LOW
;
386 case TPS6507X_DCDC_3
:
387 reg
= TPS6507X_REG_DEFDCDC3_LOW
;
393 if (min_uV
< tps
->info
[dcdc
]->min_uV
394 || min_uV
> tps
->info
[dcdc
]->max_uV
)
396 if (max_uV
< tps
->info
[dcdc
]->min_uV
397 || max_uV
> tps
->info
[dcdc
]->max_uV
)
400 for (vsel
= 0; vsel
< tps
->info
[dcdc
]->table_len
; vsel
++) {
401 int mV
= tps
->info
[dcdc
]->table
[vsel
];
404 /* Break at the first in-range value */
405 if (min_uV
<= uV
&& uV
<= max_uV
)
409 /* write to the register in case we found a match */
410 if (vsel
== tps
->info
[dcdc
]->table_len
)
413 data
= tps_6507x_reg_read(tps
, reg
);
417 data
&= ~TPS6507X_DEFDCDCX_DCDC_MASK
;
420 return tps_6507x_reg_write(tps
, reg
, data
);
423 static int tps6507x_ldo_get_voltage(struct regulator_dev
*dev
)
425 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
426 int data
, ldo
= rdev_get_id(dev
);
429 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
432 reg
= (ldo
== TPS6507X_LDO_1
?
433 TPS6507X_REG_LDO_CTRL1
: TPS6507X_REG_DEFLDO2
);
434 mask
= (ldo
== TPS6507X_LDO_1
?
435 TPS6507X_REG_LDO_CTRL1_LDO1_MASK
:
436 TPS6507X_REG_DEFLDO2_LDO2_MASK
);
439 data
= tps_6507x_reg_read(tps
, reg
);
444 return tps
->info
[ldo
]->table
[data
] * 1000;
447 static int tps6507x_ldo_set_voltage(struct regulator_dev
*dev
,
448 int min_uV
, int max_uV
)
450 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
451 int data
, vsel
, ldo
= rdev_get_id(dev
);
454 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
457 reg
= (ldo
== TPS6507X_LDO_1
?
458 TPS6507X_REG_LDO_CTRL1
: TPS6507X_REG_DEFLDO2
);
459 mask
= (ldo
== TPS6507X_LDO_1
?
460 TPS6507X_REG_LDO_CTRL1_LDO1_MASK
:
461 TPS6507X_REG_DEFLDO2_LDO2_MASK
);
464 if (min_uV
< tps
->info
[ldo
]->min_uV
|| min_uV
> tps
->info
[ldo
]->max_uV
)
466 if (max_uV
< tps
->info
[ldo
]->min_uV
|| max_uV
> tps
->info
[ldo
]->max_uV
)
469 for (vsel
= 0; vsel
< tps
->info
[ldo
]->table_len
; vsel
++) {
470 int mV
= tps
->info
[ldo
]->table
[vsel
];
473 /* Break at the first in-range value */
474 if (min_uV
<= uV
&& uV
<= max_uV
)
478 if (vsel
== tps
->info
[ldo
]->table_len
)
481 data
= tps_6507x_reg_read(tps
, reg
);
488 return tps_6507x_reg_write(tps
, reg
, data
);
491 static int tps6507x_dcdc_list_voltage(struct regulator_dev
*dev
,
494 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
495 int dcdc
= rdev_get_id(dev
);
497 if (dcdc
< TPS6507X_DCDC_1
|| dcdc
> TPS6507X_DCDC_3
)
500 if (selector
>= tps
->info
[dcdc
]->table_len
)
503 return tps
->info
[dcdc
]->table
[selector
] * 1000;
506 static int tps6507x_ldo_list_voltage(struct regulator_dev
*dev
,
509 struct tps_pmic
*tps
= rdev_get_drvdata(dev
);
510 int ldo
= rdev_get_id(dev
);
512 if (ldo
< TPS6507X_LDO_1
|| ldo
> TPS6507X_LDO_2
)
515 if (selector
>= tps
->info
[ldo
]->table_len
)
518 return tps
->info
[ldo
]->table
[selector
] * 1000;
521 /* Operations permitted on VDCDCx */
522 static struct regulator_ops tps6507x_dcdc_ops
= {
523 .is_enabled
= tps6507x_dcdc_is_enabled
,
524 .enable
= tps6507x_dcdc_enable
,
525 .disable
= tps6507x_dcdc_disable
,
526 .get_voltage
= tps6507x_dcdc_get_voltage
,
527 .set_voltage
= tps6507x_dcdc_set_voltage
,
528 .list_voltage
= tps6507x_dcdc_list_voltage
,
531 /* Operations permitted on LDOx */
532 static struct regulator_ops tps6507x_ldo_ops
= {
533 .is_enabled
= tps6507x_ldo_is_enabled
,
534 .enable
= tps6507x_ldo_enable
,
535 .disable
= tps6507x_ldo_disable
,
536 .get_voltage
= tps6507x_ldo_get_voltage
,
537 .set_voltage
= tps6507x_ldo_set_voltage
,
538 .list_voltage
= tps6507x_ldo_list_voltage
,
541 static int __devinit
tps_6507x_probe(struct i2c_client
*client
,
542 const struct i2c_device_id
*id
)
545 const struct tps_info
*info
= (void *)id
->driver_data
;
546 struct regulator_init_data
*init_data
;
547 struct regulator_dev
*rdev
;
548 struct tps_pmic
*tps
;
552 if (!i2c_check_functionality(client
->adapter
,
553 I2C_FUNC_SMBUS_BYTE_DATA
))
557 * init_data points to array of regulator_init structures
558 * coming from the board-evm file.
560 init_data
= client
->dev
.platform_data
;
564 tps
= kzalloc(sizeof(*tps
), GFP_KERNEL
);
568 mutex_init(&tps
->io_lock
);
570 /* common for all regulators */
571 tps
->client
= client
;
573 for (i
= 0; i
< TPS6507X_NUM_REGULATOR
; i
++, info
++, init_data
++) {
574 /* Register the regulators */
576 tps
->desc
[i
].name
= info
->name
;
577 tps
->desc
[i
].id
= desc_id
++;
578 tps
->desc
[i
].n_voltages
= num_voltages
[i
];
579 tps
->desc
[i
].ops
= (i
> TPS6507X_DCDC_3
?
580 &tps6507x_ldo_ops
: &tps6507x_dcdc_ops
);
581 tps
->desc
[i
].type
= REGULATOR_VOLTAGE
;
582 tps
->desc
[i
].owner
= THIS_MODULE
;
584 rdev
= regulator_register(&tps
->desc
[i
],
585 &client
->dev
, init_data
, tps
);
587 dev_err(&client
->dev
, "failed to register %s\n",
589 error
= PTR_ERR(rdev
);
593 /* Save regulator for cleanup */
597 i2c_set_clientdata(client
, tps
);
603 regulator_unregister(tps
->rdev
[i
]);
610 * tps_6507x_remove - TPS6507x driver i2c remove handler
611 * @client: i2c driver client device structure
613 * Unregister TPS driver as an i2c client device driver
615 static int __devexit
tps_6507x_remove(struct i2c_client
*client
)
617 struct tps_pmic
*tps
= i2c_get_clientdata(client
);
620 /* clear the client data in i2c */
621 i2c_set_clientdata(client
, NULL
);
623 for (i
= 0; i
< TPS6507X_NUM_REGULATOR
; i
++)
624 regulator_unregister(tps
->rdev
[i
]);
631 static const struct tps_info tps6507x_regs
[] = {
636 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
637 .table
= VDCDCx_VSEL_table
,
643 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
644 .table
= VDCDCx_VSEL_table
,
650 .table_len
= ARRAY_SIZE(VDCDCx_VSEL_table
),
651 .table
= VDCDCx_VSEL_table
,
657 .table_len
= ARRAY_SIZE(LDO1_VSEL_table
),
658 .table
= LDO1_VSEL_table
,
664 .table_len
= ARRAY_SIZE(LDO2_VSEL_table
),
665 .table
= LDO2_VSEL_table
,
669 static const struct i2c_device_id tps_6507x_id
[] = {
671 .driver_data
= (unsigned long) tps6507x_regs
,},
674 MODULE_DEVICE_TABLE(i2c
, tps_6507x_id
);
676 static struct i2c_driver tps_6507x_i2c_driver
= {
679 .owner
= THIS_MODULE
,
681 .probe
= tps_6507x_probe
,
682 .remove
= __devexit_p(tps_6507x_remove
),
683 .id_table
= tps_6507x_id
,
689 * Module init function
691 static int __init
tps_6507x_init(void)
693 return i2c_add_driver(&tps_6507x_i2c_driver
);
695 subsys_initcall(tps_6507x_init
);
700 * Module exit function
702 static void __exit
tps_6507x_cleanup(void)
704 i2c_del_driver(&tps_6507x_i2c_driver
);
706 module_exit(tps_6507x_cleanup
);
708 MODULE_AUTHOR("Texas Instruments");
709 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
710 MODULE_LICENSE("GPL v2");