blob: 1d3539324d9ae3e0ca7b8d6bff7ba1c0d8d87148 [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
Krzysztof Kozlowski724fef52016-04-05 11:05:15 +09008 * Chiwoong Byun <woong.byun@samsung.com>
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +02009 * 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>
Paul Gortmakerca1c8e92015-05-01 20:02:31 -040030#include <linux/module.h>
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +020031#include <linux/platform_device.h>
32#include <linux/regulator/driver.h>
33#include <linux/regulator/machine.h>
34#include <linux/regulator/of_regulator.h>
35#include <linux/mfd/max77686.h>
36#include <linux/mfd/max77686-private.h>
Javier Martinez Canillas5356e0d2014-10-16 18:48:50 +020037#include <dt-bindings/regulator/maxim,max77802.h>
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +020038
39/* Default ramp delay in case it is not manually set */
40#define MAX77802_RAMP_DELAY 100000 /* uV/us */
41
42#define MAX77802_OPMODE_SHIFT_LDO 6
43#define MAX77802_OPMODE_BUCK234_SHIFT 4
44#define MAX77802_OPMODE_MASK 0x3
45
46#define MAX77802_VSEL_MASK 0x3F
47#define MAX77802_DVS_VSEL_MASK 0xFF
48
49#define MAX77802_RAMP_RATE_MASK_2BIT 0xC0
50#define MAX77802_RAMP_RATE_SHIFT_2BIT 6
51#define MAX77802_RAMP_RATE_MASK_4BIT 0xF0
52#define MAX77802_RAMP_RATE_SHIFT_4BIT 4
53
Javier Martinez Canillas0505be72014-10-16 18:48:49 +020054#define MAX77802_STATUS_OFF 0x0
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +020055#define MAX77802_OFF_PWRREQ 0x1
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +020056#define MAX77802_LP_PWRREQ 0x2
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +020057
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +020058/* MAX77802 has two register formats: 2-bit and 4-bit */
59static const unsigned int ramp_table_77802_2bit[] = {
60 12500,
61 25000,
62 50000,
63 100000,
64};
65
66static unsigned int ramp_table_77802_4bit[] = {
67 1000, 2000, 3030, 4000,
68 5000, 5880, 7140, 8330,
69 9090, 10000, 11110, 12500,
70 16670, 25000, 50000, 100000,
71};
72
73struct max77802_regulator_prv {
Krzysztof Kozlowskiff633be2014-11-05 10:47:49 +010074 /* Array indexed by regulator id */
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +020075 unsigned int opmode[MAX77802_REG_MAX];
76};
77
Javier Martinez Canillas4d7078e62014-11-03 15:40:47 +010078static inline unsigned int max77802_map_mode(unsigned int mode)
Javier Martinez Canillasefbe5192014-10-16 18:48:47 +020079{
80 return mode == MAX77802_OPMODE_NORMAL ?
81 REGULATOR_MODE_NORMAL : REGULATOR_MODE_STANDBY;
82}
83
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +020084static int max77802_get_opmode_shift(int id)
85{
86 if (id == MAX77802_BUCK1 || (id >= MAX77802_BUCK5 &&
87 id <= MAX77802_BUCK10))
88 return 0;
89
90 if (id >= MAX77802_BUCK2 && id <= MAX77802_BUCK4)
91 return MAX77802_OPMODE_BUCK234_SHIFT;
92
93 if (id >= MAX77802_LDO1 && id <= MAX77802_LDO35)
94 return MAX77802_OPMODE_SHIFT_LDO;
95
96 return -EINVAL;
97}
98
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +020099/**
100 * max77802_set_suspend_disable - Disable the regulator during system suspend
101 * @rdev: regulator to mark as disabled
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200102 *
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200103 * All regulators expect LDO 1, 3, 20 and 21 support OFF by PWRREQ.
104 * Configure the regulator so the PMIC will turn it OFF during system suspend.
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200105 */
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200106static int max77802_set_suspend_disable(struct regulator_dev *rdev)
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200107{
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200108 unsigned int val = MAX77802_OFF_PWRREQ;
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200109 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
110 int id = rdev_get_id(rdev);
111 int shift = max77802_get_opmode_shift(id);
112
113 max77802->opmode[id] = val;
114 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
115 rdev->desc->enable_mask, val << shift);
116}
117
118/*
Javier Martinez Canillasefbe5192014-10-16 18:48:47 +0200119 * Some LDOs support Low Power Mode while the system is running.
120 *
121 * LDOs 1, 3, 20, 21.
122 */
123static int max77802_set_mode(struct regulator_dev *rdev, unsigned int mode)
124{
125 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
126 int id = rdev_get_id(rdev);
127 unsigned int val;
128 int shift = max77802_get_opmode_shift(id);
129
130 switch (mode) {
131 case REGULATOR_MODE_STANDBY:
132 val = MAX77802_OPMODE_LP; /* ON in Low Power Mode */
133 break;
134 case REGULATOR_MODE_NORMAL:
135 val = MAX77802_OPMODE_NORMAL; /* ON in Normal Mode */
136 break;
137 default:
138 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
139 rdev->desc->name, mode);
140 return -EINVAL;
141 }
142
143 max77802->opmode[id] = val;
144 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
145 rdev->desc->enable_mask, val << shift);
146}
147
148static unsigned max77802_get_mode(struct regulator_dev *rdev)
149{
150 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
151 int id = rdev_get_id(rdev);
152
153 return max77802_map_mode(max77802->opmode[id]);
154}
155
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200156/**
157 * max77802_set_suspend_mode - set regulator opmode when the system is suspended
158 * @rdev: regulator to change mode
159 * @mode: operating mode to be set
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200160 *
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200161 * Will set the operating mode for the regulators during system suspend.
162 * This function is valid for the three different enable control logics:
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200163 *
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200164 * Enable Control Logic1 by PWRREQ (BUCK 2-4 and LDOs 2, 4-19, 22-35)
165 * Enable Control Logic2 by PWRREQ (LDOs 1, 20, 21)
166 * Enable Control Logic3 by PWRREQ (LDO 3)
167 *
168 * If setting the regulator mode fails, the function only warns but does
169 * not return an error code to avoid the regulator core to stop setting
170 * the operating mode for the remaining regulators.
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200171 */
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200172static int max77802_set_suspend_mode(struct regulator_dev *rdev,
173 unsigned int mode)
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200174{
175 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
176 int id = rdev_get_id(rdev);
177 unsigned int val;
178 int shift = max77802_get_opmode_shift(id);
179
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200180 /*
181 * If the regulator has been disabled for suspend
182 * then is invalid to try setting a suspend mode.
183 */
Dan Carpenterbf275372014-10-27 13:45:06 +0300184 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ) {
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200185 dev_warn(&rdev->dev, "%s: is disabled, mode: 0x%x not set\n",
186 rdev->desc->name, mode);
187 return 0;
188 }
189
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200190 switch (mode) {
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200191 case REGULATOR_MODE_STANDBY:
192 /*
193 * If the regulator opmode is normal then enable
194 * ON in Low Power Mode by PWRREQ. If the mode is
195 * already Low Power then no action is required.
196 */
197 if (max77802->opmode[id] == MAX77802_OPMODE_NORMAL)
198 val = MAX77802_LP_PWRREQ;
199 else
200 return 0;
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200201 break;
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200202 case REGULATOR_MODE_NORMAL:
203 /*
204 * If the regulator operating mode is Low Power then
205 * normal is not a valid opmode in suspend. If the
206 * mode is already normal then no action is required.
207 */
208 if (max77802->opmode[id] == MAX77802_OPMODE_LP)
209 dev_warn(&rdev->dev, "%s: in Low Power: 0x%x invalid\n",
210 rdev->desc->name, mode);
211 return 0;
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200212 default:
213 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
214 rdev->desc->name, mode);
215 return -EINVAL;
216 }
217
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200218 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
219 rdev->desc->enable_mask, val << shift);
220}
221
222static int max77802_enable(struct regulator_dev *rdev)
223{
224 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
225 int id = rdev_get_id(rdev);
226 int shift = max77802_get_opmode_shift(id);
227
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200228 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ)
229 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
230
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200231 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
232 rdev->desc->enable_mask,
233 max77802->opmode[id] << shift);
234}
235
236static int max77802_find_ramp_value(struct regulator_dev *rdev,
237 const unsigned int limits[], int size,
238 unsigned int ramp_delay)
239{
240 int i;
241
242 for (i = 0; i < size; i++) {
243 if (ramp_delay <= limits[i])
244 return i;
245 }
246
247 /* Use maximum value for no ramp control */
248 dev_warn(&rdev->dev, "%s: ramp_delay: %d not supported, setting 100000\n",
249 rdev->desc->name, ramp_delay);
250 return size - 1;
251}
252
253/* Used for BUCKs 2-4 */
254static int max77802_set_ramp_delay_2bit(struct regulator_dev *rdev,
255 int ramp_delay)
256{
257 int id = rdev_get_id(rdev);
258 unsigned int ramp_value;
259
260 if (id > MAX77802_BUCK4) {
261 dev_warn(&rdev->dev,
262 "%s: regulator: ramp delay not supported\n",
263 rdev->desc->name);
264 return -EINVAL;
265 }
266 ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_2bit,
267 ARRAY_SIZE(ramp_table_77802_2bit), ramp_delay);
268
269 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
270 MAX77802_RAMP_RATE_MASK_2BIT,
271 ramp_value << MAX77802_RAMP_RATE_SHIFT_2BIT);
272}
273
274/* For BUCK1, 6 */
275static int max77802_set_ramp_delay_4bit(struct regulator_dev *rdev,
276 int ramp_delay)
277{
278 unsigned int ramp_value;
279
280 ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_4bit,
281 ARRAY_SIZE(ramp_table_77802_4bit), ramp_delay);
282
283 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
284 MAX77802_RAMP_RATE_MASK_4BIT,
285 ramp_value << MAX77802_RAMP_RATE_SHIFT_4BIT);
286}
287
288/*
289 * LDOs 2, 4-19, 22-35
290 */
291static struct regulator_ops max77802_ldo_ops_logic1 = {
292 .list_voltage = regulator_list_voltage_linear,
293 .map_voltage = regulator_map_voltage_linear,
294 .is_enabled = regulator_is_enabled_regmap,
295 .enable = max77802_enable,
296 .disable = regulator_disable_regmap,
297 .get_voltage_sel = regulator_get_voltage_sel_regmap,
298 .set_voltage_sel = regulator_set_voltage_sel_regmap,
299 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200300 .set_suspend_disable = max77802_set_suspend_disable,
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200301 .set_suspend_mode = max77802_set_suspend_mode,
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200302};
303
304/*
305 * LDOs 1, 20, 21, 3
306 */
307static struct regulator_ops max77802_ldo_ops_logic2 = {
308 .list_voltage = regulator_list_voltage_linear,
309 .map_voltage = regulator_map_voltage_linear,
310 .is_enabled = regulator_is_enabled_regmap,
311 .enable = max77802_enable,
312 .disable = regulator_disable_regmap,
313 .get_voltage_sel = regulator_get_voltage_sel_regmap,
314 .set_voltage_sel = regulator_set_voltage_sel_regmap,
315 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Javier Martinez Canillasefbe5192014-10-16 18:48:47 +0200316 .set_mode = max77802_set_mode,
317 .get_mode = max77802_get_mode,
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200318 .set_suspend_mode = max77802_set_suspend_mode,
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200319};
320
321/* BUCKS 1, 6 */
322static struct regulator_ops max77802_buck_16_dvs_ops = {
323 .list_voltage = regulator_list_voltage_linear,
324 .map_voltage = regulator_map_voltage_linear,
325 .is_enabled = regulator_is_enabled_regmap,
326 .enable = max77802_enable,
327 .disable = regulator_disable_regmap,
328 .get_voltage_sel = regulator_get_voltage_sel_regmap,
329 .set_voltage_sel = regulator_set_voltage_sel_regmap,
330 .set_voltage_time_sel = regulator_set_voltage_time_sel,
331 .set_ramp_delay = max77802_set_ramp_delay_4bit,
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200332 .set_suspend_disable = max77802_set_suspend_disable,
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200333};
334
Javier Martinez Canillasb0615f12014-10-15 18:20:33 +0200335/* BUCKs 2-4 */
336static struct regulator_ops max77802_buck_234_ops = {
337 .list_voltage = regulator_list_voltage_linear,
338 .map_voltage = regulator_map_voltage_linear,
339 .is_enabled = regulator_is_enabled_regmap,
340 .enable = max77802_enable,
341 .disable = regulator_disable_regmap,
342 .get_voltage_sel = regulator_get_voltage_sel_regmap,
343 .set_voltage_sel = regulator_set_voltage_sel_regmap,
344 .set_voltage_time_sel = regulator_set_voltage_time_sel,
345 .set_ramp_delay = max77802_set_ramp_delay_2bit,
Javier Martinez Canillasb0615f12014-10-15 18:20:33 +0200346 .set_suspend_disable = max77802_set_suspend_disable,
Javier Martinez Canillas2e0eaa12014-10-16 18:48:48 +0200347 .set_suspend_mode = max77802_set_suspend_mode,
Javier Martinez Canillasb0615f12014-10-15 18:20:33 +0200348};
349
350/* BUCKs 5, 7-10 */
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200351static struct regulator_ops max77802_buck_dvs_ops = {
352 .list_voltage = regulator_list_voltage_linear,
353 .map_voltage = regulator_map_voltage_linear,
354 .is_enabled = regulator_is_enabled_regmap,
355 .enable = max77802_enable,
356 .disable = regulator_disable_regmap,
357 .get_voltage_sel = regulator_get_voltage_sel_regmap,
358 .set_voltage_sel = regulator_set_voltage_sel_regmap,
359 .set_voltage_time_sel = regulator_set_voltage_time_sel,
360 .set_ramp_delay = max77802_set_ramp_delay_2bit,
Javier Martinez Canillase6fb2aa2014-10-15 18:20:31 +0200361 .set_suspend_disable = max77802_set_suspend_disable,
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200362};
363
364/* LDOs 3-7, 9-14, 18-26, 28, 29, 32-34 */
365#define regulator_77802_desc_p_ldo(num, supply, log) { \
366 .name = "LDO"#num, \
Krzysztof Kozlowskiff633be2014-11-05 10:47:49 +0100367 .of_match = of_match_ptr("LDO"#num), \
368 .regulators_node = of_match_ptr("regulators"), \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200369 .id = MAX77802_LDO##num, \
370 .supply_name = "inl"#supply, \
371 .ops = &max77802_ldo_ops_logic##log, \
372 .type = REGULATOR_VOLTAGE, \
373 .owner = THIS_MODULE, \
374 .min_uV = 800000, \
375 .uV_step = 50000, \
376 .ramp_delay = MAX77802_RAMP_DELAY, \
377 .n_voltages = 1 << 6, \
378 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
379 .vsel_mask = MAX77802_VSEL_MASK, \
380 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
381 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
Javier Martinez Canillas45fc84c2014-11-11 13:04:44 +0100382 .of_map_mode = max77802_map_mode, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200383}
384
385/* LDOs 1, 2, 8, 15, 17, 27, 30, 35 */
386#define regulator_77802_desc_n_ldo(num, supply, log) { \
387 .name = "LDO"#num, \
Krzysztof Kozlowskiff633be2014-11-05 10:47:49 +0100388 .of_match = of_match_ptr("LDO"#num), \
389 .regulators_node = of_match_ptr("regulators"), \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200390 .id = MAX77802_LDO##num, \
391 .supply_name = "inl"#supply, \
392 .ops = &max77802_ldo_ops_logic##log, \
393 .type = REGULATOR_VOLTAGE, \
394 .owner = THIS_MODULE, \
395 .min_uV = 800000, \
396 .uV_step = 25000, \
397 .ramp_delay = MAX77802_RAMP_DELAY, \
398 .n_voltages = 1 << 6, \
399 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
400 .vsel_mask = MAX77802_VSEL_MASK, \
401 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
402 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
Javier Martinez Canillas45fc84c2014-11-11 13:04:44 +0100403 .of_map_mode = max77802_map_mode, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200404}
405
406/* BUCKs 1, 6 */
407#define regulator_77802_desc_16_buck(num) { \
408 .name = "BUCK"#num, \
Krzysztof Kozlowskiff633be2014-11-05 10:47:49 +0100409 .of_match = of_match_ptr("BUCK"#num), \
410 .regulators_node = of_match_ptr("regulators"), \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200411 .id = MAX77802_BUCK##num, \
412 .supply_name = "inb"#num, \
413 .ops = &max77802_buck_16_dvs_ops, \
414 .type = REGULATOR_VOLTAGE, \
415 .owner = THIS_MODULE, \
416 .min_uV = 612500, \
417 .uV_step = 6250, \
418 .ramp_delay = MAX77802_RAMP_DELAY, \
419 .n_voltages = 1 << 8, \
420 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
421 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
422 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL, \
423 .enable_mask = MAX77802_OPMODE_MASK, \
Javier Martinez Canillas45fc84c2014-11-11 13:04:44 +0100424 .of_map_mode = max77802_map_mode, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200425}
426
427/* BUCKS 2-4 */
428#define regulator_77802_desc_234_buck(num) { \
429 .name = "BUCK"#num, \
Krzysztof Kozlowskiff633be2014-11-05 10:47:49 +0100430 .of_match = of_match_ptr("BUCK"#num), \
431 .regulators_node = of_match_ptr("regulators"), \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200432 .id = MAX77802_BUCK##num, \
433 .supply_name = "inb"#num, \
Javier Martinez Canillasb0615f12014-10-15 18:20:33 +0200434 .ops = &max77802_buck_234_ops, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200435 .type = REGULATOR_VOLTAGE, \
436 .owner = THIS_MODULE, \
437 .min_uV = 600000, \
438 .uV_step = 6250, \
439 .ramp_delay = MAX77802_RAMP_DELAY, \
440 .n_voltages = 0x91, \
441 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
442 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
443 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL1, \
444 .enable_mask = MAX77802_OPMODE_MASK << \
445 MAX77802_OPMODE_BUCK234_SHIFT, \
Javier Martinez Canillas45fc84c2014-11-11 13:04:44 +0100446 .of_map_mode = max77802_map_mode, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200447}
448
449/* BUCK 5 */
450#define regulator_77802_desc_buck5(num) { \
451 .name = "BUCK"#num, \
Krzysztof Kozlowskiff633be2014-11-05 10:47:49 +0100452 .of_match = of_match_ptr("BUCK"#num), \
453 .regulators_node = of_match_ptr("regulators"), \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200454 .id = MAX77802_BUCK##num, \
455 .supply_name = "inb"#num, \
456 .ops = &max77802_buck_dvs_ops, \
457 .type = REGULATOR_VOLTAGE, \
458 .owner = THIS_MODULE, \
459 .min_uV = 750000, \
460 .uV_step = 50000, \
461 .ramp_delay = MAX77802_RAMP_DELAY, \
462 .n_voltages = 1 << 6, \
463 .vsel_reg = MAX77802_REG_BUCK5OUT, \
464 .vsel_mask = MAX77802_VSEL_MASK, \
465 .enable_reg = MAX77802_REG_BUCK5CTRL, \
466 .enable_mask = MAX77802_OPMODE_MASK, \
Javier Martinez Canillas45fc84c2014-11-11 13:04:44 +0100467 .of_map_mode = max77802_map_mode, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200468}
469
470/* BUCKs 7-10 */
471#define regulator_77802_desc_buck7_10(num) { \
472 .name = "BUCK"#num, \
Krzysztof Kozlowskiff633be2014-11-05 10:47:49 +0100473 .of_match = of_match_ptr("BUCK"#num), \
474 .regulators_node = of_match_ptr("regulators"), \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200475 .id = MAX77802_BUCK##num, \
476 .supply_name = "inb"#num, \
477 .ops = &max77802_buck_dvs_ops, \
478 .type = REGULATOR_VOLTAGE, \
479 .owner = THIS_MODULE, \
480 .min_uV = 750000, \
481 .uV_step = 50000, \
482 .ramp_delay = MAX77802_RAMP_DELAY, \
483 .n_voltages = 1 << 6, \
484 .vsel_reg = MAX77802_REG_BUCK7OUT + (num - 7) * 3, \
485 .vsel_mask = MAX77802_VSEL_MASK, \
486 .enable_reg = MAX77802_REG_BUCK7CTRL + (num - 7) * 3, \
487 .enable_mask = MAX77802_OPMODE_MASK, \
Javier Martinez Canillas45fc84c2014-11-11 13:04:44 +0100488 .of_map_mode = max77802_map_mode, \
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200489}
490
Krzysztof Kozlowski6c4159a2014-10-27 16:44:13 +0100491static const struct regulator_desc regulators[] = {
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200492 regulator_77802_desc_16_buck(1),
493 regulator_77802_desc_234_buck(2),
494 regulator_77802_desc_234_buck(3),
495 regulator_77802_desc_234_buck(4),
496 regulator_77802_desc_buck5(5),
497 regulator_77802_desc_16_buck(6),
498 regulator_77802_desc_buck7_10(7),
499 regulator_77802_desc_buck7_10(8),
500 regulator_77802_desc_buck7_10(9),
501 regulator_77802_desc_buck7_10(10),
502 regulator_77802_desc_n_ldo(1, 10, 2),
503 regulator_77802_desc_n_ldo(2, 10, 1),
504 regulator_77802_desc_p_ldo(3, 3, 2),
505 regulator_77802_desc_p_ldo(4, 6, 1),
506 regulator_77802_desc_p_ldo(5, 3, 1),
507 regulator_77802_desc_p_ldo(6, 3, 1),
508 regulator_77802_desc_p_ldo(7, 3, 1),
509 regulator_77802_desc_n_ldo(8, 1, 1),
510 regulator_77802_desc_p_ldo(9, 5, 1),
511 regulator_77802_desc_p_ldo(10, 4, 1),
512 regulator_77802_desc_p_ldo(11, 4, 1),
513 regulator_77802_desc_p_ldo(12, 9, 1),
514 regulator_77802_desc_p_ldo(13, 4, 1),
515 regulator_77802_desc_p_ldo(14, 4, 1),
516 regulator_77802_desc_n_ldo(15, 1, 1),
517 regulator_77802_desc_n_ldo(17, 2, 1),
518 regulator_77802_desc_p_ldo(18, 7, 1),
519 regulator_77802_desc_p_ldo(19, 5, 1),
520 regulator_77802_desc_p_ldo(20, 7, 2),
521 regulator_77802_desc_p_ldo(21, 6, 2),
522 regulator_77802_desc_p_ldo(23, 9, 1),
523 regulator_77802_desc_p_ldo(24, 6, 1),
524 regulator_77802_desc_p_ldo(25, 9, 1),
525 regulator_77802_desc_p_ldo(26, 9, 1),
526 regulator_77802_desc_n_ldo(27, 2, 1),
527 regulator_77802_desc_p_ldo(28, 7, 1),
528 regulator_77802_desc_p_ldo(29, 7, 1),
529 regulator_77802_desc_n_ldo(30, 2, 1),
530 regulator_77802_desc_p_ldo(32, 9, 1),
531 regulator_77802_desc_p_ldo(33, 6, 1),
532 regulator_77802_desc_p_ldo(34, 9, 1),
533 regulator_77802_desc_n_ldo(35, 2, 1),
534};
535
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200536static int max77802_pmic_probe(struct platform_device *pdev)
537{
538 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200539 struct max77802_regulator_prv *max77802;
Krzysztof Kozlowskiff633be2014-11-05 10:47:49 +0100540 int i, val;
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200541 struct regulator_config config = { };
542
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200543 max77802 = devm_kzalloc(&pdev->dev,
544 sizeof(struct max77802_regulator_prv),
545 GFP_KERNEL);
546 if (!max77802)
547 return -ENOMEM;
548
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200549 config.dev = iodev->dev;
550 config.regmap = iodev->regmap;
551 config.driver_data = max77802;
552 platform_set_drvdata(pdev, max77802);
553
554 for (i = 0; i < MAX77802_REG_MAX; i++) {
555 struct regulator_dev *rdev;
Krzysztof Kozlowskiff633be2014-11-05 10:47:49 +0100556 int id = regulators[i].id;
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200557 int shift = max77802_get_opmode_shift(id);
Krzysztof Kozlowskiff633be2014-11-05 10:47:49 +0100558 int ret;
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200559
560 ret = regmap_read(iodev->regmap, regulators[i].enable_reg, &val);
Krzysztof Kozlowskia26ed452014-11-04 09:49:43 +0100561 if (ret < 0) {
562 dev_warn(&pdev->dev,
563 "cannot read current mode for %d\n", i);
564 val = MAX77802_OPMODE_NORMAL;
565 } else {
566 val = val >> shift & MAX77802_OPMODE_MASK;
567 }
Javier Martinez Canillasaea8dfb2014-08-26 13:37:41 +0200568
569 /*
570 * If the regulator is disabled and the system warm rebooted,
571 * the hardware reports OFF as the regulator operating mode.
572 * Default to operating mode NORMAL in that case.
573 */
Javier Martinez Canillas0505be72014-10-16 18:48:49 +0200574 if (val == MAX77802_STATUS_OFF)
Javier Martinez Canillasaea8dfb2014-08-26 13:37:41 +0200575 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
576 else
577 max77802->opmode[id] = val;
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200578
579 rdev = devm_regulator_register(&pdev->dev,
580 &regulators[i], &config);
581 if (IS_ERR(rdev)) {
Krzysztof Kozlowskiff633be2014-11-05 10:47:49 +0100582 ret = PTR_ERR(rdev);
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200583 dev_err(&pdev->dev,
Krzysztof Kozlowskiff633be2014-11-05 10:47:49 +0100584 "regulator init failed for %d: %d\n", i, ret);
585 return ret;
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200586 }
587 }
588
589 return 0;
590}
591
592static const struct platform_device_id max77802_pmic_id[] = {
593 {"max77802-pmic", 0},
594 { },
595};
596MODULE_DEVICE_TABLE(platform, max77802_pmic_id);
597
598static struct platform_driver max77802_pmic_driver = {
599 .driver = {
600 .name = "max77802-pmic",
Javier Martinez Canillase6f2f802014-08-18 10:32:41 +0200601 },
602 .probe = max77802_pmic_probe,
603 .id_table = max77802_pmic_id,
604};
605
606module_platform_driver(max77802_pmic_driver);
607
608MODULE_DESCRIPTION("MAXIM 77802 Regulator Driver");
609MODULE_AUTHOR("Simon Glass <sjg@chromium.org>");
610MODULE_LICENSE("GPL");