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