2023-04-27 23:31:58

by Jakob Hauser

[permalink] [raw]
Subject: [PATCH v3 0/8] Add RT5033 charger device driver

This patchset adds the charger driver "rt5033-charger". It is part of the
multifunction device rt5033. The patchset is based on an older version by
Beomho Seo of March 2015. For more information on the history and setup of
the patchset see the cover sheet of version v1, there is a link further down
below the changelog.

The patchset is still based on next-20230413.

Changes in v3:
- Drobbed v2 patch 5 "regulator: rt5033: Change regulator names to lowercase".
It affects an existing driver, therefore the uppercase node names are
tolerable.
- Patch 5: In function rt5033_get_charger_state() after "if (!regmap)"
changed "return state" to "return POWER_SUPPLY_STATUS_UNKNOWN". If the
regmap isn't available, the status is unknown.
- Patch 5: Changed function rt5033_charger_dt_init() to the devicetree
"battery" node readout style.
- Patch 5: Added dev_err() messages if the values are out of range in the
init functions rt5033_init_const_charge(), rt5033_init_fast_charge() and
rt5033_init_pre_charge().
- Patch 5: In function rt5033_charger_probe() moved rt5033_charger_dt_init()
and rt5033_charger_reg_init() behind devm_power_supply_register() because
the power supply needs to be initiated to read out the "battery" node.
- Patch 7: In function rt5033_battery_get_status() after "if (!charger)"
changed "return -ENODEV" to "return POWER_SUPPLY_STATUS_UNKNOWN". If the
charger isn't available, the status is unknown.
- Changes on patch 8 are described in that patch below the commit message.

v1: https://lore.kernel.org/linux-pm/[email protected]/T/#t
v2: https://lore.kernel.org/linux-pm/[email protected]/T/#t

The result of the patchset v3 can be seen at:
https://github.com/Jakko3/linux/blob/rt5033-charger_v3/drivers/power/supply/rt5033_charger.c

Jakob Hauser (7):
mfd: rt5033: Fix chip revision readout
mfd: rt5033: Fix STAT_MASK, HZ_MASK and AICR defines
mfd: rt5033: Apply preparatory changes before adding rt5033-charger
driver
power: supply: rt5033_charger: Add RT5033 charger device driver
power: supply: rt5033_charger: Add cable detection and USB OTG supply
power: supply: rt5033_battery: Adopt status property from charger
dt-bindings: Add rt5033 mfd, regulator and charger

Stephan Gerhold (1):
mfd: rt5033: Drop rt5033-battery sub-device

.../bindings/mfd/richtek,rt5033.yaml | 105 +++
.../power/supply/richtek,rt5033-charger.yaml | 63 ++
drivers/mfd/rt5033.c | 8 +-
drivers/power/supply/Kconfig | 8 +
drivers/power/supply/Makefile | 1 +
drivers/power/supply/rt5033_battery.c | 24 +
drivers/power/supply/rt5033_charger.c | 725 ++++++++++++++++++
include/linux/mfd/rt5033-private.h | 64 +-
include/linux/mfd/rt5033.h | 10 +-
9 files changed, 981 insertions(+), 27 deletions(-)
create mode 100644 Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml
create mode 100644 Documentation/devicetree/bindings/power/supply/richtek,rt5033-charger.yaml
create mode 100644 drivers/power/supply/rt5033_charger.c

--
2.39.2


2023-04-27 23:32:00

by Jakob Hauser

[permalink] [raw]
Subject: [PATCH v3 2/8] mfd: rt5033: Fix chip revision readout

After reading the data from the DEVICE_ID register, mask 0x0f needs to be
applied to extract the revision of the chip [1].

The other part of the DEVICE_ID register, mask 0xf0, is a vendor identification
code. That's how it is set up at similar products of Richtek, e.g. RT9455 [2]
page 21 top.

[1] https://github.com/msm8916-mainline/linux-downstream/blob/GT-I9195I/drivers/mfd/rt5033_core.c#L484
[2] https://www.richtek.com/assets/product_file/RT9455/DS9455-00.pdf

