blob: 24bbd620d933400fa75f62f5a80c1b92be8698b8 [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 },
168};
169
170#define SMPS_CTRL_MODE_OFF 0x00
171#define SMPS_CTRL_MODE_ON 0x01
172#define SMPS_CTRL_MODE_ECO 0x02
173#define SMPS_CTRL_MODE_PWM 0x03
174
175/* These values are derived from the data sheet. And are the number of steps
176 * where there is a voltage change, the ranges at beginning and end of register
177 * max/min values where there are no change are ommitted.
178 *
179 * So they are basically (maxV-minV)/stepV
180 */
181#define PALMAS_SMPS_NUM_VOLTAGES 116
182#define PALMAS_SMPS10_NUM_VOLTAGES 2
183#define PALMAS_LDO_NUM_VOLTAGES 50
184
185#define SMPS10_VSEL (1<<3)
186#define SMPS10_BOOST_EN (1<<2)
187#define SMPS10_BYPASS_EN (1<<1)
188#define SMPS10_SWITCH_EN (1<<0)
189
190#define REGULATOR_SLAVE 0
191
192static int palmas_smps_read(struct palmas *palmas, unsigned int reg,
193 unsigned int *dest)
194{
195 unsigned int addr;
196
197 addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
198
199 return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
200}
201
202static int palmas_smps_write(struct palmas *palmas, unsigned int reg,
203 unsigned int value)
204{
205 unsigned int addr;
206
207 addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
208
209 return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
210}
211
212static int palmas_ldo_read(struct palmas *palmas, unsigned int reg,
213 unsigned int *dest)
214{
215 unsigned int addr;
216
217 addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
218
219 return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
220}
221
222static int palmas_ldo_write(struct palmas *palmas, unsigned int reg,
223 unsigned int value)
224{
225 unsigned int addr;
226
227 addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
228
229 return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
230}
231
232static int palmas_is_enabled_smps(struct regulator_dev *dev)
233{
234 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
235 int id = rdev_get_id(dev);
236 unsigned int reg;
237
238 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
239
240 reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
241 reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
242
243 return !!(reg);
244}
245
246static int palmas_enable_smps(struct regulator_dev *dev)
247{
248 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
249 int id = rdev_get_id(dev);
250 unsigned int reg;
251
252 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
253
254 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
255 reg |= SMPS_CTRL_MODE_ON;
256
257 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
258
259 return 0;
260}
261
262static int palmas_disable_smps(struct regulator_dev *dev)
263{
264 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
265 int id = rdev_get_id(dev);
266 unsigned int reg;
267
268 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
269
270 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
271
272 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
273
274 return 0;
275}
276
277
278static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
279{
280 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
281 int id = rdev_get_id(dev);
282 unsigned int reg;
283
284 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
Axel Lin999f0c72012-06-07 17:08:21 +0800285 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100286
287 switch (mode) {
288 case REGULATOR_MODE_NORMAL:
289 reg |= SMPS_CTRL_MODE_ON;
290 break;
291 case REGULATOR_MODE_IDLE:
292 reg |= SMPS_CTRL_MODE_ECO;
293 break;
294 case REGULATOR_MODE_FAST:
295 reg |= SMPS_CTRL_MODE_PWM;
296 break;
297 default:
298 return -EINVAL;
299 }
300 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
301
302 return 0;
303}
304
305static unsigned int palmas_get_mode_smps(struct regulator_dev *dev)
306{
307 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
308 int id = rdev_get_id(dev);
309 unsigned int reg;
310
311 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
312 reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
313 reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
314
315 switch (reg) {
316 case SMPS_CTRL_MODE_ON:
317 return REGULATOR_MODE_NORMAL;
318 case SMPS_CTRL_MODE_ECO:
319 return REGULATOR_MODE_IDLE;
320 case SMPS_CTRL_MODE_PWM:
321 return REGULATOR_MODE_FAST;
322 }
323
324 return 0;
325}
326
327static int palmas_list_voltage_smps(struct regulator_dev *dev,
328 unsigned selector)
329{
330 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
331 int id = rdev_get_id(dev);
332 int mult = 1;
333
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100334 /* Read the multiplier set in VSEL register to return
335 * the correct voltage.
336 */
337 if (pmic->range[id])
338 mult = 2;
339
Axel Linad02e842012-11-29 09:59:16 +0800340 if (selector == 0)
341 return 0;
342 else if (selector < 6)
343 return 500000 * mult;
344 else
345 /* Voltage is linear mapping starting from selector 6,
346 * volt = (0.49V + ((selector - 5) * 0.01V)) * RANGE
347 * RANGE is either x1 or x2
348 */
349 return (490000 + ((selector - 5) * 10000)) * mult;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100350}
351
352static int palmas_map_voltage_smps(struct regulator_dev *rdev,
353 int min_uV, int max_uV)
354{
Axel Lin8a165df2012-07-17 11:29:03 +0800355 struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
356 int id = rdev_get_id(rdev);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100357 int ret, voltage;
358
Axel Lin8a165df2012-07-17 11:29:03 +0800359 if (min_uV == 0)
360 return 0;
361
362 if (pmic->range[id]) { /* RANGE is x2 */
363 if (min_uV < 1000000)
364 min_uV = 1000000;
Axel Linad02e842012-11-29 09:59:16 +0800365 ret = DIV_ROUND_UP(min_uV - 1000000, 20000) + 6;
Axel Lin8a165df2012-07-17 11:29:03 +0800366 } else { /* RANGE is x1 */
367 if (min_uV < 500000)
368 min_uV = 500000;
Axel Linad02e842012-11-29 09:59:16 +0800369 ret = DIV_ROUND_UP(min_uV - 500000, 10000) + 6;
Axel Lin8a165df2012-07-17 11:29:03 +0800370 }
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100371
372 /* Map back into a voltage to verify we're still in bounds */
373 voltage = palmas_list_voltage_smps(rdev, ret);
374 if (voltage < min_uV || voltage > max_uV)
375 return -EINVAL;
376
377 return ret;
378}
379
380static struct regulator_ops palmas_ops_smps = {
381 .is_enabled = palmas_is_enabled_smps,
382 .enable = palmas_enable_smps,
383 .disable = palmas_disable_smps,
384 .set_mode = palmas_set_mode_smps,
385 .get_mode = palmas_get_mode_smps,
Axel Linbdc4baa2012-11-29 10:01:44 +0800386 .get_voltage_sel = regulator_get_voltage_sel_regmap,
387 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100388 .list_voltage = palmas_list_voltage_smps,
389 .map_voltage = palmas_map_voltage_smps,
390};
391
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100392static struct regulator_ops palmas_ops_smps10 = {
393 .is_enabled = regulator_is_enabled_regmap,
394 .enable = regulator_enable_regmap,
395 .disable = regulator_disable_regmap,
396 .get_voltage_sel = regulator_get_voltage_sel_regmap,
397 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Axel Lin8029a002012-05-22 12:26:42 +0800398 .list_voltage = regulator_list_voltage_linear,
399 .map_voltage = regulator_map_voltage_linear,
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100400};
401
402static int palmas_is_enabled_ldo(struct regulator_dev *dev)
403{
404 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
405 int id = rdev_get_id(dev);
406 unsigned int reg;
407
408 palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
409
410 reg &= PALMAS_LDO1_CTRL_STATUS;
411
412 return !!(reg);
413}
414
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100415static struct regulator_ops palmas_ops_ldo = {
416 .is_enabled = palmas_is_enabled_ldo,
417 .enable = regulator_enable_regmap,
418 .disable = regulator_disable_regmap,
Axel Lin4a247a92012-07-18 12:34:08 +0800419 .get_voltage_sel = regulator_get_voltage_sel_regmap,
420 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Axel Lin9119ff62012-11-27 10:27:34 +0800421 .list_voltage = regulator_list_voltage_linear,
422 .map_voltage = regulator_map_voltage_linear,
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100423};
424
425/*
426 * setup the hardware based sleep configuration of the SMPS/LDO regulators
427 * from the platform data. This is different to the software based control
428 * supported by the regulator framework as it is controlled by toggling
429 * pins on the PMIC such as PREQ, SYSEN, ...
430 */
431static int palmas_smps_init(struct palmas *palmas, int id,
432 struct palmas_reg_init *reg_init)
433{
434 unsigned int reg;
435 unsigned int addr;
436 int ret;
437
438 addr = palmas_regs_info[id].ctrl_addr;
439
440 ret = palmas_smps_read(palmas, addr, &reg);
441 if (ret)
442 return ret;
443
Axel Linfedd89b2012-06-06 20:01:38 +0800444 switch (id) {
445 case PALMAS_REG_SMPS10:
446 if (reg_init->mode_sleep) {
447 reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
448 reg |= reg_init->mode_sleep <<
449 PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
450 }
451 break;
452 default:
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100453 if (reg_init->warm_reset)
454 reg |= PALMAS_SMPS12_CTRL_WR_S;
455
456 if (reg_init->roof_floor)
457 reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
458
459 if (reg_init->mode_sleep) {
460 reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
461 reg |= reg_init->mode_sleep <<
462 PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
463 }
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100464 }
Axel Linfedd89b2012-06-06 20:01:38 +0800465
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100466 ret = palmas_smps_write(palmas, addr, reg);
467 if (ret)
468 return ret;
469
470 if (palmas_regs_info[id].tstep_addr && reg_init->tstep) {
471 addr = palmas_regs_info[id].tstep_addr;
472
473 reg = reg_init->tstep & PALMAS_SMPS12_TSTEP_TSTEP_MASK;
474
475 ret = palmas_smps_write(palmas, addr, reg);
476 if (ret)
477 return ret;
478 }
479
480 if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
481 addr = palmas_regs_info[id].vsel_addr;
482
483 reg = reg_init->vsel;
484
485 ret = palmas_smps_write(palmas, addr, reg);
486 if (ret)
487 return ret;
488 }
489
490
491 return 0;
492}
493
494static int palmas_ldo_init(struct palmas *palmas, int id,
495 struct palmas_reg_init *reg_init)
496{
497 unsigned int reg;
498 unsigned int addr;
499 int ret;
500
501 addr = palmas_regs_info[id].ctrl_addr;
502
Axel Lin2735dae2012-07-18 12:31:59 +0800503 ret = palmas_ldo_read(palmas, addr, &reg);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100504 if (ret)
505 return ret;
506
507 if (reg_init->warm_reset)
508 reg |= PALMAS_LDO1_CTRL_WR_S;
509
510 if (reg_init->mode_sleep)
511 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
512
Axel Lin2735dae2012-07-18 12:31:59 +0800513 ret = palmas_ldo_write(palmas, addr, reg);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100514 if (ret)
515 return ret;
516
517 return 0;
518}
519
Graeme Gregorya361cd92012-08-28 13:47:40 +0200520static struct of_regulator_match palmas_matches[] = {
521 { .name = "smps12", },
522 { .name = "smps123", },
523 { .name = "smps3", },
524 { .name = "smps45", },
525 { .name = "smps457", },
526 { .name = "smps6", },
527 { .name = "smps7", },
528 { .name = "smps8", },
529 { .name = "smps9", },
530 { .name = "smps10", },
531 { .name = "ldo1", },
532 { .name = "ldo2", },
533 { .name = "ldo3", },
534 { .name = "ldo4", },
535 { .name = "ldo5", },
536 { .name = "ldo6", },
537 { .name = "ldo7", },
538 { .name = "ldo8", },
539 { .name = "ldo9", },
540 { .name = "ldoln", },
541 { .name = "ldousb", },
542};
543
Bill Pembertona5023572012-11-19 13:22:22 -0500544static void palmas_dt_to_pdata(struct device *dev,
Graeme Gregorya361cd92012-08-28 13:47:40 +0200545 struct device_node *node,
546 struct palmas_pmic_platform_data *pdata)
547{
548 struct device_node *regulators;
549 u32 prop;
550 int idx, ret;
551
Axel Linc92f5dd2013-01-27 21:16:56 +0800552 node = of_node_get(node);
Graeme Gregorya361cd92012-08-28 13:47:40 +0200553 regulators = of_find_node_by_name(node, "regulators");
554 if (!regulators) {
555 dev_info(dev, "regulator node not found\n");
556 return;
557 }
558
559 ret = of_regulator_match(dev, regulators, palmas_matches,
560 PALMAS_NUM_REGS);
Axel Linc92f5dd2013-01-27 21:16:56 +0800561 of_node_put(regulators);
Graeme Gregorya361cd92012-08-28 13:47:40 +0200562 if (ret < 0) {
563 dev_err(dev, "Error parsing regulator init data: %d\n", ret);
564 return;
565 }
566
567 for (idx = 0; idx < PALMAS_NUM_REGS; idx++) {
568 if (!palmas_matches[idx].init_data ||
569 !palmas_matches[idx].of_node)
570 continue;
571
572 pdata->reg_data[idx] = palmas_matches[idx].init_data;
573
574 pdata->reg_init[idx] = devm_kzalloc(dev,
575 sizeof(struct palmas_reg_init), GFP_KERNEL);
576
Graeme Gregory7be859f2013-03-07 13:17:48 +0000577 pdata->reg_init[idx]->warm_reset =
Axel Lin71f21462013-03-12 23:40:19 +0800578 of_property_read_bool(palmas_matches[idx].of_node,
579 "ti,warm-reset");
Graeme Gregorya361cd92012-08-28 13:47:40 +0200580
Graeme Gregory7be859f2013-03-07 13:17:48 +0000581 pdata->reg_init[idx]->roof_floor =
582 of_property_read_bool(palmas_matches[idx].of_node,
583 "ti,roof-floor");
Graeme Gregorya361cd92012-08-28 13:47:40 +0200584
585 ret = of_property_read_u32(palmas_matches[idx].of_node,
J Keerthy3c870e32013-02-18 10:44:20 +0530586 "ti,mode-sleep", &prop);
Graeme Gregorya361cd92012-08-28 13:47:40 +0200587 if (!ret)
588 pdata->reg_init[idx]->mode_sleep = prop;
589
590 ret = of_property_read_u32(palmas_matches[idx].of_node,
Graeme Gregorya361cd92012-08-28 13:47:40 +0200591 "ti,tstep", &prop);
592 if (!ret)
593 pdata->reg_init[idx]->tstep = prop;
594
Graeme Gregory7be859f2013-03-07 13:17:48 +0000595 ret = of_property_read_bool(palmas_matches[idx].of_node,
596 "ti,smps-range");
597 if (ret)
598 pdata->reg_init[idx]->vsel =
599 PALMAS_SMPS12_VOLTAGE_RANGE;
Graeme Gregorya361cd92012-08-28 13:47:40 +0200600 }
601
Graeme Gregory7be859f2013-03-07 13:17:48 +0000602 pdata->ldo6_vibrator = of_property_read_bool(node, "ti,ldo6-vibrator");
Graeme Gregorya361cd92012-08-28 13:47:40 +0200603}
604
605
Laxman Dewanganbbcf50b2013-03-18 14:59:49 +0530606static int palmas_regulators_probe(struct platform_device *pdev)
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100607{
608 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
609 struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
Graeme Gregorya361cd92012-08-28 13:47:40 +0200610 struct device_node *node = pdev->dev.of_node;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100611 struct regulator_dev *rdev;
612 struct regulator_config config = { };
613 struct palmas_pmic *pmic;
614 struct palmas_reg_init *reg_init;
615 int id = 0, ret;
616 unsigned int addr, reg;
617
Graeme Gregorya361cd92012-08-28 13:47:40 +0200618 if (node && !pdata) {
619 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
620
621 if (!pdata)
622 return -ENOMEM;
623
624 palmas_dt_to_pdata(&pdev->dev, node, pdata);
625 }
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100626
627 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
628 if (!pmic)
629 return -ENOMEM;
630
631 pmic->dev = &pdev->dev;
632 pmic->palmas = palmas;
633 palmas->pmic = pmic;
634 platform_set_drvdata(pdev, pmic);
635
636 ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
637 if (ret)
Axel Lin1c9d2d72012-07-18 12:33:20 +0800638 return ret;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100639
640 if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
641 pmic->smps123 = 1;
642
643 if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
644 pmic->smps457 = 1;
645
646 config.regmap = palmas->regmap[REGULATOR_SLAVE];
647 config.dev = &pdev->dev;
648 config.driver_data = pmic;
649
650 for (id = 0; id < PALMAS_REG_LDO1; id++) {
651
652 /*
653 * Miss out regulators which are not available due
654 * to slaving configurations.
655 */
656 switch (id) {
657 case PALMAS_REG_SMPS12:
658 case PALMAS_REG_SMPS3:
659 if (pmic->smps123)
660 continue;
661 break;
662 case PALMAS_REG_SMPS123:
663 if (!pmic->smps123)
664 continue;
665 break;
666 case PALMAS_REG_SMPS45:
667 case PALMAS_REG_SMPS7:
668 if (pmic->smps457)
669 continue;
670 break;
671 case PALMAS_REG_SMPS457:
672 if (!pmic->smps457)
673 continue;
674 }
675
Axel Linbdc4baa2012-11-29 10:01:44 +0800676 /* Initialise sleep/init values from platform data */
677 if (pdata && pdata->reg_init[id]) {
678 reg_init = pdata->reg_init[id];
679 ret = palmas_smps_init(palmas, id, reg_init);
680 if (ret)
681 goto err_unregister_regulator;
682 }
683
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100684 /* Register the regulators */
685 pmic->desc[id].name = palmas_regs_info[id].name;
686 pmic->desc[id].id = id;
687
Axel Linfedd89b2012-06-06 20:01:38 +0800688 switch (id) {
689 case PALMAS_REG_SMPS10:
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100690 pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
691 pmic->desc[id].ops = &palmas_ops_smps10;
Axel Lin12565b12012-07-18 12:31:03 +0800692 pmic->desc[id].vsel_reg =
693 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
694 PALMAS_SMPS10_CTRL);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100695 pmic->desc[id].vsel_mask = SMPS10_VSEL;
Graeme Gregorya68de072012-06-22 13:36:20 +0100696 pmic->desc[id].enable_reg =
697 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
698 PALMAS_SMPS10_STATUS);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100699 pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
Axel Lin8029a002012-05-22 12:26:42 +0800700 pmic->desc[id].min_uV = 3750000;
701 pmic->desc[id].uV_step = 1250000;
Axel Linfedd89b2012-06-06 20:01:38 +0800702 break;
703 default:
Axel Linbdc4baa2012-11-29 10:01:44 +0800704 /*
705 * Read and store the RANGE bit for later use
706 * This must be done before regulator is probed,
707 * otherwise we error in probe with unsupportable ranges.
708 */
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100709 addr = palmas_regs_info[id].vsel_addr;
710
711 ret = palmas_smps_read(pmic->palmas, addr, &reg);
712 if (ret)
713 goto err_unregister_regulator;
714 if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
715 pmic->range[id] = 1;
Axel Linbdc4baa2012-11-29 10:01:44 +0800716
717 pmic->desc[id].ops = &palmas_ops_smps;
718 pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
719 pmic->desc[id].vsel_reg =
720 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
721 palmas_regs_info[id].vsel_addr);
722 pmic->desc[id].vsel_mask =
723 PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100724 }
725
Axel Linbdc4baa2012-11-29 10:01:44 +0800726 pmic->desc[id].type = REGULATOR_VOLTAGE;
727 pmic->desc[id].owner = THIS_MODULE;
728
Graeme Gregorya361cd92012-08-28 13:47:40 +0200729 if (pdata)
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100730 config.init_data = pdata->reg_data[id];
731 else
732 config.init_data = NULL;
733
Laxman Dewangan504382c2013-03-20 19:26:37 +0530734 pmic->desc[id].supply_name = palmas_regs_info[id].sname;
Graeme Gregorya361cd92012-08-28 13:47:40 +0200735 config.of_node = palmas_matches[id].of_node;
736
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100737 rdev = regulator_register(&pmic->desc[id], &config);
738 if (IS_ERR(rdev)) {
739 dev_err(&pdev->dev,
740 "failed to register %s regulator\n",
741 pdev->name);
742 ret = PTR_ERR(rdev);
743 goto err_unregister_regulator;
744 }
745
746 /* Save regulator for cleanup */
747 pmic->rdev[id] = rdev;
748 }
749
750 /* Start this loop from the id left from previous loop */
751 for (; id < PALMAS_NUM_REGS; id++) {
752
753 /* Miss out regulators which are not available due
754 * to alternate functions.
755 */
756
757 /* Register the regulators */
758 pmic->desc[id].name = palmas_regs_info[id].name;
759 pmic->desc[id].id = id;
760 pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
761
762 pmic->desc[id].ops = &palmas_ops_ldo;
763
764 pmic->desc[id].type = REGULATOR_VOLTAGE;
765 pmic->desc[id].owner = THIS_MODULE;
Axel Lin9119ff62012-11-27 10:27:34 +0800766 pmic->desc[id].min_uV = 900000;
767 pmic->desc[id].uV_step = 50000;
768 pmic->desc[id].linear_min_sel = 1;
Axel Lin4a247a92012-07-18 12:34:08 +0800769 pmic->desc[id].vsel_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
770 palmas_regs_info[id].vsel_addr);
771 pmic->desc[id].vsel_mask = PALMAS_LDO1_VOLTAGE_VSEL_MASK;
Graeme Gregorya68de072012-06-22 13:36:20 +0100772 pmic->desc[id].enable_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
773 palmas_regs_info[id].ctrl_addr);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100774 pmic->desc[id].enable_mask = PALMAS_LDO1_CTRL_MODE_ACTIVE;
775
Graeme Gregorya361cd92012-08-28 13:47:40 +0200776 if (pdata)
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100777 config.init_data = pdata->reg_data[id];
778 else
779 config.init_data = NULL;
780
Laxman Dewangan504382c2013-03-20 19:26:37 +0530781 pmic->desc[id].supply_name = palmas_regs_info[id].sname;
Graeme Gregorya361cd92012-08-28 13:47:40 +0200782 config.of_node = palmas_matches[id].of_node;
783
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100784 rdev = regulator_register(&pmic->desc[id], &config);
785 if (IS_ERR(rdev)) {
786 dev_err(&pdev->dev,
787 "failed to register %s regulator\n",
788 pdev->name);
789 ret = PTR_ERR(rdev);
790 goto err_unregister_regulator;
791 }
792
793 /* Save regulator for cleanup */
794 pmic->rdev[id] = rdev;
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_ldo_init(palmas, id, reg_init);
Axel Lin1c9d2d72012-07-18 12:33:20 +0800801 if (ret) {
802 regulator_unregister(pmic->rdev[id]);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100803 goto err_unregister_regulator;
Axel Lin1c9d2d72012-07-18 12:33:20 +0800804 }
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100805 }
806 }
807 }
808
809 return 0;
810
811err_unregister_regulator:
812 while (--id >= 0)
813 regulator_unregister(pmic->rdev[id]);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100814 return ret;
815}
816
Laxman Dewanganbbcf50b2013-03-18 14:59:49 +0530817static int palmas_regulators_remove(struct platform_device *pdev)
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100818{
819 struct palmas_pmic *pmic = platform_get_drvdata(pdev);
820 int id;
821
822 for (id = 0; id < PALMAS_NUM_REGS; id++)
823 regulator_unregister(pmic->rdev[id]);
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100824 return 0;
825}
826
Greg Kroah-Hartman3d68dfe2012-12-21 13:26:06 -0800827static struct of_device_id of_palmas_match_tbl[] = {
Graeme Gregorya361cd92012-08-28 13:47:40 +0200828 { .compatible = "ti,palmas-pmic", },
Graeme Gregory7be859f2013-03-07 13:17:48 +0000829 { .compatible = "ti,twl6035-pmic", },
830 { .compatible = "ti,twl6036-pmic", },
831 { .compatible = "ti,twl6037-pmic", },
832 { .compatible = "ti,tps65913-pmic", },
833 { .compatible = "ti,tps65914-pmic", },
834 { .compatible = "ti,tps80036-pmic", },
Graeme Gregorya361cd92012-08-28 13:47:40 +0200835 { /* end */ }
836};
837
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100838static struct platform_driver palmas_driver = {
839 .driver = {
840 .name = "palmas-pmic",
Graeme Gregorya361cd92012-08-28 13:47:40 +0200841 .of_match_table = of_palmas_match_tbl,
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100842 .owner = THIS_MODULE,
843 },
Laxman Dewanganbbcf50b2013-03-18 14:59:49 +0530844 .probe = palmas_regulators_probe,
845 .remove = palmas_regulators_remove,
Graeme Gregorye5ce4202012-05-18 16:53:57 +0100846};
847
848static int __init palmas_init(void)
849{
850 return platform_driver_register(&palmas_driver);
851}
852subsys_initcall(palmas_init);
853
854static void __exit palmas_exit(void)
855{
856 platform_driver_unregister(&palmas_driver);
857}
858module_exit(palmas_exit);
859
860MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
861MODULE_DESCRIPTION("Palmas voltage regulator driver");
862MODULE_LICENSE("GPL");
863MODULE_ALIAS("platform:palmas-pmic");
Graeme Gregorya361cd92012-08-28 13:47:40 +0200864MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);