[RFC PATCH 1/4] power_supply: of: Add an API to get power_supply device from dt node
From: Chanwoo Choi
Date: Mon Mar 17 2014 - 08:45:13 EST
This patch add an API of_power_supply_get_dev() to be used by drivers to
get power_supply device in the case of DT(DeviceTree) node
(this can be used instead of power_supply_get_by_name()).
Signed-off-by: Chanwoo Choi <cw00.choi@xxxxxxxxxxx>
---
drivers/power/Kconfig | 4 ++
drivers/power/Makefile | 2 +
drivers/power/of_power_supply.c | 82 +++++++++++++++++++++++++++++++++++
include/linux/power/of_power_supply.h | 46 ++++++++++++++++++++
4 files changed, 134 insertions(+)
create mode 100644 drivers/power/of_power_supply.c
create mode 100644 include/linux/power/of_power_supply.h
diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
index ba69751..02240aa 100644
--- a/drivers/power/Kconfig
+++ b/drivers/power/Kconfig
@@ -8,6 +8,10 @@ menuconfig POWER_SUPPLY
if POWER_SUPPLY
+config OF_POWER_SUPPLY
+ def_tristate y
+ depends on OF
+
config POWER_SUPPLY_DEBUG
bool "Power supply debug"
help
diff --git a/drivers/power/Makefile b/drivers/power/Makefile
index ee54a3e..27d0c9c 100644
--- a/drivers/power/Makefile
+++ b/drivers/power/Makefile
@@ -4,6 +4,8 @@ power_supply-y := power_supply_core.o
power_supply-$(CONFIG_SYSFS) += power_supply_sysfs.o
power_supply-$(CONFIG_LEDS_TRIGGERS) += power_supply_leds.o
+obj-$(CONFIG_OF_POWER_SUPPLY) += of_power_supply.o
+
obj-$(CONFIG_POWER_SUPPLY) += power_supply.o
obj-$(CONFIG_GENERIC_ADC_BATTERY) += generic-adc-battery.o
diff --git a/drivers/power/of_power_supply.c b/drivers/power/of_power_supply.c
new file mode 100644
index 0000000..25d93a1
--- /dev/null
+++ b/drivers/power/of_power_supply.c
@@ -0,0 +1,82 @@
+/*
+ * OF helpers for Power supply class
+ *
+ * Copyright (C) 2014 Samsung Electronics
+ * Chanwoo Choi <cw00.choi@xxxxxxxxxxx>
+ *
+ * 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/module.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/power_supply.h>
+#include <linux/power/of_power_supply.h>
+
+/*
+ * of_power_supply_get_dev - Get the power_supply device from devicetree
+ * @dev - instance to the given device
+ * @index - index into list of power_supply
+ *
+ * return the instance of power_supply device
+ */
+struct power_supply *of_power_supply_get_dev(struct device *dev,
+ enum power_supply_dev_type type,
+ int index)
+{
+ struct power_supply *power_supply_dev;
+ struct device_node *node;
+ const char *name;
+ union __parent_dev {
+ struct platform_device *platform;
+ struct i2c_client *i2c;
+ } parent_dev;
+
+ if (!dev->of_node) {
+ dev_dbg(dev, "device does not have a device node entry\n");
+ return ERR_PTR(-EINVAL);
+ }
+
+ if (type >= POWER_SUPPLY_DEV_END || type <= POWER_SUPPLY_DEV_UNKNOWN) {
+ dev_dbg(dev, "unknown power_supply device : %d\n", type);
+ return ERR_PTR(-EINVAL);
+ }
+
+ node = of_parse_phandle(dev->of_node, power_supply_dev_list[type],
+ index);
+ if (!node) {
+ dev_dbg(dev, "failed to get phandle in %s node\n",
+ dev->of_node->full_name);
+ return ERR_PTR(-ENODEV);
+ }
+
+ parent_dev.platform = of_find_device_by_node(node);
+ if (parent_dev.platform) {
+ name = dev_name(&parent_dev.platform->dev);
+ goto success;
+ }
+
+ parent_dev.i2c = of_find_i2c_device_by_node(node);
+ if (parent_dev.i2c) {
+ name = parent_dev.i2c->name;
+ goto success;
+ }
+
+ return ERR_PTR(-ENODEV);
+
+success:
+ power_supply_dev = power_supply_get_by_name(name);
+ if (!power_supply_dev) {
+ dev_dbg(dev, "unable to get power supply device : %s\n", name);
+ return ERR_PTR(-ENODEV);
+ }
+
+ return power_supply_dev;
+}
+EXPORT_SYMBOL_GPL(of_power_supply_get_dev);
diff --git a/include/linux/power/of_power_supply.h b/include/linux/power/of_power_supply.h
new file mode 100644
index 0000000..cfd4874
--- /dev/null
+++ b/include/linux/power/of_power_supply.h
@@ -0,0 +1,46 @@
+/*
+ * OF helpers for Power supply class
+ *
+ * Copyright (C) 2014 Samsung Electronics
+ * Chanwoo Choi <cw00.choi@xxxxxxxxxxx>
+ *
+ * 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.
+ */
+
+#ifndef __LINUX_OF_POWER_SUPPLY_H
+#define __LINUX_OF_POWER_SUPPLY_H
+
+#include <linux/err.h>
+
+enum power_supply_dev_type {
+ POWER_SUPPLY_DEV_UNKNOWN = 0,
+ POWER_SUPPLY_DEV_FUELGAUGE,
+ POWER_SUPPLY_DEV_CHARGER,
+
+ POWER_SUPPLY_DEV_END,
+};
+
+static const char * const power_supply_dev_list[] = {
+ [POWER_SUPPLY_DEV_UNKNOWN] = "unknown",
+ [POWER_SUPPLY_DEV_FUELGAUGE] = "fuelgauge",
+ [POWER_SUPPLY_DEV_CHARGER] = "charger",
+};
+
+#if IS_ENABLED(CONFIG_OF_POWER_SUPPLY)
+extern struct power_supply
+ *of_power_supply_get_dev(struct device *dev,
+ enum power_supply_dev_type type,
+ int index);
+#else
+static inline struct power_supply
+ *of_power_supply_get_dev(struct device *dev,
+ enum power_supply_dev_type type,
+ int index)
+{
+ return ERR_PTR(-ENOSYS);
+}
+#endif /* CONFIG_OF_POWER_SUPPLY */
+#endif /* __LINUX_OF_POWER_SUPPLY_H */
--
1.8.0
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/