Skip to contentSkip to conversation

DT support for bq2415x

PATCH In review Sebastian Reichel 3 participants 11 messages Nov 24, 2013 – Dec 24, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover DT support for bq2415x

Hi,

This patchset adds DT support for the bq2415x charger, which is used in the Nokia N900. The changes depend on Pali Rohár's "[PATCH v2 0/3] Add support for charging battery in Nokia RX-51" patchset [0].

[0] https://lkml.org/lkml/2013/11/19/106

-- Sebastian

Sebastian Reichel (3):
power_supply: add power_supply_get_by_phandle
bq2415x_charger: add DT support
dt: binding documentation for bq2415x charger

 .../devicetree/bindings/power/bq2415x.txt          | 43 +++++++++++++
 drivers/power/bq2415x_charger.c                    | 72 ++++++++++++++++------
 drivers/power/power_supply_core.c                  | 24 ++++++++
 include/linux/power_supply.h                       |  2 +
 4 files changed, 121 insertions(+), 20 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/power/bq2415x.txt
Signature · 1 line
1.8.4.3
#1 lore raw

PATCH 1/3 power_supply: add power_supply_get_by_phandle

Add method to get power supply by device tree phandle.

---
 drivers/power/power_supply_core.c | 24 ++++++++++++++++++++++++
 include/linux/power_supply.h      |  2 ++
 2 files changed, 26 insertions(+)
drivers/power/power_supply_core.c +24 -0
index 08bce22..99e4b41 100644
@@ -340,6 +340,30 @@ struct power_supply *power_supply_get_by_name(const char *name)
 }
 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
 
+static int power_supply_match_device_node(struct device *dev, const void *data)
+{
+	return dev->parent && dev->parent->of_node == data;
+}
+
+struct power_supply *power_supply_get_by_phandle(struct device_node *np,
+							const char *property)
+{
+	struct device_node *power_supply_np;
+	struct device *dev;
+
+	power_supply_np = of_parse_phandle(np, property, 0);
+	if (!power_supply_np)
+		return ERR_PTR(-ENODEV);
+
+	dev = class_find_device(power_supply_class, NULL, power_supply_np,
+						power_supply_match_device_node);
+
+	of_node_put(power_supply_np);
+
+	return dev ? dev_get_drvdata(dev) : NULL;
+}
+EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
+
 int power_supply_powers(struct power_supply *psy, struct device *dev)
 {
 	return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
include/linux/power_supply.h +2 -0
index c6f52c0..4d0e0b4 100644
@@ -246,6 +246,8 @@ extern struct atomic_notifier_head    power_supply_notifier;
 extern int power_supply_reg_notifier(struct notifier_block *nb);
 extern void power_supply_unreg_notifier(struct notifier_block *nb);
 extern struct power_supply *power_supply_get_by_name(const char *name);
+extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
+							const char *property);
 extern void power_supply_changed(struct power_supply *psy);
 extern int power_supply_am_i_supplied(struct power_supply *psy);
 extern int power_supply_set_battery_charged(struct power_supply *psy);
Signature · 1 line
1.8.4.3
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] power_supply: add power_supply_get_by_phandle

Quoted 3 lines · Sebastian Reichel

Add method to get power supply by device tree phandle.

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#3 lore raw

Re: [PATCH 1/3] power_supply: add power_supply_get_by_phandle

In reply to Sebastian Reichel
Quoted 13 lines · Sebastian Reichel

Add method to get power supply by device tree phandle.

---
 drivers/power/power_supply_core.c | 24 ++++++++++++++++++++++++
 include/linux/power_supply.h      |  2 ++
 2 files changed, 26 insertions(+)
drivers/power/power_supply_core.c +0 -0
index 08bce22..99e4b41 100644
@@ -340,6 +340,30 @@ struct power_supply *power_supply_get_by_name(const char *name)

...

drivers/power/power_supply_core.c

Quoted 9 lines
drivers/power/power_supply_core.c +9 -0
+
+	dev = class_find_device(power_supply_class, NULL, power_supply_np,
+						power_supply_match_device_node);
+
+	of_node_put(power_supply_np);
+
+	return dev ? dev_get_drvdata(dev) : NULL;
+}
+EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);

