1 /*
2  * tps6507x-regulator.c
3  *
4  * Regulator driver for TPS65073 PMIC
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/regulator/tps6507x.h>
26 #include <linux/slab.h>
27 #include <linux/mfd/tps6507x.h>
28 
29 /* DCDC's */
30 #define TPS6507X_DCDC_1				0
31 #define TPS6507X_DCDC_2				1
32 #define TPS6507X_DCDC_3				2
33 /* LDOs */
34 #define TPS6507X_LDO_1				3
35 #define TPS6507X_LDO_2				4
36 
37 #define TPS6507X_MAX_REG_ID			TPS6507X_LDO_2
38 
39 /* Number of step-down converters available */
40 #define TPS6507X_NUM_DCDC			3
41 /* Number of LDO voltage regulators  available */
42 #define TPS6507X_NUM_LDO			2
43 /* Number of total regulators available */
44 #define TPS6507X_NUM_REGULATOR		(TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)
45 
46 /* Supported voltage values for regulators (in microVolts) */
47 static const unsigned int VDCDCx_VSEL_table[] = {
48 	725000, 750000, 775000, 800000,
49 	825000, 850000, 875000, 900000,
50 	925000, 950000, 975000, 1000000,
51 	1025000, 1050000, 1075000, 1100000,
52 	1125000, 1150000, 1175000, 1200000,
53 	1225000, 1250000, 1275000, 1300000,
54 	1325000, 1350000, 1375000, 1400000,
55 	1425000, 1450000, 1475000, 1500000,
56 	1550000, 1600000, 1650000, 1700000,
57 	1750000, 1800000, 1850000, 1900000,
58 	1950000, 2000000, 2050000, 2100000,
59 	2150000, 2200000, 2250000, 2300000,
60 	2350000, 2400000, 2450000, 2500000,
61 	2550000, 2600000, 2650000, 2700000,
62 	2750000, 2800000, 2850000, 2900000,
63 	3000000, 3100000, 3200000, 3300000,
64 };
65 
66 static const unsigned int LDO1_VSEL_table[] = {
67 	1000000, 1100000, 1200000, 1250000,
68 	1300000, 1350000, 1400000, 1500000,
69 	1600000, 1800000, 2500000, 2750000,
70 	2800000, 3000000, 3100000, 3300000,
71 };
72 
73 /* The voltage mapping table for LDO2 is the same as VDCDCx */
74 #define LDO2_VSEL_table VDCDCx_VSEL_table
75 
76 struct tps_info {
77 	const char *name;
78 	u8 table_len;
79 	const unsigned int *table;
80 
81 	/* Does DCDC high or the low register defines output voltage? */
82 	bool defdcdc_default;
83 };
84 
85 static struct tps_info tps6507x_pmic_regs[] = {
86 	{
87 		.name = "VDCDC1",
88 		.table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
89 		.table = VDCDCx_VSEL_table,
90 	},
91 	{
92 		.name = "VDCDC2",
93 		.table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
94 		.table = VDCDCx_VSEL_table,
95 	},
96 	{
97 		.name = "VDCDC3",
98 		.table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
99 		.table = VDCDCx_VSEL_table,
100 	},
101 	{
102 		.name = "LDO1",
103 		.table_len = ARRAY_SIZE(LDO1_VSEL_table),
104 		.table = LDO1_VSEL_table,
105 	},
106 	{
107 		.name = "LDO2",
108 		.table_len = ARRAY_SIZE(LDO2_VSEL_table),
109 		.table = LDO2_VSEL_table,
110 	},
111 };
112 
113 struct tps6507x_pmic {
114 	struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
115 	struct tps6507x_dev *mfd;
116 	struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
117 	struct tps_info *info[TPS6507X_NUM_REGULATOR];
118 	struct mutex io_lock;
119 };
120 static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
121 {
122 	u8 val;
123 	int err;
124 
125 	err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);
126 
127 	if (err)
128 		return err;
129 
130 	return val;
131 }
132 
133 static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
134 {
135 	return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
136 }
137 
138 static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
139 {
140 	int err, data;
141 
142 	mutex_lock(&tps->io_lock);
143 
144 	data = tps6507x_pmic_read(tps, reg);
145 	if (data < 0) {
146 		dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
147 		err = data;
148 		goto out;
149 	}
150 
151 	data |= mask;
152 	err = tps6507x_pmic_write(tps, reg, data);
153 	if (err)
154 		dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
155 
156 out:
157 	mutex_unlock(&tps->io_lock);
158 	return err;
159 }
160 
161 static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
162 {
163 	int err, data;
164 
165 	mutex_lock(&tps->io_lock);
166 
167 	data = tps6507x_pmic_read(tps, reg);
168 	if (data < 0) {
169 		dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
170 		err = data;
171 		goto out;
172 	}
173 
174 	data &= ~mask;
175 	err = tps6507x_pmic_write(tps, reg, data);
176 	if (err)
177 		dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
178 
179 out:
180 	mutex_unlock(&tps->io_lock);
181 	return err;
182 }
183 
184 static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
185 {
186 	int data;
187 
188 	mutex_lock(&tps->io_lock);
189 
190 	data = tps6507x_pmic_read(tps, reg);
191 	if (data < 0)
192 		dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
193 
194 	mutex_unlock(&tps->io_lock);
195 	return data;
196 }
197 
198 static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
199 {
200 	int err;
201 
202 	mutex_lock(&tps->io_lock);
203 
204 	err = tps6507x_pmic_write(tps, reg, val);
205 	if (err < 0)
206 		dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
207 
208 	mutex_unlock(&tps->io_lock);
209 	return err;
210 }
211 
212 static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
213 {
214 	struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
215 	int data, rid = rdev_get_id(dev);
216 	u8 shift;
217 
218 	if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
219 		return -EINVAL;
220 
221 	shift = TPS6507X_MAX_REG_ID - rid;
222 	data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
223 
224 	if (data < 0)
225 		return data;
226 	else
227 		return (data & 1<<shift) ? 1 : 0;
228 }
229 
230 static int tps6507x_pmic_enable(struct regulator_dev *dev)
231 {
232 	struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
233 	int rid = rdev_get_id(dev);
234 	u8 shift;
235 
236 	if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
237 		return -EINVAL;
238 
239 	shift = TPS6507X_MAX_REG_ID - rid;
240 	return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
241 }
242 
243 static int tps6507x_pmic_disable(struct regulator_dev *dev)
244 {
245 	struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
246 	int rid = rdev_get_id(dev);
247 	u8 shift;
248 
249 	if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
250 		return -EINVAL;
251 
252 	shift = TPS6507X_MAX_REG_ID - rid;
253 	return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
254 					1 << shift);
255 }
256 
257 static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev)
258 {
259 	struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
260 	int data, rid = rdev_get_id(dev);
261 	u8 reg, mask;
262 
263 	switch (rid) {
264 	case TPS6507X_DCDC_1:
265 		reg = TPS6507X_REG_DEFDCDC1;
266 		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
267 		break;
268 	case TPS6507X_DCDC_2:
269 		if (tps->info[rid]->defdcdc_default)
270 			reg = TPS6507X_REG_DEFDCDC2_HIGH;
271 		else
272 			reg = TPS6507X_REG_DEFDCDC2_LOW;
273 		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
274 		break;
275 	case TPS6507X_DCDC_3:
276 		if (tps->info[rid]->defdcdc_default)
277 			reg = TPS6507X_REG_DEFDCDC3_HIGH;
278 		else
279 			reg = TPS6507X_REG_DEFDCDC3_LOW;
280 		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
281 		break;
282 	case TPS6507X_LDO_1:
283 		reg = TPS6507X_REG_LDO_CTRL1;
284 		mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
285 		break;
286 	case TPS6507X_LDO_2:
287 		reg = TPS6507X_REG_DEFLDO2;
288 		mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
289 		break;
290 	default:
291 		return -EINVAL;
292 	}
293 
294 	data = tps6507x_pmic_reg_read(tps, reg);
295 	if (data < 0)
296 		return data;
297 
298 	data &= mask;
299 	return data;
300 }
301 
302 static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
303 					  unsigned selector)
304 {
305 	struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
306 	int data, rid = rdev_get_id(dev);
307 	u8 reg, mask;
308 
309 	switch (rid) {
310 	case TPS6507X_DCDC_1:
311 		reg = TPS6507X_REG_DEFDCDC1;
312 		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
313 		break;
314 	case TPS6507X_DCDC_2:
315 		if (tps->info[rid]->defdcdc_default)
316 			reg = TPS6507X_REG_DEFDCDC2_HIGH;
317 		else
318 			reg = TPS6507X_REG_DEFDCDC2_LOW;
319 		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
320 		break;
321 	case TPS6507X_DCDC_3:
322 		if (tps->info[rid]->defdcdc_default)
323 			reg = TPS6507X_REG_DEFDCDC3_HIGH;
324 		else
325 			reg = TPS6507X_REG_DEFDCDC3_LOW;
326 		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
327 		break;
328 	case TPS6507X_LDO_1:
329 		reg = TPS6507X_REG_LDO_CTRL1;
330 		mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
331 		break;
332 	case TPS6507X_LDO_2:
333 		reg = TPS6507X_REG_DEFLDO2;
334 		mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
335 		break;
336 	default:
337 		return -EINVAL;
338 	}
339 
340 	data = tps6507x_pmic_reg_read(tps, reg);
341 	if (data < 0)
342 		return data;
343 
344 	data &= ~mask;
345 	data |= selector;
346 
347 	return tps6507x_pmic_reg_write(tps, reg, data);
348 }
349 
350 static struct regulator_ops tps6507x_pmic_ops = {
351 	.is_enabled = tps6507x_pmic_is_enabled,
352 	.enable = tps6507x_pmic_enable,
353 	.disable = tps6507x_pmic_disable,
354 	.get_voltage_sel = tps6507x_pmic_get_voltage_sel,
355 	.set_voltage_sel = tps6507x_pmic_set_voltage_sel,
356 	.list_voltage = regulator_list_voltage_table,
357 };
358 
359 static __devinit int tps6507x_pmic_probe(struct platform_device *pdev)
360 {
361 	struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
362 	struct tps_info *info = &tps6507x_pmic_regs[0];
363 	struct regulator_config config = { };
364 	struct regulator_init_data *init_data;
365 	struct regulator_dev *rdev;
366 	struct tps6507x_pmic *tps;
367 	struct tps6507x_board *tps_board;
368 	int i;
369 	int error;
370 
371 	/**
372 	 * tps_board points to pmic related constants
373 	 * coming from the board-evm file.
374 	 */
375 
376 	tps_board = dev_get_platdata(tps6507x_dev->dev);
377 	if (!tps_board)
378 		return -EINVAL;
379 
380 	/**
381 	 * init_data points to array of regulator_init structures
382 	 * coming from the board-evm file.
383 	 */
384 	init_data = tps_board->tps6507x_pmic_init_data;
385 	if (!init_data)
386 		return -EINVAL;
387 
388 	tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
389 	if (!tps)
390 		return -ENOMEM;
391 
392 	mutex_init(&tps->io_lock);
393 
394 	/* common for all regulators */
395 	tps->mfd = tps6507x_dev;
396 
397 	for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
398 		/* Register the regulators */
399 		tps->info[i] = info;
400 		if (init_data->driver_data) {
401 			struct tps6507x_reg_platform_data *data =
402 							init_data->driver_data;
403 			tps->info[i]->defdcdc_default = data->defdcdc_default;
404 		}
405 
406 		tps->desc[i].name = info->name;
407 		tps->desc[i].id = i;
408 		tps->desc[i].n_voltages = info->table_len;
409 		tps->desc[i].volt_table = info->table;
410 		tps->desc[i].ops = &tps6507x_pmic_ops;
411 		tps->desc[i].type = REGULATOR_VOLTAGE;
412 		tps->desc[i].owner = THIS_MODULE;
413 
414 		config.dev = tps6507x_dev->dev;
415 		config.init_data = init_data;
416 		config.driver_data = tps;
417 
418 		rdev = regulator_register(&tps->desc[i], &config);
419 		if (IS_ERR(rdev)) {
420 			dev_err(tps6507x_dev->dev,
421 				"failed to register %s regulator\n",
422 				pdev->name);
423 			error = PTR_ERR(rdev);
424 			goto fail;
425 		}
426 
427 		/* Save regulator for cleanup */
428 		tps->rdev[i] = rdev;
429 	}
430 
431 	tps6507x_dev->pmic = tps;
432 	platform_set_drvdata(pdev, tps6507x_dev);
433 
434 	return 0;
435 
436 fail:
437 	while (--i >= 0)
438 		regulator_unregister(tps->rdev[i]);
439 	return error;
440 }
441 
442 static int __devexit tps6507x_pmic_remove(struct platform_device *pdev)
443 {
444 	struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
445 	struct tps6507x_pmic *tps = tps6507x_dev->pmic;
446 	int i;
447 
448 	for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
449 		regulator_unregister(tps->rdev[i]);
450 	return 0;
451 }
452 
453 static struct platform_driver tps6507x_pmic_driver = {
454 	.driver = {
455 		.name = "tps6507x-pmic",
456 		.owner = THIS_MODULE,
457 	},
458 	.probe = tps6507x_pmic_probe,
459 	.remove = __devexit_p(tps6507x_pmic_remove),
460 };
461 
462 static int __init tps6507x_pmic_init(void)
463 {
464 	return platform_driver_register(&tps6507x_pmic_driver);
465 }
466 subsys_initcall(tps6507x_pmic_init);
467 
468 static void __exit tps6507x_pmic_cleanup(void)
469 {
470 	platform_driver_unregister(&tps6507x_pmic_driver);
471 }
472 module_exit(tps6507x_pmic_cleanup);
473 
474 MODULE_AUTHOR("Texas Instruments");
475 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
476 MODULE_LICENSE("GPL v2");
477 MODULE_ALIAS("platform:tps6507x-pmic");
478