LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Lars-Peter Clausen <lars@metafoo.de>
To: Anton Vorontsov <cbouatmailru@gmail.com>
Cc: Rodolfo Giometti <giometti@linux.it>,
	Grazvydas Ignotas <notasas@gmail.com>,
	linux-kernel@vger.kernel.org,
	Lars-Peter Clausen <lars@metafoo.de>
Subject: [PATCH 05/14] POWER: bq27x00: Prepare code for addition of bq27000 platform driver
Date: Sun,  6 Feb 2011 01:48:02 +0100	[thread overview]
Message-ID: <1296953291-10373-6-git-send-email-lars@metafoo.de> (raw)
In-Reply-To: <1296953291-10373-1-git-send-email-lars@metafoo.de>

This patch simplifies the drivers data structure and moves code to be shared by
both init functions into a common function.
This patch has no functional changes, it only moves code around.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
 drivers/power/bq27x00_battery.c |   86 +++++++++++++++++---------------------
 1 files changed, 39 insertions(+), 47 deletions(-)

diff --git a/drivers/power/bq27x00_battery.c b/drivers/power/bq27x00_battery.c
index 9f16666..eee0120 100644
--- a/drivers/power/bq27x00_battery.c
+++ b/drivers/power/bq27x00_battery.c
@@ -54,8 +54,8 @@ static DEFINE_MUTEX(battery_mutex);
 
 struct bq27x00_device_info;
 struct bq27x00_access_methods {
-	int (*read)(u8 reg, int *rt_value, int b_single,
-		struct bq27x00_device_info *di);
+	int (*read)(struct bq27x00_device_info *, u8 reg, int *rt_value,
+			bool single);
 };
 
 enum bq27x00_chip { BQ27000, BQ27500 };
