blob: a8294e5bcf362d585f0739f655fb36e1c0a89642 [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>
Graeme Gregorya361cd92012-08-28 13:47:40 +020025#include <linux/of.h>
26#include <linux/of_platform.h>
27#include <linux/regulator/of_regulator.h>
Graeme Gregorye5ce4202012-05-18 16:53:57 +010028
29struct regs_info {
30 char *name;
31 u8 vsel_addr;
32 u8 ctrl_addr;
33 u8 tstep_addr;
34};
35
36static 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
170static 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
180static 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
190static 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
200static 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
210static 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
224static 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
240static 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
256static 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);
Axel Lin999f0c72012-06-07 17:08:21 +0800263 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100264
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
283static 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
305static 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 if (!selector)
313 return 0;
314
315 /* Read the multiplier set in VSEL register to return
316 * the correct voltage.
317 */
318 if (pmic->range[id])
319 mult = 2;
320
321 /* Voltage is (0.49V + (selector * 0.01V)) * RANGE
322 * as defined in data sheet. RANGE is either x1 or x2
323 */
324 return (490000 + (selector * 10000)) * mult;
325}
326
327static int palmas_get_voltage_smps_sel(struct regulator_dev *dev)
328{
329 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
330 int id = rdev_get_id(dev);
331 int selector;
332 unsigned int reg;
333 unsigned int addr;
334
335 addr = palmas_regs_info[id].vsel_addr;
336
337 palmas_smps_read(pmic->palmas, addr, &reg);
338
339 selector = reg & PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
340
341 /* Adjust selector to match list_voltage ranges */
342 if ((selector > 0) && (selector < 6))
343 selector = 6;
344 if (!selector)
345 selector = 5;
346 if (selector > 121)
347 selector = 121;
348 selector -= 5;
349
350 return selector;
351}
352
353static int palmas_set_voltage_smps_sel(struct regulator_dev *dev,
354 unsigned selector)
355{
356 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
357 int id = rdev_get_id(dev);
358 unsigned int reg = 0;
359 unsigned int addr;
360
361 addr = palmas_regs_info[id].vsel_addr;
362
363 /* Make sure we don't change the value of RANGE */
364 if (pmic->range[id])
365 reg |= PALMAS_SMPS12_VOLTAGE_RANGE;
366
367 /* Adjust the linux selector into range used in VSEL register */
368 if (selector)
369 reg |= selector + 5;
370
371 palmas_smps_write(pmic->palmas, addr, reg);
372
373 return 0;
374}
375
376static int palmas_map_voltage_smps(struct regulator_dev *rdev,
377 int min_uV, int max_uV)
378{
Axel Lin8a165df2012-07-17 11:29:03 +0800379 struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
380 int id = rdev_get_id(rdev);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100381 int ret, voltage;
382
Axel Lin8a165df2012-07-17 11:29:03 +0800383 if (min_uV == 0)
384 return 0;
385
386 if (pmic->range[id]) { /* RANGE is x2 */
387 if (min_uV < 1000000)
388 min_uV = 1000000;
389 ret = DIV_ROUND_UP(min_uV - 1000000, 20000) + 1;
390 } else { /* RANGE is x1 */
391 if (min_uV < 500000)
392 min_uV = 500000;
393 ret = DIV_ROUND_UP(min_uV - 500000, 10000) + 1;
394 }
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100395
396 /* Map back into a voltage to verify we're still in bounds */
397 voltage = palmas_list_voltage_smps(rdev, ret);
398 if (voltage < min_uV || voltage > max_uV)
399 return -EINVAL;
400
401 return ret;
402}
403
404static struct regulator_ops palmas_ops_smps = {
405 .is_enabled = palmas_is_enabled_smps,
406 .enable = palmas_enable_smps,
407 .disable = palmas_disable_smps,
408 .set_mode = palmas_set_mode_smps,
409 .get_mode = palmas_get_mode_smps,
410 .get_voltage_sel = palmas_get_voltage_smps_sel,
411 .set_voltage_sel = palmas_set_voltage_smps_sel,
412 .list_voltage = palmas_list_voltage_smps,
413 .map_voltage = palmas_map_voltage_smps,
414};
415
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100416static struct regulator_ops palmas_ops_smps10 = {
417 .is_enabled = regulator_is_enabled_regmap,
418 .enable = regulator_enable_regmap,
419 .disable = regulator_disable_regmap,
420 .get_voltage_sel = regulator_get_voltage_sel_regmap,
421 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Axel Lin8029a002012-05-22 12:26:42 +0800422 .list_voltage = regulator_list_voltage_linear,
423 .map_voltage = regulator_map_voltage_linear,
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100424};
425
426static int palmas_is_enabled_ldo(struct regulator_dev *dev)
427{
428 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
429 int id = rdev_get_id(dev);
430 unsigned int reg;
431
432 palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
433
434 reg &= PALMAS_LDO1_CTRL_STATUS;
435
436 return !!(reg);
437}
438
439static int palmas_list_voltage_ldo(struct regulator_dev *dev,
440 unsigned selector)
441{
442 if (!selector)
443 return 0;
444
445 /* voltage is 0.85V + (selector * 0.05v) */
446 return 850000 + (selector * 50000);
447}
448
449static int palmas_get_voltage_ldo_sel(struct regulator_dev *dev)
450{
451 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
452 int id = rdev_get_id(dev);
453 int selector;
454 unsigned int reg;
455 unsigned int addr;
456
457 addr = palmas_regs_info[id].vsel_addr;
458
459 palmas_ldo_read(pmic->palmas, addr, &reg);
460
461 selector = reg & PALMAS_LDO1_VOLTAGE_VSEL_MASK;
462
463 /* Adjust selector to match list_voltage ranges */
464 if (selector > 49)
465 selector = 49;
466
467 return selector;
468}
469
470static int palmas_set_voltage_ldo_sel(struct regulator_dev *dev,
471 unsigned selector)
472{
473 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
474 int id = rdev_get_id(dev);
475 unsigned int reg = 0;
476 unsigned int addr;
477
478 addr = palmas_regs_info[id].vsel_addr;
479
480 reg = selector;
481
482 palmas_ldo_write(pmic->palmas, addr, reg);
483
484 return 0;
485}
486
487static int palmas_map_voltage_ldo(struct regulator_dev *rdev,
488 int min_uV, int max_uV)
489{
490 int ret, voltage;
491
492 ret = ((min_uV - 900000) / 50000) + 1;
493 if (ret < 0)
494 return ret;
495
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
592 ret = palmas_smps_read(palmas, addr, &reg);
593 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
602 ret = palmas_smps_write(palmas, addr, reg);
603 if (ret)
604 return ret;
605
606 return 0;
607}
608
Graeme Gregorya361cd92012-08-28 13:47:40 +0200609static struct of_regulator_match palmas_matches[] = {
610 { .name = "smps12", },
611 { .name = "smps123", },
612 { .name = "smps3", },
613 { .name = "smps45", },
614 { .name = "smps457", },
615 { .name = "smps6", },
616 { .name = "smps7", },
617 { .name = "smps8", },
618 { .name = "smps9", },
619 { .name = "smps10", },
620 { .name = "ldo1", },
621 { .name = "ldo2", },
622 { .name = "ldo3", },
623 { .name = "ldo4", },
624 { .name = "ldo5", },
625 { .name = "ldo6", },
626 { .name = "ldo7", },
627 { .name = "ldo8", },
628 { .name = "ldo9", },
629 { .name = "ldoln", },
630 { .name = "ldousb", },
631};
632
633static void __devinit palmas_dt_to_pdata(struct device *dev,
634 struct device_node *node,
635 struct palmas_pmic_platform_data *pdata)
636{
637 struct device_node *regulators;
638 u32 prop;
639 int idx, ret;
640
641 regulators = of_find_node_by_name(node, "regulators");
642 if (!regulators) {
643 dev_info(dev, "regulator node not found\n");
644 return;
645 }
646
647 ret = of_regulator_match(dev, regulators, palmas_matches,
648 PALMAS_NUM_REGS);
649 if (ret < 0) {
650 dev_err(dev, "Error parsing regulator init data: %d\n", ret);
651 return;
652 }
653
654 for (idx = 0; idx < PALMAS_NUM_REGS; idx++) {
655 if (!palmas_matches[idx].init_data ||
656 !palmas_matches[idx].of_node)
657 continue;
658
659 pdata->reg_data[idx] = palmas_matches[idx].init_data;
660
661 pdata->reg_init[idx] = devm_kzalloc(dev,
662 sizeof(struct palmas_reg_init), GFP_KERNEL);
663
664 ret = of_property_read_u32(palmas_matches[idx].of_node,
665 "ti,warm_reset", &prop);
666 if (!ret)
667 pdata->reg_init[idx]->warm_reset = prop;
668
669 ret = of_property_read_u32(palmas_matches[idx].of_node,
670 "ti,roof_floor", &prop);
671 if (!ret)
672 pdata->reg_init[idx]->roof_floor = prop;
673
674 ret = of_property_read_u32(palmas_matches[idx].of_node,
675 "ti,mode_sleep", &prop);
676 if (!ret)
677 pdata->reg_init[idx]->mode_sleep = prop;
678
679 ret = of_property_read_u32(palmas_matches[idx].of_node,
680 "ti,warm_reset", &prop);
681 if (!ret)
682 pdata->reg_init[idx]->warm_reset = prop;
683
684 ret = of_property_read_u32(palmas_matches[idx].of_node,
685 "ti,tstep", &prop);
686 if (!ret)
687 pdata->reg_init[idx]->tstep = prop;
688
689 ret = of_property_read_u32(palmas_matches[idx].of_node,
690 "ti,vsel", &prop);
691 if (!ret)
692 pdata->reg_init[idx]->vsel = prop;
693 }
694
695 ret = of_property_read_u32(node, "ti,ldo6_vibrator", &prop);
696 if (!ret)
697 pdata->ldo6_vibrator = prop;
698}
699
700
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100701static __devinit int palmas_probe(struct platform_device *pdev)
702{
703 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
704 struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
Graeme Gregorya361cd92012-08-28 13:47:40 +0200705 struct device_node *node = pdev->dev.of_node;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100706 struct regulator_dev *rdev;
707 struct regulator_config config = { };
708 struct palmas_pmic *pmic;
709 struct palmas_reg_init *reg_init;
710 int id = 0, ret;
711 unsigned int addr, reg;
712
Graeme Gregorya361cd92012-08-28 13:47:40 +0200713 if (node && !pdata) {
714 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
715
716 if (!pdata)
717 return -ENOMEM;
718
719 palmas_dt_to_pdata(&pdev->dev, node, pdata);
720 }
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100721
722 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
723 if (!pmic)
724 return -ENOMEM;
725
726 pmic->dev = &pdev->dev;
727 pmic->palmas = palmas;
728 palmas->pmic = pmic;
729 platform_set_drvdata(pdev, pmic);
730
731 ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
732 if (ret)
733 goto err_unregister_regulator;
734
735 if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
736 pmic->smps123 = 1;
737
738 if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
739 pmic->smps457 = 1;
740
741 config.regmap = palmas->regmap[REGULATOR_SLAVE];
742 config.dev = &pdev->dev;
743 config.driver_data = pmic;
744
745 for (id = 0; id < PALMAS_REG_LDO1; id++) {
746
747 /*
748 * Miss out regulators which are not available due
749 * to slaving configurations.
750 */
751 switch (id) {
752 case PALMAS_REG_SMPS12:
753 case PALMAS_REG_SMPS3:
754 if (pmic->smps123)
755 continue;
756 break;
757 case PALMAS_REG_SMPS123:
758 if (!pmic->smps123)
759 continue;
760 break;
761 case PALMAS_REG_SMPS45:
762 case PALMAS_REG_SMPS7:
763 if (pmic->smps457)
764 continue;
765 break;
766 case PALMAS_REG_SMPS457:
767 if (!pmic->smps457)
768 continue;
769 }
770
771 /* Register the regulators */
772 pmic->desc[id].name = palmas_regs_info[id].name;
773 pmic->desc[id].id = id;
774
Axel Linfedd89b2012-06-06 20:01:38 +0800775 switch (id) {
776 case PALMAS_REG_SMPS10:
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100777 pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
778 pmic->desc[id].ops = &palmas_ops_smps10;
779 pmic->desc[id].vsel_reg = PALMAS_SMPS10_CTRL;
780 pmic->desc[id].vsel_mask = SMPS10_VSEL;
Graeme Gregorya68de072012-06-22 13:36:20 +0100781 pmic->desc[id].enable_reg =
782 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
783 PALMAS_SMPS10_STATUS);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100784 pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
Axel Lin8029a002012-05-22 12:26:42 +0800785 pmic->desc[id].min_uV = 3750000;
786 pmic->desc[id].uV_step = 1250000;
Axel Linfedd89b2012-06-06 20:01:38 +0800787 break;
788 default:
789 pmic->desc[id].ops = &palmas_ops_smps;
790 pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100791 }
792
793 pmic->desc[id].type = REGULATOR_VOLTAGE;
794 pmic->desc[id].owner = THIS_MODULE;
795
796 /* Initialise sleep/init values from platform data */
Graeme Gregorya361cd92012-08-28 13:47:40 +0200797 if (pdata) {
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100798 reg_init = pdata->reg_init[id];
799 if (reg_init) {
800 ret = palmas_smps_init(palmas, id, reg_init);
801 if (ret)
802 goto err_unregister_regulator;
803 }
804 }
805
806 /*
807 * read and store the RANGE bit for later use
808 * This must be done before regulator is probed otherwise
809 * we error in probe with unsuportable ranges.
810 */
811 if (id != PALMAS_REG_SMPS10) {
812 addr = palmas_regs_info[id].vsel_addr;
813
814 ret = palmas_smps_read(pmic->palmas, addr, &reg);
815 if (ret)
816 goto err_unregister_regulator;
817 if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
818 pmic->range[id] = 1;
819 }
820
Graeme Gregorya361cd92012-08-28 13:47:40 +0200821 if (pdata)
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100822 config.init_data = pdata->reg_data[id];
823 else
824 config.init_data = NULL;
825
Graeme Gregorya361cd92012-08-28 13:47:40 +0200826 config.of_node = palmas_matches[id].of_node;
827
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100828 rdev = regulator_register(&pmic->desc[id], &config);
829 if (IS_ERR(rdev)) {
830 dev_err(&pdev->dev,
831 "failed to register %s regulator\n",
832 pdev->name);
833 ret = PTR_ERR(rdev);
834 goto err_unregister_regulator;
835 }
836
837 /* Save regulator for cleanup */
838 pmic->rdev[id] = rdev;
839 }
840
841 /* Start this loop from the id left from previous loop */
842 for (; id < PALMAS_NUM_REGS; id++) {
843
844 /* Miss out regulators which are not available due
845 * to alternate functions.
846 */
847
848 /* Register the regulators */
849 pmic->desc[id].name = palmas_regs_info[id].name;
850 pmic->desc[id].id = id;
851 pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
852
853 pmic->desc[id].ops = &palmas_ops_ldo;
854
855 pmic->desc[id].type = REGULATOR_VOLTAGE;
856 pmic->desc[id].owner = THIS_MODULE;
Graeme Gregorya68de072012-06-22 13:36:20 +0100857 pmic->desc[id].enable_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
858 palmas_regs_info[id].ctrl_addr);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100859 pmic->desc[id].enable_mask = PALMAS_LDO1_CTRL_MODE_ACTIVE;
860
Graeme Gregorya361cd92012-08-28 13:47:40 +0200861 if (pdata)
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100862 config.init_data = pdata->reg_data[id];
863 else
864 config.init_data = NULL;
865
Graeme Gregorya361cd92012-08-28 13:47:40 +0200866 config.of_node = palmas_matches[id].of_node;
867
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100868 rdev = regulator_register(&pmic->desc[id], &config);
869 if (IS_ERR(rdev)) {
870 dev_err(&pdev->dev,
871 "failed to register %s regulator\n",
872 pdev->name);
873 ret = PTR_ERR(rdev);
874 goto err_unregister_regulator;
875 }
876
877 /* Save regulator for cleanup */
878 pmic->rdev[id] = rdev;
879
880 /* Initialise sleep/init values from platform data */
Graeme Gregorya361cd92012-08-28 13:47:40 +0200881 if (pdata) {
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100882 reg_init = pdata->reg_init[id];
883 if (reg_init) {
884 ret = palmas_ldo_init(palmas, id, reg_init);
885 if (ret)
886 goto err_unregister_regulator;
887 }
888 }
889 }
890
891 return 0;
892
893err_unregister_regulator:
894 while (--id >= 0)
895 regulator_unregister(pmic->rdev[id]);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100896 return ret;
897}
898
899static int __devexit palmas_remove(struct platform_device *pdev)
900{
901 struct palmas_pmic *pmic = platform_get_drvdata(pdev);
902 int id;
903
904 for (id = 0; id < PALMAS_NUM_REGS; id++)
905 regulator_unregister(pmic->rdev[id]);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100906 return 0;
907}
908
Graeme Gregorya361cd92012-08-28 13:47:40 +0200909static struct of_device_id __devinitdata of_palmas_match_tbl[] = {
910 { .compatible = "ti,palmas-pmic", },
911 { /* end */ }
912};
913
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100914static struct platform_driver palmas_driver = {
915 .driver = {
916 .name = "palmas-pmic",
Graeme Gregorya361cd92012-08-28 13:47:40 +0200917 .of_match_table = of_palmas_match_tbl,
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100918 .owner = THIS_MODULE,
919 },
920 .probe = palmas_probe,
921 .remove = __devexit_p(palmas_remove),
922};
923
924static int __init palmas_init(void)
925{
926 return platform_driver_register(&palmas_driver);
927}
928subsys_initcall(palmas_init);
929
930static void __exit palmas_exit(void)
931{
932 platform_driver_unregister(&palmas_driver);
933}
934module_exit(palmas_exit);
935
936MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
937MODULE_DESCRIPTION("Palmas voltage regulator driver");
938MODULE_LICENSE("GPL");
939MODULE_ALIAS("platform:palmas-pmic");
Graeme Gregorya361cd92012-08-28 13:47:40 +0200940MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);