From: Paul Fertser <fercerpav@gmail.com>
To: Anton Vorontsov <cbou@mail.ru>
Cc: David Woodhouse <dwmw2@infradead.org>,
linux-kernel@vger.kernel.org, Pavel Machek <pavel@ucw.cz>,
Balaji Rao <balajirrao@openmoko.org>,
Paul Fertser <fercerpav@gmail.com>
Subject: [PATCH 2/6] power: pcf50633: introduces battery charging current control
Date: Thu, 5 Nov 2009 00:24:55 +0300 [thread overview]
Message-ID: <1257369899-325-2-git-send-email-fercerpav@gmail.com> (raw)
In-Reply-To: <1257369899-325-1-git-send-email-fercerpav@gmail.com>
From: Balaji Rao <balajirrao@openmoko.org>
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.
Signed-off-by: Balaji Rao <balajirrao@openmoko.org>
Signed-off-by: Paul Fertser <fercerpav@gmail.com>
---
drivers/power/pcf50633-charger.c | 78 ++++++++++++++++++++++++++++++++++--
include/linux/mfd/pcf50633/core.h | 7 +++
2 files changed, 80 insertions(+), 5 deletions(-)
diff --git a/drivers/power/pcf50633-charger.c b/drivers/power/pcf50633-charger.c
index 48e92c0..21b6e64 100644
--- a/drivers/power/pcf50633-charger.c
+++ b/drivers/power/pcf50633-charger.c
@@ -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->charger_reference_current_ma) {
+ mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_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->charger_reference_current_ma)
+ return -ENODEV;
+
+ ma = (mbc->pcf->pdata->charger_reference_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->charger_reference_current_ma)
+ return -ENODEV;
+
+ ret = strict_strtoul(buf, 10, &ma);
+ if (ret)
+ return -EINVAL;
+
+ mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_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,
};
diff --git a/include/linux/mfd/pcf50633/core.h b/include/linux/mfd/pcf50633/core.h
index 9aba7b7..09af8fd 100644
--- a/include/linux/mfd/pcf50633/core.h
+++ b/include/linux/mfd/pcf50633/core.h
@@ -31,6 +31,13 @@ struct pcf50633_platform_data {
int charging_restart_interval;
+ /*
+ * Should be set accordingly to the reference resistor used, see
+ * I_{ch(ref)} charger reference current in the pcf50633 User
+ * Manual.
+ */
+ int charger_reference_current_ma;
+
/* Callbacks */
void (*probe_done)(struct pcf50633 *);
void (*mbc_event_callback)(struct pcf50633 *, int);
--
1.6.4.4
next prev parent reply other threads:[~2009-11-04 21:25 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-04 21:24 [PATCH 1/6] power: pcf50633: add ac power supply class to the charger Paul Fertser
2009-11-04 21:24 ` Paul Fertser [this message]
2009-11-04 21:24 ` [PATCH 3/6] gta02: set pcf50633 charger_reference_current_ma Paul Fertser
2009-11-04 21:24 ` [PATCH 4/6] power: pcf50633: get rid of charging restart software auto-triggering Paul Fertser
2009-11-04 21:24 ` [PATCH 5/6] power: pcf50633: properly reenable charging when the supply conditions change Paul Fertser
2009-11-04 21:24 ` [PATCH 6/6] power: pcf50633: query charger status directly Paul Fertser
2009-11-04 21:46 ` [PATCH 5/6] power: pcf50633: properly reenable charging when the supply conditions change Pavel Machek
2009-11-04 22:24 ` Paul Fertser
2009-11-04 23:16 ` Pavel Machek
2009-11-04 23:45 ` Anton Vorontsov
2009-11-04 23:56 ` Pavel Machek
2009-11-11 13:48 ` [PATCH 3/6] gta02: set pcf50633 charger_reference_current_ma Paul Fertser
2009-11-11 15:31 ` Anton Vorontsov
2009-11-12 7:40 ` Pavel Machek
2009-11-12 21:59 ` Russell King - ARM Linux
2009-11-13 18:26 ` Pavel Machek
2009-11-16 1:00 ` Anton Vorontsov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1257369899-325-2-git-send-email-fercerpav@gmail.com \
--to=fercerpav@gmail.com \
--cc=balajirrao@openmoko.org \
--cc=cbou@mail.ru \
--cc=dwmw2@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pavel@ucw.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox