diff mbox series

[3/4] regulator: rt5120: Add PMIC regulator support

Message ID 1654581161-12349-4-git-send-email-u0084500@gmail.com
State New
Headers show
Series Add Richtek RT5120 PMIC support | expand

Commit Message

cy_huang June 7, 2022, 5:52 a.m. UTC
From: ChiYuan Huang <cy_huang@richtek.com>

Add RT5120 PMIC regulator support.

It integrates 4 buck convertes, 1 LDO voltage regulator, 1 external
enable signal to control the external power source.

Signed-off-by: ChiYuan Huang <cy_huang@richtek.com>
---
 drivers/regulator/Kconfig            |  10 +
 drivers/regulator/Makefile           |   1 +
 drivers/regulator/rt5120-regulator.c | 417 +++++++++++++++++++++++++++++++++++
 3 files changed, 428 insertions(+)
 create mode 100644 drivers/regulator/rt5120-regulator.c

Comments

Mark Brown June 7, 2022, 7 p.m. UTC | #1
On Tue, Jun 07, 2022 at 01:52:40PM +0800, cy_huang wrote:

This looks mostly good, a few things though:

> +static void rt5120_fillin_regulator_desc(struct regulator_desc *desc, int rid)
> +{
> +	static const char * const name[] = { "buck1", "buck2", "buck3", "buck4",
> +					     "ldo", "exten" };
> +	static const char * const sname[] = { "vin1", "vin2", "vin3", "vin4",
> +					      "vinldo", NULL };

It would be easier and clearer to just make this a static table like
other drivers do, there's no need to generate anything dynamically as
far as I can see.

> +static int rt5120_of_parse_cb(struct rt5120_priv *priv, int rid,
> +			      struct of_regulator_match *match)
> +{
> +	struct regulator_desc *desc = priv->rdesc + rid;
> +	struct regulator_init_data *init_data = match->init_data;
> +
> +	if (!init_data || rid == RT5120_REGULATOR_BUCK1)
> +		return 0;
> +
> +	if (init_data->constraints.min_uV != init_data->constraints.max_uV) {
> +		dev_err(priv->dev, "Variable voltage for fixed regulator\n");
> +		return -EINVAL;
> +	}
> +
> +	desc->fixed_uV = init_data->constraints.min_uV;
> +	init_data->constraints.apply_uV = 0;

Drivers should never override constraints passed in by machine drivers,
if there's validation needed let the core do it.  The same probably
applies to providing a voltage range for a fixed regulator though that's
not modifying everything so not such a problem.

> +static int rt5120_parse_regulator_dt_data(struct rt5120_priv *priv)
> +{
> +	struct device *dev = priv->dev->parent;
> +	struct device_node *reg_node;
> +	int i, ret;
> +
> +	for (i = 0; i < RT5120_MAX_REGULATOR; i++) {
> +		rt5120_fillin_regulator_desc(priv->rdesc + i, i);
> +
> +		rt5120_regu_match[i].desc = priv->rdesc + i;
> +	}

Like I said above just make the list of regulators static data and loop
through registering them.

> +
> +	reg_node = of_get_child_by_name(dev->of_node, "regulators");
> +	if (!reg_node) {
> +		dev_err(priv->dev, "Couldn't find 'regulators' node\n");
> +		return -ENODEV;
> +	}
> +
> +	ret = of_regulator_match(priv->dev, reg_node, rt5120_regu_match,
> +				 ARRAY_SIZE(rt5120_regu_match));
> +
> +	of_node_put(reg_node);
> +
> +	if (ret < 0) {
> +		dev_err(priv->dev,
> +			"Error parsing regulator init data (%d)\n", ret);
> +		return ret;
> +	}
> +
> +	for (i = 0; i < RT5120_MAX_REGULATOR; i++) {
> +		ret = rt5120_of_parse_cb(priv, i, rt5120_regu_match + i);
> +		if (ret) {
> +			dev_err(priv->dev, "Failed in [%d] of_passe_cb\n", i);
> +			return ret;
> +		}
> +	}

This is all open coding stuff that's in the core - just provde an
of_parse_cb() operation and let the core take care of calling it.

> +static int rt5120_device_property_init(struct rt5120_priv *priv)
> +{
> +	struct device *dev = priv->dev->parent;
> +	bool prot_enable;
> +	unsigned int prot_enable_val = 0;
> +
> +	/* Assign UV/OV HW protection behavior */
> +	prot_enable = device_property_read_bool(dev,
> +					"richtek,enable-undervolt-hiccup");
> +	if (prot_enable)
> +		prot_enable_val |= RT5120_UVHICCUP_MASK;

Use the DT APIs to parse DT - since ACPI has a very strong idea of how
power management works which is fundamentally incompatible with with the
DT model we should be writing code in a way that minimises the risk that
we'll end up trying to parse DT properties out of ACPI systems and
creating confusion as DT and ACPI software tries to run on the same
system.
cy_huang June 8, 2022, 3:15 a.m. UTC | #2
Mark Brown <broonie@kernel.org> 於 2022年6月8日 週三 上午3:00寫道:
>
> On Tue, Jun 07, 2022 at 01:52:40PM +0800, cy_huang wrote:
>
> This looks mostly good, a few things though:
>
> > +static void rt5120_fillin_regulator_desc(struct regulator_desc *desc, int rid)
> > +{
> > +     static const char * const name[] = { "buck1", "buck2", "buck3", "buck4",
> > +                                          "ldo", "exten" };
> > +     static const char * const sname[] = { "vin1", "vin2", "vin3", "vin4",
> > +                                           "vinldo", NULL };
>
> It would be easier and clearer to just make this a static table like
> other drivers do, there's no need to generate anything dynamically as
> far as I can see.
My excuse. let me explain it.
buck1 voltage range from 600mV to 1393.75mV.
buck2~4/ldo/exten is the fixed regulator.
buck3 and buck4 is fixed by the IC efuse default.
buck2 and ldo is fixed by the external resistor chosen.
exten is designed to connected to the external power.

That's why I cannot directly declared it as the static regulator_desc.
>
> > +static int rt5120_of_parse_cb(struct rt5120_priv *priv, int rid,
> > +                           struct of_regulator_match *match)
> > +{
> > +     struct regulator_desc *desc = priv->rdesc + rid;
> > +     struct regulator_init_data *init_data = match->init_data;
> > +
> > +     if (!init_data || rid == RT5120_REGULATOR_BUCK1)
> > +             return 0;
> > +
> > +     if (init_data->constraints.min_uV != init_data->constraints.max_uV) {
> > +             dev_err(priv->dev, "Variable voltage for fixed regulator\n");
> > +             return -EINVAL;
> > +     }
> > +
> > +     desc->fixed_uV = init_data->constraints.min_uV;
> > +     init_data->constraints.apply_uV = 0;
>
> Drivers should never override constraints passed in by machine drivers,
> if there's validation needed let the core do it.  The same probably
> applies to providing a voltage range for a fixed regulator though that's
> not modifying everything so not such a problem.
Please check the above explanation about each power rails.
>
> > +static int rt5120_parse_regulator_dt_data(struct rt5120_priv *priv)
> > +{
> > +     struct device *dev = priv->dev->parent;
> > +     struct device_node *reg_node;
> > +     int i, ret;
> > +
> > +     for (i = 0; i < RT5120_MAX_REGULATOR; i++) {
> > +             rt5120_fillin_regulator_desc(priv->rdesc + i, i);
> > +
> > +             rt5120_regu_match[i].desc = priv->rdesc + i;
> > +     }
>
> Like I said above just make the list of regulators static data and loop
> through registering them.
Ditto
>
> > +
> > +     reg_node = of_get_child_by_name(dev->of_node, "regulators");
> > +     if (!reg_node) {
> > +             dev_err(priv->dev, "Couldn't find 'regulators' node\n");
> > +             return -ENODEV;
> > +     }
> > +
> > +     ret = of_regulator_match(priv->dev, reg_node, rt5120_regu_match,
> > +                              ARRAY_SIZE(rt5120_regu_match));
> > +
> > +     of_node_put(reg_node);
> > +
> > +     if (ret < 0) {
> > +             dev_err(priv->dev,
> > +                     "Error parsing regulator init data (%d)\n", ret);
> > +             return ret;
> > +     }
> > +
> > +     for (i = 0; i < RT5120_MAX_REGULATOR; i++) {
> > +             ret = rt5120_of_parse_cb(priv, i, rt5120_regu_match + i);
> > +             if (ret) {
> > +                     dev_err(priv->dev, "Failed in [%d] of_passe_cb\n", i);
> > +                     return ret;
> > +             }
> > +     }
>
> This is all open coding stuff that's in the core - just provde an
> of_parse_cb() operation and let the core take care of calling it.
Ditto
>
> > +static int rt5120_device_property_init(struct rt5120_priv *priv)
> > +{
> > +     struct device *dev = priv->dev->parent;
> > +     bool prot_enable;
> > +     unsigned int prot_enable_val = 0;
> > +
> > +     /* Assign UV/OV HW protection behavior */
> > +     prot_enable = device_property_read_bool(dev,
> > +                                     "richtek,enable-undervolt-hiccup");
> > +     if (prot_enable)
> > +             prot_enable_val |= RT5120_UVHICCUP_MASK;
>
> Use the DT APIs to parse DT - since ACPI has a very strong idea of how
> power management works which is fundamentally incompatible with with the
> DT model we should be writing code in a way that minimises the risk that
> we'll end up trying to parse DT properties out of ACPI systems and
> creating confusion as DT and ACPI software tries to run on the same
> system.
Ok, I'll replace it by DT API 'of_property_read_bool'.
Mark Brown June 8, 2022, 10:12 a.m. UTC | #3
On Wed, Jun 08, 2022 at 11:15:56AM +0800, ChiYuan Huang wrote:
> Mark Brown <broonie@kernel.org> 於 2022年6月8日 週三 上午3:00寫道:
> > On Tue, Jun 07, 2022 at 01:52:40PM +0800, cy_huang wrote:

> > > +     static const char * const name[] = { "buck1", "buck2", "buck3", "buck4",
> > > +                                          "ldo", "exten" };
> > > +     static const char * const sname[] = { "vin1", "vin2", "vin3", "vin4",
> > > +                                           "vinldo", NULL };

> > It would be easier and clearer to just make this a static table like
> > other drivers do, there's no need to generate anything dynamically as
> > far as I can see.

> My excuse. let me explain it.
> buck1 voltage range from 600mV to 1393.75mV.
> buck2~4/ldo/exten is the fixed regulator.
> buck3 and buck4 is fixed by the IC efuse default.
> buck2 and ldo is fixed by the external resistor chosen.
> exten is designed to connected to the external power.

> That's why I cannot directly declared it as the static regulator_desc.

So buck 2-4 need some dynamic handling then but the rest can be static -
that would be a lot clearer.  You could also have a template for the
ones with some dynamic values and just override the few fields that need
it.

> > > +     if (init_data->constraints.min_uV != init_data->constraints.max_uV) {
> > > +             dev_err(priv->dev, "Variable voltage for fixed regulator\n");
> > > +             return -EINVAL;
> > > +     }
> > > +
> > > +     desc->fixed_uV = init_data->constraints.min_uV;
> > > +     init_data->constraints.apply_uV = 0;

> > Drivers should never override constraints passed in by machine drivers,
> > if there's validation needed let the core do it.  The same probably
> > applies to providing a voltage range for a fixed regulator though that's
> > not modifying everything so not such a problem.

> Please check the above explanation about each power rails.

I'm not sure what you're referencing here?

> > > +     for (i = 0; i < RT5120_MAX_REGULATOR; i++) {
> > > +             ret = rt5120_of_parse_cb(priv, i, rt5120_regu_match + i);
> > > +             if (ret) {
> > > +                     dev_err(priv->dev, "Failed in [%d] of_passe_cb\n", i);
> > > +                     return ret;
> > > +             }
> > > +     }
> >
> > This is all open coding stuff that's in the core - just provde an
> > of_parse_cb() operation and let the core take care of calling it.

> Ditto

Or here.
cy_huang June 9, 2022, 6:35 a.m. UTC | #4
Mark Brown <broonie@kernel.org> 於 2022年6月8日 週三 下午6:12寫道:
>
> On Wed, Jun 08, 2022 at 11:15:56AM +0800, ChiYuan Huang wrote:
> > Mark Brown <broonie@kernel.org> 於 2022年6月8日 週三 上午3:00寫道:
> > > On Tue, Jun 07, 2022 at 01:52:40PM +0800, cy_huang wrote:
>
> > > > +     static const char * const name[] = { "buck1", "buck2", "buck3", "buck4",
> > > > +                                          "ldo", "exten" };
> > > > +     static const char * const sname[] = { "vin1", "vin2", "vin3", "vin4",
> > > > +                                           "vinldo", NULL };
>
> > > It would be easier and clearer to just make this a static table like
> > > other drivers do, there's no need to generate anything dynamically as
> > > far as I can see.
>
> > My excuse. let me explain it.
> > buck1 voltage range from 600mV to 1393.75mV.
> > buck2~4/ldo/exten is the fixed regulator.
> > buck3 and buck4 is fixed by the IC efuse default.
> > buck2 and ldo is fixed by the external resistor chosen.
> > exten is designed to connected to the external power.
>
> > That's why I cannot directly declared it as the static regulator_desc.
>
> So buck 2-4 need some dynamic handling then but the rest can be static -
> that would be a lot clearer.  You could also have a template for the
> ones with some dynamic values and just override the few fields that need
> it.
>
Not just buck2/3, buck2/3/4/ldo/exten all need the dynamic handling.

> > > > +     if (init_data->constraints.min_uV != init_data->constraints.max_uV) {
> > > > +             dev_err(priv->dev, "Variable voltage for fixed regulator\n");
> > > > +             return -EINVAL;
> > > > +     }
> > > > +
> > > > +     desc->fixed_uV = init_data->constraints.min_uV;
> > > > +     init_data->constraints.apply_uV = 0;
>
> > > Drivers should never override constraints passed in by machine drivers,
> > > if there's validation needed let the core do it.  The same probably
> > > applies to providing a voltage range for a fixed regulator though that's
> > > not modifying everything so not such a problem.
>
> > Please check the above explanation about each power rails.
>
> I'm not sure what you're referencing here?
>
Sorry. Let me explain it.
You mean 'of_parse_cb' must not override constraint.
But if the regulator is fixed and dynamic, after
'of_get_regulation_constraint', apply_uV will be true.
The is referring to 'fixed.c'

> > > > +     for (i = 0; i < RT5120_MAX_REGULATOR; i++) {
> > > > +             ret = rt5120_of_parse_cb(priv, i, rt5120_regu_match + i);
> > > > +             if (ret) {
> > > > +                     dev_err(priv->dev, "Failed in [%d] of_passe_cb\n", i);
> > > > +                     return ret;
> > > > +             }
> > > > +     }
> > >
> > > This is all open coding stuff that's in the core - just provde an
> > > of_parse_cb() operation and let the core take care of calling it.
>
> > Ditto
>
> Or here.
If I put 'of_parce_cb' to make core handling it, the input parameter
'init_data' is declared as const.
I cannot override the 'apply_uV'.
Right?
Mark Brown June 10, 2022, 11:03 a.m. UTC | #5
On Thu, Jun 09, 2022 at 02:35:07PM +0800, ChiYuan Huang wrote:
> Mark Brown <broonie@kernel.org> 於 2022年6月8日 週三 下午6:12寫道:
> > On Wed, Jun 08, 2022 at 11:15:56AM +0800, ChiYuan Huang wrote:

> > > My excuse. let me explain it.
> > > buck1 voltage range from 600mV to 1393.75mV.
> > > buck2~4/ldo/exten is the fixed regulator.
> > > buck3 and buck4 is fixed by the IC efuse default.
> > > buck2 and ldo is fixed by the external resistor chosen.
> > > exten is designed to connected to the external power.

> > > That's why I cannot directly declared it as the static regulator_desc.

> > So buck 2-4 need some dynamic handling then but the rest can be static -
> > that would be a lot clearer.  You could also have a template for the
> > ones with some dynamic values and just override the few fields that need
> > it.

> Not just buck2/3, buck2/3/4/ldo/exten all need the dynamic handling.

Why do the others need it?

> > > > Drivers should never override constraints passed in by machine drivers,
> > > > if there's validation needed let the core do it.  The same probably
> > > > applies to providing a voltage range for a fixed regulator though that's
> > > > not modifying everything so not such a problem.

> > > Please check the above explanation about each power rails.

> > I'm not sure what you're referencing here?

> Sorry. Let me explain it.

> You mean 'of_parse_cb' must not override constraint.
> But if the regulator is fixed and dynamic, after
> 'of_get_regulation_constraint', apply_uV will be true.
> The is referring to 'fixed.c'

fixed.c is a special case due to legacy issues and being generic, for
normal fixed voltage regulators in a device where we know what they're
fixed to they can just have their voltage hard coded in the driver.  If
there's issues with the machine providing invalid or nonsensical
constraints the driver should just let the core deal with them.

> > > > This is all open coding stuff that's in the core - just provde an
> > > > of_parse_cb() operation and let the core take care of calling it.

> > > Ditto

> > Or here.

> If I put 'of_parce_cb' to make core handling it, the input parameter
> 'init_data' is declared as const.
> I cannot override the 'apply_uV'.
> Right?

Yes, that's by design.
cy_huang June 13, 2022, 1:49 a.m. UTC | #6
Mark Brown <broonie@kernel.org> 於 2022年6月10日 週五 下午7:03寫道:
>
> On Thu, Jun 09, 2022 at 02:35:07PM +0800, ChiYuan Huang wrote:
> > Mark Brown <broonie@kernel.org> 於 2022年6月8日 週三 下午6:12寫道:
> > > On Wed, Jun 08, 2022 at 11:15:56AM +0800, ChiYuan Huang wrote:
>
> > > > My excuse. let me explain it.
> > > > buck1 voltage range from 600mV to 1393.75mV.
> > > > buck2~4/ldo/exten is the fixed regulator.
> > > > buck3 and buck4 is fixed by the IC efuse default.
> > > > buck2 and ldo is fixed by the external resistor chosen.
> > > > exten is designed to connected to the external power.
>
> > > > That's why I cannot directly declared it as the static regulator_desc.
>
> > > So buck 2-4 need some dynamic handling then but the rest can be static -
> > > that would be a lot clearer.  You could also have a template for the
> > > ones with some dynamic values and just override the few fields that need
> > > it.
>
> > Not just buck2/3, buck2/3/4/ldo/exten all need the dynamic handling.
>
> Why do the others need it?
>
Sometimes, for this kind of general purpose PMIC, it need to provide
the flexibility.
Cause buck2 and ldo can already be fixed by the external resistor,
buck3 and buck4 seems to be fixed by IC default.
So there may be the same part number and use the postfix to be
different like as 5120'A'/5120'B', etc...
And use it to define the voltage for the different IC default for
buck3 and buck4, and exten behavior.
That's due to the different application use the different power on
sequence and default voltages.l

> > > > > Drivers should never override constraints passed in by machine drivers,
> > > > > if there's validation needed let the core do it.  The same probably
> > > > > applies to providing a voltage range for a fixed regulator though that's
> > > > > not modifying everything so not such a problem.
>
> > > > Please check the above explanation about each power rails.
>
> > > I'm not sure what you're referencing here?
>
> > Sorry. Let me explain it.
>
> > You mean 'of_parse_cb' must not override constraint.
> > But if the regulator is fixed and dynamic, after
> > 'of_get_regulation_constraint', apply_uV will be true.
> > The is referring to 'fixed.c'
>
> fixed.c is a special case due to legacy issues and being generic, for
> normal fixed voltage regulators in a device where we know what they're
> fixed to they can just have their voltage hard coded in the driver.  If
> there's issues with the machine providing invalid or nonsensical
> constraints the driver should just let the core deal with them.
>
> > > > > This is all open coding stuff that's in the core - just provde an
> > > > > of_parse_cb() operation and let the core take care of calling it.
>
> > > > Ditto
>
> > > Or here.
>
> > If I put 'of_parce_cb' to make core handling it, the input parameter
> > 'init_data' is declared as const.
> > I cannot override the 'apply_uV'.
> > Right?
>
> Yes, that's by design.
I have traced the code for 'of_get_regulator_init_data' and
'set_machine_constraints' in regulator register.
If I cannot overwrite apply_uV variable, it will cause the
regulator_register return -EINVAL.
Is the below flow that you suggested?
1. of_parse_cb to check min_uV and max_uV, and fill in the fixed_uV in
regulator_desc
2. provide the duummy set/get voltage  to make set_machine_constraints
not return '-EINVAL'.
Mark Brown June 13, 2022, 11:10 a.m. UTC | #7
On Mon, Jun 13, 2022 at 09:49:31AM +0800, ChiYuan Huang wrote:
> Mark Brown <broonie@kernel.org> 於 2022年6月10日 週五 下午7:03寫道:

> > > Not just buck2/3, buck2/3/4/ldo/exten all need the dynamic handling.

> > Why do the others need it?

> Sometimes, for this kind of general purpose PMIC, it need to provide
> the flexibility.
> Cause buck2 and ldo can already be fixed by the external resistor,
> buck3 and buck4 seems to be fixed by IC default.
> So there may be the same part number and use the postfix to be
> different like as 5120'A'/5120'B', etc...
> And use it to define the voltage for the different IC default for
> buck3 and buck4, and exten behavior.
> That's due to the different application use the different power on
> sequence and default voltages.l

Variants should have separate compatibles, and if the code is doing
something fixed then it shouldn't make any difference if that fixed
thing is written in code or as a data table.

> > > If I put 'of_parce_cb' to make core handling it, the input parameter
> > > 'init_data' is declared as const.
> > > I cannot override the 'apply_uV'.

> > > Right?

> > Yes, that's by design.

> I have traced the code for 'of_get_regulator_init_data' and
> 'set_machine_constraints' in regulator register.
> If I cannot overwrite apply_uV variable, it will cause the
> regulator_register return -EINVAL.

We have a very large number of fixed voltage regulators in use on
various systems which manage perfectly fine without this.  If the
constraints are trying to set something invalid then they're what 
should be fixed.

> Is the below flow that you suggested?
> 1. of_parse_cb to check min_uV and max_uV, and fill in the fixed_uV in
> regulator_desc

No, the device should just know what voltage the fixed voltage
regulators in the device have.

> 2. provide the duummy set/get voltage  to make set_machine_constraints
> not return '-EINVAL'.

No, people just shouldn't be trying to set the voltage for fixed voltage
regulators.  If the regulators are configurable in hardware then provide
DT properties for whatever is configured (eg, resistors).
diff mbox series

Patch

diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig
index 5ef2306..76039d5 100644
--- a/drivers/regulator/Kconfig
+++ b/drivers/regulator/Kconfig
@@ -1047,6 +1047,16 @@  config REGULATOR_RT5033
 	  RT5033 PMIC. The device supports multiple regulators like
 	  current source, LDO and Buck.
 
+config REGULATOR_RT5120
+	tristate "Richtek RT5120 PMIC Regulators"
+	depends on MFD_RT5120
+	help
+	  This adds support for voltage regulator in Richtek RT5120 PMIC.
+	  It integrates 4 channels buck controller, 1 channel LDO, 1 EXTEN
+	  to control external power source. Only BUCK1 is adjustable from
+	  600mV to 1395mV, per step 6.250mV. The others are all fixed voltage
+	  by external hardware circuit.
+
 config REGULATOR_RT5190A
 	tristate "Richtek RT5190A PMIC"
 	depends on I2C
diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile
index 1b64ad5..32fce42 100644
--- a/drivers/regulator/Makefile
+++ b/drivers/regulator/Makefile
@@ -126,6 +126,7 @@  obj-$(CONFIG_REGULATOR_ROHM)	+= rohm-regulator.o
 obj-$(CONFIG_REGULATOR_RT4801)	+= rt4801-regulator.o
 obj-$(CONFIG_REGULATOR_RT4831)	+= rt4831-regulator.o
 obj-$(CONFIG_REGULATOR_RT5033)	+= rt5033-regulator.o
+obj-$(CONFIG_REGULATOR_RT5120)	+= rt5120-regulator.o
 obj-$(CONFIG_REGULATOR_RT5190A) += rt5190a-regulator.o
 obj-$(CONFIG_REGULATOR_RT6160)	+= rt6160-regulator.o
 obj-$(CONFIG_REGULATOR_RT6245)	+= rt6245-regulator.o
diff --git a/drivers/regulator/rt5120-regulator.c b/drivers/regulator/rt5120-regulator.c
new file mode 100644
index 00000000..c786564
--- /dev/null
+++ b/drivers/regulator/rt5120-regulator.c
@@ -0,0 +1,417 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <linux/bits.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/regulator/driver.h>
+#include <linux/regulator/machine.h>
+#include <linux/regulator/of_regulator.h>
+
+#define RT5120_REG_PGSTAT	0x03
+#define RT5120_REG_CH1VID	0x06
+#define RT5120_REG_CH1SLPVID	0x07
+#define RT5120_REG_ENABLE	0x08
+#define RT5120_REG_MODECTL	0x09
+#define RT5120_REG_UVOVPROT	0x0A
+#define RT5120_REG_SLPCTL	0x0C
+#define RT5120_REG_INTSTAT	0x1E
+#define RT5120_REG_DISCHG	0x1F
+
+#define RT5120_OUTPG_MASK(rid)	BIT(rid + 1)
+#define RT5120_OUTUV_MASK(rid)	BIT(rid + 9)
+#define RT5120_OUTOV_MASK(rid)	BIT(rid + 16)
+#define RT5120_CH1VID_MASK	GENMASK(6, 0)
+#define RT5120_RIDEN_MASK(rid)	BIT(rid + 1)
+#define RT5120_RADEN_MASK(rid)	BIT(rid)
+#define RT5120_FPWM_MASK(rid)	BIT(rid + 1)
+#define RT5120_UVHICCUP_MASK	BIT(1)
+#define RT5120_OVHICCUP_MASK	BIT(0)
+#define RT5120_HOTDIE_MASK	BIT(1)
+
+#define RT5120_BUCK1_MINUV	600000
+#define RT5120_BUCK1_MAXUV	1393750
+#define RT5120_BUCK1_STEPUV	6250
+#define RT5120_BUCK1_NUM_VOLT	0x80
+
+#define RT5120_AUTO_MODE	0
+#define RT5120_FPWM_MODE	1
+
+enum {
+	RT5120_REGULATOR_BUCK1 = 0,
+	RT5120_REGULATOR_BUCK2,
+	RT5120_REGULATOR_BUCK3,
+	RT5120_REGULATOR_BUCK4,
+	RT5120_REGULATOR_LDO,
+	RT5120_REGULATOR_EXTEN,
+	RT5120_MAX_REGULATOR
+};
+
+struct rt5120_priv {
+	struct device *dev;
+	struct regmap *regmap;
+	struct regulator_desc rdesc[RT5120_MAX_REGULATOR];
+};
+
+static int rt5120_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
+{
+	struct regmap *regmap = rdev_get_regmap(rdev);
+	int rid = rdev_get_id(rdev);
+	unsigned int mask = RT5120_FPWM_MASK(rid), val;
+
+	switch (mode) {
+	case REGULATOR_MODE_NORMAL:
+		val = 0;
+		break;
+	case REGULATOR_MODE_FAST:
+		val = RT5120_FPWM_MASK(rid);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return regmap_update_bits(regmap, RT5120_REG_MODECTL, mask, val);
+}
+
+static unsigned int rt5120_buck_get_mode(struct regulator_dev *rdev)
+{
+	struct regmap *regmap = rdev_get_regmap(rdev);
+	int ret, rid = rdev_get_id(rdev);
+	unsigned int val;
+
+	ret = regmap_read(regmap, RT5120_REG_MODECTL, &val);
+	if (ret)
+		return REGULATOR_MODE_INVALID;
+
+	if (val & RT5120_FPWM_MASK(rid))
+		return REGULATOR_MODE_FAST;
+
+	return REGULATOR_MODE_NORMAL;
+}
+
+static int rt5120_regulator_get_error_flags(struct regulator_dev *rdev,
+					    unsigned int *flags)
+{
+	struct regmap *regmap = rdev_get_regmap(rdev);
+	unsigned int stat, hd_stat, cur_flags = 0;
+	int rid = rdev_get_id(rdev), ret;
+
+	/*
+	 * reg 0x03/0x04/0x05 to indicate PG/UV/OV
+	 * use block read to descrease I/O xfer time
+	 */
+	ret = regmap_raw_read(regmap, RT5120_REG_PGSTAT, &stat, 3);
+	if (ret)
+		return ret;
+
+	ret = regmap_read(regmap, RT5120_REG_INTSTAT, &hd_stat);
+	if (ret)
+		return ret;
+
+	if (!(stat & RT5120_OUTPG_MASK(rid))) {
+		if (stat & RT5120_OUTUV_MASK(rid))
+			cur_flags |= REGULATOR_ERROR_UNDER_VOLTAGE;
+
+		if (stat & RT5120_OUTOV_MASK(rid))
+			cur_flags |= REGULATOR_ERROR_REGULATION_OUT;
+	}
+
+	if (hd_stat & RT5120_HOTDIE_MASK)
+		cur_flags |= REGULATOR_ERROR_OVER_TEMP;
+
+	*flags = cur_flags;
+	return 0;
+}
+
+static int rt5120_buck1_set_suspend_voltage(struct regulator_dev *rdev, int uV)
+{
+	struct regmap *regmap = rdev_get_regmap(rdev);
+	int sel;
+
+	if (uV < RT5120_BUCK1_MINUV || uV > RT5120_BUCK1_MAXUV)
+		return -EINVAL;
+
+	sel = (uV - RT5120_BUCK1_MINUV) / RT5120_BUCK1_STEPUV;
+	return regmap_write(regmap, RT5120_REG_CH1SLPVID, sel);
+}
+
+static int rt5120_regulator_set_suspend_enable(struct regulator_dev *rdev)
+{
+	struct regmap *regmap = rdev_get_regmap(rdev);
+	int rid = rdev_get_id(rdev);
+	unsigned int mask = RT5120_RIDEN_MASK(rid);
+
+	return regmap_update_bits(regmap, RT5120_REG_SLPCTL, mask, mask);
+}
+
+static int rt5120_regulator_set_suspend_disable(struct regulator_dev *rdev)
+{
+	struct regmap *regmap = rdev_get_regmap(rdev);
+	int rid = rdev_get_id(rdev);
+	unsigned int mask = RT5120_RIDEN_MASK(rid);
+
+	return regmap_update_bits(regmap, RT5120_REG_SLPCTL, mask, 0);
+}
+
+static const struct regulator_ops rt5120_buck1_ops = {
+	.enable = regulator_enable_regmap,
+	.disable = regulator_disable_regmap,
+	.is_enabled = regulator_is_enabled_regmap,
+	.list_voltage = regulator_list_voltage_linear,
+	.set_voltage_sel = regulator_set_voltage_sel_regmap,
+	.get_voltage_sel = regulator_get_voltage_sel_regmap,
+	.set_active_discharge = regulator_set_active_discharge_regmap,
+	.set_mode = rt5120_buck_set_mode,
+	.get_mode = rt5120_buck_get_mode,
+	.get_error_flags = rt5120_regulator_get_error_flags,
+	.set_suspend_voltage = rt5120_buck1_set_suspend_voltage,
+	.set_suspend_enable = rt5120_regulator_set_suspend_enable,
+	.set_suspend_disable = rt5120_regulator_set_suspend_disable,
+};
+
+static const struct regulator_ops rt5120_buck234_ops = {
+	.enable = regulator_enable_regmap,
+	.disable = regulator_disable_regmap,
+	.is_enabled = regulator_is_enabled_regmap,
+	.set_active_discharge = regulator_set_active_discharge_regmap,
+	.set_mode = rt5120_buck_set_mode,
+	.get_mode = rt5120_buck_get_mode,
+	.get_error_flags = rt5120_regulator_get_error_flags,
+	.set_suspend_enable = rt5120_regulator_set_suspend_enable,
+	.set_suspend_disable = rt5120_regulator_set_suspend_disable,
+};
+
+static const struct regulator_ops rt5120_ldo_ops = {
+	.enable = regulator_enable_regmap,
+	.disable = regulator_disable_regmap,
+	.is_enabled = regulator_is_enabled_regmap,
+	.set_active_discharge = regulator_set_active_discharge_regmap,
+	.get_error_flags = rt5120_regulator_get_error_flags,
+	.set_suspend_enable = rt5120_regulator_set_suspend_enable,
+	.set_suspend_disable = rt5120_regulator_set_suspend_disable,
+};
+
+static const struct regulator_ops rt5120_exten_ops = {
+	.enable = regulator_enable_regmap,
+	.disable = regulator_disable_regmap,
+	.is_enabled = regulator_is_enabled_regmap,
+	.set_suspend_enable = rt5120_regulator_set_suspend_enable,
+	.set_suspend_disable = rt5120_regulator_set_suspend_disable,
+};
+
+static unsigned int rt5120_buck_of_map_mode(unsigned int mode)
+{
+	switch (mode) {
+	case RT5120_AUTO_MODE:
+		return REGULATOR_MODE_NORMAL;
+	case RT5120_FPWM_MODE:
+		return REGULATOR_MODE_FAST;
+	default:
+		return REGULATOR_MODE_INVALID;
+	}
+}
+
+static void rt5120_fillin_regulator_desc(struct regulator_desc *desc, int rid)
+{
+	static const char * const name[] = { "buck1", "buck2", "buck3", "buck4",
+					     "ldo", "exten" };
+	static const char * const sname[] = { "vin1", "vin2", "vin3", "vin4",
+					      "vinldo", NULL };
+
+	desc->name = name[rid];
+	desc->supply_name = sname[rid];
+	desc->owner = THIS_MODULE;
+	desc->type = REGULATOR_VOLTAGE;
+	desc->id = rid;
+	desc->enable_reg = RT5120_REG_ENABLE;
+	desc->enable_mask = RT5120_RIDEN_MASK(rid);
+	desc->active_discharge_reg = RT5120_REG_DISCHG;
+	desc->active_discharge_mask = RT5120_RADEN_MASK(rid);
+	desc->active_discharge_on = RT5120_RADEN_MASK(rid);
+
+	switch (rid) {
+	case RT5120_REGULATOR_BUCK1:
+		desc->of_map_mode = rt5120_buck_of_map_mode;
+		desc->min_uV = RT5120_BUCK1_MINUV;
+		desc->uV_step = RT5120_BUCK1_STEPUV;
+		desc->n_voltages = RT5120_BUCK1_NUM_VOLT;
+		desc->vsel_reg = RT5120_REG_CH1VID,
+		desc->vsel_mask = RT5120_CH1VID_MASK,
+		desc->ops = &rt5120_buck1_ops;
+		break;
+	case RT5120_REGULATOR_BUCK2 ... RT5120_REGULATOR_BUCK4:
+		desc->of_map_mode = rt5120_buck_of_map_mode;
+		desc->n_voltages = 1;
+		desc->ops = &rt5120_buck234_ops;
+		break;
+	case RT5120_REGULATOR_LDO:
+		desc->n_voltages = 1;
+		desc->ops = &rt5120_ldo_ops;
+		break;
+	default:
+		desc->n_voltages = 1;
+		desc->ops = &rt5120_exten_ops;
+		break;
+	}
+}
+
+static int rt5120_of_parse_cb(struct rt5120_priv *priv, int rid,
+			      struct of_regulator_match *match)
+{
+	struct regulator_desc *desc = priv->rdesc + rid;
+	struct regulator_init_data *init_data = match->init_data;
+
+	if (!init_data || rid == RT5120_REGULATOR_BUCK1)
+		return 0;
+
+	if (init_data->constraints.min_uV != init_data->constraints.max_uV) {
+		dev_err(priv->dev, "Variable voltage for fixed regulator\n");
+		return -EINVAL;
+	}
+
+	desc->fixed_uV = init_data->constraints.min_uV;
+	init_data->constraints.apply_uV = 0;
+	return 0;
+}
+
+static struct of_regulator_match rt5120_regu_match[RT5120_MAX_REGULATOR] = {
+	[RT5120_REGULATOR_BUCK1] = { .name = "buck1", },
+	[RT5120_REGULATOR_BUCK2] = { .name = "buck2", },
+	[RT5120_REGULATOR_BUCK3] = { .name = "buck3", },
+	[RT5120_REGULATOR_BUCK4] = { .name = "buck4", },
+	[RT5120_REGULATOR_LDO] = { .name = "ldo", },
+	[RT5120_REGULATOR_EXTEN] = { .name = "exten", }
+};
+
+static int rt5120_parse_regulator_dt_data(struct rt5120_priv *priv)
+{
+	struct device *dev = priv->dev->parent;
+	struct device_node *reg_node;
+	int i, ret;
+
+	for (i = 0; i < RT5120_MAX_REGULATOR; i++) {
+		rt5120_fillin_regulator_desc(priv->rdesc + i, i);
+
+		rt5120_regu_match[i].desc = priv->rdesc + i;
+	}
+
+	reg_node = of_get_child_by_name(dev->of_node, "regulators");
+	if (!reg_node) {
+		dev_err(priv->dev, "Couldn't find 'regulators' node\n");
+		return -ENODEV;
+	}
+
+	ret = of_regulator_match(priv->dev, reg_node, rt5120_regu_match,
+				 ARRAY_SIZE(rt5120_regu_match));
+
+	of_node_put(reg_node);
+
+	if (ret < 0) {
+		dev_err(priv->dev,
+			"Error parsing regulator init data (%d)\n", ret);
+		return ret;
+	}
+
+	for (i = 0; i < RT5120_MAX_REGULATOR; i++) {
+		ret = rt5120_of_parse_cb(priv, i, rt5120_regu_match + i);
+		if (ret) {
+			dev_err(priv->dev, "Failed in [%d] of_passe_cb\n", i);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+static int rt5120_device_property_init(struct rt5120_priv *priv)
+{
+	struct device *dev = priv->dev->parent;
+	bool prot_enable;
+	unsigned int prot_enable_val = 0;
+
+	/* Assign UV/OV HW protection behavior */
+	prot_enable = device_property_read_bool(dev,
+					"richtek,enable-undervolt-hiccup");
+	if (prot_enable)
+		prot_enable_val |= RT5120_UVHICCUP_MASK;
+
+	prot_enable = device_property_read_bool(dev,
+					"richtek,enable-overvolt-hiccup");
+	if (prot_enable)
+		prot_enable_val |= RT5120_OVHICCUP_MASK;
+
+	return regmap_update_bits(priv->regmap, RT5120_REG_UVOVPROT,
+				  RT5120_UVHICCUP_MASK | RT5120_OVHICCUP_MASK,
+				  prot_enable_val);
+}
+
+static int rt5120_regulator_probe(struct platform_device *pdev)
+{
+	struct rt5120_priv *priv;
+	struct regulator_dev *rdev;
+	struct regulator_config config = {};
+	int i, ret;
+
+	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->dev = &pdev->dev;
+
+	priv->regmap = dev_get_regmap(pdev->dev.parent, NULL);
+	if (!priv->regmap) {
+		dev_err(&pdev->dev, "Failed to init regmap\n");
+		return -ENODEV;
+	}
+
+	ret = rt5120_device_property_init(priv);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to do property init\n");
+		return ret;
+	}
+
+	ret = rt5120_parse_regulator_dt_data(priv);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to parse dt data\n");
+		return ret;
+	}
+
+	config.dev = &pdev->dev;
+	config.regmap = priv->regmap;
+
+	for (i = 0; i < RT5120_MAX_REGULATOR; i++) {
+		config.of_node = rt5120_regu_match[i].of_node;
+		config.init_data = rt5120_regu_match[i].init_data;
+
+		rdev = devm_regulator_register(&pdev->dev, priv->rdesc + i,
+					       &config);
+		if (IS_ERR(rdev)) {
+			dev_err(&pdev->dev,
+				"Failed to register regulator [%d]\n", i);
+			return PTR_ERR(rdev);
+		}
+	}
+
+	return 0;
+}
+
+static const struct platform_device_id rt5120_regulator_dev_table[] = {
+	{ "rt5120-regulator", 0 },
+	{}
+};
+MODULE_DEVICE_TABLE(platform, rt5120_regulator_dev_table);
+
+static struct platform_driver rt5120_regulator_driver = {
+	.driver = {
+		.name = "rt5120-regulator",
+	},
+	.id_table = rt5120_regulator_dev_table,
+	.probe = rt5120_regulator_probe,
+};
+module_platform_driver(rt5120_regulator_driver);
+
+MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
+MODULE_DESCRIPTION("Richtek RT5120 regulator driver");
+MODULE_LICENSE("GPL v2");