The code had no !CONFIG_OF protection so I had to make the following changes to this patch:

drivers/power/power_supply_core.c +2 -0
index 23cd055..2660664 100644
@@ -341,6 +341,7 @@ struct power_supply *power_supply_get_by_name(const char *name)
 }
 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
 
+#ifdef CONFIG_OF
 static int power_supply_match_device_node(struct device *dev, const void *data)
 {
 	return dev->parent && dev->parent->of_node == data;
@@ -364,6 +365,7 @@ struct power_supply *power_supply_get_by_phandle(struct device_node *np,
 	return dev ? dev_get_drvdata(dev) : NULL;
 }
 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
+#endif /* CONFIG_OF */
 
 int power_supply_powers(struct power_supply *psy, struct device *dev)
 {
include/linux/power_supply.h +6 -0
index 8d06537..c9dc4e0 100644
@@ -244,8 +244,14 @@ extern struct atomic_notifier_head power_supply_notifier;
 extern int power_supply_reg_notifier(struct notifier_block *nb);
 extern void power_supply_unreg_notifier(struct notifier_block *nb);
 extern struct power_supply *power_supply_get_by_name(const char *name);
+#ifdef CONFIG_OF
 extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
 							const char *property);
+#else /* !CONFIG_OF */
+static inline struct power_supply *
+power_supply_get_by_phandle(struct device_node *np, const char *property)
+{ return NULL; }
+#endif /* CONFIG_OF */
 extern void power_supply_changed(struct power_supply *psy);
 extern int power_supply_am_i_supplied(struct power_supply *psy);
 extern int power_supply_set_battery_charged(struct power_supply *psy);

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw

Re: [PATCH 1/3] power_supply: add power_supply_get_by_phandle

Quoted 15 lines · Anton Vorontsov
Quoted 13 lines · Sebastian Reichel

Add method to get power supply by device tree phandle.

---
 drivers/power/power_supply_core.c | 24 ++++++++++++++++++++++++
 include/linux/power_supply.h      |  2 ++
 2 files changed, 26 insertions(+)
drivers/power/power_supply_core.c +0 -0
index 08bce22..99e4b41 100644
@@ -340,6 +340,30 @@ struct power_supply *power_supply_get_by_name(const char *name)

...

Quoted 12 lines

drivers/power/power_supply_core.c

Quoted 9 lines
drivers/power/power_supply_core.c +9 -0
+
+	dev = class_find_device(power_supply_class, NULL, power_supply_np,
+						power_supply_match_device_node);
+
+	of_node_put(power_supply_np);
+
+	return dev ? dev_get_drvdata(dev) : NULL;
+}
+EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);

The code had no !CONFIG_OF protection so I had to make the following changes to this patch:

Right. Thanks for queuing.

-- Sebastian

#5 lore raw

PATCH 2/3 bq2415x_charger: add DT support

In reply to Sebastian Reichel

This adds DT support to the bq2415x driver.

---
 drivers/power/bq2415x_charger.c | 72 +++++++++++++++++++++++++++++------------
 1 file changed, 52 insertions(+), 20 deletions(-)
