1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // max77802.c - Regulator driver for the Maxim 77802
4 //
5 // Copyright (C) 2013-2014 Google, Inc
6 // Simon Glass <sjg@chromium.org>
7 //
8 // Copyright (C) 2012 Samsung Electronics
9 // Chiwoong Byun <woong.byun@samsung.com>
10 // Jonghwa Lee <jonghwa3.lee@samsung.com>
11 //
12 // This driver is based on max8997.c
13 
14 #include <linux/kernel.h>
15 #include <linux/bug.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/of_regulator.h>
23 #include <linux/mfd/max77686.h>
24 #include <linux/mfd/max77686-private.h>
25 #include <dt-bindings/regulator/maxim,max77802.h>
26 
27 /* Default ramp delay in case it is not manually set */
28 #define MAX77802_RAMP_DELAY		100000		/* uV/us */
29 
30 #define MAX77802_OPMODE_SHIFT_LDO	6
31 #define MAX77802_OPMODE_BUCK234_SHIFT	4
32 #define MAX77802_OPMODE_MASK		0x3
33 
34 #define MAX77802_VSEL_MASK		0x3F
35 #define MAX77802_DVS_VSEL_MASK		0xFF
36 
37 #define MAX77802_RAMP_RATE_MASK_2BIT	0xC0
38 #define MAX77802_RAMP_RATE_SHIFT_2BIT	6
39 #define MAX77802_RAMP_RATE_MASK_4BIT	0xF0
40 #define MAX77802_RAMP_RATE_SHIFT_4BIT	4
41 
42 #define MAX77802_STATUS_OFF		0x0
43 #define MAX77802_OFF_PWRREQ		0x1
44 #define MAX77802_LP_PWRREQ		0x2
45 
46 static const unsigned int max77802_buck234_ramp_table[] = {
47 	12500,
48 	25000,
49 	50000,
50 	100000,
51 };
52 
53 static const unsigned int max77802_buck16_ramp_table[] = {
54 	1000,	2000,	3030,	4000,
55 	5000,	5880,	7140,	8330,
56 	9090,	10000,	11110,	12500,
57 	16670,	25000,	50000,	100000,
58 };
59 
60 struct max77802_regulator_prv {
61 	/* Array indexed by regulator id */
62 	unsigned int opmode[MAX77802_REG_MAX];
63 };
64 
max77802_map_mode(unsigned int mode)65 static inline unsigned int max77802_map_mode(unsigned int mode)
66 {
67 	return mode == MAX77802_OPMODE_NORMAL ?
68 		REGULATOR_MODE_NORMAL : REGULATOR_MODE_STANDBY;
69 }
70 
max77802_get_opmode_shift(int id)71 static int max77802_get_opmode_shift(int id)
72 {
73 	if (id == MAX77802_BUCK1 || (id >= MAX77802_BUCK5 &&
74 				     id <= MAX77802_BUCK10))
75 		return 0;
76 
77 	if (id >= MAX77802_BUCK2 && id <= MAX77802_BUCK4)
78 		return MAX77802_OPMODE_BUCK234_SHIFT;
79 
80 	if (id >= MAX77802_LDO1 && id <= MAX77802_LDO35)
81 		return MAX77802_OPMODE_SHIFT_LDO;
82 
83 	return -EINVAL;
84 }
85 
86 /**
87  * max77802_set_suspend_disable - Disable the regulator during system suspend
88  * @rdev: regulator to mark as disabled
89  *
90  * All regulators expect LDO 1, 3, 20 and 21 support OFF by PWRREQ.
91  * Configure the regulator so the PMIC will turn it OFF during system suspend.
92  */
max77802_set_suspend_disable(struct regulator_dev * rdev)93 static int max77802_set_suspend_disable(struct regulator_dev *rdev)
94 {
95 	unsigned int val = MAX77802_OFF_PWRREQ;
96 	struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
97 	unsigned int id = rdev_get_id(rdev);
98 	int shift = max77802_get_opmode_shift(id);
99 
100 	if (WARN_ON_ONCE(id >= ARRAY_SIZE(max77802->opmode)))
101 		return -EINVAL;
102 	max77802->opmode[id] = val;
103 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
104 				  rdev->desc->enable_mask, val << shift);
105 }
106 
107 /*
108  * Some LDOs support Low Power Mode while the system is running.
109  *
110  * LDOs 1, 3, 20, 21.
111  */
max77802_set_mode(struct regulator_dev * rdev,unsigned int mode)112 static int max77802_set_mode(struct regulator_dev *rdev, unsigned int mode)
113 {
114 	struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
115 	unsigned int id = rdev_get_id(rdev);
116 	unsigned int val;
117 	int shift = max77802_get_opmode_shift(id);
118 
119 	switch (mode) {
120 	case REGULATOR_MODE_STANDBY:
121 		val = MAX77802_OPMODE_LP;	/* ON in Low Power Mode */
122 		break;
123 	case REGULATOR_MODE_NORMAL:
124 		val = MAX77802_OPMODE_NORMAL;	/* ON in Normal Mode */
125 		break;
126 	default:
127 		dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
128 			 rdev->desc->name, mode);
129 		return -EINVAL;
130 	}
131 
132 	if (WARN_ON_ONCE(id >= ARRAY_SIZE(max77802->opmode)))
133 		return -EINVAL;
134 
135 	max77802->opmode[id] = val;
136 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
137 				  rdev->desc->enable_mask, val << shift);
138 }
139 
max77802_get_mode(struct regulator_dev * rdev)140 static unsigned max77802_get_mode(struct regulator_dev *rdev)
141 {
142 	struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
143 	unsigned int id = rdev_get_id(rdev);
144 
145 	if (WARN_ON_ONCE(id >= ARRAY_SIZE(max77802->opmode)))
146 		return -EINVAL;
147 	return max77802_map_mode(max77802->opmode[id]);
148 }
149 
150 /**
151  * max77802_set_suspend_mode - set regulator opmode when the system is suspended
152  * @rdev: regulator to change mode
153  * @mode: operating mode to be set
154  *
155  * Will set the operating mode for the regulators during system suspend.
156  * This function is valid for the three different enable control logics:
157  *
158  * Enable Control Logic1 by PWRREQ (BUCK 2-4 and LDOs 2, 4-19, 22-35)
159  * Enable Control Logic2 by PWRREQ (LDOs 1, 20, 21)
160  * Enable Control Logic3 by PWRREQ (LDO 3)
161  *
162  * If setting the regulator mode fails, the function only warns but does
163  * not return an error code to avoid the regulator core to stop setting
164  * the operating mode for the remaining regulators.
165  */
max77802_set_suspend_mode(struct regulator_dev * rdev,unsigned int mode)166 static int max77802_set_suspend_mode(struct regulator_dev *rdev,
167 				     unsigned int mode)
168 {
169 	struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
170 	unsigned int id = rdev_get_id(rdev);
171 	unsigned int val;
172 	int shift = max77802_get_opmode_shift(id);
173 
174 	if (WARN_ON_ONCE(id >= ARRAY_SIZE(max77802->opmode)))
175 		return -EINVAL;
176 
177 	/*
178 	 * If the regulator has been disabled for suspend
179 	 * then is invalid to try setting a suspend mode.
180 	 */
181 	if (max77802->opmode[id] == MAX77802_OFF_PWRREQ) {
182 		dev_warn(&rdev->dev, "%s: is disabled, mode: 0x%x not set\n",
183 			 rdev->desc->name, mode);
184 		return 0;
185 	}
186 
187 	switch (mode) {
188 	case REGULATOR_MODE_STANDBY:
189 		/*
190 		 * If the regulator opmode is normal then enable
191 		 * ON in Low Power Mode by PWRREQ. If the mode is
192 		 * already Low Power then no action is required.
193 		 */
194 		if (max77802->opmode[id] == MAX77802_OPMODE_NORMAL)
195 			val = MAX77802_LP_PWRREQ;
196 		else
197 			return 0;
198 		break;
199 	case REGULATOR_MODE_NORMAL:
200 		/*
201 		 * If the regulator operating mode is Low Power then
202 		 * normal is not a valid opmode in suspend. If the
203 		 * mode is already normal then no action is required.
204 		 */
205 		if (max77802->opmode[id] == MAX77802_OPMODE_LP)
206 			dev_warn(&rdev->dev, "%s: in Low Power: 0x%x invalid\n",
207 				 rdev->desc->name, mode);
208 		return 0;
209 	default:
210 		dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
211 			 rdev->desc->name, mode);
212 		return -EINVAL;
213 	}
214 
215 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
216 				  rdev->desc->enable_mask, val << shift);
217 }
218 
max77802_enable(struct regulator_dev * rdev)219 static int max77802_enable(struct regulator_dev *rdev)
220 {
221 	struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
222 	unsigned int id = rdev_get_id(rdev);
223 	int shift = max77802_get_opmode_shift(id);
224 
225 	if (WARN_ON_ONCE(id >= ARRAY_SIZE(max77802->opmode)))
226 		return -EINVAL;
227 	if (max77802->opmode[id] == MAX77802_OFF_PWRREQ)
228 		max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
229 
230 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
231 				  rdev->desc->enable_mask,
232 				  max77802->opmode[id] << shift);
233 }
234 
235 /*
236  * LDOs 2, 4-19, 22-35
237  */
238 static const struct regulator_ops max77802_ldo_ops_logic1 = {
239 	.list_voltage		= regulator_list_voltage_linear,
240 	.map_voltage		= regulator_map_voltage_linear,
241 	.is_enabled		= regulator_is_enabled_regmap,
242 	.enable			= max77802_enable,
243 	.disable		= regulator_disable_regmap,
244 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
245 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
246 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
247 	.set_suspend_disable	= max77802_set_suspend_disable,
248 	.set_suspend_mode	= max77802_set_suspend_mode,
249 };
250 
251 /*
252  * LDOs 1, 20, 21, 3
253  */
254 static const struct regulator_ops max77802_ldo_ops_logic2 = {
255 	.list_voltage		= regulator_list_voltage_linear,
256 	.map_voltage		= regulator_map_voltage_linear,
257 	.is_enabled		= regulator_is_enabled_regmap,
258 	.enable			= max77802_enable,
259 	.disable		= regulator_disable_regmap,
260 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
261 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
262 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
263 	.set_mode		= max77802_set_mode,
264 	.get_mode		= max77802_get_mode,
265 	.set_suspend_mode	= max77802_set_suspend_mode,
266 };
267 
268 /* BUCKS 1, 6 */
269 static const struct regulator_ops max77802_buck_16_dvs_ops = {
270 	.list_voltage		= regulator_list_voltage_linear,
271 	.map_voltage		= regulator_map_voltage_linear,
272 	.is_enabled		= regulator_is_enabled_regmap,
273 	.enable			= max77802_enable,
274 	.disable		= regulator_disable_regmap,
275 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
276 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
277 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
278 	.set_ramp_delay		= regulator_set_ramp_delay_regmap,
279 	.set_suspend_disable	= max77802_set_suspend_disable,
280 };
281 
282 /* BUCKs 2-4 */
283 static const struct regulator_ops max77802_buck_234_ops = {
284 	.list_voltage		= regulator_list_voltage_linear,
285 	.map_voltage		= regulator_map_voltage_linear,
286 	.is_enabled		= regulator_is_enabled_regmap,
287 	.enable			= max77802_enable,
288 	.disable		= regulator_disable_regmap,
289 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
290 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
291 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
292 	.set_ramp_delay		= regulator_set_ramp_delay_regmap,
293 	.set_suspend_disable	= max77802_set_suspend_disable,
294 	.set_suspend_mode	= max77802_set_suspend_mode,
295 };
296 
297 /* BUCKs 5, 7-10 */
298 static const struct regulator_ops max77802_buck_dvs_ops = {
299 	.list_voltage		= regulator_list_voltage_linear,
300 	.map_voltage		= regulator_map_voltage_linear,
301 	.is_enabled		= regulator_is_enabled_regmap,
302 	.enable			= max77802_enable,
303 	.disable		= regulator_disable_regmap,
304 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
305 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
306 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
307 	.set_suspend_disable	= max77802_set_suspend_disable,
308 };
309 
310 /* LDOs 3-7, 9-14, 18-26, 28, 29, 32-34 */
311 #define regulator_77802_desc_p_ldo(num, supply, log)	{		\
312 	.name		= "LDO"#num,					\
313 	.of_match	= of_match_ptr("LDO"#num),			\
314 	.regulators_node	= of_match_ptr("regulators"),		\
315 	.id		= MAX77802_LDO##num,				\
316 	.supply_name	= "inl"#supply,					\
317 	.ops		= &max77802_ldo_ops_logic##log,			\
318 	.type		= REGULATOR_VOLTAGE,				\
319 	.owner		= THIS_MODULE,					\
320 	.min_uV		= 800000,					\
321 	.uV_step	= 50000,					\
322 	.ramp_delay	= MAX77802_RAMP_DELAY,				\
323 	.n_voltages	= 1 << 6,					\
324 	.vsel_reg	= MAX77802_REG_LDO1CTRL1 + num - 1,		\
325 	.vsel_mask	= MAX77802_VSEL_MASK,				\
326 	.enable_reg	= MAX77802_REG_LDO1CTRL1 + num - 1,		\
327 	.enable_mask	= MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
328 	.of_map_mode	= max77802_map_mode,				\
329 }
330 
331 /* LDOs 1, 2, 8, 15, 17, 27, 30, 35 */
332 #define regulator_77802_desc_n_ldo(num, supply, log)   {		\
333 	.name		= "LDO"#num,					\
334 	.of_match	= of_match_ptr("LDO"#num),			\
335 	.regulators_node	= of_match_ptr("regulators"),		\
336 	.id		= MAX77802_LDO##num,				\
337 	.supply_name	= "inl"#supply,					\
338 	.ops		= &max77802_ldo_ops_logic##log,			\
339 	.type		= REGULATOR_VOLTAGE,				\
340 	.owner		= THIS_MODULE,					\
341 	.min_uV		= 800000,					\
342 	.uV_step	= 25000,					\
343 	.ramp_delay	= MAX77802_RAMP_DELAY,				\
344 	.n_voltages	= 1 << 6,					\
345 	.vsel_reg	= MAX77802_REG_LDO1CTRL1 + num - 1,		\
346 	.vsel_mask	= MAX77802_VSEL_MASK,				\
347 	.enable_reg	= MAX77802_REG_LDO1CTRL1 + num - 1,		\
348 	.enable_mask	= MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
349 	.of_map_mode	= max77802_map_mode,				\
350 }
351 
352 /* BUCKs 1, 6 */
353 #define regulator_77802_desc_16_buck(num)	{		\
354 	.name		= "BUCK"#num,					\
355 	.of_match	= of_match_ptr("BUCK"#num),			\
356 	.regulators_node	= of_match_ptr("regulators"),		\
357 	.id		= MAX77802_BUCK##num,				\
358 	.supply_name	= "inb"#num,					\
359 	.ops		= &max77802_buck_16_dvs_ops,			\
360 	.type		= REGULATOR_VOLTAGE,				\
361 	.owner		= THIS_MODULE,					\
362 	.min_uV		= 612500,					\
363 	.uV_step	= 6250,						\
364 	.ramp_delay	= MAX77802_RAMP_DELAY,				\
365 	.n_voltages	= 1 << 8,					\
366 	.vsel_reg	= MAX77802_REG_BUCK ## num ## DVS1,		\
367 	.vsel_mask	= MAX77802_DVS_VSEL_MASK,			\
368 	.enable_reg	= MAX77802_REG_BUCK ## num ## CTRL,		\
369 	.enable_mask	= MAX77802_OPMODE_MASK,				\
370 	.ramp_reg	= MAX77802_REG_BUCK ## num ## CTRL,		\
371 	.ramp_mask	= MAX77802_RAMP_RATE_MASK_4BIT,			\
372 	.ramp_delay_table = max77802_buck16_ramp_table,			\
373 	.n_ramp_values	= ARRAY_SIZE(max77802_buck16_ramp_table),	\
374 	.of_map_mode	= max77802_map_mode,				\
375 }
376 
377 /* BUCKS 2-4 */
378 #define regulator_77802_desc_234_buck(num)	{		\
379 	.name		= "BUCK"#num,					\
380 	.of_match	= of_match_ptr("BUCK"#num),			\
381 	.regulators_node	= of_match_ptr("regulators"),		\
382 	.id		= MAX77802_BUCK##num,				\
383 	.supply_name	= "inb"#num,					\
384 	.ops		= &max77802_buck_234_ops,			\
385 	.type		= REGULATOR_VOLTAGE,				\
386 	.owner		= THIS_MODULE,					\
387 	.min_uV		= 600000,					\
388 	.uV_step	= 6250,						\
389 	.ramp_delay	= MAX77802_RAMP_DELAY,				\
390 	.n_voltages	= 0x91,						\
391 	.vsel_reg	= MAX77802_REG_BUCK ## num ## DVS1,		\
392 	.vsel_mask	= MAX77802_DVS_VSEL_MASK,			\
393 	.enable_reg	= MAX77802_REG_BUCK ## num ## CTRL1,		\
394 	.enable_mask	= MAX77802_OPMODE_MASK <<			\
395 				MAX77802_OPMODE_BUCK234_SHIFT,		\
396 	.ramp_reg	= MAX77802_REG_BUCK ## num ## CTRL1,		\
397 	.ramp_mask	= MAX77802_RAMP_RATE_MASK_2BIT,			\
398 	.ramp_delay_table = max77802_buck234_ramp_table,		\
399 	.n_ramp_values	= ARRAY_SIZE(max77802_buck234_ramp_table),	\
400 	.of_map_mode	= max77802_map_mode,				\
401 }
402 
403 /* BUCK 5 */
404 #define regulator_77802_desc_buck5(num)		{		\
405 	.name		= "BUCK"#num,					\
406 	.of_match	= of_match_ptr("BUCK"#num),			\
407 	.regulators_node	= of_match_ptr("regulators"),		\
408 	.id		= MAX77802_BUCK##num,				\
409 	.supply_name	= "inb"#num,					\
410 	.ops		= &max77802_buck_dvs_ops,			\
411 	.type		= REGULATOR_VOLTAGE,				\
412 	.owner		= THIS_MODULE,					\
413 	.min_uV		= 750000,					\
414 	.uV_step	= 50000,					\
415 	.ramp_delay	= MAX77802_RAMP_DELAY,				\
416 	.n_voltages	= 1 << 6,					\
417 	.vsel_reg	= MAX77802_REG_BUCK5OUT,			\
418 	.vsel_mask	= MAX77802_VSEL_MASK,				\
419 	.enable_reg	= MAX77802_REG_BUCK5CTRL,			\
420 	.enable_mask	= MAX77802_OPMODE_MASK,				\
421 	.of_map_mode	= max77802_map_mode,				\
422 }
423 
424 /* BUCKs 7-10 */
425 #define regulator_77802_desc_buck7_10(num)	{		\
426 	.name		= "BUCK"#num,					\
427 	.of_match	= of_match_ptr("BUCK"#num),			\
428 	.regulators_node	= of_match_ptr("regulators"),		\
429 	.id		= MAX77802_BUCK##num,				\
430 	.supply_name	= "inb"#num,					\
431 	.ops		= &max77802_buck_dvs_ops,			\
432 	.type		= REGULATOR_VOLTAGE,				\
433 	.owner		= THIS_MODULE,					\
434 	.min_uV		= 750000,					\
435 	.uV_step	= 50000,					\
436 	.ramp_delay	= MAX77802_RAMP_DELAY,				\
437 	.n_voltages	= 1 << 6,					\
438 	.vsel_reg	= MAX77802_REG_BUCK7OUT + (num - 7) * 3,	\
439 	.vsel_mask	= MAX77802_VSEL_MASK,				\
440 	.enable_reg	= MAX77802_REG_BUCK7CTRL + (num - 7) * 3,	\
441 	.enable_mask	= MAX77802_OPMODE_MASK,				\
442 	.of_map_mode	= max77802_map_mode,				\
443 }
444 
445 static const struct regulator_desc regulators[] = {
446 	regulator_77802_desc_16_buck(1),
447 	regulator_77802_desc_234_buck(2),
448 	regulator_77802_desc_234_buck(3),
449 	regulator_77802_desc_234_buck(4),
450 	regulator_77802_desc_buck5(5),
451 	regulator_77802_desc_16_buck(6),
452 	regulator_77802_desc_buck7_10(7),
453 	regulator_77802_desc_buck7_10(8),
454 	regulator_77802_desc_buck7_10(9),
455 	regulator_77802_desc_buck7_10(10),
456 	regulator_77802_desc_n_ldo(1, 10, 2),
457 	regulator_77802_desc_n_ldo(2, 10, 1),
458 	regulator_77802_desc_p_ldo(3, 3, 2),
459 	regulator_77802_desc_p_ldo(4, 6, 1),
460 	regulator_77802_desc_p_ldo(5, 3, 1),
461 	regulator_77802_desc_p_ldo(6, 3, 1),
462 	regulator_77802_desc_p_ldo(7, 3, 1),
463 	regulator_77802_desc_n_ldo(8, 1, 1),
464 	regulator_77802_desc_p_ldo(9, 5, 1),
465 	regulator_77802_desc_p_ldo(10, 4, 1),
466 	regulator_77802_desc_p_ldo(11, 4, 1),
467 	regulator_77802_desc_p_ldo(12, 9, 1),
468 	regulator_77802_desc_p_ldo(13, 4, 1),
469 	regulator_77802_desc_p_ldo(14, 4, 1),
470 	regulator_77802_desc_n_ldo(15, 1, 1),
471 	regulator_77802_desc_n_ldo(17, 2, 1),
472 	regulator_77802_desc_p_ldo(18, 7, 1),
473 	regulator_77802_desc_p_ldo(19, 5, 1),
474 	regulator_77802_desc_p_ldo(20, 7, 2),
475 	regulator_77802_desc_p_ldo(21, 6, 2),
476 	regulator_77802_desc_p_ldo(23, 9, 1),
477 	regulator_77802_desc_p_ldo(24, 6, 1),
478 	regulator_77802_desc_p_ldo(25, 9, 1),
479 	regulator_77802_desc_p_ldo(26, 9, 1),
480 	regulator_77802_desc_n_ldo(27, 2, 1),
481 	regulator_77802_desc_p_ldo(28, 7, 1),
482 	regulator_77802_desc_p_ldo(29, 7, 1),
483 	regulator_77802_desc_n_ldo(30, 2, 1),
484 	regulator_77802_desc_p_ldo(32, 9, 1),
485 	regulator_77802_desc_p_ldo(33, 6, 1),
486 	regulator_77802_desc_p_ldo(34, 9, 1),
487 	regulator_77802_desc_n_ldo(35, 2, 1),
488 };
489 
max77802_pmic_probe(struct platform_device * pdev)490 static int max77802_pmic_probe(struct platform_device *pdev)
491 {
492 	struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
493 	struct max77802_regulator_prv *max77802;
494 	int i, val;
495 	struct regulator_config config = { };
496 
497 	max77802 = devm_kzalloc(&pdev->dev,
498 				sizeof(struct max77802_regulator_prv),
499 				GFP_KERNEL);
500 	if (!max77802)
501 		return -ENOMEM;
502 
503 	config.dev = iodev->dev;
504 	config.regmap = iodev->regmap;
505 	config.driver_data = max77802;
506 	platform_set_drvdata(pdev, max77802);
507 
508 	for (i = 0; i < MAX77802_REG_MAX; i++) {
509 		struct regulator_dev *rdev;
510 		unsigned int id = regulators[i].id;
511 		int shift = max77802_get_opmode_shift(id);
512 		int ret;
513 
514 		ret = regmap_read(iodev->regmap, regulators[i].enable_reg, &val);
515 		if (ret < 0) {
516 			dev_warn(&pdev->dev,
517 				"cannot read current mode for %d\n", i);
518 			val = MAX77802_OPMODE_NORMAL;
519 		} else {
520 			val = val >> shift & MAX77802_OPMODE_MASK;
521 		}
522 
523 		/*
524 		 * If the regulator is disabled and the system warm rebooted,
525 		 * the hardware reports OFF as the regulator operating mode.
526 		 * Default to operating mode NORMAL in that case.
527 		 */
528 		if (id < ARRAY_SIZE(max77802->opmode)) {
529 			if (val == MAX77802_STATUS_OFF)
530 				max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
531 			else
532 				max77802->opmode[id] = val;
533 		}
534 
535 		rdev = devm_regulator_register(&pdev->dev,
536 					       &regulators[i], &config);
537 		if (IS_ERR(rdev)) {
538 			ret = PTR_ERR(rdev);
539 			dev_err(&pdev->dev,
540 				"regulator init failed for %d: %d\n", i, ret);
541 			return ret;
542 		}
543 	}
544 
545 	return 0;
546 }
547 
548 static const struct platform_device_id max77802_pmic_id[] = {
549 	{"max77802-pmic", 0},
550 	{ },
551 };
552 MODULE_DEVICE_TABLE(platform, max77802_pmic_id);
553 
554 static struct platform_driver max77802_pmic_driver = {
555 	.driver = {
556 		.name = "max77802-pmic",
557 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
558 	},
559 	.probe = max77802_pmic_probe,
560 	.id_table = max77802_pmic_id,
561 };
562 
563 module_platform_driver(max77802_pmic_driver);
564 
565 MODULE_DESCRIPTION("MAXIM 77802 Regulator Driver");
566 MODULE_AUTHOR("Simon Glass <sjg@chromium.org>");
567 MODULE_LICENSE("GPL");
568