Linux Audio

Check our new training course

Linux kernel drivers training

Mar 31-Apr 9, 2025, special US time zones
Register
Loading...
v5.9
  1/*
  2 * tps65023-regulator.c
  3 *
  4 * Supports TPS65023 Regulator
  5 *
  6 * Copyright (C) 2009 Texas Instrument Incorporated - https://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/slab.h>
 27#include <linux/regmap.h>
 28
 29/* Register definitions */
 30#define	TPS65023_REG_VERSION		0
 31#define	TPS65023_REG_PGOODZ		1
 32#define	TPS65023_REG_MASK		2
 33#define	TPS65023_REG_REG_CTRL		3
 34#define	TPS65023_REG_CON_CTRL		4
 35#define	TPS65023_REG_CON_CTRL2		5
 36#define	TPS65023_REG_DEF_CORE		6
 37#define	TPS65023_REG_DEFSLEW		7
 38#define	TPS65023_REG_LDO_CTRL		8
 39
 40/* PGOODZ bitfields */
 41#define	TPS65023_PGOODZ_PWRFAILZ	BIT(7)
 42#define	TPS65023_PGOODZ_LOWBATTZ	BIT(6)
 43#define	TPS65023_PGOODZ_VDCDC1		BIT(5)
 44#define	TPS65023_PGOODZ_VDCDC2		BIT(4)
 45#define	TPS65023_PGOODZ_VDCDC3		BIT(3)
 46#define	TPS65023_PGOODZ_LDO2		BIT(2)
 47#define	TPS65023_PGOODZ_LDO1		BIT(1)
 48
 49/* MASK bitfields */
 50#define	TPS65023_MASK_PWRFAILZ		BIT(7)
 51#define	TPS65023_MASK_LOWBATTZ		BIT(6)
 52#define	TPS65023_MASK_VDCDC1		BIT(5)
 53#define	TPS65023_MASK_VDCDC2		BIT(4)
 54#define	TPS65023_MASK_VDCDC3		BIT(3)
 55#define	TPS65023_MASK_LDO2		BIT(2)
 56#define	TPS65023_MASK_LDO1		BIT(1)
 57
 58/* REG_CTRL bitfields */
 59#define TPS65023_REG_CTRL_VDCDC1_EN	BIT(5)
 60#define TPS65023_REG_CTRL_VDCDC2_EN	BIT(4)
 61#define TPS65023_REG_CTRL_VDCDC3_EN	BIT(3)
 62#define TPS65023_REG_CTRL_LDO2_EN	BIT(2)
 63#define TPS65023_REG_CTRL_LDO1_EN	BIT(1)
 64
 65/* REG_CTRL2 bitfields */
 66#define TPS65023_REG_CTRL2_GO		BIT(7)
 67#define TPS65023_REG_CTRL2_CORE_ADJ	BIT(6)
 68#define TPS65023_REG_CTRL2_DCDC2	BIT(2)
 69#define TPS65023_REG_CTRL2_DCDC1	BIT(1)
 70#define TPS65023_REG_CTRL2_DCDC3	BIT(0)
 71
 72/* Number of step-down converters available */
 73#define TPS65023_NUM_DCDC		3
 74/* Number of LDO voltage regulators  available */
 75#define TPS65023_NUM_LDO		2
 76/* Number of total regulators available */
 77#define TPS65023_NUM_REGULATOR	(TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
 78
 79/* DCDCs */
 80#define TPS65023_DCDC_1			0
 81#define TPS65023_DCDC_2			1
 82#define TPS65023_DCDC_3			2
 83/* LDOs */
 84#define TPS65023_LDO_1			3
 85#define TPS65023_LDO_2			4
 86
 87#define TPS65023_MAX_REG_ID		TPS65023_LDO_2
 88
 89#define TPS65023_REGULATOR_DCDC(_num, _t, _em)			\
 90	{							\
 91		.name		= "VDCDC"#_num,			\
 92		.of_match	= of_match_ptr("VDCDC"#_num),	\
 93		.regulators_node = of_match_ptr("regulators"),	\
 94		.id		= TPS65023_DCDC_##_num,		\
 95		.n_voltages     = ARRAY_SIZE(_t),		\
 96		.ops		= &tps65023_dcdc_ops,		\
 97		.type		= REGULATOR_VOLTAGE,		\
 98		.owner		= THIS_MODULE,			\
 99		.volt_table	= _t,				\
100		.vsel_reg	= TPS65023_REG_DEF_CORE,	\
101		.vsel_mask	= ARRAY_SIZE(_t) - 1,		\
102		.enable_mask	= _em,				\
103		.enable_reg	= TPS65023_REG_REG_CTRL,	\
104		.apply_reg	= TPS65023_REG_CON_CTRL2,	\
105		.apply_bit	= TPS65023_REG_CTRL2_GO,	\
106	}							\
107
108#define TPS65023_REGULATOR_LDO(_num, _t, _vm)			\
109	{							\
110		.name		= "LDO"#_num,			\
111		.of_match	= of_match_ptr("LDO"#_num),	\
112		.regulators_node = of_match_ptr("regulators"),	\
113		.id		= TPS65023_LDO_##_num,		\
114		.n_voltages     = ARRAY_SIZE(_t),		\
115		.ops		= &tps65023_ldo_ops,		\
116		.type		= REGULATOR_VOLTAGE,		\
117		.owner		= THIS_MODULE,			\
118		.volt_table	= _t,				\
119		.vsel_reg	= TPS65023_REG_LDO_CTRL,	\
120		.vsel_mask	= _vm,				\
121		.enable_mask	= 1 << (_num),			\
122		.enable_reg	= TPS65023_REG_REG_CTRL,	\
123	}							\
124
125/* Supported voltage values for regulators */
126static const unsigned int VCORE_VSEL_table[] = {
127	800000, 825000, 850000, 875000,
128	900000, 925000, 950000, 975000,
129	1000000, 1025000, 1050000, 1075000,
130	1100000, 1125000, 1150000, 1175000,
131	1200000, 1225000, 1250000, 1275000,
132	1300000, 1325000, 1350000, 1375000,
133	1400000, 1425000, 1450000, 1475000,
134	1500000, 1525000, 1550000, 1600000,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135};
136
137static const unsigned int DCDC_FIXED_3300000_VSEL_table[] = {
138	3300000,
 
 
 
 
 
139};
140
141static const unsigned int DCDC_FIXED_1800000_VSEL_table[] = {
142	1800000,
143};
 
144
145/* Supported voltage values for LDO regulators for tps65020 */
146static const unsigned int TPS65020_LDO_VSEL_table[] = {
147	1000000, 1050000, 1100000, 1300000,
148	1800000, 2500000, 3000000, 3300000,
149};
150
151/* Supported voltage values for LDO regulators
152 * for tps65021 and tps65023 */
153static const unsigned int TPS65023_LDO1_VSEL_table[] = {
154	1000000, 1100000, 1300000, 1800000,
155	2200000, 2600000, 2800000, 3150000,
156};
157
158static const unsigned int TPS65023_LDO2_VSEL_table[] = {
159	1050000, 1200000, 1300000, 1800000,
160	2500000, 2800000, 3000000, 3300000,
161};
162
163/* PMIC details */
164struct tps_pmic {
165	struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
166	const struct tps_driver_data *driver_data;
167	struct regmap *regmap;
168};
169
170/* Struct passed as driver data */
171struct tps_driver_data {
172	const struct regulator_desc *desc;
173	u8 core_regulator;
174};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
176static int tps65023_dcdc_get_voltage_sel(struct regulator_dev *dev)
177{
178	struct tps_pmic *tps = rdev_get_drvdata(dev);
179	int dcdc = rdev_get_id(dev);
 
180
181	if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
182		return -EINVAL;
183
184	if (dcdc != tps->driver_data->core_regulator)
185		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
187	return regulator_get_voltage_sel_regmap(dev);
 
 
 
 
 
 
 
 
 
 
188}
189
190static int tps65023_dcdc_set_voltage_sel(struct regulator_dev *dev,
191					 unsigned selector)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192{
193	struct tps_pmic *tps = rdev_get_drvdata(dev);
194	int dcdc = rdev_get_id(dev);
 
195
196	if (dcdc != tps->driver_data->core_regulator)
197		return -EINVAL;
198
199	return regulator_set_voltage_sel_regmap(dev, selector);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200}
201
202/* Operations permitted on VDCDCx */
203static const struct regulator_ops tps65023_dcdc_ops = {
204	.is_enabled = regulator_is_enabled_regmap,
205	.enable = regulator_enable_regmap,
206	.disable = regulator_disable_regmap,
207	.get_voltage_sel = tps65023_dcdc_get_voltage_sel,
208	.set_voltage_sel = tps65023_dcdc_set_voltage_sel,
209	.list_voltage = regulator_list_voltage_table,
210	.map_voltage = regulator_map_voltage_ascend,
211};
212
213/* Operations permitted on LDOx */
214static const struct regulator_ops tps65023_ldo_ops = {
215	.is_enabled = regulator_is_enabled_regmap,
216	.enable = regulator_enable_regmap,
217	.disable = regulator_disable_regmap,
218	.get_voltage_sel = regulator_get_voltage_sel_regmap,
219	.set_voltage_sel = regulator_set_voltage_sel_regmap,
220	.list_voltage = regulator_list_voltage_table,
221	.map_voltage = regulator_map_voltage_ascend,
222};
223
224static const struct regmap_config tps65023_regmap_config = {
225	.reg_bits = 8,
226	.val_bits = 8,
227};
 
 
 
 
228
229static const struct regulator_desc tps65020_regulators[] = {
230	TPS65023_REGULATOR_DCDC(1, DCDC_FIXED_3300000_VSEL_table, 0x20),
231	TPS65023_REGULATOR_DCDC(2, DCDC_FIXED_1800000_VSEL_table, 0x10),
232	TPS65023_REGULATOR_DCDC(3, VCORE_VSEL_table, 0x08),
233	TPS65023_REGULATOR_LDO(1, TPS65020_LDO_VSEL_table, 0x07),
234	TPS65023_REGULATOR_LDO(2, TPS65020_LDO_VSEL_table, 0x70),
235};
236
237static const struct regulator_desc tps65021_regulators[] = {
238	TPS65023_REGULATOR_DCDC(1, DCDC_FIXED_3300000_VSEL_table, 0x20),
239	TPS65023_REGULATOR_DCDC(2, DCDC_FIXED_1800000_VSEL_table, 0x10),
240	TPS65023_REGULATOR_DCDC(3, VCORE_VSEL_table, 0x08),
241	TPS65023_REGULATOR_LDO(1, TPS65023_LDO1_VSEL_table, 0x07),
242	TPS65023_REGULATOR_LDO(2, TPS65023_LDO2_VSEL_table, 0x70),
243};
244
245static const struct regulator_desc tps65023_regulators[] = {
246	TPS65023_REGULATOR_DCDC(1, VCORE_VSEL_table, 0x20),
247	TPS65023_REGULATOR_DCDC(2, DCDC_FIXED_3300000_VSEL_table, 0x10),
248	TPS65023_REGULATOR_DCDC(3, DCDC_FIXED_1800000_VSEL_table, 0x08),
249	TPS65023_REGULATOR_LDO(1, TPS65023_LDO1_VSEL_table, 0x07),
250	TPS65023_REGULATOR_LDO(2, TPS65023_LDO2_VSEL_table, 0x70),
251};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
253static struct tps_driver_data tps65020_drv_data = {
254	.desc = tps65020_regulators,
255	.core_regulator = TPS65023_DCDC_3,
 
 
 
 
 
256};
257
258static struct tps_driver_data tps65021_drv_data = {
259	.desc = tps65021_regulators,
260	.core_regulator = TPS65023_DCDC_3,
 
 
 
 
 
261};
262
263static struct tps_driver_data tps65023_drv_data = {
264	.desc = tps65023_regulators,
265	.core_regulator = TPS65023_DCDC_1,
266};
267
268static int tps_65023_probe(struct i2c_client *client,
269				     const struct i2c_device_id *id)
270{
271	struct regulator_init_data *init_data = dev_get_platdata(&client->dev);
272	struct regulator_config config = { };
 
273	struct tps_pmic *tps;
274	int i;
275	int error;
276
277	tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
 
 
 
 
 
 
 
 
 
 
 
278	if (!tps)
279		return -ENOMEM;
280
281	tps->driver_data = (struct tps_driver_data *)id->driver_data;
282
283	tps->regmap = devm_regmap_init_i2c(client, &tps65023_regmap_config);
284	if (IS_ERR(tps->regmap)) {
285		error = PTR_ERR(tps->regmap);
286		dev_err(&client->dev, "Failed to allocate register map: %d\n",
287			error);
288		return error;
289	}
290
291	/* common for all regulators */
292	config.dev = &client->dev;
293	config.driver_data = tps;
294	config.regmap = tps->regmap;
295
296	for (i = 0; i < TPS65023_NUM_REGULATOR; i++) {
297		if (init_data)
298			config.init_data = &init_data[i];
 
 
 
 
 
 
299
300		/* Register the regulators */
301		tps->rdev[i] = devm_regulator_register(&client->dev,
302					&tps->driver_data->desc[i], &config);
303		if (IS_ERR(tps->rdev[i])) {
304			dev_err(&client->dev, "failed to register %s\n",
305				id->name);
306			return PTR_ERR(tps->rdev[i]);
 
307		}
 
 
 
308	}
309
310	i2c_set_clientdata(client, tps);
311
312	/* Enable setting output voltage by I2C */
313	regmap_update_bits(tps->regmap, TPS65023_REG_CON_CTRL2,
314			   TPS65023_REG_CTRL2_CORE_ADJ, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
316	return 0;
317}
318
319static const struct of_device_id tps65023_of_match[] = {
320	{ .compatible = "ti,tps65020", .data = &tps65020_drv_data},
321	{ .compatible = "ti,tps65021", .data = &tps65021_drv_data},
322	{ .compatible = "ti,tps65023", .data = &tps65023_drv_data},
323	{},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
324};
325MODULE_DEVICE_TABLE(of, tps65023_of_match);
326
327static const struct i2c_device_id tps_65023_id[] = {
328	{
329		.name = "tps65023",
330		.driver_data = (kernel_ulong_t)&tps65023_drv_data
331	}, {
332		.name = "tps65021",
333		.driver_data = (kernel_ulong_t)&tps65021_drv_data
334	}, {
335		.name = "tps65020",
336		.driver_data = (kernel_ulong_t)&tps65020_drv_data
337	},
338	{ },
339};
 
340MODULE_DEVICE_TABLE(i2c, tps_65023_id);
341
342static struct i2c_driver tps_65023_i2c_driver = {
343	.driver = {
344		.name = "tps65023",
345		.of_match_table = of_match_ptr(tps65023_of_match),
346	},
347	.probe = tps_65023_probe,
 
348	.id_table = tps_65023_id,
349};
350
 
 
 
 
 
351static int __init tps_65023_init(void)
352{
353	return i2c_add_driver(&tps_65023_i2c_driver);
354}
355subsys_initcall(tps_65023_init);
356
 
 
 
 
 
357static void __exit tps_65023_cleanup(void)
358{
359	i2c_del_driver(&tps_65023_i2c_driver);
360}
361module_exit(tps_65023_cleanup);
362
363MODULE_AUTHOR("Texas Instruments");
364MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
365MODULE_LICENSE("GPL v2");
v3.1
  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>
 27#include <linux/slab.h>
 28#include <linux/regmap.h>
 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
 66/* LDO_CTRL bitfields */
 67#define TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_id)	((ldo_id)*4)
 68#define TPS65023_LDO_CTRL_LDOx_MASK(ldo_id)	(0xF0 >> ((ldo_id)*4))
 
 
 
 69
 70/* Number of step-down converters available */
 71#define TPS65023_NUM_DCDC		3
 72/* Number of LDO voltage regulators  available */
 73#define TPS65023_NUM_LDO		2
 74/* Number of total regulators available */
 75#define TPS65023_NUM_REGULATOR	(TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
 76
 77/* DCDCs */
 78#define TPS65023_DCDC_1			0
 79#define TPS65023_DCDC_2			1
 80#define TPS65023_DCDC_3			2
 81/* LDOs */
 82#define TPS65023_LDO_1			3
 83#define TPS65023_LDO_2			4
 84
 85#define TPS65023_MAX_REG_ID		TPS65023_LDO_2
 86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 87/* Supported voltage values for regulators */
 88static const u16 VDCDC1_VSEL_table[] = {
 89	800, 825, 850, 875,
 90	900, 925, 950, 975,
 91	1000, 1025, 1050, 1075,
 92	1100, 1125, 1150, 1175,
 93	1200, 1225, 1250, 1275,
 94	1300, 1325, 1350, 1375,
 95	1400, 1425, 1450, 1475,
 96	1500, 1525, 1550, 1600,
 97};
 98
 99static const u16 LDO1_VSEL_table[] = {
100	1000, 1100, 1300, 1800,
101	2200, 2600, 2800, 3150,
102};
103
104static const u16 LDO2_VSEL_table[] = {
105	1050, 1200, 1300, 1800,
106	2500, 2800, 3000, 3300,
107};
108
109static unsigned int num_voltages[] = {ARRAY_SIZE(VDCDC1_VSEL_table),
110				0, 0, ARRAY_SIZE(LDO1_VSEL_table),
111				ARRAY_SIZE(LDO2_VSEL_table)};
112
113/* Regulator specific details */
114struct tps_info {
115	const char *name;
116	unsigned min_uV;
117	unsigned max_uV;
118	bool fixed;
119	u8 table_len;
120	const u16 *table;
121};
122
123/* PMIC details */
124struct tps_pmic {
125	struct regulator_desc desc[TPS65023_NUM_REGULATOR];
126	struct i2c_client *client;
127	struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
128	const struct tps_info *info[TPS65023_NUM_REGULATOR];
129	struct regmap *regmap;
130};
131
132static int tps_65023_set_bits(struct tps_pmic *tps, u8 reg, u8 mask)
133{
134	return regmap_update_bits(tps->regmap, reg, mask, mask);
135}
136
137static int tps_65023_clear_bits(struct tps_pmic *tps, u8 reg, u8 mask)
138{
139	return regmap_update_bits(tps->regmap, reg, mask, 0);
140}
 
141
142static int tps_65023_reg_read(struct tps_pmic *tps, u8 reg)
143{
144	unsigned int val;
145	int ret;
 
 
146
147	ret = regmap_read(tps->regmap, reg, &val);
 
 
 
148
149	if (ret != 0)
150		return ret;
151	else
152		return val;
153}
 
154
155static int tps_65023_reg_write(struct tps_pmic *tps, u8 reg, u8 val)
156{
157	return regmap_write(tps->regmap, reg, val);
158}
159
160static int tps65023_dcdc_is_enabled(struct regulator_dev *dev)
161{
162	struct tps_pmic *tps = rdev_get_drvdata(dev);
163	int data, dcdc = rdev_get_id(dev);
164	u8 shift;
165
166	if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
167		return -EINVAL;
168
169	shift = TPS65023_NUM_REGULATOR - dcdc;
170	data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
171
172	if (data < 0)
173		return data;
174	else
175		return (data & 1<<shift) ? 1 : 0;
176}
177
178static int tps65023_ldo_is_enabled(struct regulator_dev *dev)
179{
180	struct tps_pmic *tps = rdev_get_drvdata(dev);
181	int data, ldo = rdev_get_id(dev);
182	u8 shift;
183
184	if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
185		return -EINVAL;
186
187	shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
188	data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
189
190	if (data < 0)
191		return data;
192	else
193		return (data & 1<<shift) ? 1 : 0;
194}
195
196static int tps65023_dcdc_enable(struct regulator_dev *dev)
197{
198	struct tps_pmic *tps = rdev_get_drvdata(dev);
199	int dcdc = rdev_get_id(dev);
200	u8 shift;
201
202	if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
203		return -EINVAL;
204
205	shift = TPS65023_NUM_REGULATOR - dcdc;
206	return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
207}
208
209static int tps65023_dcdc_disable(struct regulator_dev *dev)
210{
211	struct tps_pmic *tps = rdev_get_drvdata(dev);
212	int dcdc = rdev_get_id(dev);
213	u8 shift;
214
215	if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
216		return -EINVAL;
217
218	shift = TPS65023_NUM_REGULATOR - dcdc;
219	return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
220}
221
222static int tps65023_ldo_enable(struct regulator_dev *dev)
223{
224	struct tps_pmic *tps = rdev_get_drvdata(dev);
225	int ldo = rdev_get_id(dev);
226	u8 shift;
227
228	if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
229		return -EINVAL;
230
231	shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
232	return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
233}
234
235static int tps65023_ldo_disable(struct regulator_dev *dev)
236{
237	struct tps_pmic *tps = rdev_get_drvdata(dev);
238	int ldo = rdev_get_id(dev);
239	u8 shift;
240
241	if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
242		return -EINVAL;
243
244	shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
245	return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
246}
247
248static int tps65023_dcdc_get_voltage(struct regulator_dev *dev)
249{
250	struct tps_pmic *tps = rdev_get_drvdata(dev);
251	int data, dcdc = rdev_get_id(dev);
252
253	if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
254		return -EINVAL;
255
256	if (dcdc == TPS65023_DCDC_1) {
257		data = tps_65023_reg_read(tps, TPS65023_REG_DEF_CORE);
258		if (data < 0)
259			return data;
260		data &= (tps->info[dcdc]->table_len - 1);
261		return tps->info[dcdc]->table[data] * 1000;
262	} else
263		return tps->info[dcdc]->min_uV;
264}
265
266static int tps65023_dcdc_set_voltage(struct regulator_dev *dev,
267				     int min_uV, int max_uV,
268				     unsigned *selector)
269{
270	struct tps_pmic *tps = rdev_get_drvdata(dev);
271	int dcdc = rdev_get_id(dev);
272	int vsel;
273
274	if (dcdc != TPS65023_DCDC_1)
275		return -EINVAL;
276
277	if (min_uV < tps->info[dcdc]->min_uV
278			|| min_uV > tps->info[dcdc]->max_uV)
279		return -EINVAL;
280	if (max_uV < tps->info[dcdc]->min_uV
281			|| max_uV > tps->info[dcdc]->max_uV)
282		return -EINVAL;
283
284	for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
285		int mV = tps->info[dcdc]->table[vsel];
286		int uV = mV * 1000;
287
288		/* Break at the first in-range value */
289		if (min_uV <= uV && uV <= max_uV)
290			break;
291	}
292
293	*selector = vsel;
294
295	/* write to the register in case we found a match */
296	if (vsel == tps->info[dcdc]->table_len)
297		return -EINVAL;
298	else
299		return tps_65023_reg_write(tps, TPS65023_REG_DEF_CORE, vsel);
300}
301
302static int tps65023_ldo_get_voltage(struct regulator_dev *dev)
303{
304	struct tps_pmic *tps = rdev_get_drvdata(dev);
305	int data, ldo = rdev_get_id(dev);
 
 
 
 
 
 
306
307	if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
308		return -EINVAL;
 
 
 
 
 
 
 
 
309
310	data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
311	if (data < 0)
312		return data;
313
314	data >>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1));
315	data &= (tps->info[ldo]->table_len - 1);
316	return tps->info[ldo]->table[data] * 1000;
317}
318
319static int tps65023_ldo_set_voltage(struct regulator_dev *dev,
320				    int min_uV, int max_uV, unsigned *selector)
321{
322	struct tps_pmic *tps = rdev_get_drvdata(dev);
323	int data, vsel, ldo = rdev_get_id(dev);
 
 
324
325	if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
326		return -EINVAL;
 
 
 
 
 
327
328	if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
329		return -EINVAL;
330	if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
331		return -EINVAL;
332
333	for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
334		int mV = tps->info[ldo]->table[vsel];
335		int uV = mV * 1000;
336
337		/* Break at the first in-range value */
338		if (min_uV <= uV && uV <= max_uV)
339			break;
340	}
341
342	if (vsel == tps->info[ldo]->table_len)
343		return -EINVAL;
344
345	*selector = vsel;
346
347	data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
348	if (data < 0)
349		return data;
350
351	data &= TPS65023_LDO_CTRL_LDOx_MASK(ldo - TPS65023_LDO_1);
352	data |= (vsel << (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1)));
353	return tps_65023_reg_write(tps, TPS65023_REG_LDO_CTRL, data);
354}
355
356static int tps65023_dcdc_list_voltage(struct regulator_dev *dev,
357					unsigned selector)
358{
359	struct tps_pmic *tps = rdev_get_drvdata(dev);
360	int dcdc = rdev_get_id(dev);
361
362	if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
363		return -EINVAL;
364
365	if (dcdc == TPS65023_DCDC_1) {
366		if (selector >= tps->info[dcdc]->table_len)
367			return -EINVAL;
368		else
369			return tps->info[dcdc]->table[selector] * 1000;
370	} else
371		return tps->info[dcdc]->min_uV;
372}
373
374static int tps65023_ldo_list_voltage(struct regulator_dev *dev,
375					unsigned selector)
376{
377	struct tps_pmic *tps = rdev_get_drvdata(dev);
378	int ldo = rdev_get_id(dev);
379
380	if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
381		return -EINVAL;
382
383	if (selector >= tps->info[ldo]->table_len)
384		return -EINVAL;
385	else
386		return tps->info[ldo]->table[selector] * 1000;
387}
388
389/* Operations permitted on VDCDCx */
390static struct regulator_ops tps65023_dcdc_ops = {
391	.is_enabled = tps65023_dcdc_is_enabled,
392	.enable = tps65023_dcdc_enable,
393	.disable = tps65023_dcdc_disable,
394	.get_voltage = tps65023_dcdc_get_voltage,
395	.set_voltage = tps65023_dcdc_set_voltage,
396	.list_voltage = tps65023_dcdc_list_voltage,
397};
398
399/* Operations permitted on LDOx */
400static struct regulator_ops tps65023_ldo_ops = {
401	.is_enabled = tps65023_ldo_is_enabled,
402	.enable = tps65023_ldo_enable,
403	.disable = tps65023_ldo_disable,
404	.get_voltage = tps65023_ldo_get_voltage,
405	.set_voltage = tps65023_ldo_set_voltage,
406	.list_voltage = tps65023_ldo_list_voltage,
407};
408
409static struct regmap_config tps65023_regmap_config = {
410	.reg_bits = 8,
411	.val_bits = 8,
412};
413
414static int __devinit tps_65023_probe(struct i2c_client *client,
415				     const struct i2c_device_id *id)
416{
417	const struct tps_info *info = (void *)id->driver_data;
418	struct regulator_init_data *init_data;
419	struct regulator_dev *rdev;
420	struct tps_pmic *tps;
421	int i;
422	int error;
423
424	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
425		return -EIO;
426
427	/**
428	 * init_data points to array of regulator_init structures
429	 * coming from the board-evm file.
430	 */
431	init_data = client->dev.platform_data;
432	if (!init_data)
433		return -EIO;
434
435	tps = kzalloc(sizeof(*tps), GFP_KERNEL);
436	if (!tps)
437		return -ENOMEM;
438
439	tps->regmap = regmap_init_i2c(client, &tps65023_regmap_config);
 
 
440	if (IS_ERR(tps->regmap)) {
441		error = PTR_ERR(tps->regmap);
442		dev_err(&client->dev, "Failed to allocate register map: %d\n",
443			error);
444		goto fail_alloc;
445	}
446
447	/* common for all regulators */
448	tps->client = client;
449
450	for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) {
451		/* Store regulator specific information */
452		tps->info[i] = info;
453
454		tps->desc[i].name = info->name;
455		tps->desc[i].id = i;
456		tps->desc[i].n_voltages = num_voltages[i];
457		tps->desc[i].ops = (i > TPS65023_DCDC_3 ?
458					&tps65023_ldo_ops : &tps65023_dcdc_ops);
459		tps->desc[i].type = REGULATOR_VOLTAGE;
460		tps->desc[i].owner = THIS_MODULE;
461
462		/* Register the regulators */
463		rdev = regulator_register(&tps->desc[i], &client->dev,
464					  init_data, tps);
465		if (IS_ERR(rdev)) {
466			dev_err(&client->dev, "failed to register %s\n",
467				id->name);
468			error = PTR_ERR(rdev);
469			goto fail;
470		}
471
472		/* Save regulator for cleanup */
473		tps->rdev[i] = rdev;
474	}
475
476	i2c_set_clientdata(client, tps);
477
478	return 0;
479
480 fail:
481	while (--i >= 0)
482		regulator_unregister(tps->rdev[i]);
483
484	regmap_exit(tps->regmap);
485 fail_alloc:
486	kfree(tps);
487	return error;
488}
489
490/**
491 * tps_65023_remove - TPS65023 driver i2c remove handler
492 * @client: i2c driver client device structure
493 *
494 * Unregister TPS driver as an i2c client device driver
495 */
496static int __devexit tps_65023_remove(struct i2c_client *client)
497{
498	struct tps_pmic *tps = i2c_get_clientdata(client);
499	int i;
500
501	for (i = 0; i < TPS65023_NUM_REGULATOR; i++)
502		regulator_unregister(tps->rdev[i]);
503
504	regmap_exit(tps->regmap);
505	kfree(tps);
506
507	return 0;
508}
509
510static const struct tps_info tps65023_regs[] = {
511	{
512		.name = "VDCDC1",
513		.min_uV =  800000,
514		.max_uV = 1600000,
515		.table_len = ARRAY_SIZE(VDCDC1_VSEL_table),
516		.table = VDCDC1_VSEL_table,
517	},
518	{
519		.name = "VDCDC2",
520		.min_uV =  3300000,
521		.max_uV = 3300000,
522		.fixed = 1,
523	},
524	{
525		.name = "VDCDC3",
526		.min_uV =  1800000,
527		.max_uV = 1800000,
528		.fixed = 1,
529	},
530	{
531		.name = "LDO1",
532		.min_uV = 1000000,
533		.max_uV = 3150000,
534		.table_len = ARRAY_SIZE(LDO1_VSEL_table),
535		.table = LDO1_VSEL_table,
536	},
537	{
538		.name = "LDO2",
539		.min_uV = 1050000,
540		.max_uV = 3300000,
541		.table_len = ARRAY_SIZE(LDO2_VSEL_table),
542		.table = LDO2_VSEL_table,
543	},
544};
 
