Skip to contentSkip to conversation

Add devicetree functionality to w1 busses

PATCH Superseded Daniel Mack 3 participants 8 messages Jun 28, 2018 – Jul 3, 2018

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add devicetree functionality to w1 busses

This patch set contains four small patches that bring devicetree functionality to w1 bus masters and slaves in general. As an example, the DS2760 driver is made aware of devicetree nodes as an example. Other drivers can easily be ported later.

W1 masters scan their bus in order to discover slave devices. Once one is found, a driver matching the family of the device is instanciated which handles it.

All that's needed for now is a call to of_find_matching_node() when a slave device is attached, so the corresponding of_node pointer is set. For devices such as the DS2760, another driver is probed which can access the w1 slave through dev->parent.

Patch #1 adds some DT bindings documentation
Patch #2 adds the call to of_find_matching_node() Patch #3 implements the match table for the DS2760 w1 slave Patch #4 makes the DS2760 supply driver aware of DT environments

This works fine on a PXA3xx based board with a battery attached to the w1-gpio bus master controller.

Daniel Mack (4):
dt-bindings: w1: document sub-node bindings for DS2760
w1: core: match sub-nodes of bus masters in devicetree
w1: ds2760: add devicetree matching glue
power: ds2760_battery: add device tree glue

 .../devicetree/bindings/w1/maxim,ds2760.txt    | 15 +++++++++++++++
 .../devicetree/bindings/w1/w1-gpio.txt         |  6 ++++++
 Documentation/devicetree/bindings/w1/w1.txt    | 18 ++++++++++++++++++
 drivers/power/supply/ds2760_battery.c          |  6 +++++-
 drivers/w1/slaves/w1_ds2760.c                  |  9 +++++++++
 drivers/w1/w1.c                                |  3 +++
 include/linux/w1.h                             |  2 ++
 7 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/w1/maxim,ds2760.txt
 create mode 100644 Documentation/devicetree/bindings/w1/w1.txt
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/4 dt-bindings: w1: document sub-node bindings for DS2760

This patch add a generic w1 bindings document that merely describes how slave deviceses are grouped under master nodes. It also adds a specific binding for the ds2760 battery monitor.

---
 .../devicetree/bindings/w1/maxim,ds2760.txt    | 15 +++++++++++++++
 .../devicetree/bindings/w1/w1-gpio.txt         |  6 ++++++
 Documentation/devicetree/bindings/w1/w1.txt    | 18 ++++++++++++++++++
 3 files changed, 39 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/w1/maxim,ds2760.txt
 create mode 100644 Documentation/devicetree/bindings/w1/w1.txt
Documentation/devicetree/bindings/w1/maxim,ds2760.txt new file +15 -0
new file mode 100644
index 000000000000..d916fdd213f7
@@ -0,0 +1,15 @@
+Devicetree bindings for Maxim DS2760
+====================================
+
+The ds2760 is a w1 slave device and must hence have its sub-node in DT
+under a w1 bus master node.
+
+The device exposes a power supply, so the details described in
+Documentation/devicetree/bindings/power/supply/power_supply.txt apply.
+
+Required properties:
+- compatible: must be "maxim,ds2760"
+
+Optional properties:
+- power-supplies:	Refers to one or more power supplies connected to
+			this battery.
Documentation/devicetree/bindings/w1/w1-gpio.txt +6 -0
index 6e09c35d9f1a..6694317bcbed 100644
@@ -13,10 +13,16 @@ Optional properties:
  - linux,open-drain: if specified, the data pin is considered in
 		     open-drain mode.
 
+Also refer to the generic w1.txt document.
+
 Examples:
 
 	onewire@0 {
 		compatible = "w1-gpio";
 		gpios = <&gpio 126 0>, <&gpio 105 0>;
+
+		battery {
+			compatible = "maxim,ds2760";
+		};
 	};
 
Documentation/devicetree/bindings/w1/w1.txt new file +18 -0
new file mode 100644
index 000000000000..6ac103f972a0
@@ -0,0 +1,18 @@
+Generic devicetree bindings for onewire (w1) busses
+===================================================
+
+Onewire busses are described through nodes of their master bus controller.
+Slave devices are listed as sub-nodes of such master devices. For now, one
+one slave is allowed per bus master.
+
+
+Example:
+
+	onewire {
+		compatible = "w1-gpio";
+		gpios = <&gpio 0 GPIO_ACTIVE_HIGH>;
+
+		battery {
+			compatible = "maxim,ds2760";
+		};
+	};
Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH v2 1/4] dt-bindings: w1: document sub-node bindings for DS2760

Quoted 42 lines · Daniel Mack