@@ -63,11 +63,11 @@ enum bq27x00_chip { BQ27000, BQ27500 };
 struct bq27x00_device_info {
 	struct device 		*dev;
 	int			id;
-	struct bq27x00_access_methods	*bus;
-	struct power_supply	bat;
 	enum bq27x00_chip	chip;
 
-	struct i2c_client	*client;
+	struct power_supply	bat;
+
+	struct bq27x00_access_methods bus;
 };
 
 static enum power_supply_property bq27x00_battery_props[] = {
@@ -87,10 +87,10 @@ static enum power_supply_property bq27x00_battery_props[] = {
  * Common code for BQ27x00 devices
  */
 
-static int bq27x00_read(u8 reg, int *rt_value, int b_single,
-			struct bq27x00_device_info *di)
+static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
+		int *rt_value, bool single)
 {
-	return di->bus->read(reg, rt_value, b_single, di);
+	return di->bus.read(di, reg, rt_value, single);
 }
 
 /*
@@ -102,7 +102,7 @@ static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
 	int ret;
 	int temp = 0;
 
-	ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
+	ret = bq27x00_read(di, BQ27x00_REG_TEMP, &temp, false);
 	if (ret) {
 		dev_err(di->dev, "error reading temperature\n");
 		return ret;
@@ -123,7 +123,7 @@ static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
 	int ret;
 	int volt = 0;
 
-	ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
+	ret = bq27x00_read(di, BQ27x00_REG_VOLT, &volt, false);
 	if (ret) {
 		dev_err(di->dev, "error reading voltage\n");
 		return ret;
@@ -143,7 +143,7 @@ static int bq27x00_battery_current(struct bq27x00_device_info *di)
 	int curr = 0;
 	int flags = 0;
 
-	ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
+	ret = bq27x00_read(di, BQ27x00_REG_AI, &curr, false);
 	if (ret) {
 		dev_err(di->dev, "error reading current\n");
 		return 0;
@@ -153,7 +153,7 @@ static int bq27x00_battery_current(struct bq27x00_device_info *di)
 		/* bq27500 returns signed value */
 		curr = (int)((s16)curr) * 1000;
 	} else {
-		ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
+		ret = bq27x00_read(di, BQ27x00_REG_FLAGS, &flags, false);
 		if (ret < 0) {
 			dev_err(di->dev, "error reading flags\n");
 			return 0;
@@ -178,9 +178,9 @@ static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
 	int rsoc = 0;
 
 	if (di->chip == BQ27500)
-		ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di);
+		ret = bq27x00_read(di, BQ27500_REG_SOC, &rsoc, false);
 	else
-		ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di);
+		ret = bq27x00_read(di, BQ27000_REG_RSOC, &rsoc, true);
 	if (ret) {
 		dev_err(di->dev, "error reading relative State-of-Charge\n");
 		return ret;
@@ -196,7 +196,7 @@ static int bq27x00_battery_status(struct bq27x00_device_info *di,
 	int status;
 	int ret;
 
-	ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
+	ret = bq27x00_read(di, BQ27x00_REG_FLAGS, &flags, false);
 	if (ret < 0) {
 		dev_err(di->dev, "error reading flags\n");
 		return ret;
@@ -230,7 +230,7 @@ static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg,
 	int tval = 0;
 	int ret;
 
-	ret = bq27x00_read(reg, &tval, 0, di);
+	ret = bq27x00_read(reg, &tval, false);
 	if (ret) {
 		dev_err(di->dev, "error reading register %02x\n", reg);
 		return ret;
@@ -296,23 +296,35 @@ static int bq27x00_battery_get_property(struct power_supply *psy,
 	return ret;
 }
 
-static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
+static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
 {
+	int ret;
+
 	di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
 	di->bat.properties = bq27x00_battery_props;
 	di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
 	di->bat.get_property = bq27x00_battery_get_property;
 	di->bat.external_power_changed = NULL;
+
+	ret = power_supply_register(di->dev, &di->bat);
+	if (ret) {
+		dev_err(di->dev, "failed to register battery: %d\n", ret);
+		return ret;
+	}
+
+	dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
+
+	return 0;
 }
 
 /*
  * i2c specific code
  */
 
-static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
-			struct bq27x00_device_info *di)
+static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg,
+			int *rt_value, bool single)
 {
-	struct i2c_client *client = di->client;
+	struct i2c_client *client = to_i2c_client(di->dev);
 	struct i2c_msg msg[1];
 	unsigned char data[2];
 	int err;
@@ -329,7 +341,7 @@ static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
 	err = i2c_transfer(client->adapter, msg, 1);
 
 	if (err >= 0) {
-		if (!b_single)
+		if (!single)
 			msg->len = 2;
 		else
 			msg->len = 1;
@@ -337,7 +349,7 @@ static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
 		msg->flags = I2C_M_RD;
 		err = i2c_transfer(client->adapter, msg, 1);
 		if (err >= 0) {
-			if (!b_single)
+			if (!single)
 				*rt_value = get_unaligned_le16(data);
 			else
 				*rt_value = data[0];
@@ -353,7 +365,6 @@ static int bq27x00_battery_probe(struct i2c_client *client,
 {
 	char *name;
 	struct bq27x00_device_info *di;
-	struct bq27x00_access_methods *bus;
 	int num;
 	int retval = 0;
 
@@ -380,38 +391,20 @@ static int bq27x00_battery_probe(struct i2c_client *client,
 		retval = -ENOMEM;
 		goto batt_failed_2;
 	}
+
 	di->id = num;
+	di->dev = &client->dev;
 	di->chip = id->driver_data;
+	di->bat.name = name;
+	di->bus.read = &bq27x00_read_i2c;
 
-	bus = kzalloc(sizeof(*bus), GFP_KERNEL);
-	if (!bus) {
-		dev_err(&client->dev, "failed to allocate access method "
-					"data\n");
-		retval = -ENOMEM;
+	if (bq27x00_powersupply_init(di))
 		goto batt_failed_3;
-	}
 
 	i2c_set_clientdata(client, di);
-	di->dev = &client->dev;
-	di->bat.name = name;
-	bus->read = &bq27x00_read_i2c;
-	di->bus = bus;
-	di->client = client;
-
-	bq27x00_powersupply_init(di);
-
-	retval = power_supply_register(&client->dev, &di->bat);
-	if (retval) {
-		dev_err(&client->dev, "failed to register battery\n");
-		goto batt_failed_4;
-	}
-
-	dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
 
 	return 0;
 
-batt_failed_4:
-	kfree(bus);
 batt_failed_3:
 	kfree(di);
 batt_failed_2:
@@ -430,7 +423,6 @@ static int bq27x00_battery_remove(struct i2c_client *client)
 
 	power_supply_unregister(&di->bat);
 
-	kfree(di->bus);
 	kfree(di->bat.name);
 
 	mutex_lock(&battery_mutex);
-- 
1.7.2.3


  parent reply	other threads:[~2011-02-06 19:36 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-06  0:47 [PATCH 00/14] POWER: BQ27x00: New Properties, fixes, bq27000 support Lars-Peter Clausen
2011-02-06  0:47 ` [PATCH 01/14] POWER: bq27x00: Add type property Lars-Peter Clausen
2011-02-06 16:43   ` Rodolfo Giometti
2011-02-06  0:47 ` [PATCH 02/14] POWER: bq27x00: Improve temperature precession Lars-Peter Clausen
2011-02-07  8:06   ` Rodolfo Giometti
2011-02-06  0:48 ` [PATCH 03/14] POWER: bq27x00: Fix current now property Lars-Peter Clausen
2011-02-07  8:07   ` Rodolfo Giometti
2011-02-06  0:48 ` [PATCH 04/14] POWER: bq27x00: Return -ENODEV for properties if the battery is not present Lars-Peter Clausen
2011-02-06 16:40   ` Rodolfo Giometti
2011-02-06  0:48 ` Lars-Peter Clausen [this message]
2011-02-07  8:09   ` [PATCH 05/14] POWER: bq27x00: Prepare code for addition of bq27000 platform driver Rodolfo Giometti
2011-02-06  0:48 ` [PATCH 06/14] POWER: bq27x00: Add bq27000 support Lars-Peter Clausen
2011-02-06  0:48 ` [PATCH 07/14] POWER: bq27X00: Cache battery registers Lars-Peter Clausen
2011-02-06  0:48 ` [PATCH 08/14] POWER: bq27x00: Poll battery state Lars-Peter Clausen
2011-02-06  0:48 ` [PATCH 09/14] POWER: bq27x00: Add new properties Lars-Peter Clausen
2011-02-06  0:48 ` [PATCH 10/14] POWER: bq27x00: Add MODULE_DEVICE_TABLE Lars-Peter Clausen
2011-02-06 16:49   ` Rodolfo Giometti
2011-02-06  0:48 ` [PATCH 11/14] POWER: bq27x00: Give more specific reports on battery status Lars-Peter Clausen
2011-02-06 16:38   ` Rodolfo Giometti
2011-02-06  0:48 ` [PATCH 12/14] POWER: bq27x00: Minor cleanups Lars-Peter Clausen
2011-02-06  0:48 ` [PATCH 13/14] POWER: bq27x00: Cleanup bq27x00_i2c_read Lars-Peter Clausen
2011-02-06  0:48 ` [PATCH 14/14] POWER: Ignore -ENODATA errors when generating a uevent Lars-Peter Clausen
2011-02-06 22:47 ` [PATCH 00/14] POWER: BQ27x00: New Properties, fixes, bq27000 support Felipe Contreras
2011-02-06 23:09   ` Lars-Peter Clausen
2011-02-07  0:00     ` Felipe Contreras
2011-02-07  0:12       ` Lars-Peter Clausen
2011-02-07 10:52     ` Pali Rohár
2011-02-07  0:58 ` Grazvydas Ignotas
2011-02-07  1:30   ` Lars-Peter Clausen
2011-02-08 11:40     ` Grazvydas Ignotas
2011-02-08 16:13       ` Lars-Peter Clausen
2011-02-08 16:28         ` Anton Vorontsov
2011-02-08 17:09           ` Lars-Peter Clausen

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=1296953291-10373-6-git-send-email-lars@metafoo.de \
    --to=lars@metafoo.de \
    --cc=cbouatmailru@gmail.com \
    --cc=giometti@linux.it \
    --cc=linux-kernel@vger.kernel.org \
    --cc=notasas@gmail.com \
    --subject='Re: [PATCH 05/14] POWER: bq27x00: Prepare code for addition of bq27000 platform driver' \
    /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

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).