power: battery: Generic battery driver using IIO
Subsystems (4)
RFC PATCH 1/1 power: battery: Generic battery driver using IIO
From: anish kumar <[email protected]>
This patch is to use IIO to write a generic batttery driver. There are some inherent assumptions here:
1.User is having both main battery and backup battery. 2.Both batteries use same channel to get the information.
--- drivers/power/Kconfig | 8 + drivers/power/Makefile | 1 + drivers/power/generic-battery.c | 374 +++++++++++++++++++++++++++++++++ include/linux/power/generic-battery.h | 26 +++ 4 files changed, 409 insertions(+), 0 deletions(-) create mode 100644 drivers/power/generic-battery.c create mode 100644 include/linux/power/generic-battery.h
drivers/power/Kconfig +8 -0
@@ -309,6 +309,14 @@ config AB8500_BATTERY_THERM_ON_BATCTRL help Say Y to enable battery temperature measurements using thermistor connected on BATCTRL ADC. + +config GENERIC_BATTERY + tristate "Generic battery support using IIO" + depends on IIO + help + Say Y here to enable support for the generic battery driver + which uses IIO framework to read adc for it's main and backup + battery. endif # POWER_SUPPLY source "drivers/power/avs/Kconfig"
drivers/power/Makefile +1 -0
@@ -45,3 +45,4 @@ obj-$(CONFIG_CHARGER_MAX8997) += max8997_charger.o obj-$(CONFIG_CHARGER_MAX8998) += max8998_charger.o obj-$(CONFIG_POWER_AVS) += avs/ obj-$(CONFIG_CHARGER_SMB347) += smb347-charger.o +obj-$(CONFIG_GENERIC_BATTERY) += generic-battery.o
drivers/power/generic-battery.c new file +374 -0
374 lines not on this page · raw message
include/linux/power/generic-battery.h new file +26 -0
@@ -0,0 +1,26 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef GENERIC_BATTERY_H +#define GENERIC_BATTERY_H + +#include <linux/power_supply.h> + +/** +* struct generic_platform_data - platform data for generic battery +* @battery_info: Information about the battery +* @cal_charge: platform callback to calculate charge +* @gpio_charge_finished: GPIO number used for interrupts +* @gpio_inverted: Is the gpio inverted? +*/ +struct generic_platform_data { +struct power_supply_info battery_info; +int (*cal_charge)(long ); +int gpio_charge_finished; +int gpio_inverted; +}; + +#endif /* GENERIC_BATTERY_H */
Signature · 1 line
1.7.1
Quoted 460 lines · anish kumar
From: anish kumar <[email protected]>
This patch is to use IIO to write a generic batttery driver. There are some inherent assumptions here:
1.User is having both main battery and backup battery. 2.Both batteries use same channel to get the information.Signed-off-by anish kumar <[email protected]>--- drivers/power/Kconfig | 8 + drivers/power/Makefile | 1 + drivers/power/generic-battery.c | 374 +++++++++++++++++++++++++++++++++ include/linux/power/generic-battery.h | 26 +++ 4 files changed, 409 insertions(+), 0 deletions(-) create mode 100644 drivers/power/generic-battery.c create mode 100644 include/linux/power/generic-battery.hdrivers/power/Kconfig +8
-0@@ -309,6 +309,14 @@ config AB8500_BATTERY_THERM_ON_BATCTRL help Say Y to enable battery temperature measurements using thermistor connected on BATCTRL ADC. + +config GENERIC_BATTERY + tristate "Generic battery support using IIO" + depends on IIO + help + Say Y here to enable support for the generic battery driver + which uses IIO framework to read adc for it's main and backup + battery. endif # POWER_SUPPLY source "drivers/power/avs/Kconfig"drivers/power/Makefile +1
-0@@ -45,3 +45,4 @@ obj-$(CONFIG_CHARGER_MAX8997) += max8997_charger.o obj-$(CONFIG_CHARGER_MAX8998) += max8998_charger.o obj-$(CONFIG_POWER_AVS) += avs/ obj-$(CONFIG_CHARGER_SMB347) += smb347-charger.o +obj-$(CONFIG_GENERIC_BATTERY) += generic-battery.odrivers/power/generic-battery.c new file +374
-0@@ -0,0 +1,374 @@ +/* +* Generice battery driver code using IIO +* Copyright (C) 2012, Anish Kumar <[email protected]> +* based on jz4740-battery.c +* based on s3c_adc_battery.c +* +* This file is subject to the terms and conditions of the GNU General Public +* License. See the file COPYING in the main directory of this archive for +* more details. +* +*/ + +#include <linux/interrupt.h> +#include <linux/platform_device.h> +#include <linux/power_supply.h> +#include <linux/gpio.h> +#include <linux/err.h> +#include <linux/timer.h> +#include <linux/jiffies.h> +#include <linux/errno.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/iio/consumer.h> +#include <linux/iio/types.h> + +#include <linux/power/generic-battery.h> + +#define BAT_POLL_INTERVAL 10000/* ms */ +#define JITTER_DELAY 500/* ms */ + +enum BAT { +MAIN_BAT =0 , +BACKUP_BAT , +BAT_MAX , +}; + +struct generic_adc_bat { +struct power_supply psy; +struct iio_channel *channels; +int channel_index; +}; + +struct generic_bat { +struct generic_adc_bat bat[BAT_MAX ]; +struct generic_platform_data pdata; +int volt_value; +int cur_value; +unsigned int timestamp; +int level; +int status; +int cable_plugged:1 ; +}; + +static struct generic_bat generic_bat = { + .bat[MAIN_BAT ] = { + .psy.name ="main-bat" , + }, + .bat[BACKUP_BAT ] = { + .psy.name ="backup-bat" , + }, +}; + +static struct delayed_work bat_work; + +static void generic_adc_bat_ext_power_changed (struct power_supply *psy) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +} + +static enum power_supply_property generic_adc_main_bat_props[] = { +POWER_SUPPLY_PROP_STATUS , +POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN , +POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN , +POWER_SUPPLY_PROP_CHARGE_NOW , +POWER_SUPPLY_PROP_VOLTAGE_NOW , +POWER_SUPPLY_PROP_CURRENT_NOW , +POWER_SUPPLY_PROP_TECHNOLOGY , +POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN , +POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN , +POWER_SUPPLY_PROP_MODEL_NAME , +}; + +static int charge_finished (struct generic_bat *bat) +{ +return bat->pdata.gpio_inverted ? + !gpio_get_value (bat->pdata.gpio_charge_finished) : +gpio_get_value (bat->pdata.gpio_charge_finished); +} + +static int generic_adc_bat_get_property (struct power_supply *psy, +enum power_supply_property psp, +union power_supply_propval *val) +{ +struct generic_adc_bat *adc_bat; +struct generic_bat *bat; +int ret, scaleint, scalepart, iio_val; +long result =0 ; + +if (!strcmp (psy->name,"main-battery" )) { + adc_bat =container_of (psy, + struct generic_adc_bat, psy); + bat =container_of (adc_bat, + struct generic_bat, bat[MAIN_BAT]); + }else if (!strcmp (psy->name,"backup-battery" )) { + adc_bat = container_of(psy, struct generic_adc_bat, psy); + bat =container_of (adc_bat, + struct generic_bat, bat[BACKUP_BAT]); + }else { +/* Ideally this should never happen */ +WARN_ON (1 ); +return -EINVAL ; + } + +if (!bat) { +dev_err (psy->dev,"no battery infos ?!\n" ); +return -EINVAL ; + } + ret =iio_read_channel_raw (&adc_bat->channels[adc_bat->channel_index], + &iio_val); + ret =iio_read_channel_scale (&adc_bat->channels[adc_bat->channel_index], + &scaleint, &scalepart); +if (ret <0 ) +return ret; + +switch (ret) { +case IIO_VAL_INT : + result = iio_val * scaleint; +break ; +case IIO_VAL_INT_PLUS_MICRO : + result = (s64 )iio_val * (s64 )scaleint + +div_s64 ((s64 )iio_val * (s64 )scalepart,1000000LL ); +break ; +case IIO_VAL_INT_PLUS_NANO : + result = (s64 )iio_val * (s64 )scaleint + +div_s64 ((s64 )iio_val * (s64 )scalepart,1000000000LL ); +break ; + } + +switch (psp) { +case POWER_SUPPLY_PROP_STATUS : +if (bat->pdata.gpio_charge_finished <0 ) + val->intval = bat->level ==100000 ? +POWER_SUPPLY_STATUS_FULL : bat->status; +else + val->intval = bat->status; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN : + val->intval =0 ; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_NOW : + val->intval = bat->pdata.cal_charge (result); +return 0 ; +case POWER_SUPPLY_PROP_VOLTAGE_NOW : + val->intval = result; +return 0 ; +case POWER_SUPPLY_PROP_CURRENT_NOW : + val->intval = result; +return 0 ; +case POWER_SUPPLY_PROP_TECHNOLOGY : + val->intval = bat->pdata.battery_info.technology; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN : + val->intval = bat->pdata.battery_info.voltage_min_design; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN : + val->intval = bat->pdata.battery_info.voltage_max_design; +break ; +case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN : + val->intval = bat->pdata.battery_info.charge_full_design; +break ; +case POWER_SUPPLY_PROP_MODEL_NAME : + val->strval = bat->pdata.battery_info.name; +break ; +default : +return -EINVAL ; + } +return ret; +} + +static void generic_adc_bat_work (struct work_struct *work) +{ +struct generic_bat *bat = &generic_bat; +int is_charged; +int is_plugged; +static int was_plugged; + +/* backup battery doesn't have current monitoring capability anyway */ + is_plugged =power_supply_am_i_supplied (&bat->bat[MAIN_BAT ].psy); + bat->cable_plugged = is_plugged; +if (is_plugged != was_plugged) { + was_plugged = is_plugged; +if (is_plugged) + bat->status =POWER_SUPPLY_STATUS_CHARGING ; +else + bat->status =POWER_SUPPLY_STATUS_DISCHARGING ; + }else { +if ((bat->pdata.gpio_charge_finished >=0 ) && is_plugged) { + is_charged =charge_finished (&generic_bat); +if (is_charged) + bat->status =POWER_SUPPLY_STATUS_FULL ; +else + bat->status =POWER_SUPPLY_STATUS_CHARGING ; + } + } + +power_supply_changed (&bat->bat[MAIN_BAT ].psy); +} + +static irqreturn_t generic_adc_bat_charged (int irq,void *dev_id) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +return IRQ_HANDLED ; +} + +static int __devinit generic_adc_bat_probe (struct platform_device *pdev) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct iio_channel *channels; +int num_channels =0 ; +int ret, i, j, k =0 ; +enum iio_chan_type type; + +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) { + generic_bat.bat[i].psy.type =POWER_SUPPLY_TYPE_BATTERY ; + generic_bat.bat[i].psy.properties = + generic_adc_main_bat_props; + generic_bat.bat[i].psy.num_properties = +ARRAY_SIZE (generic_adc_main_bat_props); + generic_bat.bat[i].psy.get_property = + generic_adc_bat_get_property; + generic_bat.bat[i].psy.external_power_changed = + generic_adc_bat_ext_power_changed; + } + + generic_bat.volt_value =-1 ; + generic_bat.cur_value =-1 ; + generic_bat.cable_plugged =0 ; + generic_bat.status =POWER_SUPPLY_STATUS_DISCHARGING ; + +memcpy (&generic_bat.pdata, pdata,sizeof (struct generic_platform_data )); + +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) { + ret =power_supply_register (&pdev->dev, + &generic_bat.bat[i].psy); +if (ret) + goto err_reg_main; + } + + channels =iio_channel_get_all (dev_name (&pdev->dev)); +if (IS_ERR (channels)) { + ret =PTR_ERR (channels); + goto err_reg_backup; + } + +while (channels[num_channels].indio_dev) + num_channels++; + +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) + generic_bat.bat[i].channels =kzalloc (sizeof (struct iio_channel ) + *BAT_MAX ,GFP_KERNEL ); + +/* assuming main battery and backup battery is using the same channel */ +for (i =0 ; i < num_channels; i++) { + ret =iio_get_channel_type (&channels[i], &type); +if (ret <0 ) + goto err_gpio; + +if (type ==IIO_VOLTAGE || type ==IIO_CURRENT ) { +for (j =0 ; j <BAT_MAX ; j++) { + generic_bat.bat[j].channel_index = k; + generic_bat.bat[j].channels[k] = channels[i]; + } + k++; + } +continue ; + } + +INIT_DELAYED_WORK (&bat_work, generic_adc_bat_work); + +if (pdata->gpio_charge_finished >=0 ) { + ret =gpio_request (pdata->gpio_charge_finished,"charged" ); +if (ret) + goto err_gpio; + + ret =request_irq (gpio_to_irq (pdata->gpio_charge_finished), + generic_adc_bat_charged, +IRQF_TRIGGER_RISING |IRQF_TRIGGER_FALLING , +"battery charged" ,NULL ); +if (ret) + goto err_gpio; + } + +dev_info (&pdev->dev,"successfully loaded\n" ); + +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +iio_channel_release_all (channels); + +return 0 ; + +err_gpio: +iio_channel_release_all (channels); +err_reg_backup: +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy); +err_reg_main: +return ret; +} + +static int generic_adc_bat_remove (struct platform_device *pdev) +{ +int i; +struct generic_platform_data *pdata = pdev->dev.platform_data; + +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy); + +if (pdata->gpio_charge_finished >=0 ) { +free_irq (gpio_to_irq (pdata->gpio_charge_finished),NULL ); +gpio_free (pdata->gpio_charge_finished); + } + +cancel_delayed_work (&bat_work); + +return 0 ; +} + +#ifdef CONFIG_PM +static int generic_adc_bat_suspend (struct platform_device *pdev, +pm_message_t state) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct generic_bat *bat =container_of (pdata, + struct generic_bat, pdata); + +cancel_delayed_work_sync (&bat_work); + bat->status =POWER_SUPPLY_STATUS_UNKNOWN ; + +return 0 ; +} + +static int generic_adc_bat_resume (struct platform_device *pdev) +{ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +return 0 ; +} +#else +#define generic_adc_bat_suspend NULL +#define generic_adc_bat_resume NULL +#endif + +static struct platform_driver generic_adc_bat_driver = { + .driver = { + .name ="generic-adc-battery" , + }, + .probe = generic_adc_bat_probe, + .remove = generic_adc_bat_remove, + .suspend = generic_adc_bat_suspend, + .resume = generic_adc_bat_resume, +}; + +module_platform_driver (generic_adc_bat_driver); + +MODULE_AUTHOR ("anish kumar <[email protected]>" ); +MODULE_DESCRIPTION ("generic battery driver using IIO" ); +MODULE_LICENSE ("GPL" );include/linux/power/generic-battery.h new file +26
-1@@ -0,0 +1,26 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef GENERIC_BATTERY_H +#define GENERIC_BATTERY_H + +#include <linux/power_supply.h> + +/** +* struct generic_platform_data - platform data for generic battery +* @battery_info: Information about the battery +* @cal_charge: platform callback to calculate charge +* @gpio_charge_finished: GPIO number used for interrupts +* @gpio_inverted: Is the gpio inverted? +*/ +struct generic_platform_data { +struct power_supply_info battery_info; +int (*cal_charge)(long ); +int gpio_charge_finished; +int gpio_inverted; +}; + +#endif /* GENERIC_BATTERY_H */ --1.7.1
Quoted 5 lines
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Just Curious what is the use of this module? Is there any user space program uses this? When ACPI drivers for battery is available how useful will this be?
Thanks,
Sannu K
On Sun, Sep 02, 2012 at 09:44:04PM +0530, Sannu K wrote: [...]
Quoted 3 lines
Just Curious what is the use of this module? Is there any user space program uses this? When ACPI drivers for battery is available how useful will this be?
That's mostly for embedded devices. They often have batteries directly connected to ADC (analog to digital converters) channels, so that's the only way to read e.g. voltage.
As for userspace, since it's battery class driver, pretty much every userspace stack can use it.
Thanks,
Anton.
Quoted 11 lines · Anton Vorontsov
On Sun, Sep 02, 2012 at 09:44:04PM +0530, Sannu K wrote: [...]
Quoted 3 lines
Just Curious what is the use of this module? Is there any user space program uses this? When ACPI drivers for battery is available how useful will this be?
That's mostly for embedded devices. They often have batteries directly connected to ADC (analog to digital converters) channels, so that's the only way to read e.g. voltage.
As for userspace, since it's battery class driver, pretty much every userspace stack can use it.
Can I get some review comments on this?
Quoted 3 lines
Thanks,
Anton.
Jonathan Cameron
maintainerQuoted 3 lines · anish kumar
From: anish kumar <[email protected]>
This patch is to use IIO to write a generic batttery driver.
battery
Quoted 2 lines
There are some inherent assumptions here:
1.User is having both main battery and backup battery.
Seems rather like that could be easily enough relaxed or configured via platform data?
Quoted 1 line
2.Both batteries use same channel to get the information.
Do you mean same actual channel (physical pin) or same ADC (with appropriate mux etc)?
Quoted 1 line
Mostly fine. There are a few corners I didn't understand as I don't
have time to dive into the battery framework in the near future. Will leave it up to those more familiar with that side of things!
My only real issue is that it would be cleaner to do the individual channels by name rather than a get all as you 'know' here how many channels are expected.
Also don't release the IIO channels until you are actually finished using them as holding them should prevent the ADC module from going away underneath you (which is nasty :)
Sorry it took so long to get back to you on this.
So what happened in 1985?
Jonathan
Quoted 15 lines
Signed-off-by anish kumar <[email protected]>--- drivers/power/Kconfig | 8 + drivers/power/Makefile | 1 + drivers/power/generic-battery.c | 374 +++++++++++++++++++++++++++++++++ include/linux/power/generic-battery.h | 26 +++ 4 files changed, 409 insertions(+), 0 deletions(-)create mode 100644
drivers/power/generic-battery.ccreate mode 100644include/linux/power/generic-battery.hdrivers/power/Kconfig +0
-0
drivers/power/Kconfig config AB8500_BATTERY_THERM_ON_BATCTRL · line 318
Quoted 10 lines
drivers/power/Kconfig +7
-0@@ -309,6 +309,14 @@ config AB8500_BATTERY_THERM_ON_BATCTRL help Say Y to enable battery temperature measurements using thermistor connected on BATCTRL ADC. + +config GENERIC_BATTERY + tristate "Generic battery support using IIO" + depends on IIO + help + Say Y here to enable support for the generic battery driver + which uses IIO framework to read adc for it's main and backup
ADC
Quoted 19 lines
drivers/power/Kconfig +1
-0+ battery. endif # POWER_SUPPLY source "drivers/power/avs/Kconfig"drivers/power/Makefile +1
-0@@ -45,3 +45,4 @@ obj-$(CONFIG_CHARGER_MAX8997) += max8997_charger.o obj-$(CONFIG_CHARGER_MAX8998) += max8998_charger.o obj-$(CONFIG_POWER_AVS) += avs/ obj-$(CONFIG_CHARGER_SMB347) += smb347-charger.o +obj-$(CONFIG_GENERIC_BATTERY) += generic-battery.odrivers/power/generic-battery.c new file +0
-0
drivers/power/generic-battery.c line 2
Quoted 2 lines
drivers/power/generic-battery.c new file +2
-0@@ -0,0 +1,374 @@ +/* +* Generice battery driver code using IIO
Generic
drivers/power/generic-battery.c line 10
Quoted 8 lines
drivers/power/generic-battery.c new file +8
-0+* Copyright (C) 2012, Anish Kumar <[email protected]> +* based on jz4740-battery.c +* based on s3c_adc_battery.c +* +* This file is subject to the terms and conditions of the GNU General Public +* License. See the file COPYING in the main directory of this archive for +* more details. +*
bonus blank line to remove
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+*/ + +#include <linux/interrupt.h> +#include <linux/platform_device.h> +#include <linux/power_supply.h> +#include <linux/gpio.h> +#include <linux/err.h> +#include <linux/timer.h> +#include <linux/jiffies.h> +#include <linux/errno.h>
drivers/power/generic-battery.c line 30
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+#include <linux/init.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/iio/consumer.h> +#include <linux/iio/types.h> + +#include <linux/power/generic-battery.h> + +#define BAT_POLL_INTERVAL 10000/* ms */ +#define JITTER_DELAY 500/* ms */
Elements to make configurable?
drivers/power/generic-battery.c line 37
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+ +enum BAT { +MAIN_BAT =0 , +BACKUP_BAT , +BAT_MAX , +}; +
Document this please. It's not immediately obvious what channel_index is.
Why not just have a direct pointer to the correct channel?
drivers/power/generic-battery.c line 44
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+struct generic_adc_bat { +struct power_supply psy; +struct iio_channel *channels; +int channel_index; +}; + +struct generic_bat {
Spacing is a bit random in here
Quoted 49 lines
drivers/power/generic-battery.c new file +49
-0+struct generic_adc_bat bat[BAT_MAX ]; +struct generic_platform_data pdata; +int volt_value; +int cur_value; +unsigned int timestamp; +int level; +int status; + int cable_plugged:1; +}; + +static struct generic_bat generic_bat = { + .bat[MAIN_BAT ] = { + .psy.name ="main-bat" , + }, + .bat[BACKUP_BAT ] = { + .psy.name ="backup-bat" , + }, +}; + +static struct delayed_work bat_work; + +static void generic_adc_bat_ext_power_changed (struct power_supply *psy) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +} + +static enum power_supply_property generic_adc_main_bat_props[] = { +POWER_SUPPLY_PROP_STATUS , +POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN , +POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN , +POWER_SUPPLY_PROP_CHARGE_NOW , +POWER_SUPPLY_PROP_VOLTAGE_NOW , +POWER_SUPPLY_PROP_CURRENT_NOW , +POWER_SUPPLY_PROP_TECHNOLOGY , +POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN , +POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN , +POWER_SUPPLY_PROP_MODEL_NAME , +}; + +static int charge_finished (struct generic_bat *bat) +{ +return bat->pdata.gpio_inverted ? + !gpio_get_value (bat->pdata.gpio_charge_finished) : +gpio_get_value (bat->pdata.gpio_charge_finished); +} + +static int generic_adc_bat_get_property(struct power_supply *psy, + enum power_supply_property psp,
drivers/power/generic-battery.c line 103
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+ union power_supply_propval *val) +{ +struct generic_adc_bat *adc_bat; +struct generic_bat *bat; +int ret, scaleint, scalepart, iio_val; +long result =0 ; + +if (!strcmp (psy->name,"main-battery" )) { + adc_bat =container_of (psy, + struct generic_adc_bat, psy);
This first statement is I think shared so move it out of the if / else
Quoted 13 lines
drivers/power/generic-battery.c new file +13
-0+ bat =container_of (adc_bat, + struct generic_bat, bat[MAIN_BAT]); + }else if (!strcmp (psy->name,"backup-battery" )) { + adc_bat = container_of(psy, struct generic_adc_bat, psy); + bat =container_of (adc_bat, + struct generic_bat, bat[BACKUP_BAT]); + }else { +/* Ideally this should never happen */ +WARN_ON (1 ); +return -EINVAL ; + } + +if (!bat) {
drivers/power/generic-battery.c line 126
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+dev_err (psy->dev,"no battery infos ?!\n" ); +return -EINVAL ; + } + ret =iio_read_channel_raw (&adc_bat->channels[adc_bat->channel_index], + &iio_val); + ret =iio_read_channel_scale (&adc_bat->channels[adc_bat->channel_index], + &scaleint, &scalepart); +if (ret <0 ) +return ret; +
A quick comment here on what the battery framework is expecting vs IIO
supplying would be good. It may be that this particular output is one that we should lift over into the core rather than end up with multiple drivers doing the same thing.
Quoted 24 lines
drivers/power/generic-battery.c new file +24
-0+switch (ret) { +case IIO_VAL_INT : + result = iio_val * scaleint; +break ; +case IIO_VAL_INT_PLUS_MICRO : + result = (s64 )iio_val * (s64 )scaleint + +div_s64 ((s64 )iio_val * (s64 )scalepart,1000000LL ); +break ; +case IIO_VAL_INT_PLUS_NANO : + result = (s64 )iio_val * (s64 )scaleint + +div_s64 ((s64 )iio_val * (s64 )scalepart,1000000000LL ); +break ; + } + +switch (psp) { +case POWER_SUPPLY_PROP_STATUS : +if (bat->pdata.gpio_charge_finished <0 ) + val->intval = bat->level ==100000 ? +POWER_SUPPLY_STATUS_FULL : bat->status; +else + val->intval = bat->status; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN : + val->intval =0 ;
drivers/power/generic-battery.c line 160
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+return 0 ; +case POWER_SUPPLY_PROP_CHARGE_NOW : + val->intval = bat->pdata.cal_charge (result); +return 0 ; +case POWER_SUPPLY_PROP_VOLTAGE_NOW : + val->intval = result; +return 0 ; +case POWER_SUPPLY_PROP_CURRENT_NOW : + val->intval = result; +return 0 ;
why return from these and break from the later ones (to return much the same I think...)?
Quoted 17 lines
drivers/power/generic-battery.c new file +17
-0+case POWER_SUPPLY_PROP_TECHNOLOGY : + val->intval = bat->pdata.battery_info.technology; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN : + val->intval = bat->pdata.battery_info.voltage_min_design; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN : + val->intval = bat->pdata.battery_info.voltage_max_design; +break ; +case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN : + val->intval = bat->pdata.battery_info.charge_full_design; +break ; +case POWER_SUPPLY_PROP_MODEL_NAME : + val->strval = bat->pdata.battery_info.name; +break ; +default : +return -EINVAL ;
drivers/power/generic-battery.c line 187
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+ } +return ret; +} + +static void generic_adc_bat_work (struct work_struct *work) +{ +struct generic_bat *bat = &generic_bat; +int is_charged; +int is_plugged; +static int was_plugged;
That's nasty. Prevents even the theoretical posibility of multiple copies of the driver. That should be in the device specific data.
drivers/power/generic-battery.c line 189
Quoted 2 lines
drivers/power/generic-battery.c new file +2
-0+ +/* backup battery doesn't have current monitoring capability anyway */
Always or in this particular configuration?
Quoted 36 lines
drivers/power/generic-battery.c new file +36
-0+ is_plugged =power_supply_am_i_supplied (&bat->bat[MAIN_BAT ].psy); + bat->cable_plugged = is_plugged; +if (is_plugged != was_plugged) { + was_plugged = is_plugged; +if (is_plugged) + bat->status =POWER_SUPPLY_STATUS_CHARGING ; +else + bat->status =POWER_SUPPLY_STATUS_DISCHARGING ; + }else { +if ((bat->pdata.gpio_charge_finished >=0 ) && is_plugged) { + is_charged =charge_finished (&generic_bat); +if (is_charged) + bat->status =POWER_SUPPLY_STATUS_FULL ; +else + bat->status =POWER_SUPPLY_STATUS_CHARGING ; + } + } + +power_supply_changed (&bat->bat[MAIN_BAT ].psy); +} + +static irqreturn_t generic_adc_bat_charged (int irq,void *dev_id) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +return IRQ_HANDLED ; +} + +static int __devinit generic_adc_bat_probe (struct platform_device *pdev) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct iio_channel *channels; +int num_channels =0 ; +int ret, i, j, k =0 ; +enum iio_chan_type type; +
drivers/power/generic-battery.c line 235
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) { + generic_bat.bat[i].psy.type =POWER_SUPPLY_TYPE_BATTERY ; + generic_bat.bat[i].psy.properties = + generic_adc_main_bat_props; + generic_bat.bat[i].psy.num_properties = +ARRAY_SIZE (generic_adc_main_bat_props); + generic_bat.bat[i].psy.get_property = + generic_adc_bat_get_property; + generic_bat.bat[i].psy.external_power_changed = + generic_adc_bat_ext_power_changed;
Could you do this with a static const array? Looks like there is nothing dynamic in here and that would make for cleaner code to read. Given other elements of psy are clearly dynamic (dev pointer) you will have to memcpy the static version in which is tedious. Still easier to read though in my view.
drivers/power/generic-battery.c line 244
Quoted 9 lines
drivers/power/generic-battery.c new file +9
-0+ } + + generic_bat.volt_value =-1 ; + generic_bat.cur_value =-1 ; + generic_bat.cable_plugged =0 ; + generic_bat.status =POWER_SUPPLY_STATUS_DISCHARGING ; + +memcpy (&generic_bat.pdata, pdata,sizeof (struct generic_platform_data )); +
Is there anything dynamic in the pdata? If not it would be cleaner just to use a pointer to it rather than make a copy (I can't immediately spot anything but them I'm not familiar with the battery side of things.
drivers/power/generic-battery.c line 252
Quoted 8 lines
drivers/power/generic-battery.c new file +8
-0+for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) { + ret =power_supply_register (&pdev->dev, + &generic_bat.bat[i].psy); +if (ret) + goto err_reg_main; + } + + channels =iio_channel_get_all (dev_name (&pdev->dev));
I would give these explicit names and get the two individual channels by name. I think it will give you cleaner code.
Quoted 5 lines
drivers/power/generic-battery.c new file +5
-0+if (IS_ERR (channels)) { + ret =PTR_ERR (channels); + goto err_reg_backup; + } +
drivers/power/generic-battery.c line 267
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+while (channels[num_channels].indio_dev) + num_channels++; + +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) + generic_bat.bat[i].channels =kzalloc (sizeof (struct iio_channel ) + *BAT_MAX ,GFP_KERNEL ); + +/* assuming main battery and backup battery is using the same channel */ +for (i =0 ; i < num_channels; i++) { + ret =iio_get_channel_type (&channels[i], &type);
Using named channels you should 'know' you have the correct type. You can of course check if you don't trust your platform data though!
Quoted 25 lines
drivers/power/generic-battery.c new file +25
-0+if (ret <0 ) + goto err_gpio; + +if (type ==IIO_VOLTAGE || type ==IIO_CURRENT ) { +for (j =0 ; j <BAT_MAX ; j++) { + generic_bat.bat[j].channel_index = k; + generic_bat.bat[j].channels[k] = channels[i]; + } + k++; + } +continue ; + } + +INIT_DELAYED_WORK (&bat_work, generic_adc_bat_work); + +if (pdata->gpio_charge_finished >=0 ) { + ret =gpio_request (pdata->gpio_charge_finished,"charged" ); +if (ret) + goto err_gpio; + + ret =request_irq (gpio_to_irq (pdata->gpio_charge_finished), + generic_adc_bat_charged, +IRQF_TRIGGER_RISING |IRQF_TRIGGER_FALLING , +"battery charged" ,NULL ); +if (ret)
drivers/power/generic-battery.c line 302
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+ goto err_gpio; + } + +dev_info (&pdev->dev,"successfully loaded\n" ); + +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +iio_channel_release_all (channels);
Not if you want to prevent the adc driver from being removed from under you. They should only be released in the driver removal.
Quoted 9 lines
drivers/power/generic-battery.c new file +9
-0+ +return 0 ; + +err_gpio: +iio_channel_release_all (channels); +err_reg_backup: +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy); +err_reg_main:
drivers/power/generic-battery.c line 321
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+return ret; +} + +static int generic_adc_bat_remove (struct platform_device *pdev) +{ +int i; +struct generic_platform_data *pdata = pdev->dev.platform_data; + +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy);
You should release the IIO channels here when they are no longer needed.
drivers/power/generic-battery.c line 323
Quoted 2 lines
drivers/power/generic-battery.c new file +2
-0+ +if (pdata->gpio_charge_finished >=0 ) {
I forget, is a gpio index of 0 definitely invalid?
Quoted 60 lines
drivers/power/generic-battery.c new file +51
-0+free_irq (gpio_to_irq (pdata->gpio_charge_finished),NULL ); +gpio_free (pdata->gpio_charge_finished); + } + +cancel_delayed_work (&bat_work); + +return 0 ; +} + +#ifdef CONFIG_PM +static int generic_adc_bat_suspend (struct platform_device *pdev, +pm_message_t state) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct generic_bat *bat =container_of (pdata, + struct generic_bat, pdata); + +cancel_delayed_work_sync (&bat_work); + bat->status =POWER_SUPPLY_STATUS_UNKNOWN ; + +return 0 ; +} + +static int generic_adc_bat_resume (struct platform_device *pdev) +{ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +return 0 ; +} +#else +#define generic_adc_bat_suspend NULL +#define generic_adc_bat_resume NULL +#endif + +static struct platform_driver generic_adc_bat_driver = { + .driver = { + .name ="generic-adc-battery" , + }, + .probe = generic_adc_bat_probe, + .remove = generic_adc_bat_remove, + .suspend = generic_adc_bat_suspend, + .resume = generic_adc_bat_resume, +}; + +module_platform_driver (generic_adc_bat_driver); + +MODULE_AUTHOR ("anish kumar <[email protected]>" ); +MODULE_DESCRIPTION ("generic battery driver using IIO" ); +MODULE_LICENSE ("GPL" );include/linux/power/generic-battery.h new file +3
-0@@ -0,0 +1,26 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as
include/linux/power/generic-battery.h line 13
Quoted 10 lines
include/linux/power/generic-battery.h new file +10
-0+* published by the Free Software Foundation. +*/ + +#ifndef GENERIC_BATTERY_H +#define GENERIC_BATTERY_H + +#include <linux/power_supply.h> + +/** +* struct generic_platform_data - platform data for generic battery
A little inconsistent on capitalization. (might as well do it now or one of those people who delight in trivial patches will 'tidy' it up for you :)
Quoted 14 lines
include/linux/power/generic-battery.h new file +13
-0+* @battery_info: Information about the battery +* @cal_charge: platform callback to calculate charge +* @gpio_charge_finished: GPIO number used for interrupts +* @gpio_inverted: Is the gpio inverted? +*/ +struct generic_platform_data { +struct power_supply_info battery_info; +int (*cal_charge)(long ); +int gpio_charge_finished; +int gpio_inverted; +}; + +#endif /* GENERIC_BATTERY_H */
Quoted 7 lines · anish kumar
From: anish kumar <[email protected]>
This patch is to use IIO to write a generic batttery driver. There are some inherent assumptions here:
1.User is having both main battery and backup battery. 2.Both batteries use same channel to get the information.
Hi thanks for stepping up to take care of this and sorry for the late reply.
Quoted 2 lines
Signed-off-by anish kumar <[email protected]>---
[...]
Quoted 16 lines
drivers/power/generic-battery.c new file +16
-0+ +struct generic_adc_bat { +struct power_supply psy; +struct iio_channel *channels; +int channel_index; +}; + +struct generic_bat { +struct generic_adc_bat bat[BAT_MAX ]; +struct generic_platform_data pdata; +int volt_value; +int cur_value; +unsigned int timestamp; +int level; +int status; +int cable_plugged:1 ;
drivers/power/generic-battery.c line 62
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+}; + +static struct generic_bat generic_bat = { + .bat[MAIN_BAT ] = { + .psy.name ="main-bat" , + }, + .bat[BACKUP_BAT ] = { + .psy.name ="backup-bat" , + }, +};
This should be per device. I'd also just use one power_supply per device. If somebody has multiple batteries in their system they can register multiple devices. This should make the driver more flexible.
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static struct delayed_work bat_work;
This should also go into the generic_bat struct.
+
Quoted 8 lines
drivers/power/generic-battery.c new file +8
-0+static void generic_adc_bat_ext_power_changed (struct power_supply *psy) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +} + +static enum power_supply_property generic_adc_main_bat_props[] = { +POWER_SUPPLY_PROP_STATUS ,
drivers/power/generic-battery.c line 83
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN , +POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN , +POWER_SUPPLY_PROP_CHARGE_NOW , +POWER_SUPPLY_PROP_VOLTAGE_NOW , +POWER_SUPPLY_PROP_CURRENT_NOW , +POWER_SUPPLY_PROP_TECHNOLOGY , +POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN , +POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN , + POWER_SUPPLY_PROP_MODEL_NAME, +};
It probably make sense to create this at runtime, depending on which kind of IIO channels are provided. E.g. for VOLTAGE_NOW/CURRENT_NOW.
[...]
Quoted 23 lines
drivers/power/generic-battery.c new file +23
-0+ +static int generic_adc_bat_get_property (struct power_supply *psy, +enum power_supply_property psp, +union power_supply_propval *val) +{ +struct generic_adc_bat *adc_bat; +struct generic_bat *bat; +int ret, scaleint, scalepart, iio_val; +long result =0 ; + +if (!strcmp (psy->name,"main-battery" )) { + adc_bat =container_of (psy, + struct generic_adc_bat, psy); + bat =container_of (adc_bat, + struct generic_bat, bat[MAIN_BAT]); + }else if (!strcmp (psy->name,"backup-battery" )) { + adc_bat = container_of(psy, struct generic_adc_bat, psy); + bat =container_of (adc_bat, + struct generic_bat, bat[BACKUP_BAT]); + }else { +/* Ideally this should never happen */ +WARN_ON (1 ); +return -EINVAL ;
drivers/power/generic-battery.c line 123
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+ } + +if (!bat) { +dev_err (psy->dev,"no battery infos ?!\n" ); +return -EINVAL ; + } + ret =iio_read_channel_raw (&adc_bat->channels[adc_bat->channel_index], + &iio_val); + ret =iio_read_channel_scale (&adc_bat->channels[adc_bat->channel_index], + &scaleint, &scalepart);
It would probably make sense to only sample if the attribute for VOLTAGE_NOW/CURRENT_NOW property is read.
[...]
Quoted 11 lines
drivers/power/generic-battery.c new file +11
-0+ +switch (psp) { +case POWER_SUPPLY_PROP_STATUS : +if (bat->pdata.gpio_charge_finished <0 ) + val->intval = bat->level ==100000 ? +POWER_SUPPLY_STATUS_FULL : bat->status; +else + val->intval = bat->status; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN : + val->intval =0 ;
drivers/power/generic-battery.c line 160
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+return 0 ; +case POWER_SUPPLY_PROP_CHARGE_NOW : + val->intval = bat->pdata.cal_charge (result); +return 0 ; +case POWER_SUPPLY_PROP_VOLTAGE_NOW : + val->intval = result; +return 0 ; +case POWER_SUPPLY_PROP_CURRENT_NOW : + val->intval = result; +return 0 ;
also this will return the same value for VOLTAGE_NOW and CURRENT_NOW since there is only one channel per battery. It might make sense to allow multiple channels per battery, one reporting current one voltage and also one for power.
Quoted 21 lines
drivers/power/generic-battery.c new file +21
-0+case POWER_SUPPLY_PROP_TECHNOLOGY : + val->intval = bat->pdata.battery_info.technology; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN : + val->intval = bat->pdata.battery_info.voltage_min_design; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN : + val->intval = bat->pdata.battery_info.voltage_max_design; +break ; +case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN : + val->intval = bat->pdata.battery_info.charge_full_design; +break ; +case POWER_SUPPLY_PROP_MODEL_NAME : + val->strval = bat->pdata.battery_info.name; +break ; +default : +return -EINVAL ; + } +return ret; +} +
[...]
Quoted 9 lines
drivers/power/generic-battery.c new file +9
-0+ +static int __devinit generic_adc_bat_probe (struct platform_device *pdev) +{ +[...] + +/* assuming main battery and backup battery is using the same channel */ +for (i =0 ; i < num_channels; i++) { + ret =iio_get_channel_type (&channels[i], &type); +if (ret <0 )
drivers/power/generic-battery.c line 278
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+ goto err_gpio; + +if (type ==IIO_VOLTAGE || type ==IIO_CURRENT ) { +for (j =0 ; j <BAT_MAX ; j++) { + generic_bat.bat[j].channel_index = k; + generic_bat.bat[j].channels[k] = channels[i]; + } + k++; + } +continue ;
continue at the end of a loop is a noop.
drivers/power/generic-battery.c line 283
Quoted 5 lines
drivers/power/generic-battery.c new file +5
-0+ } + +INIT_DELAYED_WORK (&bat_work, generic_adc_bat_work); + +if (pdata->gpio_charge_finished >=0 ) {
gpio_is_valid
drivers/power/generic-battery.c line 296
Quoted 13 lines
drivers/power/generic-battery.c new file +13
-0+ ret =gpio_request (pdata->gpio_charge_finished,"charged" ); +if (ret) + goto err_gpio; + + ret =request_irq (gpio_to_irq (pdata->gpio_charge_finished), + generic_adc_bat_charged, +IRQF_TRIGGER_RISING |IRQF_TRIGGER_FALLING , +"battery charged" ,NULL ); +if (ret) + goto err_gpio; + } + +dev_info (&pdev->dev,"successfully loaded\n" );
I'd remove that message.
Quoted 18 lines
drivers/power/generic-battery.c new file +18
-0+ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +iio_channel_release_all (channels); + +return 0 ; + +err_gpio: +iio_channel_release_all (channels); +err_reg_backup: +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy); +err_reg_main: +return ret; +} +
[...]
Quoted 26 lines
drivers/power/generic-battery.c new file +26
-0+ +#ifdef CONFIG_PM +static int generic_adc_bat_suspend (struct platform_device *pdev, +pm_message_t state) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct generic_bat *bat =container_of (pdata, + struct generic_bat, pdata); + +cancel_delayed_work_sync (&bat_work); + bat->status =POWER_SUPPLY_STATUS_UNKNOWN ; + +return 0 ; +} + +static int generic_adc_bat_resume (struct platform_device *pdev) +{ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +return 0 ; +} +#else +#define generic_adc_bat_suspend NULL +#define generic_adc_bat_resume NULL
drivers/power/generic-battery.c line 367
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+#endif + +static struct platform_driver generic_adc_bat_driver = { + .driver = { + .name ="generic-adc-battery" , + }, + .probe = generic_adc_bat_probe, + .remove = generic_adc_bat_remove, + .suspend = generic_adc_bat_suspend, + .resume = generic_adc_bat_resume,
Use dev_pm_ops instead. Using the suspend and resume callbacks is deprecated.
Quoted 22 lines
drivers/power/generic-battery.c new file +7
-0+}; + +module_platform_driver (generic_adc_bat_driver); + +MODULE_AUTHOR ("anish kumar <[email protected]>" ); +MODULE_DESCRIPTION ("generic battery driver using IIO" ); +MODULE_LICENSE ("GPL" );include/linux/power/generic-battery.h new file +9
-0@@ -0,0 +1,26 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef GENERIC_BATTERY_H +#define GENERIC_BATTERY_H +
include/linux/power/generic-battery.h line 19
Quoted 10 lines
include/linux/power/generic-battery.h new file +10
-0+#include <linux/power_supply.h> + +/** +* struct generic_platform_data - platform data for generic battery +* @battery_info: Information about the battery +* @cal_charge: platform callback to calculate charge +* @gpio_charge_finished: GPIO number used for interrupts +* @gpio_inverted: Is the gpio inverted? +*/ +struct generic_platform_data {
The name might be a bit to generic ;) I'd go for generic_battery_platform_data or iio_battery_platform_data.
Quoted 7 lines
include/linux/power/generic-battery.h new file +7
-0+struct power_supply_info battery_info; +int (*cal_charge)(long ); +int gpio_charge_finished; + int gpio_inverted; +}; + +#endif /* GENERIC_BATTERY_H */
Thanks for your time.
Quoted 9 lines · Jonathan Cameron
Quoted 3 lines · anish kumar
From: anish kumar <[email protected]>
This patch is to use IIO to write a generic batttery driver.
battery
Quoted 2 lines
There are some inherent assumptions here:
1.User is having both main battery and backup battery.Seems rather like that could be easily enough relaxed or configured via platform data?
Yes
Quoted 3 lines
Quoted 1 line
2.Both batteries use same channel to get the information.
Do you mean same actual channel (physical pin) or same ADC (with appropriate mux etc)?
As lars pointed out it would be better if we have multiple channels per battery.The channel what I am referring here is the name of different channels which will be exported by adc driver to get voltage, current and power.
Quoted 8 lines
Quoted 1 line
Mostly fine. There are a few corners I didn't understand as I don't have time to dive into the battery framework in the near future. Will leave it up to those more familiar with that side of things!
My only real issue is that it would be cleaner to do the individual channels by name rather than a get all as you 'know' here how many channels are expected.
agreed.
Quoted 4 lines
Also don't release the IIO channels until you are actually finished using them as holding them should prevent the ADC module from going away underneath you (which is nasty :)
agreed.
Quoted 2 lines
Sorry it took so long to get back to you on this.
That is the biggest advantage of working in open-source. We can take our own time.
Quoted 2 lines
So what happened in 1985?
Was hurrying to get an email id :)
Quoted 72 lines
Jonathan
Quoted 15 lines
Signed-off-by anish kumar <[email protected]>--- drivers/power/Kconfig | 8 + drivers/power/Makefile | 1 + drivers/power/generic-battery.c | 374 +++++++++++++++++++++++++++++++++ include/linux/power/generic-battery.h | 26 +++ 4 files changed, 409 insertions(+), 0 deletions(-)create mode 100644
drivers/power/generic-battery.ccreate mode 100644include/linux/power/generic-battery.hdrivers/power/Kconfig +0
-0drivers/power/Kconfig config AB8500_BATTERY_THERM_ON_BATCTRL · line 318
Quoted 10 lines
drivers/power/Kconfig +7
-0@@ -309,6 +309,14 @@ config AB8500_BATTERY_THERM_ON_BATCTRL help Say Y to enable battery temperature measurements using thermistor connected on BATCTRL ADC. + +config GENERIC_BATTERY + tristate "Generic battery support using IIO" + depends on IIO + help + Say Y here to enable support for the generic battery driver + which uses IIO framework to read adc for it's main and backupADC
Quoted 19 lines
drivers/power/Kconfig +1
-0+ battery. endif # POWER_SUPPLY source "drivers/power/avs/Kconfig"drivers/power/Makefile +1
-0@@ -45,3 +45,4 @@ obj-$(CONFIG_CHARGER_MAX8997) += max8997_charger.o obj-$(CONFIG_CHARGER_MAX8998) += max8998_charger.o obj-$(CONFIG_POWER_AVS) += avs/ obj-$(CONFIG_CHARGER_SMB347) += smb347-charger.o +obj-$(CONFIG_GENERIC_BATTERY) += generic-battery.odrivers/power/generic-battery.c new file +0
-0drivers/power/generic-battery.c line 2
Quoted 2 lines
drivers/power/generic-battery.c new file +2
-0@@ -0,0 +1,374 @@ +/* +* Generice battery driver code using IIO Generic
drivers/power/generic-battery.c line 10
Quoted 8 lines
drivers/power/generic-battery.c new file +8
-0+* Copyright (C) 2012, Anish Kumar <[email protected]> +* based on jz4740-battery.c +* based on s3c_adc_battery.c +* +* This file is subject to the terms and conditions of the GNU General Public +* License. See the file COPYING in the main directory of this archive for +* more details. +* bonus blank line to remove
Quoted 13 lines
drivers/power/generic-battery.c new file +13
-0+*/ + +#include <linux/interrupt.h> +#include <linux/platform_device.h> +#include <linux/power_supply.h> +#include <linux/gpio.h> +#include <linux/err.h> +#include <linux/timer.h> +#include <linux/jiffies.h> +#include <linux/errno.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/slab.h>
Quoted 8 lines
drivers/power/generic-battery.c line 30
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+#include <linux/iio/consumer.h> +#include <linux/iio/types.h> + +#include <linux/power/generic-battery.h> + +#define BAT_POLL_INTERVAL 10000/* ms */ +#define JITTER_DELAY 500/* ms */ Elements to make configurable?
Yes probably make it platform data?
Quoted 9 lines
drivers/power/generic-battery.c line 37
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+ +enum BAT { +MAIN_BAT =0 , +BACKUP_BAT , +BAT_MAX , +}; +Document this please. It's not immediately obvious what
channel_indexis.
This will be removed as we will be using only one device.
Quoted 62 lines
Why not just have a direct pointer to the correct channel?
drivers/power/generic-battery.c line 44
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+struct generic_adc_bat { +struct power_supply psy; +struct iio_channel *channels; +int channel_index; +}; + +struct generic_bat {Spacing is a bit random in here
Quoted 52 lines
drivers/power/generic-battery.c new file +52
-0+struct generic_adc_bat bat[BAT_MAX ]; +struct generic_platform_data pdata; +int volt_value; +int cur_value; +unsigned int timestamp; +int level; +int status; + int cable_plugged:1; +}; + +static struct generic_bat generic_bat = { + .bat[MAIN_BAT ] = { + .psy.name ="main-bat" , + }, + .bat[BACKUP_BAT ] = { + .psy.name ="backup-bat" , + }, +}; + +static struct delayed_work bat_work; + +static void generic_adc_bat_ext_power_changed (struct power_supply *psy) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +} + +static enum power_supply_property generic_adc_main_bat_props[] = { +POWER_SUPPLY_PROP_STATUS , +POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN , +POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN , +POWER_SUPPLY_PROP_CHARGE_NOW , +POWER_SUPPLY_PROP_VOLTAGE_NOW , +POWER_SUPPLY_PROP_CURRENT_NOW , +POWER_SUPPLY_PROP_TECHNOLOGY , +POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN , +POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN , +POWER_SUPPLY_PROP_MODEL_NAME , +}; + +static int charge_finished (struct generic_bat *bat) +{ +return bat->pdata.gpio_inverted ? + !gpio_get_value (bat->pdata.gpio_charge_finished) : +gpio_get_value (bat->pdata.gpio_charge_finished); +} + +static int generic_adc_bat_get_property (struct power_supply *psy, +enum power_supply_property psp, +union power_supply_propval *val) +{ +struct generic_adc_bat *adc_bat;
Quoted 8 lines
drivers/power/generic-battery.c line 103
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+struct generic_bat *bat; +int ret, scaleint, scalepart, iio_val; +long result =0 ; + +if (!strcmp (psy->name,"main-battery" )) { + adc_bat =container_of (psy, + struct generic_adc_bat, psy);This first statement is I think shared so move it out of the if / else
This whole logic will change as we will be using only one device and if has user has multiple batteries then he can register multiple devices.
Quoted 15 lines
Quoted 4 lines
drivers/power/generic-battery.c new file +3
-0+ bat =container_of (adc_bat, + struct generic_bat, bat[MAIN_BAT]); + }else if (!strcmp (psy->name,"backup-battery" )) {drivers/power/generic-battery.c line 116
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+ adc_bat = container_of(psy, struct generic_adc_bat, psy); + bat =container_of (adc_bat, + struct generic_bat, bat[BACKUP_BAT]); + }else { +/* Ideally this should never happen */ +WARN_ON (1 ); +return -EINVAL ; + } + +if (!bat) {> + dev_err(psy->dev, "no battery infos ?!\n");
Quoted 13 lines
drivers/power/generic-battery.c line 126
Quoted 9 lines
drivers/power/generic-battery.c new file +9
-0+return -EINVAL ; + + ret =iio_read_channel_raw (&adc_bat->channels[adc_bat->channel_index], + &iio_val); + ret =iio_read_channel_scale (&adc_bat->channels[adc_bat->channel_index], + &scaleint, &scalepart); +if (ret <0 ) +return ret; +A quick comment here on what the battery framework is expecting vs IIO supplying would be good. It may be that this particular output is one that we should lift over into the core rather than end up with multiple drivers doing the same thing.
I think for this we need to get all the channels supported by a particular adc device and once we get the information regarding the channel(current/voltage/power) supported we can find out "What information is exported about this channel which may include scale or raw adc value".This can be used to call the corresponding API's.
Quoted 29 lines
Quoted 29 lines
drivers/power/generic-battery.c new file +28
-0+switch (ret) { +case IIO_VAL_INT : + result = iio_val * scaleint; +break ; +case IIO_VAL_INT_PLUS_MICRO : + result = (s64 )iio_val * (s64 )scaleint + +div_s64 ((s64 )iio_val * (s64 )scalepart,1000000LL ); +break ; +case IIO_VAL_INT_PLUS_NANO : + result = (s64 )iio_val * (s64 )scaleint + +div_s64 ((s64 )iio_val * (s64 )scalepart,1000000000LL ); +break ; + } + +switch (psp) { +case POWER_SUPPLY_PROP_STATUS : +if (bat->pdata.gpio_charge_finished <0 ) + val->intval = bat->level ==100000 ? +POWER_SUPPLY_STATUS_FULL : bat->status; +else + val->intval = bat->status; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN : + val->intval =0 ; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_NOW : + val->intval = bat->pdata.cal_charge (result); +return 0 ;
Quoted 8 lines
drivers/power/generic-battery.c line 160
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+case POWER_SUPPLY_PROP_VOLTAGE_NOW : + val->intval = result; +return 0 ; +case POWER_SUPPLY_PROP_CURRENT_NOW : + val->intval = result; +return 0 ;why return from these and break from the later ones (to return much the same I think...)?
my mistake.
Quoted 21 lines
Quoted 21 lines
drivers/power/generic-battery.c new file +21
-0+case POWER_SUPPLY_PROP_TECHNOLOGY : + val->intval = bat->pdata.battery_info.technology; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN : + val->intval = bat->pdata.battery_info.voltage_min_design; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN : + val->intval = bat->pdata.battery_info.voltage_max_design; +break ; +case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN : + val->intval = bat->pdata.battery_info.charge_full_design; +break ; +case POWER_SUPPLY_PROP_MODEL_NAME : + val->strval = bat->pdata.battery_info.name; +break ; +default : +return -EINVAL ; + } +return ret; +} +
Quoted 8 lines
drivers/power/generic-battery.c line 187
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+static void generic_adc_bat_work (struct work_struct *work) +{ +struct generic_bat *bat = &generic_bat; +int is_charged; +int is_plugged; +static int was_plugged;That's nasty. Prevents even the theoretical posibility of multiple copies of the driver. That should be in the device specific data.
yes should be part of device specific data.
Quoted 3 lines
drivers/power/generic-battery.c line 188
Quoted 2 lines
drivers/power/generic-battery.c new file +2
-0+ +/* backup battery doesn't have current monitoring capability anyway */ Always or in this particular configuration?
I was told by Anton Vorontsov.Anyway now this driver will be per device so shouldn't matter.
Quoted 52 lines
Quoted 37 lines
drivers/power/generic-battery.c new file +36
-0+ is_plugged =power_supply_am_i_supplied (&bat->bat[MAIN_BAT ].psy); + bat->cable_plugged = is_plugged; +if (is_plugged != was_plugged) { + was_plugged = is_plugged; +if (is_plugged) + bat->status =POWER_SUPPLY_STATUS_CHARGING ; +else + bat->status =POWER_SUPPLY_STATUS_DISCHARGING ; + }else { +if ((bat->pdata.gpio_charge_finished >=0 ) && is_plugged) { + is_charged =charge_finished (&generic_bat); +if (is_charged) + bat->status =POWER_SUPPLY_STATUS_FULL ; +else + bat->status =POWER_SUPPLY_STATUS_CHARGING ; + } + } + +power_supply_changed (&bat->bat[MAIN_BAT ].psy); +} + +static irqreturn_t generic_adc_bat_charged (int irq,void *dev_id) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +return IRQ_HANDLED ; +} + +static int __devinit generic_adc_bat_probe (struct platform_device *pdev) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct iio_channel *channels; +int num_channels =0 ; +int ret, i, j, k =0 ; +enum iio_chan_type type; +drivers/power/generic-battery.c line 235
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) { + generic_bat.bat[i].psy.type =POWER_SUPPLY_TYPE_BATTERY ; + generic_bat.bat[i].psy.properties = + generic_adc_main_bat_props; + generic_bat.bat[i].psy.num_properties = +ARRAY_SIZE (generic_adc_main_bat_props); + generic_bat.bat[i].psy.get_property = + generic_adc_bat_get_property; + generic_bat.bat[i].psy.external_power_changed = + generic_adc_bat_ext_power_changed;Could you do this with a static const array? Looks like there is nothing dynamic in here and that would make for cleaner code to read. Given other elements of psy are clearly dynamic (dev pointer) you will have to memcpy the static version in which is tedious. Still easier to read though in my view.
Quoted 1 line
drivers/power/generic-battery.c line 235
Quoted 1 line
drivers/power/generic-battery.c new file +1
-0+ }
right.
Quoted 4 lines
Quoted 4 lines
drivers/power/generic-battery.c new file +4
-0+ + generic_bat.volt_value =-1 ; + generic_bat.cur_value =-1 ; + generic_bat.cable_plugged =0 ;
Quoted 8 lines
drivers/power/generic-battery.c line 244
Quoted 4 lines
drivers/power/generic-battery.c new file +4
-0+ generic_bat.status =POWER_SUPPLY_STATUS_DISCHARGING ; + +memcpy (&generic_bat.pdata, pdata,sizeof (struct generic_platform_data )); +Is there anything dynamic in the pdata? If not it would be cleaner just to use a pointer to it rather than make a copy (I can't immediately spot anything but them I'm not familiar with the battery
side of things.
We can just assign it to generic_bat so that it can be used in the relevant functions.Otherwise we need to use dev_set_drvdata and dev_get_drvdata.
Quoted 10 lines
drivers/power/generic-battery.c line 252
Quoted 8 lines
drivers/power/generic-battery.c new file +8
-0+for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) { + ret =power_supply_register (&pdev->dev, + &generic_bat.bat[i].psy); +if (ret) + goto err_reg_main; + } + + channels =iio_channel_get_all (dev_name (&pdev->dev));I would give these explicit names and get the two individual channels by name. I think it will give you cleaner code.
Yes, now it will be based on pdata->voltage_channel, pdata->current_channel and so on.We will use this to get the channel.
Quoted 9 lines
Quoted 9 lines
drivers/power/generic-battery.c new file +9
-0+if (IS_ERR (channels)) { + ret =PTR_ERR (channels); + goto err_reg_backup; + } + +while (channels[num_channels].indio_dev) + num_channels++; + +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++)
Quoted 8 lines
drivers/power/generic-battery.c line 267
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+ generic_bat.bat[i].channels =kzalloc (sizeof (struct iio_channel ) + *BAT_MAX ,GFP_KERNEL ); + +/* assuming main battery and backup battery is using the same channel */ +for (i =0 ; i < num_channels; i++) { + ret =iio_get_channel_type (&channels[i], &type);Using named channels you should 'know' you have the correct type. You can of course check if you don't trust your platform data though!
right.
Quoted 29 lines
Quoted 29 lines
drivers/power/generic-battery.c new file +29
-0+if (ret <0 ) + goto err_gpio; + +if (type ==IIO_VOLTAGE || type ==IIO_CURRENT ) { +for (j =0 ; j <BAT_MAX ; j++) { + generic_bat.bat[j].channel_index = k; + generic_bat.bat[j].channels[k] = channels[i]; + } + k++; + } +continue ; + } + +INIT_DELAYED_WORK (&bat_work, generic_adc_bat_work); + +if (pdata->gpio_charge_finished >=0 ) { + ret =gpio_request (pdata->gpio_charge_finished,"charged" ); +if (ret) + goto err_gpio; + + ret =request_irq (gpio_to_irq (pdata->gpio_charge_finished), + generic_adc_bat_charged, +IRQF_TRIGGER_RISING |IRQF_TRIGGER_FALLING , +"battery charged" ,NULL ); +if (ret) + goto err_gpio; + } + +dev_info (&pdev->dev,"successfully loaded\n" );
Quoted 8 lines
drivers/power/generic-battery.c line 302
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +iio_channel_release_all (channels);Not if you want to prevent the adc driver from being removed from under you. They should only be released in the driver removal.
right.
Quoted 13 lines
Quoted 13 lines
drivers/power/generic-battery.c new file +13
-0+ +return 0 ; + +err_gpio: +iio_channel_release_all (channels); +err_reg_backup: +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy); +err_reg_main: +return ret; +} + +static int generic_adc_bat_remove (struct platform_device *pdev)
Quoted 8 lines
drivers/power/generic-battery.c line 321
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+{ +int i; +struct generic_platform_data *pdata = pdev->dev.platform_data; + +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy);You should release the IIO channels here when they are no longer needed.
right.
Quoted 68 lines
drivers/power/generic-battery.c line 322
Quoted 2 lines
drivers/power/generic-battery.c new file +2
-0+ +if (pdata->gpio_charge_finished >=0 ) {I forget, is a gpio index of 0 definitely invalid?
Quoted 65 lines
drivers/power/generic-battery.c new file +51
-0+free_irq (gpio_to_irq (pdata->gpio_charge_finished),NULL ); +gpio_free (pdata->gpio_charge_finished); + } + +cancel_delayed_work (&bat_work); + +return 0 ; +} + +#ifdef CONFIG_PM +static int generic_adc_bat_suspend (struct platform_device *pdev, +pm_message_t state) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct generic_bat *bat =container_of (pdata, + struct generic_bat, pdata); + +cancel_delayed_work_sync (&bat_work); + bat->status =POWER_SUPPLY_STATUS_UNKNOWN ; + +return 0 ; +} + +static int generic_adc_bat_resume (struct platform_device *pdev) +{ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +return 0 ; +} +#else +#define generic_adc_bat_suspend NULL +#define generic_adc_bat_resume NULL +#endif + +static struct platform_driver generic_adc_bat_driver = { + .driver = { + .name ="generic-adc-battery" , + }, + .probe = generic_adc_bat_probe, + .remove = generic_adc_bat_remove, + .suspend = generic_adc_bat_suspend, + .resume = generic_adc_bat_resume, +}; + +module_platform_driver (generic_adc_bat_driver); + +MODULE_AUTHOR ("anish kumar <[email protected]>" ); +MODULE_DESCRIPTION ("generic battery driver using IIO" ); +MODULE_LICENSE ("GPL" );include/linux/power/generic-battery.h new file +8
-0@@ -0,0 +1,26 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef GENERIC_BATTERY_H +#define GENERIC_BATTERY_H
Quoted 8 lines
include/linux/power/generic-battery.h line 13
Quoted 5 lines
include/linux/power/generic-battery.h new file +5
-0+ +#include <linux/power_supply.h> + +/** +* struct generic_platform_data - platform data for generic battery A little inconsistent on capitalization. (might as well do it now or one of those people who delight in trivial patches will 'tidy' it up for you :)
I don't mind as it is a good start for people to get familiar with the linux patch process.
However I will address this valuable comments.
Quoted 15 lines
Quoted 14 lines
include/linux/power/generic-battery.h new file +13
-0+* @battery_info: Information about the battery +* @cal_charge: platform callback to calculate charge +* @gpio_charge_finished: GPIO number used for interrupts +* @gpio_inverted: Is the gpio inverted? +*/ +struct generic_platform_data { +struct power_supply_info battery_info; +int (*cal_charge)(long ); +int gpio_charge_finished; +int gpio_inverted; +}; + +#endif /* GENERIC_BATTERY_H */
Quoted 10 lines · Lars-Peter Clausen
Quoted 7 lines · anish kumar
From: anish kumar <[email protected]>
This patch is to use IIO to write a generic batttery driver. There are some inherent assumptions here:
1.User is having both main battery and backup battery. 2.Both batteries use same channel to get the information.Hi thanks for stepping up to take care of this and sorry for the late reply.
Thanks to you.
Quoted 26 lines
Quoted 3 lines
Signed-off-by anish kumar <[email protected]>---
[...]
Quoted 22 lines
drivers/power/generic-battery.c new file +22
-0+ +struct generic_adc_bat { +struct power_supply psy; +struct iio_channel *channels; +int channel_index; +}; + +struct generic_bat { +struct generic_adc_bat bat[BAT_MAX ]; +struct generic_platform_data pdata; +int volt_value; +int cur_value; +unsigned int timestamp; +int level; +int status; +int cable_plugged:1 ; +}; + +static struct generic_bat generic_bat = { + .bat[MAIN_BAT ] = { + .psy.name ="main-bat" , + },
Quoted 8 lines
drivers/power/generic-battery.c line 62
Quoted 4 lines
drivers/power/generic-battery.c new file +4
-0+ .bat[BACKUP_BAT] = { + .psy.name = "backup-bat", + }, +};This should be per device. I'd also just use one
power_supplyper device. If somebody has multiple batteries in their system they can register multiple devices. This should make the driver more flexible.
Yes makes sense of having one driver/device and if someone has more devices to register than it can be done by registering multiple devices with different platform data.
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ +static struct delayed_work bat_work;This should also go into the
generic_batstruct.
right.
Quoted 15 lines
+
Quoted 13 lines
drivers/power/generic-battery.c new file +13
-0+static void generic_adc_bat_ext_power_changed (struct power_supply *psy) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +} + +static enum power_supply_property generic_adc_main_bat_props[] = { +POWER_SUPPLY_PROP_STATUS , +POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN , +POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN , +POWER_SUPPLY_PROP_CHARGE_NOW , +POWER_SUPPLY_PROP_VOLTAGE_NOW , +POWER_SUPPLY_PROP_CURRENT_NOW ,
Quoted 8 lines
drivers/power/generic-battery.c line 83
Quoted 5 lines
drivers/power/generic-battery.c new file +5
-0+POWER_SUPPLY_PROP_TECHNOLOGY , +POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN , +POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN , + POWER_SUPPLY_PROP_MODEL_NAME, +};It probably make sense to create this at runtime, depending on which kind of IIO channels are provided. E.g. for VOLTAGE_NOW/CURRENT_NOW.
Yes, something like pdata->voltage_channel/pdata->current_channel/pdata->power_channel and if this property is set then register the VOLTAGE_NOW/CURRENT_NOW/POWER_ property.
Quoted 30 lines
[...]
Quoted 28 lines
drivers/power/generic-battery.c new file +28
-0+ +static int generic_adc_bat_get_property (struct power_supply *psy, +enum power_supply_property psp, +union power_supply_propval *val) +{ +struct generic_adc_bat *adc_bat; +struct generic_bat *bat; +int ret, scaleint, scalepart, iio_val; +long result =0 ; + +if (!strcmp (psy->name,"main-battery" )) { + adc_bat =container_of (psy, + struct generic_adc_bat, psy); + bat =container_of (adc_bat, + struct generic_bat, bat[MAIN_BAT]); + }else if (!strcmp (psy->name,"backup-battery" )) { + adc_bat = container_of(psy, struct generic_adc_bat, psy); + bat =container_of (adc_bat, + struct generic_bat, bat[BACKUP_BAT]); + }else { +/* Ideally this should never happen */ +WARN_ON (1 ); +return -EINVAL ; + } + +if (!bat) { +dev_err (psy->dev,"no battery infos ?!\n" ); +return -EINVAL ;
Quoted 8 lines
drivers/power/generic-battery.c line 123
Quoted 5 lines
drivers/power/generic-battery.c new file +5
-0+ } + ret =iio_read_channel_raw (&adc_bat->channels[adc_bat->channel_index], + &iio_val); + ret =iio_read_channel_scale (&adc_bat->channels[adc_bat->channel_index], + &scaleint, &scalepart);It would probably make sense to only sample if the attribute for VOLTAGE_NOW/CURRENT_NOW property is read.
right.
Quoted 20 lines
[...]
Quoted 17 lines
drivers/power/generic-battery.c new file +17
-0+ +switch (psp) { +case POWER_SUPPLY_PROP_STATUS : +if (bat->pdata.gpio_charge_finished <0 ) + val->intval = bat->level ==100000 ? +POWER_SUPPLY_STATUS_FULL : bat->status; +else + val->intval = bat->status; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN : + val->intval =0 ; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_NOW : + val->intval = bat->pdata.cal_charge (result); +return 0 ; +case POWER_SUPPLY_PROP_VOLTAGE_NOW : + val->intval = result;
Quoted 8 lines
drivers/power/generic-battery.c line 160
Quoted 4 lines
drivers/power/generic-battery.c new file +4
-0+return 0 ; +case POWER_SUPPLY_PROP_CURRENT_NOW : + val->intval = result; +return 0 ;also this will return the same value for
VOLTAGE_NOWandCURRENT_NOWsince there is only one channel per battery. It might make sense to allow multiple channels per battery, one reporting current one voltage and also one for power.
absolutely makes sense.
Quoted 37 lines
Quoted 23 lines
drivers/power/generic-battery.c new file +21
-0+case POWER_SUPPLY_PROP_TECHNOLOGY : + val->intval = bat->pdata.battery_info.technology; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN : + val->intval = bat->pdata.battery_info.voltage_min_design; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN : + val->intval = bat->pdata.battery_info.voltage_max_design; +break ; +case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN : + val->intval = bat->pdata.battery_info.charge_full_design; +break ; +case POWER_SUPPLY_PROP_MODEL_NAME : + val->strval = bat->pdata.battery_info.name; +break ; +default : +return -EINVAL ; + } +return ret; +} +[...]
Quoted 13 lines
drivers/power/generic-battery.c new file +13
-0+ +static int __devinit generic_adc_bat_probe (struct platform_device *pdev) +{ +[...] + +/* assuming main battery and backup battery is using the same channel */ +for (i =0 ; i < num_channels; i++) { + ret =iio_get_channel_type (&channels[i], &type); +if (ret <0 ) + goto err_gpio; + +if (type ==IIO_VOLTAGE || type ==IIO_CURRENT ) { +for (j =0 ; j <BAT_MAX ; j++) {
Quoted 8 lines
drivers/power/generic-battery.c line 278
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+ generic_bat.bat[j].channel_index = k; + generic_bat.bat[j].channels[k] = channels[i]; + } + k++; + } +continue ;continue at the end of a loop is a noop.
right.
Quoted 8 lines
drivers/power/generic-battery.c line 283
Quoted 6 lines
drivers/power/generic-battery.c new file +5
-0+ } + +INIT_DELAYED_WORK (&bat_work, generic_adc_bat_work); + +if (pdata->gpio_charge_finished >=0 ) {
gpio_is_valid
right.
Quoted 16 lines
Quoted 4 lines
drivers/power/generic-battery.c new file +3
-0+ ret =gpio_request (pdata->gpio_charge_finished,"charged" ); +if (ret) + goto err_gpio;drivers/power/generic-battery.c line 296
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+ + ret =request_irq (gpio_to_irq (pdata->gpio_charge_finished), + generic_adc_bat_charged, +IRQF_TRIGGER_RISING |IRQF_TRIGGER_FALLING , +"battery charged" ,NULL ); +if (ret) + goto err_gpio; + } + +dev_info (&pdev->dev,"successfully loaded\n" );I'd remove that message.
ok.
Quoted 52 lines
Quoted 19 lines
drivers/power/generic-battery.c new file +18
-0+ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +iio_channel_release_all (channels); + +return 0 ; + +err_gpio: +iio_channel_release_all (channels); +err_reg_backup: +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy); +err_reg_main: +return ret; +} +[...]
Quoted 30 lines
drivers/power/generic-battery.c new file +30
-0+ +#ifdef CONFIG_PM +static int generic_adc_bat_suspend (struct platform_device *pdev, +pm_message_t state) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct generic_bat *bat =container_of (pdata, + struct generic_bat, pdata); + +cancel_delayed_work_sync (&bat_work); + bat->status =POWER_SUPPLY_STATUS_UNKNOWN ; + +return 0 ; +} + +static int generic_adc_bat_resume (struct platform_device *pdev) +{ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +return 0 ; +} +#else +#define generic_adc_bat_suspend NULL +#define generic_adc_bat_resume NULL +#endif + +static struct platform_driver generic_adc_bat_driver = { + .driver = {
Quoted 8 lines
drivers/power/generic-battery.c line 367
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+ .name = "generic-adc-battery", + }, + .probe = generic_adc_bat_probe, + .remove = generic_adc_bat_remove, + .suspend = generic_adc_bat_suspend, + .resume = generic_adc_bat_resume,Use
dev_pm_opsinstead. Using the suspend and resume callbacks is deprecated.
makes sense.
Quoted 28 lines
Quoted 28 lines
drivers/power/generic-battery.c new file +7
-0+}; + +module_platform_driver (generic_adc_bat_driver); + +MODULE_AUTHOR ("anish kumar <[email protected]>" ); +MODULE_DESCRIPTION ("generic battery driver using IIO" ); +MODULE_LICENSE ("GPL" );include/linux/power/generic-battery.h new file +14
-0@@ -0,0 +1,26 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef GENERIC_BATTERY_H +#define GENERIC_BATTERY_H + +#include <linux/power_supply.h> + +/** +* struct generic_platform_data - platform data for generic battery +* @battery_info: Information about the battery
Quoted 8 lines
include/linux/power/generic-battery.h line 19
Quoted 5 lines
include/linux/power/generic-battery.h new file +5
-0+* @cal_charge: platform callback to calculate charge +* @gpio_charge_finished: GPIO number used for interrupts +* @gpio_inverted: Is the gpio inverted? +*/ +struct generic_platform_data {The name might be a bit to generic ;) I'd go for
generic_battery_platform_dataoriio_battery_platform_data.
it will be iio_battery_platform_data.
Quoted 9 lines
Quoted 8 lines
include/linux/power/generic-battery.h new file +7
-0+struct power_supply_info battery_info; +int (*cal_charge)(long ); +int gpio_charge_finished; + int gpio_inverted; +}; + +#endif /* GENERIC_BATTERY_H */
On Sat, Sep 08, 2012 at 12:40:18PM +0530, anish kumar wrote:
[...]
Quoted 3 lines
Quoted 2 lines
Quoted 1 line
Signed-off-by anish kumar <[email protected]>So what happened in 1985?
Was hurrying to get an email id :)
I so much understand... ;-)
Much thanks to Lars-Peter and Jonathan, saved me a lot of time reviewing the driver.
Anish, the idea of generic IIO driver looks great, I'm impatiently waiting for a new revision!
One suggestion: the file name says generic-battery, the code sometimes uses generic_adc_battery sometimes generic_battery. I'd suggest being consistent, and stick to one naming scheme. I'd prefer generic_adc_battery.
Thanks!
Anton.
Jonathan Cameron
maintainerQuoted 11 lines · anish kumar
Thanks for your time.
Quoted 9 lines · Jonathan Cameron
Quoted 3 lines · anish kumar
From: anish kumar <[email protected]>
This patch is to use IIO to write a generic batttery driver.
battery
Quoted 2 lines
There are some inherent assumptions here:
1.User is having both main battery and backup battery.Seems rather like that could be easily enough relaxed or configured via platform data?
Quoted 8 lines
Yes
Quoted 3 lines
Quoted 1 line
2.Both batteries use same channel to get the information.
Do you mean same actual channel (physical pin) or same ADC (with appropriate mux etc)?
As lars pointed out it would be better if we have multiple channels per battery.The channel what I am referring here is the name of different channels which will be exported by adc driver to get voltage, current and power.
I agree entirely with what Lars-Peter said. A separate instance of the driver per battery will make life much cleaner and allow as you say for separate channels for voltage, current and power for each one. I guess it may be a case of trying to get each one for every battery and continue with whatever turns out to be available.
Quoted 10 lines
Quoted 8 lines
Quoted 1 line
Mostly fine. There are a few corners I didn't understand as I don't have time to dive into the battery framework in the near future. Will leave it up to those more familiar with that side of things!
My only real issue is that it would be cleaner to do the individual channels by name rather than a get all as you 'know' here how many channels are expected.
agreed.
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Also don't release the IIO channels until you are actually finished using them as holding them should prevent the ADC module from going away underneath you (which is nasty :)
agreed.
Quoted 2 lines
Sorry it took so long to get back to you on this.
That is the biggest advantage of working in open-source. We can take our own time.
:)
Quoted 3 lines
Quoted 2 lines
So what happened in 1985?
Was hurrying to get an email id :)
And there I was imagining a magical significance to it....
Quoted 161 lines
Quoted 72 lines
Jonathan
Quoted 15 lines
Signed-off-by anish kumar <[email protected]>--- drivers/power/Kconfig | 8 + drivers/power/Makefile | 1 + drivers/power/generic-battery.c | 374 +++++++++++++++++++++++++++++++++ include/linux/power/generic-battery.h | 26 +++ 4 files changed, 409 insertions(+), 0 deletions(-)create mode 100644
drivers/power/generic-battery.ccreate mode 100644include/linux/power/generic-battery.hdrivers/power/Kconfig +0
-0drivers/power/Kconfig config AB8500_BATTERY_THERM_ON_BATCTRL · line 318
Quoted 10 lines
drivers/power/Kconfig +7
-0@@ -309,6 +309,14 @@ config AB8500_BATTERY_THERM_ON_BATCTRL help Say Y to enable battery temperature measurements using thermistor connected on BATCTRL ADC. + +config GENERIC_BATTERY + tristate "Generic battery support using IIO" + depends on IIO + help + Say Y here to enable support for the generic battery driver + which uses IIO framework to read adc for it's main and backupADC
Quoted 19 lines
drivers/power/Kconfig +1
-0+ battery. endif # POWER_SUPPLY source "drivers/power/avs/Kconfig"drivers/power/Makefile +1
-0@@ -45,3 +45,4 @@ obj-$(CONFIG_CHARGER_MAX8997) += max8997_charger.o obj-$(CONFIG_CHARGER_MAX8998) += max8998_charger.o obj-$(CONFIG_POWER_AVS) += avs/ obj-$(CONFIG_CHARGER_SMB347) += smb347-charger.o +obj-$(CONFIG_GENERIC_BATTERY) += generic-battery.odrivers/power/generic-battery.c new file +0
-0drivers/power/generic-battery.c line 2
Quoted 2 lines
drivers/power/generic-battery.c new file +2
-0@@ -0,0 +1,374 @@ +/* +* Generice battery driver code using IIO Generic
drivers/power/generic-battery.c line 10
Quoted 8 lines
drivers/power/generic-battery.c new file +8
-0+* Copyright (C) 2012, Anish Kumar <[email protected]> +* based on jz4740-battery.c +* based on s3c_adc_battery.c +* +* This file is subject to the terms and conditions of the GNU General Public +* License. See the file COPYING in the main directory of this archive for +* more details. +* bonus blank line to remove
Quoted 13 lines
drivers/power/generic-battery.c new file +13
-0+*/ + +#include <linux/interrupt.h> +#include <linux/platform_device.h> +#include <linux/power_supply.h> +#include <linux/gpio.h> +#include <linux/err.h> +#include <linux/timer.h> +#include <linux/jiffies.h> +#include <linux/errno.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/slab.h> Quoted 8 lines
drivers/power/generic-battery.c line 30
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+#include <linux/iio/consumer.h> +#include <linux/iio/types.h> + +#include <linux/power/generic-battery.h> + +#define BAT_POLL_INTERVAL 10000/* ms */ +#define JITTER_DELAY 500/* ms */ Elements to make configurable?
Yes probably make it platform data?
Quoted 9 lines
drivers/power/generic-battery.c line 37
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+ +enum BAT { +MAIN_BAT =0 , +BACKUP_BAT , +BAT_MAX , +}; +Document this please. It's not immediately obvious what
channel_indexis.This will be removed as we will be using only one device.
Quoted 62 lines
Why not just have a direct pointer to the correct channel?
drivers/power/generic-battery.c line 44
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+struct generic_adc_bat { +struct power_supply psy; +struct iio_channel *channels; +int channel_index; +}; + +struct generic_bat {Spacing is a bit random in here
Quoted 52 lines
drivers/power/generic-battery.c new file +52
-0+struct generic_adc_bat bat[BAT_MAX ]; +struct generic_platform_data pdata; +int volt_value; +int cur_value; +unsigned int timestamp; +int level; +int status; + int cable_plugged:1; +}; + +static struct generic_bat generic_bat = { + .bat[MAIN_BAT ] = { + .psy.name ="main-bat" , + }, + .bat[BACKUP_BAT ] = { + .psy.name ="backup-bat" , + }, +}; + +static struct delayed_work bat_work; + +static void generic_adc_bat_ext_power_changed (struct power_supply *psy) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +} + +static enum power_supply_property generic_adc_main_bat_props[] = { +POWER_SUPPLY_PROP_STATUS , +POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN , +POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN , +POWER_SUPPLY_PROP_CHARGE_NOW , +POWER_SUPPLY_PROP_VOLTAGE_NOW , +POWER_SUPPLY_PROP_CURRENT_NOW , +POWER_SUPPLY_PROP_TECHNOLOGY , +POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN , +POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN , +POWER_SUPPLY_PROP_MODEL_NAME , +}; + +static int charge_finished (struct generic_bat *bat) +{ +return bat->pdata.gpio_inverted ? + !gpio_get_value (bat->pdata.gpio_charge_finished) : +gpio_get_value (bat->pdata.gpio_charge_finished); +} + +static int generic_adc_bat_get_property (struct power_supply *psy, +enum power_supply_property psp, +union power_supply_propval *val) +{ +struct generic_adc_bat *adc_bat;Quoted 8 lines
drivers/power/generic-battery.c line 103
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+struct generic_bat *bat; +int ret, scaleint, scalepart, iio_val; +long result =0 ; + +if (!strcmp (psy->name,"main-battery" )) { + adc_bat =container_of (psy, + struct generic_adc_bat, psy);This first statement is I think shared so move it out of the if / else
Quoted 3 lines
This whole logic will change as we will be using only one device and if has user has multiple batteries then he can register multiple devices.
Yes, much cleaner that way.
Quoted 28 lines
Quoted 15 lines
Quoted 4 lines
drivers/power/generic-battery.c new file +3
-0+ bat =container_of (adc_bat, + struct generic_bat, bat[MAIN_BAT]); + }else if (!strcmp (psy->name,"backup-battery" )) {drivers/power/generic-battery.c line 116
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+ adc_bat = container_of(psy, struct generic_adc_bat, psy); + bat =container_of (adc_bat, + struct generic_bat, bat[BACKUP_BAT]); + }else { +/* Ideally this should never happen */ +WARN_ON (1 ); +return -EINVAL ; + } + +if (!bat) {> + dev_err(psy->dev, "no battery infos ?!\n");Quoted 13 lines
drivers/power/generic-battery.c line 126
Quoted 9 lines
drivers/power/generic-battery.c new file +9
-0+return -EINVAL ; + + ret =iio_read_channel_raw (&adc_bat->channels[adc_bat->channel_index], + &iio_val); + ret =iio_read_channel_scale (&adc_bat->channels[adc_bat->channel_index], + &scaleint, &scalepart); +if (ret <0 ) +return ret; +A quick comment here on what the battery framework is expecting vs IIO supplying would be good. It may be that this particular output is one that we should lift over into the core rather than end up with multiple drivers doing the same thing.
Quoted 5 lines
I think for this we need to get all the channels supported by a particular adc device and once we get the information regarding the channel(current/voltage/power) supported we can find out "What information is exported about this channel which may include scale or raw adc value".This can be used to call the corresponding API's.
Agreed. If I'd read this far I'd never have said the same thing at the top of this email ;)
Quoted 67 lines
Quoted 29 lines
Quoted 29 lines
drivers/power/generic-battery.c new file +28
-0+switch (ret) { +case IIO_VAL_INT : + result = iio_val * scaleint; +break ; +case IIO_VAL_INT_PLUS_MICRO : + result = (s64 )iio_val * (s64 )scaleint + +div_s64 ((s64 )iio_val * (s64 )scalepart,1000000LL ); +break ; +case IIO_VAL_INT_PLUS_NANO : + result = (s64 )iio_val * (s64 )scaleint + +div_s64 ((s64 )iio_val * (s64 )scalepart,1000000000LL ); +break ; + } + +switch (psp) { +case POWER_SUPPLY_PROP_STATUS : +if (bat->pdata.gpio_charge_finished <0 ) + val->intval = bat->level ==100000 ? +POWER_SUPPLY_STATUS_FULL : bat->status; +else + val->intval = bat->status; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN : + val->intval =0 ; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_NOW : + val->intval = bat->pdata.cal_charge (result); +return 0 ;Quoted 8 lines
drivers/power/generic-battery.c line 160
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+case POWER_SUPPLY_PROP_VOLTAGE_NOW : + val->intval = result; +return 0 ; +case POWER_SUPPLY_PROP_CURRENT_NOW : + val->intval = result; +return 0 ;why return from these and break from the later ones (to return much the same I think...)?
my mistake.
Quoted 21 lines
Quoted 21 lines
drivers/power/generic-battery.c new file +21
-0+case POWER_SUPPLY_PROP_TECHNOLOGY : + val->intval = bat->pdata.battery_info.technology; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN : + val->intval = bat->pdata.battery_info.voltage_min_design; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN : + val->intval = bat->pdata.battery_info.voltage_max_design; +break ; +case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN : + val->intval = bat->pdata.battery_info.charge_full_design; +break ; +case POWER_SUPPLY_PROP_MODEL_NAME : + val->strval = bat->pdata.battery_info.name; +break ; +default : +return -EINVAL ; + } +return ret; +} +Quoted 8 lines
drivers/power/generic-battery.c line 187
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+static void generic_adc_bat_work (struct work_struct *work) +{ +struct generic_bat *bat = &generic_bat; +int is_charged; +int is_plugged; +static int was_plugged;That's nasty. Prevents even the theoretical posibility of multiple copies of the driver. That should be in the device specific data.
Quoted 6 lines
yes should be part of device specific data.
Quoted 3 lines
drivers/power/generic-battery.c line 188
Quoted 2 lines
drivers/power/generic-battery.c new file +2
-0+ +/* backup battery doesn't have current monitoring capability anyway */ Always or in this particular configuration?
I was told by Anton Vorontsov.Anyway now this driver will be per device so shouldn't matter.
Maybe one day people will stick enough batteries on my phone for it to last one whole day ;)
Quoted 79 lines
Quoted 52 lines
Quoted 37 lines
drivers/power/generic-battery.c new file +36
-0+ is_plugged =power_supply_am_i_supplied (&bat->bat[MAIN_BAT ].psy); + bat->cable_plugged = is_plugged; +if (is_plugged != was_plugged) { + was_plugged = is_plugged; +if (is_plugged) + bat->status =POWER_SUPPLY_STATUS_CHARGING ; +else + bat->status =POWER_SUPPLY_STATUS_DISCHARGING ; + }else { +if ((bat->pdata.gpio_charge_finished >=0 ) && is_plugged) { + is_charged =charge_finished (&generic_bat); +if (is_charged) + bat->status =POWER_SUPPLY_STATUS_FULL ; +else + bat->status =POWER_SUPPLY_STATUS_CHARGING ; + } + } + +power_supply_changed (&bat->bat[MAIN_BAT ].psy); +} + +static irqreturn_t generic_adc_bat_charged (int irq,void *dev_id) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +return IRQ_HANDLED ; +} + +static int __devinit generic_adc_bat_probe (struct platform_device *pdev) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct iio_channel *channels; +int num_channels =0 ; +int ret, i, j, k =0 ; +enum iio_chan_type type; +drivers/power/generic-battery.c line 235
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) { + generic_bat.bat[i].psy.type =POWER_SUPPLY_TYPE_BATTERY ; + generic_bat.bat[i].psy.properties = + generic_adc_main_bat_props; + generic_bat.bat[i].psy.num_properties = +ARRAY_SIZE (generic_adc_main_bat_props); + generic_bat.bat[i].psy.get_property = + generic_adc_bat_get_property; + generic_bat.bat[i].psy.external_power_changed = + generic_adc_bat_ext_power_changed;Could you do this with a static const array? Looks like there is nothing dynamic in here and that would make for cleaner code to read. Given other elements of psy are clearly dynamic (dev pointer) you will have to memcpy the static version in which is tedious. Still easier to read though in my view.
Quoted 1 line
drivers/power/generic-battery.c line 235
Quoted 1 line
drivers/power/generic-battery.c new file +1
-0+ }right.
Quoted 4 lines
Quoted 4 lines
drivers/power/generic-battery.c new file +4
-0+ + generic_bat.volt_value =-1 ; + generic_bat.cur_value =-1 ; + generic_bat.cable_plugged =0 ;Quoted 8 lines
drivers/power/generic-battery.c line 244
Quoted 4 lines
drivers/power/generic-battery.c new file +4
-0+ generic_bat.status =POWER_SUPPLY_STATUS_DISCHARGING ; + +memcpy (&generic_bat.pdata, pdata,sizeof (struct generic_platform_data )); +Is there anything dynamic in the pdata? If not it would be cleaner just to use a pointer to it rather than make a copy (I can't immediately spot anything but them I'm not familiar with the battery
side of things.We can just assign it to
generic_batso that it can be used in the relevant functions.Otherwise we need to usedev_set_drvdataanddev_get_drvdata.Quoted 10 lines
drivers/power/generic-battery.c line 252
Quoted 8 lines
drivers/power/generic-battery.c new file +8
-0+for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) { + ret =power_supply_register (&pdev->dev, + &generic_bat.bat[i].psy); +if (ret) + goto err_reg_main; + } + + channels =iio_channel_get_all (dev_name (&pdev->dev));I would give these explicit names and get the two individual channels by name. I think it will give you cleaner code.
Quoted 2 lines
Yes, now it will be based on
pdata->voltage_channel,pdata->current_channeland so on.We will use this to get the channel.
Maybe better to just do this via the iio_map structures in the platform data and standard naming for each channel.
"voltage", "current", "power" would do nicely. Note we'll have to add the relevant naming for the other side to more IIO device drivers via the
'datasheet_name' element of iio_chan_spec. A quick grep shows this
is only done in drivers/staging/iio/max1363_core.c so far (bet you can't guess what is soldered to my test board :)
Quoted 154 lines
Quoted 9 lines
Quoted 9 lines
drivers/power/generic-battery.c new file +9
-0+if (IS_ERR (channels)) { + ret =PTR_ERR (channels); + goto err_reg_backup; + } + +while (channels[num_channels].indio_dev) + num_channels++; + +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++)Quoted 8 lines
drivers/power/generic-battery.c line 267
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+ generic_bat.bat[i].channels =kzalloc (sizeof (struct iio_channel ) + *BAT_MAX ,GFP_KERNEL ); + +/* assuming main battery and backup battery is using the same channel */ +for (i =0 ; i < num_channels; i++) { + ret =iio_get_channel_type (&channels[i], &type);Using named channels you should 'know' you have the correct type. You can of course check if you don't trust your platform data though!
right.
Quoted 29 lines
Quoted 29 lines
drivers/power/generic-battery.c new file +29
-0+if (ret <0 ) + goto err_gpio; + +if (type ==IIO_VOLTAGE || type ==IIO_CURRENT ) { +for (j =0 ; j <BAT_MAX ; j++) { + generic_bat.bat[j].channel_index = k; + generic_bat.bat[j].channels[k] = channels[i]; + } + k++; + } +continue ; + } + +INIT_DELAYED_WORK (&bat_work, generic_adc_bat_work); + +if (pdata->gpio_charge_finished >=0 ) { + ret =gpio_request (pdata->gpio_charge_finished,"charged" ); +if (ret) + goto err_gpio; + + ret =request_irq (gpio_to_irq (pdata->gpio_charge_finished), + generic_adc_bat_charged, +IRQF_TRIGGER_RISING |IRQF_TRIGGER_FALLING , +"battery charged" ,NULL ); +if (ret) + goto err_gpio; + } + +dev_info (&pdev->dev,"successfully loaded\n" );Quoted 8 lines
drivers/power/generic-battery.c line 302
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +iio_channel_release_all (channels);Not if you want to prevent the adc driver from being removed from under you. They should only be released in the driver removal.
right.
Quoted 13 lines
Quoted 13 lines
drivers/power/generic-battery.c new file +13
-0+ +return 0 ; + +err_gpio: +iio_channel_release_all (channels); +err_reg_backup: +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy); +err_reg_main: +return ret; +} + +static int generic_adc_bat_remove (struct platform_device *pdev)Quoted 8 lines
drivers/power/generic-battery.c line 321
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+{ +int i; +struct generic_platform_data *pdata = pdev->dev.platform_data; + +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy);You should release the IIO channels here when they are no longer needed.
right.
Quoted 68 lines
drivers/power/generic-battery.c line 322
Quoted 2 lines
drivers/power/generic-battery.c new file +2
-0+ +if (pdata->gpio_charge_finished >=0 ) {I forget, is a gpio index of 0 definitely invalid?
Quoted 65 lines
drivers/power/generic-battery.c new file +51
-0+free_irq (gpio_to_irq (pdata->gpio_charge_finished),NULL ); +gpio_free (pdata->gpio_charge_finished); + } + +cancel_delayed_work (&bat_work); + +return 0 ; +} + +#ifdef CONFIG_PM +static int generic_adc_bat_suspend (struct platform_device *pdev, +pm_message_t state) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct generic_bat *bat =container_of (pdata, + struct generic_bat, pdata); + +cancel_delayed_work_sync (&bat_work); + bat->status =POWER_SUPPLY_STATUS_UNKNOWN ; + +return 0 ; +} + +static int generic_adc_bat_resume (struct platform_device *pdev) +{ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +return 0 ; +} +#else +#define generic_adc_bat_suspend NULL +#define generic_adc_bat_resume NULL +#endif + +static struct platform_driver generic_adc_bat_driver = { + .driver = { + .name ="generic-adc-battery" , + }, + .probe = generic_adc_bat_probe, + .remove = generic_adc_bat_remove, + .suspend = generic_adc_bat_suspend, + .resume = generic_adc_bat_resume, +}; + +module_platform_driver (generic_adc_bat_driver); + +MODULE_AUTHOR ("anish kumar <[email protected]>" ); +MODULE_DESCRIPTION ("generic battery driver using IIO" ); +MODULE_LICENSE ("GPL" );include/linux/power/generic-battery.h new file +8
-0@@ -0,0 +1,26 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef GENERIC_BATTERY_H +#define GENERIC_BATTERY_H Quoted 8 lines
include/linux/power/generic-battery.h line 13
Quoted 5 lines
include/linux/power/generic-battery.h new file +5
-0+ +#include <linux/power_supply.h> + +/** +* struct generic_platform_data - platform data for generic battery A little inconsistent on capitalization. (might as well do it now or one of those people who delight in trivial patches will 'tidy' it up for you :)
Quoted 2 lines
I don't mind as it is a good start for people to get familiar with the linux patch process.
Can tell you don't get to deal with the tedious merge conflicts that result ;)
Quoted 18 lines
However I will address this valuable comments.
Quoted 15 lines
Quoted 14 lines
include/linux/power/generic-battery.h new file +13
-0+* @battery_info: Information about the battery +* @cal_charge: platform callback to calculate charge +* @gpio_charge_finished: GPIO number used for interrupts +* @gpio_inverted: Is the gpio inverted? +*/ +struct generic_platform_data { +struct power_supply_info battery_info; +int (*cal_charge)(long ); +int gpio_charge_finished; +int gpio_inverted; +}; + +#endif /* GENERIC_BATTERY_H */
Jonathan Cameron
maintainerQuoted 78 lines · anish kumar
Quoted 10 lines · Lars-Peter Clausen
Quoted 7 lines · anish kumar
From: anish kumar <[email protected]>
This patch is to use IIO to write a generic batttery driver. There are some inherent assumptions here:
1.User is having both main battery and backup battery. 2.Both batteries use same channel to get the information.Hi thanks for stepping up to take care of this and sorry for the late reply.
Thanks to you.
Quoted 26 lines
Quoted 3 lines
Signed-off-by anish kumar <[email protected]>---
[...]
Quoted 22 lines
drivers/power/generic-battery.c new file +22
-0+ +struct generic_adc_bat { +struct power_supply psy; +struct iio_channel *channels; +int channel_index; +}; + +struct generic_bat { +struct generic_adc_bat bat[BAT_MAX ]; +struct generic_platform_data pdata; +int volt_value; +int cur_value; +unsigned int timestamp; +int level; +int status; +int cable_plugged:1 ; +}; + +static struct generic_bat generic_bat = { + .bat[MAIN_BAT ] = { + .psy.name ="main-bat" , + },Quoted 8 lines
drivers/power/generic-battery.c line 62
Quoted 4 lines
drivers/power/generic-battery.c new file +4
-0+ .bat[BACKUP_BAT] = { + .psy.name = "backup-bat", + }, +};This should be per device. I'd also just use one
power_supplyper device. If somebody has multiple batteries in their system they can register multiple devices. This should make the driver more flexible.Yes makes sense of having one driver/device and if someone has more devices to register than it can be done by registering multiple devices with different platform data.
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ +static struct delayed_work bat_work;This should also go into the
generic_batstruct.right.
Quoted 15 lines
+
Quoted 13 lines
drivers/power/generic-battery.c new file +13
-0+static void generic_adc_bat_ext_power_changed (struct power_supply *psy) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +} + +static enum power_supply_property generic_adc_main_bat_props[] = { +POWER_SUPPLY_PROP_STATUS , +POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN , +POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN , +POWER_SUPPLY_PROP_CHARGE_NOW , +POWER_SUPPLY_PROP_VOLTAGE_NOW , +POWER_SUPPLY_PROP_CURRENT_NOW ,Quoted 8 lines
drivers/power/generic-battery.c line 83
Quoted 5 lines
drivers/power/generic-battery.c new file +5
-0+POWER_SUPPLY_PROP_TECHNOLOGY , +POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN , +POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN , + POWER_SUPPLY_PROP_MODEL_NAME, +};It probably make sense to create this at runtime, depending on which kind of IIO channels are provided. E.g. for VOLTAGE_NOW/CURRENT_NOW.
Quoted 4 lines
Yes, something like pdata->voltage_channel/pdata->current_channel/pdata->power_channel and if this property is set then register the VOLTAGE_NOW/CURRENT_NOW/POWER_ property.
As stated in the other branch of this thread, just use standard naming and do this via the iio_map that you'll need in platform data anyway.
Quoted 254 lines
Quoted 30 lines
[...]
Quoted 28 lines
drivers/power/generic-battery.c new file +28
-0+ +static int generic_adc_bat_get_property (struct power_supply *psy, +enum power_supply_property psp, +union power_supply_propval *val) +{ +struct generic_adc_bat *adc_bat; +struct generic_bat *bat; +int ret, scaleint, scalepart, iio_val; +long result =0 ; + +if (!strcmp (psy->name,"main-battery" )) { + adc_bat =container_of (psy, + struct generic_adc_bat, psy); + bat =container_of (adc_bat, + struct generic_bat, bat[MAIN_BAT]); + }else if (!strcmp (psy->name,"backup-battery" )) { + adc_bat = container_of(psy, struct generic_adc_bat, psy); + bat =container_of (adc_bat, + struct generic_bat, bat[BACKUP_BAT]); + }else { +/* Ideally this should never happen */ +WARN_ON (1 ); +return -EINVAL ; + } + +if (!bat) { +dev_err (psy->dev,"no battery infos ?!\n" ); +return -EINVAL ;Quoted 8 lines
drivers/power/generic-battery.c line 123
Quoted 5 lines
drivers/power/generic-battery.c new file +5
-0+ } + ret =iio_read_channel_raw (&adc_bat->channels[adc_bat->channel_index], + &iio_val); + ret =iio_read_channel_scale (&adc_bat->channels[adc_bat->channel_index], + &scaleint, &scalepart);It would probably make sense to only sample if the attribute for VOLTAGE_NOW/CURRENT_NOW property is read.
right.
Quoted 20 lines
[...]
Quoted 17 lines
drivers/power/generic-battery.c new file +17
-0+ +switch (psp) { +case POWER_SUPPLY_PROP_STATUS : +if (bat->pdata.gpio_charge_finished <0 ) + val->intval = bat->level ==100000 ? +POWER_SUPPLY_STATUS_FULL : bat->status; +else + val->intval = bat->status; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN : + val->intval =0 ; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_NOW : + val->intval = bat->pdata.cal_charge (result); +return 0 ; +case POWER_SUPPLY_PROP_VOLTAGE_NOW : + val->intval = result;Quoted 8 lines
drivers/power/generic-battery.c line 160
Quoted 4 lines
drivers/power/generic-battery.c new file +4
-0+return 0 ; +case POWER_SUPPLY_PROP_CURRENT_NOW : + val->intval = result; +return 0 ;also this will return the same value for
VOLTAGE_NOWandCURRENT_NOWsince there is only one channel per battery. It might make sense to allow multiple channels per battery, one reporting current one voltage and also one for power.absolutely makes sense.
Quoted 37 lines
Quoted 23 lines
drivers/power/generic-battery.c new file +21
-0+case POWER_SUPPLY_PROP_TECHNOLOGY : + val->intval = bat->pdata.battery_info.technology; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN : + val->intval = bat->pdata.battery_info.voltage_min_design; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN : + val->intval = bat->pdata.battery_info.voltage_max_design; +break ; +case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN : + val->intval = bat->pdata.battery_info.charge_full_design; +break ; +case POWER_SUPPLY_PROP_MODEL_NAME : + val->strval = bat->pdata.battery_info.name; +break ; +default : +return -EINVAL ; + } +return ret; +} +[...]
Quoted 13 lines
drivers/power/generic-battery.c new file +13
-0+ +static int __devinit generic_adc_bat_probe (struct platform_device *pdev) +{ +[...] + +/* assuming main battery and backup battery is using the same channel */ +for (i =0 ; i < num_channels; i++) { + ret =iio_get_channel_type (&channels[i], &type); +if (ret <0 ) + goto err_gpio; + +if (type ==IIO_VOLTAGE || type ==IIO_CURRENT ) { +for (j =0 ; j <BAT_MAX ; j++) {Quoted 8 lines
drivers/power/generic-battery.c line 278
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+ generic_bat.bat[j].channel_index = k; + generic_bat.bat[j].channels[k] = channels[i]; + } + k++; + } +continue ;continue at the end of a loop is a noop.
right.
Quoted 8 lines
drivers/power/generic-battery.c line 283
Quoted 6 lines
drivers/power/generic-battery.c new file +5
-0+ } + +INIT_DELAYED_WORK (&bat_work, generic_adc_bat_work); + +if (pdata->gpio_charge_finished >=0 ) {
gpio_is_validright.
Quoted 16 lines
Quoted 4 lines
drivers/power/generic-battery.c new file +3
-0+ ret =gpio_request (pdata->gpio_charge_finished,"charged" ); +if (ret) + goto err_gpio;drivers/power/generic-battery.c line 296
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+ + ret =request_irq (gpio_to_irq (pdata->gpio_charge_finished), + generic_adc_bat_charged, +IRQF_TRIGGER_RISING |IRQF_TRIGGER_FALLING , +"battery charged" ,NULL ); +if (ret) + goto err_gpio; + } + +dev_info (&pdev->dev,"successfully loaded\n" );I'd remove that message.
ok.
Quoted 52 lines
Quoted 19 lines
drivers/power/generic-battery.c new file +18
-0+ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +iio_channel_release_all (channels); + +return 0 ; + +err_gpio: +iio_channel_release_all (channels); +err_reg_backup: +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy); +err_reg_main: +return ret; +} +[...]
Quoted 30 lines
drivers/power/generic-battery.c new file +30
-0+ +#ifdef CONFIG_PM +static int generic_adc_bat_suspend (struct platform_device *pdev, +pm_message_t state) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct generic_bat *bat =container_of (pdata, + struct generic_bat, pdata); + +cancel_delayed_work_sync (&bat_work); + bat->status =POWER_SUPPLY_STATUS_UNKNOWN ; + +return 0 ; +} + +static int generic_adc_bat_resume (struct platform_device *pdev) +{ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +return 0 ; +} +#else +#define generic_adc_bat_suspend NULL +#define generic_adc_bat_resume NULL +#endif + +static struct platform_driver generic_adc_bat_driver = { + .driver = {Quoted 8 lines
drivers/power/generic-battery.c line 367
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+ .name = "generic-adc-battery", + }, + .probe = generic_adc_bat_probe, + .remove = generic_adc_bat_remove, + .suspend = generic_adc_bat_suspend, + .resume = generic_adc_bat_resume,Use
dev_pm_opsinstead. Using the suspend and resume callbacks is deprecated.makes sense.
Quoted 28 lines
Quoted 28 lines
drivers/power/generic-battery.c new file +7
-0+}; + +module_platform_driver (generic_adc_bat_driver); + +MODULE_AUTHOR ("anish kumar <[email protected]>" ); +MODULE_DESCRIPTION ("generic battery driver using IIO" ); +MODULE_LICENSE ("GPL" );include/linux/power/generic-battery.h new file +14
-0@@ -0,0 +1,26 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef GENERIC_BATTERY_H +#define GENERIC_BATTERY_H + +#include <linux/power_supply.h> + +/** +* struct generic_platform_data - platform data for generic battery +* @battery_info: Information about the battery Quoted 8 lines
include/linux/power/generic-battery.h line 19
Quoted 5 lines
include/linux/power/generic-battery.h new file +5
-0+* @cal_charge: platform callback to calculate charge +* @gpio_charge_finished: GPIO number used for interrupts +* @gpio_inverted: Is the gpio inverted? +*/ +struct generic_platform_data {The name might be a bit to generic ;) I'd go for
generic_battery_platform_dataoriio_battery_platform_data.it will be
iio_battery_platform_data.Quoted 9 lines
include/linux/power/generic-battery.h line 26
Quoted 8 lines
include/linux/power/generic-battery.h new file +7
-0+struct power_supply_info battery_info; +int (*cal_charge)(long ); +int gpio_charge_finished; + int gpio_inverted; +}; + +#endif /* GENERIC_BATTERY_H */ --
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 407 lines · Jonathan Cameron
Quoted 11 lines · anish kumar
Thanks for your time.
Quoted 9 lines · Jonathan Cameron
Quoted 3 lines · anish kumar
From: anish kumar <[email protected]>
This patch is to use IIO to write a generic batttery driver.
battery
Quoted 2 lines
There are some inherent assumptions here:
1.User is having both main battery and backup battery.Seems rather like that could be easily enough relaxed or configured via platform data?
Quoted 8 lines
Yes
Quoted 3 lines
Quoted 1 line
2.Both batteries use same channel to get the information.
Do you mean same actual channel (physical pin) or same ADC (with appropriate mux etc)?
As lars pointed out it would be better if we have multiple channels per battery.The channel what I am referring here is the name of different channels which will be exported by adc driver to get voltage, current and power.
I agree entirely with what Lars-Peter said. A separate instance of the driver per battery will make life much cleaner and allow as you say for separate channels for voltage, current and power for each one. I guess it may be a case of trying to get each one for every battery and continue with whatever turns out to be available.
Quoted 10 lines
Quoted 8 lines
Quoted 1 line
Mostly fine. There are a few corners I didn't understand as I don't have time to dive into the battery framework in the near future. Will leave it up to those more familiar with that side of things!
My only real issue is that it would be cleaner to do the individual channels by name rather than a get all as you 'know' here how many channels are expected.
agreed.
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Also don't release the IIO channels until you are actually finished using them as holding them should prevent the ADC module from going away underneath you (which is nasty :)
agreed.
Quoted 2 lines
Sorry it took so long to get back to you on this.
That is the biggest advantage of working in open-source. We can take our own time.
:)
Quoted 3 lines
Quoted 2 lines
So what happened in 1985?
Was hurrying to get an email id :)
And there I was imagining a magical significance to it....
Quoted 161 lines
Quoted 72 lines
Jonathan
Quoted 15 lines
Signed-off-by anish kumar <[email protected]>--- drivers/power/Kconfig | 8 + drivers/power/Makefile | 1 + drivers/power/generic-battery.c | 374 +++++++++++++++++++++++++++++++++ include/linux/power/generic-battery.h | 26 +++ 4 files changed, 409 insertions(+), 0 deletions(-)create mode 100644
drivers/power/generic-battery.ccreate mode 100644include/linux/power/generic-battery.hdrivers/power/Kconfig +0
-0drivers/power/Kconfig config AB8500_BATTERY_THERM_ON_BATCTRL · line 318
Quoted 10 lines
drivers/power/Kconfig +7
-0@@ -309,6 +309,14 @@ config AB8500_BATTERY_THERM_ON_BATCTRL help Say Y to enable battery temperature measurements using thermistor connected on BATCTRL ADC. + +config GENERIC_BATTERY + tristate "Generic battery support using IIO" + depends on IIO + help + Say Y here to enable support for the generic battery driver + which uses IIO framework to read adc for it's main and backupADC
Quoted 19 lines
drivers/power/Kconfig +1
-0+ battery. endif # POWER_SUPPLY source "drivers/power/avs/Kconfig"drivers/power/Makefile +1
-0@@ -45,3 +45,4 @@ obj-$(CONFIG_CHARGER_MAX8997) += max8997_charger.o obj-$(CONFIG_CHARGER_MAX8998) += max8998_charger.o obj-$(CONFIG_POWER_AVS) += avs/ obj-$(CONFIG_CHARGER_SMB347) += smb347-charger.o +obj-$(CONFIG_GENERIC_BATTERY) += generic-battery.odrivers/power/generic-battery.c new file +0
-0drivers/power/generic-battery.c line 2
Quoted 2 lines
drivers/power/generic-battery.c new file +2
-0@@ -0,0 +1,374 @@ +/* +* Generice battery driver code using IIO Generic
drivers/power/generic-battery.c line 10
Quoted 8 lines
drivers/power/generic-battery.c new file +8
-0+* Copyright (C) 2012, Anish Kumar <[email protected]> +* based on jz4740-battery.c +* based on s3c_adc_battery.c +* +* This file is subject to the terms and conditions of the GNU General Public +* License. See the file COPYING in the main directory of this archive for +* more details. +* bonus blank line to remove
Quoted 13 lines
drivers/power/generic-battery.c new file +13
-0+*/ + +#include <linux/interrupt.h> +#include <linux/platform_device.h> +#include <linux/power_supply.h> +#include <linux/gpio.h> +#include <linux/err.h> +#include <linux/timer.h> +#include <linux/jiffies.h> +#include <linux/errno.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/slab.h> Quoted 8 lines
drivers/power/generic-battery.c line 30
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+#include <linux/iio/consumer.h> +#include <linux/iio/types.h> + +#include <linux/power/generic-battery.h> + +#define BAT_POLL_INTERVAL 10000/* ms */ +#define JITTER_DELAY 500/* ms */ Elements to make configurable?
Yes probably make it platform data?
Quoted 9 lines
drivers/power/generic-battery.c line 37
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+ +enum BAT { +MAIN_BAT =0 , +BACKUP_BAT , +BAT_MAX , +}; +Document this please. It's not immediately obvious what
channel_indexis.This will be removed as we will be using only one device.
Quoted 62 lines
Why not just have a direct pointer to the correct channel?
drivers/power/generic-battery.c line 44
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+struct generic_adc_bat { +struct power_supply psy; +struct iio_channel *channels; +int channel_index; +}; + +struct generic_bat {Spacing is a bit random in here
Quoted 52 lines
drivers/power/generic-battery.c new file +52
-0+struct generic_adc_bat bat[BAT_MAX ]; +struct generic_platform_data pdata; +int volt_value; +int cur_value; +unsigned int timestamp; +int level; +int status; + int cable_plugged:1; +}; + +static struct generic_bat generic_bat = { + .bat[MAIN_BAT ] = { + .psy.name ="main-bat" , + }, + .bat[BACKUP_BAT ] = { + .psy.name ="backup-bat" , + }, +}; + +static struct delayed_work bat_work; + +static void generic_adc_bat_ext_power_changed (struct power_supply *psy) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +} + +static enum power_supply_property generic_adc_main_bat_props[] = { +POWER_SUPPLY_PROP_STATUS , +POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN , +POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN , +POWER_SUPPLY_PROP_CHARGE_NOW , +POWER_SUPPLY_PROP_VOLTAGE_NOW , +POWER_SUPPLY_PROP_CURRENT_NOW , +POWER_SUPPLY_PROP_TECHNOLOGY , +POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN , +POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN , +POWER_SUPPLY_PROP_MODEL_NAME , +}; + +static int charge_finished (struct generic_bat *bat) +{ +return bat->pdata.gpio_inverted ? + !gpio_get_value (bat->pdata.gpio_charge_finished) : +gpio_get_value (bat->pdata.gpio_charge_finished); +} + +static int generic_adc_bat_get_property (struct power_supply *psy, +enum power_supply_property psp, +union power_supply_propval *val) +{ +struct generic_adc_bat *adc_bat;Quoted 8 lines
drivers/power/generic-battery.c line 103
Quoted 7 lines
drivers/power/generic-battery.c new file +7
-0+struct generic_bat *bat; +int ret, scaleint, scalepart, iio_val; +long result =0 ; + +if (!strcmp (psy->name,"main-battery" )) { + adc_bat =container_of (psy, + struct generic_adc_bat, psy);This first statement is I think shared so move it out of the if / else
Quoted 3 lines
This whole logic will change as we will be using only one device and if has user has multiple batteries then he can register multiple devices.
Yes, much cleaner that way.
Quoted 28 lines
Quoted 15 lines
Quoted 4 lines
drivers/power/generic-battery.c new file +3
-0+ bat =container_of (adc_bat, + struct generic_bat, bat[MAIN_BAT]); + }else if (!strcmp (psy->name,"backup-battery" )) {drivers/power/generic-battery.c line 116
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+ adc_bat = container_of(psy, struct generic_adc_bat, psy); + bat =container_of (adc_bat, + struct generic_bat, bat[BACKUP_BAT]); + }else { +/* Ideally this should never happen */ +WARN_ON (1 ); +return -EINVAL ; + } + +if (!bat) {> + dev_err(psy->dev, "no battery infos ?!\n");Quoted 13 lines
drivers/power/generic-battery.c line 126
Quoted 9 lines
drivers/power/generic-battery.c new file +9
-0+return -EINVAL ; + + ret =iio_read_channel_raw (&adc_bat->channels[adc_bat->channel_index], + &iio_val); + ret =iio_read_channel_scale (&adc_bat->channels[adc_bat->channel_index], + &scaleint, &scalepart); +if (ret <0 ) +return ret; +A quick comment here on what the battery framework is expecting vs IIO supplying would be good. It may be that this particular output is one that we should lift over into the core rather than end up with multiple drivers doing the same thing.
Quoted 5 lines
I think for this we need to get all the channels supported by a particular adc device and once we get the information regarding the channel(current/voltage/power) supported we can find out "What information is exported about this channel which may include scale or raw adc value".This can be used to call the corresponding API's.
Agreed. If I'd read this far I'd never have said the same thing at the top of this email ;)
Quoted 67 lines
Quoted 29 lines
Quoted 29 lines
drivers/power/generic-battery.c new file +28
-0+switch (ret) { +case IIO_VAL_INT : + result = iio_val * scaleint; +break ; +case IIO_VAL_INT_PLUS_MICRO : + result = (s64 )iio_val * (s64 )scaleint + +div_s64 ((s64 )iio_val * (s64 )scalepart,1000000LL ); +break ; +case IIO_VAL_INT_PLUS_NANO : + result = (s64 )iio_val * (s64 )scaleint + +div_s64 ((s64 )iio_val * (s64 )scalepart,1000000000LL ); +break ; + } + +switch (psp) { +case POWER_SUPPLY_PROP_STATUS : +if (bat->pdata.gpio_charge_finished <0 ) + val->intval = bat->level ==100000 ? +POWER_SUPPLY_STATUS_FULL : bat->status; +else + val->intval = bat->status; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN : + val->intval =0 ; +return 0 ; +case POWER_SUPPLY_PROP_CHARGE_NOW : + val->intval = bat->pdata.cal_charge (result); +return 0 ;Quoted 8 lines
drivers/power/generic-battery.c line 160
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+case POWER_SUPPLY_PROP_VOLTAGE_NOW : + val->intval = result; +return 0 ; +case POWER_SUPPLY_PROP_CURRENT_NOW : + val->intval = result; +return 0 ;why return from these and break from the later ones (to return much the same I think...)?
my mistake.
Quoted 21 lines
Quoted 21 lines
drivers/power/generic-battery.c new file +21
-0+case POWER_SUPPLY_PROP_TECHNOLOGY : + val->intval = bat->pdata.battery_info.technology; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN : + val->intval = bat->pdata.battery_info.voltage_min_design; +break ; +case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN : + val->intval = bat->pdata.battery_info.voltage_max_design; +break ; +case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN : + val->intval = bat->pdata.battery_info.charge_full_design; +break ; +case POWER_SUPPLY_PROP_MODEL_NAME : + val->strval = bat->pdata.battery_info.name; +break ; +default : +return -EINVAL ; + } +return ret; +} +Quoted 8 lines
drivers/power/generic-battery.c line 187
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+static void generic_adc_bat_work (struct work_struct *work) +{ +struct generic_bat *bat = &generic_bat; +int is_charged; +int is_plugged; +static int was_plugged;That's nasty. Prevents even the theoretical posibility of multiple copies of the driver. That should be in the device specific data.
Quoted 6 lines
yes should be part of device specific data.
Quoted 3 lines
drivers/power/generic-battery.c line 188
Quoted 2 lines
drivers/power/generic-battery.c new file +2
-0+ +/* backup battery doesn't have current monitoring capability anyway */ Always or in this particular configuration?
I was told by Anton Vorontsov.Anyway now this driver will be per device so shouldn't matter.
Maybe one day people will stick enough batteries on my phone for it to last one whole day ;)
Quoted 79 lines
Quoted 52 lines
Quoted 37 lines
drivers/power/generic-battery.c new file +36
-0+ is_plugged =power_supply_am_i_supplied (&bat->bat[MAIN_BAT ].psy); + bat->cable_plugged = is_plugged; +if (is_plugged != was_plugged) { + was_plugged = is_plugged; +if (is_plugged) + bat->status =POWER_SUPPLY_STATUS_CHARGING ; +else + bat->status =POWER_SUPPLY_STATUS_DISCHARGING ; + }else { +if ((bat->pdata.gpio_charge_finished >=0 ) && is_plugged) { + is_charged =charge_finished (&generic_bat); +if (is_charged) + bat->status =POWER_SUPPLY_STATUS_FULL ; +else + bat->status =POWER_SUPPLY_STATUS_CHARGING ; + } + } + +power_supply_changed (&bat->bat[MAIN_BAT ].psy); +} + +static irqreturn_t generic_adc_bat_charged (int irq,void *dev_id) +{ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); +return IRQ_HANDLED ; +} + +static int __devinit generic_adc_bat_probe (struct platform_device *pdev) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct iio_channel *channels; +int num_channels =0 ; +int ret, i, j, k =0 ; +enum iio_chan_type type; +drivers/power/generic-battery.c line 235
Quoted 10 lines
drivers/power/generic-battery.c new file +10
-0+for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) { + generic_bat.bat[i].psy.type =POWER_SUPPLY_TYPE_BATTERY ; + generic_bat.bat[i].psy.properties = + generic_adc_main_bat_props; + generic_bat.bat[i].psy.num_properties = +ARRAY_SIZE (generic_adc_main_bat_props); + generic_bat.bat[i].psy.get_property = + generic_adc_bat_get_property; + generic_bat.bat[i].psy.external_power_changed = + generic_adc_bat_ext_power_changed;Could you do this with a static const array? Looks like there is nothing dynamic in here and that would make for cleaner code to read. Given other elements of psy are clearly dynamic (dev pointer) you will have to memcpy the static version in which is tedious. Still easier to read though in my view.
Quoted 1 line
drivers/power/generic-battery.c line 235
Quoted 1 line
drivers/power/generic-battery.c new file +1
-0+ }right.
Quoted 4 lines
Quoted 4 lines
drivers/power/generic-battery.c new file +4
-0+ + generic_bat.volt_value =-1 ; + generic_bat.cur_value =-1 ; + generic_bat.cable_plugged =0 ;Quoted 8 lines
drivers/power/generic-battery.c line 244
Quoted 4 lines
drivers/power/generic-battery.c new file +4
-0+ generic_bat.status =POWER_SUPPLY_STATUS_DISCHARGING ; + +memcpy (&generic_bat.pdata, pdata,sizeof (struct generic_platform_data )); +Is there anything dynamic in the pdata? If not it would be cleaner just to use a pointer to it rather than make a copy (I can't immediately spot anything but them I'm not familiar with the battery
side of things.We can just assign it to
generic_batso that it can be used in the relevant functions.Otherwise we need to usedev_set_drvdataanddev_get_drvdata.Quoted 10 lines
drivers/power/generic-battery.c line 252
Quoted 8 lines
drivers/power/generic-battery.c new file +8
-0+for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) { + ret =power_supply_register (&pdev->dev, + &generic_bat.bat[i].psy); +if (ret) + goto err_reg_main; + } + + channels =iio_channel_get_all (dev_name (&pdev->dev));I would give these explicit names and get the two individual channels by name. I think it will give you cleaner code.
Quoted 2 lines
Yes, now it will be based on
pdata->voltage_channel,pdata->current_channeland so on.We will use this to get the channel.
Quoted 8 lines
Maybe better to just do this via the
iio_mapstructures in the platform data and standard naming for each channel."voltage", "current", "power" would do nicely. Note we'll have to add the relevant naming for the other side to more IIO device drivers via the
'datasheet_name' element ofiio_chan_spec. A quick grep shows this
is only done indrivers/staging/iio/max1363_core.cso far (bet you can't guess what is soldered to my test board :)
Sorry I couldn't understand this but this is what I came up with.Hope it doesn't disappoint.
It has the advantage of getting extended easily.
enum chan_type{
VOLTAGE,
CURRENT,
POWER,
MAX_TYPE
};
struct channel_map {
struct iio_map channel;
enum chan_type type;
};struct iio_battery_platform_data {
struct power_supply_info battery_info;
struct channel_map *map;
int num_map;
char battery_name[BAT_MAX_NAME];
int (*cal_charge)(long);
int gpio_charge_finished;
int gpio_inverted;
int bat_poll_interval;
int jitter_delay;
};here goes the assignment:
for(i = 0; i < pdata->num_map; i++) {
switch(pdata->map[i].type)
case VOLTAGE:
adc_bat->channel[VOLTAGE] =
iio_channel_get(
pdata->map[i].channel.consumer_dev_name,
pdata->map[i].channel.adc_channel_label);
adc_bat->chan_index = 1 << VOLTAGE;
break;
case CURRENT:
adc_bat->channel[CURRENT] =
iio_channel_get(
pdata->map->channel->consumer_dev_name,
pdata->map[i].channel.adc_channel_label);
adc_bat->chan_index = 1 << CURRENT;
break;
case POWER:
adc_bat->channel[POWER] =
iio_channel_get(
pdata->map[i].channel.consumer_dev_name,
pdata->map[i].channel.adc_channel_label);
adc_bat->chan_index = 1 << POWER;
break;
default:
pr_info("add any other channels here in the case\n");
}With the chan_index we can know which property is set or every time we need to run through pdata->map[i].type to know the property set.
Quoted 177 lines
Quoted 155 lines
Quoted 10 lines
Quoted 10 lines
drivers/power/generic-battery.c new file +9
-0+if (IS_ERR (channels)) { + ret =PTR_ERR (channels); + goto err_reg_backup; + } + +while (channels[num_channels].indio_dev) + num_channels++; + +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++)Quoted 8 lines
drivers/power/generic-battery.c line 267
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+ generic_bat.bat[i].channels =kzalloc (sizeof (struct iio_channel ) + *BAT_MAX ,GFP_KERNEL ); + +/* assuming main battery and backup battery is using the same channel */ +for (i =0 ; i < num_channels; i++) { + ret =iio_get_channel_type (&channels[i], &type);Using named channels you should 'know' you have the correct type. You can of course check if you don't trust your platform data though!
right.
Quoted 29 lines
Quoted 29 lines
drivers/power/generic-battery.c new file +29
-0+if (ret <0 ) + goto err_gpio; + +if (type ==IIO_VOLTAGE || type ==IIO_CURRENT ) { +for (j =0 ; j <BAT_MAX ; j++) { + generic_bat.bat[j].channel_index = k; + generic_bat.bat[j].channels[k] = channels[i]; + } + k++; + } +continue ; + } + +INIT_DELAYED_WORK (&bat_work, generic_adc_bat_work); + +if (pdata->gpio_charge_finished >=0 ) { + ret =gpio_request (pdata->gpio_charge_finished,"charged" ); +if (ret) + goto err_gpio; + + ret =request_irq (gpio_to_irq (pdata->gpio_charge_finished), + generic_adc_bat_charged, +IRQF_TRIGGER_RISING |IRQF_TRIGGER_FALLING , +"battery charged" ,NULL ); +if (ret) + goto err_gpio; + } + +dev_info (&pdev->dev,"successfully loaded\n" );Quoted 8 lines
drivers/power/generic-battery.c line 302
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +iio_channel_release_all (channels);Not if you want to prevent the adc driver from being removed from under you. They should only be released in the driver removal.
right.
Quoted 13 lines
Quoted 13 lines
drivers/power/generic-battery.c new file +13
-0+ +return 0 ; + +err_gpio: +iio_channel_release_all (channels); +err_reg_backup: +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy); +err_reg_main: +return ret; +} + +static int generic_adc_bat_remove (struct platform_device *pdev)Quoted 8 lines
drivers/power/generic-battery.c line 321
Quoted 6 lines
drivers/power/generic-battery.c new file +6
-0+{ +int i; +struct generic_platform_data *pdata = pdev->dev.platform_data; + +for (i =0 ; i <ARRAY_SIZE (generic_bat.bat); i++) +power_supply_unregister (&generic_bat.bat[i].psy);You should release the IIO channels here when they are no longer needed.
right.
Quoted 68 lines
drivers/power/generic-battery.c line 322
Quoted 2 lines
drivers/power/generic-battery.c new file +2
-0+ +if (pdata->gpio_charge_finished >=0 ) {I forget, is a gpio index of 0 definitely invalid?
Quoted 65 lines
drivers/power/generic-battery.c new file +51
-0+free_irq (gpio_to_irq (pdata->gpio_charge_finished),NULL ); +gpio_free (pdata->gpio_charge_finished); + } + +cancel_delayed_work (&bat_work); + +return 0 ; +} + +#ifdef CONFIG_PM +static int generic_adc_bat_suspend (struct platform_device *pdev, +pm_message_t state) +{ +struct generic_platform_data *pdata = pdev->dev.platform_data; +struct generic_bat *bat =container_of (pdata, + struct generic_bat, pdata); + +cancel_delayed_work_sync (&bat_work); + bat->status =POWER_SUPPLY_STATUS_UNKNOWN ; + +return 0 ; +} + +static int generic_adc_bat_resume (struct platform_device *pdev) +{ +/* Schedule timer to check current status */ +schedule_delayed_work (&bat_work, +msecs_to_jiffies (JITTER_DELAY )); + +return 0 ; +} +#else +#define generic_adc_bat_suspend NULL +#define generic_adc_bat_resume NULL +#endif + +static struct platform_driver generic_adc_bat_driver = { + .driver = { + .name ="generic-adc-battery" , + }, + .probe = generic_adc_bat_probe, + .remove = generic_adc_bat_remove, + .suspend = generic_adc_bat_suspend, + .resume = generic_adc_bat_resume, +}; + +module_platform_driver (generic_adc_bat_driver); + +MODULE_AUTHOR ("anish kumar <[email protected]>" ); +MODULE_DESCRIPTION ("generic battery driver using IIO" ); +MODULE_LICENSE ("GPL" );include/linux/power/generic-battery.h new file +8
-0@@ -0,0 +1,26 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef GENERIC_BATTERY_H +#define GENERIC_BATTERY_H Quoted 8 lines
include/linux/power/generic-battery.h line 13
Quoted 5 lines
include/linux/power/generic-battery.h new file +5
-0+ +#include <linux/power_supply.h> + +/** +* struct generic_platform_data - platform data for generic battery A little inconsistent on capitalization. (might as well do it now or one of those people who delight in trivial patches will 'tidy' it up for you :)
Quoted 2 lines
I don't mind as it is a good start for people to get familiar with the linux patch process.
Can tell you don't get to deal with the tedious merge conflicts that result ;)
Quoted 18 lines
However I will address this valuable comments.
Quoted 15 lines
Quoted 14 lines
include/linux/power/generic-battery.h new file +13
-0+* @battery_info: Information about the battery +* @cal_charge: platform callback to calculate charge +* @gpio_charge_finished: GPIO number used for interrupts +* @gpio_inverted: Is the gpio inverted? +*/ +struct generic_platform_data { +struct power_supply_info battery_info; +int (*cal_charge)(long ); +int gpio_charge_finished; +int gpio_inverted; +}; + +#endif /* GENERIC_BATTERY_H */
Quoted 69 lines · anish kumar
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
drivers/power/generic-battery.c line 252
Quoted 1 line
drivers/power/generic-battery.c new file +1
-0+ channels =iio_channel_get_all (dev_name (&pdev->dev));I would give these explicit names and get the two individual channels by name. I think it will give you cleaner code.
Yes, now it will be based on
pdata->voltage_channel,pdata->current_channeland so on.We will use this to get the channel.Quoted 8 lines
Maybe better to just do this via the
iio_mapstructures in the platform data and standard naming for each channel."voltage", "current", "power" would do nicely. Note we'll have to add the relevant naming for the other side to more IIO device drivers via the
'datasheet_name' element ofiio_chan_spec. A quick grep shows this
is only done indrivers/staging/iio/max1363_core.cso far (bet you can't guess what is soldered to my test board :)Sorry I couldn't understand this but this is what I came up with.Hope it doesn't disappoint.
It has the advantage of getting extended easily.enum chan_type{
VOLTAGE,
CURRENT,
POWER,
MAX_TYPE
};struct channel_map { struct iio_map channel; enum chan_type type; };struct iio_battery_platform_data { struct power_supply_info battery_info; struct channel_map *map; int num_map; char battery_name[BAT_MAX_NAME]; int (*cal_charge)(long); int gpio_charge_finished; int gpio_inverted; int bat_poll_interval; int jitter_delay; };here goes the assignment:
for(i = 0; i < pdata->num_map; i++) { switch(pdata->map[i].type) case VOLTAGE: adc_bat->channel[VOLTAGE] = iio_channel_get( pdata->map[i].channel.consumer_dev_name, pdata->map[i].channel.adc_channel_label); adc_bat->chan_index = 1 << VOLTAGE; break; case CURRENT: adc_bat->channel[CURRENT] = iio_channel_get( pdata->map->channel->consumer_dev_name, pdata->map[i].channel.adc_channel_label); adc_bat->chan_index = 1 << CURRENT; break; case POWER: adc_bat->channel[POWER] = iio_channel_get( pdata->map[i].channel.consumer_dev_name, pdata->map[i].channel.adc_channel_label); adc_bat->chan_index = 1 << POWER; break; default: pr_info("add any other channels here in the case\n"); }
Quoted 2 lines
With the
chan_indexwe can know which property is set or every time we need to run through pdata->map[i].type to know the property set.
Hi,
I think what Jonathon meant is something like this.
adc_bat->channel[VOLTAGE] = iio_channel_get(dev_name(&pdev->dev), "voltage");
...
adc_bat->channel[CURRENT] = iio_channel_get(dev_name(&pdev->dev), "current");
...
adc_bat->channel[POWER] = iio_channel_get(dev_name(&pdev->dev), "power");
That's the beauty of the channel iio_map you can map an arbitrary channel of your ADC to a virtual identifier.
E.g. an example map could look like.
static struct iio_map map[] = {
{ "AIN1", "iio-battery.1", "voltage" },
};With this when the battery driver calls iio_channel_get(... "voltage") it will get the "AIN1" channel of the device.
- Lars
Jonathan Cameron
maintainerQuoted 86 lines · Lars-Peter Clausen
Quoted 69 lines · anish kumar
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
drivers/power/generic-battery.c line 252
Quoted 1 line
drivers/power/generic-battery.c new file +1
-0+ channels =iio_channel_get_all (dev_name (&pdev->dev));I would give these explicit names and get the two individual channels by name. I think it will give you cleaner code.
Yes, now it will be based on
pdata->voltage_channel,pdata->current_channeland so on.We will use this to get the channel.Quoted 8 lines
Maybe better to just do this via the
iio_mapstructures in the platform data and standard naming for each channel."voltage", "current", "power" would do nicely. Note we'll have to add the relevant naming for the other side to more IIO device drivers via the
'datasheet_name' element ofiio_chan_spec. A quick grep shows this
is only done indrivers/staging/iio/max1363_core.cso far (bet you can't guess what is soldered to my test board :)Sorry I couldn't understand this but this is what I came up with.Hope it doesn't disappoint.
It has the advantage of getting extended easily.enum chan_type{
VOLTAGE,
CURRENT,
POWER,
MAX_TYPE
};struct channel_map { struct iio_map channel; enum chan_type type; };struct iio_battery_platform_data { struct power_supply_info battery_info; struct channel_map *map; int num_map; char battery_name[BAT_MAX_NAME]; int (*cal_charge)(long); int gpio_charge_finished; int gpio_inverted; int bat_poll_interval; int jitter_delay; };here goes the assignment:
for(i = 0; i < pdata->num_map; i++) { switch(pdata->map[i].type) case VOLTAGE: adc_bat->channel[VOLTAGE] = iio_channel_get( pdata->map[i].channel.consumer_dev_name, pdata->map[i].channel.adc_channel_label); adc_bat->chan_index = 1 << VOLTAGE; break; case CURRENT: adc_bat->channel[CURRENT] = iio_channel_get( pdata->map->channel->consumer_dev_name, pdata->map[i].channel.adc_channel_label); adc_bat->chan_index = 1 << CURRENT; break; case POWER: adc_bat->channel[POWER] = iio_channel_get( pdata->map[i].channel.consumer_dev_name, pdata->map[i].channel.adc_channel_label); adc_bat->chan_index = 1 << POWER; break; default: pr_info("add any other channels here in the case\n"); }Quoted 2 lines
With the
chan_indexwe can know which property is set or every time we need to run through pdata->map[i].type to know the property set.Hi,
I think what Jonathon meant is something like this.
adc_bat->channel[VOLTAGE] =
iio_channel_get(dev_name(&pdev->dev), "voltage");
...
adc_bat->channel[CURRENT] =iio_channel_get(dev_name(&pdev->dev), "current");
...
adc_bat->channel[POWER] =iio_channel_get(dev_name(&pdev->dev), "power");That's the beauty of the channel
iio_mapyou can map an arbitrary channel of your ADC to a virtual identifier.
Quoted 9 lines
E.g. an example map could look like.
static struct iio_map map[] = { { "AIN1", "iio-battery.1", "voltage" }, };With this when the battery driver calls iio_channel_get(... "voltage") it will get the "AIN1" channel of the device.
Exactly what I meant, thanks Lars.
I'm not sure any consumers are actually using that interface at the moment as the main ones are hwmon and input (in my wip branch) and in both those cases the association doesn't matter.
Re:iio: Documentation change for inkern interface.
Hello Jonathan,
I didn't hear from you on this patch.Probably
this patch is lost in your box or somehow it
got missed.
Is it already applied?
a-n-i-s-h
Author: anish kumar <[email protected]> Date: Sun Sep 9 01:05:26 2012 +0530
iio: Documentation change for inkern interface.
This commit- 314be14bb renamed the _st_ functions to loose the bit that was meant for staging version but forgot to change the documentation which still have _st_ sprinkled in some of the places.
(quoted diff) +0 -0
b/drivers/staging/iio/Documentation/inkernel.txt
index a05823e..ab52840 100644
drivers/staging/iio/Documentation/inkernel.txt +0 -0
@@ -48,11 +48,11 @@ There are then a number of functions that can be
used to get information
about this channel such as it's current reading.
e.g.
-iio_st_read_channel_raw() - get a reading -iio_st_read_channel_type() - get the type of channel +iio_read_channel_raw() - get a reading +iio_get_channel_type() - get the type of channel
There is also provision for retrieving all of the channels associated with a given consumer. This is useful for generic drivers such as iio_hwmon where the number and naming of channels is not known by the
-consumer driver. To do this, use iio_st_channel_get_all. +consumer driver. To do this, use iio_channel_get_all.