linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Quentin Schulz <quentin.schulz@free-electrons.com>
To: jic23@kernel.org, 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
Cc: Quentin Schulz <quentin.schulz@free-electrons.com>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	thomas.petazzoni@free-electrons.com, icenowy@aosc.xyz,
	bonbons@linux-vserver.org
Subject: [PATCH 08/22] power: supply: add AC power supply driver for AXP20X and AXP22X PMICs
Date: Mon,  2 Jan 2017 17:37:08 +0100	[thread overview]
Message-ID: <20170102163723.7939-9-quentin.schulz@free-electrons.com> (raw)
In-Reply-To: <20170102163723.7939-1-quentin.schulz@free-electrons.com>

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 <quentin.schulz@free-electrons.com>
---
 drivers/power/supply/Kconfig           |  12 ++
 drivers/power/supply/Makefile          |   1 +
 drivers/power/supply/axp20x_ac_power.c | 251 +++++++++++++++++++++++++++++++++
 3 files changed, 264 insertions(+)
 create mode 100644 drivers/power/supply/axp20x_ac_power.c

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..d7bc25c
--- /dev/null
+++ b/drivers/power/supply/axp20x_ac_power.c
@@ -0,0 +1,251 @@
+/*
+ * AXP20X and AXP22X PMICs' ACIN power supply driver
+ *
+ * Copyright (C) 2016 Free Electrons
+ *	Quentin Schulz <quentin.schulz@free-electrons.com>
+ *
+ * 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 <linux/device.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/mfd/axp20x.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/power_supply.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/iio/consumer.h>
+
+#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, &reg);
+		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, &reg);
+		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, &reg);
+		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 µV.
+		 */
+		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 mV but Power Supply framework gives µV.
+		 */
+		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,
+};
+
+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;
+	static const char * const axp20x_irq_names[] = { "ACIN_PLUGIN",
+		"ACIN_REMOVAL", NULL };
+	static const char * const *irq_names;
+	const struct power_supply_desc *ac_power_desc;
+	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;
+
+	power->axp20x_id = (int)of_device_get_match_data(&pdev->dev);
+
+	irq_names = axp20x_irq_names;
+
+	if (power->axp20x_id == AXP202_ID) {
+		ac_power_desc = &axp20x_ac_power_desc;
+
+		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);
+		}
+	} else {
+		ac_power_desc = &axp22x_ac_power_desc;
+	}
+
+	power->np = pdev->dev.of_node;
+	power->regmap = axp20x->regmap;
+
+	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, ac_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 *)AXP202_ID,
+	}, {
+		.compatible = "x-powers,axp221-ac-power-supply",
+		.data = (void *)AXP221_ID,
+	}, { /* 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,
+	},
+};
+
+module_platform_driver(axp20x_ac_power_driver);
+
+MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
+MODULE_DESCRIPTION("AXP20X and AXP22X PMICs' AC power supply driver");
+MODULE_LICENSE("GPL");
-- 
2.9.3

  parent reply	other threads:[~2017-01-02 20:58 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-02 16:37 [PATCH 00/22] add support for AXP20X and AXP22X power supply drivers Quentin Schulz
2017-01-02 16:37 ` [PATCH 01/22] dt-bindings: iio: adc: add AXP20X/AXP22X ADC DT binding Quentin Schulz
2017-01-03 23:20   ` Rob Herring
2017-01-05  4:05     ` Chen-Yu Tsai
2017-01-05 16:40   ` Maxime Ripard
2017-01-02 16:37 ` [PATCH 02/22] mfd: axp20x: add ADC data regs to volatile regs for AXP22X Quentin Schulz
2017-01-04 11:55   ` Lee Jones
2017-01-05  4:12   ` Chen-Yu Tsai
2017-01-02 16:37 ` [PATCH 03/22] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs Quentin Schulz
2017-01-05  5:42   ` Chen-Yu Tsai
2017-01-05  8:06     ` Quentin Schulz
2017-01-05  8:27       ` Chen-Yu Tsai
2017-01-05  9:50         ` Quentin Schulz
2017-01-05 10:28           ` Chen-Yu Tsai
2017-01-07 19:23             ` Jonathan Cameron
2017-01-05 16:46           ` Maxime Ripard
2017-01-07 19:20           ` Jonathan Cameron
2017-01-05 16:51   ` Maxime Ripard
2017-01-07 19:13   ` Jonathan Cameron
2017-01-02 16:37 ` [PATCH 04/22] mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs Quentin Schulz
2017-01-04 11:56   ` Lee Jones
2017-01-04 11:56     ` Lee Jones
2017-01-05  5:51       ` Chen-Yu Tsai
2017-01-02 16:37 ` [PATCH 05/22] ARM: dtsi: axp209: add AXP209 ADC subnode Quentin Schulz
2017-01-05  5:51   ` Chen-Yu Tsai
2017-01-05  8:08     ` Quentin Schulz
2017-01-05  8:16       ` Chen-Yu Tsai
2017-01-02 16:37 ` [PATCH 06/22] ARM: dtsi: axp22x: add AXP22X " Quentin Schulz
2017-01-05  5:52   ` Chen-Yu Tsai
2017-01-02 16:37 ` [PATCH 07/22] dt-bindings: power: supply: add AXP20X/AXP22X AC power supply Quentin Schulz
2017-01-04 13:14   ` Rob Herring
2017-01-05  6:17     ` Chen-Yu Tsai
2017-01-07 19:26       ` Jonathan Cameron
2017-01-02 16:37 ` Quentin Schulz [this message]
2017-01-07 19:31   ` [PATCH 08/22] power: supply: add AC power supply driver for AXP20X and AXP22X PMICs Jonathan Cameron
2017-01-08 10:41     ` Quentin Schulz
2017-01-17  3:00   ` Sebastian Reichel
2017-01-26 13:32     ` Quentin Schulz
2017-01-27  8:20       ` Maxime Ripard
2017-01-28 14:30         ` Jonathan Cameron
2017-01-29 15:16           ` Sebastian Reichel
2017-01-02 16:37 ` [PATCH 09/22] mfd: axp20x: add AC power supply cells for " Quentin Schulz
2017-01-04 11:57   ` Lee Jones
2017-01-02 16:37 ` [PATCH 10/22] ARM: dtsi: axp209: add AC power supply subnode Quentin Schulz
2017-01-02 16:37 ` [PATCH 11/22] ARM: dtsi: axp22x: " Quentin Schulz
2017-01-02 16:37 ` [PATCH 12/22] ARM: dts: sun8i: sina33: enable ACIN " Quentin Schulz
2017-01-02 16:37 ` [PATCH 13/22] ARM: sun5i: chip: " Quentin Schulz
2017-01-02 16:37 ` [PATCH 14/22] dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding Quentin Schulz
2017-01-04 13:21   ` Rob Herring
2017-01-07 19:33     ` Jonathan Cameron
2017-01-08 10:48       ` Quentin Schulz
2017-01-08 10:59         ` Jonathan Cameron
2017-01-02 16:37 ` [PATCH 15/22] mfd: axp20x: add CHRG_CTRL1 to writeable regs for AXP20X/AXP22X Quentin Schulz
2017-01-04 11:57   ` Lee Jones
2017-01-05  6:10   ` Chen-Yu Tsai
2017-01-05  8:10     ` Quentin Schulz
2017-01-02 16:37 ` [PATCH 16/22] mfd: axp20x: add V_OFF to writeable regs for AXP20X and AXP22X Quentin Schulz
2017-01-04 11:57   ` Lee Jones
2017-01-05  6:02     ` Chen-Yu Tsai
2017-01-02 16:37 ` [PATCH 17/22] power: supply: add battery driver for AXP20X and AXP22X PMICs Quentin Schulz
2017-01-05 17:02   ` Maxime Ripard
2017-01-05 17:34   ` Ezequiel Garcia
2017-01-06  2:46     ` Sebastian Reichel
2017-01-06  3:39   ` Chen-Yu Tsai
2017-01-06  8:29     ` Quentin Schulz
2017-01-17  3:46   ` Sebastian Reichel
2017-01-02 16:37 ` [PATCH 18/22] mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver Quentin Schulz
2017-01-04 11:57   ` Lee Jones
2017-01-02 16:37 ` [PATCH 19/22] ARM: dtsi: axp209: add battery power supply subnode Quentin Schulz
2017-01-02 16:37 ` [PATCH 20/22] ARM: dtsi: axp22x: " Quentin Schulz
2017-01-02 16:37 ` [PATCH 21/22] ARM: dts: sun8i: sina33: enable " Quentin Schulz
2017-01-02 16:37 ` [PATCH 22/22] ARM: sun5i: chip: " Quentin Schulz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170102163723.7939-9-quentin.schulz@free-electrons.com \
    --to=quentin.schulz@free-electrons.com \
    --cc=bonbons@linux-vserver.org \
    --cc=devicetree@vger.kernel.org \
    --cc=icenowy@aosc.xyz \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=lee.jones@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=mark.rutland@arm.com \
    --cc=maxime.ripard@free-electrons.com \
    --cc=pmeerw@pmeerw.net \
    --cc=robh+dt@kernel.org \
    --cc=sre@kernel.org \
    --cc=thomas.petazzoni@free-electrons.com \
    --cc=wens@csie.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).