1 /*
2  * Driver for Regulator part of Palmas PMIC Chips
3  *
4  * Copyright 2011-2012 Texas Instruments Inc.
5  *
6  * Author: Graeme Gregory <gg@slimlogic.co.uk>
7  *
8  *  This program is free software; you can redistribute it and/or modify it
9  *  under  the terms of the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the License, or (at your
11  *  option) any later version.
12  *
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/slab.h>
23 #include <linux/regmap.h>
24 #include <linux/mfd/palmas.h>
25 #include <linux/of.h>
26 #include <linux/of_platform.h>
27 #include <linux/regulator/of_regulator.h>
28 
29 struct regs_info {
30 	char	*name;
31 	u8	vsel_addr;
32 	u8	ctrl_addr;
33 	u8	tstep_addr;
34 };
35 
36 static const struct regs_info palmas_regs_info[] = {
37 	{
38 		.name		= "SMPS12",
39 		.vsel_addr	= PALMAS_SMPS12_VOLTAGE,
40 		.ctrl_addr	= PALMAS_SMPS12_CTRL,
41 		.tstep_addr	= PALMAS_SMPS12_TSTEP,
42 	},
43 	{
44 		.name		= "SMPS123",
45 		.vsel_addr	= PALMAS_SMPS12_VOLTAGE,
46 		.ctrl_addr	= PALMAS_SMPS12_CTRL,
47 		.tstep_addr	= PALMAS_SMPS12_TSTEP,
48 	},
49 	{
50 		.name		= "SMPS3",
51 		.vsel_addr	= PALMAS_SMPS3_VOLTAGE,
52 		.ctrl_addr	= PALMAS_SMPS3_CTRL,
53 	},
54 	{
55 		.name		= "SMPS45",
56 		.vsel_addr	= PALMAS_SMPS45_VOLTAGE,
57 		.ctrl_addr	= PALMAS_SMPS45_CTRL,
58 		.tstep_addr	= PALMAS_SMPS45_TSTEP,
59 	},
60 	{
61 		.name		= "SMPS457",
62 		.vsel_addr	= PALMAS_SMPS45_VOLTAGE,
63 		.ctrl_addr	= PALMAS_SMPS45_CTRL,
64 		.tstep_addr	= PALMAS_SMPS45_TSTEP,
65 	},
66 	{
67 		.name		= "SMPS6",
68 		.vsel_addr	= PALMAS_SMPS6_VOLTAGE,
69 		.ctrl_addr	= PALMAS_SMPS6_CTRL,
70 		.tstep_addr	= PALMAS_SMPS6_TSTEP,
71 	},
72 	{
73 		.name		= "SMPS7",
74 		.vsel_addr	= PALMAS_SMPS7_VOLTAGE,
75 		.ctrl_addr	= PALMAS_SMPS7_CTRL,
76 	},
77 	{
78 		.name		= "SMPS8",
79 		.vsel_addr	= PALMAS_SMPS8_VOLTAGE,
80 		.ctrl_addr	= PALMAS_SMPS8_CTRL,
81 		.tstep_addr	= PALMAS_SMPS8_TSTEP,
82 	},
83 	{
84 		.name		= "SMPS9",
85 		.vsel_addr	= PALMAS_SMPS9_VOLTAGE,
86 		.ctrl_addr	= PALMAS_SMPS9_CTRL,
87 	},
88 	{
89 		.name		= "SMPS10",
90 	},
91 	{
92 		.name		= "LDO1",
93 		.vsel_addr	= PALMAS_LDO1_VOLTAGE,
94 		.ctrl_addr	= PALMAS_LDO1_CTRL,
95 	},
96 	{
97 		.name		= "LDO2",
98 		.vsel_addr	= PALMAS_LDO2_VOLTAGE,
99 		.ctrl_addr	= PALMAS_LDO2_CTRL,
100 	},
101 	{
102 		.name		= "LDO3",
103 		.vsel_addr	= PALMAS_LDO3_VOLTAGE,
104 		.ctrl_addr	= PALMAS_LDO3_CTRL,
105 	},
106 	{
107 		.name		= "LDO4",
108 		.vsel_addr	= PALMAS_LDO4_VOLTAGE,
109 		.ctrl_addr	= PALMAS_LDO4_CTRL,
110 	},
111 	{
112 		.name		= "LDO5",
113 		.vsel_addr	= PALMAS_LDO5_VOLTAGE,
114 		.ctrl_addr	= PALMAS_LDO5_CTRL,
115 	},
116 	{
117 		.name		= "LDO6",
118 		.vsel_addr	= PALMAS_LDO6_VOLTAGE,
119 		.ctrl_addr	= PALMAS_LDO6_CTRL,
120 	},
121 	{
122 		.name		= "LDO7",
123 		.vsel_addr	= PALMAS_LDO7_VOLTAGE,
124 		.ctrl_addr	= PALMAS_LDO7_CTRL,
125 	},
126 	{
127 		.name		= "LDO8",
128 		.vsel_addr	= PALMAS_LDO8_VOLTAGE,
129 		.ctrl_addr	= PALMAS_LDO8_CTRL,
130 	},
131 	{
132 		.name		= "LDO9",
133 		.vsel_addr	= PALMAS_LDO9_VOLTAGE,
134 		.ctrl_addr	= PALMAS_LDO9_CTRL,
135 	},
136 	{
137 		.name		= "LDOLN",
138 		.vsel_addr	= PALMAS_LDOLN_VOLTAGE,
139 		.ctrl_addr	= PALMAS_LDOLN_CTRL,
140 	},
141 	{
142 		.name		= "LDOUSB",
143 		.vsel_addr	= PALMAS_LDOUSB_VOLTAGE,
144 		.ctrl_addr	= PALMAS_LDOUSB_CTRL,
145 	},
146 };
147 
148 #define SMPS_CTRL_MODE_OFF		0x00
149 #define SMPS_CTRL_MODE_ON		0x01
150 #define SMPS_CTRL_MODE_ECO		0x02
151 #define SMPS_CTRL_MODE_PWM		0x03
152 
153 /* These values are derived from the data sheet. And are the number of steps
154  * where there is a voltage change, the ranges at beginning and end of register
155  * max/min values where there are no change are ommitted.
156  *
157  * So they are basically (maxV-minV)/stepV
158  */
159 #define PALMAS_SMPS_NUM_VOLTAGES	116
160 #define PALMAS_SMPS10_NUM_VOLTAGES	2
161 #define PALMAS_LDO_NUM_VOLTAGES		50
162 
163 #define SMPS10_VSEL			(1<<3)
164 #define SMPS10_BOOST_EN			(1<<2)
165 #define SMPS10_BYPASS_EN		(1<<1)
166 #define SMPS10_SWITCH_EN		(1<<0)
167 
168 #define REGULATOR_SLAVE			0
169 
170 static int palmas_smps_read(struct palmas *palmas, unsigned int reg,
171 		unsigned int *dest)
172 {
173 	unsigned int addr;
174 
175 	addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
176 
177 	return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
178 }
179 
180 static int palmas_smps_write(struct palmas *palmas, unsigned int reg,
181 		unsigned int value)
182 {
183 	unsigned int addr;
184 
185 	addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
186 
187 	return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
188 }
189 
190 static int palmas_ldo_read(struct palmas *palmas, unsigned int reg,
191 		unsigned int *dest)
192 {
193 	unsigned int addr;
194 
195 	addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
196 
197 	return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
198 }
199 
200 static int palmas_ldo_write(struct palmas *palmas, unsigned int reg,
201 		unsigned int value)
202 {
203 	unsigned int addr;
204 
205 	addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
206 
207 	return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
208 }
209 
210 static int palmas_is_enabled_smps(struct regulator_dev *dev)
211 {
212 	struct palmas_pmic *pmic = rdev_get_drvdata(dev);
213 	int id = rdev_get_id(dev);
214 	unsigned int reg;
215 
216 	palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
217 
218 	reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
219 	reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
220 
221 	return !!(reg);
222 }
223 
224 static int palmas_enable_smps(struct regulator_dev *dev)
225 {
226 	struct palmas_pmic *pmic = rdev_get_drvdata(dev);
227 	int id = rdev_get_id(dev);
228 	unsigned int reg;
229 
230 	palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
231 
232 	reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
233 	reg |= SMPS_CTRL_MODE_ON;
234 
235 	palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
236 
237 	return 0;
238 }
239 
240 static int palmas_disable_smps(struct regulator_dev *dev)
241 {
242 	struct palmas_pmic *pmic = rdev_get_drvdata(dev);
243 	int id = rdev_get_id(dev);
244 	unsigned int reg;
245 
246 	palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
247 
248 	reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
249 
250 	palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
251 
252 	return 0;
253 }
254 
255 
256 static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
257 {
258 	struct palmas_pmic *pmic = rdev_get_drvdata(dev);
259 	int id = rdev_get_id(dev);
260 	unsigned int reg;
261 
262 	palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
263 	reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
264 
265 	switch (mode) {
266 	case REGULATOR_MODE_NORMAL:
267 		reg |= SMPS_CTRL_MODE_ON;
268 		break;
269 	case REGULATOR_MODE_IDLE:
270 		reg |= SMPS_CTRL_MODE_ECO;
271 		break;
272 	case REGULATOR_MODE_FAST:
273 		reg |= SMPS_CTRL_MODE_PWM;
274 		break;
275 	default:
276 		return -EINVAL;
277 	}
278 	palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
279 
280 	return 0;
281 }
282 
283 static unsigned int palmas_get_mode_smps(struct regulator_dev *dev)
284 {
285 	struct palmas_pmic *pmic = rdev_get_drvdata(dev);
286 	int id = rdev_get_id(dev);
287 	unsigned int reg;
288 
289 	palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
290 	reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
291 	reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
292 
293 	switch (reg) {
294 	case SMPS_CTRL_MODE_ON:
295 		return REGULATOR_MODE_NORMAL;
296 	case SMPS_CTRL_MODE_ECO:
297 		return REGULATOR_MODE_IDLE;
298 	case SMPS_CTRL_MODE_PWM:
299 		return REGULATOR_MODE_FAST;
300 	}
301 
302 	return 0;
303 }
304 
305 static int palmas_list_voltage_smps(struct regulator_dev *dev,
306 					unsigned selector)
307 {
308 	struct palmas_pmic *pmic = rdev_get_drvdata(dev);
309 	int id = rdev_get_id(dev);
310 	int mult = 1;
311 
312 	/* Read the multiplier set in VSEL register to return
313 	 * the correct voltage.
314 	 */
315 	if (pmic->range[id])
316 		mult = 2;
317 
318 	if (selector == 0)
319 		return 0;
320 	else if (selector < 6)
321 		return 500000 * mult;
322 	else
323 		/* Voltage is linear mapping starting from selector 6,
324 		 * volt = (0.49V + ((selector - 5) * 0.01V)) * RANGE
325 		 * RANGE is either x1 or x2
326 		 */
327 		return (490000 + ((selector - 5) * 10000)) * mult;
328 }
329 
330 static int palmas_map_voltage_smps(struct regulator_dev *rdev,
331 		int min_uV, int max_uV)
332 {
333 	struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
334 	int id = rdev_get_id(rdev);
335 	int ret, voltage;
336 
337 	if (min_uV == 0)
338 		return 0;
339 
340 	if (pmic->range[id]) { /* RANGE is x2 */
341 		if (min_uV < 1000000)
342 			min_uV = 1000000;
343 		ret = DIV_ROUND_UP(min_uV - 1000000, 20000) + 6;
344 	} else {		/* RANGE is x1 */
345 		if (min_uV < 500000)
346 			min_uV = 500000;
347 		ret = DIV_ROUND_UP(min_uV - 500000, 10000) + 6;
348 	}
349 
350 	/* Map back into a voltage to verify we're still in bounds */
351 	voltage = palmas_list_voltage_smps(rdev, ret);
352 	if (voltage < min_uV || voltage > max_uV)
353 		return -EINVAL;
354 
355 	return ret;
356 }
357 
358 static struct regulator_ops palmas_ops_smps = {
359 	.is_enabled		= palmas_is_enabled_smps,
360 	.enable			= palmas_enable_smps,
361 	.disable		= palmas_disable_smps,
362 	.set_mode		= palmas_set_mode_smps,
363 	.get_mode		= palmas_get_mode_smps,
364 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
365 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
366 	.list_voltage		= palmas_list_voltage_smps,
367 	.map_voltage		= palmas_map_voltage_smps,
368 };
369 
370 static struct regulator_ops palmas_ops_smps10 = {
371 	.is_enabled		= regulator_is_enabled_regmap,
372 	.enable			= regulator_enable_regmap,
373 	.disable		= regulator_disable_regmap,
374 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
375 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
376 	.list_voltage		= regulator_list_voltage_linear,
377 	.map_voltage		= regulator_map_voltage_linear,
378 };
379 
380 static int palmas_is_enabled_ldo(struct regulator_dev *dev)
381 {
382 	struct palmas_pmic *pmic = rdev_get_drvdata(dev);
383 	int id = rdev_get_id(dev);
384 	unsigned int reg;
385 
386 	palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
387 
388 	reg &= PALMAS_LDO1_CTRL_STATUS;
389 
390 	return !!(reg);
391 }
392 
393 static struct regulator_ops palmas_ops_ldo = {
394 	.is_enabled		= palmas_is_enabled_ldo,
395 	.enable			= regulator_enable_regmap,
396 	.disable		= regulator_disable_regmap,
397 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
398 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
399 	.list_voltage		= regulator_list_voltage_linear,
400 	.map_voltage		= regulator_map_voltage_linear,
401 };
402 
403 /*
404  * setup the hardware based sleep configuration of the SMPS/LDO regulators
405  * from the platform data. This is different to the software based control
406  * supported by the regulator framework as it is controlled by toggling
407  * pins on the PMIC such as PREQ, SYSEN, ...
408  */
409 static int palmas_smps_init(struct palmas *palmas, int id,
410 		struct palmas_reg_init *reg_init)
411 {
412 	unsigned int reg;
413 	unsigned int addr;
414 	int ret;
415 
416 	addr = palmas_regs_info[id].ctrl_addr;
417 
418 	ret = palmas_smps_read(palmas, addr, &reg);
419 	if (ret)
420 		return ret;
421 
422 	switch (id) {
423 	case PALMAS_REG_SMPS10:
424 		if (reg_init->mode_sleep) {
425 			reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
426 			reg |= reg_init->mode_sleep <<
427 					PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
428 		}
429 		break;
430 	default:
431 		if (reg_init->warm_reset)
432 			reg |= PALMAS_SMPS12_CTRL_WR_S;
433 
434 		if (reg_init->roof_floor)
435 			reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
436 
437 		if (reg_init->mode_sleep) {
438 			reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
439 			reg |= reg_init->mode_sleep <<
440 					PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
441 		}
442 	}
443 
444 	ret = palmas_smps_write(palmas, addr, reg);
445 	if (ret)
446 		return ret;
447 
448 	if (palmas_regs_info[id].tstep_addr && reg_init->tstep) {
449 		addr = palmas_regs_info[id].tstep_addr;
450 
451 		reg = reg_init->tstep & PALMAS_SMPS12_TSTEP_TSTEP_MASK;
452 
453 		ret = palmas_smps_write(palmas, addr, reg);
454 		if (ret)
455 			return ret;
456 	}
457 
458 	if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
459 		addr = palmas_regs_info[id].vsel_addr;
460 
461 		reg = reg_init->vsel;
462 
463 		ret = palmas_smps_write(palmas, addr, reg);
464 		if (ret)
465 			return ret;
466 	}
467 
468 
469 	return 0;
470 }
471 
472 static int palmas_ldo_init(struct palmas *palmas, int id,
473 		struct palmas_reg_init *reg_init)
474 {
475 	unsigned int reg;
476 	unsigned int addr;
477 	int ret;
478 
479 	addr = palmas_regs_info[id].ctrl_addr;
480 
481 	ret = palmas_ldo_read(palmas, addr, &reg);
482 	if (ret)
483 		return ret;
484 
485 	if (reg_init->warm_reset)
486 		reg |= PALMAS_LDO1_CTRL_WR_S;
487 
488 	if (reg_init->mode_sleep)
489 		reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
490 
491 	ret = palmas_ldo_write(palmas, addr, reg);
492 	if (ret)
493 		return ret;
494 
495 	return 0;
496 }
497 
498 static struct of_regulator_match palmas_matches[] = {
499 	{ .name = "smps12", },
500 	{ .name = "smps123", },
501 	{ .name = "smps3", },
502 	{ .name = "smps45", },
503 	{ .name = "smps457", },
504 	{ .name = "smps6", },
505 	{ .name = "smps7", },
506 	{ .name = "smps8", },
507 	{ .name = "smps9", },
508 	{ .name = "smps10", },
509 	{ .name = "ldo1", },
510 	{ .name = "ldo2", },
511 	{ .name = "ldo3", },
512 	{ .name = "ldo4", },
513 	{ .name = "ldo5", },
514 	{ .name = "ldo6", },
515 	{ .name = "ldo7", },
516 	{ .name = "ldo8", },
517 	{ .name = "ldo9", },
518 	{ .name = "ldoln", },
519 	{ .name = "ldousb", },
520 };
521 
522 static void palmas_dt_to_pdata(struct device *dev,
523 		struct device_node *node,
524 		struct palmas_pmic_platform_data *pdata)
525 {
526 	struct device_node *regulators;
527 	u32 prop;
528 	int idx, ret;
529 
530 	regulators = of_find_node_by_name(node, "regulators");
531 	if (!regulators) {
532 		dev_info(dev, "regulator node not found\n");
533 		return;
534 	}
535 
536 	ret = of_regulator_match(dev, regulators, palmas_matches,
537 			PALMAS_NUM_REGS);
538 	if (ret < 0) {
539 		dev_err(dev, "Error parsing regulator init data: %d\n", ret);
540 		return;
541 	}
542 
543 	for (idx = 0; idx < PALMAS_NUM_REGS; idx++) {
544 		if (!palmas_matches[idx].init_data ||
545 				!palmas_matches[idx].of_node)
546 			continue;
547 
548 		pdata->reg_data[idx] = palmas_matches[idx].init_data;
549 
550 		pdata->reg_init[idx] = devm_kzalloc(dev,
551 				sizeof(struct palmas_reg_init), GFP_KERNEL);
552 
553 		ret = of_property_read_u32(palmas_matches[idx].of_node,
554 				"ti,warm_reset", &prop);
555 		if (!ret)
556 			pdata->reg_init[idx]->warm_reset = prop;
557 
558 		ret = of_property_read_u32(palmas_matches[idx].of_node,
559 				"ti,roof_floor", &prop);
560 		if (!ret)
561 			pdata->reg_init[idx]->roof_floor = prop;
562 
563 		ret = of_property_read_u32(palmas_matches[idx].of_node,
564 				"ti,mode_sleep", &prop);
565 		if (!ret)
566 			pdata->reg_init[idx]->mode_sleep = prop;
567 
568 		ret = of_property_read_u32(palmas_matches[idx].of_node,
569 				"ti,warm_reset", &prop);
570 		if (!ret)
571 			pdata->reg_init[idx]->warm_reset = prop;
572 
573 		ret = of_property_read_u32(palmas_matches[idx].of_node,
574 				"ti,tstep", &prop);
575 		if (!ret)
576 			pdata->reg_init[idx]->tstep = prop;
577 
578 		ret = of_property_read_u32(palmas_matches[idx].of_node,
579 				"ti,vsel", &prop);
580 		if (!ret)
581 			pdata->reg_init[idx]->vsel = prop;
582 	}
583 
584 	ret = of_property_read_u32(node, "ti,ldo6_vibrator", &prop);
585 	if (!ret)
586 		pdata->ldo6_vibrator = prop;
587 }
588 
589 
590 static int palmas_probe(struct platform_device *pdev)
591 {
592 	struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
593 	struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
594 	struct device_node *node = pdev->dev.of_node;
595 	struct regulator_dev *rdev;
596 	struct regulator_config config = { };
597 	struct palmas_pmic *pmic;
598 	struct palmas_reg_init *reg_init;
599 	int id = 0, ret;
600 	unsigned int addr, reg;
601 
602 	if (node && !pdata) {
603 		pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
604 
605 		if (!pdata)
606 			return -ENOMEM;
607 
608 		palmas_dt_to_pdata(&pdev->dev, node, pdata);
609 	}
610 
611 	pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
612 	if (!pmic)
613 		return -ENOMEM;
614 
615 	pmic->dev = &pdev->dev;
616 	pmic->palmas = palmas;
617 	palmas->pmic = pmic;
618 	platform_set_drvdata(pdev, pmic);
619 
620 	ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
621 	if (ret)
622 		return ret;
623 
624 	if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
625 		pmic->smps123 = 1;
626 
627 	if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
628 		pmic->smps457 = 1;
629 
630 	config.regmap = palmas->regmap[REGULATOR_SLAVE];
631 	config.dev = &pdev->dev;
632 	config.driver_data = pmic;
633 
634 	for (id = 0; id < PALMAS_REG_LDO1; id++) {
635 
636 		/*
637 		 * Miss out regulators which are not available due
638 		 * to slaving configurations.
639 		 */
640 		switch (id) {
641 		case PALMAS_REG_SMPS12:
642 		case PALMAS_REG_SMPS3:
643 			if (pmic->smps123)
644 				continue;
645 			break;
646 		case PALMAS_REG_SMPS123:
647 			if (!pmic->smps123)
648 				continue;
649 			break;
650 		case PALMAS_REG_SMPS45:
651 		case PALMAS_REG_SMPS7:
652 			if (pmic->smps457)
653 				continue;
654 			break;
655 		case PALMAS_REG_SMPS457:
656 			if (!pmic->smps457)
657 				continue;
658 		}
659 
660 		/* Initialise sleep/init values from platform data */
661 		if (pdata && pdata->reg_init[id]) {
662 			reg_init = pdata->reg_init[id];
663 			ret = palmas_smps_init(palmas, id, reg_init);
664 			if (ret)
665 				goto err_unregister_regulator;
666 		}
667 
668 		/* Register the regulators */
669 		pmic->desc[id].name = palmas_regs_info[id].name;
670 		pmic->desc[id].id = id;
671 
672 		switch (id) {
673 		case PALMAS_REG_SMPS10:
674 			pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
675 			pmic->desc[id].ops = &palmas_ops_smps10;
676 			pmic->desc[id].vsel_reg =
677 					PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
678 							PALMAS_SMPS10_CTRL);
679 			pmic->desc[id].vsel_mask = SMPS10_VSEL;
680 			pmic->desc[id].enable_reg =
681 					PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
682 							PALMAS_SMPS10_STATUS);
683 			pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
684 			pmic->desc[id].min_uV = 3750000;
685 			pmic->desc[id].uV_step = 1250000;
686 			break;
687 		default:
688 			/*
689 			 * Read and store the RANGE bit for later use
690 			 * This must be done before regulator is probed,
691 			 * otherwise we error in probe with unsupportable ranges.
692 			 */
693 			addr = palmas_regs_info[id].vsel_addr;
694 
695 			ret = palmas_smps_read(pmic->palmas, addr, &reg);
696 			if (ret)
697 				goto err_unregister_regulator;
698 			if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
699 				pmic->range[id] = 1;
700 
701 			pmic->desc[id].ops = &palmas_ops_smps;
702 			pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
703 			pmic->desc[id].vsel_reg =
704 					PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
705 						palmas_regs_info[id].vsel_addr);
706 			pmic->desc[id].vsel_mask =
707 					PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
708 		}
709 
710 		pmic->desc[id].type = REGULATOR_VOLTAGE;
711 		pmic->desc[id].owner = THIS_MODULE;
712 
713 		if (pdata)
714 			config.init_data = pdata->reg_data[id];
715 		else
716 			config.init_data = NULL;
717 
718 		config.of_node = palmas_matches[id].of_node;
719 
720 		rdev = regulator_register(&pmic->desc[id], &config);
721 		if (IS_ERR(rdev)) {
722 			dev_err(&pdev->dev,
723 				"failed to register %s regulator\n",
724 				pdev->name);
725 			ret = PTR_ERR(rdev);
726 			goto err_unregister_regulator;
727 		}
728 
729 		/* Save regulator for cleanup */
730 		pmic->rdev[id] = rdev;
731 	}
732 
733 	/* Start this loop from the id left from previous loop */
734 	for (; id < PALMAS_NUM_REGS; id++) {
735 
736 		/* Miss out regulators which are not available due
737 		 * to alternate functions.
738 		 */
739 
740 		/* Register the regulators */
741 		pmic->desc[id].name = palmas_regs_info[id].name;
742 		pmic->desc[id].id = id;
743 		pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
744 
745 		pmic->desc[id].ops = &palmas_ops_ldo;
746 
747 		pmic->desc[id].type = REGULATOR_VOLTAGE;
748 		pmic->desc[id].owner = THIS_MODULE;
749 		pmic->desc[id].min_uV = 900000;
750 		pmic->desc[id].uV_step = 50000;
751 		pmic->desc[id].linear_min_sel = 1;
752 		pmic->desc[id].vsel_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
753 						palmas_regs_info[id].vsel_addr);
754 		pmic->desc[id].vsel_mask = PALMAS_LDO1_VOLTAGE_VSEL_MASK;
755 		pmic->desc[id].enable_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
756 						palmas_regs_info[id].ctrl_addr);
757 		pmic->desc[id].enable_mask = PALMAS_LDO1_CTRL_MODE_ACTIVE;
758 
759 		if (pdata)
760 			config.init_data = pdata->reg_data[id];
761 		else
762 			config.init_data = NULL;
763 
764 		config.of_node = palmas_matches[id].of_node;
765 
766 		rdev = regulator_register(&pmic->desc[id], &config);
767 		if (IS_ERR(rdev)) {
768 			dev_err(&pdev->dev,
769 				"failed to register %s regulator\n",
770 				pdev->name);
771 			ret = PTR_ERR(rdev);
772 			goto err_unregister_regulator;
773 		}
774 
775 		/* Save regulator for cleanup */
776 		pmic->rdev[id] = rdev;
777 
778 		/* Initialise sleep/init values from platform data */
779 		if (pdata) {
780 			reg_init = pdata->reg_init[id];
781 			if (reg_init) {
782 				ret = palmas_ldo_init(palmas, id, reg_init);
783 				if (ret) {
784 					regulator_unregister(pmic->rdev[id]);
785 					goto err_unregister_regulator;
786 				}
787 			}
788 		}
789 	}
790 
791 	return 0;
792 
793 err_unregister_regulator:
794 	while (--id >= 0)
795 		regulator_unregister(pmic->rdev[id]);
796 	return ret;
797 }
798 
799 static int palmas_remove(struct platform_device *pdev)
800 {
801 	struct palmas_pmic *pmic = platform_get_drvdata(pdev);
802 	int id;
803 
804 	for (id = 0; id < PALMAS_NUM_REGS; id++)
805 		regulator_unregister(pmic->rdev[id]);
806 	return 0;
807 }
808 
809 static struct of_device_id of_palmas_match_tbl[] = {
810 	{ .compatible = "ti,palmas-pmic", },
811 	{ /* end */ }
812 };
813 
814 static struct platform_driver palmas_driver = {
815 	.driver = {
816 		.name = "palmas-pmic",
817 		.of_match_table = of_palmas_match_tbl,
818 		.owner = THIS_MODULE,
819 	},
820 	.probe = palmas_probe,
821 	.remove = palmas_remove,
822 };
823 
824 static int __init palmas_init(void)
825 {
826 	return platform_driver_register(&palmas_driver);
827 }
828 subsys_initcall(palmas_init);
829 
830 static void __exit palmas_exit(void)
831 {
832 	platform_driver_unregister(&palmas_driver);
833 }
834 module_exit(palmas_exit);
835 
836 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
837 MODULE_DESCRIPTION("Palmas voltage regulator driver");
838 MODULE_LICENSE("GPL");
839 MODULE_ALIAS("platform:palmas-pmic");
840 MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);
841