This patch add a generic w1 bindings document that merely describes how slave deviceses are grouped under master nodes. It also adds a specific binding for the ds2760 battery monitor.

---
 .../devicetree/bindings/w1/maxim,ds2760.txt    | 15 +++++++++++++++
 .../devicetree/bindings/w1/w1-gpio.txt         |  6 ++++++
 Documentation/devicetree/bindings/w1/w1.txt    | 18 ++++++++++++++++++
 3 files changed, 39 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/w1/maxim,ds2760.txt
 create mode 100644 Documentation/devicetree/bindings/w1/w1.txt
Documentation/devicetree/bindings/w1/maxim,ds2760.txt new file +15 -0
new file mode 100644
index 000000000000..d916fdd213f7
@@ -0,0 +1,15 @@
+Devicetree bindings for Maxim DS2760
+====================================
+
+The ds2760 is a w1 slave device and must hence have its sub-node in DT
+under a w1 bus master node.
+
+The device exposes a power supply, so the details described in
+Documentation/devicetree/bindings/power/supply/power_supply.txt apply.
+
+Required properties:
+- compatible: must be "maxim,ds2760"
+
+Optional properties:
+- power-supplies:	Refers to one or more power supplies connected to
+			this battery.
Documentation/devicetree/bindings/w1/w1-gpio.txt +0 -0
index 6e09c35d9f1a..6694317bcbed 100644
@@ -13,10 +13,16 @@ Optional properties:
  - linux,open-drain: if specified, the data pin is considered in
 		     open-drain mode.
 

Documentation/devicetree/bindings/w1/w1-gpio.txt Optional properties: · line 25

Quoted 10 lines
Documentation/devicetree/bindings/w1/w1-gpio.txt +5 -0
+Also refer to the generic w1.txt document.
+
 Examples:
 
 	onewire@0 {
 		compatible = "w1-gpio";
 		gpios = <&gpio 126 0>, <&gpio 105 0>;
+
+		battery {
+			compatible = "maxim,ds2760";

Perhaps show the power-supplies property as that's the whole point to have in DT.

Quoted 9 lines
Documentation/devicetree/bindings/w1/w1-gpio.txt +1 -0
+		};
 	};
 
Documentation/devicetree/bindings/w1/w1.txt new file +0 -0
new file mode 100644
index 000000000000..6ac103f972a0

Documentation/devicetree/bindings/w1/w1.txt line 6

Quoted 6 lines
Documentation/devicetree/bindings/w1/w1.txt new file +6 -0
@@ -0,0 +1,18 @@
+Generic devicetree bindings for onewire (w1) busses
+===================================================
+
+Onewire busses are described through nodes of their master bus controller.
+Slave devices are listed as sub-nodes of such master devices. For now, one
+one slave is allowed per bus master.

s/one one/only one/

Quoted 15 lines
Documentation/devicetree/bindings/w1/w1.txt new file +12 -0
+
+
+Example:
+
+	onewire {
+		compatible = "w1-gpio";
+		gpios = <&gpio 0 GPIO_ACTIVE_HIGH>;
+
+		battery {
+			compatible = "maxim,ds2760";
+		};
+	};
Signature · 1 line
2.17.1
#3 lore raw

PATCH v2 2/4 w1: core: match sub-nodes of bus masters in devicetree

In reply to Daniel Mack

Once a new slave device is detected, match it against all sub-nodes of the master bus controller. If a match is found, set the slave device's of_node pointer.

---
 drivers/w1/w1.c    | 3 +++
 include/linux/w1.h | 2 ++
 2 files changed, 5 insertions(+)
drivers/w1/w1.c +3 -0
index caef0e0fd817..890c038c25f8 100644
@@ -26,6 +26,7 @@
 #include <linux/kthread.h>
 #include <linux/freezer.h>
 #include <linux/hwmon.h>
+#include <linux/of.h>
 
 #include <linux/atomic.h>
 
@@ -686,6 +687,8 @@ static int __w1_attach_slave_device(struct w1_slave *sl)
 	sl->dev.bus = &w1_bus_type;
 	sl->dev.release = &w1_slave_release;
 	sl->dev.groups = w1_slave_groups;
