diff mbox series

[v5,05/10] power: supply: rt5033_charger: Add RT5033 charger device driver

Message ID 20230514123130.41172-6-jahau@rocketmail.com
State Superseded
Headers show
Series [v5,01/10] mfd: rt5033: Drop rt5033-battery sub-device | expand

Commit Message

Jakob Hauser May 14, 2023, 12:31 p.m. UTC
This patch adds device driver of Richtek RT5033 PMIC. The driver supports
switching charger. rt5033 charger provides three charging modes. The charging
modes are pre-charge mode, fast charge mode and constant voltage mode. They
vary in charge rate, the charge parameters can be controlled by i2c interface.

Cc: Beomho Seo <beomho.seo@samsung.com>
Cc: Chanwoo Choi <cw00.choi@samsung.com>
Tested-by: Raymond Hackley <raymondhackley@protonmail.com>
Signed-off-by: Jakob Hauser <jahau@rocketmail.com>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/power/supply/Kconfig          |   8 +
 drivers/power/supply/Makefile         |   1 +
 drivers/power/supply/rt5033_charger.c | 472 ++++++++++++++++++++++++++
 include/linux/mfd/rt5033.h            |  16 -
 4 files changed, 481 insertions(+), 16 deletions(-)
 create mode 100644 drivers/power/supply/rt5033_charger.c

Comments

Jakob Hauser May 14, 2023, 12:54 p.m. UTC | #1
Hi Sebastian,