Signed-off-by: Jakob Hauser <[email protected]>
---
drivers/mfd/rt5033.c | 5 +++--
include/linux/mfd/rt5033-private.h | 4 ++++
2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/mfd/rt5033.c b/drivers/mfd/rt5033.c
index 8029d444b794..3eee4242ee02 100644
--- a/drivers/mfd/rt5033.c
+++ b/drivers/mfd/rt5033.c
@@ -55,7 +55,7 @@ static const struct regmap_config rt5033_regmap_config = {
static int rt5033_i2c_probe(struct i2c_client *i2c)
{
struct rt5033_dev *rt5033;
- unsigned int dev_id;
+ unsigned int dev_id, chip_rev;
int ret;

rt5033 = devm_kzalloc(&i2c->dev, sizeof(*rt5033), GFP_KERNEL);
@@ -78,7 +78,8 @@ static int rt5033_i2c_probe(struct i2c_client *i2c)
dev_err(&i2c->dev, "Device not found\n");
return -ENODEV;
}
- dev_info(&i2c->dev, "Device found Device ID: %04x\n", dev_id);
+ chip_rev = dev_id & RT5033_CHIP_REV_MASK;
+ dev_info(&i2c->dev, "Device found (rev. %d)\n", chip_rev);

ret = regmap_add_irq_chip(rt5033->regmap, rt5033->irq,
IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
diff --git a/include/linux/mfd/rt5033-private.h b/include/linux/mfd/rt5033-private.h
index 6bb432f6a96c..b035a67cec73 100644
--- a/include/linux/mfd/rt5033-private.h
+++ b/include/linux/mfd/rt5033-private.h
@@ -71,6 +71,10 @@ enum rt5033_reg {
/* RT5033 CHGCTRL2 register */
#define RT5033_CHGCTRL2_CV_MASK 0xfc

+/* RT5033 DEVICE_ID register */
+#define RT5033_VENDOR_ID_MASK 0xf0
+#define RT5033_CHIP_REV_MASK 0x0f
+
/* RT5033 CHGCTRL3 register */
#define RT5033_CHGCTRL3_CFO_EN_MASK 0x40
#define RT5033_CHGCTRL3_TIMER_MASK 0x38
--
2.39.2

2023-04-27 23:32:34

by Jakob Hauser

[permalink] [raw]
Subject: [PATCH v3 3/8] mfd: rt5033: Fix STAT_MASK, HZ_MASK and AICR defines

The charger state mask RT5033_CHG_STAT_MASK should be 0x30 [1][2].

The high impedance mask RT5033_RT_HZ_MASK is actually value 0x02 [3] and is
assosiated to the RT5033 CHGCTRL1 register [4]. Accordingly also change
RT5033_CHARGER_HZ_ENABLE to 0x02 to avoid the need of a bit shift upon
application.

For input current limiting AICR mode, the define for the 1000 mA step was
missing [5]. Additionally add the define for DISABLE option. Concerning the
mask, remove RT5033_AICR_MODE_MASK because there is already
RT5033_CHGCTRL1_IAICR_MASK further up. They are redundant and the upper one
makes more sense to have the masks of a register colleted there as an
overview.

[1] https://github.com/msm8916-mainline/linux-downstream/blob/GT-I9195I/drivers/battery/rt5033_charger.c#L669-L682
[2] https://github.com/torvalds/linux/blob/v6.0/include/linux/mfd/rt5033-private.h#L59-L62
[3] https://github.com/msm8916-mainline/linux-downstream/blob/GT-I9195I/include/linux/battery/charger/rt5033_charger.h#L44
[4] https://github.com/msm8916-mainline/linux-downstream/blob/GT-I9195I/drivers/battery/rt5033_charger.c#L223
[5] https://github.com/msm8916-mainline/linux-downstream/blob/GT-I9195I/drivers/battery/rt5033_charger.c#L278

Signed-off-by: Jakob Hauser <[email protected]>
Acked-for-MFD-by: Lee Jones <[email protected]>
---
include/linux/mfd/rt5033-private.h | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/include/linux/mfd/rt5033-private.h b/include/linux/mfd/rt5033-private.h
index b035a67cec73..b6773ebf4e6b 100644
--- a/include/linux/mfd/rt5033-private.h
+++ b/include/linux/mfd/rt5033-private.h
@@ -55,7 +55,7 @@ enum rt5033_reg {
};

/* RT5033 Charger state register */
-#define RT5033_CHG_STAT_MASK 0x20
+#define RT5033_CHG_STAT_MASK 0x30
#define RT5033_CHG_STAT_DISCHARGING 0x00
#define RT5033_CHG_STAT_FULL 0x10
#define RT5033_CHG_STAT_CHARGING 0x20
@@ -67,6 +67,7 @@ enum rt5033_reg {
/* RT5033 CHGCTRL1 register */
#define RT5033_CHGCTRL1_IAICR_MASK 0xe0
#define RT5033_CHGCTRL1_MODE_MASK 0x01
+#define RT5033_CHGCTRL1_HZ_MASK 0x02

/* RT5033 CHGCTRL2 register */
#define RT5033_CHGCTRL2_CV_MASK 0xfc
@@ -92,7 +93,6 @@ enum rt5033_reg {

/* RT5033 RT CTRL1 register */
#define RT5033_RT_CTRL1_UUG_MASK 0x02
-#define RT5033_RT_HZ_MASK 0x01

/* RT5033 control register */
#define RT5033_CTRL_FCCM_BUCK_MASK BIT(0)
@@ -119,13 +119,14 @@ enum rt5033_reg {
* register), AICR mode limits the input current. For example, the AIRC 100
* mode limits the input current to 100 mA.
*/
+#define RT5033_AICR_DISABLE 0x00
#define RT5033_AICR_100_MODE 0x20
#define RT5033_AICR_500_MODE 0x40
#define RT5033_AICR_700_MODE 0x60
#define RT5033_AICR_900_MODE 0x80
+#define RT5033_AICR_1000_MODE 0xa0
#define RT5033_AICR_1500_MODE 0xc0
#define RT5033_AICR_2000_MODE 0xe0
-#define RT5033_AICR_MODE_MASK 0xe0

/* RT5033 use internal timer need to set time */
#define RT5033_FAST_CHARGE_TIMER4 0x00
@@ -195,7 +196,7 @@ enum rt5033_reg {

/* RT5033 charger high impedance mode */
#define RT5033_CHARGER_HZ_DISABLE 0x00
-#define RT5033_CHARGER_HZ_ENABLE 0x01
+#define RT5033_CHARGER_HZ_ENABLE 0x02

/* RT5033 regulator BUCK output voltage uV */
#define RT5033_REGULATOR_BUCK_VOLTAGE_MIN 1000000U
--
2.39.2

2023-04-27 23:32:42

by Jakob Hauser

[permalink] [raw]
Subject: [PATCH v3 4/8] mfd: rt5033: Apply preparatory changes before adding rt5033-charger driver

Order the register blocks to have the masks in descending manner.

Add new defines for constant voltage shift (RT5033_CHGCTRL2_CV_SHIFT),
MIVR mask (RT5033_CHGCTRL4_MIVR_MASK), pre-charge current shift
(RT5033_CHGCTRL4_IPREC_SHIFT), internal timer disable
(RT5033_INT_TIMER_DISABLE), termination disable (RT5033_TE_DISABLE),
CFO disable (RT5033_CFO_DISABLE), UUG disable (RT5033_CHARGER_UUG_DISABLE).

The fast charge timer type needs to be written on mask 0x38
(RT5033_CHGCTRL3_TIMER_MASK). To avoid a bit shift on application, change the
values of the timer types to fit the mask. Added the timout duration as a
comment. And the timer between TIMER8 and TIMER12 is most likely TIMER10, see
e.g. RT5036 [1] page 28 bottom.

Add value options for MIVR (Minimum Input Voltage Regulation).

Move RT5033_TE_ENABLE_MASK to the block "RT5033 CHGCTRL1 register", in order
to have the masks of the register collected there. To fit the naming scheme,
rename it to RT5033_CHGCTRL1_TE_EN_MASK.

Move RT5033_CHG_MAX_CURRENT to the block "RT5033 charger fast-charge current".

Add new defines RT5033_CV_MAX_VOLTAGE and RT5033_CHG_MAX_PRE_CURRENT to the
blocks "RT5033 charger constant charge voltage" and "RT5033 charger pre-charge
current limits".

In include/linux/mfd/rt5033.h, turn power_supply "psy" into a pointer in order
to use it in devm_power_supply_register().

[1] https://media.digikey.com/pdf/Data%20Sheets/Richtek%20PDF/RT5036%20%20Preliminary.pdf

Signed-off-by: Jakob Hauser <[email protected]>
---
include/linux/mfd/rt5033-private.h | 53 ++++++++++++++++++++----------
include/linux/mfd/rt5033.h | 2 +-
2 files changed, 36 insertions(+), 19 deletions(-)

diff --git a/include/linux/mfd/rt5033-private.h b/include/linux/mfd/rt5033-private.h
index b6773ebf4e6b..0221f806d139 100644
--- a/include/linux/mfd/rt5033-private.h
+++ b/include/linux/mfd/rt5033-private.h
@@ -55,22 +55,24 @@ enum rt5033_reg {
};

/* RT5033 Charger state register */
+#define RT5033_CHG_STAT_TYPE_MASK 0x60
+#define RT5033_CHG_STAT_TYPE_PRE 0x20
+#define RT5033_CHG_STAT_TYPE_FAST 0x60
#define RT5033_CHG_STAT_MASK 0x30
#define RT5033_CHG_STAT_DISCHARGING 0x00
#define RT5033_CHG_STAT_FULL 0x10
#define RT5033_CHG_STAT_CHARGING 0x20
#define RT5033_CHG_STAT_NOT_CHARGING 0x30
-#define RT5033_CHG_STAT_TYPE_MASK 0x60
-#define RT5033_CHG_STAT_TYPE_PRE 0x20
-#define RT5033_CHG_STAT_TYPE_FAST 0x60

/* RT5033 CHGCTRL1 register */
#define RT5033_CHGCTRL1_IAICR_MASK 0xe0
-#define RT5033_CHGCTRL1_MODE_MASK 0x01
+#define RT5033_CHGCTRL1_TE_EN_MASK 0x08
#define RT5033_CHGCTRL1_HZ_MASK 0x02
+#define RT5033_CHGCTRL1_MODE_MASK 0x01

/* RT5033 CHGCTRL2 register */
#define RT5033_CHGCTRL2_CV_MASK 0xfc
+#define RT5033_CHGCTRL2_CV_SHIFT 0x02

/* RT5033 DEVICE_ID register */
#define RT5033_VENDOR_ID_MASK 0xf0
@@ -82,14 +84,15 @@ enum rt5033_reg {
#define RT5033_CHGCTRL3_TIMER_EN_MASK 0x01

/* RT5033 CHGCTRL4 register */
-#define RT5033_CHGCTRL4_EOC_MASK 0x07
+#define RT5033_CHGCTRL4_MIVR_MASK 0xe0
#define RT5033_CHGCTRL4_IPREC_MASK 0x18
+#define RT5033_CHGCTRL4_IPREC_SHIFT 0x03
+#define RT5033_CHGCTRL4_EOC_MASK 0x07

/* RT5033 CHGCTRL5 register */
-#define RT5033_CHGCTRL5_VPREC_MASK 0x0f
#define RT5033_CHGCTRL5_ICHG_MASK 0xf0
#define RT5033_CHGCTRL5_ICHG_SHIFT 0x04
-#define RT5033_CHG_MAX_CURRENT 0x0d
+#define RT5033_CHGCTRL5_VPREC_MASK 0x0f

/* RT5033 RT CTRL1 register */
#define RT5033_RT_CTRL1_UUG_MASK 0x02
@@ -128,20 +131,28 @@ enum rt5033_reg {
#define RT5033_AICR_1500_MODE 0xc0
#define RT5033_AICR_2000_MODE 0xe0

-/* RT5033 use internal timer need to set time */
-#define RT5033_FAST_CHARGE_TIMER4 0x00
-#define RT5033_FAST_CHARGE_TIMER6 0x01
-#define RT5033_FAST_CHARGE_TIMER8 0x02
-#define RT5033_FAST_CHARGE_TIMER9 0x03
-#define RT5033_FAST_CHARGE_TIMER12 0x04
-#define RT5033_FAST_CHARGE_TIMER14 0x05
-#define RT5033_FAST_CHARGE_TIMER16 0x06
+/* RT5033 charger minimum input voltage regulation */
+#define RT5033_CHARGER_MIVR_DISABLE 0x00
+#define RT5033_CHARGER_MIVR_4200MV 0x20
+#define RT5033_CHARGER_MIVR_4300MV 0x40
+#define RT5033_CHARGER_MIVR_4400MV 0x60
+#define RT5033_CHARGER_MIVR_4500MV 0x80
+#define RT5033_CHARGER_MIVR_4600MV 0xa0
+#define RT5033_CHARGER_MIVR_4700MV 0xc0
+#define RT5033_CHARGER_MIVR_4800MV 0xe0

+/* RT5033 use internal timer need to set time */
+#define RT5033_FAST_CHARGE_TIMER4 0x00 /* 4 hrs */
+#define RT5033_FAST_CHARGE_TIMER6 0x08 /* 6 hrs */
+#define RT5033_FAST_CHARGE_TIMER8 0x10 /* 8 hrs */
+#define RT5033_FAST_CHARGE_TIMER10 0x18 /* 10 hrs */
+#define RT5033_FAST_CHARGE_TIMER12 0x20 /* 12 hrs */
+#define RT5033_FAST_CHARGE_TIMER14 0x28 /* 14 hrs */
+#define RT5033_FAST_CHARGE_TIMER16 0x30 /* 16 hrs */
+
+#define RT5033_INT_TIMER_DISABLE 0x00
#define RT5033_INT_TIMER_ENABLE 0x01

-/* RT5033 charger termination enable mask */
-#define RT5033_TE_ENABLE_MASK 0x08
-
/*
* RT5033 charger opa mode. RT5033 has two opa modes for OTG: charger mode
* and boost mode.
@@ -150,25 +161,30 @@ enum rt5033_reg {
#define RT5033_BOOST_MODE 0x01

/* RT5033 charger termination enable */
+#define RT5033_TE_DISABLE 0x00
#define RT5033_TE_ENABLE 0x08

/* RT5033 charger CFO enable */
+#define RT5033_CFO_DISABLE 0x00
#define RT5033_CFO_ENABLE 0x40

/* RT5033 charger constant charge voltage (as in CHGCTRL2 register), uV */
#define RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MIN 3650000U
#define RT5033_CHARGER_CONST_VOLTAGE_STEP_NUM 25000U
#define RT5033_CHARGER_CONST_VOLTAGE_LIMIT_MAX 4400000U
+#define RT5033_CV_MAX_VOLTAGE 0x1e

/* RT5033 charger pre-charge current limits (as in CHGCTRL4 register), uA */
#define RT5033_CHARGER_PRE_CURRENT_LIMIT_MIN 350000U
#define RT5033_CHARGER_PRE_CURRENT_STEP_NUM 100000U
#define RT5033_CHARGER_PRE_CURRENT_LIMIT_MAX 650000U
+#define RT5033_CHG_MAX_PRE_CURRENT 0x03

/* RT5033 charger fast-charge current (as in CHGCTRL5 register), uA */
#define RT5033_CHARGER_FAST_CURRENT_MIN 700000U
#define RT5033_CHARGER_FAST_CURRENT_STEP_NUM 100000U
#define RT5033_CHARGER_FAST_CURRENT_MAX 2000000U
+#define RT5033_CHG_MAX_CURRENT 0x0d

/*
* RT5033 charger const-charge end of charger current (
@@ -192,6 +208,7 @@ enum rt5033_reg {
* RT5033 charger UUG. It enables MOS auto control by H/W charger
* circuit.
*/
+#define RT5033_CHARGER_UUG_DISABLE 0x00
#define RT5033_CHARGER_UUG_ENABLE 0x02

/* RT5033 charger high impedance mode */
diff --git a/include/linux/mfd/rt5033.h b/include/linux/mfd/rt5033.h
index 8f306ac15a27..e99e2ab0c1c1 100644
--- a/include/linux/mfd/rt5033.h
+++ b/include/linux/mfd/rt5033.h
@@ -51,7 +51,7 @@ struct rt5033_charger_data {
struct rt5033_charger {
struct device *dev;
struct rt5033_dev *rt5033;
- struct power_supply psy;
+ struct power_supply *psy;
struct rt5033_charger_data *chg;
};

--
2.39.2

2023-04-27 23:32:45

by Jakob Hauser

[permalink] [raw]
Subject: [PATCH v3 5/8] power: supply: rt5033_charger: Add RT5033 charger device driver

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 <[email protected]>
Cc: Chanwoo Choi <[email protected]>
Tested-by: Raymond Hackley <[email protected]>
Signed-off-by: Jakob Hauser <[email protected]>
---
drivers/power/supply/Kconfig | 8 +
drivers/power/supply/Makefile | 1 +
drivers/power/supply/rt5033_charger.c | 464 ++++++++++++++++++++++++++
3 files changed, 473 insertions(+)
create mode 100644 drivers/power/supply/rt5033_charger.c

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..038530d2f0a0
--- /dev/null
+++ b/drivers/power/supply/rt5033_charger.c
@@ -0,0 +1,464 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Battery charger driver for RT5033
+ *
+ * Copyright (C) 2014 Samsung Electronics, Co., Ltd.
+ * Author: Beomho Seo <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/power_supply.h>
+#include <linux/mfd/rt5033-private.h>
+#include <linux/mfd/rt5033.h>
+
+static int rt5033_get_charger_state(struct rt5033_charger *charger)
+{
+ struct regmap *regmap = charger->rt5033->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->rt5033->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->rt5033->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->rt5033->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->rt5033->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->rt5033->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->rt5033->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->rt5033->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->rt5033->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->rt5033->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->rt5033->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->rt5033->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);
+ int ret = 0;
+
+ 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 ret;
+}
+
+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) {
+ dev_err(charger->dev, "failed to get battery info\n");
+ return ERR_PTR(-EINVAL);
+ }
+
+ /* 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 rt5033_dev *rt5033 = dev_get_drvdata(pdev->dev.parent);
+ 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->rt5033 = rt5033;
+
+ 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)) {
+ dev_err(&pdev->dev, "failed: power supply register\n");
+ return PTR_ERR(charger->psy);
+ }
+
+ 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 <[email protected]>");
+MODULE_LICENSE("GPL v2");
--
2.39.2

2023-04-27 23:37:40

by Jakob Hauser

[permalink] [raw]
Subject: [PATCH v3 8/8] dt-bindings: Add rt5033 mfd, regulator and charger

Add device tree binding documentation for rt5033 multifunction device, voltage
regulator and battery charger.

Cc: Beomho Seo <[email protected]>
Cc: Chanwoo Choi <[email protected]>
Signed-off-by: Jakob Hauser <[email protected]>
---
The patch is based on next-20230413.

The drivers for rt5033 (mfd) and rt5033-regulator are existing. Whereas the
the driver rt5033-charger is new in this patchset.

The extcon phandle is still there, as I didn't know what Rob meant by
"standard connector bindings".

Changes in v3:
- Removed redundant "documentation" in the commit title.
- Squashed regulator into the mfd binding.
- Restored the regulator node names to uppercase because it is an existing
driver.
- In the charger binding replaced the vendor properties by "battery" node
style. Accodringly updated the example in the mfd.

.../bindings/mfd/richtek,rt5033.yaml | 105 ++++++++++++++++++
.../power/supply/richtek,rt5033-charger.yaml | 63 +++++++++++
2 files changed, 168 insertions(+)
create mode 100644 Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml
create mode 100644 Documentation/devicetree/bindings/power/supply/richtek,rt5033-charger.yaml

diff --git a/Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml b/Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml
new file mode 100644
index 000000000000..ee704914201f
--- /dev/null
+++ b/Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml
@@ -0,0 +1,105 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mfd/richtek,rt5033.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Richtek RT5033 Power Management Integrated Circuit
+
+maintainers:
+ - Jakob Hauser <[email protected]>
+
+description:
+ RT5033 is a multifunction device which includes battery charger, fuel gauge,
+ flash LED current source, LDO and synchronous Buck converter for portable
+ applications. It is interfaced to host controller using I2C interface. The
+ battery fuel gauge uses a separate I2C bus.
+
+properties:
+ compatible:
+ const: richtek,rt5033
+
+ reg:
+ maxItems: 1
+
+ interrupts:
+ maxItems: 1
+
+ regulators:
+ description:
+ The regulators of RT5033 have to be instantiated under a sub-node named
+ "regulators". For SAFE_LDO voltage there is only one value of 4.9 V. LDO
+ voltage ranges from 1.2 V to 3.0 V in 0.1 V steps. BUCK voltage ranges
+ from 1.0 V to 3.0 V in 0.1 V steps.
+ type: object
+ patternProperties:
+ "^(SAFE_LDO|LDO|BUCK)$":
+ type: object
+ $ref: /schemas/regulator/regulator.yaml#
+ unevaluatedProperties: false
+ additionalProperties: false
+
+ charger:
+ type: object
+ $ref: /schemas/power/supply/richtek,rt5033-charger.yaml#
+
+required:
+ - compatible
+ - reg
+ - interrupts
+
+additionalProperties: false
+
+examples:
+ - |
+ #include <dt-bindings/interrupt-controller/irq.h>
+
+ battery: battery {
+ compatible = "simple-battery";
+ precharge-current-microamp = <450000>;
+ constant-charge-current-max-microamp = <1000000>;
+ charge-term-current-microamp = <150000>;
+ precharge-upper-limit-microvolt = <3500000>;
+ constant-charge-voltage-max-microvolt = <4350000>;
+ };
+
+ i2c {
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ pmic@34 {
+ compatible = "richtek,rt5033";
+ reg = <0x34>;
+
+ interrupt-parent = <&msmgpio>;
+ interrupts = <62 IRQ_TYPE_EDGE_FALLING>;
+
+ pinctrl-names = "default";
+ pinctrl-0 = <&pmic_int_default>;
+
+ regulators {
+ safe_ldo_reg: SAFE_LDO {
+ regulator-name = "SAFE_LDO";
+ regulator-min-microvolt = <4900000>;
+ regulator-max-microvolt = <4900000>;
+ regulator-always-on;
+ };
+ ldo_reg: LDO {
+ regulator-name = "LDO";
+ regulator-min-microvolt = <2800000>;
+ regulator-max-microvolt = <2800000>;
+ };
+ buck_reg: BUCK {
+ regulator-name = "BUCK";
+ regulator-min-microvolt = <1200000>;
+ regulator-max-microvolt = <1200000>;
+ };
+ };
+
+ charger {
+ compatible = "richtek,rt5033-charger";
+ monitored-battery = <&battery>;
+ extcon = <&muic>;
+ };
+ };
+ };
diff --git a/Documentation/devicetree/bindings/power/supply/richtek,rt5033-charger.yaml b/Documentation/devicetree/bindings/power/supply/richtek,rt5033-charger.yaml
new file mode 100644
index 000000000000..bf08e8db365e
--- /dev/null
+++ b/Documentation/devicetree/bindings/power/supply/richtek,rt5033-charger.yaml
@@ -0,0 +1,63 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/power/supply/richtek,rt5033-charger.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Richtek RT5033 PIMC Battery Charger
+
+maintainers:
+ - Jakob Hauser <[email protected]>
+
+description:
+ The battery charger of the multifunction device RT5033 has to be instantiated
+ under sub-node named "charger" using the following format.
+
+properties:
+ compatible:
+ const: richtek,rt5033-charger
+
+ monitored-battery:
+ description: |
+ Phandle to the monitored battery according to battery.yaml. The battery
+ node needs to contain five parameters.
+
+ precharge-current-microamp:
+ Current of pre-charge mode. The pre-charge current levels are 350 mA
+ to 650 mA programmed by I2C per 100 mA.
+
+ constant-charge-current-max-microamp:
+ Current of fast-charge mode. The fast-charge current levels are 700 mA
+ to 2000 mA programmed by I2C per 100 mA.
+
+ charge-term-current-microamp:
+ This property is end of charge current. Its level ranges from 150 mA
+ to 600 mA. Between 150 mA and 300 mA in 50 mA steps, between 300 mA and
+ 600 mA in 100 mA steps.
+
+ precharge-upper-limit-microvolt:
+ Voltage of pre-charge mode. If the battery voltage is below the pre-charge
+ threshold voltage, the charger is in pre-charge mode with pre-charge
+ current. Its levels are 2.3 V to 3.8 V programmed by I2C per 0.1 V.
+
+ constant-charge-voltage-max-microvolt:
+ Battery regulation voltage of constant voltage mode. This voltage levels
+ from 3.65 V to 4.4 V by I2C per 0.025 V.
+
+ extcon:
+ description:
+ Phandle to the extcon device.
+ maxItems: 1
+
+required:
+ - monitored-battery
+
+additionalProperties: false
+
+examples:
+ - |
+ charger {
+ compatible = "richtek,rt5033-charger";
+ monitored-battery = <&battery>;
+ extcon = <&muic>;
+ };
--
2.39.2

2023-04-27 23:38:26

by Jakob Hauser

[permalink] [raw]
Subject: [PATCH v3 7/8] power: supply: rt5033_battery: Adopt status property from charger

The rt5033-battery fuelgauge can't get a status by itself. The rt5033-charger
can, let's get this value.

Tested-by: Raymond Hackley <[email protected]>
Signed-off-by: Jakob Hauser <[email protected]>
---
drivers/power/supply/rt5033_battery.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)

diff --git a/drivers/power/supply/rt5033_battery.c b/drivers/power/supply/rt5033_battery.c
index 5c04cf305219..a6520716d813 100644
--- a/drivers/power/supply/rt5033_battery.c
+++ b/drivers/power/supply/rt5033_battery.c
@@ -12,6 +12,26 @@
#include <linux/mfd/rt5033-private.h>
#include <linux/mfd/rt5033.h>

+static int rt5033_battery_get_status(struct i2c_client *client)
+{
+ struct power_supply *charger;
+ union power_supply_propval val;
+ int ret;
+
+ charger = power_supply_get_by_name("rt5033-charger");
+ if (!charger)
+ return POWER_SUPPLY_STATUS_UNKNOWN;
+
+ ret = power_supply_get_property(charger, POWER_SUPPLY_PROP_STATUS, &val);
+ if (ret) {
+ power_supply_put(charger);
+ return POWER_SUPPLY_STATUS_UNKNOWN;
+ }
+
+ power_supply_put(charger);
+ return val.intval;
+}
+
static int rt5033_battery_get_capacity(struct i2c_client *client)
{
struct rt5033_battery *battery = i2c_get_clientdata(client);
@@ -84,6 +104,9 @@ static int rt5033_battery_get_property(struct power_supply *psy,
case POWER_SUPPLY_PROP_CAPACITY:
val->intval = rt5033_battery_get_capacity(battery->client);
break;
+ case POWER_SUPPLY_PROP_STATUS:
+ val->intval = rt5033_battery_get_status(battery->client);
+ break;
default:
return -EINVAL;
}
@@ -96,6 +119,7 @@ static enum power_supply_property rt5033_battery_props[] = {
POWER_SUPPLY_PROP_VOLTAGE_OCV,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_CAPACITY,
+ POWER_SUPPLY_PROP_STATUS,
};

static const struct regmap_config rt5033_battery_regmap_config = {
--
2.39.2

2023-04-27 23:38:32

by Jakob Hauser

[permalink] [raw]
Subject: [PATCH v3 6/8] power: supply: rt5033_charger: Add cable detection and USB OTG supply

Implement cable detection by extcon and handle the driver according to the
connector type.

There are basically three types of action: "set_charging", "set_otg" and
"set_disconnect".

A forth helper function to "unset_otg" was added because this is used in both
"set_charging" and "set_disconnect". In the first case it covers the rather
rare event that someone changes from OTG to charging without disconnect. In
the second case, when disconnecting, the values are set back to the ones from
initialization to return into a defined state.

Additionally, there is "set_mivr". When connecting to e.g. a laptop/PC, the
minimum input voltage regulation (MIVR) shall prevent a voltage drop if the
cable or the supply is weak. The MIVR value is set to 4600MV, same as in the
Android driver [1]. When disconnecting, MIVR is set back to DISABLED.

In the function rt5033_get_charger_state(): When in OTG mode, the chip
reports status "charging". Change this to "discharging" because there is
no charging going on in OTG mode [2].

[1] https://github.com/msm8916-mainline/linux-downstream/blob/GT-I9195I/drivers/battery/rt5033_charger.c#L499
[2] https://github.com/msm8916-mainline/linux-downstream/blob/GT-I9195I/drivers/battery/rt5033_charger.c#L686-L687

Tested-by: Raymond Hackley <[email protected]>
Signed-off-by: Jakob Hauser <[email protected]>
---
drivers/power/supply/rt5033_charger.c | 265 +++++++++++++++++++++++++-
include/linux/mfd/rt5033.h | 8 +
2 files changed, 271 insertions(+), 2 deletions(-)

diff --git a/drivers/power/supply/rt5033_charger.c b/drivers/power/supply/rt5033_charger.c
index 038530d2f0a0..94f7b8e0da42 100644
--- a/drivers/power/supply/rt5033_charger.c
+++ b/drivers/power/supply/rt5033_charger.c
@@ -10,7 +10,10 @@
* published by the Free Software Foundation.
*/

+#include <linux/devm-helpers.h>
+#include <linux/extcon.h>
#include <linux/module.h>
+#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/mfd/rt5033-private.h>
@@ -44,6 +47,10 @@ static int rt5033_get_charger_state(struct rt5033_charger *charger)
state = POWER_SUPPLY_STATUS_UNKNOWN;
}

+ /* For OTG mode, RT5033 would still report "charging" */
+ if (charger->otg)
+ state = POWER_SUPPLY_STATUS_DISCHARGING;
+
return state;
}

@@ -135,6 +142,9 @@ static inline int rt5033_init_const_charge(struct rt5033_charger *charger)
return -EINVAL;
}

+ /* Store that value for later usage */
+ charger->cv_regval = reg_data;
+
/* Set end of charge current */
if (chg->eoc_uamp < RT5033_CHARGER_EOC_MIN ||
chg->eoc_uamp > RT5033_CHARGER_EOC_MAX) {
@@ -318,6 +328,152 @@ static int rt5033_charger_reg_init(struct rt5033_charger *charger)
return 0;
}

+static int rt5033_charger_set_otg(struct rt5033_charger *charger)
+{
+ int ret;
+
+ mutex_lock(&charger->lock);
+
+ /* Set OTG boost v_out to 5 volts */
+ ret = regmap_update_bits(charger->rt5033->regmap, RT5033_REG_CHG_CTRL2,
+ RT5033_CHGCTRL2_CV_MASK,
+ 0x37 << RT5033_CHGCTRL2_CV_SHIFT);
+ if (ret) {
+ dev_err(charger->dev, "Failed set OTG boost v_out\n");
+ return -EINVAL;
+ }
+
+ /* Set operation mode to OTG */
+ ret = regmap_update_bits(charger->rt5033->regmap, RT5033_REG_CHG_CTRL1,
+ RT5033_CHGCTRL1_MODE_MASK, RT5033_BOOST_MODE);
+ if (ret) {
+ dev_err(charger->dev, "Failed to update OTG mode.\n");
+ return -EINVAL;
+ }
+
+ /* In case someone switched from charging to OTG directly */
+ if (charger->online)
+ charger->online = false;
+
+ charger->otg = true;
+
+ mutex_unlock(&charger->lock);
+
+ return 0;
+}
+
+static int rt5033_charger_unset_otg(struct rt5033_charger *charger)
+{
+ int ret;
+ u8 data;
+
+ /* Restore constant voltage for charging */
+ data = charger->cv_regval;
+ ret = regmap_update_bits(charger->rt5033->regmap, RT5033_REG_CHG_CTRL2,
+ RT5033_CHGCTRL2_CV_MASK,
+ data << RT5033_CHGCTRL2_CV_SHIFT);
+ if (ret) {
+ dev_err(charger->dev, "Failed to restore constant voltage\n");
+ return -EINVAL;
+ }
+
+ /* Set operation mode to charging */
+ ret = regmap_update_bits(charger->rt5033->regmap, RT5033_REG_CHG_CTRL1,
+ RT5033_CHGCTRL1_MODE_MASK, RT5033_CHARGER_MODE);
+ if (ret) {
+ dev_err(charger->dev, "Failed to update charger mode.\n");
+ return -EINVAL;
+ }
+
+ charger->otg = false;
+
+ return 0;
+}
+
+static int rt5033_charger_set_charging(struct rt5033_charger *charger)
+{
+ int ret;
+
+ mutex_lock(&charger->lock);
+
+ /* In case someone switched from OTG to charging directly */
+ if (charger->otg) {
+ ret = rt5033_charger_unset_otg(charger);
+ if (ret)
+ return -EINVAL;
+ }
+
+ charger->online = true;
+
+ mutex_unlock(&charger->lock);
+
+ return 0;
+}
+
+static int rt5033_charger_set_mivr(struct rt5033_charger *charger)
+{
+ int ret;
+
+ mutex_lock(&charger->lock);
+
+ /*
+ * When connected via USB connector type SDP (Standard Downstream Port),
+ * the minimum input voltage regulation (MIVR) should be enabled. It
+ * prevents an input voltage drop due to insufficient current provided
+ * by the adapter or USB input. As a downside, it may reduces the
+ * charging current and thus slows the charging.
+ */
+ ret = regmap_update_bits(charger->rt5033->regmap, RT5033_REG_CHG_CTRL4,
+ RT5033_CHGCTRL4_MIVR_MASK, RT5033_CHARGER_MIVR_4600MV);
+ if (ret) {
+ dev_err(charger->dev, "Failed to set MIVR level.\n");
+ return -EINVAL;
+ }
+
+ charger->mivr_enabled = true;
+
+ mutex_unlock(&charger->lock);
+
+ /* Beyond this, do the same steps like setting charging */
+ rt5033_charger_set_charging(charger);
+
+ return 0;
+}
+
+static int rt5033_charger_set_disconnect(struct rt5033_charger *charger)
+{
+ int ret;
+
+ mutex_lock(&charger->lock);
+
+ /* Disable MIVR if enabled */
+ if (charger->mivr_enabled) {
+ ret = regmap_update_bits(charger->rt5033->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;
+ }
+
+ charger->mivr_enabled = false;
+ }
+
+ if (charger->otg) {
+ ret = rt5033_charger_unset_otg(charger);
+ if (ret)
+ return -EINVAL;
+ }
+
+ if (charger->online)
+ charger->online = false;
+
+ mutex_unlock(&charger->lock);
+
+ return 0;
+}
+
static enum power_supply_property rt5033_charger_props[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_CHARGE_TYPE,
@@ -355,8 +511,7 @@ static int rt5033_charger_get_property(struct power_supply *psy,
val->strval = RT5033_MANUFACTURER;
break;
case POWER_SUPPLY_PROP_ONLINE:
- val->intval = (rt5033_get_charger_state(charger) ==
- POWER_SUPPLY_STATUS_CHARGING);
+ val->intval = charger->online;
break;
default:
return -EINVAL;
@@ -392,6 +547,86 @@ static struct rt5033_charger_data *rt5033_charger_dt_init(
return chg;
}

+static void rt5033_charger_extcon_work(struct work_struct *work)
+{
+ struct rt5033_charger *charger =
+ container_of(work, struct rt5033_charger, extcon_work);
+ struct extcon_dev *edev = charger->edev;
+ int connector, state;
+ int ret;
+
+ for (connector = EXTCON_USB_HOST; connector <= EXTCON_CHG_USB_PD;
+ connector++) {
+ state = extcon_get_state(edev, connector);
+ if (state == 1)
+ break;
+ }
+
+ /*
+ * Adding a delay between extcon notification and extcon action. This
+ * makes extcon action execution more reliable. Without the delay the
+ * execution sometimes fails, possibly because the chip is busy or not
+ * ready.
+ */
+ msleep(100);
+
+ switch (connector) {
+ case EXTCON_CHG_USB_SDP:
+ ret = rt5033_charger_set_mivr(charger);
+ if (ret) {
+ dev_err(charger->dev, "failed to set USB mode\n");
+ break;
+ }
+ dev_info(charger->dev, "USB mode. connector type: %d\n",
+ connector);
+ break;
+ case EXTCON_CHG_USB_DCP:
+ case EXTCON_CHG_USB_CDP:
+ case EXTCON_CHG_USB_ACA:
+ case EXTCON_CHG_USB_FAST:
+ case EXTCON_CHG_USB_SLOW:
+ case EXTCON_CHG_WPT:
+ case EXTCON_CHG_USB_PD:
+ ret = rt5033_charger_set_charging(charger);
+ if (ret) {
+ dev_err(charger->dev, "failed to set charging\n");
+ break;
+ }
+ dev_info(charger->dev, "charging. connector type: %d\n",
+ connector);
+ break;
+ case EXTCON_USB_HOST:
+ ret = rt5033_charger_set_otg(charger);
+ if (ret) {
+ dev_err(charger->dev, "failed to set OTG\n");
+ break;
+ }
+ dev_info(charger->dev, "OTG enabled\n");
+ break;
+ default:
+ ret = rt5033_charger_set_disconnect(charger);
+ if (ret) {
+ dev_err(charger->dev, "failed to set disconnect\n");
+ break;
+ }
+ dev_info(charger->dev, "disconnected\n");
+ break;
+ }
+
+ power_supply_changed(charger->psy);
+}
+
+static int rt5033_charger_extcon_notifier(struct notifier_block *nb,
+ unsigned long event, void *param)
+{
+ struct rt5033_charger *charger =
+ container_of(nb, struct rt5033_charger, extcon_nb);
+
+ schedule_work(&charger->extcon_work);
+
+ return NOTIFY_OK;
+}
+
static const struct power_supply_desc rt5033_charger_desc = {
.name = "rt5033-charger",
.type = POWER_SUPPLY_TYPE_USB,
@@ -414,6 +649,7 @@ static int rt5033_charger_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, charger);
charger->dev = &pdev->dev;
charger->rt5033 = rt5033;
+ mutex_init(&charger->lock);

psy_cfg.of_node = pdev->dev.of_node;
psy_cfg.drv_data = charger;
@@ -434,6 +670,31 @@ static int rt5033_charger_probe(struct platform_device *pdev)
if (ret)
return ret;

+ /*
+ * Extcon support is not vital for the charger to work. If no extcon
+ * is available, just emit a warning and leave the probe function.
+ */
+ charger->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
+ if (IS_ERR(charger->edev)) {
+ dev_warn(&pdev->dev, "no extcon phandle found in device-tree\n");
+ goto out;
+ }
+
+ ret = devm_work_autocancel(&pdev->dev, &charger->extcon_work,
+ rt5033_charger_extcon_work);
+ if (ret) {
+ dev_err(&pdev->dev, "failed to initialize extcon work\n");
+ return ret;
+ }
+
+ charger->extcon_nb.notifier_call = rt5033_charger_extcon_notifier;
+ ret = devm_extcon_register_notifier_all(&pdev->dev, charger->edev,
+ &charger->extcon_nb);
+ if (ret) {
+ dev_err(&pdev->dev, "failed to register extcon notifier\n");
+ return ret;
+ }
+out:
return 0;
}

diff --git a/include/linux/mfd/rt5033.h b/include/linux/mfd/rt5033.h
index e99e2ab0c1c1..d2c613764756 100644
--- a/include/linux/mfd/rt5033.h
+++ b/include/linux/mfd/rt5033.h
@@ -53,6 +53,14 @@ struct rt5033_charger {
struct rt5033_dev *rt5033;
struct power_supply *psy;
struct rt5033_charger_data *chg;
+ struct extcon_dev *edev;
+ struct notifier_block extcon_nb;
+ struct work_struct extcon_work;
+ struct mutex lock;
+ bool online;
+ bool otg;
+ bool mivr_enabled;
+ u8 cv_regval;
};

#endif /* __RT5033_H__ */
--
2.39.2

2023-04-28 14:40:01

by Henrik Grimler

[permalink] [raw]
Subject: Re: [PATCH v3 8/8] dt-bindings: Add rt5033 mfd, regulator and charger

Hi Jakob,

One question inline.

On Fri, Apr 28, 2023 at 01:30:11AM +0200, Jakob Hauser wrote:
> Add device tree binding documentation for rt5033 multifunction device, voltage
> regulator and battery charger.
>
> Cc: Beomho Seo <[email protected]>
> Cc: Chanwoo Choi <[email protected]>
> Signed-off-by: Jakob Hauser <[email protected]>
> ---
> The patch is based on next-20230413.
>
> The drivers for rt5033 (mfd) and rt5033-regulator are existing. Whereas the
> the driver rt5033-charger is new in this patchset.
>
> The extcon phandle is still there, as I didn't know what Rob meant by
> "standard connector bindings".
>
> Changes in v3:
> - Removed redundant "documentation" in the commit title.
> - Squashed regulator into the mfd binding.
> - Restored the regulator node names to uppercase because it is an existing
> driver.
> - In the charger binding replaced the vendor properties by "battery" node
> style. Accodringly updated the example in the mfd.
>
> .../bindings/mfd/richtek,rt5033.yaml | 105 ++++++++++++++++++
> .../power/supply/richtek,rt5033-charger.yaml | 63 +++++++++++
> 2 files changed, 168 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml
> create mode 100644 Documentation/devicetree/bindings/power/supply/richtek,rt5033-charger.yaml
>
> diff --git a/Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml b/Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml
> new file mode 100644
> index 000000000000..ee704914201f
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/mfd/richtek,rt5033.yaml
> @@ -0,0 +1,105 @@
> +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/mfd/richtek,rt5033.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Richtek RT5033 Power Management Integrated Circuit
> +
> +maintainers:
> + - Jakob Hauser <[email protected]>
> +
> +description:
> + RT5033 is a multifunction device which includes battery charger, fuel gauge,
> + flash LED current source, LDO and synchronous Buck converter for portable
> + applications. It is interfaced to host controller using I2C interface. The
> + battery fuel gauge uses a separate I2C bus.
> +
> +properties:
> + compatible:
> + const: richtek,rt5033
> +
> + reg:
> + maxItems: 1
> +
> + interrupts:
> + maxItems: 1
> +
> + regulators:
> + description:
> + The regulators of RT5033 have to be instantiated under a sub-node named
> + "regulators". For SAFE_LDO voltage there is only one value of 4.9 V. LDO

Is only 4.9 V valid for SAFE_LDO? If I am reading driver found in
vendor kernel for SM-A500F it seems to to allow values between 3.3 and
4.95 V [1]. Same range is also written in the devicetree for the
device [2].

[1] https://github.com/msm8916-mainline/linux-downstream/blob/SM-A500F/drivers/regulator/rt5033_regulator.c#L109-L114
[2] https://github.com/msm8916-mainline/linux-downstream/blob/SM-A500F/arch/arm/boot/dts/samsung/msm8916/msm8916-sec-a5u-eur-r01.dtsi#L148-L149

Best regards,
Henrik Grimler

> + voltage ranges from 1.2 V to 3.0 V in 0.1 V steps. BUCK voltage ranges
> + from 1.0 V to 3.0 V in 0.1 V steps.
> + type: object
> + patternProperties:
> + "^(SAFE_LDO|LDO|BUCK)$":
> + type: object
> + $ref: /schemas/regulator/regulator.yaml#
> + unevaluatedProperties: false
> + additionalProperties: false
> +
> + charger:
> + type: object
> + $ref: /schemas/power/supply/richtek,rt5033-charger.yaml#
> +
> +required:
> + - compatible
> + - reg
> + - interrupts
> +
> +additionalProperties: false
> +
> +examples:
> + - |
> + #include <dt-bindings/interrupt-controller/irq.h>
> +
> + battery: battery {
> + compatible = "simple-battery";
> + precharge-current-microamp = <450000>;
> + constant-charge-current-max-microamp = <1000000>;
> + charge-term-current-microamp = <150000>;
> + precharge-upper-limit-microvolt = <3500000>;
> + constant-charge-voltage-max-microvolt = <4350000>;
> + };
> +
> + i2c {
> + #address-cells = <1>;
> + #size-cells = <0>;
> +
> + pmic@34 {
> + compatible = "richtek,rt5033";
> + reg = <0x34>;
> +
> + interrupt-parent = <&msmgpio>;
> + interrupts = <62 IRQ_TYPE_EDGE_FALLING>;
> +
> + pinctrl-names = "default";
> + pinctrl-0 = <&pmic_int_default>;
> +
> + regulators {
> + safe_ldo_reg: SAFE_LDO {
> + regulator-name = "SAFE_LDO";
> + regulator-min-microvolt = <4900000>;
> + regulator-max-microvolt = <4900000>;
> + regulator-always-on;
> + };
> + ldo_reg: LDO {
> + regulator-name = "LDO";
> + regulator-min-microvolt = <2800000>;
> + regulator-max-microvolt = <2800000>;
> + };
> + buck_reg: BUCK {
> + regulator-name = "BUCK";
> + regulator-min-microvolt = <1200000>;
> + regulator-max-microvolt = <1200000>;
> + };
> + };
> +
> + charger {
> + compatible = "richtek,rt5033-charger";
> + monitored-battery = <&battery>;
> + extcon = <&muic>;
> + };
> + };
> + };
> diff --git a/Documentation/devicetree/bindings/power/supply/richtek,rt5033-charger.yaml b/Documentation/devicetree/bindings/power/supply/richtek,rt5033-charger.yaml
> new file mode 100644
> index 000000000000..bf08e8db365e
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/power/supply/richtek,rt5033-charger.yaml
> @@ -0,0 +1,63 @@
> +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/power/supply/richtek,rt5033-charger.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Richtek RT5033 PIMC Battery Charger
> +
> +maintainers:
> + - Jakob Hauser <[email protected]>
> +
> +description:
> + The battery charger of the multifunction device RT5033 has to be instantiated
> + under sub-node named "charger" using the following format.
> +
> +properties:
> + compatible:
> + const: richtek,rt5033-charger
> +
> + monitored-battery:
> + description: |
> + Phandle to the monitored battery according to battery.yaml. The battery
> + node needs to contain five parameters.
> +
> + precharge-current-microamp:
> + Current of pre-charge mode. The pre-charge current levels are 350 mA
> + to 650 mA programmed by I2C per 100 mA.
> +
> + constant-charge-current-max-microamp:
> + Current of fast-charge mode. The fast-charge current levels are 700 mA
> + to 2000 mA programmed by I2C per 100 mA.
> +
> + charge-term-current-microamp:
> + This property is end of charge current. Its level ranges from 150 mA
> + to 600 mA. Between 150 mA and 300 mA in 50 mA steps, between 300 mA and
> + 600 mA in 100 mA steps.
> +
> + precharge-upper-limit-microvolt:
> + Voltage of pre-charge mode. If the battery voltage is below the pre-charge
> + threshold voltage, the charger is in pre-charge mode with pre-charge
> + current. Its levels are 2.3 V to 3.8 V programmed by I2C per 0.1 V.
> +
> + constant-charge-voltage-max-microvolt:
> + Battery regulation voltage of constant voltage mode. This voltage levels
> + from 3.65 V to 4.4 V by I2C per 0.025 V.
> +
> + extcon:
> + description:
> + Phandle to the extcon device.
> + maxItems: 1
> +
> +required:
> + - monitored-battery
> +
> +additionalProperties: false
> +
> +examples:
> + - |
> + charger {
> + compatible = "richtek,rt5033-charger";
> + monitored-battery = <&battery>;
> + extcon = <&muic>;
> + };
> --
> 2.39.2
>

2023-04-28 15:53:04

by Jakob Hauser

[permalink] [raw]
Subject: Re: [PATCH v3 8/8] dt-bindings: Add rt5033 mfd, regulator and charger

Hi Henrik,

On 28.04.23 16:39, Henrik Grimler wrote:
> On Fri, Apr 28, 2023 at 01:30:11AM +0200, Jakob Hauser wrote:
...
>> + regulators:
>> + description:
>> + The regulators of RT5033 have to be instantiated under a sub-node named
>> + "regulators". For SAFE_LDO voltage there is only one value of 4.9 V. LDO
>
> Is only 4.9 V valid for SAFE_LDO? If I am reading driver found in
> vendor kernel for SM-A500F it seems to to allow values between 3.3 and
> 4.95 V [1]. Same range is also written in the devicetree for the
> device [2].
>
> [1] https://github.com/msm8916-mainline/linux-downstream/blob/SM-A500F/drivers/regulator/rt5033_regulator.c#L109-L114
> [2] https://github.com/msm8916-mainline/linux-downstream/blob/SM-A500F/arch/arm/boot/dts/samsung/msm8916/msm8916-sec-a5u-eur-r01.dtsi#L148-L149
>

You're right, the RT5033 regulator SAFE_LDO is capable of more than one
voltage.

In the mainline rt5033-regulator driver, however, for SAFE_LDO only the
one voltage at 4.9 V is implemented so far.
https://github.com/torvalds/linux/blob/v6.3/include/linux/mfd/rt5033-private.h#L211-L212
https://github.com/torvalds/linux/blob/v6.3/drivers/regulator/rt5033-regulator.c#L83

For the charger driver this seems sufficient.

Kind regards,
Jakob

2023-05-01 07:52:35

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v3 8/8] dt-bindings: Add rt5033 mfd, regulator and charger

On 28/04/2023 01:30, Jakob Hauser wrote:
> Add device tree binding documentation for rt5033 multifunction device, voltage
> regulator and battery charger.
>
> Cc: Beomho Seo <[email protected]>
> Cc: Chanwoo Choi <[email protected]>
> Signed-off-by: Jakob Hauser <[email protected]>


(...)

> +
> +required:
> + - monitored-battery
> +
> +additionalProperties: false
> +
> +examples:
> + - |
> + charger {
> + compatible = "richtek,rt5033-charger";
> + monitored-battery = <&battery>;
> + extcon = <&muic>;


Everything up to here looked ok, but extcon is not a hardware property.
Please do not mix adding missing bindings for existing device with
adding new properties. You should use connector for the USB port.

Best regards,
Krzysztof

2023-05-01 09:16:45

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH v3 8/8] dt-bindings: Add rt5033 mfd, regulator and charger

On Fri, Apr 28, 2023 at 1:36 AM Jakob Hauser <[email protected]> wrote:

> Add device tree binding documentation for rt5033 multifunction device, voltage
> regulator and battery charger.
>
> Cc: Beomho Seo <[email protected]>
> Cc: Chanwoo Choi <[email protected]>
> Signed-off-by: Jakob Hauser <[email protected]>
(...)
> Changes in v3:

I'm happy with the changes requested by me in v3 so:
Reviewed-by: Linus Walleij <[email protected]>

I see other reviewers have more comments.

Yours,
Linus Walleij

2023-05-01 17:42:49

by Jakob Hauser

[permalink] [raw]
Subject: Re: [PATCH v3 8/8] dt-bindings: Add rt5033 mfd, regulator and charger

Hi Krzysztof,

On 01.05.23 09:21, Krzysztof Kozlowski wrote:
> On 28/04/2023 01:30, Jakob Hauser wrote:
>> Add device tree binding documentation for rt5033 multifunction device, voltage
>> regulator and battery charger.
>>
>> Cc: Beomho Seo <[email protected]>
>> Cc: Chanwoo Choi <[email protected]>
>> Signed-off-by: Jakob Hauser <[email protected]>
>
>
> (...)
>
>> +
>> +required:
>> + - monitored-battery
>> +
>> +additionalProperties: false
>> +
>> +examples:
>> + - |
>> + charger {
>> + compatible = "richtek,rt5033-charger";
>> + monitored-battery = <&battery>;
>> + extcon = <&muic>;
>
>
> Everything up to here looked ok, but extcon is not a hardware property.
> Please do not mix adding missing bindings for existing device with
> adding new properties. You should use connector for the USB port.
>

Rob already raised this in v1. In patch 8 v3 comments below '---' I
mentioned that the extcon phandle is still there because I don't
understand what to do.

The devices using rt5033 I'm aware of:

- arch/arm64/boot/dts/qcom/msm8916-samsung-a2015-common.dtsi
used by:
- msm8916-samsung-a3u-eur.dts
- msm8916-samsung-a5u-eur.dts
- arch/arm64/boot/dts/qcom/msm8916-samsung-e2015-common.dtsi
used by:
- msm8916-samsung-e5.dts
- msm8916-samsung-e7.dts
- msm8916-samsung-grandmax.dts
- arch/arm64/boot/dts/qcom/msm8916-samsung-serranove.dts

not yet mainlined:
- arch/arm64/boot/dts/qcom/msm8916-samsung-gprime-common.dtsi
used by:
- msm8916-samsung-fortuna3g.dts
- msm8916-samsung-fortunaltezt.dts
- msm8916-samsung-gprimeltecan.dts

They all have either an SM5502 or SM5504 MUIC (Micro-USB Interface
Controller) chip installed. It reports the information what type of
connector got plugged into the USB plug. An example for the devicetree
entry can be found at the bottom of
Documentation/devicetree/bindings/extcon/siliconmitus,sm5502-muic.yaml [1].

Documentation on the connector binding you are referring to would be the
following, I guess:
Documentation/devicetree/bindings/connector/usb-connector.yaml [2].

It's not clear to me what to do. To my understanding, in the devicetree
of the device a "connector" node should be placed within the extcon/muic
node. Like this? (Connector node at the very bottom.)

i2c-muic {
compatible = "i2c-gpio";
sda-gpios = <&msmgpio 105 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>;
scl-gpios = <&msmgpio 106 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>;

pinctrl-names = "default";
pinctrl-0 = <&muic_i2c_default>;

#address-cells = <1>;
#size-cells = <0>;

muic: extcon@14 {
compatible = "siliconmitus,sm5504-muic";
reg = <0x14>;

interrupt-parent = <&msmgpio>;
interrupts = <12 IRQ_TYPE_EDGE_FALLING>;

pinctrl-names = "default";
pinctrl-0 = <&muic_irq_default>;

usb_con: connector {
compatible = "usb-b-connector";
label = "micro-USB";
type = "micro";
};
};
};

And how to set up the rt5033-charger to retrieve the information of the
extcon/muic driver in that case?

I was looking for examples but didn't find anything that helped me to
answer that question.

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/devicetree/bindings/extcon/siliconmitus,sm5502-muic.yaml?h=v6.3
[2]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/devicetree/bindings/connector/usb-connector.yaml?h=v6.3

Kind regards,
Jakob

2023-05-01 21:19:24

by Jakob Hauser

[permalink] [raw]
Subject: Re: [PATCH v3 8/8] dt-bindings: Add rt5033 mfd, regulator and charger

Hi Krzysztof,

On 01.05.23 19:35, Jakob Hauser wrote:
> On 01.05.23 09:21, Krzysztof Kozlowski wrote:
>> On 28/04/2023 01:30, Jakob Hauser wrote:
>>> Add device tree binding documentation for rt5033 multifunction
>>> device, voltage
>>> regulator and battery charger.
>>>
>>> Cc: Beomho Seo <[email protected]>
>>> Cc: Chanwoo Choi <[email protected]>
>>> Signed-off-by: Jakob Hauser <[email protected]>
>>
>>
>> (...)
>>
>>> +
>>> +required:
>>> +  - monitored-battery
>>> +
>>> +additionalProperties: false
>>> +
>>> +examples:
>>> +  - |
>>> +    charger {
>>> +        compatible = "richtek,rt5033-charger";
>>> +        monitored-battery = <&battery>;
>>> +        extcon = <&muic>;
>>
>>
>> Everything up to here looked ok, but extcon is not a hardware property.
>> Please do not mix adding missing bindings for existing device with
>> adding new properties. You should use connector for the USB port.
>>

...

> And how to set up the rt5033-charger to retrieve the information of the
> extcon/muic driver in that case?
>

...

To add more context:
According to my understanding, the extcon subsystem provides three ways
to get an extcon device [3]:
- by name
- by devicetree node
- by phandle

For rt5033-charger, the extcon device name can be different depending on
the consumer device. For the node I wouldn't know how to get from the
charger/mfd node to the extcon node, I don't see a direct relation in
case of rt5033-charger (it's no parent node or something like that).
Therefore I chose the third option: phandle.

In the rt5033-charger driver, the location of the
extcon_get_edev_by_phandle() call is shown in link [4], it gets added in
patch 6.

[3]
https://github.com/torvalds/linux/blob/v6.3/include/linux/extcon.h#L223-L229
[4]
https://github.com/Jakko3/linux/blob/rt5033-charger_v3/drivers/power/supply/rt5033_charger.c#L677

Kind regards,
Jakob

2023-05-02 11:03:26

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v3 8/8] dt-bindings: Add rt5033 mfd, regulator and charger

On 01/05/2023 23:16, Jakob Hauser wrote:
> Hi Krzysztof,
>
> On 01.05.23 19:35, Jakob Hauser wrote:
>> On 01.05.23 09:21, Krzysztof Kozlowski wrote:
>>> On 28/04/2023 01:30, Jakob Hauser wrote:
>>>> Add device tree binding documentation for rt5033 multifunction
>>>> device, voltage
>>>> regulator and battery charger.
>>>>
>>>> Cc: Beomho Seo <[email protected]>
>>>> Cc: Chanwoo Choi <[email protected]>
>>>> Signed-off-by: Jakob Hauser <[email protected]>
>>>
>>>
>>> (...)
>>>
>>>> +
>>>> +required:
>>>> +  - monitored-battery
>>>> +
>>>> +additionalProperties: false
>>>> +
>>>> +examples:
>>>> +  - |
>>>> +    charger {
>>>> +        compatible = "richtek,rt5033-charger";
>>>> +        monitored-battery = <&battery>;
>>>> +        extcon = <&muic>;
>>>
>>>
>>> Everything up to here looked ok, but extcon is not a hardware property.
>>> Please do not mix adding missing bindings for existing device with
>>> adding new properties. You should use connector for the USB port.
>>>
>
> ...
>
>> And how to set up the rt5033-charger to retrieve the information of the
>> extcon/muic driver in that case?
>>
>
> ...
>
> To add more context:
> According to my understanding, the extcon subsystem provides three ways
> to get an extcon device [3]:
> - by name
> - by devicetree node
> - by phandle
>
> For rt5033-charger, the extcon device name can be different depending on
> the consumer device. For the node I wouldn't know how to get from the
> charger/mfd node to the extcon node, I don't see a direct relation in
> case of rt5033-charger (it's no parent node or something like that).
> Therefore I chose the third option: phandle.
>
> In the rt5033-charger driver, the location of the
> extcon_get_edev_by_phandle() call is shown in link [4], it gets added in
> patch 6.
>

Hi Jakob,

I am currently busy, so I won't be able to help you and dig in your
reply. I will get to you a bit later (or maybe Rob will help here).

However please check if ports graph does not solve your case:
https://lore.kernel.org/all/[email protected]/

It is already used for orientation and usb-role-switch (which should
solve the need for extcon, AFAIR).

If it does not help, ping me again, and I'll try to get to you a bit later.

Apologies for this, just very busy times. :)

Best regards,
Krzysztof

2023-05-03 19:35:33

by Jakob Hauser

[permalink] [raw]
Subject: Re: [PATCH v3 8/8] dt-bindings: Add rt5033 mfd, regulator and charger

Hi Krzysztof, Hi all,

On 02.05.23 12:59, Krzysztof Kozlowski wrote:
...
> Apologies for this, just very busy times. :)
>

Thanks for letting me know. Take the time you need.

Writing towards the list:

I think there is a misunderstanding here.

The connector node provides information about the installed USB
hardware. E.g. property "usb-role-switch" means "Indicates that the
device is capable of assigning the USB data role (USB host or USB
device) for a given USB connector [...]" [5]. To my understanding, in
relation with a port node this actually says that this port has this
capability. This is not relevant to the rt5033-charger driver.

The rt5033-charger driver needs to pair with the extcon chip because it
needs the information about *external* connectors being attached [6].

Extcon devices like SM5502 or SM5504 are real hardware. I'm not adding
new properties. The way of getting an excton device from the devicetree
by phandle is part of the extcon subsystem:
- function to get the excton device by phandle: [7]
- line that's looking for the property "extcon": [8]

The connector node is the wrong approach, as far as I can tell on my
current state of knowledge. It doesn't provide the information needed by
the rt5033-charger driver.

[5]
https://github.com/torvalds/linux/blob/v6.3/Documentation/devicetree/bindings/usb/usb-drd.yaml#L51-L55
[6]
https://github.com/torvalds/linux/blob/v6.3/include/linux/extcon.h#L32-L60
[7]
https://github.com/torvalds/linux/blob/v6.3/drivers/extcon/extcon.c#L1361-L1392
[8]
https://github.com/torvalds/linux/blob/v6.3/drivers/extcon/extcon.c#L1381

Kind regards,
Jakob

2023-05-05 11:57:34

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH v3 5/8] power: supply: rt5033_charger: Add RT5033 charger device driver

On Fri, Apr 28, 2023 at 1:30 AM Jakob Hauser <[email protected]> wrote:

> 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 <[email protected]>
> Cc: Chanwoo Choi <[email protected]>
> Tested-by: Raymond Hackley <[email protected]>
> Signed-off-by: Jakob Hauser <[email protected]>

Reviewed-by: Linus Walleij <[email protected]>

> + ret = power_supply_get_battery_info(charger->psy, &info);
> + if (ret) {
> + dev_err(charger->dev, "failed to get battery info\n");
> + return ERR_PTR(-EINVAL);
> + }
> +
> + /* 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;

Code reuse, exactly how it should be! :)

Yours,
Linus Walleij

2023-05-05 20:33:20

by Rob Herring

[permalink] [raw]
Subject: Re: [PATCH v3 8/8] dt-bindings: Add rt5033 mfd, regulator and charger

On Wed, May 03, 2023 at 09:33:49PM +0200, Jakob Hauser wrote:
> Hi Krzysztof, Hi all,
>
> On 02.05.23 12:59, Krzysztof Kozlowski wrote:
> ...
> > Apologies for this, just very busy times. :)
> >
>
> Thanks for letting me know. Take the time you need.
>
> Writing towards the list:
>
> I think there is a misunderstanding here.
>
> The connector node provides information about the installed USB hardware.
> E.g. property "usb-role-switch" means "Indicates that the device is capable
> of assigning the USB data role (USB host or USB device) for a given USB
> connector [...]" [5]. To my understanding, in relation with a port node this
> actually says that this port has this capability. This is not relevant to
> the rt5033-charger driver.
>
> The rt5033-charger driver needs to pair with the extcon chip because it
> needs the information about *external* connectors being attached [6].
>
> Extcon devices like SM5502 or SM5504 are real hardware. I'm not adding new
> properties. The way of getting an excton device from the devicetree by
> phandle is part of the extcon subsystem:
> - function to get the excton device by phandle: [7]
> - line that's looking for the property "extcon": [8]

extcon as a binding is inadequate for handling the increasing
complexities of connectors. Whether we need another property to link
things to connector nodes, perhaps.


> The connector node is the wrong approach, as far as I can tell on my current
> state of knowledge. It doesn't provide the information needed by the
> rt5033-charger driver.

What information is that?

You need information from the DT or run-time information from the
'extcon chip driver'? In the latter case, I'd expect the charger driver
to get its connector node (either TBD phandle or search the DT if
there's only 1 possible connector), and from that get the driver
controlling the connector.

Rob

2023-05-06 11:00:45

by Jakob Hauser

[permalink] [raw]
Subject: Re: [PATCH v3 8/8] dt-bindings: Add rt5033 mfd, regulator and charger

Hi Rob,

On 05.05.23 22:13, Rob Herring wrote:
> On Wed, May 03, 2023 at 09:33:49PM +0200, Jakob Hauser wrote:
>> Hi Krzysztof, Hi all,
>>
>> On 02.05.23 12:59, Krzysztof Kozlowski wrote:
>> ...
>>> Apologies for this, just very busy times. :)
>>>
>>
>> Thanks for letting me know. Take the time you need.
>>
>> Writing towards the list:
>>
>> I think there is a misunderstanding here.
>>
>> The connector node provides information about the installed USB hardware.
>> E.g. property "usb-role-switch" means "Indicates that the device is capable
>> of assigning the USB data role (USB host or USB device) for a given USB
>> connector [...]" [5]. To my understanding, in relation with a port node this
>> actually says that this port has this capability. This is not relevant to
>> the rt5033-charger driver.
>>
>> The rt5033-charger driver needs to pair with the extcon chip because it
>> needs the information about *external* connectors being attached [6].
>>
>> Extcon devices like SM5502 or SM5504 are real hardware. I'm not adding new
>> properties. The way of getting an excton device from the devicetree by
>> phandle is part of the extcon subsystem:
>> - function to get the excton device by phandle: [7]
>> - line that's looking for the property "extcon": [8]
>
> extcon as a binding is inadequate for handling the increasing
> complexities of connectors. Whether we need another property to link
> things to connector nodes, perhaps.

Thanks for clarifying.

>> The connector node is the wrong approach, as far as I can tell on my current
>> state of knowledge. It doesn't provide the information needed by the
>> rt5033-charger driver.
>
> What information is that?
>
> You need information from the DT or run-time information from the
> 'extcon chip driver'? In the latter case, I'd expect the charger driver
> to get its connector node (either TBD phandle or search the DT if
> there's only 1 possible connector), and from that get the driver
> controlling the connector.

Yes, the latter case: run-time information from the 'extcon chip driver'.

Hm, so I need to add a connector node below the extcon node, search for
that connector and go one level up to get the extcon. In the specific
case that's an unnecessary detour, I'm not too happy about it :/ Though
I understand that in a broader perspective the connector thing can be
more stringent.

I'll prepare something like this for v4 then...

Kind regard,
Jakob