From patchwork Fri Jan 27 08:54:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quentin Schulz X-Patchwork-Id: 9540759 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4494B604A0 for ; Fri, 27 Jan 2017 09:05:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 39D1E27D4D for ; Fri, 27 Jan 2017 09:05:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2B6A1267EC; Fri, 27 Jan 2017 09:05:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00 autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 60FBD267EC for ; Fri, 27 Jan 2017 09:05:19 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cX2T4-0003ax-R7; Fri, 27 Jan 2017 09:05:14 +0000 Received: from mail.free-electrons.com ([62.4.15.54]) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cX2K9-0002KV-8G for linux-arm-kernel@lists.infradead.org; Fri, 27 Jan 2017 08:56:09 +0000 Received: by mail.free-electrons.com (Postfix, from userid 110) id 52E4520DEB; Fri, 27 Jan 2017 09:55:25 +0100 (CET) Received: from localhost.localdomain (LStLambert-657-1-97-87.w90-63.abo.wanadoo.fr [90.63.216.87]) by mail.free-electrons.com (Postfix) with ESMTPSA id D92D020DA7; Fri, 27 Jan 2017 09:55:24 +0100 (CET) From: Quentin Schulz To: knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, robh+dt@kernel.org, mark.rutland@arm.com, wens@csie.org, sre@kernel.org, linux@armlinux.org.uk, maxime.ripard@free-electrons.com, lee.jones@linaro.org Subject: [PATCH v2 10/25] power: supply: add AC power supply driver for AXP20X and AXP22X PMICs Date: Fri, 27 Jan 2017 09:54:43 +0100 Message-Id: <20170127085458.18270-11-quentin.schulz@free-electrons.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170127085458.18270-1-quentin.schulz@free-electrons.com> References: <20170127085458.18270-1-quentin.schulz@free-electrons.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170127_005601_663223_701652C2 X-CRM114-Status: GOOD ( 24.19 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: thomas.petazzoni@free-electrons.com, devicetree@vger.kernel.org, linux-pm@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Quentin Schulz , bonbons@linux-vserver.org, icenowy@aosc.xyz, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP The X-Powers AXP20X and AXP22X PMICs expose the status of AC power supply. Moreover, the AXP20X can also expose the current current and voltage values of the AC power supply. This adds the driver which exposes the status of the AC power supply of the AXP20X and AXP22X PMICs. Signed-off-by: Quentin Schulz Acked-by: Jonathan Cameron --- v2: - replaced ยต character by a common u for micro units to make checkpatch happy, - use of structure for specific data instead of an ID and if condiftions, - use dev_get_regmap instead of manually looking for it in the parent drvdata, drivers/power/supply/Kconfig | 12 ++ drivers/power/supply/Makefile | 1 + drivers/power/supply/axp20x_ac_power.c | 250 +++++++++++++++++++++++++++++++++ 3 files changed, 263 insertions(+) create mode 100644 drivers/power/supply/axp20x_ac_power.c +module_platform_driver(axp20x_ac_power_driver); + +MODULE_AUTHOR("Quentin Schulz "); +MODULE_DESCRIPTION("AXP20X and AXP22X PMICs' AC power supply driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 76806a0..c552b4b 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -214,6 +214,18 @@ config BATTERY_DA9150 This driver can also be built as a module. If so, the module will be called da9150-fg. +config CHARGER_AXP20X + tristate "X-Powers AXP20X and AXP22X AC power supply driver" + depends on MFD_AXP20X + depends on AXP20X_ADC + depends on IIO + help + Say Y here to enable support for X-Powers AXP20X and AXP22X PMICs' AC + power supply. + + This driver can also be built as a module. If so, the module will be + called axp20x_ac_power. + config AXP288_CHARGER tristate "X-Powers AXP288 Charger" depends on MFD_AXP20X && EXTCON_AXP288 diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index 36c599d..7d22417 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -18,6 +18,7 @@ obj-$(CONFIG_TEST_POWER) += test_power.o obj-$(CONFIG_BATTERY_88PM860X) += 88pm860x_battery.o obj-$(CONFIG_BATTERY_ACT8945A) += act8945a_charger.o +obj-$(CONFIG_CHARGER_AXP20X) += axp20x_ac_power.o obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o obj-$(CONFIG_BATTERY_DS2780) += ds2780_battery.o obj-$(CONFIG_BATTERY_DS2781) += ds2781_battery.o diff --git a/drivers/power/supply/axp20x_ac_power.c b/drivers/power/supply/axp20x_ac_power.c new file mode 100644 index 0000000..59ab4f2 --- /dev/null +++ b/drivers/power/supply/axp20x_ac_power.c @@ -0,0 +1,250 @@ +/* + * AXP20X and AXP22X PMICs' ACIN power supply driver + * + * Copyright (C) 2016 Free Electrons + * Quentin Schulz + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define AXP20X_PWR_STATUS_ACIN_PRESENT BIT(7) +#define AXP20X_PWR_STATUS_ACIN_AVAIL BIT(6) + +#define DRVNAME "axp20x-ac-power-supply" + +struct axp20x_ac_power { + struct device_node *np; + struct regmap *regmap; + struct power_supply *supply; + int axp20x_id; + struct iio_channel *acin_v; + struct iio_channel *acin_i; +}; + +static irqreturn_t axp20x_ac_power_irq(int irq, void *devid) +{ + struct axp20x_ac_power *power = devid; + + power_supply_changed(power->supply); + + return IRQ_HANDLED; +} + +static int axp20x_ac_power_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct axp20x_ac_power *power = power_supply_get_drvdata(psy); + int ret, reg; + + switch (psp) { + case POWER_SUPPLY_PROP_HEALTH: + ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, ®); + if (ret) + return ret; + + if (reg & AXP20X_PWR_STATUS_ACIN_PRESENT) { + val->intval = POWER_SUPPLY_HEALTH_GOOD; + return 0; + } + + val->intval = POWER_SUPPLY_HEALTH_UNKNOWN; + return 0; + + case POWER_SUPPLY_PROP_PRESENT: + ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, ®); + if (ret) + return ret; + + val->intval = !!(reg & AXP20X_PWR_STATUS_ACIN_PRESENT); + return 0; + + case POWER_SUPPLY_PROP_ONLINE: + ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, ®); + if (ret) + return ret; + + val->intval = !!(reg & AXP20X_PWR_STATUS_ACIN_AVAIL); + return 0; + + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + ret = iio_read_channel_processed(power->acin_v, &val->intval); + if (ret) + return ret; + + /* IIO framework gives mV but Power Supply framework gives uV */ + val->intval *= 1000; + + return 0; + + case POWER_SUPPLY_PROP_CURRENT_NOW: + ret = iio_read_channel_processed(power->acin_i, &val->intval); + if (ret) + return ret; + + /* IIO framework gives mA but Power Supply framework gives uA */ + val->intval *= 1000; + + return 0; + + default: + return -EINVAL; + } + + return -EINVAL; +} + +static enum power_supply_property axp20x_ac_power_properties[] = { + POWER_SUPPLY_PROP_HEALTH, + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_ONLINE, + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_CURRENT_NOW, +}; + +static enum power_supply_property axp22x_ac_power_properties[] = { + POWER_SUPPLY_PROP_HEALTH, + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_ONLINE, +}; + +static const struct power_supply_desc axp20x_ac_power_desc = { + .name = "axp20x-ac", + .type = POWER_SUPPLY_TYPE_MAINS, + .properties = axp20x_ac_power_properties, + .num_properties = ARRAY_SIZE(axp20x_ac_power_properties), + .get_property = axp20x_ac_power_get_property, +}; + +static const struct power_supply_desc axp22x_ac_power_desc = { + .name = "axp22x-ac", + .type = POWER_SUPPLY_TYPE_MAINS, + .properties = axp22x_ac_power_properties, + .num_properties = ARRAY_SIZE(axp22x_ac_power_properties), + .get_property = axp20x_ac_power_get_property, +}; + +struct axp_data { + const struct power_supply_desc *power_desc; + bool acin_adc; +}; + +static const struct axp_data axp20x_data = { + .power_desc = &axp20x_ac_power_desc, + .acin_adc = true, +}; + +static const struct axp_data axp22x_data = { + .power_desc = &axp22x_ac_power_desc, + .acin_adc = false, +}; + +static int axp20x_ac_power_probe(struct platform_device *pdev) +{ + struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); + struct power_supply_config psy_cfg = {}; + struct axp20x_ac_power *power; + struct axp_data *axp_data; + static const char * const irq_names[] = { "ACIN_PLUGIN", "ACIN_REMOVAL", + NULL }; + int i, irq, ret; + + if (!of_device_is_available(pdev->dev.of_node)) + return -ENODEV; + + if (!axp20x) { + dev_err(&pdev->dev, "Parent drvdata not set\n"); + return -EINVAL; + } + + power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL); + if (!power) + return -ENOMEM; + + axp_data = (struct axp_data *)of_device_get_match_data(&pdev->dev); + + if (axp_data->acin_adc) { + power->acin_v = devm_iio_channel_get(&pdev->dev, "acin_v"); + if (IS_ERR(power->acin_v)) { + if (PTR_ERR(power->acin_v) == -ENODEV) + return -EPROBE_DEFER; + return PTR_ERR(power->acin_v); + } + + power->acin_i = devm_iio_channel_get(&pdev->dev, "acin_i"); + if (IS_ERR(power->acin_i)) { + if (PTR_ERR(power->acin_i) == -ENODEV) + return -EPROBE_DEFER; + return PTR_ERR(power->acin_i); + } + } + + power->regmap = dev_get_regmap(pdev->dev.parent, NULL); + + platform_set_drvdata(pdev, power); + + psy_cfg.of_node = pdev->dev.of_node; + psy_cfg.drv_data = power; + + power->supply = devm_power_supply_register(&pdev->dev, + axp_data->power_desc, + &psy_cfg); + if (IS_ERR(power->supply)) + return PTR_ERR(power->supply); + + /* Request irqs after registering, as irqs may trigger immediately */ + for (i = 0; irq_names[i]; i++) { + irq = platform_get_irq_byname(pdev, irq_names[i]); + if (irq < 0) { + dev_warn(&pdev->dev, "No IRQ for %s: %d\n", + irq_names[i], irq); + continue; + } + irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq); + ret = devm_request_any_context_irq(&pdev->dev, irq, + axp20x_ac_power_irq, 0, + DRVNAME, power); + if (ret < 0) + dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n", + irq_names[i], ret); + } + + return 0; +} + +static const struct of_device_id axp20x_ac_power_match[] = { + { + .compatible = "x-powers,axp202-ac-power-supply", + .data = (void *)&axp20x_data, + }, { + .compatible = "x-powers,axp221-ac-power-supply", + .data = (void *)&axp22x_data, + }, { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, axp20x_ac_power_match); + +static struct platform_driver axp20x_ac_power_driver = { + .probe = axp20x_ac_power_probe, + .driver = { + .name = DRVNAME, + .of_match_table = axp20x_ac_power_match, + }, +}; +