xref: /linux/drivers/regulator/tps65023-regulator.c (revision b889fcf63cb62e7fdb7816565e28f44dbe4a76a5)
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/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 /* Supported voltage values for regulators */
90 static const unsigned int VCORE_VSEL_table[] = {
91 	800000, 825000, 850000, 875000,
92 	900000, 925000, 950000, 975000,
93 	1000000, 1025000, 1050000, 1075000,
94 	1100000, 1125000, 1150000, 1175000,
95 	1200000, 1225000, 1250000, 1275000,
96 	1300000, 1325000, 1350000, 1375000,
97 	1400000, 1425000, 1450000, 1475000,
98 	1500000, 1525000, 1550000, 1600000,
99 };
100 
101 static const unsigned int DCDC_FIXED_3300000_VSEL_table[] = {
102 	3300000,
103 };
104 
105 static const unsigned int DCDC_FIXED_1800000_VSEL_table[] = {
106 	1800000,
107 };
108 
109 /* Supported voltage values for LDO regulators for tps65020 */
110 static const unsigned int TPS65020_LDO1_VSEL_table[] = {
111 	1000000, 1050000, 1100000, 1300000,
112 	1800000, 2500000, 3000000, 3300000,
113 };
114 
115 static const unsigned int TPS65020_LDO2_VSEL_table[] = {
116 	1000000, 1050000, 1100000, 1300000,
117 	1800000, 2500000, 3000000, 3300000,
118 };
119 
120 /* Supported voltage values for LDO regulators
121  * for tps65021 and tps65023 */
122 static const unsigned int TPS65023_LDO1_VSEL_table[] = {
123 	1000000, 1100000, 1300000, 1800000,
124 	2200000, 2600000, 2800000, 3150000,
125 };
126 
127 static const unsigned int TPS65023_LDO2_VSEL_table[] = {
128 	1050000, 1200000, 1300000, 1800000,
129 	2500000, 2800000, 3000000, 3300000,
130 };
131 
132 /* Regulator specific details */
133 struct tps_info {
134 	const char *name;
135 	u8 table_len;
136 	const unsigned int *table;
137 };
138 
139 /* PMIC details */
140 struct tps_pmic {
141 	struct regulator_desc desc[TPS65023_NUM_REGULATOR];
142 	struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
143 	const struct tps_info *info[TPS65023_NUM_REGULATOR];
144 	struct regmap *regmap;
145 	u8 core_regulator;
146 };
147 
148 /* Struct passed as driver data */
149 struct tps_driver_data {
150 	const struct tps_info *info;
151 	u8 core_regulator;
152 };
153 
154 static int tps65023_dcdc_get_voltage_sel(struct regulator_dev *dev)
155 {
156 	struct tps_pmic *tps = rdev_get_drvdata(dev);
157 	int ret;
158 	int data, dcdc = rdev_get_id(dev);
159 
160 	if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
161 		return -EINVAL;
162 
163 	if (dcdc == tps->core_regulator) {
164 		ret = regmap_read(tps->regmap, TPS65023_REG_DEF_CORE, &data);
165 		if (ret != 0)
166 			return ret;
167 		data &= (tps->info[dcdc]->table_len - 1);
168 		return data;
169 	} else
170 		return 0;
171 }
172 
173 static int tps65023_dcdc_set_voltage_sel(struct regulator_dev *dev,
174 					 unsigned selector)
175 {
176 	struct tps_pmic *tps = rdev_get_drvdata(dev);
177 	int dcdc = rdev_get_id(dev);
178 	int ret;
179 
180 	if (dcdc != tps->core_regulator)
181 		return -EINVAL;
182 
183 	ret = regmap_write(tps->regmap, TPS65023_REG_DEF_CORE, selector);
184 	if (ret)
185 		goto out;
186 
187 	/* Tell the chip that we have changed the value in DEFCORE
188 	 * and its time to update the core voltage
189 	 */
190 	ret = regmap_update_bits(tps->regmap, TPS65023_REG_CON_CTRL2,
191 				 TPS65023_REG_CTRL2_GO, TPS65023_REG_CTRL2_GO);
192 
193 out:
194 	return ret;
195 }
196 
197 /* Operations permitted on VDCDCx */
198 static struct regulator_ops tps65023_dcdc_ops = {
199 	.is_enabled = regulator_is_enabled_regmap,
200 	.enable = regulator_enable_regmap,
201 	.disable = regulator_disable_regmap,
202 	.get_voltage_sel = tps65023_dcdc_get_voltage_sel,
203 	.set_voltage_sel = tps65023_dcdc_set_voltage_sel,
204 	.list_voltage = regulator_list_voltage_table,
205 };
206 
207 /* Operations permitted on LDOx */
208 static struct regulator_ops tps65023_ldo_ops = {
209 	.is_enabled = regulator_is_enabled_regmap,
210 	.enable = regulator_enable_regmap,
211 	.disable = regulator_disable_regmap,
212 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
213 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
214 	.list_voltage = regulator_list_voltage_table,
215 };
216 
217 static struct regmap_config tps65023_regmap_config = {
218 	.reg_bits = 8,
219 	.val_bits = 8,
220 };
221 
222 static int tps_65023_probe(struct i2c_client *client,
223 				     const struct i2c_device_id *id)
224 {
225 	const struct tps_driver_data *drv_data = (void *)id->driver_data;
226 	const struct tps_info *info = drv_data->info;
227 	struct regulator_config config = { };
228 	struct regulator_init_data *init_data;
229 	struct regulator_dev *rdev;
230 	struct tps_pmic *tps;
231 	int i;
232 	int error;
233 
234 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
235 		return -EIO;
236 
237 	/**
238 	 * init_data points to array of regulator_init structures
239 	 * coming from the board-evm file.
240 	 */
241 	init_data = client->dev.platform_data;
242 	if (!init_data)
243 		return -EIO;
244 
245 	tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
246 	if (!tps)
247 		return -ENOMEM;
248 
249 	tps->regmap = devm_regmap_init_i2c(client, &tps65023_regmap_config);
250 	if (IS_ERR(tps->regmap)) {
251 		error = PTR_ERR(tps->regmap);
252 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
253 			error);
254 		return error;
255 	}
256 
257 	/* common for all regulators */
258 	tps->core_regulator = drv_data->core_regulator;
259 
260 	for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) {
261 		/* Store regulator specific information */
262 		tps->info[i] = info;
263 
264 		tps->desc[i].name = info->name;
265 		tps->desc[i].id = i;
266 		tps->desc[i].n_voltages = info->table_len;
267 		tps->desc[i].volt_table = info->table;
268 		tps->desc[i].ops = (i > TPS65023_DCDC_3 ?
269 					&tps65023_ldo_ops : &tps65023_dcdc_ops);
270 		tps->desc[i].type = REGULATOR_VOLTAGE;
271 		tps->desc[i].owner = THIS_MODULE;
272 
273 		tps->desc[i].enable_reg = TPS65023_REG_REG_CTRL;
274 		switch (i) {
275 		case TPS65023_LDO_1:
276 			tps->desc[i].vsel_reg = TPS65023_REG_LDO_CTRL;
277 			tps->desc[i].vsel_mask = 0x07;
278 			tps->desc[i].enable_mask = 1 << 1;
279 			break;
280 		case TPS65023_LDO_2:
281 			tps->desc[i].vsel_reg = TPS65023_REG_LDO_CTRL;
282 			tps->desc[i].vsel_mask = 0x70;
283 			tps->desc[i].enable_mask = 1 << 2;
284 			break;
285 		default: /* DCDCx */
286 			tps->desc[i].enable_mask =
287 					1 << (TPS65023_NUM_REGULATOR - i);
288 		}
289 
290 		config.dev = &client->dev;
291 		config.init_data = init_data;
292 		config.driver_data = tps;
293 		config.regmap = tps->regmap;
294 
295 		/* Register the regulators */
296 		rdev = regulator_register(&tps->desc[i], &config);
297 		if (IS_ERR(rdev)) {
298 			dev_err(&client->dev, "failed to register %s\n",
299 				id->name);
300 			error = PTR_ERR(rdev);
301 			goto fail;
302 		}
303 
304 		/* Save regulator for cleanup */
305 		tps->rdev[i] = rdev;
306 	}
307 
308 	i2c_set_clientdata(client, tps);
309 
310 	/* Enable setting output voltage by I2C */
311 	regmap_update_bits(tps->regmap, TPS65023_REG_CON_CTRL2,
312 			TPS65023_REG_CTRL2_CORE_ADJ, TPS65023_REG_CTRL2_CORE_ADJ);
313 
314 	return 0;
315 
316  fail:
317 	while (--i >= 0)
318 		regulator_unregister(tps->rdev[i]);
319 	return error;
320 }
321 
322 static int tps_65023_remove(struct i2c_client *client)
323 {
324 	struct tps_pmic *tps = i2c_get_clientdata(client);
325 	int i;
326 
327 	for (i = 0; i < TPS65023_NUM_REGULATOR; i++)
328 		regulator_unregister(tps->rdev[i]);
329 	return 0;
330 }
331 
332 static const struct tps_info tps65020_regs[] = {
333 	{
334 		.name = "VDCDC1",
335 		.table_len = ARRAY_SIZE(DCDC_FIXED_3300000_VSEL_table),
336 		.table = DCDC_FIXED_3300000_VSEL_table,
337 	},
338 	{
339 		.name = "VDCDC2",
340 		.table_len = ARRAY_SIZE(DCDC_FIXED_1800000_VSEL_table),
341 		.table = DCDC_FIXED_1800000_VSEL_table,
342 	},
343 	{
344 		.name = "VDCDC3",
345 		.table_len = ARRAY_SIZE(VCORE_VSEL_table),
346 		.table = VCORE_VSEL_table,
347 	},
348 	{
349 		.name = "LDO1",
350 		.table_len = ARRAY_SIZE(TPS65020_LDO1_VSEL_table),
351 		.table = TPS65020_LDO1_VSEL_table,
352 	},
353 	{
354 		.name = "LDO2",
355 		.table_len = ARRAY_SIZE(TPS65020_LDO2_VSEL_table),
356 		.table = TPS65020_LDO2_VSEL_table,
357 	},
358 };
359 
360 static const struct tps_info tps65021_regs[] = {
361 	{
362 		.name = "VDCDC1",
363 		.table_len = ARRAY_SIZE(DCDC_FIXED_3300000_VSEL_table),
364 		.table = DCDC_FIXED_3300000_VSEL_table,
365 	},
366 	{
367 		.name = "VDCDC2",
368 		.table_len = ARRAY_SIZE(DCDC_FIXED_1800000_VSEL_table),
369 		.table = DCDC_FIXED_1800000_VSEL_table,
370 	},
371 	{
372 		.name = "VDCDC3",
373 		.table_len = ARRAY_SIZE(VCORE_VSEL_table),
374 		.table = VCORE_VSEL_table,
375 	},
376 	{
377 		.name = "LDO1",
378 		.table_len = ARRAY_SIZE(TPS65023_LDO1_VSEL_table),
379 		.table = TPS65023_LDO1_VSEL_table,
380 	},
381 	{
382 		.name = "LDO2",
383 		.table_len = ARRAY_SIZE(TPS65023_LDO2_VSEL_table),
384 		.table = TPS65023_LDO2_VSEL_table,
385 	},
386 };
387 
388 static const struct tps_info tps65023_regs[] = {
389 	{
390 		.name = "VDCDC1",
391 		.table_len = ARRAY_SIZE(VCORE_VSEL_table),
392 		.table = VCORE_VSEL_table,
393 	},
394 	{
395 		.name = "VDCDC2",
396 		.table_len = ARRAY_SIZE(DCDC_FIXED_3300000_VSEL_table),
397 		.table = DCDC_FIXED_3300000_VSEL_table,
398 	},
399 	{
400 		.name = "VDCDC3",
401 		.table_len = ARRAY_SIZE(DCDC_FIXED_1800000_VSEL_table),
402 		.table = DCDC_FIXED_1800000_VSEL_table,
403 	},
404 	{
405 		.name = "LDO1",
406 		.table_len = ARRAY_SIZE(TPS65023_LDO1_VSEL_table),
407 		.table = TPS65023_LDO1_VSEL_table,
408 	},
409 	{
410 		.name = "LDO2",
411 		.table_len = ARRAY_SIZE(TPS65023_LDO2_VSEL_table),
412 		.table = TPS65023_LDO2_VSEL_table,
413 	},
414 };
415 
416 static struct tps_driver_data tps65020_drv_data = {
417 	.info = tps65020_regs,
418 	.core_regulator = TPS65023_DCDC_3,
419 };
420 
421 static struct tps_driver_data tps65021_drv_data = {
422 	.info = tps65021_regs,
423 	.core_regulator = TPS65023_DCDC_3,
424 };
425 
426 static struct tps_driver_data tps65023_drv_data = {
427 	.info = tps65023_regs,
428 	.core_regulator = TPS65023_DCDC_1,
429 };
430 
431 static const struct i2c_device_id tps_65023_id[] = {
432 	{.name = "tps65023",
433 	.driver_data = (unsigned long) &tps65023_drv_data},
434 	{.name = "tps65021",
435 	.driver_data = (unsigned long) &tps65021_drv_data,},
436 	{.name = "tps65020",
437 	.driver_data = (unsigned long) &tps65020_drv_data},
438 	{ },
439 };
440 
441 MODULE_DEVICE_TABLE(i2c, tps_65023_id);
442 
443 static struct i2c_driver tps_65023_i2c_driver = {
444 	.driver = {
445 		.name = "tps65023",
446 		.owner = THIS_MODULE,
447 	},
448 	.probe = tps_65023_probe,
449 	.remove = tps_65023_remove,
450 	.id_table = tps_65023_id,
451 };
452 
453 static int __init tps_65023_init(void)
454 {
455 	return i2c_add_driver(&tps_65023_i2c_driver);
456 }
457 subsys_initcall(tps_65023_init);
458 
459 static void __exit tps_65023_cleanup(void)
460 {
461 	i2c_del_driver(&tps_65023_i2c_driver);
462 }
463 module_exit(tps_65023_cleanup);
464 
465 MODULE_AUTHOR("Texas Instruments");
466 MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
467 MODULE_LICENSE("GPL v2");
468