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