* [PATCH v2 0/4] power: supply: add support for Pegatron Chagall battery @ 2025-04-13 11:10 Svyatoslav Ryhel 2025-04-13 11:10 ` [PATCH v2 1/4] dt-bindings: vendor-prefixes: add prefix for Pegatron Corporation Svyatoslav Ryhel ` (3 more replies) 0 siblings, 4 replies; 10+ messages in thread From: Svyatoslav Ryhel @ 2025-04-13 11:10 UTC (permalink / raw) To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Thierry Reding, Jonathan Hunter, Neil Armstrong, Jonathan Cameron, Heiko Stuebner, Rafał Miłecki, Aradhya Bhatia, Svyatoslav Ryhel Cc: linux-pm, devicetree, linux-kernel, linux-tegra The Pegatron Chagall is an Android tablet utilizing a customized Cypress CG7153AM microcontroller (MCU) as its battery fuel gauge. It supports a single-cell battery and features a dual-color charging LED. --- Changes in v2: - removed CG7153AM mentions in code, documentation and commit messages - moved schema to power/supply - left only pegatron,chagall compatible, other is dropped --- Svyatoslav Ryhel (4): dt-bindings: vendor-prefixes: add prefix for Pegatron Corporation dt-bindings: power: supply: Document Pegatron Chagall fuel gauge power/supply: Add driver for Pegatron Chagall battery ARM: tegra: chagall: Add embedded controller node .../power/supply/pegatron,chagall-ec.yaml | 49 +++ .../devicetree/bindings/vendor-prefixes.yaml | 2 + .../dts/nvidia/tegra30-pegatron-chagall.dts | 16 + drivers/power/supply/Kconfig | 12 + drivers/power/supply/Makefile | 1 + drivers/power/supply/chagall-battery.c | 308 ++++++++++++++++++ 6 files changed, 388 insertions(+) create mode 100644 Documentation/devicetree/bindings/power/supply/pegatron,chagall-ec.yaml create mode 100644 drivers/power/supply/chagall-battery.c -- 2.43.0 ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v2 1/4] dt-bindings: vendor-prefixes: add prefix for Pegatron Corporation 2025-04-13 11:10 [PATCH v2 0/4] power: supply: add support for Pegatron Chagall battery Svyatoslav Ryhel @ 2025-04-13 11:10 ` Svyatoslav Ryhel 2025-04-13 11:10 ` [PATCH v2 2/4] dt-bindings: power: supply: Document Pegatron Chagall fuel gauge Svyatoslav Ryhel ` (2 subsequent siblings) 3 siblings, 0 replies; 10+ messages in thread From: Svyatoslav Ryhel @ 2025-04-13 11:10 UTC (permalink / raw) To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Thierry Reding, Jonathan Hunter, Neil Armstrong, Jonathan Cameron, Heiko Stuebner, Rafał Miłecki, Aradhya Bhatia, Svyatoslav Ryhel Cc: linux-pm, devicetree, linux-kernel, linux-tegra PEGATRON Corporation is a Taiwanese electronics manufacturing company that mainly develops computing, communications and consumer electronics for branded vendors. Link https://www.pegatroncorp.com/ Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com> Acked-by: Rob Herring (Arm) <robh@kernel.org> --- Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Documentation/devicetree/bindings/vendor-prefixes.yaml b/Documentation/devicetree/bindings/vendor-prefixes.yaml index da01616802c7..d36389aa4d7b 100644 --- a/Documentation/devicetree/bindings/vendor-prefixes.yaml +++ b/Documentation/devicetree/bindings/vendor-prefixes.yaml @@ -1146,6 +1146,8 @@ patternProperties: description: Parallax Inc. "^pda,.*": description: Precision Design Associates, Inc. + "^pegatron,.*": + description: Pegatron Corporation "^pericom,.*": description: Pericom Technology Inc. "^pervasive,.*": -- 2.43.0 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v2 2/4] dt-bindings: power: supply: Document Pegatron Chagall fuel gauge 2025-04-13 11:10 [PATCH v2 0/4] power: supply: add support for Pegatron Chagall battery Svyatoslav Ryhel 2025-04-13 11:10 ` [PATCH v2 1/4] dt-bindings: vendor-prefixes: add prefix for Pegatron Corporation Svyatoslav Ryhel @ 2025-04-13 11:10 ` Svyatoslav Ryhel 2025-04-15 21:48 ` Rob Herring (Arm) 2025-04-13 11:10 ` [PATCH v2 3/4] power/supply: Add driver for Pegatron Chagall battery Svyatoslav Ryhel 2025-04-13 11:10 ` [PATCH v2 4/4] ARM: tegra: chagall: Add embedded controller node Svyatoslav Ryhel 3 siblings, 1 reply; 10+ messages in thread From: Svyatoslav Ryhel @ 2025-04-13 11:10 UTC (permalink / raw) To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Thierry Reding, Jonathan Hunter, Neil Armstrong, Jonathan Cameron, Heiko Stuebner, Rafał Miłecki, Aradhya Bhatia, Svyatoslav Ryhel Cc: linux-pm, devicetree, linux-kernel, linux-tegra Add binding for Pegatron Chagall tablets battery monitor. Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com> --- .../power/supply/pegatron,chagall-ec.yaml | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Documentation/devicetree/bindings/power/supply/pegatron,chagall-ec.yaml diff --git a/Documentation/devicetree/bindings/power/supply/pegatron,chagall-ec.yaml b/Documentation/devicetree/bindings/power/supply/pegatron,chagall-ec.yaml new file mode 100644 index 000000000000..defb0861e268 --- /dev/null +++ b/Documentation/devicetree/bindings/power/supply/pegatron,chagall-ec.yaml @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/power/supply/pegatron,chagall-ec.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Pegatron Chagall EC + +maintainers: + - Svyatoslav Ryhel <clamor95@gmail.com> + +description: + Pegatron Chagall EC is based on an 8-bit programmable microcontroller from + Infineon/Cypress Semiconductor, it communicates over I2C and is used in the + Pegatron Chagall tablet for fuel gauge and battery control functions. + +$ref: /schemas/power/supply/power-supply.yaml + +properties: + compatible: + const: pegatron,chagall-ec + + reg: + maxItems: 1 + + monitored-battery: true + power-supplies: true + +required: + - compatible + - reg + +unevaluatedProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + embedded-controller@10 { + compatible = "pegatron,chagall-ec"; + reg = <0x10>; + + monitored-battery = <&battery>; + power-supplies = <&mains>; + }; + }; +... -- 2.43.0 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v2 2/4] dt-bindings: power: supply: Document Pegatron Chagall fuel gauge 2025-04-13 11:10 ` [PATCH v2 2/4] dt-bindings: power: supply: Document Pegatron Chagall fuel gauge Svyatoslav Ryhel @ 2025-04-15 21:48 ` Rob Herring (Arm) 0 siblings, 0 replies; 10+ messages in thread From: Rob Herring (Arm) @ 2025-04-15 21:48 UTC (permalink / raw) To: Svyatoslav Ryhel Cc: Thierry Reding, devicetree, Sebastian Reichel, Krzysztof Kozlowski, Heiko Stuebner, linux-pm, linux-kernel, Rafał Miłecki, Neil Armstrong, linux-tegra, Conor Dooley, Jonathan Cameron, Aradhya Bhatia, Jonathan Hunter On Sun, 13 Apr 2025 14:10:31 +0300, Svyatoslav Ryhel wrote: > Add binding for Pegatron Chagall tablets battery monitor. > > Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com> > --- > .../power/supply/pegatron,chagall-ec.yaml | 49 +++++++++++++++++++ > 1 file changed, 49 insertions(+) > create mode 100644 Documentation/devicetree/bindings/power/supply/pegatron,chagall-ec.yaml > Reviewed-by: Rob Herring (Arm) <robh@kernel.org> ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v2 3/4] power/supply: Add driver for Pegatron Chagall battery 2025-04-13 11:10 [PATCH v2 0/4] power: supply: add support for Pegatron Chagall battery Svyatoslav Ryhel 2025-04-13 11:10 ` [PATCH v2 1/4] dt-bindings: vendor-prefixes: add prefix for Pegatron Corporation Svyatoslav Ryhel 2025-04-13 11:10 ` [PATCH v2 2/4] dt-bindings: power: supply: Document Pegatron Chagall fuel gauge Svyatoslav Ryhel @ 2025-04-13 11:10 ` Svyatoslav Ryhel 2025-04-13 15:15 ` ALOK TIWARI 2025-04-28 23:02 ` Sebastian Reichel 2025-04-13 11:10 ` [PATCH v2 4/4] ARM: tegra: chagall: Add embedded controller node Svyatoslav Ryhel 3 siblings, 2 replies; 10+ messages in thread From: Svyatoslav Ryhel @ 2025-04-13 11:10 UTC (permalink / raw) To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Thierry Reding, Jonathan Hunter, Neil Armstrong, Jonathan Cameron, Heiko Stuebner, Rafał Miłecki, Aradhya Bhatia, Svyatoslav Ryhel Cc: linux-pm, devicetree, linux-kernel, linux-tegra The Pegatron Chagall is an Android tablet utilizing a customized Cypress CG7153AM microcontroller (MCU) as its battery fuel gauge. It supports a single-cell battery and features a dual-color charging LED. Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com> --- drivers/power/supply/Kconfig | 12 + drivers/power/supply/Makefile | 1 + drivers/power/supply/chagall-battery.c | 308 +++++++++++++++++++++++++ 3 files changed, 321 insertions(+) create mode 100644 drivers/power/supply/chagall-battery.c diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 9f2eef6787f7..d0fc9db524bd 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -107,6 +107,18 @@ config BATTERY_ACT8945A Say Y here to enable support for power supply provided by Active-semi ActivePath ACT8945A charger. +config BATTERY_CHAGALL + tristate "Pegatron Chagall battery driver" + depends on I2C + depends on LEDS_CLASS + help + Say Y to include support for Cypress CG7153AM IC based battery + fuel gauge with custom firmware found in Pegatron Chagall based + tablet line. + + This driver can also be built as a module. If so, the module will be + called chagall-battery. + config BATTERY_CPCAP tristate "Motorola CPCAP PMIC battery driver" depends on MFD_CPCAP && IIO diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index 59c4a9f40d28..4ecf48a33fdd 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -23,6 +23,7 @@ obj-$(CONFIG_CHARGER_ADP5061) += adp5061.o obj-$(CONFIG_BATTERY_ACT8945A) += act8945a_charger.o obj-$(CONFIG_BATTERY_AXP20X) += axp20x_battery.o obj-$(CONFIG_CHARGER_AXP20X) += axp20x_ac_power.o +obj-$(CONFIG_BATTERY_CHAGALL) += chagall-battery.o obj-$(CONFIG_BATTERY_CPCAP) += cpcap-battery.o obj-$(CONFIG_BATTERY_CW2015) += cw2015_battery.o obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o diff --git a/drivers/power/supply/chagall-battery.c b/drivers/power/supply/chagall-battery.c new file mode 100644 index 000000000000..1a278331efe7 --- /dev/null +++ b/drivers/power/supply/chagall-battery.c @@ -0,0 +1,308 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include <linux/array_size.h> +#include <linux/delay.h> +#include <linux/devm-helpers.h> +#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/leds.h> +#include <linux/mod_devicetable.h> +#include <linux/module.h> +#include <linux/power_supply.h> +#include <linux/regmap.h> + +#define CHAGALL_REG_LED_AMBER 0x60 +#define CHAGALL_REG_LED_WHITE 0x70 +#define CHAGALL_REG_BATTERY_TEMPERATURE 0xa2 +#define CHAGALL_REG_BATTERY_VOLTAGE 0xa4 +#define CHAGALL_REG_BATTERY_CURRENT 0xa6 +#define CHAGALL_REG_BATTERY_CAPACITY 0xa8 +#define CHAGALL_REG_BATTERY_CHARGING_CURRENT 0xaa +#define CHAGALL_REG_BATTERY_CHARGING_VOLTAGE 0xac +#define CHAGALL_REG_BATTERY_STATUS 0xae +#define BATTERY_DISCHARGING BIT(6) +#define BATTERY_FULL_CHARGED BIT(5) +#define BATTERY_FULL_DISCHARGED BIT(4) +#define CHAGALL_REG_BATTERY_REMAIN_CAPACITY 0xb0 +#define CHAGALL_REG_BATTERY_FULL_CAPACITY 0xb2 +#define CHAGALL_REG_MAX_COUNT 0xb4 + +#define CHAGALL_BATTERY_DATA_REFRESH 5000 +#define TEMP_CELSIUS_OFFSET 2731 + +static const struct regmap_config chagall_battery_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = CHAGALL_REG_MAX_COUNT, + .reg_format_endian = REGMAP_ENDIAN_LITTLE, + .val_format_endian = REGMAP_ENDIAN_LITTLE, +}; + +struct chagall_battery_data { + struct regmap *regmap; + struct led_classdev amber_led; + struct led_classdev white_led; + struct power_supply *battery; + struct delayed_work poll_work; + u16 last_state; +}; + +static void chagall_led_set_brightness_amber(struct led_classdev *led, + enum led_brightness brightness) +{ + struct chagall_battery_data *cg = + container_of(led, struct chagall_battery_data, amber_led); + + regmap_write(cg->regmap, CHAGALL_REG_LED_AMBER, brightness); +} + +static void chagall_led_set_brightness_white(struct led_classdev *led, + enum led_brightness brightness) +{ + struct chagall_battery_data *cg = + container_of(led, struct chagall_battery_data, white_led); + + regmap_write(cg->regmap, CHAGALL_REG_LED_WHITE, brightness); +} + +static void chagall_leds_status_update(struct chagall_battery_data *cg, int state) +{ + switch (state) { + case POWER_SUPPLY_STATUS_FULL: + led_set_brightness(&cg->amber_led, LED_OFF); + led_set_brightness(&cg->white_led, LED_ON); + break; + + case POWER_SUPPLY_STATUS_CHARGING: + led_set_brightness(&cg->white_led, LED_OFF); + led_set_brightness(&cg->amber_led, LED_ON); + break; + + default: + led_set_brightness(&cg->amber_led, LED_OFF); + led_set_brightness(&cg->white_led, LED_OFF); + break; + } +} + +static const enum power_supply_property chagall_battery_properties[] = { + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_VOLTAGE_MAX, + POWER_SUPPLY_PROP_CURRENT_NOW, + POWER_SUPPLY_PROP_CURRENT_MAX, + POWER_SUPPLY_PROP_CAPACITY, + POWER_SUPPLY_PROP_TEMP, + POWER_SUPPLY_PROP_CHARGE_FULL, + POWER_SUPPLY_PROP_CHARGE_NOW, +}; + +static const unsigned int chagall_battery_prop_offs[] = { + [POWER_SUPPLY_PROP_TEMP] = CHAGALL_REG_BATTERY_TEMPERATURE, + [POWER_SUPPLY_PROP_VOLTAGE_NOW] = CHAGALL_REG_BATTERY_VOLTAGE, + [POWER_SUPPLY_PROP_CURRENT_NOW] = CHAGALL_REG_BATTERY_CURRENT, + [POWER_SUPPLY_PROP_CAPACITY] = CHAGALL_REG_BATTERY_CAPACITY, + [POWER_SUPPLY_PROP_CURRENT_MAX] = CHAGALL_REG_BATTERY_CHARGING_CURRENT, + [POWER_SUPPLY_PROP_VOLTAGE_MAX] = CHAGALL_REG_BATTERY_CHARGING_VOLTAGE, + [POWER_SUPPLY_PROP_STATUS] = CHAGALL_REG_BATTERY_STATUS, + [POWER_SUPPLY_PROP_CHARGE_NOW] = CHAGALL_REG_BATTERY_REMAIN_CAPACITY, + [POWER_SUPPLY_PROP_CHARGE_FULL] = CHAGALL_REG_BATTERY_FULL_CAPACITY, +}; + +static int chagall_battery_get_value(struct chagall_battery_data *cg, + enum power_supply_property psp, u32 *val) +{ + if (psp >= ARRAY_SIZE(chagall_battery_prop_offs)) + return -EINVAL; + if (!chagall_battery_prop_offs[psp]) + return -EINVAL; + + /* Battery data is stored in 2 consecutive registers with little-endian */ + return regmap_bulk_read(cg->regmap, chagall_battery_prop_offs[psp], val, 2); +} + +static int chagall_battery_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct chagall_battery_data *cg = power_supply_get_drvdata(psy); + int ret; + + switch (psp) { + case POWER_SUPPLY_PROP_PRESENT: + val->intval = 1; + break; + + default: + ret = chagall_battery_get_value(cg, psp, &val->intval); + if (ret) + return ret; + + switch (psp) { + case POWER_SUPPLY_PROP_TEMP: + val->intval -= TEMP_CELSIUS_OFFSET; + break; + + case POWER_SUPPLY_PROP_VOLTAGE_MAX: + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + case POWER_SUPPLY_PROP_CURRENT_MAX: + case POWER_SUPPLY_PROP_CURRENT_NOW: + case POWER_SUPPLY_PROP_CHARGE_FULL: + case POWER_SUPPLY_PROP_CHARGE_NOW: + val->intval *= 1000; + break; + + case POWER_SUPPLY_PROP_STATUS: + if (val->intval & BATTERY_FULL_CHARGED) + val->intval = POWER_SUPPLY_STATUS_FULL; + else if (val->intval & BATTERY_FULL_DISCHARGED) + val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; + else if (val->intval & BATTERY_DISCHARGING) + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; + else + val->intval = POWER_SUPPLY_STATUS_CHARGING; + break; + + default: + break; + } + + break; + } + + return 0; +} + +static void chagall_battery_poll_work(struct work_struct *work) +{ + struct chagall_battery_data *cg = + container_of(work, struct chagall_battery_data, poll_work.work); + u32 state; + int ret; + + ret = chagall_battery_get_value(cg, POWER_SUPPLY_PROP_STATUS, &state); + if (ret) + return; + + if (state & BATTERY_FULL_CHARGED) + state = POWER_SUPPLY_STATUS_FULL; + else if (state & BATTERY_DISCHARGING) + state = POWER_SUPPLY_STATUS_DISCHARGING; + else + state = POWER_SUPPLY_STATUS_CHARGING; + + if (cg->last_state != state) { + cg->last_state = state; + power_supply_changed(cg->battery); + } + + chagall_leds_status_update(cg, state); + + /* continuously send uevent notification */ + schedule_delayed_work(&cg->poll_work, + msecs_to_jiffies(CHAGALL_BATTERY_DATA_REFRESH)); +} + +static const struct power_supply_desc chagall_battery_desc = { + .name = "chagall-battery", + .type = POWER_SUPPLY_TYPE_BATTERY, + .properties = chagall_battery_properties, + .num_properties = ARRAY_SIZE(chagall_battery_properties), + .get_property = chagall_battery_get_property, + .external_power_changed = power_supply_changed, +}; + +static int chagall_battery_probe(struct i2c_client *client) +{ + struct chagall_battery_data *cg; + struct device *dev = &client->dev; + struct power_supply_config cfg = { }; + int ret; + + cg = devm_kzalloc(dev, sizeof(*cg), GFP_KERNEL); + if (!cg) + return -ENOMEM; + + cfg.drv_data = cg; + cfg.fwnode = dev_fwnode(dev); + + i2c_set_clientdata(client, cg); + + cg->regmap = devm_regmap_init_i2c(client, &chagall_battery_regmap_config); + if (IS_ERR(cg->regmap)) + return dev_err_probe(dev, PTR_ERR(cg->regmap), "cannot allocate regmap\n"); + + cg->last_state = POWER_SUPPLY_STATUS_UNKNOWN; + cg->battery = devm_power_supply_register(dev, &chagall_battery_desc, &cfg); + if (IS_ERR(cg->battery)) + return dev_err_probe(dev, PTR_ERR(cg->battery), "failed to register power supply\n"); + + cg->amber_led.name = "power::amber"; + cg->amber_led.max_brightness = 1; + cg->amber_led.flags = LED_CORE_SUSPENDRESUME; + cg->amber_led.brightness_set = chagall_led_set_brightness_amber; + + ret = devm_led_classdev_register(dev, &cg->amber_led); + if (ret) + return dev_err_probe(dev, ret, "failed to register amber LED\n"); + + cg->white_led.name = "power::white"; + cg->white_led.max_brightness = 1; + cg->white_led.flags = LED_CORE_SUSPENDRESUME; + cg->white_led.brightness_set = chagall_led_set_brightness_white; + + ret = devm_led_classdev_register(dev, &cg->white_led); + if (ret) + return dev_err_probe(dev, ret, "failed to register white LED\n"); + + led_set_brightness(&cg->amber_led, LED_OFF); + led_set_brightness(&cg->white_led, LED_OFF); + + ret = devm_delayed_work_autocancel(dev, &cg->poll_work, chagall_battery_poll_work); + if (ret) + return ret; + + schedule_delayed_work(&cg->poll_work, msecs_to_jiffies(CHAGALL_BATTERY_DATA_REFRESH)); + return 0; +} + +static int __maybe_unused chagall_battery_suspend(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + struct chagall_battery_data *cg = i2c_get_clientdata(client); + + cancel_delayed_work_sync(&cg->poll_work); + return 0; +} + +static int __maybe_unused chagall_battery_resume(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + struct chagall_battery_data *cg = i2c_get_clientdata(client); + + schedule_delayed_work(&cg->poll_work, msecs_to_jiffies(CHAGALL_BATTERY_DATA_REFRESH)); + return 0; +} + +static SIMPLE_DEV_PM_OPS(chagall_battery_pm_ops, + chagall_battery_suspend, chagall_battery_resume); + +static const struct of_device_id chagall_of_match[] = { + { .compatible = "pegatron,chagall-ec" }, + { } +}; +MODULE_DEVICE_TABLE(of, chagall_of_match); + +static struct i2c_driver chagall_battery_driver = { + .driver = { + .name = "chagall-battery", + .pm = &chagall_battery_pm_ops, + .of_match_table = chagall_of_match, + }, + .probe = chagall_battery_probe, +}; +module_i2c_driver(chagall_battery_driver); + +MODULE_AUTHOR("Svyatoslav Ryhel <clamor95@gmail.com>"); +MODULE_DESCRIPTION("Pegatron Chagall fuel gauge driver"); +MODULE_LICENSE("GPL"); -- 2.43.0 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v2 3/4] power/supply: Add driver for Pegatron Chagall battery 2025-04-13 11:10 ` [PATCH v2 3/4] power/supply: Add driver for Pegatron Chagall battery Svyatoslav Ryhel @ 2025-04-13 15:15 ` ALOK TIWARI 2025-04-13 15:20 ` Svyatoslav Ryhel 2025-04-28 23:02 ` Sebastian Reichel 1 sibling, 1 reply; 10+ messages in thread From: ALOK TIWARI @ 2025-04-13 15:15 UTC (permalink / raw) To: Svyatoslav Ryhel, Sebastian Reichel, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Thierry Reding, Jonathan Hunter, Neil Armstrong, Jonathan Cameron, Heiko Stuebner, Rafał Miłecki, Aradhya Bhatia Cc: linux-pm, devicetree, linux-kernel, linux-tegra subject -> "power: supply:" On 13-04-2025 16:40, Svyatoslav Ryhel wrote: > The Pegatron Chagall is an Android tablet utilizing a customized Cypress > CG7153AM microcontroller (MCU) as its battery fuel gauge. It supports a > single-cell battery and features a dual-color charging LED. > > Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com> > --- > drivers/power/supply/Kconfig | 12 + > drivers/power/supply/Makefile | 1 + > drivers/power/supply/chagall-battery.c | 308 +++++++++++++++++++++++++ > 3 files changed, 321 insertions(+) > create mode 100644 drivers/power/supply/chagall-battery.c > > diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig > index 9f2eef6787f7..d0fc9db524bd 100644 > --- a/drivers/power/supply/Kconfig > +++ b/drivers/power/supply/Kconfig > @@ -107,6 +107,18 @@ config BATTERY_ACT8945A > Say Y here to enable support for power supply provided by > Active-semi ActivePath ACT8945A charger. > > +config BATTERY_CHAGALL > + tristate "Pegatron Chagall battery driver" > + depends on I2C > + depends on LEDS_CLASS > + help > + Say Y to include support for Cypress CG7153AM IC based battery > + fuel gauge with custom firmware found in Pegatron Chagall based > + tablet line. > + > + This driver can also be built as a module. If so, the module will be > + called chagall-battery. > + > config BATTERY_CPCAP > tristate "Motorola CPCAP PMIC battery driver" > depends on MFD_CPCAP && IIO > diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile > index 59c4a9f40d28..4ecf48a33fdd 100644 > --- a/drivers/power/supply/Makefile > +++ b/drivers/power/supply/Makefile > @@ -23,6 +23,7 @@ obj-$(CONFIG_CHARGER_ADP5061) += adp5061.o > obj-$(CONFIG_BATTERY_ACT8945A) += act8945a_charger.o > obj-$(CONFIG_BATTERY_AXP20X) += axp20x_battery.o > obj-$(CONFIG_CHARGER_AXP20X) += axp20x_ac_power.o > +obj-$(CONFIG_BATTERY_CHAGALL) += chagall-battery.o > obj-$(CONFIG_BATTERY_CPCAP) += cpcap-battery.o > obj-$(CONFIG_BATTERY_CW2015) += cw2015_battery.o > obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o > diff --git a/drivers/power/supply/chagall-battery.c b/drivers/power/supply/chagall-battery.c > new file mode 100644 > index 000000000000..1a278331efe7 > --- /dev/null > +++ b/drivers/power/supply/chagall-battery.c > @@ -0,0 +1,308 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later Blank line require > +#include <linux/array_size.h> > +#include <linux/delay.h> > +#include <linux/devm-helpers.h> > +#include <linux/err.h> > +#include <linux/i2c.h> > +#include <linux/leds.h> > +#include <linux/mod_devicetable.h> > +#include <linux/module.h> > +#include <linux/power_supply.h> > +#include <linux/regmap.h> > + > +#define CHAGALL_REG_LED_AMBER 0x60 > +#define CHAGALL_REG_LED_WHITE 0x70 > +#define CHAGALL_REG_BATTERY_TEMPERATURE 0xa2 > +#define CHAGALL_REG_BATTERY_VOLTAGE 0xa4 > +#define CHAGALL_REG_BATTERY_CURRENT 0xa6 > +#define CHAGALL_REG_BATTERY_CAPACITY 0xa8 > +#define CHAGALL_REG_BATTERY_CHARGING_CURRENT 0xaa > +#define CHAGALL_REG_BATTERY_CHARGING_VOLTAGE 0xac > +#define CHAGALL_REG_BATTERY_STATUS 0xae > +#define BATTERY_DISCHARGING BIT(6) > +#define BATTERY_FULL_CHARGED BIT(5) > +#define BATTERY_FULL_DISCHARGED BIT(4) ' ' after "#define", remove extra ' ' > +#define CHAGALL_REG_BATTERY_REMAIN_CAPACITY 0xb0 > +#define CHAGALL_REG_BATTERY_FULL_CAPACITY 0xb2 > +#define CHAGALL_REG_MAX_COUNT 0xb4 > + > +#define CHAGALL_BATTERY_DATA_REFRESH 5000 > +#define TEMP_CELSIUS_OFFSET 2731 > + > +static const struct regmap_config chagall_battery_regmap_config = { > + .reg_bits = 8, > + .val_bits = 8, > + .max_register = CHAGALL_REG_MAX_COUNT, > + .reg_format_endian = REGMAP_ENDIAN_LITTLE, > + .val_format_endian = REGMAP_ENDIAN_LITTLE, > +}; > + > +struct chagall_battery_data { > + struct regmap *regmap; > + struct led_classdev amber_led; > + struct led_classdev white_led; > + struct power_supply *battery; > + struct delayed_work poll_work; > + u16 last_state; > +}; > + > +static void chagall_led_set_brightness_amber(struct led_classdev *led, > + enum led_brightness brightness) > +{ > + struct chagall_battery_data *cg = > + container_of(led, struct chagall_battery_data, amber_led); > + > + regmap_write(cg->regmap, CHAGALL_REG_LED_AMBER, brightness); > +} > + > +static void chagall_led_set_brightness_white(struct led_classdev *led, > + enum led_brightness brightness) > +{ > + struct chagall_battery_data *cg = > + container_of(led, struct chagall_battery_data, white_led); > + > + regmap_write(cg->regmap, CHAGALL_REG_LED_WHITE, brightness); > +} > + > +static void chagall_leds_status_update(struct chagall_battery_data *cg, int state) > +{ > + switch (state) { > + case POWER_SUPPLY_STATUS_FULL: > + led_set_brightness(&cg->amber_led, LED_OFF); > + led_set_brightness(&cg->white_led, LED_ON); > + break; > + no Blank line. > + case POWER_SUPPLY_STATUS_CHARGING: > + led_set_brightness(&cg->white_led, LED_OFF); > + led_set_brightness(&cg->amber_led, LED_ON); > + break; > + no Blank line. > + default: > + led_set_brightness(&cg->amber_led, LED_OFF); > + led_set_brightness(&cg->white_led, LED_OFF); > + break; > + } > +} > + > +static const enum power_supply_property chagall_battery_properties[] = { > + POWER_SUPPLY_PROP_STATUS, > + POWER_SUPPLY_PROP_PRESENT, > + POWER_SUPPLY_PROP_VOLTAGE_NOW, > + POWER_SUPPLY_PROP_VOLTAGE_MAX, > + POWER_SUPPLY_PROP_CURRENT_NOW, > + POWER_SUPPLY_PROP_CURRENT_MAX, > + POWER_SUPPLY_PROP_CAPACITY, > + POWER_SUPPLY_PROP_TEMP, > + POWER_SUPPLY_PROP_CHARGE_FULL, > + POWER_SUPPLY_PROP_CHARGE_NOW, > +}; > + > +static const unsigned int chagall_battery_prop_offs[] = { > + [POWER_SUPPLY_PROP_TEMP] = CHAGALL_REG_BATTERY_TEMPERATURE, > + [POWER_SUPPLY_PROP_VOLTAGE_NOW] = CHAGALL_REG_BATTERY_VOLTAGE, > + [POWER_SUPPLY_PROP_CURRENT_NOW] = CHAGALL_REG_BATTERY_CURRENT, > + [POWER_SUPPLY_PROP_CAPACITY] = CHAGALL_REG_BATTERY_CAPACITY, > + [POWER_SUPPLY_PROP_CURRENT_MAX] = CHAGALL_REG_BATTERY_CHARGING_CURRENT, > + [POWER_SUPPLY_PROP_VOLTAGE_MAX] = CHAGALL_REG_BATTERY_CHARGING_VOLTAGE, > + [POWER_SUPPLY_PROP_STATUS] = CHAGALL_REG_BATTERY_STATUS, > + [POWER_SUPPLY_PROP_CHARGE_NOW] = CHAGALL_REG_BATTERY_REMAIN_CAPACITY, > + [POWER_SUPPLY_PROP_CHARGE_FULL] = CHAGALL_REG_BATTERY_FULL_CAPACITY, > +}; > + > +static int chagall_battery_get_value(struct chagall_battery_data *cg, > + enum power_supply_property psp, u32 *val) > +{ > + if (psp >= ARRAY_SIZE(chagall_battery_prop_offs)) > + return -EINVAL; > + if (!chagall_battery_prop_offs[psp]) > + return -EINVAL; > + > + /* Battery data is stored in 2 consecutive registers with little-endian */ > + return regmap_bulk_read(cg->regmap, chagall_battery_prop_offs[psp], val, 2); > +} > + > +static int chagall_battery_get_property(struct power_supply *psy, > + enum power_supply_property psp, > + union power_supply_propval *val) > +{ > + struct chagall_battery_data *cg = power_supply_get_drvdata(psy); > + int ret; > + > + switch (psp) { > + case POWER_SUPPLY_PROP_PRESENT: > + val->intval = 1; > + break; > + > + default: > + ret = chagall_battery_get_value(cg, psp, &val->intval); > + if (ret) > + return ret; > + > + switch (psp) { > + case POWER_SUPPLY_PROP_TEMP: > + val->intval -= TEMP_CELSIUS_OFFSET; > + break; > + > + case POWER_SUPPLY_PROP_VOLTAGE_MAX: > + case POWER_SUPPLY_PROP_VOLTAGE_NOW: > + case POWER_SUPPLY_PROP_CURRENT_MAX: > + case POWER_SUPPLY_PROP_CURRENT_NOW: > + case POWER_SUPPLY_PROP_CHARGE_FULL: > + case POWER_SUPPLY_PROP_CHARGE_NOW: > + val->intval *= 1000; > + break; > + > + case POWER_SUPPLY_PROP_STATUS: > + if (val->intval & BATTERY_FULL_CHARGED) > + val->intval = POWER_SUPPLY_STATUS_FULL; > + else if (val->intval & BATTERY_FULL_DISCHARGED) > + val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; > + else if (val->intval & BATTERY_DISCHARGING) > + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; > + else > + val->intval = POWER_SUPPLY_STATUS_CHARGING; > + break; > + no blank line is required between a case label. > + default: > + break; > + } > + > + break; > + } > + > + return 0; > +} > + > +static void chagall_battery_poll_work(struct work_struct *work) > +{ > + struct chagall_battery_data *cg = > + container_of(work, struct chagall_battery_data, poll_work.work); > + u32 state; > + int ret; > + > + ret = chagall_battery_get_value(cg, POWER_SUPPLY_PROP_STATUS, &state); > + if (ret) > + return; > + > + if (state & BATTERY_FULL_CHARGED) > + state = POWER_SUPPLY_STATUS_FULL; > + else if (state & BATTERY_DISCHARGING) > + state = POWER_SUPPLY_STATUS_DISCHARGING; > + else > + state = POWER_SUPPLY_STATUS_CHARGING; > + > + if (cg->last_state != state) { > + cg->last_state = state; > + power_supply_changed(cg->battery); > + } > + > + chagall_leds_status_update(cg, state); > + > + /* continuously send uevent notification */ > + schedule_delayed_work(&cg->poll_work, > + msecs_to_jiffies(CHAGALL_BATTERY_DATA_REFRESH)); > +} > + > +static const struct power_supply_desc chagall_battery_desc = { > + .name = "chagall-battery", > + .type = POWER_SUPPLY_TYPE_BATTERY, > + .properties = chagall_battery_properties, > + .num_properties = ARRAY_SIZE(chagall_battery_properties), > + .get_property = chagall_battery_get_property, > + .external_power_changed = power_supply_changed, > +}; > + > +static int chagall_battery_probe(struct i2c_client *client) > +{ > + struct chagall_battery_data *cg; > + struct device *dev = &client->dev; > + struct power_supply_config cfg = { }; > + int ret; > + > + cg = devm_kzalloc(dev, sizeof(*cg), GFP_KERNEL); > + if (!cg) > + return -ENOMEM; > + > + cfg.drv_data = cg; > + cfg.fwnode = dev_fwnode(dev); > + > + i2c_set_clientdata(client, cg); > + > + cg->regmap = devm_regmap_init_i2c(client, &chagall_battery_regmap_config); > + if (IS_ERR(cg->regmap)) > + return dev_err_probe(dev, PTR_ERR(cg->regmap), "cannot allocate regmap\n"); > + > + cg->last_state = POWER_SUPPLY_STATUS_UNKNOWN; > + cg->battery = devm_power_supply_register(dev, &chagall_battery_desc, &cfg); > + if (IS_ERR(cg->battery)) > + return dev_err_probe(dev, PTR_ERR(cg->battery), "failed to register power supply\n"); > + > + cg->amber_led.name = "power::amber"; > + cg->amber_led.max_brightness = 1; > + cg->amber_led.flags = LED_CORE_SUSPENDRESUME; > + cg->amber_led.brightness_set = chagall_led_set_brightness_amber; > + > + ret = devm_led_classdev_register(dev, &cg->amber_led); > + if (ret) > + return dev_err_probe(dev, ret, "failed to register amber LED\n"); > + > + cg->white_led.name = "power::white"; > + cg->white_led.max_brightness = 1; > + cg->white_led.flags = LED_CORE_SUSPENDRESUME; > + cg->white_led.brightness_set = chagall_led_set_brightness_white; > + > + ret = devm_led_classdev_register(dev, &cg->white_led); > + if (ret) > + return dev_err_probe(dev, ret, "failed to register white LED\n"); > + > + led_set_brightness(&cg->amber_led, LED_OFF); > + led_set_brightness(&cg->white_led, LED_OFF); > + > + ret = devm_delayed_work_autocancel(dev, &cg->poll_work, chagall_battery_poll_work); > + if (ret) > + return ret; > + > + schedule_delayed_work(&cg->poll_work, msecs_to_jiffies(CHAGALL_BATTERY_DATA_REFRESH)); a '\n' before return is customary > + return 0; > +} > + > +static int __maybe_unused chagall_battery_suspend(struct device *dev) > +{ > + struct i2c_client *client = to_i2c_client(dev); > + struct chagall_battery_data *cg = i2c_get_clientdata(client); > + > + cancel_delayed_work_sync(&cg->poll_work); a '\n' before return > + return 0; > +} > + > +static int __maybe_unused chagall_battery_resume(struct device *dev) > +{ > + struct i2c_client *client = to_i2c_client(dev); > + struct chagall_battery_data *cg = i2c_get_clientdata(client); > + > + schedule_delayed_work(&cg->poll_work, msecs_to_jiffies(CHAGALL_BATTERY_DATA_REFRESH)); a '\n' before return is customary > + return 0; > +} > + > +static SIMPLE_DEV_PM_OPS(chagall_battery_pm_ops, > + chagall_battery_suspend, chagall_battery_resume); > + > +static const struct of_device_id chagall_of_match[] = { > + { .compatible = "pegatron,chagall-ec" }, > + { } > +}; > +MODULE_DEVICE_TABLE(of, chagall_of_match); > + > +static struct i2c_driver chagall_battery_driver = { > + .driver = { > + .name = "chagall-battery", > + .pm = &chagall_battery_pm_ops, > + .of_match_table = chagall_of_match, > + }, > + .probe = chagall_battery_probe, > +}; > +module_i2c_driver(chagall_battery_driver); > + > +MODULE_AUTHOR("Svyatoslav Ryhel <clamor95@gmail.com>"); > +MODULE_DESCRIPTION("Pegatron Chagall fuel gauge driver"); > +MODULE_LICENSE("GPL"); Thanks, Alok ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v2 3/4] power/supply: Add driver for Pegatron Chagall battery 2025-04-13 15:15 ` ALOK TIWARI @ 2025-04-13 15:20 ` Svyatoslav Ryhel 0 siblings, 0 replies; 10+ messages in thread From: Svyatoslav Ryhel @ 2025-04-13 15:20 UTC (permalink / raw) To: ALOK TIWARI Cc: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Thierry Reding, Jonathan Hunter, Neil Armstrong, Jonathan Cameron, Heiko Stuebner, Rafał Miłecki, Aradhya Bhatia, linux-pm, devicetree, linux-kernel, linux-tegra нд, 13 квіт. 2025 р. о 18:16 ALOK TIWARI <alok.a.tiwari@oracle.com> пише: > > > > subject -> "power: supply:" > > On 13-04-2025 16:40, Svyatoslav Ryhel wrote: > > The Pegatron Chagall is an Android tablet utilizing a customized Cypress > > CG7153AM microcontroller (MCU) as its battery fuel gauge. It supports a > > single-cell battery and features a dual-color charging LED. > > > > Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com> > > --- > > drivers/power/supply/Kconfig | 12 + > > drivers/power/supply/Makefile | 1 + > > drivers/power/supply/chagall-battery.c | 308 +++++++++++++++++++++++++ > > 3 files changed, 321 insertions(+) > > create mode 100644 drivers/power/supply/chagall-battery.c > > > > diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig > > index 9f2eef6787f7..d0fc9db524bd 100644 > > --- a/drivers/power/supply/Kconfig > > +++ b/drivers/power/supply/Kconfig > > @@ -107,6 +107,18 @@ config BATTERY_ACT8945A > > Say Y here to enable support for power supply provided by > > Active-semi ActivePath ACT8945A charger. > > > > +config BATTERY_CHAGALL > > + tristate "Pegatron Chagall battery driver" > > + depends on I2C > > + depends on LEDS_CLASS > > + help > > + Say Y to include support for Cypress CG7153AM IC based battery > > + fuel gauge with custom firmware found in Pegatron Chagall based > > + tablet line. > > + > > + This driver can also be built as a module. If so, the module will be > > + called chagall-battery. > > + > > config BATTERY_CPCAP > > tristate "Motorola CPCAP PMIC battery driver" > > depends on MFD_CPCAP && IIO > > diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile > > index 59c4a9f40d28..4ecf48a33fdd 100644 > > --- a/drivers/power/supply/Makefile > > +++ b/drivers/power/supply/Makefile > > @@ -23,6 +23,7 @@ obj-$(CONFIG_CHARGER_ADP5061) += adp5061.o > > obj-$(CONFIG_BATTERY_ACT8945A) += act8945a_charger.o > > obj-$(CONFIG_BATTERY_AXP20X) += axp20x_battery.o > > obj-$(CONFIG_CHARGER_AXP20X) += axp20x_ac_power.o > > +obj-$(CONFIG_BATTERY_CHAGALL) += chagall-battery.o > > obj-$(CONFIG_BATTERY_CPCAP) += cpcap-battery.o > > obj-$(CONFIG_BATTERY_CW2015) += cw2015_battery.o > > obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o > > diff --git a/drivers/power/supply/chagall-battery.c b/drivers/power/supply/chagall-battery.c > > new file mode 100644 > > index 000000000000..1a278331efe7 > > --- /dev/null > > +++ b/drivers/power/supply/chagall-battery.c > > @@ -0,0 +1,308 @@ > > +// SPDX-License-Identifier: GPL-2.0-or-later > > Blank line require > > > +#include <linux/array_size.h> > > +#include <linux/delay.h> > > +#include <linux/devm-helpers.h> > > +#include <linux/err.h> > > +#include <linux/i2c.h> > > +#include <linux/leds.h> > > +#include <linux/mod_devicetable.h> > > +#include <linux/module.h> > > +#include <linux/power_supply.h> > > +#include <linux/regmap.h> > > + > > +#define CHAGALL_REG_LED_AMBER 0x60 > > +#define CHAGALL_REG_LED_WHITE 0x70 > > +#define CHAGALL_REG_BATTERY_TEMPERATURE 0xa2 > > +#define CHAGALL_REG_BATTERY_VOLTAGE 0xa4 > > +#define CHAGALL_REG_BATTERY_CURRENT 0xa6 > > +#define CHAGALL_REG_BATTERY_CAPACITY 0xa8 > > +#define CHAGALL_REG_BATTERY_CHARGING_CURRENT 0xaa > > +#define CHAGALL_REG_BATTERY_CHARGING_VOLTAGE 0xac > > +#define CHAGALL_REG_BATTERY_STATUS 0xae > > +#define BATTERY_DISCHARGING BIT(6) > > +#define BATTERY_FULL_CHARGED BIT(5) > > +#define BATTERY_FULL_DISCHARGED BIT(4) > > ' ' after "#define", remove extra ' ' > > > +#define CHAGALL_REG_BATTERY_REMAIN_CAPACITY 0xb0 > > +#define CHAGALL_REG_BATTERY_FULL_CAPACITY 0xb2 > > +#define CHAGALL_REG_MAX_COUNT 0xb4 > > + > > +#define CHAGALL_BATTERY_DATA_REFRESH 5000 > > +#define TEMP_CELSIUS_OFFSET 2731 > > + > > +static const struct regmap_config chagall_battery_regmap_config = { > > + .reg_bits = 8, > > + .val_bits = 8, > > + .max_register = CHAGALL_REG_MAX_COUNT, > > + .reg_format_endian = REGMAP_ENDIAN_LITTLE, > > + .val_format_endian = REGMAP_ENDIAN_LITTLE, > > +}; > > + > > +struct chagall_battery_data { > > + struct regmap *regmap; > > + struct led_classdev amber_led; > > + struct led_classdev white_led; > > + struct power_supply *battery; > > + struct delayed_work poll_work; > > + u16 last_state; > > +}; > > + > > +static void chagall_led_set_brightness_amber(struct led_classdev *led, > > + enum led_brightness brightness) > > +{ > > + struct chagall_battery_data *cg = > > + container_of(led, struct chagall_battery_data, amber_led); > > + > > + regmap_write(cg->regmap, CHAGALL_REG_LED_AMBER, brightness); > > +} > > + > > +static void chagall_led_set_brightness_white(struct led_classdev *led, > > + enum led_brightness brightness) > > +{ > > + struct chagall_battery_data *cg = > > + container_of(led, struct chagall_battery_data, white_led); > > + > > + regmap_write(cg->regmap, CHAGALL_REG_LED_WHITE, brightness); > > +} > > + > > +static void chagall_leds_status_update(struct chagall_battery_data *cg, int state) > > +{ > > + switch (state) { > > + case POWER_SUPPLY_STATUS_FULL: > > + led_set_brightness(&cg->amber_led, LED_OFF); > > + led_set_brightness(&cg->white_led, LED_ON); > > + break; > > + > > no Blank line. > > > + case POWER_SUPPLY_STATUS_CHARGING: > > + led_set_brightness(&cg->white_led, LED_OFF); > > + led_set_brightness(&cg->amber_led, LED_ON); > > + break; > > + > > no Blank line. > > > + default: > > + led_set_brightness(&cg->amber_led, LED_OFF); > > + led_set_brightness(&cg->white_led, LED_OFF); > > + break; > > + } > > +} > > + > > +static const enum power_supply_property chagall_battery_properties[] = { > > + POWER_SUPPLY_PROP_STATUS, > > + POWER_SUPPLY_PROP_PRESENT, > > + POWER_SUPPLY_PROP_VOLTAGE_NOW, > > + POWER_SUPPLY_PROP_VOLTAGE_MAX, > > + POWER_SUPPLY_PROP_CURRENT_NOW, > > + POWER_SUPPLY_PROP_CURRENT_MAX, > > + POWER_SUPPLY_PROP_CAPACITY, > > + POWER_SUPPLY_PROP_TEMP, > > + POWER_SUPPLY_PROP_CHARGE_FULL, > > + POWER_SUPPLY_PROP_CHARGE_NOW, > > +}; > > + > > +static const unsigned int chagall_battery_prop_offs[] = { > > + [POWER_SUPPLY_PROP_TEMP] = CHAGALL_REG_BATTERY_TEMPERATURE, > > + [POWER_SUPPLY_PROP_VOLTAGE_NOW] = CHAGALL_REG_BATTERY_VOLTAGE, > > + [POWER_SUPPLY_PROP_CURRENT_NOW] = CHAGALL_REG_BATTERY_CURRENT, > > + [POWER_SUPPLY_PROP_CAPACITY] = CHAGALL_REG_BATTERY_CAPACITY, > > + [POWER_SUPPLY_PROP_CURRENT_MAX] = CHAGALL_REG_BATTERY_CHARGING_CURRENT, > > + [POWER_SUPPLY_PROP_VOLTAGE_MAX] = CHAGALL_REG_BATTERY_CHARGING_VOLTAGE, > > + [POWER_SUPPLY_PROP_STATUS] = CHAGALL_REG_BATTERY_STATUS, > > + [POWER_SUPPLY_PROP_CHARGE_NOW] = CHAGALL_REG_BATTERY_REMAIN_CAPACITY, > > + [POWER_SUPPLY_PROP_CHARGE_FULL] = CHAGALL_REG_BATTERY_FULL_CAPACITY, > > +}; > > + > > +static int chagall_battery_get_value(struct chagall_battery_data *cg, > > + enum power_supply_property psp, u32 *val) > > +{ > > + if (psp >= ARRAY_SIZE(chagall_battery_prop_offs)) > > + return -EINVAL; > > + if (!chagall_battery_prop_offs[psp]) > > + return -EINVAL; > > + > > + /* Battery data is stored in 2 consecutive registers with little-endian */ > > + return regmap_bulk_read(cg->regmap, chagall_battery_prop_offs[psp], val, 2); > > +} > > + > > +static int chagall_battery_get_property(struct power_supply *psy, > > + enum power_supply_property psp, > > + union power_supply_propval *val) > > +{ > > + struct chagall_battery_data *cg = power_supply_get_drvdata(psy); > > + int ret; > > + > > + switch (psp) { > > + case POWER_SUPPLY_PROP_PRESENT: > > + val->intval = 1; > > + break; > > + > > + default: > > + ret = chagall_battery_get_value(cg, psp, &val->intval); > > + if (ret) > > + return ret; > > + > > + switch (psp) { > > + case POWER_SUPPLY_PROP_TEMP: > > + val->intval -= TEMP_CELSIUS_OFFSET; > > + break; > > + > > + case POWER_SUPPLY_PROP_VOLTAGE_MAX: > > + case POWER_SUPPLY_PROP_VOLTAGE_NOW: > > + case POWER_SUPPLY_PROP_CURRENT_MAX: > > + case POWER_SUPPLY_PROP_CURRENT_NOW: > > + case POWER_SUPPLY_PROP_CHARGE_FULL: > > + case POWER_SUPPLY_PROP_CHARGE_NOW: > > + val->intval *= 1000; > > + break; > > + > > + case POWER_SUPPLY_PROP_STATUS: > > + if (val->intval & BATTERY_FULL_CHARGED) > > + val->intval = POWER_SUPPLY_STATUS_FULL; > > + else if (val->intval & BATTERY_FULL_DISCHARGED) > > + val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; > > + else if (val->intval & BATTERY_DISCHARGING) > > + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; > > + else > > + val->intval = POWER_SUPPLY_STATUS_CHARGING; > > + break; > > + > > no blank line is required between a case label. > > > + default: > > + break; > > + } > > + > > + break; > > + } > > + > > + return 0; > > +} > > + > > +static void chagall_battery_poll_work(struct work_struct *work) > > +{ > > + struct chagall_battery_data *cg = > > + container_of(work, struct chagall_battery_data, poll_work.work); > > + u32 state; > > + int ret; > > + > > + ret = chagall_battery_get_value(cg, POWER_SUPPLY_PROP_STATUS, &state); > > + if (ret) > > + return; > > + > > + if (state & BATTERY_FULL_CHARGED) > > + state = POWER_SUPPLY_STATUS_FULL; > > + else if (state & BATTERY_DISCHARGING) > > + state = POWER_SUPPLY_STATUS_DISCHARGING; > > + else > > + state = POWER_SUPPLY_STATUS_CHARGING; > > + > > + if (cg->last_state != state) { > > + cg->last_state = state; > > + power_supply_changed(cg->battery); > > + } > > + > > + chagall_leds_status_update(cg, state); > > + > > + /* continuously send uevent notification */ > > + schedule_delayed_work(&cg->poll_work, > > + msecs_to_jiffies(CHAGALL_BATTERY_DATA_REFRESH)); > > +} > > + > > +static const struct power_supply_desc chagall_battery_desc = { > > + .name = "chagall-battery", > > + .type = POWER_SUPPLY_TYPE_BATTERY, > > + .properties = chagall_battery_properties, > > + .num_properties = ARRAY_SIZE(chagall_battery_properties), > > + .get_property = chagall_battery_get_property, > > + .external_power_changed = power_supply_changed, > > +}; > > + > > +static int chagall_battery_probe(struct i2c_client *client) > > +{ > > + struct chagall_battery_data *cg; > > + struct device *dev = &client->dev; > > + struct power_supply_config cfg = { }; > > + int ret; > > + > > + cg = devm_kzalloc(dev, sizeof(*cg), GFP_KERNEL); > > + if (!cg) > > + return -ENOMEM; > > + > > + cfg.drv_data = cg; > > + cfg.fwnode = dev_fwnode(dev); > > + > > + i2c_set_clientdata(client, cg); > > + > > + cg->regmap = devm_regmap_init_i2c(client, &chagall_battery_regmap_config); > > + if (IS_ERR(cg->regmap)) > > + return dev_err_probe(dev, PTR_ERR(cg->regmap), "cannot allocate regmap\n"); > > + > > + cg->last_state = POWER_SUPPLY_STATUS_UNKNOWN; > > + cg->battery = devm_power_supply_register(dev, &chagall_battery_desc, &cfg); > > + if (IS_ERR(cg->battery)) > > + return dev_err_probe(dev, PTR_ERR(cg->battery), "failed to register power supply\n"); > > + > > + cg->amber_led.name = "power::amber"; > > + cg->amber_led.max_brightness = 1; > > + cg->amber_led.flags = LED_CORE_SUSPENDRESUME; > > + cg->amber_led.brightness_set = chagall_led_set_brightness_amber; > > + > > + ret = devm_led_classdev_register(dev, &cg->amber_led); > > + if (ret) > > + return dev_err_probe(dev, ret, "failed to register amber LED\n"); > > + > > + cg->white_led.name = "power::white"; > > + cg->white_led.max_brightness = 1; > > + cg->white_led.flags = LED_CORE_SUSPENDRESUME; > > + cg->white_led.brightness_set = chagall_led_set_brightness_white; > > + > > + ret = devm_led_classdev_register(dev, &cg->white_led); > > + if (ret) > > + return dev_err_probe(dev, ret, "failed to register white LED\n"); > > + > > + led_set_brightness(&cg->amber_led, LED_OFF); > > + led_set_brightness(&cg->white_led, LED_OFF); > > + > > + ret = devm_delayed_work_autocancel(dev, &cg->poll_work, chagall_battery_poll_work); > > + if (ret) > > + return ret; > > + > > + schedule_delayed_work(&cg->poll_work, msecs_to_jiffies(CHAGALL_BATTERY_DATA_REFRESH)); > > a '\n' before return is customary > > > + return 0; > > +} > > + > > +static int __maybe_unused chagall_battery_suspend(struct device *dev) > > +{ > > + struct i2c_client *client = to_i2c_client(dev); > > + struct chagall_battery_data *cg = i2c_get_clientdata(client); > > + > > + cancel_delayed_work_sync(&cg->poll_work); > > a '\n' before return > > > + return 0; > > +} > > + > > +static int __maybe_unused chagall_battery_resume(struct device *dev) > > +{ > > + struct i2c_client *client = to_i2c_client(dev); > > + struct chagall_battery_data *cg = i2c_get_clientdata(client); > > + > > + schedule_delayed_work(&cg->poll_work, msecs_to_jiffies(CHAGALL_BATTERY_DATA_REFRESH)); > > a '\n' before return is customary > > > + return 0; > > +} > > + > > +static SIMPLE_DEV_PM_OPS(chagall_battery_pm_ops, > > + chagall_battery_suspend, chagall_battery_resume); > > + > > +static const struct of_device_id chagall_of_match[] = { > > + { .compatible = "pegatron,chagall-ec" }, > > + { } > > +}; > > +MODULE_DEVICE_TABLE(of, chagall_of_match); > > + > > +static struct i2c_driver chagall_battery_driver = { > > + .driver = { > > + .name = "chagall-battery", > > + .pm = &chagall_battery_pm_ops, > > + .of_match_table = chagall_of_match, > > + }, > > + .probe = chagall_battery_probe, > > +}; > > +module_i2c_driver(chagall_battery_driver); > > + > > +MODULE_AUTHOR("Svyatoslav Ryhel <clamor95@gmail.com>"); > > +MODULE_DESCRIPTION("Pegatron Chagall fuel gauge driver"); > > +MODULE_LICENSE("GPL"); > > > > Thanks, > Alok Acknowledged. Thank you. ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v2 3/4] power/supply: Add driver for Pegatron Chagall battery 2025-04-13 11:10 ` [PATCH v2 3/4] power/supply: Add driver for Pegatron Chagall battery Svyatoslav Ryhel 2025-04-13 15:15 ` ALOK TIWARI @ 2025-04-28 23:02 ` Sebastian Reichel 2025-04-29 5:41 ` Svyatoslav Ryhel 1 sibling, 1 reply; 10+ messages in thread From: Sebastian Reichel @ 2025-04-28 23:02 UTC (permalink / raw) To: Svyatoslav Ryhel Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Thierry Reding, Jonathan Hunter, Neil Armstrong, Jonathan Cameron, Heiko Stuebner, Rafał Miłecki, Aradhya Bhatia, linux-pm, devicetree, linux-kernel, linux-tegra [-- Attachment #1: Type: text/plain, Size: 5781 bytes --] Hi, On Sun, Apr 13, 2025 at 02:10:32PM +0300, Svyatoslav Ryhel wrote: > The Pegatron Chagall is an Android tablet utilizing a customized Cypress > CG7153AM microcontroller (MCU) as its battery fuel gauge. It supports a > single-cell battery and features a dual-color charging LED. > > Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com> > --- This looks mostly good to me, but I have some comments. > drivers/power/supply/Kconfig | 12 + > drivers/power/supply/Makefile | 1 + > drivers/power/supply/chagall-battery.c | 308 +++++++++++++++++++++++++ > 3 files changed, 321 insertions(+) > create mode 100644 drivers/power/supply/chagall-battery.c [...] > +static void chagall_leds_status_update(struct chagall_battery_data *cg, int state) > +{ > + switch (state) { > + case POWER_SUPPLY_STATUS_FULL: > + led_set_brightness(&cg->amber_led, LED_OFF); > + led_set_brightness(&cg->white_led, LED_ON); > + break; > + > + case POWER_SUPPLY_STATUS_CHARGING: > + led_set_brightness(&cg->white_led, LED_OFF); > + led_set_brightness(&cg->amber_led, LED_ON); > + break; > + > + default: > + led_set_brightness(&cg->amber_led, LED_OFF); > + led_set_brightness(&cg->white_led, LED_OFF); > + break; > + } > +} Instead of doing this, you should setup LED triggers when registering the LEDs. The white LED can use power-supply's full_trig and the orange LED can use power-supply's charging_trig, which should have the same effect. > +static const enum power_supply_property chagall_battery_properties[] = { > + POWER_SUPPLY_PROP_STATUS, > + POWER_SUPPLY_PROP_PRESENT, > + POWER_SUPPLY_PROP_VOLTAGE_NOW, > + POWER_SUPPLY_PROP_VOLTAGE_MAX, > + POWER_SUPPLY_PROP_CURRENT_NOW, > + POWER_SUPPLY_PROP_CURRENT_MAX, > + POWER_SUPPLY_PROP_CAPACITY, > + POWER_SUPPLY_PROP_TEMP, > + POWER_SUPPLY_PROP_CHARGE_FULL, > + POWER_SUPPLY_PROP_CHARGE_NOW, > +}; > + > +static const unsigned int chagall_battery_prop_offs[] = { > + [POWER_SUPPLY_PROP_TEMP] = CHAGALL_REG_BATTERY_TEMPERATURE, > + [POWER_SUPPLY_PROP_VOLTAGE_NOW] = CHAGALL_REG_BATTERY_VOLTAGE, > + [POWER_SUPPLY_PROP_CURRENT_NOW] = CHAGALL_REG_BATTERY_CURRENT, > + [POWER_SUPPLY_PROP_CAPACITY] = CHAGALL_REG_BATTERY_CAPACITY, > + [POWER_SUPPLY_PROP_CURRENT_MAX] = CHAGALL_REG_BATTERY_CHARGING_CURRENT, > + [POWER_SUPPLY_PROP_VOLTAGE_MAX] = CHAGALL_REG_BATTERY_CHARGING_VOLTAGE, > + [POWER_SUPPLY_PROP_STATUS] = CHAGALL_REG_BATTERY_STATUS, > + [POWER_SUPPLY_PROP_CHARGE_NOW] = CHAGALL_REG_BATTERY_REMAIN_CAPACITY, > + [POWER_SUPPLY_PROP_CHARGE_FULL] = CHAGALL_REG_BATTERY_FULL_CAPACITY, > +}; Please use the same order for chagall_battery_prop_offs and chagall_battery_properties. Makes it a lot easier to see that all options have been covered. > +static int chagall_battery_get_value(struct chagall_battery_data *cg, > + enum power_supply_property psp, u32 *val) > +{ > + if (psp >= ARRAY_SIZE(chagall_battery_prop_offs)) > + return -EINVAL; > + if (!chagall_battery_prop_offs[psp]) > + return -EINVAL; > + > + /* Battery data is stored in 2 consecutive registers with little-endian */ > + return regmap_bulk_read(cg->regmap, chagall_battery_prop_offs[psp], val, 2); > +} > + > +static int chagall_battery_get_property(struct power_supply *psy, > + enum power_supply_property psp, > + union power_supply_propval *val) > +{ > + struct chagall_battery_data *cg = power_supply_get_drvdata(psy); > + int ret; > + > + switch (psp) { > + case POWER_SUPPLY_PROP_PRESENT: > + val->intval = 1; > + break; > + > + default: > + ret = chagall_battery_get_value(cg, psp, &val->intval); > + if (ret) > + return ret; > + > + switch (psp) { > + case POWER_SUPPLY_PROP_TEMP: > + val->intval -= TEMP_CELSIUS_OFFSET; > + break; > + > + case POWER_SUPPLY_PROP_VOLTAGE_MAX: > + case POWER_SUPPLY_PROP_VOLTAGE_NOW: > + case POWER_SUPPLY_PROP_CURRENT_MAX: > + case POWER_SUPPLY_PROP_CURRENT_NOW: > + case POWER_SUPPLY_PROP_CHARGE_FULL: > + case POWER_SUPPLY_PROP_CHARGE_NOW: > + val->intval *= 1000; > + break; > + > + case POWER_SUPPLY_PROP_STATUS: > + if (val->intval & BATTERY_FULL_CHARGED) > + val->intval = POWER_SUPPLY_STATUS_FULL; > + else if (val->intval & BATTERY_FULL_DISCHARGED) > + val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; Have you tested this path? POWER_SUPPLY_STATUS_NOT_CHARGING is intended to be used when the battery is neither charging nor discharging. Does BATTERY_FULL_DISCHARGED mean, that the battery is fully depleted and not providing any energy at all? Or is this some kind of "battery level is criticial, you should attach a power-supply now or the system will be turn off by itself soon"? > + else if (val->intval & BATTERY_DISCHARGING) > + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; > + else > + val->intval = POWER_SUPPLY_STATUS_CHARGING; > + break; > + > + default: > + break; > + } > + > + break; > + } > + > + return 0; > +} > + > +static void chagall_battery_poll_work(struct work_struct *work) > +{ > + struct chagall_battery_data *cg = > + container_of(work, struct chagall_battery_data, poll_work.work); > + u32 state; > + int ret; > + > + ret = chagall_battery_get_value(cg, POWER_SUPPLY_PROP_STATUS, &state); > + if (ret) > + return; > + > + if (state & BATTERY_FULL_CHARGED) > + state = POWER_SUPPLY_STATUS_FULL; > + else if (state & BATTERY_DISCHARGING) > + state = POWER_SUPPLY_STATUS_DISCHARGING; > + else > + state = POWER_SUPPLY_STATUS_CHARGING; This basically duplicates the logic from chagall_battery_get_property(), so put the translation logic into a helper function and use it in both places. Greetings, -- Sebastian [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 833 bytes --] ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v2 3/4] power/supply: Add driver for Pegatron Chagall battery 2025-04-28 23:02 ` Sebastian Reichel @ 2025-04-29 5:41 ` Svyatoslav Ryhel 0 siblings, 0 replies; 10+ messages in thread From: Svyatoslav Ryhel @ 2025-04-29 5:41 UTC (permalink / raw) To: Sebastian Reichel Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Thierry Reding, Jonathan Hunter, Neil Armstrong, Jonathan Cameron, Heiko Stuebner, Rafał Miłecki, Aradhya Bhatia, linux-pm, devicetree, linux-kernel, linux-tegra вт, 29 квіт. 2025 р. о 02:02 Sebastian Reichel <sebastian.reichel@collabora.com> пише: > > Hi, > > On Sun, Apr 13, 2025 at 02:10:32PM +0300, Svyatoslav Ryhel wrote: > > The Pegatron Chagall is an Android tablet utilizing a customized Cypress > > CG7153AM microcontroller (MCU) as its battery fuel gauge. It supports a > > single-cell battery and features a dual-color charging LED. > > > > Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com> > > --- > > This looks mostly good to me, but I have some comments. > > > drivers/power/supply/Kconfig | 12 + > > drivers/power/supply/Makefile | 1 + > > drivers/power/supply/chagall-battery.c | 308 +++++++++++++++++++++++++ > > 3 files changed, 321 insertions(+) > > create mode 100644 drivers/power/supply/chagall-battery.c > > [...] > > > +static void chagall_leds_status_update(struct chagall_battery_data *cg, int state) > > +{ > > + switch (state) { > > + case POWER_SUPPLY_STATUS_FULL: > > + led_set_brightness(&cg->amber_led, LED_OFF); > > + led_set_brightness(&cg->white_led, LED_ON); > > + break; > > + > > + case POWER_SUPPLY_STATUS_CHARGING: > > + led_set_brightness(&cg->white_led, LED_OFF); > > + led_set_brightness(&cg->amber_led, LED_ON); > > + break; > > + > > + default: > > + led_set_brightness(&cg->amber_led, LED_OFF); > > + led_set_brightness(&cg->white_led, LED_OFF); > > + break; > > + } > > +} > > Instead of doing this, you should setup LED triggers when > registering the LEDs. The white LED can use power-supply's full_trig > and the orange LED can use power-supply's charging_trig, which > should have the same effect. > > > +static const enum power_supply_property chagall_battery_properties[] = { > > + POWER_SUPPLY_PROP_STATUS, > > + POWER_SUPPLY_PROP_PRESENT, > > + POWER_SUPPLY_PROP_VOLTAGE_NOW, > > + POWER_SUPPLY_PROP_VOLTAGE_MAX, > > + POWER_SUPPLY_PROP_CURRENT_NOW, > > + POWER_SUPPLY_PROP_CURRENT_MAX, > > + POWER_SUPPLY_PROP_CAPACITY, > > + POWER_SUPPLY_PROP_TEMP, > > + POWER_SUPPLY_PROP_CHARGE_FULL, > > + POWER_SUPPLY_PROP_CHARGE_NOW, > > +}; > > + > > +static const unsigned int chagall_battery_prop_offs[] = { > > + [POWER_SUPPLY_PROP_TEMP] = CHAGALL_REG_BATTERY_TEMPERATURE, > > + [POWER_SUPPLY_PROP_VOLTAGE_NOW] = CHAGALL_REG_BATTERY_VOLTAGE, > > + [POWER_SUPPLY_PROP_CURRENT_NOW] = CHAGALL_REG_BATTERY_CURRENT, > > + [POWER_SUPPLY_PROP_CAPACITY] = CHAGALL_REG_BATTERY_CAPACITY, > > + [POWER_SUPPLY_PROP_CURRENT_MAX] = CHAGALL_REG_BATTERY_CHARGING_CURRENT, > > + [POWER_SUPPLY_PROP_VOLTAGE_MAX] = CHAGALL_REG_BATTERY_CHARGING_VOLTAGE, > > + [POWER_SUPPLY_PROP_STATUS] = CHAGALL_REG_BATTERY_STATUS, > > + [POWER_SUPPLY_PROP_CHARGE_NOW] = CHAGALL_REG_BATTERY_REMAIN_CAPACITY, > > + [POWER_SUPPLY_PROP_CHARGE_FULL] = CHAGALL_REG_BATTERY_FULL_CAPACITY, > > +}; > > Please use the same order for chagall_battery_prop_offs and > chagall_battery_properties. Makes it a lot easier to see > that all options have been covered. > > > +static int chagall_battery_get_value(struct chagall_battery_data *cg, > > + enum power_supply_property psp, u32 *val) > > +{ > > + if (psp >= ARRAY_SIZE(chagall_battery_prop_offs)) > > + return -EINVAL; > > + if (!chagall_battery_prop_offs[psp]) > > + return -EINVAL; > > + > > + /* Battery data is stored in 2 consecutive registers with little-endian */ > > + return regmap_bulk_read(cg->regmap, chagall_battery_prop_offs[psp], val, 2); > > +} > > + > > +static int chagall_battery_get_property(struct power_supply *psy, > > + enum power_supply_property psp, > > + union power_supply_propval *val) > > +{ > > + struct chagall_battery_data *cg = power_supply_get_drvdata(psy); > > + int ret; > > + > > + switch (psp) { > > + case POWER_SUPPLY_PROP_PRESENT: > > + val->intval = 1; > > + break; > > + > > + default: > > + ret = chagall_battery_get_value(cg, psp, &val->intval); > > + if (ret) > > + return ret; > > + > > + switch (psp) { > > + case POWER_SUPPLY_PROP_TEMP: > > + val->intval -= TEMP_CELSIUS_OFFSET; > > + break; > > + > > + case POWER_SUPPLY_PROP_VOLTAGE_MAX: > > + case POWER_SUPPLY_PROP_VOLTAGE_NOW: > > + case POWER_SUPPLY_PROP_CURRENT_MAX: > > + case POWER_SUPPLY_PROP_CURRENT_NOW: > > + case POWER_SUPPLY_PROP_CHARGE_FULL: > > + case POWER_SUPPLY_PROP_CHARGE_NOW: > > + val->intval *= 1000; > > + break; > > + > > + case POWER_SUPPLY_PROP_STATUS: > > + if (val->intval & BATTERY_FULL_CHARGED) > > + val->intval = POWER_SUPPLY_STATUS_FULL; > > + else if (val->intval & BATTERY_FULL_DISCHARGED) > > + val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; > > Have you tested this path? POWER_SUPPLY_STATUS_NOT_CHARGING is > intended to be used when the battery is neither charging nor > discharging. Does BATTERY_FULL_DISCHARGED mean, that the battery > is fully depleted and not providing any energy at all? Or is this > some kind of "battery level is criticial, you should attach a > power-supply now or the system will be turn off by itself soon"? > This one is tricky. I have transferred this logic from downstream kernel and couldn't trigger BATTERY_FULL_DISCHARGED ever. It might be a brief state you describe as "battery level is criticial, you should attach a power-supply now or the system will be turn off by itself soon" but there is no such entry in POWER_SUPPLY_STATUS enum, so I assumed that POWER_SUPPLY_STATUS_NOT_CHARGING co-responds to such state. If this is not the case I will just remove this entry. > > + else if (val->intval & BATTERY_DISCHARGING) > > + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; > > + else > > + val->intval = POWER_SUPPLY_STATUS_CHARGING; > > + break; > > + > > + default: > > + break; > > + } > > + > > + break; > > + } > > + > > + return 0; > > +} > > + > > +static void chagall_battery_poll_work(struct work_struct *work) > > +{ > > + struct chagall_battery_data *cg = > > + container_of(work, struct chagall_battery_data, poll_work.work); > > + u32 state; > > + int ret; > > + > > + ret = chagall_battery_get_value(cg, POWER_SUPPLY_PROP_STATUS, &state); > > + if (ret) > > + return; > > + > > + if (state & BATTERY_FULL_CHARGED) > > + state = POWER_SUPPLY_STATUS_FULL; > > + else if (state & BATTERY_DISCHARGING) > > + state = POWER_SUPPLY_STATUS_DISCHARGING; > > + else > > + state = POWER_SUPPLY_STATUS_CHARGING; > > This basically duplicates the logic from chagall_battery_get_property(), > so put the translation logic into a helper function and use it in > both places. > > Greetings, > > -- Sebastian ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v2 4/4] ARM: tegra: chagall: Add embedded controller node 2025-04-13 11:10 [PATCH v2 0/4] power: supply: add support for Pegatron Chagall battery Svyatoslav Ryhel ` (2 preceding siblings ...) 2025-04-13 11:10 ` [PATCH v2 3/4] power/supply: Add driver for Pegatron Chagall battery Svyatoslav Ryhel @ 2025-04-13 11:10 ` Svyatoslav Ryhel 3 siblings, 0 replies; 10+ messages in thread From: Svyatoslav Ryhel @ 2025-04-13 11:10 UTC (permalink / raw) To: Sebastian Reichel, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Thierry Reding, Jonathan Hunter, Neil Armstrong, Jonathan Cameron, Heiko Stuebner, Rafał Miłecki, Aradhya Bhatia, Svyatoslav Ryhel Cc: linux-pm, devicetree, linux-kernel, linux-tegra Add embedded controller node to Pegatron Chagall device-tree. Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com> --- .../boot/dts/nvidia/tegra30-pegatron-chagall.dts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/arm/boot/dts/nvidia/tegra30-pegatron-chagall.dts b/arch/arm/boot/dts/nvidia/tegra30-pegatron-chagall.dts index 4012f9c799a8..b7d0ebb766a6 100644 --- a/arch/arm/boot/dts/nvidia/tegra30-pegatron-chagall.dts +++ b/arch/arm/boot/dts/nvidia/tegra30-pegatron-chagall.dts @@ -1155,6 +1155,14 @@ lcd_ddc: i2c@7000c000 { status = "okay"; clock-frequency = <400000>; + embedded-controller@10 { + compatible = "pegatron,chagall-ec"; + reg = <0x10>; + + monitored-battery = <&battery>; + power-supplies = <&mains>; + }; + /* Wolfson Microelectronics WM8903 audio codec */ wm8903: audio-codec@1a { compatible = "wlf,wm8903"; @@ -2596,6 +2604,14 @@ backlight: backlight { default-brightness-level = <15>; }; + battery: battery-cell { + compatible = "simple-battery"; + device-chemistry = "lithium-ion-polymer"; + charge-full-design-microamp-hours = <3050000>; + energy-full-design-microwatt-hours = <23000000>; + operating-range-celsius = <0 45>; + }; + /* PMIC has a built-in 32KHz oscillator which is used by PMC */ clk32k_in: clock-32k { compatible = "fixed-clock"; -- 2.43.0 ^ permalink raw reply related [flat|nested] 10+ messages in thread
end of thread, other threads:[~2025-04-29 5:42 UTC | newest] Thread overview: 10+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-04-13 11:10 [PATCH v2 0/4] power: supply: add support for Pegatron Chagall battery Svyatoslav Ryhel 2025-04-13 11:10 ` [PATCH v2 1/4] dt-bindings: vendor-prefixes: add prefix for Pegatron Corporation Svyatoslav Ryhel 2025-04-13 11:10 ` [PATCH v2 2/4] dt-bindings: power: supply: Document Pegatron Chagall fuel gauge Svyatoslav Ryhel 2025-04-15 21:48 ` Rob Herring (Arm) 2025-04-13 11:10 ` [PATCH v2 3/4] power/supply: Add driver for Pegatron Chagall battery Svyatoslav Ryhel 2025-04-13 15:15 ` ALOK TIWARI 2025-04-13 15:20 ` Svyatoslav Ryhel 2025-04-28 23:02 ` Sebastian Reichel 2025-04-29 5:41 ` Svyatoslav Ryhel 2025-04-13 11:10 ` [PATCH v2 4/4] ARM: tegra: chagall: Add embedded controller node Svyatoslav Ryhel
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).