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