On 14.05.23 14:31, Jakob Hauser wrote:
...
> +static struct rt5033_charger_data *rt5033_charger_dt_init(
> +						struct rt5033_charger *charger)
> +{
> +	struct rt5033_charger_data *chg;
> +	struct power_supply_battery_info *info;
> +	int ret;
> +
> +	chg = devm_kzalloc(charger->dev, sizeof(*chg), GFP_KERNEL);
> +	if (!chg)
> +		return ERR_PTR(-ENOMEM);
> +
> +	ret = power_supply_get_battery_info(charger->psy, &info);
> +	if (ret)
> +		return ERR_PTR(dev_err_probe(charger->dev, -EINVAL,
> +			       "missing battery info\n"));

Here you suggested to use: "info = charger->psy->battery_info;". This 
didn't work.

The supply type of the rt5033-charger is set as POWER_SUPPLY_TYPE_USB. 
The one of rt5033-battery is POWER_SUPPLY_TYPE_BATTERY. Which makes 
sense because if both of them are POWER_SUPPLY_TYPE_BATTERY, userspace 
sees two batteries reported, one of which with 0% capacity (the charger 
doesn't report capacity).

The ...->psy->battery_info, however, gets populated only for a power 
supply device that is supply type POWER_SUPPLY_TYPE_BATTERY [1].

[1] 
https://github.com/torvalds/linux/blob/v6.4-rc1/drivers/power/supply/power_supply_core.c#L1390-L1399

> +
> +	/* Assign data. Validity will be checked in the init functions. */
> +	chg->pre_uamp = info->precharge_current_ua;
> +	chg->fast_uamp = info->constant_charge_current_max_ua;
> +	chg->eoc_uamp = info->charge_term_current_ua;
> +	chg->pre_uvolt = info->precharge_voltage_max_uv;
> +	chg->const_uvolt = info->constant_charge_voltage_max_uv;
> +
> +	return chg;
> +}
...

Kind regards,
Jakob
Christophe JAILLET May 14, 2023, 2:31 p.m. UTC | #2
Le 14/05/2023 à 14:31, Jakob Hauser a écrit :
> This patch adds device driver of Richtek RT5033 PMIC. The driver supports
> switching charger. rt5033 charger provides three charging modes. The charging
> modes are pre-charge mode, fast charge mode and constant voltage mode. They
> vary in charge rate, the charge parameters can be controlled by i2c interface.
> 
> Cc: Beomho Seo <beomho.seo-Sze3O3UU22JBDgjK7y7TUQ@public.gmane.org>
> Cc: Chanwoo Choi <cw00.choi-Sze3O3UU22JBDgjK7y7TUQ@public.gmane.org>
> Tested-by: Raymond Hackley <raymondhackley-g/b1ySJe57IN+BqQ9rBEUg@public.gmane.org>
> Signed-off-by: Jakob Hauser <jahau-ur4TIblo6goN+BqQ9rBEUg@public.gmane.org>
> Reviewed-by: Linus Walleij <linus.walleij-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
> ---
>   drivers/power/supply/Kconfig          |   8 +
>   drivers/power/supply/Makefile         |   1 +
>   drivers/power/supply/rt5033_charger.c | 472 ++++++++++++++++++++++++++
>   include/linux/mfd/rt5033.h            |  16 -
>   4 files changed, 481 insertions(+), 16 deletions(-)
>   create mode 100644 drivers/power/supply/rt5033_charger.c
> 

[...]

> +static int rt5033_charger_probe(struct platform_device *pdev)
> +{
> +	struct rt5033_charger *charger;
> +	struct power_supply_config psy_cfg = {};
> +	int ret;
> +
> +	charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
> +	if (!charger)
> +		return -ENOMEM;
> +
> +	platform_set_drvdata(pdev, charger);
> +	charger->dev = &pdev->dev;
> +	charger->regmap = dev_get_regmap(pdev->dev.parent, NULL);
> +
> +	psy_cfg.of_node = pdev->dev.of_node;
> +	psy_cfg.drv_data = charger;
> +
> +	charger->psy = devm_power_supply_register(&pdev->dev,
> +						  &rt5033_charger_desc,
> +						  &psy_cfg);
> +	if (IS_ERR(charger->psy))
> +		return dev_err_probe(&pdev->dev, PTR_ERR(charger->psy),
> +				     "Failed to register power supply\n");
> +
> +	charger->chg = rt5033_charger_dt_init(charger);
> +	if (IS_ERR_OR_NULL(charger->chg))

Hi,

Nit: charger->chg can't be NULL.

> +		return -ENODEV;

Why bother returning specific error code in rt5033_charger_dt_init() if 
they are eaten here.

return PTR_ERR(charger->chg)?


CJ

> +
> +	ret = rt5033_charger_reg_init(charger);
> +	if (ret)
> +		return ret;
> +
> +	return 0;
> +}

[...]
Jakob Hauser May 14, 2023, 5:03 p.m. UTC | #3
Hi Christophe, Hi all,

On 14.05.23 16:31, Christophe JAILLET wrote:
> Le 14/05/2023 à 14:31, Jakob Hauser a écrit :

...

>> +static int rt5033_charger_probe(struct platform_device *pdev)
>> +{
>> +    struct rt5033_charger *charger;
>> +    struct power_supply_config psy_cfg = {};
>> +    int ret;
>> +
>> +    charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
>> +    if (!charger)
>> +        return -ENOMEM;
>> +
>> +    platform_set_drvdata(pdev, charger);
>> +    charger->dev = &pdev->dev;
>> +    charger->regmap = dev_get_regmap(pdev->dev.parent, NULL);
>> +
>> +    psy_cfg.of_node = pdev->dev.of_node;
>> +    psy_cfg.drv_data = charger;
>> +
>> +    charger->psy = devm_power_supply_register(&pdev->dev,
>> +                          &rt5033_charger_desc,
>> +                          &psy_cfg);
>> +    if (IS_ERR(charger->psy))
>> +        return dev_err_probe(&pdev->dev, PTR_ERR(charger->psy),
>> +                     "Failed to register power supply\n");
>> +
>> +    charger->chg = rt5033_charger_dt_init(charger);
>> +    if (IS_ERR_OR_NULL(charger->chg))
> 
> Hi,
> 
> Nit: charger->chg can't be NULL.
> 
>> +        return -ENODEV;
> 
> Why bother returning specific error code in rt5033_charger_dt_init() if 
> they are eaten here.
> 
> return PTR_ERR(charger->chg)?
> 

Thanks for the heads-up.

...

Writing towards the list:

The way it is done in the current patchset is taken from the original 
patchset of March 2015 [2]. I kept the original as far as possible.

By now I'm not happy with the way of initializing "struct 
rt5033_charger_data". I realized this in the course of the review. As I 
didn't want to disturb the review with this, I had planned a small 
clean-up patch after this review is finished.

The cause of the complicated handling of "struct rt5033_charger_data" 
lies inside of the "struct rt5033_charger". There the "struct 
rt5033_charger_data" is initialized as pointer *chg.

The clean-up would be:

  - Inside of "struct rt5033_charger" change the
    "struct rt5033_charger_data" to non-pointer "chg". It is then
    initialized right away.

       struct rt5033_charger_data      chg;

  - Change function rt5033_charger_dt_init() from type
    "struct rt5033_charger_data" to type "int".

       static int rt5033_charger_dt_init(struct rt5033_charger *charger)

  - In the probe function, call the function rt5033_charger_dt_init() in
    the same way like e.g. the following rt5033_charger_reg_init():

       ret = rt5033_charger_dt_init(charger);
               if (ret)
                       return ret;

  - Within function rt5033_charger_dt_init() and all other functions
    using the charger data, get the address of the already-initialized
    struct &charger->chg.

       struct rt5033_charger_data *chg = &charger->chg;

This would also solve the issue reported by Christophe because the 
errors inside function rt5033_charger_dt_init() would be passed to the 
probe function by the "ret =" and being returned there with "return ret".

I'm not sure how to handle this now. I would prefer to get the review of 
this patchset finished and send a clean-up patch afterwards.

[2] 
https://lore.kernel.org/lkml/1425864191-4121-1-git-send-email-beomho.seo@samsung.com/T/#u

Kind regards,
Jakob
Sebastian Reichel May 14, 2023, 10:51 p.m. UTC | #4
Hi,

On Sun, May 14, 2023 at 07:03:03PM +0200, Jakob Hauser wrote:
> Hi Christophe, Hi all,
> 
> On 14.05.23 16:31, Christophe JAILLET wrote:
> > Le 14/05/2023 à 14:31, Jakob Hauser a écrit :
> 
> ...
> 
> > > +static int rt5033_charger_probe(struct platform_device *pdev)
> > > +{
> > > +    struct rt5033_charger *charger;
> > > +    struct power_supply_config psy_cfg = {};
> > > +    int ret;
> > > +
> > > +    charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
> > > +    if (!charger)
> > > +        return -ENOMEM;
> > > +
> > > +    platform_set_drvdata(pdev, charger);
> > > +    charger->dev = &pdev->dev;
> > > +    charger->regmap = dev_get_regmap(pdev->dev.parent, NULL);
> > > +
> > > +    psy_cfg.of_node = pdev->dev.of_node;
> > > +    psy_cfg.drv_data = charger;
> > > +
> > > +    charger->psy = devm_power_supply_register(&pdev->dev,
> > > +                          &rt5033_charger_desc,
> > > +                          &psy_cfg);
> > > +    if (IS_ERR(charger->psy))
> > > +        return dev_err_probe(&pdev->dev, PTR_ERR(charger->psy),
> > > +                     "Failed to register power supply\n");
> > > +
> > > +    charger->chg = rt5033_charger_dt_init(charger);
> > > +    if (IS_ERR_OR_NULL(charger->chg))
> > 
> > Hi,
> > 
> > Nit: charger->chg can't be NULL.
> > 
> > > +        return -ENODEV;
> > 
> > Why bother returning specific error code in rt5033_charger_dt_init() if
> > they are eaten here.
> > 
> > return PTR_ERR(charger->chg)?
> > 
> 
> Thanks for the heads-up.
> 
> ...
> 
> Writing towards the list:
> 
> The way it is done in the current patchset is taken from the original
> patchset of March 2015 [2]. I kept the original as far as possible.
> 
> By now I'm not happy with the way of initializing "struct
> rt5033_charger_data". I realized this in the course of the review. As I
> didn't want to disturb the review with this, I had planned a small clean-up
> patch after this review is finished.
> 
> The cause of the complicated handling of "struct rt5033_charger_data" lies
> inside of the "struct rt5033_charger". There the "struct
> rt5033_charger_data" is initialized as pointer *chg.
> 
> The clean-up would be:
> 
>  - Inside of "struct rt5033_charger" change the
>    "struct rt5033_charger_data" to non-pointer "chg". It is then
>    initialized right away.
> 
>       struct rt5033_charger_data      chg;
> 
>  - Change function rt5033_charger_dt_init() from type
>    "struct rt5033_charger_data" to type "int".
> 
>       static int rt5033_charger_dt_init(struct rt5033_charger *charger)
> 
>  - In the probe function, call the function rt5033_charger_dt_init() in
>    the same way like e.g. the following rt5033_charger_reg_init():
> 
>       ret = rt5033_charger_dt_init(charger);
>               if (ret)
>                       return ret;
> 
>  - Within function rt5033_charger_dt_init() and all other functions
>    using the charger data, get the address of the already-initialized
>    struct &charger->chg.
> 
>       struct rt5033_charger_data *chg = &charger->chg;
> 
> This would also solve the issue reported by Christophe because the errors
> inside function rt5033_charger_dt_init() would be passed to the probe
> function by the "ret =" and being returned there with "return ret".
> 
> I'm not sure how to handle this now. I would prefer to get the review of
> this patchset finished and send a clean-up patch afterwards.
> 
> [2] https://lore.kernel.org/lkml/1425864191-4121-1-git-send-email-beomho.seo@samsung.com/T/#u

Sounds sensible, until then please use 'return PTR_ERR(charger->chg)'
as suggested by Christophe. With this fixed:

Acked-by: Sebastian Reichel <sebastian.reichel@collabora.com>

-- Sebastian
diff mbox series

Patch

diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
index c78be9f322e6..ea11797670ca 100644
--- a/drivers/power/supply/Kconfig
+++ b/drivers/power/supply/Kconfig
@@ -766,6 +766,14 @@  config BATTERY_RT5033
 	  The fuelgauge calculates and determines the battery state of charge
 	  according to battery open circuit voltage.
 
+config CHARGER_RT5033
+	tristate "RT5033 battery charger support"
+	depends on MFD_RT5033
+	help
+	  This adds support for battery charger in Richtek RT5033 PMIC.
+	  The device supports pre-charge mode, fast charge mode and
+	  constant voltage mode.
+
 config CHARGER_RT9455
 	tristate "Richtek RT9455 battery charger driver"
 	depends on I2C
diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
index 4adbfba02d05..dfc624bbcf1d 100644
--- a/drivers/power/supply/Makefile
+++ b/drivers/power/supply/Makefile
@@ -54,6 +54,7 @@  obj-$(CONFIG_BATTERY_MAX17040)	+= max17040_battery.o
 obj-$(CONFIG_BATTERY_MAX17042)	+= max17042_battery.o
 obj-$(CONFIG_BATTERY_MAX1721X)	+= max1721x_battery.o
 obj-$(CONFIG_BATTERY_RT5033)	+= rt5033_battery.o
+obj-$(CONFIG_CHARGER_RT5033)	+= rt5033_charger.o
 obj-$(CONFIG_CHARGER_RT9455)	+= rt9455_charger.o
 obj-$(CONFIG_CHARGER_RT9467)	+= rt9467-charger.o
 obj-$(CONFIG_CHARGER_RT9471)	+= rt9471.o
diff --git a/drivers/power/supply/rt5033_charger.c b/drivers/power/supply/rt5033_charger.c
new file mode 100644
index 000000000000..1aa346dd0679
--- /dev/null
+++ b/drivers/power/supply/rt5033_charger.c
@@ -0,0 +1,472 @@ 
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Battery charger driver for RT5033
+ *
+ * Copyright (C) 2014 Samsung Electronics, Co., Ltd.
+ * Author: Beomho Seo <beomho.seo@samsung.com>
+ */
+
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/power_supply.h>
+#include <linux/regmap.h>
+#include <linux/mfd/rt5033-private.h>
+
+struct rt5033_charger_data {
+	unsigned int pre_uamp;
+	unsigned int pre_uvolt;
+	unsigned int const_uvolt;
+	unsigned int eoc_uamp;
+	unsigned int fast_uamp;
+};
+
+struct rt5033_charger {
+	struct device			*dev;
+	struct regmap			*regmap;
+	struct power_supply		*psy;
+	struct rt5033_charger_data	*chg;
+};
+
+static int rt5033_get_charger_state(struct rt5033_charger *charger)
+{
+	struct regmap *regmap = charger->regmap;
+	unsigned int reg_data;
+	int state;
+
+	if (!regmap)
+		return POWER_SUPPLY_STATUS_UNKNOWN;
+
+	regmap_read(regmap, RT5033_REG_CHG_STAT, &reg_data);
+
+	switch (reg_data & RT5033_CHG_STAT_MASK) {
+	case RT5033_CHG_STAT_DISCHARGING:
+		state = POWER_SUPPLY_STATUS_DISCHARGING;
+		break;
+	case RT5033_CHG_STAT_CHARGING:
+		state = POWER_SUPPLY_STATUS_CHARGING;
+		break;
+	case RT5033_CHG_STAT_FULL:
+		state = POWER_SUPPLY_STATUS_FULL;
+		break;
+	case RT5033_CHG_STAT_NOT_CHARGING:
+		state = POWER_SUPPLY_STATUS_NOT_CHARGING;
+		break;
+	default:
+		state = POWER_SUPPLY_STATUS_UNKNOWN;
+	}
+
+	return state;
+}
+
+static int rt5033_get_charger_type(struct rt5033_charger *charger)
+{
+	struct regmap *regmap = charger->regmap;
+	unsigned int reg_data;
+	int state;
+
+	regmap_read(regmap, RT5033_REG_CHG_STAT, &reg_data);
+
+	switch (reg_data & RT5033_CHG_STAT_TYPE_MASK) {
+	case RT5033_CHG_STAT_TYPE_FAST:
+		state = POWER_SUPPLY_CHARGE_TYPE_FAST;
+		break;
+	case RT5033_CHG_STAT_TYPE_PRE:
+		state = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
+		break;
+	default:
+		state = POWER_SUPPLY_CHARGE_TYPE_NONE;
+	}
+
+	return state;
+}
+
+static int rt5033_get_charger_current_limit(struct rt5033_charger *charger)
+{
+	struct regmap *regmap = charger->regmap;
+	unsigned int state, reg_data, data;
+
+	regmap_read(regmap, RT5033_REG_CHG_CTRL5, &reg_data);
+
+	state = (reg_data & RT5033_CHGCTRL5_ICHG_MASK)
+		 >> RT5033_CHGCTRL5_ICHG_SHIFT;
+
+	data = RT5033_CHARGER_FAST_CURRENT_MIN +
+		RT5033_CHARGER_FAST_CURRENT_STEP_NUM * state;
+
+	return data;
+}
+
+static int rt5033_get_charger_const_voltage(struct rt5033_charger *charger)
+{
+	struct regmap *regmap = charger->regmap;
+	unsigned int state, reg_data, data;
+
+	regmap_read(regmap, RT5033_REG_CHG_CTRL2, &reg_data);
+
+	state = (reg_data & RT5033_CHGCTRL2_CV_MASK)
+		 >> RT5033_CHGCTRL2_CV_SHIFT;
+
+	data = RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN +
+		RT5033_CHARGER_CONST_VOLTAGE_STEP_NUM * state;
+
+	return data;
+}
+
+static inline int rt5033_init_const_charge(struct rt5033_charger *charger)
+{
+	struct rt5033_charger_data *chg = charger->chg;
+	int ret;
+	unsigned int val;
+	u8 reg_data;
+
+	/* Set constant voltage mode */
+	if (chg->const_uvolt < RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN ||
+	    chg->const_uvolt > RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MAX) {
+		dev_err(charger->dev,
+			"Value 'constant-charge-voltage-max-microvolt' out of range\n");
+		return -EINVAL;
+	}
+
+	if (chg->const_uvolt == RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN)
+		reg_data = 0x00;
+	else if (chg->const_uvolt == RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MAX)
+		reg_data = RT5033_CV_MAX_VOLTAGE;
+	else {
+		val = chg->const_uvolt;
+		val -= RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN;
+		val /= RT5033_CHARGER_CONST_VOLTAGE_STEP_NUM;
+		reg_data = val;
+	}
+
+	ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL2,
+			RT5033_CHGCTRL2_CV_MASK,
+			reg_data << RT5033_CHGCTRL2_CV_SHIFT);
+	if (ret) {
+		dev_err(charger->dev, "Failed regmap update\n");
+		return -EINVAL;
+	}
+
+	/* Set end of charge current */
+	if (chg->eoc_uamp < RT5033_CHARGER_EOC_MIN ||
+	    chg->eoc_uamp > RT5033_CHARGER_EOC_MAX) {
+		dev_err(charger->dev,
+			"Value 'charge-term-current-microamp' out of range\n");
+		return -EINVAL;
+	}
+
+	if (chg->eoc_uamp == RT5033_CHARGER_EOC_MIN)
+		reg_data = 0x01;
+	else if (chg->eoc_uamp == RT5033_CHARGER_EOC_MAX)
+		reg_data = 0x07;
+	else {
+		val = chg->eoc_uamp;
+		if (val < RT5033_CHARGER_EOC_REF) {
+			val -= RT5033_CHARGER_EOC_MIN;
+			val /= RT5033_CHARGER_EOC_STEP_NUM1;
+			reg_data = 0x01 + val;
+		} else if (val > RT5033_CHARGER_EOC_REF) {
+			val -= RT5033_CHARGER_EOC_REF;
+			val /= RT5033_CHARGER_EOC_STEP_NUM2;
+			reg_data = 0x04 + val;
+		} else {
+			reg_data = 0x04;
+		}
+	}
+
+	ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL4,
+			RT5033_CHGCTRL4_EOC_MASK, reg_data);
+	if (ret) {
+		dev_err(charger->dev, "Failed regmap update\n");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static inline int rt5033_init_fast_charge(struct rt5033_charger *charger)
+{
+	struct rt5033_charger_data *chg = charger->chg;
+	int ret;
+	unsigned int val;
+	u8 reg_data;
+
+	/* Set limit input current */
+	ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL1,
+			RT5033_CHGCTRL1_IAICR_MASK, RT5033_AICR_2000_MODE);
+	if (ret) {
+		dev_err(charger->dev, "Failed regmap update\n");
+		return -EINVAL;
+	}
+
+	/* Set fast-charge mode charging current */
+	if (chg->fast_uamp < RT5033_CHARGER_FAST_CURRENT_MIN ||
+	    chg->fast_uamp > RT5033_CHARGER_FAST_CURRENT_MAX) {
+		dev_err(charger->dev,
+			"Value 'constant-charge-current-max-microamp' out of range\n");
+		return -EINVAL;
+	}
+
+	if (chg->fast_uamp == RT5033_CHARGER_FAST_CURRENT_MIN)
+		reg_data = 0x00;
+	else if (chg->fast_uamp == RT5033_CHARGER_FAST_CURRENT_MAX)
+		reg_data = RT5033_CHG_MAX_CURRENT;
+	else {
+		val = chg->fast_uamp;
+		val -= RT5033_CHARGER_FAST_CURRENT_MIN;
+		val /= RT5033_CHARGER_FAST_CURRENT_STEP_NUM;
+		reg_data = val;
+	}
+
+	ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL5,
+			RT5033_CHGCTRL5_ICHG_MASK,
+			reg_data << RT5033_CHGCTRL5_ICHG_SHIFT);
+	if (ret) {
+		dev_err(charger->dev, "Failed regmap update\n");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static inline int rt5033_init_pre_charge(struct rt5033_charger *charger)
+{
+	struct rt5033_charger_data *chg = charger->chg;
+	int ret;
+	unsigned int val;
+	u8 reg_data;
+
+	/* Set pre-charge threshold voltage */
+	if (chg->pre_uvolt < RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MIN ||
+	    chg->pre_uvolt > RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MAX) {
+		dev_err(charger->dev,
+			"Value 'precharge-upper-limit-microvolt' out of range\n");
+		return -EINVAL;
+	}
+
+	if (chg->pre_uvolt == RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MIN)
+		reg_data = 0x00;
+	else if (chg->pre_uvolt == RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MAX)
+		reg_data = 0x0f;
+	else {
+		val = chg->pre_uvolt;
+		val -= RT5033_CHARGER_PRE_THRESHOLD_LIMIT_MIN;
+		val /= RT5033_CHARGER_PRE_THRESHOLD_STEP_NUM;
+		reg_data = val;
+	}
+
+	ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL5,
+			RT5033_CHGCTRL5_VPREC_MASK, reg_data);
+	if (ret) {
+		dev_err(charger->dev, "Failed regmap update\n");
+		return -EINVAL;
+	}
+
+	/* Set pre-charge mode charging current */
+	if (chg->pre_uamp < RT5033_CHARGER_PRE_CURRENT_LIMIT_MIN ||
+	    chg->pre_uamp > RT5033_CHARGER_PRE_CURRENT_LIMIT_MAX) {
+		dev_err(charger->dev,
+			"Value 'precharge-current-microamp' out of range\n");
+		return -EINVAL;
+	}
+
+	if (chg->pre_uamp == RT5033_CHARGER_PRE_CURRENT_LIMIT_MIN)
+		reg_data = 0x00;
+	else if (chg->pre_uamp == RT5033_CHARGER_PRE_CURRENT_LIMIT_MAX)
+		reg_data = RT5033_CHG_MAX_PRE_CURRENT;
+	else {
+		val = chg->pre_uamp;
+		val -= RT5033_CHARGER_PRE_CURRENT_LIMIT_MIN;
+		val /= RT5033_CHARGER_PRE_CURRENT_STEP_NUM;
+		reg_data = val;
+	}
+
+	ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL4,
+			RT5033_CHGCTRL4_IPREC_MASK,
+			reg_data << RT5033_CHGCTRL4_IPREC_SHIFT);
+	if (ret) {
+		dev_err(charger->dev, "Failed regmap update\n");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int rt5033_charger_reg_init(struct rt5033_charger *charger)
+{
+	int ret = 0;
+
+	/* Enable charging termination */
+	ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL1,
+			RT5033_CHGCTRL1_TE_EN_MASK, RT5033_TE_ENABLE);
+	if (ret) {
+		dev_err(charger->dev, "Failed to enable charging termination.\n");
+		return -EINVAL;
+	}
+
+	/*
+	 * Disable minimum input voltage regulation (MIVR), this improves
+	 * the charging performance.
+	 */
+	ret = regmap_update_bits(charger->regmap, RT5033_REG_CHG_CTRL4,
+			RT5033_CHGCTRL4_MIVR_MASK, RT5033_CHARGER_MIVR_DISABLE);
+	if (ret) {
+		dev_err(charger->dev, "Failed to disable MIVR.\n");
+		return -EINVAL;
+	}
+
+	ret = rt5033_init_pre_charge(charger);
+	if (ret)
+		return ret;
+
+	ret = rt5033_init_fast_charge(charger);
+	if (ret)
+		return ret;
+
+	ret = rt5033_init_const_charge(charger);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static enum power_supply_property rt5033_charger_props[] = {
+	POWER_SUPPLY_PROP_STATUS,
+	POWER_SUPPLY_PROP_CHARGE_TYPE,
+	POWER_SUPPLY_PROP_CURRENT_MAX,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
+	POWER_SUPPLY_PROP_MODEL_NAME,
+	POWER_SUPPLY_PROP_MANUFACTURER,
+	POWER_SUPPLY_PROP_ONLINE,
+};
+
+static int rt5033_charger_get_property(struct power_supply *psy,
+			enum power_supply_property psp,
+			union power_supply_propval *val)
+{
+	struct rt5033_charger *charger = power_supply_get_drvdata(psy);
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_STATUS:
+		val->intval = rt5033_get_charger_state(charger);
+		break;
+	case POWER_SUPPLY_PROP_CHARGE_TYPE:
+		val->intval = rt5033_get_charger_type(charger);
+		break;
+	case POWER_SUPPLY_PROP_CURRENT_MAX:
+		val->intval = rt5033_get_charger_current_limit(charger);
+		break;
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+		val->intval = rt5033_get_charger_const_voltage(charger);
+		break;
+	case POWER_SUPPLY_PROP_MODEL_NAME:
+		val->strval = RT5033_CHARGER_MODEL;
+		break;
+	case POWER_SUPPLY_PROP_MANUFACTURER:
+		val->strval = RT5033_MANUFACTURER;
+		break;
+	case POWER_SUPPLY_PROP_ONLINE:
+		val->intval = (rt5033_get_charger_state(charger) ==
+				POWER_SUPPLY_STATUS_CHARGING);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static struct rt5033_charger_data *rt5033_charger_dt_init(
+						struct rt5033_charger *charger)
+{
+	struct rt5033_charger_data *chg;
+	struct power_supply_battery_info *info;
+	int ret;
+
+	chg = devm_kzalloc(charger->dev, sizeof(*chg), GFP_KERNEL);
+	if (!chg)
+		return ERR_PTR(-ENOMEM);
+
+	ret = power_supply_get_battery_info(charger->psy, &info);
+	if (ret)
+		return ERR_PTR(dev_err_probe(charger->dev, -EINVAL,
+			       "missing battery info\n"));
+
+	/* Assign data. Validity will be checked in the init functions. */
+	chg->pre_uamp = info->precharge_current_ua;
+	chg->fast_uamp = info->constant_charge_current_max_ua;
+	chg->eoc_uamp = info->charge_term_current_ua;
+	chg->pre_uvolt = info->precharge_voltage_max_uv;
+	chg->const_uvolt = info->constant_charge_voltage_max_uv;
+
+	return chg;
+}
+
+static const struct power_supply_desc rt5033_charger_desc = {
+	.name = "rt5033-charger",
+	.type = POWER_SUPPLY_TYPE_USB,
+	.properties = rt5033_charger_props,
+	.num_properties = ARRAY_SIZE(rt5033_charger_props),
+	.get_property = rt5033_charger_get_property,
+};
+
+static int rt5033_charger_probe(struct platform_device *pdev)
+{
+	struct rt5033_charger *charger;
+	struct power_supply_config psy_cfg = {};
+	int ret;
+
+	charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
+	if (!charger)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, charger);
+	charger->dev = &pdev->dev;
+	charger->regmap = dev_get_regmap(pdev->dev.parent, NULL);
+
+	psy_cfg.of_node = pdev->dev.of_node;
+	psy_cfg.drv_data = charger;
+
+	charger->psy = devm_power_supply_register(&pdev->dev,
+						  &rt5033_charger_desc,
+						  &psy_cfg);
+	if (IS_ERR(charger->psy))
+		return dev_err_probe(&pdev->dev, PTR_ERR(charger->psy),
+				     "Failed to register power supply\n");
+
+	charger->chg = rt5033_charger_dt_init(charger);
+	if (IS_ERR_OR_NULL(charger->chg))
+		return -ENODEV;
+
+	ret = rt5033_charger_reg_init(charger);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static const struct platform_device_id rt5033_charger_id[] = {
+	{ "rt5033-charger", },
+	{ }
+};
+MODULE_DEVICE_TABLE(platform, rt5033_charger_id);
+
+static const struct of_device_id rt5033_charger_of_match[] = {
+	{ .compatible = "richtek,rt5033-charger", },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, rt5033_charger_of_match);
+
+static struct platform_driver rt5033_charger_driver = {
+	.driver = {
+		.name = "rt5033-charger",
+		.of_match_table = rt5033_charger_of_match,
+	},
+	.probe = rt5033_charger_probe,
+	.id_table = rt5033_charger_id,
+};
+module_platform_driver(rt5033_charger_driver);
+
+MODULE_DESCRIPTION("Richtek RT5033 charger driver");
+MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
+MODULE_LICENSE("GPL v2");
diff --git a/include/linux/mfd/rt5033.h b/include/linux/mfd/rt5033.h
index e99e2ab0c1c1..3992fb2ef0a8 100644
--- a/include/linux/mfd/rt5033.h
+++ b/include/linux/mfd/rt5033.h
@@ -39,20 +39,4 @@  struct rt5033_battery {
 	struct power_supply	*psy;
 };
 
-/* RT5033 charger platform data */
-struct rt5033_charger_data {
-	unsigned int pre_uamp;
-	unsigned int pre_uvolt;
-	unsigned int const_uvolt;
-	unsigned int eoc_uamp;
-	unsigned int fast_uamp;
-};
-
-struct rt5033_charger {
-	struct device			*dev;
-	struct rt5033_dev		*rt5033;
-	struct power_supply		*psy;
-	struct rt5033_charger_data	*chg;
-};
-
 #endif /* __RT5033_H__ */