blob: c61c0fa83e22f57e3466e839452218cf52f9ed9f [file] [log] [blame]
Graeme Gregorye5ce4202012-05-18 16:53:57 +01001/*
2 * Driver for Regulator part of Palmas PMIC Chips
3 *
Graeme Gregory7be859f2013-03-07 13:17:48 +00004 * Copyright 2011-2013 Texas Instruments Inc.
Graeme Gregorye5ce4202012-05-18 16:53:57 +01005 *
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;
Laxman Dewangan504382c2013-03-20 19:26:37 +053031 char *sname;
Graeme Gregorye5ce4202012-05-18 16:53:57 +010032 u8 vsel_addr;
33 u8 ctrl_addr;
34 u8 tstep_addr;
35};
36
37static const struct regs_info palmas_regs_info[] = {
38 {
39 .name = "SMPS12",
Laxman Dewangan504382c2013-03-20 19:26:37 +053040 .sname = "smps1-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +010041 .vsel_addr = PALMAS_SMPS12_VOLTAGE,
42 .ctrl_addr = PALMAS_SMPS12_CTRL,
43 .tstep_addr = PALMAS_SMPS12_TSTEP,
44 },
45 {
46 .name = "SMPS123",
Laxman Dewangan504382c2013-03-20 19:26:37 +053047 .sname = "smps1-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +010048 .vsel_addr = PALMAS_SMPS12_VOLTAGE,
49 .ctrl_addr = PALMAS_SMPS12_CTRL,
50 .tstep_addr = PALMAS_SMPS12_TSTEP,
51 },
52 {
53 .name = "SMPS3",
Laxman Dewangan504382c2013-03-20 19:26:37 +053054 .sname = "smps3-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +010055 .vsel_addr = PALMAS_SMPS3_VOLTAGE,
56 .ctrl_addr = PALMAS_SMPS3_CTRL,
57 },
58 {
59 .name = "SMPS45",
Laxman Dewangan504382c2013-03-20 19:26:37 +053060 .sname = "smps4-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +010061 .vsel_addr = PALMAS_SMPS45_VOLTAGE,
62 .ctrl_addr = PALMAS_SMPS45_CTRL,
63 .tstep_addr = PALMAS_SMPS45_TSTEP,
64 },
65 {
66 .name = "SMPS457",
Laxman Dewangan504382c2013-03-20 19:26:37 +053067 .sname = "smps4-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +010068 .vsel_addr = PALMAS_SMPS45_VOLTAGE,
69 .ctrl_addr = PALMAS_SMPS45_CTRL,
70 .tstep_addr = PALMAS_SMPS45_TSTEP,
71 },
72 {
73 .name = "SMPS6",
Laxman Dewangan504382c2013-03-20 19:26:37 +053074 .sname = "smps6-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +010075 .vsel_addr = PALMAS_SMPS6_VOLTAGE,
76 .ctrl_addr = PALMAS_SMPS6_CTRL,
77 .tstep_addr = PALMAS_SMPS6_TSTEP,
78 },
79 {
80 .name = "SMPS7",
Laxman Dewangan504382c2013-03-20 19:26:37 +053081 .sname = "smps7-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +010082 .vsel_addr = PALMAS_SMPS7_VOLTAGE,
83 .ctrl_addr = PALMAS_SMPS7_CTRL,
84 },
85 {
86 .name = "SMPS8",
Laxman Dewangan504382c2013-03-20 19:26:37 +053087 .sname = "smps8-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +010088 .vsel_addr = PALMAS_SMPS8_VOLTAGE,
89 .ctrl_addr = PALMAS_SMPS8_CTRL,
90 .tstep_addr = PALMAS_SMPS8_TSTEP,
91 },
92 {
93 .name = "SMPS9",
Laxman Dewangan504382c2013-03-20 19:26:37 +053094 .sname = "smps9-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +010095 .vsel_addr = PALMAS_SMPS9_VOLTAGE,
96 .ctrl_addr = PALMAS_SMPS9_CTRL,
97 },
98 {
99 .name = "SMPS10",
Laxman Dewangan504382c2013-03-20 19:26:37 +0530100 .sname = "smps10-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100101 },
102 {
103 .name = "LDO1",
Laxman Dewangan504382c2013-03-20 19:26:37 +0530104 .sname = "ldo1-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100105 .vsel_addr = PALMAS_LDO1_VOLTAGE,
106 .ctrl_addr = PALMAS_LDO1_CTRL,
107 },
108 {
109 .name = "LDO2",
Laxman Dewangan504382c2013-03-20 19:26:37 +0530110 .sname = "ldo2-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100111 .vsel_addr = PALMAS_LDO2_VOLTAGE,
112 .ctrl_addr = PALMAS_LDO2_CTRL,
113 },
114 {
115 .name = "LDO3",
Laxman Dewangan504382c2013-03-20 19:26:37 +0530116 .sname = "ldo3-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100117 .vsel_addr = PALMAS_LDO3_VOLTAGE,
118 .ctrl_addr = PALMAS_LDO3_CTRL,
119 },
120 {
121 .name = "LDO4",
Laxman Dewangan504382c2013-03-20 19:26:37 +0530122 .sname = "ldo4-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100123 .vsel_addr = PALMAS_LDO4_VOLTAGE,
124 .ctrl_addr = PALMAS_LDO4_CTRL,
125 },
126 {
127 .name = "LDO5",
Laxman Dewangan504382c2013-03-20 19:26:37 +0530128 .sname = "ldo5-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100129 .vsel_addr = PALMAS_LDO5_VOLTAGE,
130 .ctrl_addr = PALMAS_LDO5_CTRL,
131 },
132 {
133 .name = "LDO6",
Laxman Dewangan504382c2013-03-20 19:26:37 +0530134 .sname = "ldo6-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100135 .vsel_addr = PALMAS_LDO6_VOLTAGE,
136 .ctrl_addr = PALMAS_LDO6_CTRL,
137 },
138 {
139 .name = "LDO7",
Laxman Dewangan504382c2013-03-20 19:26:37 +0530140 .sname = "ldo7-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100141 .vsel_addr = PALMAS_LDO7_VOLTAGE,
142 .ctrl_addr = PALMAS_LDO7_CTRL,
143 },
144 {
145 .name = "LDO8",
Laxman Dewangan504382c2013-03-20 19:26:37 +0530146 .sname = "ldo8-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100147 .vsel_addr = PALMAS_LDO8_VOLTAGE,
148 .ctrl_addr = PALMAS_LDO8_CTRL,
149 },
150 {
151 .name = "LDO9",
Laxman Dewangan504382c2013-03-20 19:26:37 +0530152 .sname = "ldo9-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100153 .vsel_addr = PALMAS_LDO9_VOLTAGE,
154 .ctrl_addr = PALMAS_LDO9_CTRL,
155 },
156 {
157 .name = "LDOLN",
Laxman Dewangan504382c2013-03-20 19:26:37 +0530158 .sname = "ldoln-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100159 .vsel_addr = PALMAS_LDOLN_VOLTAGE,
160 .ctrl_addr = PALMAS_LDOLN_CTRL,
161 },
162 {
163 .name = "LDOUSB",
Laxman Dewangan504382c2013-03-20 19:26:37 +0530164 .sname = "ldousb-in",
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100165 .vsel_addr = PALMAS_LDOUSB_VOLTAGE,
166 .ctrl_addr = PALMAS_LDOUSB_CTRL,
167 },
Laxman Dewanganaa07f022013-04-17 15:13:12 +0530168 {
169 .name = "REGEN1",
170 .ctrl_addr = PALMAS_REGEN1_CTRL,
171 },
172 {
173 .name = "REGEN2",
174 .ctrl_addr = PALMAS_REGEN2_CTRL,
175 },
176 {
177 .name = "REGEN3",
178 .ctrl_addr = PALMAS_REGEN3_CTRL,
179 },
180 {
181 .name = "SYSEN1",
182 .ctrl_addr = PALMAS_SYSEN1_CTRL,
183 },
184 {
185 .name = "SYSEN2",
186 .ctrl_addr = PALMAS_SYSEN2_CTRL,
187 },
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100188};
189
190#define SMPS_CTRL_MODE_OFF 0x00
191#define SMPS_CTRL_MODE_ON 0x01
192#define SMPS_CTRL_MODE_ECO 0x02
193#define SMPS_CTRL_MODE_PWM 0x03
194
195/* These values are derived from the data sheet. And are the number of steps
196 * where there is a voltage change, the ranges at beginning and end of register
197 * max/min values where there are no change are ommitted.
198 *
199 * So they are basically (maxV-minV)/stepV
200 */
201#define PALMAS_SMPS_NUM_VOLTAGES 116
202#define PALMAS_SMPS10_NUM_VOLTAGES 2
203#define PALMAS_LDO_NUM_VOLTAGES 50
204
205#define SMPS10_VSEL (1<<3)
206#define SMPS10_BOOST_EN (1<<2)
207#define SMPS10_BYPASS_EN (1<<1)
208#define SMPS10_SWITCH_EN (1<<0)
209
210#define REGULATOR_SLAVE 0
211
212static int palmas_smps_read(struct palmas *palmas, unsigned int reg,
213 unsigned int *dest)
214{
215 unsigned int addr;
216
217 addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
218
219 return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
220}
221
222static int palmas_smps_write(struct palmas *palmas, unsigned int reg,
223 unsigned int value)
224{
225 unsigned int addr;
226
227 addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
228
229 return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
230}
231
232static int palmas_ldo_read(struct palmas *palmas, unsigned int reg,
233 unsigned int *dest)
234{
235 unsigned int addr;
236
237 addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
238
239 return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
240}
241
242static int palmas_ldo_write(struct palmas *palmas, unsigned int reg,
243 unsigned int value)
244{
245 unsigned int addr;
246
247 addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
248
249 return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
250}
251
252static int palmas_is_enabled_smps(struct regulator_dev *dev)
253{
254 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
255 int id = rdev_get_id(dev);
256 unsigned int reg;
257
258 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
259
260 reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
261 reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
262
263 return !!(reg);
264}
265
266static int palmas_enable_smps(struct regulator_dev *dev)
267{
268 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
269 int id = rdev_get_id(dev);
270 unsigned int reg;
271
272 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
273
274 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
275 reg |= SMPS_CTRL_MODE_ON;
276
277 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
278
279 return 0;
280}
281
282static int palmas_disable_smps(struct regulator_dev *dev)
283{
284 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
285 int id = rdev_get_id(dev);
286 unsigned int reg;
287
288 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
289
290 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
291
292 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
293
294 return 0;
295}
296
297
298static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
299{
300 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
301 int id = rdev_get_id(dev);
302 unsigned int reg;
303
304 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
Axel Lin999f0c72012-06-07 17:08:21 +0800305 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100306
307 switch (mode) {
308 case REGULATOR_MODE_NORMAL:
309 reg |= SMPS_CTRL_MODE_ON;
310 break;
311 case REGULATOR_MODE_IDLE:
312 reg |= SMPS_CTRL_MODE_ECO;
313 break;
314 case REGULATOR_MODE_FAST:
315 reg |= SMPS_CTRL_MODE_PWM;
316 break;
317 default:
318 return -EINVAL;
319 }
320 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
321
322 return 0;
323}
324
325static unsigned int palmas_get_mode_smps(struct regulator_dev *dev)
326{
327 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
328 int id = rdev_get_id(dev);
329 unsigned int reg;
330
331 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
332 reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
333 reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
334
335 switch (reg) {
336 case SMPS_CTRL_MODE_ON:
337 return REGULATOR_MODE_NORMAL;
338 case SMPS_CTRL_MODE_ECO:
339 return REGULATOR_MODE_IDLE;
340 case SMPS_CTRL_MODE_PWM:
341 return REGULATOR_MODE_FAST;
342 }
343
344 return 0;
345}
346
347static int palmas_list_voltage_smps(struct regulator_dev *dev,
348 unsigned selector)
349{
350 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
351 int id = rdev_get_id(dev);
352 int mult = 1;
353
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100354 /* Read the multiplier set in VSEL register to return
355 * the correct voltage.
356 */
357 if (pmic->range[id])
358 mult = 2;
359
Axel Linad02e842012-11-29 09:59:16 +0800360 if (selector == 0)
361 return 0;
362 else if (selector < 6)
363 return 500000 * mult;
364 else
365 /* Voltage is linear mapping starting from selector 6,
366 * volt = (0.49V + ((selector - 5) * 0.01V)) * RANGE
367 * RANGE is either x1 or x2
368 */
369 return (490000 + ((selector - 5) * 10000)) * mult;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100370}
371
372static int palmas_map_voltage_smps(struct regulator_dev *rdev,
373 int min_uV, int max_uV)
374{
Axel Lin8a165df2012-07-17 11:29:03 +0800375 struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
376 int id = rdev_get_id(rdev);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100377 int ret, voltage;
378
Axel Lin8a165df2012-07-17 11:29:03 +0800379 if (min_uV == 0)
380 return 0;
381
382 if (pmic->range[id]) { /* RANGE is x2 */
383 if (min_uV < 1000000)
384 min_uV = 1000000;
Axel Linad02e842012-11-29 09:59:16 +0800385 ret = DIV_ROUND_UP(min_uV - 1000000, 20000) + 6;
Axel Lin8a165df2012-07-17 11:29:03 +0800386 } else { /* RANGE is x1 */
387 if (min_uV < 500000)
388 min_uV = 500000;
Axel Linad02e842012-11-29 09:59:16 +0800389 ret = DIV_ROUND_UP(min_uV - 500000, 10000) + 6;
Axel Lin8a165df2012-07-17 11:29:03 +0800390 }
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100391
392 /* Map back into a voltage to verify we're still in bounds */
393 voltage = palmas_list_voltage_smps(rdev, ret);
394 if (voltage < min_uV || voltage > max_uV)
395 return -EINVAL;
396
397 return ret;
398}
399
400static struct regulator_ops palmas_ops_smps = {
401 .is_enabled = palmas_is_enabled_smps,
402 .enable = palmas_enable_smps,
403 .disable = palmas_disable_smps,
404 .set_mode = palmas_set_mode_smps,
405 .get_mode = palmas_get_mode_smps,
Axel Linbdc4baa2012-11-29 10:01:44 +0800406 .get_voltage_sel = regulator_get_voltage_sel_regmap,
407 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100408 .list_voltage = palmas_list_voltage_smps,
409 .map_voltage = palmas_map_voltage_smps,
410};
411
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100412static struct regulator_ops palmas_ops_smps10 = {
413 .is_enabled = regulator_is_enabled_regmap,
414 .enable = regulator_enable_regmap,
415 .disable = regulator_disable_regmap,
416 .get_voltage_sel = regulator_get_voltage_sel_regmap,
417 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Axel Lin8029a002012-05-22 12:26:42 +0800418 .list_voltage = regulator_list_voltage_linear,
419 .map_voltage = regulator_map_voltage_linear,
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100420};
421
422static int palmas_is_enabled_ldo(struct regulator_dev *dev)
423{
424 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
425 int id = rdev_get_id(dev);
426 unsigned int reg;
427
428 palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
429
430 reg &= PALMAS_LDO1_CTRL_STATUS;
431
432 return !!(reg);
433}
434
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100435static struct regulator_ops palmas_ops_ldo = {
436 .is_enabled = palmas_is_enabled_ldo,
437 .enable = regulator_enable_regmap,
438 .disable = regulator_disable_regmap,
Axel Lin4a247a92012-07-18 12:34:08 +0800439 .get_voltage_sel = regulator_get_voltage_sel_regmap,
440 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Axel Lin9119ff62012-11-27 10:27:34 +0800441 .list_voltage = regulator_list_voltage_linear,
442 .map_voltage = regulator_map_voltage_linear,
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100443};
444
Laxman Dewanganaa07f022013-04-17 15:13:12 +0530445static struct regulator_ops palmas_ops_extreg = {
446 .is_enabled = regulator_is_enabled_regmap,
447 .enable = regulator_enable_regmap,
448 .disable = regulator_disable_regmap,
449};
450
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100451/*
452 * setup the hardware based sleep configuration of the SMPS/LDO regulators
453 * from the platform data. This is different to the software based control
454 * supported by the regulator framework as it is controlled by toggling
455 * pins on the PMIC such as PREQ, SYSEN, ...
456 */
457static int palmas_smps_init(struct palmas *palmas, int id,
458 struct palmas_reg_init *reg_init)
459{
460 unsigned int reg;
461 unsigned int addr;
462 int ret;
463
464 addr = palmas_regs_info[id].ctrl_addr;
465
466 ret = palmas_smps_read(palmas, addr, &reg);
467 if (ret)
468 return ret;
469
Axel Linfedd89b2012-06-06 20:01:38 +0800470 switch (id) {
471 case PALMAS_REG_SMPS10:
Laxman Dewangan30590d02013-04-17 15:13:11 +0530472 reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
473 if (reg_init->mode_sleep)
Axel Linfedd89b2012-06-06 20:01:38 +0800474 reg |= reg_init->mode_sleep <<
475 PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
Axel Linfedd89b2012-06-06 20:01:38 +0800476 break;
477 default:
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100478 if (reg_init->warm_reset)
479 reg |= PALMAS_SMPS12_CTRL_WR_S;
Laxman Dewangan30590d02013-04-17 15:13:11 +0530480 else
481 reg &= ~PALMAS_SMPS12_CTRL_WR_S;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100482
483 if (reg_init->roof_floor)
484 reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
Laxman Dewangan30590d02013-04-17 15:13:11 +0530485 else
486 reg &= ~PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100487
Laxman Dewangan30590d02013-04-17 15:13:11 +0530488 reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
489 if (reg_init->mode_sleep)
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100490 reg |= reg_init->mode_sleep <<
491 PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100492 }
Axel Linfedd89b2012-06-06 20:01:38 +0800493
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100494 ret = palmas_smps_write(palmas, addr, reg);
495 if (ret)
496 return ret;
497
498 if (palmas_regs_info[id].tstep_addr && reg_init->tstep) {
499 addr = palmas_regs_info[id].tstep_addr;
500
501 reg = reg_init->tstep & PALMAS_SMPS12_TSTEP_TSTEP_MASK;
502
503 ret = palmas_smps_write(palmas, addr, reg);
504 if (ret)
505 return ret;
506 }
507
508 if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
509 addr = palmas_regs_info[id].vsel_addr;
510
511 reg = reg_init->vsel;
512
513 ret = palmas_smps_write(palmas, addr, reg);
514 if (ret)
515 return ret;
516 }
517
518
519 return 0;
520}
521
522static int palmas_ldo_init(struct palmas *palmas, int id,
523 struct palmas_reg_init *reg_init)
524{
525 unsigned int reg;
526 unsigned int addr;
527 int ret;
528
529 addr = palmas_regs_info[id].ctrl_addr;
530
Axel Lin2735dae2012-07-18 12:31:59 +0800531 ret = palmas_ldo_read(palmas, addr, &reg);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100532 if (ret)
533 return ret;
534
535 if (reg_init->warm_reset)
536 reg |= PALMAS_LDO1_CTRL_WR_S;
Laxman Dewangan30590d02013-04-17 15:13:11 +0530537 else
538 reg &= ~PALMAS_LDO1_CTRL_WR_S;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100539
540 if (reg_init->mode_sleep)
541 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
Laxman Dewangan30590d02013-04-17 15:13:11 +0530542 else
543 reg &= ~PALMAS_LDO1_CTRL_MODE_SLEEP;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100544
Axel Lin2735dae2012-07-18 12:31:59 +0800545 ret = palmas_ldo_write(palmas, addr, reg);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100546 if (ret)
547 return ret;
548
549 return 0;
550}
551
Laxman Dewanganaa07f022013-04-17 15:13:12 +0530552static int palmas_extreg_init(struct palmas *palmas, int id,
553 struct palmas_reg_init *reg_init)
554{
555 unsigned int addr;
556 int ret;
557 unsigned int val = 0;
558
559 addr = palmas_regs_info[id].ctrl_addr;
560
561 if (reg_init->mode_sleep)
562 val = PALMAS_REGEN1_CTRL_MODE_SLEEP;
563
564 ret = palmas_update_bits(palmas, PALMAS_RESOURCE_BASE,
565 addr, PALMAS_REGEN1_CTRL_MODE_SLEEP, val);
566 if (ret < 0) {
567 dev_err(palmas->dev, "Resource reg 0x%02x update failed %d\n",
568 addr, ret);
569 return ret;
570 }
571 return 0;
572}
573
Graeme Gregorya361cd92012-08-28 13:47:40 +0200574static struct of_regulator_match palmas_matches[] = {
575 { .name = "smps12", },
576 { .name = "smps123", },
577 { .name = "smps3", },
578 { .name = "smps45", },
579 { .name = "smps457", },
580 { .name = "smps6", },
581 { .name = "smps7", },
582 { .name = "smps8", },
583 { .name = "smps9", },
584 { .name = "smps10", },
585 { .name = "ldo1", },
586 { .name = "ldo2", },
587 { .name = "ldo3", },
588 { .name = "ldo4", },
589 { .name = "ldo5", },
590 { .name = "ldo6", },
591 { .name = "ldo7", },
592 { .name = "ldo8", },
593 { .name = "ldo9", },
594 { .name = "ldoln", },
595 { .name = "ldousb", },
Laxman Dewanganaa07f022013-04-17 15:13:12 +0530596 { .name = "regen1", },
597 { .name = "regen2", },
598 { .name = "regen3", },
599 { .name = "sysen1", },
600 { .name = "sysen2", },
Graeme Gregorya361cd92012-08-28 13:47:40 +0200601};
602
Bill Pembertona5023572012-11-19 13:22:22 -0500603static void palmas_dt_to_pdata(struct device *dev,
Graeme Gregorya361cd92012-08-28 13:47:40 +0200604 struct device_node *node,
605 struct palmas_pmic_platform_data *pdata)
606{
607 struct device_node *regulators;
608 u32 prop;
609 int idx, ret;
610
Axel Linc92f5dd2013-01-27 21:16:56 +0800611 node = of_node_get(node);
Graeme Gregorya361cd92012-08-28 13:47:40 +0200612 regulators = of_find_node_by_name(node, "regulators");
613 if (!regulators) {
614 dev_info(dev, "regulator node not found\n");
615 return;
616 }
617
618 ret = of_regulator_match(dev, regulators, palmas_matches,
619 PALMAS_NUM_REGS);
Axel Linc92f5dd2013-01-27 21:16:56 +0800620 of_node_put(regulators);
Graeme Gregorya361cd92012-08-28 13:47:40 +0200621 if (ret < 0) {
622 dev_err(dev, "Error parsing regulator init data: %d\n", ret);
623 return;
624 }
625
626 for (idx = 0; idx < PALMAS_NUM_REGS; idx++) {
627 if (!palmas_matches[idx].init_data ||
628 !palmas_matches[idx].of_node)
629 continue;
630
631 pdata->reg_data[idx] = palmas_matches[idx].init_data;
632
633 pdata->reg_init[idx] = devm_kzalloc(dev,
634 sizeof(struct palmas_reg_init), GFP_KERNEL);
635
Graeme Gregory7be859f2013-03-07 13:17:48 +0000636 pdata->reg_init[idx]->warm_reset =
Axel Lin71f21462013-03-12 23:40:19 +0800637 of_property_read_bool(palmas_matches[idx].of_node,
638 "ti,warm-reset");
Graeme Gregorya361cd92012-08-28 13:47:40 +0200639
Graeme Gregory7be859f2013-03-07 13:17:48 +0000640 pdata->reg_init[idx]->roof_floor =
641 of_property_read_bool(palmas_matches[idx].of_node,
642 "ti,roof-floor");
Graeme Gregorya361cd92012-08-28 13:47:40 +0200643
644 ret = of_property_read_u32(palmas_matches[idx].of_node,
J Keerthy3c870e32013-02-18 10:44:20 +0530645 "ti,mode-sleep", &prop);
Graeme Gregorya361cd92012-08-28 13:47:40 +0200646 if (!ret)
647 pdata->reg_init[idx]->mode_sleep = prop;
648
649 ret = of_property_read_u32(palmas_matches[idx].of_node,
Graeme Gregorya361cd92012-08-28 13:47:40 +0200650 "ti,tstep", &prop);
651 if (!ret)
652 pdata->reg_init[idx]->tstep = prop;
653
Graeme Gregory7be859f2013-03-07 13:17:48 +0000654 ret = of_property_read_bool(palmas_matches[idx].of_node,
655 "ti,smps-range");
656 if (ret)
657 pdata->reg_init[idx]->vsel =
658 PALMAS_SMPS12_VOLTAGE_RANGE;
Graeme Gregorya361cd92012-08-28 13:47:40 +0200659 }
660
Graeme Gregory7be859f2013-03-07 13:17:48 +0000661 pdata->ldo6_vibrator = of_property_read_bool(node, "ti,ldo6-vibrator");
Graeme Gregorya361cd92012-08-28 13:47:40 +0200662}
663
664
Laxman Dewanganbbcf50b2013-03-18 14:59:49 +0530665static int palmas_regulators_probe(struct platform_device *pdev)
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100666{
667 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
668 struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
Graeme Gregorya361cd92012-08-28 13:47:40 +0200669 struct device_node *node = pdev->dev.of_node;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100670 struct regulator_dev *rdev;
671 struct regulator_config config = { };
672 struct palmas_pmic *pmic;
673 struct palmas_reg_init *reg_init;
674 int id = 0, ret;
675 unsigned int addr, reg;
676
Graeme Gregorya361cd92012-08-28 13:47:40 +0200677 if (node && !pdata) {
678 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
679
680 if (!pdata)
681 return -ENOMEM;
682
683 palmas_dt_to_pdata(&pdev->dev, node, pdata);
684 }
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100685
686 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
687 if (!pmic)
688 return -ENOMEM;
689
690 pmic->dev = &pdev->dev;
691 pmic->palmas = palmas;
692 palmas->pmic = pmic;
693 platform_set_drvdata(pdev, pmic);
694
695 ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
696 if (ret)
Axel Lin1c9d2d72012-07-18 12:33:20 +0800697 return ret;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100698
699 if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
700 pmic->smps123 = 1;
701
702 if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
703 pmic->smps457 = 1;
704
705 config.regmap = palmas->regmap[REGULATOR_SLAVE];
706 config.dev = &pdev->dev;
707 config.driver_data = pmic;
708
709 for (id = 0; id < PALMAS_REG_LDO1; id++) {
710
711 /*
712 * Miss out regulators which are not available due
713 * to slaving configurations.
714 */
715 switch (id) {
716 case PALMAS_REG_SMPS12:
717 case PALMAS_REG_SMPS3:
718 if (pmic->smps123)
719 continue;
720 break;
721 case PALMAS_REG_SMPS123:
722 if (!pmic->smps123)
723 continue;
724 break;
725 case PALMAS_REG_SMPS45:
726 case PALMAS_REG_SMPS7:
727 if (pmic->smps457)
728 continue;
729 break;
730 case PALMAS_REG_SMPS457:
731 if (!pmic->smps457)
732 continue;
733 }
734
Axel Linbdc4baa2012-11-29 10:01:44 +0800735 /* Initialise sleep/init values from platform data */
736 if (pdata && pdata->reg_init[id]) {
737 reg_init = pdata->reg_init[id];
738 ret = palmas_smps_init(palmas, id, reg_init);
739 if (ret)
740 goto err_unregister_regulator;
741 }
742
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100743 /* Register the regulators */
744 pmic->desc[id].name = palmas_regs_info[id].name;
745 pmic->desc[id].id = id;
746
Axel Linfedd89b2012-06-06 20:01:38 +0800747 switch (id) {
748 case PALMAS_REG_SMPS10:
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100749 pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
750 pmic->desc[id].ops = &palmas_ops_smps10;
Axel Lin12565b12012-07-18 12:31:03 +0800751 pmic->desc[id].vsel_reg =
752 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
753 PALMAS_SMPS10_CTRL);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100754 pmic->desc[id].vsel_mask = SMPS10_VSEL;
Graeme Gregorya68de072012-06-22 13:36:20 +0100755 pmic->desc[id].enable_reg =
756 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
757 PALMAS_SMPS10_STATUS);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100758 pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
Axel Lin8029a002012-05-22 12:26:42 +0800759 pmic->desc[id].min_uV = 3750000;
760 pmic->desc[id].uV_step = 1250000;
Axel Linfedd89b2012-06-06 20:01:38 +0800761 break;
762 default:
Axel Linbdc4baa2012-11-29 10:01:44 +0800763 /*
764 * Read and store the RANGE bit for later use
765 * This must be done before regulator is probed,
766 * otherwise we error in probe with unsupportable ranges.
767 */
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100768 addr = palmas_regs_info[id].vsel_addr;
769
770 ret = palmas_smps_read(pmic->palmas, addr, &reg);
771 if (ret)
772 goto err_unregister_regulator;
773 if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
774 pmic->range[id] = 1;
Axel Linbdc4baa2012-11-29 10:01:44 +0800775
776 pmic->desc[id].ops = &palmas_ops_smps;
777 pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
778 pmic->desc[id].vsel_reg =
779 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
780 palmas_regs_info[id].vsel_addr);
781 pmic->desc[id].vsel_mask =
782 PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100783 }
784
Axel Linbdc4baa2012-11-29 10:01:44 +0800785 pmic->desc[id].type = REGULATOR_VOLTAGE;
786 pmic->desc[id].owner = THIS_MODULE;
787
Graeme Gregorya361cd92012-08-28 13:47:40 +0200788 if (pdata)
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100789 config.init_data = pdata->reg_data[id];
790 else
791 config.init_data = NULL;
792
Laxman Dewangan504382c2013-03-20 19:26:37 +0530793 pmic->desc[id].supply_name = palmas_regs_info[id].sname;
Graeme Gregorya361cd92012-08-28 13:47:40 +0200794 config.of_node = palmas_matches[id].of_node;
795
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100796 rdev = regulator_register(&pmic->desc[id], &config);
797 if (IS_ERR(rdev)) {
798 dev_err(&pdev->dev,
799 "failed to register %s regulator\n",
800 pdev->name);
801 ret = PTR_ERR(rdev);
802 goto err_unregister_regulator;
803 }
804
805 /* Save regulator for cleanup */
806 pmic->rdev[id] = rdev;
807 }
808
809 /* Start this loop from the id left from previous loop */
810 for (; id < PALMAS_NUM_REGS; id++) {
811
812 /* Miss out regulators which are not available due
813 * to alternate functions.
814 */
815
816 /* Register the regulators */
817 pmic->desc[id].name = palmas_regs_info[id].name;
818 pmic->desc[id].id = id;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100819 pmic->desc[id].type = REGULATOR_VOLTAGE;
820 pmic->desc[id].owner = THIS_MODULE;
Laxman Dewanganaa07f022013-04-17 15:13:12 +0530821
822 if (id < PALMAS_REG_REGEN1) {
823 pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
824 pmic->desc[id].ops = &palmas_ops_ldo;
825 pmic->desc[id].min_uV = 900000;
826 pmic->desc[id].uV_step = 50000;
827 pmic->desc[id].linear_min_sel = 1;
828 pmic->desc[id].vsel_reg =
829 PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
Axel Lin4a247a92012-07-18 12:34:08 +0800830 palmas_regs_info[id].vsel_addr);
Laxman Dewanganaa07f022013-04-17 15:13:12 +0530831 pmic->desc[id].vsel_mask =
832 PALMAS_LDO1_VOLTAGE_VSEL_MASK;
833 pmic->desc[id].enable_reg =
834 PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
Graeme Gregorya68de072012-06-22 13:36:20 +0100835 palmas_regs_info[id].ctrl_addr);
Laxman Dewanganaa07f022013-04-17 15:13:12 +0530836 pmic->desc[id].enable_mask =
837 PALMAS_LDO1_CTRL_MODE_ACTIVE;
838 } else {
839 pmic->desc[id].n_voltages = 1;
840 pmic->desc[id].ops = &palmas_ops_extreg;
841 pmic->desc[id].enable_reg =
842 PALMAS_BASE_TO_REG(PALMAS_RESOURCE_BASE,
843 palmas_regs_info[id].ctrl_addr);
844 pmic->desc[id].enable_mask =
845 PALMAS_REGEN1_CTRL_MODE_ACTIVE;
846 }
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100847
Graeme Gregorya361cd92012-08-28 13:47:40 +0200848 if (pdata)
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100849 config.init_data = pdata->reg_data[id];
850 else
851 config.init_data = NULL;
852
Laxman Dewangan504382c2013-03-20 19:26:37 +0530853 pmic->desc[id].supply_name = palmas_regs_info[id].sname;
Graeme Gregorya361cd92012-08-28 13:47:40 +0200854 config.of_node = palmas_matches[id].of_node;
855
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100856 rdev = regulator_register(&pmic->desc[id], &config);
857 if (IS_ERR(rdev)) {
858 dev_err(&pdev->dev,
859 "failed to register %s regulator\n",
860 pdev->name);
861 ret = PTR_ERR(rdev);
862 goto err_unregister_regulator;
863 }
864
865 /* Save regulator for cleanup */
866 pmic->rdev[id] = rdev;
867
868 /* Initialise sleep/init values from platform data */
Graeme Gregorya361cd92012-08-28 13:47:40 +0200869 if (pdata) {
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100870 reg_init = pdata->reg_init[id];
871 if (reg_init) {
Laxman Dewanganaa07f022013-04-17 15:13:12 +0530872 if (id < PALMAS_REG_REGEN1)
873 ret = palmas_ldo_init(palmas,
874 id, reg_init);
875 else
876 ret = palmas_extreg_init(palmas,
877 id, reg_init);
Axel Lin1c9d2d72012-07-18 12:33:20 +0800878 if (ret) {
879 regulator_unregister(pmic->rdev[id]);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100880 goto err_unregister_regulator;
Axel Lin1c9d2d72012-07-18 12:33:20 +0800881 }
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100882 }
883 }
884 }
885
886 return 0;
887
888err_unregister_regulator:
889 while (--id >= 0)
890 regulator_unregister(pmic->rdev[id]);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100891 return ret;
892}
893
Laxman Dewanganbbcf50b2013-03-18 14:59:49 +0530894static int palmas_regulators_remove(struct platform_device *pdev)
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100895{
896 struct palmas_pmic *pmic = platform_get_drvdata(pdev);
897 int id;
898
899 for (id = 0; id < PALMAS_NUM_REGS; id++)
900 regulator_unregister(pmic->rdev[id]);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100901 return 0;
902}
903
Greg Kroah-Hartman3d68dfe2012-12-21 13:26:06 -0800904static struct of_device_id of_palmas_match_tbl[] = {
Graeme Gregorya361cd92012-08-28 13:47:40 +0200905 { .compatible = "ti,palmas-pmic", },
Graeme Gregory7be859f2013-03-07 13:17:48 +0000906 { .compatible = "ti,twl6035-pmic", },
907 { .compatible = "ti,twl6036-pmic", },
908 { .compatible = "ti,twl6037-pmic", },
909 { .compatible = "ti,tps65913-pmic", },
910 { .compatible = "ti,tps65914-pmic", },
911 { .compatible = "ti,tps80036-pmic", },
Graeme Gregorya361cd92012-08-28 13:47:40 +0200912 { /* end */ }
913};
914
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100915static struct platform_driver palmas_driver = {
916 .driver = {
917 .name = "palmas-pmic",
Graeme Gregorya361cd92012-08-28 13:47:40 +0200918 .of_match_table = of_palmas_match_tbl,
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100919 .owner = THIS_MODULE,
920 },
Laxman Dewanganbbcf50b2013-03-18 14:59:49 +0530921 .probe = palmas_regulators_probe,
922 .remove = palmas_regulators_remove,
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100923};
924
925static int __init palmas_init(void)
926{
927 return platform_driver_register(&palmas_driver);
928}
929subsys_initcall(palmas_init);
930
931static void __exit palmas_exit(void)
932{
933 platform_driver_unregister(&palmas_driver);
934}
935module_exit(palmas_exit);
936
937MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
938MODULE_DESCRIPTION("Palmas voltage regulator driver");
939MODULE_LICENSE("GPL");
940MODULE_ALIAS("platform:palmas-pmic");
Graeme Gregorya361cd92012-08-28 13:47:40 +0200941MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);