public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Pali Rohár" <pali.rohar@gmail.com>
To: Anton Vorontsov <cbouatmailru@gmail.com>
Cc: David Woodhouse <dwmw2@infradead.org>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] power: bq27x00_battery: Export all battery registers via sysfs
Date: Sat, 19 Jan 2013 19:20:27 +0100	[thread overview]
Message-ID: <201301191920.27839@pali> (raw)
In-Reply-To: <1358604104-7667-1-git-send-email-pali.rohar@gmail.com>

[-- Attachment #1: Type: Text/Plain, Size: 5059 bytes --]

On Saturday 19 January 2013 15:01:43 Pali Rohár wrote:
> bq27xxx batteries have a lot of properties, more than
> power_supply interface. Some of them can be usefull for
> userspace applications (like CI bit) but does not make sense
> to add bq specified property to power_supply interface. When
> bq27x00_battery is not loaded userspace application (like
> i2cget) can use /dev/i2c-* interface for raw access. But when
> kernel module is attached to i2c device, userspace
> applications cannot access it via /dev/i2c-*. Also it is not
> usefull for userspace to unload module before reading values
> form /dev/i2c-* and then load it again.
> 
> This patch exporting all battery registers to sysfs entry
> "registers" in battery power_supply directory. Format is
> "register=value" on separate line. Because length of
> registers are different for each bq battery more for loops
> are needed.
> 
> Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
> ---
>  drivers/power/bq27x00_battery.c |   98
> +++++++++++++++++++++++++++++++++++++-- 1 file changed, 95
> insertions(+), 3 deletions(-)
> 

Opps, this patch causing null ptr derefernce. Here is new fixed version:

diff --git a/drivers/power/bq27x00_battery.c b/drivers/power/bq27x00_battery.c
index 7087d0d..4f54ad3 100644
--- a/drivers/power/bq27x00_battery.c
+++ b/drivers/power/bq27x00_battery.c
@@ -690,6 +690,90 @@ static void bq27x00_external_power_changed(struct power_supply *psy)
 	schedule_delayed_work(&di->work, 0);
 }
 
+/* code for register device access via sysfs */
+
+static ssize_t bq27x00_battery_sysfs_print_reg(struct bq27x00_device_info *di,
+					u8 reg, bool single, char *buf)
+{
+	int ret = bq27x00_read(di, reg, single);
+	if (ret < 0)
+		return sprintf(buf, "%#.2x=error %d\n", reg, ret);
+	else
+		return sprintf(buf, "%#.2x=%#.2x\n", reg, ret);
+}
+
+static ssize_t bq27x00_battery_sysfs_show_registers(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct power_supply *psy = dev_get_drvdata(dev);
+	struct bq27x00_device_info *di = container_of(psy,
+						struct bq27x00_device_info,
+						bat);
+	u8 reg;
+	ssize_t ret = 0;
+
+	switch (di->chip) {
+
+	case BQ27000:
+		for (reg=0x00; reg<=0x01; reg+=1)
+			ret += bq27x00_battery_sysfs_print_reg(di, reg, true, buf+ret);
+		for (reg=0x02; reg<=0x08; reg+=2)
+			ret += bq27x00_battery_sysfs_print_reg(di, reg, false, buf+ret);
+		for (reg=0x0A; reg<=0x0B; reg+=1)
+			ret += bq27x00_battery_sysfs_print_reg(di, reg, true, buf+ret);
+		for (reg=0x0C; reg<=0x2A; reg+=2)
+			ret += bq27x00_battery_sysfs_print_reg(di, reg, false, buf+ret);
+		for (reg=0x2C; reg<=0x7F; reg+=1)
+			ret += bq27x00_battery_sysfs_print_reg(di, reg, true, buf+ret);
+		break;
+
+	case BQ27500:
+		for (reg=0x00; reg<=0x2C; reg+=2)
+			ret += bq27x00_battery_sysfs_print_reg(di, reg, false, buf+ret);
+		for (reg=0x2E; reg<=0x3B; reg+=1)
+			ret += bq27x00_battery_sysfs_print_reg(di, reg, true, buf+ret);
+		for (reg=0x3C; reg<=0x3C; reg+=2)
+			ret += bq27x00_battery_sysfs_print_reg(di, reg, false, buf+ret);
+		for (reg=0x3E; reg<=0x7F; reg+=1)
+			ret += bq27x00_battery_sysfs_print_reg(di, reg, true, buf+ret);
+		break;
+
+	case BQ27425:
+		for (reg=0x00; reg<=0x3C; reg+=2)
+			ret += bq27x00_battery_sysfs_print_reg(di, reg, false, buf+ret);
+		for (reg=0x3E; reg<=0x7F; reg+=1)
+			ret += bq27x00_battery_sysfs_print_reg(di, reg, true, buf+ret);
+		break;
+
+	}
+
+	return ret;
+}
+
+static DEVICE_ATTR(registers, S_IRUGO,
+		bq27x00_battery_sysfs_show_registers, NULL);
+
+static struct attribute *bq27x00_battery_sysfs_attributes[] = {
+	&dev_attr_registers.attr,
+	NULL,
+};
+
+static const struct attribute_group bq27x00_battery_sysfs_attr_group = {
+	.attrs = bq27x00_battery_sysfs_attributes,
+};
+
+static int bq27x00_battery_sysfs_init(struct bq27x00_device_info *di)
+{
+	return sysfs_create_group(&di->bat.dev->kobj,
+			&bq27x00_battery_sysfs_attr_group);
+}
+
+static void bq27x00_battery_sysfs_exit(struct bq27x00_device_info *di)
+{
+	sysfs_remove_group(&di->bat.dev->kobj,
+			&bq27x00_battery_sysfs_attr_group);
+}
+
 static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
 {
 	int ret;
@@ -711,6 +795,15 @@ static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
 	ret = power_supply_register(di->dev, &di->bat);
 	if (ret) {
 		dev_err(di->dev, "failed to register battery: %d\n", ret);
+		mutex_destroy(&di->lock);
+		return ret;
+	}
+
+	ret = bq27x00_battery_sysfs_init(di);
+	if (ret) {
+		dev_err(di->dev, "failed to register battery: %d\n", ret);
+		power_supply_unregister(&di->bat);
+		mutex_destroy(&di->lock);
 		return ret;
 	}
 
@@ -733,6 +826,7 @@ static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
 
 	cancel_delayed_work_sync(&di->work);
 
+	bq27x00_battery_sysfs_exit(di);
 	power_supply_unregister(&di->bat);
 
 	mutex_destroy(&di->lock);


-- 
Pali Rohár
pali.rohar@gmail.com

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

  reply	other threads:[~2013-01-19 18:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-19 14:01 [PATCH] power: bq27x00_battery: Export all battery registers via sysfs Pali Rohár
2013-01-19 18:20 ` Pali Rohár [this message]
2013-01-20  2:00 ` Anton Vorontsov
2013-01-20  9:53 ` Lars-Peter Clausen
2013-03-26 19:27   ` Pali Rohár
2013-03-26 19:52     ` 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=201301191920.27839@pali \
    --to=pali.rohar@gmail.com \
    --cc=cbouatmailru@gmail.com \
    --cc=dwmw2@infradead.org \
    --cc=linux-kernel@vger.kernel.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