545
546static const struct i2c_device_id tps_65023_id[] = {
547	{.name = "tps65023",
548	.driver_data = (unsigned long) tps65023_regs,},
549	{.name = "tps65021",
550	.driver_data = (unsigned long) tps65023_regs,},
 
 
 
 
 
 
551	{ },
552};
553
554MODULE_DEVICE_TABLE(i2c, tps_65023_id);
555
556static struct i2c_driver tps_65023_i2c_driver = {
557	.driver = {
558		.name = "tps65023",
559		.owner = THIS_MODULE,
560	},
561	.probe = tps_65023_probe,
562	.remove = __devexit_p(tps_65023_remove),
563	.id_table = tps_65023_id,
564};
565
566/**
567 * tps_65023_init
568 *
569 * Module init function
570 */
571static int __init tps_65023_init(void)
572{
573	return i2c_add_driver(&tps_65023_i2c_driver);
574}
575subsys_initcall(tps_65023_init);
576
577/**
578 * tps_65023_cleanup
579 *
580 * Module exit function
581 */
582static void __exit tps_65023_cleanup(void)
583{
584	i2c_del_driver(&tps_65023_i2c_driver);
585}
586module_exit(tps_65023_cleanup);
587
588MODULE_AUTHOR("Texas Instruments");
589MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
590MODULE_LICENSE("GPL v2");