drivers/power/bq2415x_charger.c +52 -20
index d89583d..76a140c 100644
@@ -170,6 +170,7 @@ struct bq2415x_device {
 	struct bq2415x_platform_data init_data;
 	struct power_supply charger;
 	struct delayed_work work;
+	struct power_supply *notify_psy;
 	struct notifier_block nb;
 	enum bq2415x_mode reported_mode;/* mode reported by hook function */
 	enum bq2415x_mode mode;		/* current configured mode */
@@ -806,7 +807,7 @@ static int bq2415x_notifier_call(struct notifier_block *nb,
 	if (val != PSY_EVENT_PROP_CHANGED)
 		return NOTIFY_OK;
 
-	if (strcmp(psy->name, bq->init_data.notify_device) != 0)
+	if (psy != bq->notify_psy)
 		return NOTIFY_OK;
 
 	dev_dbg(bq->dev, "notifier call was called\n");
@@ -1538,10 +1539,11 @@ static int bq2415x_probe(struct i2c_client *client,
 	int num;
 	char *name;
 	struct bq2415x_device *bq;
-	struct power_supply *psy;
+	struct device_node *np = client->dev.of_node;
+	struct bq2415x_platform_data *pdata = client->dev.platform_data;
 
-	if (!client->dev.platform_data) {
-		dev_err(&client->dev, "platform data not set\n");
+	if (!np && !pdata) {
+		dev_err(&client->dev, "platform data missing\n");
 		return -ENODEV;
 	}
 
@@ -1566,6 +1568,17 @@ static int bq2415x_probe(struct i2c_client *client,
 		goto error_2;
 	}
 
+	if (np) {
+		bq->notify_psy = power_supply_get_by_phandle(np, "ti,usb-charger-detection");
+
+		if (!bq->notify_psy)
+			return -EPROBE_DEFER;
+	}
+	else if (pdata->notify_device)
+		bq->notify_psy = power_supply_get_by_name(pdata->notify_device);
+	else
+		bq->notify_psy = NULL;
+
 	i2c_set_clientdata(client, bq);
 
 	bq->id = num;
@@ -1577,8 +1590,34 @@ static int bq2415x_probe(struct i2c_client *client,
 	bq->autotimer = 0;
 	bq->automode = 0;
 
-	memcpy(&bq->init_data, client->dev.platform_data,
-			sizeof(bq->init_data));
+	if (np) {
+		ret = of_property_read_u32(np, "ti,current-limit",
+				&bq->init_data.current_limit);
+		if (ret)
+			return ret;
+		ret = of_property_read_u32(np, "ti,weak-battery-voltage",
+				&bq->init_data.weak_battery_voltage);
+		if (ret)
+			return ret;
+		ret = of_property_read_u32(np, "ti,battery-regulation-voltage",
+				&bq->init_data.battery_regulation_voltage);
+		if (ret)
+			return ret;
+		ret = of_property_read_u32(np, "ti,charge-current",
+				&bq->init_data.charge_current);
+		if (ret)
+			return ret;
+		ret = of_property_read_u32(np, "ti,termination-current",
+				&bq->init_data.termination_current);
+		if (ret)
+			return ret;
+		ret = of_property_read_u32(np, "ti,resistor-sense",
+				&bq->init_data.resistor_sense);
+		if (ret)
+			return ret;
+	} else {
+		memcpy(&bq->init_data, pdata, sizeof(bq->init_data));
+	}
 
 	bq2415x_reset_chip(bq);
 
@@ -1600,25 +1639,18 @@ static int bq2415x_probe(struct i2c_client *client,
 		goto error_4;
 	}
 
-	if (bq->init_data.notify_device) {
+	if (bq->notify_psy) {
 		bq->nb.notifier_call = bq2415x_notifier_call;
 		ret = power_supply_reg_notifier(&bq->nb);
 		if (ret) {
 			dev_err(bq->dev, "failed to reg notifier: %d\n", ret);
 			goto error_5;
 		}
-		psy = power_supply_get_by_name(bq->init_data.notify_device);
-		if (psy) {
-			/* Query for initial reported_mode and set it */
-			bq2415x_notifier_call(&bq->nb,
-					PSY_EVENT_PROP_CHANGED, psy);
-			bq2415x_set_mode(bq, bq->reported_mode);
-		} else {
-			dev_info(bq->dev, "notifier power supply device (%s) "
-				"for automode is not registred yet... "
-				"automode will not work without that device\n",
-				bq->init_data.notify_device);
-		}
+
+		/* Query for initial reported_mode and set it */
+		bq2415x_notifier_call(&bq->nb, PSY_EVENT_PROP_CHANGED, bq->notify_psy);
+		bq2415x_set_mode(bq, bq->reported_mode);
+
 		bq->automode = 1;
 		dev_info(bq->dev, "automode enabled\n");
 	} else {
@@ -1653,7 +1685,7 @@ static int bq2415x_remove(struct i2c_client *client)
 {
 	struct bq2415x_device *bq = i2c_get_clientdata(client);
 
-	if (bq->init_data.notify_device)
+	if (bq->notify_psy)
 		power_supply_unreg_notifier(&bq->nb);
 
 	bq2415x_sysfs_exit(bq);
Signature · 1 line
1.8.4.3
#7 lore raw

PATCH 3/3 dt: binding documentation for bq2415x charger

In reply to Sebastian Reichel

Add devicetree binding documentation for bq2415x charger.

---
 .../devicetree/bindings/power/bq2415x.txt          | 43 ++++++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/bq2415x.txt
Documentation/devicetree/bindings/power/bq2415x.txt new file +43 -0
new file mode 100644
index 0000000..cb2e753
@@ -0,0 +1,43 @@
+Binding for TI bq2415x Li-Ion Charger
+
+Required properties:
+- compatible: Should contain one of the following:
+ * "ti,bq24150"
+ * "ti,bq24150"
+ * "ti,bq24150a"
+ * "ti,bq24151"
+ * "ti,bq24151a"
+ * "ti,bq24152"
+ * "ti,bq24153"
+ * "ti,bq24153a"
+ * "ti,bq24155"
+ * "ti,bq24156"
+ * "ti,bq24156a"
+ * "ti,bq24158"
+- reg: integer, i2c address of the device
+- ti,current-limit: integer, current limit in mA
+- ti,weak-battery-voltage: integer, weak battery voltage threshold in mV
+- ti,battery-regulation-voltage: integer, battery regulation voltage in mV
+- ti,charge-current: integer, charging current in mA
+- ti,termination-current: integer, termination current in mA
+- ti,resistor-sense: integer, value of sensing resistor in milliohm
+
+Optional properties:
+- ti,usb-charger-detection: phandle to usb charger detection device
+			    (required for auto mode)
+
+Example from Nokia N900:
+
+bq24150a {
+	compatible = "ti,bq24150a";
+	reg = <0x6b>;
+
+	ti,current-limit = <100>;
+	ti,weak-battery-voltage = <3400>;
+	ti,battery-regulation-voltage = <4200>;
+	ti,charge-current = <650>;
+	ti,termination-current = <100>;
+	ti,resistor-sense = <68>;
+
+	ti,usb-charger-detection = <&isp1704>;
+};
Signature · 1 line
1.8.4.3
#8 lore raw

Re: [PATCH 3/3] dt: binding documentation for bq2415x charger

Quoted 3 lines · Sebastian Reichel

Add devicetree binding documentation for bq2415x charger.

Thanks!

Quoted 2 lines
+- reg: integer, i2c address of the device
+- ti,current-limit: integer, current limit in mA

Does this need to be "ti," specific? Most of fields are likely to be needed for other li-ion chargers....

"Specifies maximum current charger can pull from power supply"

?

Quoted 1 line

+- ti,charge-current: integer, charging current in mA

...why/how is it different from current-limit? Is the current-limit on 5V, while the charge-current relative to battery voltage... so that charge-current can be > current-limit when battery voltage is low?

"Maximum current that will be supplied to the battery, as determined by voltage on current sense resistor"?

Quoted 1 line

+- ti,weak-battery-voltage: integer, weak battery voltage threshold in mV

It would be good to explain what this threshold means. Voltage so low that system needs to be shut down?

Hmm, it looks to me like it is "as long as battery is below this voltage, fast charge is not started. Instead, slow 'precharge' is performed."

Quoted 2 lines

+- ti,battery-regulation-voltage: integer, battery regulation
voltage in mV

"Selects maximum voltage for charging."

Quoted 1 line

+- ti,termination-current: integer, termination current in mA

"When current in constant-voltage phase drops below this value, charge is terminated".

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#9 lore raw
In reply to Sebastian Reichel
Quoted 3 lines · Sebastian Reichel

This patchset adds DT support for the bq2415x charger, which is used in the Nokia N900. The changes depend on Pali Rohár's "[PATCH v2 0/3] Add support for charging battery in Nokia RX-51" patchset [0].

Patches 1/3 and 2/3 applied, thanks a lot!

Anton
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw
Quoted 6 lines · Anton Vorontsov
Quoted 3 lines · Sebastian Reichel

This patchset adds DT support for the bq2415x charger, which is used in the Nokia N900. The changes depend on Pali Rohár's "[PATCH v2 0/3] Add support for charging battery in Nokia RX-51" patchset [0].

Patches 1/3 and 2/3 applied, thanks a lot!

Thanks. I will try to update Patch 3 at the weekend. It should be added to the mainline kernel together with Patch 2 :)

-- Sebastian