blob: 0ec27cf9278523cca89eb39d3855dc605d92ff6f [file] [log] [blame]
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +02001/*
2 * max77802.c - Regulator driver for the Maxim 77802
3 *
4 * Copyright (C) 2013-2014 Google, Inc
5 * Simon Glass <sjg@chromium.org>
6 *
7 * Copyright (C) 2012 Samsung Electronics
8 * Chiwoong Byun <woong.byun@smasung.com>
9 * Jonghwa Lee <jonghwa3.lee@samsung.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * This driver is based on max8997.c
22 */
23
24#include <linux/kernel.h>
25#include <linux/bug.h>
26#include <linux/err.h>
27#include <linux/gpio.h>
28#include <linux/slab.h>
29#include <linux/gpio/consumer.h>
30#include <linux/platform_device.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
33#include <linux/regulator/of_regulator.h>
34#include <linux/mfd/max77686.h>
35#include <linux/mfd/max77686-private.h>
Javier Martinez Canillas5356e0d2014-10-16 18:48:50 +020036#include <dt-bindings/regulator/maxim,max77802.h>
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +020037
38/* Default ramp delay in case it is not manually set */
39#define MAX77802_RAMP_DELAY 100000 /* uV/us */
40
41#define MAX77802_OPMODE_SHIFT_LDO 6
42#define MAX77802_OPMODE_BUCK234_SHIFT 4
43#define MAX77802_OPMODE_MASK 0x3
44
45#define MAX77802_VSEL_MASK 0x3F
46#define MAX77802_DVS_VSEL_MASK 0xFF
47
48#define MAX77802_RAMP_RATE_MASK_2BIT 0xC0
49#define MAX77802_RAMP_RATE_SHIFT_2BIT 6
50#define MAX77802_RAMP_RATE_MASK_4BIT 0xF0
51#define MAX77802_RAMP_RATE_SHIFT_4BIT 4
52
Javier Martinez Canillas0505be72014-10-16 18:48:49 +020053#define MAX77802_STATUS_OFF 0x0
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +020054#define MAX77802_OFF_PWRREQ 0x1
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +020055#define MAX77802_LP_PWRREQ 0x2
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +020056
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +020057/* MAX77802 has two register formats: 2-bit and 4-bit */
58static const unsigned int ramp_table_77802_2bit[] = {
59 12500,
60 25000,
61 50000,
62 100000,
63};
64
65static unsigned int ramp_table_77802_4bit[] = {
66 1000, 2000, 3030, 4000,
67 5000, 5880, 7140, 8330,
68 9090, 10000, 11110, 12500,
69 16670, 25000, 50000, 100000,
70};
71
72struct max77802_regulator_prv {
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +020073 unsigned int opmode[MAX77802_REG_MAX];
74};
75
Javier Martinez Canillas4d7078e62014-11-03 15:40:47 +010076static inline unsigned int max77802_map_mode(unsigned int mode)
Javier Martinez Canillasefbe5192014-10-16 18:48:47 +020077{
78 return mode == MAX77802_OPMODE_NORMAL ?
79 REGULATOR_MODE_NORMAL : REGULATOR_MODE_STANDBY;
80}
81
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +020082static int max77802_get_opmode_shift(int id)
83{
84 if (id == MAX77802_BUCK1 || (id >= MAX77802_BUCK5 &&
85 id <= MAX77802_BUCK10))
86 return 0;
87
88 if (id >= MAX77802_BUCK2 && id <= MAX77802_BUCK4)
89 return MAX77802_OPMODE_BUCK234_SHIFT;
90
91 if (id >= MAX77802_LDO1 && id <= MAX77802_LDO35)
92 return MAX77802_OPMODE_SHIFT_LDO;
93
94 return -EINVAL;
95}
96
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +020097/**
98 * max77802_set_suspend_disable - Disable the regulator during system suspend
99 * @rdev: regulator to mark as disabled
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200100 *
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200101 * All regulators expect LDO 1, 3, 20 and 21 support OFF by PWRREQ.
102 * Configure the regulator so the PMIC will turn it OFF during system suspend.
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200103 */
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200104static int max77802_set_suspend_disable(struct regulator_dev *rdev)
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200105{
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200106 unsigned int val = MAX77802_OFF_PWRREQ;
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200107 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
108 int id = rdev_get_id(rdev);
109 int shift = max77802_get_opmode_shift(id);
110
111 max77802->opmode[id] = val;
112 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
113 rdev->desc->enable_mask, val << shift);
114}
115
116/*
Javier Martinez Canillasefbe5192014-10-16 18:48:47 +0200117 * Some LDOs support Low Power Mode while the system is running.
118 *
119 * LDOs 1, 3, 20, 21.
120 */
121static int max77802_set_mode(struct regulator_dev *rdev, unsigned int mode)
122{
123 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
124 int id = rdev_get_id(rdev);
125 unsigned int val;
126 int shift = max77802_get_opmode_shift(id);
127
128 switch (mode) {
129 case REGULATOR_MODE_STANDBY:
130 val = MAX77802_OPMODE_LP; /* ON in Low Power Mode */
131 break;
132 case REGULATOR_MODE_NORMAL:
133 val = MAX77802_OPMODE_NORMAL; /* ON in Normal Mode */
134 break;
135 default:
136 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
137 rdev->desc->name, mode);
138 return -EINVAL;
139 }
140
141 max77802->opmode[id] = val;
142 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
143 rdev->desc->enable_mask, val << shift);
144}
145
146static unsigned max77802_get_mode(struct regulator_dev *rdev)
147{
148 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
149 int id = rdev_get_id(rdev);
150
151 return max77802_map_mode(max77802->opmode[id]);
152}
153
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200154/**
155 * max77802_set_suspend_mode - set regulator opmode when the system is suspended
156 * @rdev: regulator to change mode
157 * @mode: operating mode to be set
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200158 *
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200159 * Will set the operating mode for the regulators during system suspend.
160 * This function is valid for the three different enable control logics:
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200161 *
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200162 * Enable Control Logic1 by PWRREQ (BUCK 2-4 and LDOs 2, 4-19, 22-35)
163 * Enable Control Logic2 by PWRREQ (LDOs 1, 20, 21)
164 * Enable Control Logic3 by PWRREQ (LDO 3)
165 *
166 * If setting the regulator mode fails, the function only warns but does
167 * not return an error code to avoid the regulator core to stop setting
168 * the operating mode for the remaining regulators.
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200169 */
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200170static int max77802_set_suspend_mode(struct regulator_dev *rdev,
171 unsigned int mode)
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200172{
173 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
174 int id = rdev_get_id(rdev);
175 unsigned int val;
176 int shift = max77802_get_opmode_shift(id);
177
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200178 /*
179 * If the regulator has been disabled for suspend
180 * then is invalid to try setting a suspend mode.
181 */
Dan Carpenterbf275372014-10-27 13:45:06 +0300182 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ) {
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200183 dev_warn(&rdev->dev, "%s: is disabled, mode: 0x%x not set\n",
184 rdev->desc->name, mode);
185 return 0;
186 }
187
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200188 switch (mode) {
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200189 case REGULATOR_MODE_STANDBY:
190 /*
191 * If the regulator opmode is normal then enable
192 * ON in Low Power Mode by PWRREQ. If the mode is
193 * already Low Power then no action is required.
194 */
195 if (max77802->opmode[id] == MAX77802_OPMODE_NORMAL)
196 val = MAX77802_LP_PWRREQ;
197 else
198 return 0;
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200199 break;
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200200 case REGULATOR_MODE_NORMAL:
201 /*
202 * If the regulator operating mode is Low Power then
203 * normal is not a valid opmode in suspend. If the
204 * mode is already normal then no action is required.
205 */
206 if (max77802->opmode[id] == MAX77802_OPMODE_LP)
207 dev_warn(&rdev->dev, "%s: in Low Power: 0x%x invalid\n",
208 rdev->desc->name, mode);
209 return 0;
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200210 default:
211 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
212 rdev->desc->name, mode);
213 return -EINVAL;
214 }
215
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200216 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
217 rdev->desc->enable_mask, val << shift);
218}
219
220static int max77802_enable(struct regulator_dev *rdev)
221{
222 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
223 int id = rdev_get_id(rdev);
224 int shift = max77802_get_opmode_shift(id);
225
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200226 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ)
227 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
228
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200229 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
230 rdev->desc->enable_mask,
231 max77802->opmode[id] << shift);
232}
233
234static int max77802_find_ramp_value(struct regulator_dev *rdev,
235 const unsigned int limits[], int size,
236 unsigned int ramp_delay)
237{
238 int i;
239
240 for (i = 0; i < size; i++) {
241 if (ramp_delay <= limits[i])
242 return i;
243 }
244
245 /* Use maximum value for no ramp control */
246 dev_warn(&rdev->dev, "%s: ramp_delay: %d not supported, setting 100000\n",
247 rdev->desc->name, ramp_delay);
248 return size - 1;
249}
250
251/* Used for BUCKs 2-4 */
252static int max77802_set_ramp_delay_2bit(struct regulator_dev *rdev,
253 int ramp_delay)
254{
255 int id = rdev_get_id(rdev);
256 unsigned int ramp_value;
257
258 if (id > MAX77802_BUCK4) {
259 dev_warn(&rdev->dev,
260 "%s: regulator: ramp delay not supported\n",
261 rdev->desc->name);
262 return -EINVAL;
263 }
264 ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_2bit,
265 ARRAY_SIZE(ramp_table_77802_2bit), ramp_delay);
266
267 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
268 MAX77802_RAMP_RATE_MASK_2BIT,
269 ramp_value << MAX77802_RAMP_RATE_SHIFT_2BIT);
270}
271
272/* For BUCK1, 6 */
273static int max77802_set_ramp_delay_4bit(struct regulator_dev *rdev,
274 int ramp_delay)
275{
276 unsigned int ramp_value;
277
278 ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_4bit,
279 ARRAY_SIZE(ramp_table_77802_4bit), ramp_delay);
280
281 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
282 MAX77802_RAMP_RATE_MASK_4BIT,
283 ramp_value << MAX77802_RAMP_RATE_SHIFT_4BIT);
284}
285
286/*
287 * LDOs 2, 4-19, 22-35
288 */
289static struct regulator_ops max77802_ldo_ops_logic1 = {
290 .list_voltage = regulator_list_voltage_linear,
291 .map_voltage = regulator_map_voltage_linear,
292 .is_enabled = regulator_is_enabled_regmap,
293 .enable = max77802_enable,
294 .disable = regulator_disable_regmap,
295 .get_voltage_sel = regulator_get_voltage_sel_regmap,
296 .set_voltage_sel = regulator_set_voltage_sel_regmap,
297 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200298 .set_suspend_disable = max77802_set_suspend_disable,
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200299 .set_suspend_mode = max77802_set_suspend_mode,
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200300};
301
302/*
303 * LDOs 1, 20, 21, 3
304 */
305static struct regulator_ops max77802_ldo_ops_logic2 = {
306 .list_voltage = regulator_list_voltage_linear,
307 .map_voltage = regulator_map_voltage_linear,
308 .is_enabled = regulator_is_enabled_regmap,
309 .enable = max77802_enable,
310 .disable = regulator_disable_regmap,
311 .get_voltage_sel = regulator_get_voltage_sel_regmap,
312 .set_voltage_sel = regulator_set_voltage_sel_regmap,
313 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Javier Martinez Canillasefbe5192014-10-16 18:48:47 +0200314 .set_mode = max77802_set_mode,
315 .get_mode = max77802_get_mode,
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200316 .set_suspend_mode = max77802_set_suspend_mode,
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200317};
318
319/* BUCKS 1, 6 */
320static struct regulator_ops max77802_buck_16_dvs_ops = {
321 .list_voltage = regulator_list_voltage_linear,
322 .map_voltage = regulator_map_voltage_linear,
323 .is_enabled = regulator_is_enabled_regmap,
324 .enable = max77802_enable,
325 .disable = regulator_disable_regmap,
326 .get_voltage_sel = regulator_get_voltage_sel_regmap,
327 .set_voltage_sel = regulator_set_voltage_sel_regmap,
328 .set_voltage_time_sel = regulator_set_voltage_time_sel,
329 .set_ramp_delay = max77802_set_ramp_delay_4bit,
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200330 .set_suspend_disable = max77802_set_suspend_disable,
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200331};
332
Javier Martinez Canillasb0615f12014-10-15 18:20:33 +0200333/* BUCKs 2-4 */
334static struct regulator_ops max77802_buck_234_ops = {
335 .list_voltage = regulator_list_voltage_linear,
336 .map_voltage = regulator_map_voltage_linear,
337 .is_enabled = regulator_is_enabled_regmap,
338 .enable = max77802_enable,
339 .disable = regulator_disable_regmap,
340 .get_voltage_sel = regulator_get_voltage_sel_regmap,
341 .set_voltage_sel = regulator_set_voltage_sel_regmap,
342 .set_voltage_time_sel = regulator_set_voltage_time_sel,
343 .set_ramp_delay = max77802_set_ramp_delay_2bit,
Javier Martinez Canillasb0615f12014-10-15 18:20:33 +0200344 .set_suspend_disable = max77802_set_suspend_disable,
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200345 .set_suspend_mode = max77802_set_suspend_mode,
Javier Martinez Canillasb0615f12014-10-15 18:20:33 +0200346};
347
348/* BUCKs 5, 7-10 */
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200349static struct regulator_ops max77802_buck_dvs_ops = {
350 .list_voltage = regulator_list_voltage_linear,
351 .map_voltage = regulator_map_voltage_linear,
352 .is_enabled = regulator_is_enabled_regmap,
353 .enable = max77802_enable,
354 .disable = regulator_disable_regmap,
355 .get_voltage_sel = regulator_get_voltage_sel_regmap,
356 .set_voltage_sel = regulator_set_voltage_sel_regmap,
357 .set_voltage_time_sel = regulator_set_voltage_time_sel,
358 .set_ramp_delay = max77802_set_ramp_delay_2bit,
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200359 .set_suspend_disable = max77802_set_suspend_disable,
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200360};
361
362/* LDOs 3-7, 9-14, 18-26, 28, 29, 32-34 */
363#define regulator_77802_desc_p_ldo(num, supply, log) { \
364 .name = "LDO"#num, \
365 .id = MAX77802_LDO##num, \
366 .supply_name = "inl"#supply, \
367 .ops = &max77802_ldo_ops_logic##log, \
368 .type = REGULATOR_VOLTAGE, \
369 .owner = THIS_MODULE, \
370 .min_uV = 800000, \
371 .uV_step = 50000, \
372 .ramp_delay = MAX77802_RAMP_DELAY, \
373 .n_voltages = 1 << 6, \
374 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
375 .vsel_mask = MAX77802_VSEL_MASK, \
376 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
377 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
Javier Martinez Canillas45fc84c2014-11-11 13:04:44 +0100378 .of_map_mode = max77802_map_mode, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200379}
380
381/* LDOs 1, 2, 8, 15, 17, 27, 30, 35 */
382#define regulator_77802_desc_n_ldo(num, supply, log) { \
383 .name = "LDO"#num, \
384 .id = MAX77802_LDO##num, \
385 .supply_name = "inl"#supply, \
386 .ops = &max77802_ldo_ops_logic##log, \
387 .type = REGULATOR_VOLTAGE, \
388 .owner = THIS_MODULE, \
389 .min_uV = 800000, \
390 .uV_step = 25000, \
391 .ramp_delay = MAX77802_RAMP_DELAY, \
392 .n_voltages = 1 << 6, \
393 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
394 .vsel_mask = MAX77802_VSEL_MASK, \
395 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
396 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
Javier Martinez Canillas45fc84c2014-11-11 13:04:44 +0100397 .of_map_mode = max77802_map_mode, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200398}
399
400/* BUCKs 1, 6 */
401#define regulator_77802_desc_16_buck(num) { \
402 .name = "BUCK"#num, \
403 .id = MAX77802_BUCK##num, \
404 .supply_name = "inb"#num, \
405 .ops = &max77802_buck_16_dvs_ops, \
406 .type = REGULATOR_VOLTAGE, \
407 .owner = THIS_MODULE, \
408 .min_uV = 612500, \
409 .uV_step = 6250, \
410 .ramp_delay = MAX77802_RAMP_DELAY, \
411 .n_voltages = 1 << 8, \
412 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
413 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
414 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL, \
415 .enable_mask = MAX77802_OPMODE_MASK, \
Javier Martinez Canillas45fc84c2014-11-11 13:04:44 +0100416 .of_map_mode = max77802_map_mode, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200417}
418
419/* BUCKS 2-4 */
420#define regulator_77802_desc_234_buck(num) { \
421 .name = "BUCK"#num, \
422 .id = MAX77802_BUCK##num, \
423 .supply_name = "inb"#num, \
Javier Martinez Canillasb0615f12014-10-15 18:20:33 +0200424 .ops = &max77802_buck_234_ops, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200425 .type = REGULATOR_VOLTAGE, \
426 .owner = THIS_MODULE, \
427 .min_uV = 600000, \
428 .uV_step = 6250, \
429 .ramp_delay = MAX77802_RAMP_DELAY, \
430 .n_voltages = 0x91, \
431 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
432 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
433 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL1, \
434 .enable_mask = MAX77802_OPMODE_MASK << \
435 MAX77802_OPMODE_BUCK234_SHIFT, \
Javier Martinez Canillas45fc84c2014-11-11 13:04:44 +0100436 .of_map_mode = max77802_map_mode, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200437}
438
439/* BUCK 5 */
440#define regulator_77802_desc_buck5(num) { \
441 .name = "BUCK"#num, \
442 .id = MAX77802_BUCK##num, \
443 .supply_name = "inb"#num, \
444 .ops = &max77802_buck_dvs_ops, \
445 .type = REGULATOR_VOLTAGE, \
446 .owner = THIS_MODULE, \
447 .min_uV = 750000, \
448 .uV_step = 50000, \
449 .ramp_delay = MAX77802_RAMP_DELAY, \
450 .n_voltages = 1 << 6, \
451 .vsel_reg = MAX77802_REG_BUCK5OUT, \
452 .vsel_mask = MAX77802_VSEL_MASK, \
453 .enable_reg = MAX77802_REG_BUCK5CTRL, \
454 .enable_mask = MAX77802_OPMODE_MASK, \
Javier Martinez Canillas45fc84c2014-11-11 13:04:44 +0100455 .of_map_mode = max77802_map_mode, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200456}
457
458/* BUCKs 7-10 */
459#define regulator_77802_desc_buck7_10(num) { \
460 .name = "BUCK"#num, \
461 .id = MAX77802_BUCK##num, \
462 .supply_name = "inb"#num, \
463 .ops = &max77802_buck_dvs_ops, \
464 .type = REGULATOR_VOLTAGE, \
465 .owner = THIS_MODULE, \
466 .min_uV = 750000, \
467 .uV_step = 50000, \
468 .ramp_delay = MAX77802_RAMP_DELAY, \
469 .n_voltages = 1 << 6, \
470 .vsel_reg = MAX77802_REG_BUCK7OUT + (num - 7) * 3, \
471 .vsel_mask = MAX77802_VSEL_MASK, \
472 .enable_reg = MAX77802_REG_BUCK7CTRL + (num - 7) * 3, \
473 .enable_mask = MAX77802_OPMODE_MASK, \
Javier Martinez Canillas45fc84c2014-11-11 13:04:44 +0100474 .of_map_mode = max77802_map_mode, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200475}
476
Krzysztof Kozlowski6c4159a2014-10-27 16:44:13 +0100477static const struct regulator_desc regulators[] = {
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200478 regulator_77802_desc_16_buck(1),
479 regulator_77802_desc_234_buck(2),
480 regulator_77802_desc_234_buck(3),
481 regulator_77802_desc_234_buck(4),
482 regulator_77802_desc_buck5(5),
483 regulator_77802_desc_16_buck(6),
484 regulator_77802_desc_buck7_10(7),
485 regulator_77802_desc_buck7_10(8),
486 regulator_77802_desc_buck7_10(9),
487 regulator_77802_desc_buck7_10(10),
488 regulator_77802_desc_n_ldo(1, 10, 2),
489 regulator_77802_desc_n_ldo(2, 10, 1),
490 regulator_77802_desc_p_ldo(3, 3, 2),
491 regulator_77802_desc_p_ldo(4, 6, 1),
492 regulator_77802_desc_p_ldo(5, 3, 1),
493 regulator_77802_desc_p_ldo(6, 3, 1),
494 regulator_77802_desc_p_ldo(7, 3, 1),
495 regulator_77802_desc_n_ldo(8, 1, 1),
496 regulator_77802_desc_p_ldo(9, 5, 1),
497 regulator_77802_desc_p_ldo(10, 4, 1),
498 regulator_77802_desc_p_ldo(11, 4, 1),
499 regulator_77802_desc_p_ldo(12, 9, 1),
500 regulator_77802_desc_p_ldo(13, 4, 1),
501 regulator_77802_desc_p_ldo(14, 4, 1),
502 regulator_77802_desc_n_ldo(15, 1, 1),
503 regulator_77802_desc_n_ldo(17, 2, 1),
504 regulator_77802_desc_p_ldo(18, 7, 1),
505 regulator_77802_desc_p_ldo(19, 5, 1),
506 regulator_77802_desc_p_ldo(20, 7, 2),
507 regulator_77802_desc_p_ldo(21, 6, 2),
508 regulator_77802_desc_p_ldo(23, 9, 1),
509 regulator_77802_desc_p_ldo(24, 6, 1),
510 regulator_77802_desc_p_ldo(25, 9, 1),
511 regulator_77802_desc_p_ldo(26, 9, 1),
512 regulator_77802_desc_n_ldo(27, 2, 1),
513 regulator_77802_desc_p_ldo(28, 7, 1),
514 regulator_77802_desc_p_ldo(29, 7, 1),
515 regulator_77802_desc_n_ldo(30, 2, 1),
516 regulator_77802_desc_p_ldo(32, 9, 1),
517 regulator_77802_desc_p_ldo(33, 6, 1),
518 regulator_77802_desc_p_ldo(34, 9, 1),
519 regulator_77802_desc_n_ldo(35, 2, 1),
520};
521
522#ifdef CONFIG_OF
523static int max77802_pmic_dt_parse_pdata(struct platform_device *pdev,
524 struct max77686_platform_data *pdata)
525{
526 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
527 struct device_node *pmic_np, *regulators_np;
528 struct max77686_regulator_data *rdata;
529 struct of_regulator_match rmatch;
530 unsigned int i;
531
532 pmic_np = iodev->dev->of_node;
533 regulators_np = of_get_child_by_name(pmic_np, "regulators");
534 if (!regulators_np) {
535 dev_err(&pdev->dev, "could not find regulators sub-node\n");
536 return -EINVAL;
537 }
538
539 pdata->num_regulators = ARRAY_SIZE(regulators);
540 rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
541 pdata->num_regulators, GFP_KERNEL);
542 if (!rdata) {
543 of_node_put(regulators_np);
544 return -ENOMEM;
545 }
546
547 for (i = 0; i < pdata->num_regulators; i++) {
548 rmatch.name = regulators[i].name;
549 rmatch.init_data = NULL;
550 rmatch.of_node = NULL;
551 if (of_regulator_match(&pdev->dev, regulators_np, &rmatch,
552 1) != 1) {
553 dev_warn(&pdev->dev, "No matching regulator for '%s'\n",
554 rmatch.name);
555 continue;
556 }
557 rdata[i].initdata = rmatch.init_data;
558 rdata[i].of_node = rmatch.of_node;
559 rdata[i].id = regulators[i].id;
560 }
561
562 pdata->regulators = rdata;
563 of_node_put(regulators_np);
564
565 return 0;
566}
567#else
568static int max77802_pmic_dt_parse_pdata(struct platform_device *pdev,
569 struct max77686_platform_data *pdata)
570{
571 return 0;
572}
573#endif /* CONFIG_OF */
574
575static int max77802_pmic_probe(struct platform_device *pdev)
576{
577 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
578 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
579 struct max77802_regulator_prv *max77802;
580 int i, ret = 0, val;
581 struct regulator_config config = { };
582
583 /* This is allocated by the MFD driver */
584 if (!pdata) {
585 dev_err(&pdev->dev, "no platform data found for regulator\n");
586 return -ENODEV;
587 }
588
589 max77802 = devm_kzalloc(&pdev->dev,
590 sizeof(struct max77802_regulator_prv),
591 GFP_KERNEL);
592 if (!max77802)
593 return -ENOMEM;
594
595 if (iodev->dev->of_node) {
596 ret = max77802_pmic_dt_parse_pdata(pdev, pdata);
597 if (ret)
598 return ret;
599 }
600
601 config.dev = iodev->dev;
602 config.regmap = iodev->regmap;
603 config.driver_data = max77802;
604 platform_set_drvdata(pdev, max77802);
605
606 for (i = 0; i < MAX77802_REG_MAX; i++) {
607 struct regulator_dev *rdev;
608 int id = pdata->regulators[i].id;
609 int shift = max77802_get_opmode_shift(id);
610
611 config.init_data = pdata->regulators[i].initdata;
612 config.of_node = pdata->regulators[i].of_node;
613
614 ret = regmap_read(iodev->regmap, regulators[i].enable_reg, &val);
Krzysztof Kozlowskia26ed452014-11-04 09:49:43 +0100615 if (ret < 0) {
616 dev_warn(&pdev->dev,
617 "cannot read current mode for %d\n", i);
618 val = MAX77802_OPMODE_NORMAL;
619 } else {
620 val = val >> shift & MAX77802_OPMODE_MASK;
621 }
Javier Martinez Canillasaea8dfb2014-08-26 13:37:41 +0200622
623 /*
624 * If the regulator is disabled and the system warm rebooted,
625 * the hardware reports OFF as the regulator operating mode.
626 * Default to operating mode NORMAL in that case.
627 */
Javier Martinez Canillas0505be72014-10-16 18:48:49 +0200628 if (val == MAX77802_STATUS_OFF)
Javier Martinez Canillasaea8dfb2014-08-26 13:37:41 +0200629 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
630 else
631 max77802->opmode[id] = val;
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200632
633 rdev = devm_regulator_register(&pdev->dev,
634 &regulators[i], &config);
635 if (IS_ERR(rdev)) {
636 dev_err(&pdev->dev,
637 "regulator init failed for %d\n", i);
638 return PTR_ERR(rdev);
639 }
640 }
641
642 return 0;
643}
644
645static const struct platform_device_id max77802_pmic_id[] = {
646 {"max77802-pmic", 0},
647 { },
648};
649MODULE_DEVICE_TABLE(platform, max77802_pmic_id);
650
651static struct platform_driver max77802_pmic_driver = {
652 .driver = {
653 .name = "max77802-pmic",
654 .owner = THIS_MODULE,
655 },
656 .probe = max77802_pmic_probe,
657 .id_table = max77802_pmic_id,
658};
659
660module_platform_driver(max77802_pmic_driver);
661
662MODULE_DESCRIPTION("MAXIM 77802 Regulator Driver");
663MODULE_AUTHOR("Simon Glass <sjg@chromium.org>");
664MODULE_LICENSE("GPL");