+	sl->dev.of_node = of_find_matching_node(sl->master->dev.of_node,
+						sl->family->of_match_table);
 
 	dev_set_name(&sl->dev, "%02x-%012llx",
 		 (unsigned int) sl->reg_num.family,
include/linux/w1.h +2 -0
index 694101f744c7..3111585c371f 100644
@@ -274,6 +274,8 @@ struct w1_family {
 
 	struct w1_family_ops	*fops;
 
+	const struct of_device_id *of_match_table;
+
 	atomic_t		refcnt;
 };
 
Signature · 1 line
2.17.1
#4 lore raw

PATCH v2 3/4 w1: ds2760: add devicetree matching glue

In reply to Daniel Mack

Add an id table for ds2760 so it can be matched by the core.

---
 drivers/w1/slaves/w1_ds2760.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/w1/slaves/w1_ds2760.c +9 -0
index 26168abfb8b8..e1726a4e4b1d 100644
@@ -17,6 +17,7 @@
 #include <linux/mutex.h>
 #include <linux/idr.h>
 #include <linux/gfp.h>
+#include <linux/of.h>
 
 #include <linux/w1.h>
 
@@ -157,6 +158,13 @@ static void w1_ds2760_remove_slave(struct w1_slave *sl)
 	platform_device_unregister(pdev);
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id w1_ds2760_of_ids[] = {
+	{ .compatible = "maxim,ds2760" },
+	{}
+};
+#endif
+
 static struct w1_family_ops w1_ds2760_fops = {
 	.add_slave    = w1_ds2760_add_slave,
 	.remove_slave = w1_ds2760_remove_slave,
@@ -166,6 +174,7 @@ static struct w1_family_ops w1_ds2760_fops = {
 static struct w1_family w1_ds2760_family = {
 	.fid = W1_FAMILY_DS2760,
 	.fops = &w1_ds2760_fops,
+	.of_match_table = of_match_ptr(w1_ds2760_of_ids),
 };
 module_w1_family(w1_ds2760_family);
 
Signature · 1 line
2.17.1
#5 lore raw

PATCH v2 4/4 power: ds2760_battery: add device tree glue

In reply to Daniel Mack

The w1 slave device used by this driver now has a of_node in case it was matched against a devicetree sub-node of the bus master. This can now be passed down to the power supply core which will parse more properties from the node, such as 'power-supplies'.

---
 drivers/power/supply/ds2760_battery.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/power/supply/ds2760_battery.c +5 -1
index ae180dc929c9..77dfdeb5fbc0 100644
@@ -510,6 +510,10 @@ static int ds2760_battery_probe(struct platform_device *pdev)
 	char status;
 	int retval = 0;
 	struct ds2760_device_info *di;
+	struct device *w1_dev;
+
+	w1_dev = pdev->dev.parent;
+	psy_cfg.of_node = w1_dev->of_node;
 
 	di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
 	if (!di) {
@@ -520,7 +524,7 @@ static int ds2760_battery_probe(struct platform_device *pdev)
 	platform_set_drvdata(pdev, di);
 
 	di->dev				= &pdev->dev;
-	di->w1_dev			= pdev->dev.parent;
+	di->w1_dev			= w1_dev;
 	di->bat_desc.name		= dev_name(&pdev->dev);
 	di->bat_desc.type		= POWER_SUPPLY_TYPE_BATTERY;
 	di->bat_desc.properties		= ds2760_battery_props;
Signature · 1 line
2.17.1
#6 lore raw

Re: [PATCH v2 4/4] power: ds2760_battery: add device tree glue

Hi,

Quoted 7 lines · Daniel Mack

The w1 slave device used by this driver now has a of_node in case it was matched against a devicetree sub-node of the bus master. This can now be passed down to the power supply core which will parse more properties from the node, such as 'power-supplies'.

---

Thanks for the patch series. Having proper DT support is a nice addition. But instead of extending the platform device hack, please merge the w1 driver into ds2760_battery.c as it happened to the bq27000 driver already. The intermediate platform device is completly useless and only makes things more complicated.

Basically move all code from w1_ds2760.c into ds2760_battery.c, drop some EXPORT_SYMBOL and make functions static instead. Then move content from probe/remove function into add_slave/remove_slave callbacks and drop all platform device things.

-- Sebastian

#7 lore raw

Re: [PATCH v2 4/4] power: ds2760_battery: add device tree glue

Quoted 17 lines · Sebastian Reichel

Hi,

Quoted 7 lines · Daniel Mack

The w1 slave device used by this driver now has a of_node in case it was matched against a devicetree sub-node of the bus master. This can now be passed down to the power supply core which will parse more properties from the node, such as 'power-supplies'.

---

Thanks for the patch series. Having proper DT support is a nice addition. But instead of extending the platform device hack, please merge the w1 driver into ds2760_battery.c as it happened to the bq27000 driver already. The intermediate platform device is completly useless and only makes things more complicated.

Quoted 4 lines

Basically move all code from w1_ds2760.c into ds2760_battery.c, drop some EXPORT_SYMBOL and make functions static instead. Then move content from probe/remove function into add_slave/remove_slave callbacks and drop all platform device things.

Okay, yes, that makes sense. I never understood why there are multiple drivers for this.

Thanks,
Daniel