power: pcf50633: add ac power supply class to the charger
Subsystems (3)
PATCH 1/1 power: pcf50633: add ac power supply class to the charger
From: Sean McNeil <[email protected]>
This adds an appropriate ac power_supply class and shows usb only when at the appropriate current limit.
--- drivers/power/pcf50633-charger.c | 46 +++++++++++++++++++++++++++++++++++++- 1 files changed, 45 insertions(+), 1 deletions(-)
drivers/power/pcf50633-charger.c +45 -1
@@ -36,6 +36,7 @@ struct pcf50633_mbc {struct power_supply usb;struct power_supply adapter; +struct power_supply ac;struct delayed_work charging_restart_work; };
@@ -237,6 +238,7 @@ pcf50633_mbc_irq_handler(int irq, void *data)else if (irq ==PCF50633_IRQ_USBLIMOFF ) mbc->usb_active =1 ; +power_supply_changed (&mbc->ac);power_supply_changed (&mbc->usb);power_supply_changed (&mbc->adapter);
@@ -269,10 +271,34 @@ static int usb_get_property(struct power_supply *psy, { struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, usb);int ret =0 ; +u8 usblim =pcf50633_reg_read (mbc->pcf,PCF50633_REG_MBCC7 ) & +PCF50633_MBCC7_USB_MASK ; + +switch (psp) { +case POWER_SUPPLY_PROP_ONLINE : + val->intval = mbc->usb_online && + (usblim <= PCF50633_MBCC7_USB_500mA); +break ; +default : + ret = -EINVAL ; +break ; + } +return ret; +} + +static int ac_get_property (struct power_supply *psy, +enum power_supply_property psp, +union power_supply_propval *val) +{ + struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, ac); +int ret =0 ; +u8 usblim =pcf50633_reg_read (mbc->pcf,PCF50633_REG_MBCC7 ) & +PCF50633_MBCC7_USB_MASK ;switch (psp) {case POWER_SUPPLY_PROP_ONLINE : - val->intval = mbc->usb_online; + val->intval = mbc->usb_online && + (usblim == PCF50633_MBCC7_USB_1000mA);break ;default : ret = -EINVAL ;
@@ -337,6 +363,14 @@ static int __devinit pcf50633_mbc_probe(struct platform_device *pdev) mbc->usb.supplied_to = mbc->pcf->pdata->batteries; mbc->usb.num_supplicants = mbc->pcf->pdata->num_batteries; + mbc->ac.name ="ac" ; + mbc->ac.type =POWER_SUPPLY_TYPE_MAINS ; + mbc->ac.properties = power_props; + mbc->ac.num_properties =ARRAY_SIZE (power_props); + mbc->ac.get_property = ac_get_property; + mbc->ac.supplied_to = mbc->pcf->pdata->batteries; + mbc->ac.num_supplicants = mbc->pcf->pdata->num_batteries; + ret =power_supply_register (&pdev->dev, &mbc->adapter);if (ret) {dev_err (mbc->pcf->dev,"failed to register adapter\n" );
@@ -352,6 +386,15 @@ static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)return ret; } + ret =power_supply_register (&pdev->dev, &mbc->ac); +if (ret) { +dev_err (mbc->pcf->dev,"failed to register ac\n" ); +power_supply_unregister (&mbc->adapter); +power_supply_unregister (&mbc->usb); +kfree (mbc); +return ret; + } +INIT_DELAYED_WORK (&mbc->charging_restart_work, pcf50633_mbc_charging_restart);
@@ -379,6 +422,7 @@ static int __devexit pcf50633_mbc_remove(struct platform_device *pdev)power_supply_unregister (&mbc->usb);power_supply_unregister (&mbc->adapter); +power_supply_unregister (&mbc->ac);cancel_delayed_work_sync (&mbc->charging_restart_work);
Signature · 1 line
1.6.0.6
PATCH 1/1 power: pcf50633: introduces battery charging current control
From: Balaji Rao <[email protected]>
--- drivers/power/pcf50633-charger.c | 73 ++++++++++++++++++++++++++++++++++--- include/linux/mfd/pcf50633/core.h | 2 + 2 files changed, 70 insertions(+), 5 deletions(-)
drivers/power/pcf50633-charger.c +68 -5
@@ -48,16 +48,21 @@ int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)u8 bits;int charging_start =1 ;u8 mbcs2, chgmod; +unsigned int mbcc5; -if (ma >=1000 ) +if (ma >=1000 ) { bits = PCF50633_MBCC7_USB_1000mA; -else if (ma >=500 ) + ma =1000 ; + }else if (ma >=500 ) { bits = PCF50633_MBCC7_USB_500mA; -else if (ma >=100 ) + ma =500 ; + }else if (ma >=100 ) { bits = PCF50633_MBCC7_USB_100mA; -else { + ma =100 ; + }else { bits =PCF50633_MBCC7_USB_SUSPEND ; charging_start =0 ; + ma =0 ; } ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7,
@@ -67,7 +72,24 @@ int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)else dev_info (pcf->dev,"usb curlim to %d mA\n" , ma); -/* Manual charging start */ +/* +* We limit the charging current to be the USB current limit. +* The reason is that on pcf50633, when it enters PMU Standby mode, +* which it does when the device goes "off", the USB current limit +* reverts to the variant default. In at least one common case, that +* default is 500mA. By setting the charging current to be the same +* as the USB limit we set here before PMU standby, we enforce it only +* using the correct amount of current even when the USB current limit +* gets reset to the wrong thing +*/ + +if (mbc->pcf->pdata->chg_ref_current_ma) { + mbcc5 = (ma <<8 ) / mbc->pcf->pdata->chg_ref_current_ma; +if (mbcc5 >255 ) + mbcc5 =255 ; +pcf50633_reg_write (mbc->pcf,PCF50633_REG_MBCC5 , mbcc5); + } + mbcs2 =pcf50633_reg_read (pcf,PCF50633_REG_MBCS2 ); chgmod = (mbcs2 &PCF50633_MBCS2_MBC_MASK );
@@ -157,9 +179,50 @@ static ssize_t set_usblim(struct device *dev, static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim); +static ssize_t +show_chglim (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct pcf50633_mbc *mbc =dev_get_drvdata (dev); +u8 mbcc5 =pcf50633_reg_read (mbc->pcf,PCF50633_REG_MBCC5 ); +unsigned int ma; + +if (!mbc->pcf->pdata->chg_ref_current_ma) +return -ENODEV ; + + ma = (mbc->pcf->pdata->chg_ref_current_ma * mbcc5) >>8 ; + +return sprintf (buf,"%u\n" , ma); +} + +static ssize_t set_chglim (struct device *dev, +struct device_attribute *attr,const char *buf,size_t count) +{ +struct pcf50633_mbc *mbc =dev_get_drvdata (dev); +unsigned long ma; +unsigned int mbcc5; +int ret; + +if (!mbc->pcf->pdata->chg_ref_current_ma) +return -ENODEV ; + + ret =strict_strtoul (buf,10 , &ma); +if (ret) +return -EINVAL ; + + mbcc5 = (ma <<8 ) / mbc->pcf->pdata->chg_ref_current_ma; +if (mbcc5 >255 ) + mbcc5 =255 ; +pcf50633_reg_write (mbc->pcf,PCF50633_REG_MBCC5 , mbcc5); + +return count; +} + +static DEVICE_ATTR(chg_curlim, S_IRUGO | S_IWUSR, show_chglim, set_chglim); +static struct attribute *pcf50633_mbc_sysfs_entries[] = { &dev_attr_chgmode.attr, &dev_attr_usb_curlim.attr, + &dev_attr_chg_curlim.attr,NULL , };
include/linux/mfd/pcf50633/core.h +2 -0
@@ -31,6 +31,8 @@ struct pcf50633_platform_data {int charging_restart_interval; +int chg_ref_current_ma; +/* Callbacks */ void (*probe_done)(struct pcf50633 *);void (*mbc_event_callback)(struct pcf50633 *,int );
Signature · 1 line
1.6.0.6
PATCH 1/1 gta02: set pcf50633 chg_ref_current_ma
From: Lars-Peter Clausen <[email protected]>
This value is board-specific and is needed for calculations to set charging current limit properly.
---
Since this patch compile-depends on previous probably it should go through the same tree as others. Nelson (gta02 maintainer) agrees with whatever you think is best.
arch/arm/mach-s3c2442/mach-gta02.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
arch/arm/mach-s3c2442/mach-gta02.c +3 -0
@@ -268,6 +268,9 @@ struct pcf50633_platform_data gta02_pcf_pdata = { .batteries = gta02_batteries, .num_batteries = ARRAY_SIZE(gta02_batteries), + + .chg_ref_current_ma = 1000, + .reg_init_data = { [PCF50633_REGULATOR_AUTO] = { .constraints = {
Signature · 1 line
1.6.0.6
PATCH 1/1 power: pcf50633: get rid of charging restart software auto-triggering
After reaching Battery Full condition MBC state machine switches back into charging mode when the battery voltage falls below 96% of a battery float voltage. The voltage drop in Li-Ion batteries is marginal (1-2%) till about 80% of its capacity - which means, after a BATFULL, charging won't be restarted until 75-80%.
That is a desired behaviour recommended by battery manufacturers, don't mess with it.
--- drivers/power/pcf50633-charger.c | 46 ------------------------------------- include/linux/mfd/pcf50633/core.h | 2 - 2 files changed, 0 insertions(+), 48 deletions(-)
drivers/power/pcf50633-charger.c +0 -46
@@ -37,8 +37,6 @@ struct pcf50633_mbc {struct power_supply usb;struct power_supply adapter;struct power_supply ac; - -struct delayed_work charging_restart_work; };int pcf50633_mbc_usb_curlim_set (struct pcf50633 *pcf,int ma)
@@ -231,44 +229,10 @@ static struct attribute_group mbc_attr_group = { .attrs = pcf50633_mbc_sysfs_entries, }; -/* MBC state machine switches into charging mode when the battery voltage -* falls below 96% of a battery float voltage. But the voltage drop in Li-ion -* batteries is marginal(1~2 %) till about 80% of its capacity - which means, -* after a BATFULL, charging won't be restarted until 80%. -* -* This work_struct function restarts charging at regular intervals to make -* sure we don't discharge too much -*/ - -static void pcf50633_mbc_charging_restart (struct work_struct *work) -{ -struct pcf50633_mbc *mbc; -u8 mbcs2, chgmod; - - mbc = container_of(work, struct pcf50633_mbc, - charging_restart_work.work); - - mbcs2 =pcf50633_reg_read (mbc->pcf,PCF50633_REG_MBCS2 ); - chgmod = (mbcs2 &PCF50633_MBCS2_MBC_MASK ); - -if (chgmod !=PCF50633_MBCS2_MBC_BAT_FULL ) -return ; - -/* Restart charging */ -pcf50633_reg_set_bit_mask (mbc->pcf,PCF50633_REG_MBCC1 , -PCF50633_MBCC1_RESUME ,PCF50633_MBCC1_RESUME ); - mbc->usb_active =1 ; -power_supply_changed (&mbc->usb); - -dev_info (mbc->pcf->dev,"Charging restarted\n" ); -} -static void pcf50633_mbc_irq_handler (int irq,void *data) {struct pcf50633_mbc *mbc = data; -int chg_restart_interval = - mbc->pcf->pdata->charging_restart_interval;/* USB */ if (irq ==PCF50633_IRQ_USBINS ) {
@@ -277,7 +241,6 @@ pcf50633_mbc_irq_handler(int irq, void *data) mbc->usb_online =0 ; mbc->usb_active =0 ;pcf50633_mbc_usb_curlim_set (mbc->pcf,0 ); -cancel_delayed_work_sync (&mbc->charging_restart_work); }/* Adapter */
@@ -292,10 +255,6 @@ pcf50633_mbc_irq_handler(int irq, void *data)if (irq ==PCF50633_IRQ_BATFULL ) { mbc->usb_active =0 ; mbc->adapter_active =0 ; - -if (chg_restart_interval >0 ) -schedule_delayed_work (&mbc->charging_restart_work, - chg_restart_interval); }else if (irq ==PCF50633_IRQ_USBLIMON ) mbc->usb_active =0 ;else if (irq ==PCF50633_IRQ_USBLIMOFF )
@@ -458,9 +417,6 @@ static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)return ret; } -INIT_DELAYED_WORK (&mbc->charging_restart_work, - pcf50633_mbc_charging_restart); - ret =sysfs_create_group (&pdev->dev.kobj, &mbc_attr_group);if (ret)dev_err (mbc->pcf->dev,"failed to create sysfs entries\n" );
@@ -487,8 +443,6 @@ static int __devexit pcf50633_mbc_remove(struct platform_device *pdev)power_supply_unregister (&mbc->adapter);power_supply_unregister (&mbc->ac); -cancel_delayed_work_sync (&mbc->charging_restart_work); -kfree (mbc);return 0 ;
include/linux/mfd/pcf50633/core.h +0 -2
@@ -29,8 +29,6 @@ struct pcf50633_platform_data {char **batteries;int num_batteries; -int charging_restart_interval; -int chg_ref_current_ma;/* Callbacks */
Signature · 1 line
1.6.0.6
PATCH 1/1 power: pcf50633: properly reenable charging when the supply conditions change
If chgmod == BATFULL, setting chgena has no effect. Datasheet says we need to set resume instead but when autoresume is used resume doesn't work. Clear and set chgena instead.
This enables a user to force charging by re-plugging USB even when the charger entered Battery Full mode, might be handy before a long trip.
--- drivers/power/pcf50633-charger.c | 10 +++++++--- 1 files changed, 7 insertions(+), 3 deletions(-)
drivers/power/pcf50633-charger.c +7 -3
@@ -92,14 +92,18 @@ int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma) chgmod = (mbcs2 &PCF50633_MBCS2_MBC_MASK );/* If chgmod == BATFULL, setting chgena has no effect. -* We need to set resume instead. +* Datasheet says we need to set resume instead but when autoresume is +* used resume doesn't work. Clear and set chgena instead. */ if (chgmod !=PCF50633_MBCS2_MBC_BAT_FULL )pcf50633_reg_set_bit_mask (pcf,PCF50633_REG_MBCC1 ,PCF50633_MBCC1_CHGENA ,PCF50633_MBCC1_CHGENA ); -else +else { +pcf50633_reg_clear_bits (pcf,PCF50633_REG_MBCC1 , +PCF50633_MBCC1_CHGENA );pcf50633_reg_set_bit_mask (pcf,PCF50633_REG_MBCC1 , -PCF50633_MBCC1_RESUME ,PCF50633_MBCC1_RESUME ); +PCF50633_MBCC1_CHGENA ,PCF50633_MBCC1_CHGENA ); + } mbc->usb_active = charging_start;
Signature · 1 line
1.6.0.6
PATCH 1/1 power: pcf50633: query charger status directly
Current scheme is fragile and is likely to go off sync, especially on batfull->adapter charging automatic MBC transition.
Query the status bit every time we need it instead.
We need to export another function to query for USB presence because we can't read anything from PCF50633 (via I2C) inside irq context and that is needed by usb gadgets.
--- drivers/power/pcf50633-charger.c | 50 ++++++++++++++++++++++---------------- include/linux/mfd/pcf50633/mbc.h | 1 + 2 files changed, 30 insertions(+), 21 deletions(-)
drivers/power/pcf50633-charger.c +29 -21
@@ -29,9 +29,7 @@struct pcf50633_mbc {struct pcf50633 *pcf; -int adapter_active;int adapter_online; -int usb_active;int usb_online;struct power_supply usb;
@@ -88,7 +86,7 @@ int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)pcf50633_reg_write (mbc->pcf,PCF50633_REG_MBCC5 , mbcc5); } - mbcs2 =pcf50633_reg_read (pcf,PCF50633_REG_MBCS2 ); + mbcs2 =pcf50633_reg_read (mbc->pcf,PCF50633_REG_MBCS2 ); chgmod = (mbcs2 &PCF50633_MBCS2_MBC_MASK );/* If chgmod == BATFULL, setting chgena has no effect.
@@ -105,8 +103,6 @@ int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma) PCF50633_MBCC1_CHGENA, PCF50633_MBCC1_CHGENA); } - mbc->usb_active = charging_start; -power_supply_changed (&mbc->usb);return ret;
@@ -117,20 +113,44 @@ int pcf50633_mbc_get_status(struct pcf50633 *pcf) {struct pcf50633_mbc *mbc =platform_get_drvdata (pcf->mbc_pdev);int status =0 ; +u8 chgmod; + +if (!mbc) +return 0 ; + + chgmod =pcf50633_reg_read (mbc->pcf,PCF50633_REG_MBCS2 ) + &PCF50633_MBCS2_MBC_MASK ;if (mbc->usb_online) status |=PCF50633_MBC_USB_ONLINE ; -if (mbc->usb_active) +if (chgmod ==PCF50633_MBCS2_MBC_USB_PRE || + chgmod ==PCF50633_MBCS2_MBC_USB_PRE_WAIT || + chgmod ==PCF50633_MBCS2_MBC_USB_FAST || + chgmod ==PCF50633_MBCS2_MBC_USB_FAST_WAIT ) status |=PCF50633_MBC_USB_ACTIVE ;if (mbc->adapter_online) status |=PCF50633_MBC_ADAPTER_ONLINE ; -if (mbc->adapter_active) +if (chgmod ==PCF50633_MBCS2_MBC_ADP_PRE || + chgmod ==PCF50633_MBCS2_MBC_ADP_PRE_WAIT || + chgmod ==PCF50633_MBCS2_MBC_ADP_FAST || + chgmod ==PCF50633_MBCS2_MBC_ADP_FAST_WAIT ) status |=PCF50633_MBC_ADAPTER_ACTIVE ;return status; }EXPORT_SYMBOL_GPL (pcf50633_mbc_get_status); +int pcf50633_mbc_get_usb_online_status (struct pcf50633 *pcf) +{ +struct pcf50633_mbc *mbc =platform_get_drvdata (pcf->mbc_pdev); + +if (!mbc) +return 0 ; + +return mbc->usb_online; +} +EXPORT_SYMBOL_GPL (pcf50633_mbc_get_usb_online_status); +static ssize_t show_chgmode (struct device *dev,struct device_attribute *attr,char *buf) {
@@ -243,26 +263,14 @@ pcf50633_mbc_irq_handler(int irq, void *data) mbc->usb_online =1 ; }else if (irq ==PCF50633_IRQ_USBREM ) { mbc->usb_online =0 ; - mbc->usb_active =0 ;pcf50633_mbc_usb_curlim_set (mbc->pcf,0 ); }/* Adapter */ -if (irq ==PCF50633_IRQ_ADPINS ) { +if (irq ==PCF50633_IRQ_ADPINS ) mbc->adapter_online =1 ; - mbc->adapter_active =1 ; - }else if (irq ==PCF50633_IRQ_ADPREM ) { +else if (irq ==PCF50633_IRQ_ADPREM ) mbc->adapter_online =0 ; - mbc->adapter_active =0 ; - } - -if (irq ==PCF50633_IRQ_BATFULL ) { - mbc->usb_active =0 ; - mbc->adapter_active =0 ; - }else if (irq ==PCF50633_IRQ_USBLIMON ) - mbc->usb_active =0 ; -else if (irq ==PCF50633_IRQ_USBLIMOFF ) - mbc->usb_active =1 ;power_supply_changed (&mbc->ac);power_supply_changed (&mbc->usb);
include/linux/mfd/pcf50633/mbc.h +1 -0
@@ -128,6 +128,7 @@ enum pcf50633_reg_mbcs3 {int pcf50633_mbc_usb_curlim_set (struct pcf50633 *pcf,int ma);int pcf50633_mbc_get_status (struct pcf50633 *); +int pcf50633_mbc_get_usb_online_status (struct pcf50633 *);#endif
Signature · 1 line
1.6.0.6
Re: [PATCH] gta02: set pcf50633 chg_ref_current_ma
In reply to Paul FertserQuoted 5 lines · Paul Fertser
From: Lars-Peter Clausen <[email protected]>
This value is board-specific and is needed for calculations to set charging current limit properly.
Quoted 8 lines
Signed-off-by Lars-Peter Clausen <[email protected]>Acked-by Nelson Castillo <[email protected]>Signed-off-by Paul Fertser <[email protected]>---
Since this patch compile-depends on previous probably it should go through the same tree as others. Nelson (gta02 maintainer) agrees with whatever you think is best.
I would happy to apply this patch via battery tree, along with other your patches.
Just to make things clear: we no longer need RMK's Ack for mach-specific patches that don't touch core code? Or maybe Ben's Ack is still required?
Want to be on a safe side before applying.
Thanks!
Quoted 19 lines
arch/arm/mach-s3c2442/mach-gta02.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)arch/arm/mach-s3c2442/mach-gta02.c +3
-0@@ -268,6 +268,9 @@ struct pcf50633_platform_data gta02_pcf_pdata = { .batteries = gta02_batteries, .num_batteries = ARRAY_SIZE(gta02_batteries), + + .chg_ref_current_ma = 1000, + .reg_init_data = { [PCF50633_REGULATOR_AUTO] = { .constraints = {Signature · 1 line
1.6.0.6
Signature · 3 lines
Anton Vorontsov email: cbouatmailru at gmail.com irc://irc.freenode.net/bd2
Re: [PATCH] power: pcf50633: introduces battery charging current control
In reply to Paul FertserHi!
Quoted 1 line
From: Balaji Rao <[email protected]>
Could we get some changelog here? Like 'it adds new file to sysfs interface, ...'
drivers/power/pcf50633-charger.c static ssize_t set_usblim(struct device *dev, · line 183
Quoted 2 lines
drivers/power/pcf50633-charger.c +2
-0+static ssize_t +show_chglim (struct device *dev,struct device_attribute *attr,char *buf)
Could we get meaningful function names?
include/linux/mfd/pcf50633/core.h struct pcf50633_platform_data { · line 35
Quoted 6 lines
include/linux/mfd/pcf50633/core.h +2
-0@@ -31,6 +31,8 @@ struct pcf50633_platform_data {int charging_restart_interval; +int chg_ref_current_ma; +
...and variable names? This is also inconsistent with name above.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH] power: pcf50633: introduces battery charging current control
Hi,
Pavel, thanks for you review.
Quoted 4 lines · Pavel Machek
Quoted 1 line
From: Balaji Rao <[email protected]>
Could we get some changelog here? Like 'it adds new file to sysfs interface, ...'
Done.
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+static ssize_t +show_chglim(struct device *dev, struct device_attribute *attr, char *buf)Could we get meaningful function names?
Well, this function name is derived from the sysfs attribute name and since we already have "usb_curlim" sysfs attribute and corresponding functions show_usblim() and set_usblim() it might be beneficial to keep the driver code consistent. I now documented the attribute behaviour in the code in the hope that users will look it up if in doubt.
Quoted 8 lines
Review struct pcf50633_platform_data { · line 35
Quoted 6 lines
(quoted diff) +2
-0@@ -31,6 +31,8 @@ struct pcf50633_platform_data { int charging_restart_interval; + int chg_ref_current_ma; +...and variable names? This is also inconsistent with name above.
I added a comment explaining what this value is supposed to be set to. The name is borrowed directly from the datasheet where it is called "charger reference current". I hope this is ok.
Please find the new patch attached.
NB: Anton, if you apply this modified version, a new comment in include/linux/mfd/pcf50633/core.h will produce a trivial merge conflict with the later patch ("get rid of charging restart software auto-triggering"). I can resend the whole series for your convenience after everything else is sorted out if you prefer.
Signature · 2 lines
Be free, use free (http://www.gnu.org/philosophy/free-sw.html) software! mailto:[email protected]
Quoted 1 line
From
06206380d4d200371d5da118a6b5ded0105c808eMon Sep 17 00:00:00 2001
From: Balaji Rao <[email protected]>
Date: Thu, 29 Jan 2009 15:00:37 +0000
Subject: [PATCH v2] power: pcf50633: introduces battery charging current control
Implement a new sysfs attribute to allow changing MBC charging limit on the fly independently of usb current limit. It also gets set automatically every time usb current limit is changed.
Limiting charging current also prevents violating USB specification in the case when the whole device is shut down and usb current limit is reset to the factory default by the pcf50633 state transition.
--- drivers/power/pcf50633-charger.c | 78 ++++++++++++++++++++++++++++++++++-- include/linux/mfd/pcf50633/core.h | 7 +++ 2 files changed, 80 insertions(+), 5 deletions(-)
drivers/power/pcf50633-charger.c +73 -5
@@ -48,16 +48,21 @@ int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)u8 bits;int charging_start =1 ;u8 mbcs2, chgmod; +unsigned int mbcc5; -if (ma >=1000 ) +if (ma >=1000 ) { bits = PCF50633_MBCC7_USB_1000mA; -else if (ma >=500 ) + ma =1000 ; + }else if (ma >=500 ) { bits = PCF50633_MBCC7_USB_500mA; -else if (ma >=100 ) + ma =500 ; + }else if (ma >=100 ) { bits = PCF50633_MBCC7_USB_100mA; -else { + ma =100 ; + }else { bits =PCF50633_MBCC7_USB_SUSPEND ; charging_start =0 ; + ma =0 ; } ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7,
@@ -67,7 +72,24 @@ int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)else dev_info (pcf->dev,"usb curlim to %d mA\n" , ma); -/* Manual charging start */ +/* +* We limit the charging current to be the USB current limit. +* The reason is that on pcf50633, when it enters PMU Standby mode, +* which it does when the device goes "off", the USB current limit +* reverts to the variant default. In at least one common case, that +* default is 500mA. By setting the charging current to be the same +* as the USB limit we set here before PMU standby, we enforce it only +* using the correct amount of current even when the USB current limit +* gets reset to the wrong thing +*/ + +if (mbc->pcf->pdata->chg_ref_current_ma) { + mbcc5 = (ma <<8 ) / mbc->pcf->pdata->chg_ref_current_ma; +if (mbcc5 >255 ) + mbcc5 =255 ; +pcf50633_reg_write (mbc->pcf,PCF50633_REG_MBCC5 , mbcc5); + } + mbcs2 =pcf50633_reg_read (pcf,PCF50633_REG_MBCS2 ); chgmod = (mbcs2 &PCF50633_MBCS2_MBC_MASK );
@@ -157,9 +179,55 @@ static ssize_t set_usblim(struct device *dev, static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim); +static ssize_t +show_chglim (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct pcf50633_mbc *mbc =dev_get_drvdata (dev); +u8 mbcc5 =pcf50633_reg_read (mbc->pcf,PCF50633_REG_MBCC5 ); +unsigned int ma; + +if (!mbc->pcf->pdata->chg_ref_current_ma) +return -ENODEV ; + + ma = (mbc->pcf->pdata->chg_ref_current_ma * mbcc5) >>8 ; + +return sprintf (buf,"%u\n" , ma); +} + +static ssize_t set_chglim (struct device *dev, +struct device_attribute *attr,const char *buf,size_t count) +{ +struct pcf50633_mbc *mbc =dev_get_drvdata (dev); +unsigned long ma; +unsigned int mbcc5; +int ret; + +if (!mbc->pcf->pdata->chg_ref_current_ma) +return -ENODEV ; + + ret =strict_strtoul (buf,10 , &ma); +if (ret) +return -EINVAL ; + + mbcc5 = (ma <<8 ) / mbc->pcf->pdata->chg_ref_current_ma; +if (mbcc5 >255 ) + mbcc5 =255 ; +pcf50633_reg_write (mbc->pcf,PCF50633_REG_MBCC5 , mbcc5); + +return count; +} + +/* +* This attribute allows to change MBC charging limit on the fly +* independently of usb current limit. It also gets set automatically every +* time usb current limit is changed. +*/ +static DEVICE_ATTR(chg_curlim, S_IRUGO | S_IWUSR, show_chglim, set_chglim); +static struct attribute *pcf50633_mbc_sysfs_entries[] = { &dev_attr_chgmode.attr, &dev_attr_usb_curlim.attr, + &dev_attr_chg_curlim.attr,NULL , };
include/linux/mfd/pcf50633/core.h +7 -0
@@ -31,6 +31,13 @@ struct pcf50633_platform_data {int charging_restart_interval; +/* +* Should be set according to the reference resistor used, see +* I_{ch(ref)} charger reference current in the pcf50633 User +* Manual. +*/ +int chg_ref_current_ma; +/* Callbacks */ void (*probe_done)(struct pcf50633 *);void (*mbc_event_callback)(struct pcf50633 *,int );
Signature · 1 line
1.6.0.6
Re: [PATCH] power: pcf50633: introduces battery charging current control
Hi!
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
include/linux/mfd/pcf50633/core.h +0
-0@@ -31,6 +31,8 @@ struct pcf50633_platform_data {int charging_restart_interval;
Quoted 8 lines
Quoted 4 lines
include/linux/mfd/pcf50633/core.h struct pcf50633_platform_data { · line 40
Quoted 2 lines
include/linux/mfd/pcf50633/core.h +2
-0+int chg_ref_current_ma; +...and variable names? This is also inconsistent with name above.
I added a comment explaining what this value is supposed to be set to. The name is borrowed directly from the datasheet where it is called "charger reference current". I hope this is ok.
Can you call it "charging_reference_current_ma_" (or maybe charging_reference_ma, or something), then? You chave "charging_restart_interval" just above. Having both charging_ and chg_ prefixes is strange, and chg_ really does not hint at charging (I'd read it "change").
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH] power: pcf50633: introduces battery charging current control
In reply to Paul FertserQuoted 129 lines · Paul Fertser
From: Balaji Rao <[email protected]>
Signed-off-by Balaji Rao <[email protected]>Signed-off-by Paul Fertser <[email protected]>--- drivers/power/pcf50633-charger.c | 73 ++++++++++++++++++++++++++++++++++--- include/linux/mfd/pcf50633/core.h | 2 + 2 files changed, 70 insertions(+), 5 deletions(-)drivers/power/pcf50633-charger.c +68
-5@@ -48,16 +48,21 @@ int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)u8 bits;int charging_start =1 ;u8 mbcs2, chgmod; +unsigned int mbcc5; -if (ma >=1000 ) +if (ma >=1000 ) { bits = PCF50633_MBCC7_USB_1000mA; -else if (ma >=500 ) + ma =1000 ; + }else if (ma >=500 ) { bits = PCF50633_MBCC7_USB_500mA; -else if (ma >=100 ) + ma =500 ; + }else if (ma >=100 ) { bits = PCF50633_MBCC7_USB_100mA; -else { + ma =100 ; + }else { bits =PCF50633_MBCC7_USB_SUSPEND ; charging_start =0 ; + ma =0 ; } ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7,@@ -67,7 +72,24 @@ int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)else dev_info (pcf->dev,"usb curlim to %d mA\n" , ma); -/* Manual charging start */ +/* +* We limit the charging current to be the USB current limit. +* The reason is that on pcf50633, when it enters PMU Standby mode, +* which it does when the device goes "off", the USB current limit +* reverts to the variant default. In at least one common case, that +* default is 500mA. By setting the charging current to be the same +* as the USB limit we set here before PMU standby, we enforce it only +* using the correct amount of current even when the USB current limit +* gets reset to the wrong thing +*/ + +if (mbc->pcf->pdata->chg_ref_current_ma) { + mbcc5 = (ma <<8 ) / mbc->pcf->pdata->chg_ref_current_ma; +if (mbcc5 >255 ) + mbcc5 =255 ; +pcf50633_reg_write (mbc->pcf,PCF50633_REG_MBCC5 , mbcc5); + } + mbcs2 =pcf50633_reg_read (pcf,PCF50633_REG_MBCS2 ); chgmod = (mbcs2 &PCF50633_MBCS2_MBC_MASK );@@ -157,9 +179,50 @@ static ssize_t set_usblim(struct device *dev, static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim); +static ssize_t +show_chglim (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct pcf50633_mbc *mbc =dev_get_drvdata (dev); +u8 mbcc5 =pcf50633_reg_read (mbc->pcf,PCF50633_REG_MBCC5 ); +unsigned int ma; + +if (!mbc->pcf->pdata->chg_ref_current_ma) +return -ENODEV ; + + ma = (mbc->pcf->pdata->chg_ref_current_ma * mbcc5) >>8 ; + +return sprintf (buf,"%u\n" , ma); +} + +static ssize_t set_chglim (struct device *dev, +struct device_attribute *attr,const char *buf,size_t count) +{ +struct pcf50633_mbc *mbc =dev_get_drvdata (dev); +unsigned long ma; +unsigned int mbcc5; +int ret; + +if (!mbc->pcf->pdata->chg_ref_current_ma) +return -ENODEV ; + + ret =strict_strtoul (buf,10 , &ma); +if (ret) +return -EINVAL ; + + mbcc5 = (ma <<8 ) / mbc->pcf->pdata->chg_ref_current_ma; +if (mbcc5 >255 ) + mbcc5 =255 ; +pcf50633_reg_write (mbc->pcf,PCF50633_REG_MBCC5 , mbcc5); + +return count; +} + +static DEVICE_ATTR(chg_curlim, S_IRUGO | S_IWUSR, show_chglim, set_chglim); +static struct attribute *pcf50633_mbc_sysfs_entries[] = { &dev_attr_chgmode.attr, &dev_attr_usb_curlim.attr, + &dev_attr_chg_curlim.attr,NULL , };include/linux/mfd/pcf50633/core.h +2
-0@@ -31,6 +31,8 @@ struct pcf50633_platform_data {int charging_restart_interval; +int chg_ref_current_ma; +/* Callbacks */ void (*probe_done)(struct pcf50633 *);void (*mbc_event_callback)(struct pcf50633 *,int );
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html