From fc2bc0c29a4e32da13ab75101edde030a6362d10 Mon Sep 17 00:00:00 2001 From: Julia Lawall Date: Sun, 19 Aug 2012 10:44:19 +0200 Subject: ab8500_charger: Fix error return code Initialize return variable before exiting on an error path. A simplified version of the semantic match that finds this problem is as follows: (http://coccinelle.lip6.fr/) // ( if@p1 (\(ret < 0\|ret != 0\)) { ... return ret; } | ret@p1 = 0 ) ... when != ret = e1 when != &ret *if(...) { ... when != ret = e2 when forall return ret; } // Signed-off-by: Julia Lawall Signed-off-by: Anton Vorontsov --- drivers/power/ab8500_charger.c | 1 + 1 file changed, 1 insertion(+) (limited to 'drivers') diff --git a/drivers/power/ab8500_charger.c b/drivers/power/ab8500_charger.c index d4f0c98428cb..c2ef94702e49 100644 --- a/drivers/power/ab8500_charger.c +++ b/drivers/power/ab8500_charger.c @@ -2614,6 +2614,7 @@ static int __devinit ab8500_charger_probe(struct platform_device *pdev) create_singlethread_workqueue("ab8500_charger_wq"); if (di->charger_wq == NULL) { dev_err(di->dev, "failed to create work queue\n"); + ret = -ENOMEM; goto free_device_info; } -- cgit v1.2.3 From 588d4f0b05c9bb09c2474460be78f41813b2d861 Mon Sep 17 00:00:00 2001 From: Julia Lawall Date: Sun, 19 Aug 2012 10:44:18 +0200 Subject: wm97xx_battery: Fix error return code Initialize return variable before exiting on an error path. A simplified version of the semantic match that finds this problem is as follows: (http://coccinelle.lip6.fr/) // ( if@p1 (\(ret < 0\|ret != 0\)) { ... return ret; } | ret@p1 = 0 ) ... when != ret = e1 when != &ret *if(...) { ... when != ret = e2 when forall return ret; } // Signed-off-by: Julia Lawall Signed-off-by: Anton Vorontsov --- drivers/power/wm97xx_battery.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/power/wm97xx_battery.c b/drivers/power/wm97xx_battery.c index d2d4c08c681c..0ba331a06dcc 100644 --- a/drivers/power/wm97xx_battery.c +++ b/drivers/power/wm97xx_battery.c @@ -212,8 +212,10 @@ static int __devinit wm97xx_bat_probe(struct platform_device *dev) props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */ prop = kzalloc(props * sizeof(*prop), GFP_KERNEL); - if (!prop) + if (!prop) { + ret = -ENOMEM; goto err3; + } prop[i++] = POWER_SUPPLY_PROP_PRESENT; if (pdata->charge_gpio >= 0) -- cgit v1.2.3 From 05cdbdb95f98fcd080e7494f7d0ca4886aed3c64 Mon Sep 17 00:00:00 2001 From: Julia Lawall Date: Sun, 19 Aug 2012 10:44:22 +0200 Subject: ab8500_btemp: Fix error return code Initialize return variable before exiting on an error path. A simplified version of the semantic match that finds this problem is as follows: (http://coccinelle.lip6.fr/) // ( if@p1 (\(ret < 0\|ret != 0\)) { ... return ret; } | ret@p1 = 0 ) ... when != ret = e1 when != &ret *if(...) { ... when != ret = e2 when forall return ret; } // Signed-off-by: Julia Lawall Signed-off-by: Anton Vorontsov --- drivers/power/ab8500_btemp.c | 1 + 1 file changed, 1 insertion(+) (limited to 'drivers') diff --git a/drivers/power/ab8500_btemp.c b/drivers/power/ab8500_btemp.c index bba3ccac72fe..2b76943ff9cf 100644 --- a/drivers/power/ab8500_btemp.c +++ b/drivers/power/ab8500_btemp.c @@ -1014,6 +1014,7 @@ static int __devinit ab8500_btemp_probe(struct platform_device *pdev) create_singlethread_workqueue("ab8500_btemp_wq"); if (di->btemp_wq == NULL) { dev_err(di->dev, "failed to create work queue\n"); + ret = -ENOMEM; goto free_device_info; } -- cgit v1.2.3 From d2941651d754fa04eebd0bba70d558424d838565 Mon Sep 17 00:00:00 2001 From: Julia Lawall Date: Sun, 19 Aug 2012 10:44:21 +0200 Subject: ab8500_fg: Fix error return code Initialize return variable before exiting on an error path. A simplified version of the semantic match that finds this problem is as follows: (http://coccinelle.lip6.fr/) // ( if@p1 (\(ret < 0\|ret != 0\)) { ... return ret; } | ret@p1 = 0 ) ... when != ret = e1 when != &ret *if(...) { ... when != ret = e2 when forall return ret; } // Signed-off-by: Julia Lawall Signed-off-by: Anton Vorontsov --- drivers/power/ab8500_fg.c | 1 + 1 file changed, 1 insertion(+) (limited to 'drivers') diff --git a/drivers/power/ab8500_fg.c b/drivers/power/ab8500_fg.c index bf022255994c..11633e327d38 100644 --- a/drivers/power/ab8500_fg.c +++ b/drivers/power/ab8500_fg.c @@ -2506,6 +2506,7 @@ static int __devinit ab8500_fg_probe(struct platform_device *pdev) di->fg_wq = create_singlethread_workqueue("ab8500_fg_wq"); if (di->fg_wq == NULL) { dev_err(di->dev, "failed to create work queue\n"); + ret = -ENOMEM; goto free_device_info; } -- cgit v1.2.3 From f77609959ade7378eed8b03df4c248f6d44c7e15 Mon Sep 17 00:00:00 2001 From: Julia Lawall Date: Sun, 19 Aug 2012 10:44:20 +0200 Subject: bq27x00_battery: Fix error return code Initialize return variable before exiting on an error path. A simplified version of the semantic match that finds this problem is as follows: (http://coccinelle.lip6.fr/) // ( if@p1 (\(ret < 0\|ret != 0\)) { ... return ret; } | ret@p1 = 0 ) ... when != ret = e1 when != &ret *if(...) { ... when != ret = e2 when forall return ret; } // Signed-off-by: Julia Lawall Signed-off-by: Anton Vorontsov --- drivers/power/bq27x00_battery.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/power/bq27x00_battery.c b/drivers/power/bq27x00_battery.c index 181ddece5181..5860d4dfbe9c 100644 --- a/drivers/power/bq27x00_battery.c +++ b/drivers/power/bq27x00_battery.c @@ -814,7 +814,8 @@ static int bq27x00_battery_probe(struct i2c_client *client, di->bat.name = name; di->bus.read = &bq27x00_read_i2c; - if (bq27x00_powersupply_init(di)) + retval = bq27x00_powersupply_init(di); + if (retval) goto batt_failed_3; i2c_set_clientdata(client, di); -- cgit v1.2.3 From 6297b5e54b5511d4e72e8d5fc3b139650adc74d8 Mon Sep 17 00:00:00 2001 From: Devendra Naga Date: Sun, 29 Jul 2012 23:31:55 +0545 Subject: lp8727_charger: Unregister power supply at error path of lp8727_register_psy if usb power supply registration fails, we wont unregister the ac power supply if battery power supply registration fails, we wont unregister the usb, and ac supply, take care of those things and also no need of goto -err_mem: at the fail case of kzalloc simply can have return -ENOMEM Signed-off-by: Devendra Naga Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 6a364f4798f7..4e37b269ac95 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -362,7 +362,7 @@ static int lp8727_register_psy(struct lp8727_chg *pchg) psy = kzalloc(sizeof(*psy), GFP_KERNEL); if (!psy) - goto err_mem; + return -ENOMEM; pchg->psy = psy; @@ -375,7 +375,7 @@ static int lp8727_register_psy(struct lp8727_chg *pchg) psy->ac.num_supplicants = ARRAY_SIZE(battery_supplied_to); if (power_supply_register(pchg->dev, &psy->ac)) - goto err_psy; + goto err_psy_ac; psy->usb.name = "usb"; psy->usb.type = POWER_SUPPLY_TYPE_USB; @@ -386,7 +386,7 @@ static int lp8727_register_psy(struct lp8727_chg *pchg) psy->usb.num_supplicants = ARRAY_SIZE(battery_supplied_to); if (power_supply_register(pchg->dev, &psy->usb)) - goto err_psy; + goto err_psy_usb; psy->batt.name = "main_batt"; psy->batt.type = POWER_SUPPLY_TYPE_BATTERY; @@ -396,13 +396,15 @@ static int lp8727_register_psy(struct lp8727_chg *pchg) psy->batt.external_power_changed = lp8727_charger_changed; if (power_supply_register(pchg->dev, &psy->batt)) - goto err_psy; + goto err_psy_batt; return 0; -err_mem: - return -ENOMEM; -err_psy: +err_psy_batt: + power_supply_unregister(&psy->usb); +err_psy_usb: + power_supply_unregister(&psy->ac); +err_psy_ac: kfree(psy); return -EPERM; } -- cgit v1.2.3 From bc909f27f55ceb8f950ced5cf3a26d6b26e86ca6 Mon Sep 17 00:00:00 2001 From: Anton Vorontsov Date: Wed, 22 Aug 2012 19:29:44 -0700 Subject: da9052-battery: Fix da9052_determine_vc_tbl_index's return value The patch fixes the following warnings: CHECK drivers/power/da9052-battery.c drivers/power/da9052-battery.c:330:15: warning: symbol 'da9052_determine_vc_tbl_index' was not declared. Should it be static? CC drivers/power/da9052-battery.o drivers/power/da9052-battery.c: In function 'da9052_determine_vc_tbl_index': drivers/power/da9052-battery.c:348:1: warning: control reaches end of non-void function [-Wreturn-type] Signed-off-by: Anton Vorontsov --- drivers/power/da9052-battery.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/power/da9052-battery.c b/drivers/power/da9052-battery.c index a5f6a0ec1572..20b86ed946e3 100644 --- a/drivers/power/da9052-battery.c +++ b/drivers/power/da9052-battery.c @@ -327,7 +327,7 @@ static int da9052_bat_interpolate(int vbat_lower, int vbat_upper, return tmp; } -unsigned char da9052_determine_vc_tbl_index(unsigned char adc_temp) +static unsigned char da9052_determine_vc_tbl_index(unsigned char adc_temp) { int i; @@ -345,6 +345,13 @@ unsigned char da9052_determine_vc_tbl_index(unsigned char adc_temp) && (adc_temp <= vc_tbl_ref[i])) return i + 1; } + /* + * For some reason authors of the driver didn't presume that we can + * end up here. It might be OK, but might be not, no one knows for + * sure. Go check your battery, is it on fire? + */ + WARN_ON(1); + return 0; } static int da9052_bat_read_capacity(struct da9052_battery *bat, int *capacity) -- cgit v1.2.3 From 4ac64d51265957f78770c112727e5cfbe615d279 Mon Sep 17 00:00:00 2001 From: Axel Lin Date: Wed, 8 Aug 2012 16:08:18 +0800 Subject: power_supply: Remove broken mark for da9052-battery The fix for MFD part is already merged so we can remove the broken mark for da9052-battery. Signed-off-by: Axel Lin Signed-off-by: Anton Vorontsov --- drivers/power/Kconfig | 1 - 1 file changed, 1 deletion(-) (limited to 'drivers') diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig index c1892f321c46..fcc1bb076217 100644 --- a/drivers/power/Kconfig +++ b/drivers/power/Kconfig @@ -167,7 +167,6 @@ config BATTERY_DA9030 config BATTERY_DA9052 tristate "Dialog DA9052 Battery" depends on PMIC_DA9052 - depends on BROKEN help Say Y here to enable support for batteries charger integrated into DA9052 PMIC. -- cgit v1.2.3 From 94b05cabc07d7ebb4f9d2f2dce6ab7db20f7fbac Mon Sep 17 00:00:00 2001 From: Il Han Date: Fri, 17 Aug 2012 16:49:43 +0900 Subject: twl4030_charger: It would be better not to use the 0b-prefix It would be better not to use the 0b-prefix for earlier versions of gcc-4.3. Signed-off-by: Il Han Acked-by: NeilBrown Signed-off-by: Anton Vorontsov --- drivers/power/twl4030_charger.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'drivers') diff --git a/drivers/power/twl4030_charger.c b/drivers/power/twl4030_charger.c index 15f4d5d8611b..8d6bc6859a92 100644 --- a/drivers/power/twl4030_charger.c +++ b/drivers/power/twl4030_charger.c @@ -41,16 +41,16 @@ #define TWL4030_STS_VBUS BIT(7) #define TWL4030_STS_USB_ID BIT(2) #define TWL4030_BBCHEN BIT(4) -#define TWL4030_BBSEL_MASK 0b1100 -#define TWL4030_BBSEL_2V5 0b0000 -#define TWL4030_BBSEL_3V0 0b0100 -#define TWL4030_BBSEL_3V1 0b1000 -#define TWL4030_BBSEL_3V2 0b1100 -#define TWL4030_BBISEL_MASK 0b11 -#define TWL4030_BBISEL_25uA 0b00 -#define TWL4030_BBISEL_150uA 0b01 -#define TWL4030_BBISEL_500uA 0b10 -#define TWL4030_BBISEL_1000uA 0b11 +#define TWL4030_BBSEL_MASK 0x0c +#define TWL4030_BBSEL_2V5 0x00 +#define TWL4030_BBSEL_3V0 0x04 +#define TWL4030_BBSEL_3V1 0x08 +#define TWL4030_BBSEL_3V2 0x0c +#define TWL4030_BBISEL_MASK 0x03 +#define TWL4030_BBISEL_25uA 0x00 +#define TWL4030_BBISEL_150uA 0x01 +#define TWL4030_BBISEL_500uA 0x02 +#define TWL4030_BBISEL_1000uA 0x03 /* BCI interrupts */ #define TWL4030_WOVF BIT(0) /* Watchdog overflow */ -- cgit v1.2.3 From bc6e51d8891629a9d117dc75a8a2eb9a411a3510 Mon Sep 17 00:00:00 2001 From: Devendra Naga Date: Mon, 30 Jul 2012 16:29:10 +0545 Subject: ds2781_battery: Convert to module_platform_driver module_platform_driver can be used to replace the platform_driver register and unregister functions, with the calls to module_init and module_exit, i.e. all the code that is doing like the below static int __init mymod_init(void) { return platform_driver_register(&drv_operations); } static void __exit mymod_exit(void) { platform_driver_unregister(&drv_operations); } module_init(mymod_init); module_exit(mymod_exit); can be replaced with module_platform_driver(drv_operations)... Signed-off-by: Devendra Naga Signed-off-by: Anton Vorontsov --- drivers/power/ds2781_battery.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) (limited to 'drivers') diff --git a/drivers/power/ds2781_battery.c b/drivers/power/ds2781_battery.c index 7a1ff4e4cf9a..88e304aea2dc 100644 --- a/drivers/power/ds2781_battery.c +++ b/drivers/power/ds2781_battery.c @@ -834,20 +834,7 @@ static struct platform_driver ds2781_battery_driver = { .probe = ds2781_battery_probe, .remove = __devexit_p(ds2781_battery_remove), }; - -static int __init ds2781_battery_init(void) -{ - return platform_driver_register(&ds2781_battery_driver); -} - -static void __exit ds2781_battery_exit(void) -{ - platform_driver_unregister(&ds2781_battery_driver); -} - -module_init(ds2781_battery_init); -module_exit(ds2781_battery_exit); - +module_platform_driver(ds2781_battery_driver); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Renata Sayakhova "); -- cgit v1.2.3 From deacc4fe57794f80ad4449c7cea611921c2d0420 Mon Sep 17 00:00:00 2001 From: Devendra Naga Date: Mon, 30 Jul 2012 16:29:33 +0545 Subject: ds2781_battery: Replace call to kzalloc with devm_kzalloc With devm_kzalloc there is no need of freeing at the errorpath and also at unregister it. Signed-off-by: Devendra Naga Signed-off-by: Anton Vorontsov --- drivers/power/ds2781_battery.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'drivers') diff --git a/drivers/power/ds2781_battery.c b/drivers/power/ds2781_battery.c index 88e304aea2dc..22b3c8c93552 100644 --- a/drivers/power/ds2781_battery.c +++ b/drivers/power/ds2781_battery.c @@ -755,11 +755,9 @@ static int __devinit ds2781_battery_probe(struct platform_device *pdev) int ret = 0; struct ds2781_device_info *dev_info; - dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL); - if (!dev_info) { - ret = -ENOMEM; - goto fail; - } + dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL); + if (!dev_info) + return -ENOMEM; platform_set_drvdata(pdev, dev_info); @@ -774,7 +772,7 @@ static int __devinit ds2781_battery_probe(struct platform_device *pdev) ret = power_supply_register(&pdev->dev, &dev_info->bat); if (ret) { dev_err(dev_info->dev, "failed to register battery\n"); - goto fail_free_info; + goto fail; } ret = sysfs_create_group(&dev_info->bat.dev->kobj, &ds2781_attr_group); @@ -808,8 +806,6 @@ fail_remove_group: sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2781_attr_group); fail_unregister: power_supply_unregister(&dev_info->bat); -fail_free_info: - kfree(dev_info); fail: return ret; } @@ -823,7 +819,6 @@ static int __devexit ds2781_battery_remove(struct platform_device *pdev) power_supply_unregister(&dev_info->bat); - kfree(dev_info); return 0; } -- cgit v1.2.3 From b1b56872be3b36af19313bf0953b1361c36b4a98 Mon Sep 17 00:00:00 2001 From: Ramakrishna Pallala Date: Thu, 23 Aug 2012 06:50:21 +0530 Subject: power_supply: Add new power supply AUTHENTIC property It is possible that users can use non-standard chargers or use invalid batteries especially with mobile devices. This patch adds a new power supply property called 'AUTHENTIC' to indicate this to the user(user space). Signed-off-by: Ramakrishna Pallala Signed-off-by: Anton Vorontsov --- Documentation/power/power_supply_class.txt | 3 +++ drivers/power/power_supply_sysfs.c | 1 + include/linux/power_supply.h | 1 + 3 files changed, 5 insertions(+) (limited to 'drivers') diff --git a/Documentation/power/power_supply_class.txt b/Documentation/power/power_supply_class.txt index 2f0ddc15b5ac..48badec1cec3 100644 --- a/Documentation/power/power_supply_class.txt +++ b/Documentation/power/power_supply_class.txt @@ -81,6 +81,9 @@ This defines trickle and fast charges. For batteries that are already charged or discharging, 'n/a' can be displayed (or 'unknown', if the status is not known). +AUTHENTIC - indicates the power supply (battery or charger) connected +to the platform is authentic(1) or non authentic(0). + HEALTH - represents health of the battery, values corresponds to POWER_SUPPLY_HEALTH_*, defined in battery.h. diff --git a/drivers/power/power_supply_sysfs.c b/drivers/power/power_supply_sysfs.c index 1d96614a17a4..be34a6d4664d 100644 --- a/drivers/power/power_supply_sysfs.c +++ b/drivers/power/power_supply_sysfs.c @@ -138,6 +138,7 @@ static struct device_attribute power_supply_attrs[] = { POWER_SUPPLY_ATTR(health), POWER_SUPPLY_ATTR(present), POWER_SUPPLY_ATTR(online), + POWER_SUPPLY_ATTR(authentic), POWER_SUPPLY_ATTR(technology), POWER_SUPPLY_ATTR(cycle_count), POWER_SUPPLY_ATTR(voltage_max), diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 0bafbb15f29c..204d43dc6ddc 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -88,6 +88,7 @@ enum power_supply_property { POWER_SUPPLY_PROP_HEALTH, POWER_SUPPLY_PROP_PRESENT, POWER_SUPPLY_PROP_ONLINE, + POWER_SUPPLY_PROP_AUTHENTIC, POWER_SUPPLY_PROP_TECHNOLOGY, POWER_SUPPLY_PROP_CYCLE_COUNT, POWER_SUPPLY_PROP_VOLTAGE_MAX, -- cgit v1.2.3 From c084e092e47ca2435673702c6cf6e87a731ecc6f Mon Sep 17 00:00:00 2001 From: Anton Vorontsov Date: Wed, 22 Aug 2012 19:59:45 -0700 Subject: da9030_battery: Don't use 0 as NULL Noticed the new warning: CHECK drivers/power/da9030_battery.c drivers/power/da9030_battery.c:190:68: warning: Using plain integer as NULL pointer This commit fixes the issue. Signed-off-by: Anton Vorontsov --- drivers/power/da9030_battery.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'drivers') diff --git a/drivers/power/da9030_battery.c b/drivers/power/da9030_battery.c index 3fd3e95d2b85..94762e67e22b 100644 --- a/drivers/power/da9030_battery.c +++ b/drivers/power/da9030_battery.c @@ -187,8 +187,8 @@ static const struct file_operations bat_debug_fops = { static struct dentry *da9030_bat_create_debugfs(struct da9030_charger *charger) { - charger->debug_file = debugfs_create_file("charger", 0666, 0, charger, - &bat_debug_fops); + charger->debug_file = debugfs_create_file("charger", 0666, NULL, + charger, &bat_debug_fops); return charger->debug_file; } -- cgit v1.2.3 From dbb61fc7452632a4225b2fe3719b6c2d1306e257 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Fri, 27 Jul 2012 14:01:34 +0900 Subject: charger-manager: Disable regulator when charger cable is detached This patch disable regulator for charging when charger cable is detached before stopping charging forcibly on abnormal battery state and check return value of regulator_enable/disable() function to confirm correct operation of enabling or disabling regulator for charging. Signed-off-by: Chanwoo Choi Signed-off-by: Myungjoo Ham Signed-off-by: Kyungmin Park Signed-off-by: Anton Vorontsov --- drivers/power/charger-manager.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'drivers') diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c index 526e5c931294..240de49934f1 100644 --- a/drivers/power/charger-manager.c +++ b/drivers/power/charger-manager.c @@ -271,9 +271,24 @@ static int try_charger_enable(struct charger_manager *cm, bool enable) if (enable) { if (cm->emergency_stop) return -EAGAIN; - for (i = 0 ; i < desc->num_charger_regulators ; i++) - regulator_enable(desc->charger_regulators[i].consumer); + for (i = 0 ; i < desc->num_charger_regulators ; i++) { + err = regulator_enable(desc->charger_regulators[i].consumer); + if (err < 0) { + dev_warn(cm->dev, + "Cannot enable %s regulator\n", + desc->charger_regulators[i].regulator_name); + } + } } else { + for (i = 0 ; i < desc->num_charger_regulators ; i++) { + err = regulator_disable(desc->charger_regulators[i].consumer); + if (err < 0) { + dev_warn(cm->dev, + "Cannot disable %s regulator\n", + desc->charger_regulators[i].regulator_name); + } + } + /* * Abnormal battery state - Stop charging forcibly, * even if charger was enabled at the other places -- cgit v1.2.3 From fd65ee5f1c21af9ff9f113842d513ca50749ad68 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Fri, 27 Jul 2012 14:01:37 +0900 Subject: charger-manager: Use replacement variable to check state of battery This patch remove unnecessary variable(cm->fullbatt_vchk_uV) by using 'desc->fullbatt_uV' field directly in fullbatt_handler() function to check the state of battery. Signed-off-by: Chanwoo Choi Signed-off-by: Myungjoo Ham Signed-off-by: Kyungmin Park Signed-off-by: Anton Vorontsov --- drivers/power/charger-manager.c | 2 +- include/linux/power/charger-manager.h | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) (limited to 'drivers') diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c index 240de49934f1..cdf29d2eb7a5 100644 --- a/drivers/power/charger-manager.c +++ b/drivers/power/charger-manager.c @@ -415,7 +415,7 @@ static void fullbatt_vchk(struct work_struct *work) return; } - diff = cm->fullbatt_vchk_uV; + diff = desc->fullbatt_uV; diff -= batt_uV; dev_dbg(cm->dev, "VBATT dropped %duV after full-batt.\n", diff); diff --git a/include/linux/power/charger-manager.h b/include/linux/power/charger-manager.h index cd22029e32aa..7d7b90fb7b4b 100644 --- a/include/linux/power/charger-manager.h +++ b/include/linux/power/charger-manager.h @@ -194,8 +194,6 @@ struct charger_desc { * @charger_enabled: the state of charger * @fullbatt_vchk_jiffies_at: * jiffies at the time full battery check will occur. - * @fullbatt_vchk_uV: voltage in microvolt - * criteria for full battery * @fullbatt_vchk_work: work queue for full battery check * @emergency_stop: * When setting true, stop charging @@ -218,7 +216,6 @@ struct charger_manager { bool charger_enabled; unsigned long fullbatt_vchk_jiffies_at; - unsigned int fullbatt_vchk_uV; struct delayed_work fullbatt_vchk_work; int emergency_stop; -- cgit v1.2.3 From 2815b786c3bb86fff97f1f6e2f0874903ff2339b Mon Sep 17 00:00:00 2001 From: Ramakrishna Pallala Date: Mon, 30 Jul 2012 12:49:21 +0530 Subject: power_supply: Add new power supply properties CHARGE_CURRENT/VOLTAGE_MAX There are different types of chargers avalibale like AC, Solar, USB, etc.. Even in USB we have different types SDP/DCP/CDP/ACA and all these chargers have different o/p ratings. For example SDP supports only 500mA of charge current whereas AC charger can support upto 8A or more. Similarly batteries also come with charge current and voltage ratings and these ratings vary depending on its capacity and the technology used. This patch adds two new power supply properties CONSTANT_CHARGE_CURRENT_MAX and CONSTANT_CHARGE_CURRENT_MAX. Signed-off-by: Ramakrishna Pallala Signed-off-by: Anton Vorontsov --- Documentation/power/power_supply_class.txt | 4 ++++ drivers/power/power_supply_sysfs.c | 2 ++ include/linux/power_supply.h | 4 ++++ 3 files changed, 10 insertions(+) (limited to 'drivers') diff --git a/Documentation/power/power_supply_class.txt b/Documentation/power/power_supply_class.txt index 48badec1cec3..9c647bd7c5a9 100644 --- a/Documentation/power/power_supply_class.txt +++ b/Documentation/power/power_supply_class.txt @@ -116,8 +116,12 @@ be negative; there is no empty or full value. It is only useful for relative, time-based measurements. CONSTANT_CHARGE_CURRENT - constant charge current programmed by charger. +CONSTANT_CHARGE_CURRENT_MAX - maximum charge current supported by the +power supply object. CONSTANT_CHARGE_VOLTAGE - constant charge voltage programmed by charger. +CONSTANT_CHARGE_VOLTAGE_MAX - maximum charge voltage supported by the +power supply object. ENERGY_FULL, ENERGY_EMPTY - same as above but for energy. diff --git a/drivers/power/power_supply_sysfs.c b/drivers/power/power_supply_sysfs.c index be34a6d4664d..395c2cfa16c0 100644 --- a/drivers/power/power_supply_sysfs.c +++ b/drivers/power/power_supply_sysfs.c @@ -161,7 +161,9 @@ static struct device_attribute power_supply_attrs[] = { POWER_SUPPLY_ATTR(charge_avg), POWER_SUPPLY_ATTR(charge_counter), POWER_SUPPLY_ATTR(constant_charge_current), + POWER_SUPPLY_ATTR(constant_charge_current_max), POWER_SUPPLY_ATTR(constant_charge_voltage), + POWER_SUPPLY_ATTR(constant_charge_voltage_max), POWER_SUPPLY_ATTR(energy_full_design), POWER_SUPPLY_ATTR(energy_empty_design), POWER_SUPPLY_ATTR(energy_full), diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 204d43dc6ddc..e5ef45834c3c 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -111,7 +111,9 @@ enum power_supply_property { POWER_SUPPLY_PROP_CHARGE_AVG, POWER_SUPPLY_PROP_CHARGE_COUNTER, POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, + POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, + POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN, POWER_SUPPLY_PROP_ENERGY_FULL, @@ -249,6 +251,7 @@ static inline bool power_supply_is_amp_property(enum power_supply_property psp) case POWER_SUPPLY_PROP_CHARGE_AVG: case POWER_SUPPLY_PROP_CHARGE_COUNTER: case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: case POWER_SUPPLY_PROP_CURRENT_MAX: case POWER_SUPPLY_PROP_CURRENT_NOW: case POWER_SUPPLY_PROP_CURRENT_AVG: @@ -277,6 +280,7 @@ static inline bool power_supply_is_watt_property(enum power_supply_property psp) case POWER_SUPPLY_PROP_VOLTAGE_AVG: case POWER_SUPPLY_PROP_VOLTAGE_OCV: case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: case POWER_SUPPLY_PROP_POWER_NOW: return 1; default: -- cgit v1.2.3 From ec60ea5cd6c9f4b249de1580b8ebfa430055008c Mon Sep 17 00:00:00 2001 From: Paul Parsons Date: Thu, 20 Sep 2012 14:26:05 -0700 Subject: pda_power: Fix ac_draw usage before it being set When I reboot my iPAQ hx4700 in its cradle, the battery will not begin to charge even though the AC supply is connected. Charging will start only after the PDA power driver is tickled by some other power event, such as reseating the iPAQ in its cradle or connecting the USB cable. The problem lies in pda_power_probe(), where ac_draw is used by the call to update_charger() before being set by the call to regulator_get(). Moving the regulator_get() call to before the update_charger() call fixes the problem. Signed-off-by: Paul Parsons Cc: Philipp Zabel Signed-off-by: Anton Vorontsov --- drivers/power/pda_power.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'drivers') diff --git a/drivers/power/pda_power.c b/drivers/power/pda_power.c index 7312f2651647..75536ebdb132 100644 --- a/drivers/power/pda_power.c +++ b/drivers/power/pda_power.c @@ -281,6 +281,14 @@ static int pda_power_probe(struct platform_device *pdev) goto init_failed; } + ac_draw = regulator_get(dev, "ac_draw"); + if (IS_ERR(ac_draw)) { + dev_dbg(dev, "couldn't get ac_draw regulator\n"); + ac_draw = NULL; + ret = PTR_ERR(ac_draw); + goto ac_draw_failed; + } + update_status(); update_charger(); @@ -309,13 +317,6 @@ static int pda_power_probe(struct platform_device *pdev) pda_psy_usb.num_supplicants = pdata->num_supplicants; } - ac_draw = regulator_get(dev, "ac_draw"); - if (IS_ERR(ac_draw)) { - dev_dbg(dev, "couldn't get ac_draw regulator\n"); - ac_draw = NULL; - ret = PTR_ERR(ac_draw); - } - #ifdef CONFIG_USB_OTG_UTILS transceiver = usb_get_phy(USB_PHY_TYPE_USB2); if (!IS_ERR_OR_NULL(transceiver)) { @@ -415,6 +416,7 @@ ac_supply_failed: regulator_put(ac_draw); ac_draw = NULL; } +ac_draw_failed: if (pdata->exit) pdata->exit(dev); init_failed: -- cgit v1.2.3 From 1502cfe19bac2f8490e4167f2b7cb50b9759b273 Mon Sep 17 00:00:00 2001 From: Ramakrishna Pallala Date: Tue, 18 Sep 2012 21:58:07 +0530 Subject: smb347-charger: Fix battery status reporting logic for charger faults This patch checks for charger status register for determining the battery charging status and reports Discharing/Charging/Not Charging/Full accordingly. This patch also adds the interrupt support for Safety Timer Expiration. This interrupt is helpful in debugging the cause for charger fault. Signed-off-by: Ramakrishna Pallala Acked-by: Mika Westerberg Signed-off-by: Anton Vorontsov --- drivers/power/smb347-charger.c | 97 +++++++++++++++++++++++++++++++++++------- 1 file changed, 82 insertions(+), 15 deletions(-) (limited to 'drivers') diff --git a/drivers/power/smb347-charger.c b/drivers/power/smb347-charger.c index 332dd0110bda..a9707c11fbed 100644 --- a/drivers/power/smb347-charger.c +++ b/drivers/power/smb347-charger.c @@ -80,6 +80,7 @@ #define CFG_FAULT_IRQ_DCIN_UV BIT(2) #define CFG_STATUS_IRQ 0x0d #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4) +#define CFG_STATUS_IRQ_CHARGE_TIMEOUT BIT(7) #define CFG_ADDRESS 0x0e /* Command registers */ @@ -96,6 +97,9 @@ #define IRQSTAT_C_TERMINATION_STAT BIT(0) #define IRQSTAT_C_TERMINATION_IRQ BIT(1) #define IRQSTAT_C_TAPER_IRQ BIT(3) +#define IRQSTAT_D 0x38 +#define IRQSTAT_D_CHARGE_TIMEOUT_STAT BIT(2) +#define IRQSTAT_D_CHARGE_TIMEOUT_IRQ BIT(3) #define IRQSTAT_E 0x39 #define IRQSTAT_E_USBIN_UV_STAT BIT(0) #define IRQSTAT_E_USBIN_UV_IRQ BIT(1) @@ -109,8 +113,10 @@ #define STAT_B 0x3c #define STAT_C 0x3d #define STAT_C_CHG_ENABLED BIT(0) +#define STAT_C_HOLDOFF_STAT BIT(3) #define STAT_C_CHG_MASK 0x06 #define STAT_C_CHG_SHIFT 1 +#define STAT_C_CHG_TERM BIT(5) #define STAT_C_CHARGER_ERROR BIT(6) #define STAT_E 0x3f @@ -701,7 +707,7 @@ fail: static irqreturn_t smb347_interrupt(int irq, void *data) { struct smb347_charger *smb = data; - unsigned int stat_c, irqstat_e, irqstat_c; + unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e; bool handled = false; int ret; @@ -717,6 +723,12 @@ static irqreturn_t smb347_interrupt(int irq, void *data) return IRQ_NONE; } + ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d); + if (ret < 0) { + dev_warn(smb->dev, "reading IRQSTAT_D failed\n"); + return IRQ_NONE; + } + ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e); if (ret < 0) { dev_warn(smb->dev, "reading IRQSTAT_E failed\n"); @@ -724,13 +736,11 @@ static irqreturn_t smb347_interrupt(int irq, void *data) } /* - * If we get charger error we report the error back to user and - * disable charging. + * If we get charger error we report the error back to user. + * If the error is recovered charging will resume again. */ if (stat_c & STAT_C_CHARGER_ERROR) { - dev_err(smb->dev, "error in charger, disabling charging\n"); - - smb347_charging_disable(smb); + dev_err(smb->dev, "charging stopped due to charger error\n"); power_supply_changed(&smb->battery); handled = true; } @@ -743,6 +753,20 @@ static irqreturn_t smb347_interrupt(int irq, void *data) if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) { if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) power_supply_changed(&smb->battery); + dev_dbg(smb->dev, "going to HW maintenance mode\n"); + handled = true; + } + + /* + * If we got a charger timeout INT that means the charge + * full is not detected with in charge timeout value. + */ + if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_IRQ) { + dev_dbg(smb->dev, "total Charge Timeout INT received\n"); + + if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT) + dev_warn(smb->dev, "charging stopped due to timeout\n"); + power_supply_changed(&smb->battery); handled = true; } @@ -776,6 +800,7 @@ static int smb347_irq_set(struct smb347_charger *smb, bool enable) * Enable/disable interrupts for: * - under voltage * - termination current reached + * - charger timeout * - charger error */ ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff, @@ -784,7 +809,8 @@ static int smb347_irq_set(struct smb347_charger *smb, bool enable) goto fail; ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff, - enable ? CFG_STATUS_IRQ_TERMINATION_OR_TAPER : 0); + enable ? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER | + CFG_STATUS_IRQ_CHARGE_TIMEOUT) : 0); if (ret < 0) goto fail; @@ -988,6 +1014,51 @@ static enum power_supply_property smb347_usb_properties[] = { POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, }; +static int smb347_get_charging_status(struct smb347_charger *smb) +{ + int ret, status; + unsigned int val; + + if (!smb347_is_ps_online(smb)) + return POWER_SUPPLY_STATUS_DISCHARGING; + + ret = regmap_read(smb->regmap, STAT_C, &val); + if (ret < 0) + return ret; + + if ((val & STAT_C_CHARGER_ERROR) || + (val & STAT_C_HOLDOFF_STAT)) { + /* + * set to NOT CHARGING upon charger error + * or charging has stopped. + */ + status = POWER_SUPPLY_STATUS_NOT_CHARGING; + } else { + if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) { + /* + * set to charging if battery is in pre-charge, + * fast charge or taper charging mode. + */ + status = POWER_SUPPLY_STATUS_CHARGING; + } else if (val & STAT_C_CHG_TERM) { + /* + * set the status to FULL if battery is not in pre + * charge, fast charge or taper charging mode AND + * charging is terminated at least once. + */ + status = POWER_SUPPLY_STATUS_FULL; + } else { + /* + * in this case no charger error or termination + * occured but charging is not in progress!!! + */ + status = POWER_SUPPLY_STATUS_NOT_CHARGING; + } + } + + return status; +} + static int smb347_battery_get_property(struct power_supply *psy, enum power_supply_property prop, union power_supply_propval *val) @@ -1003,14 +1074,10 @@ static int smb347_battery_get_property(struct power_supply *psy, switch (prop) { case POWER_SUPPLY_PROP_STATUS: - if (!smb347_is_ps_online(smb)) { - val->intval = POWER_SUPPLY_STATUS_DISCHARGING; - break; - } - if (smb347_charging_status(smb)) - val->intval = POWER_SUPPLY_STATUS_CHARGING; - else - val->intval = POWER_SUPPLY_STATUS_FULL; + ret = smb347_get_charging_status(smb); + if (ret < 0) + return ret; + val->intval = ret; break; case POWER_SUPPLY_PROP_CHARGE_TYPE: -- cgit v1.2.3 From a22b41a31e5382792151f193d185a3cd39593cfd Mon Sep 17 00:00:00 2001 From: Olof Johansson Date: Thu, 6 Sep 2012 11:32:29 -0700 Subject: sbs-battery: Probe should try talking to the device Turns out this driver doesn't actually try talking to the device at probe time, so if it's incorrectly configured in the device tree or platform data (or if the battery has been removed from the system), then probe will succeed and every access will sit there and time out. The end result is a possibly laggy system that thinks it has a battery but can never read status, which isn't very useful. Instead, just read any register (I chose status) at probe, and if that fails, don't register the device. Signed-off-by: Olof Johansson Acked-by: Rhyland Klein Signed-off-by: Anton Vorontsov --- drivers/power/sbs-battery.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'drivers') diff --git a/drivers/power/sbs-battery.c b/drivers/power/sbs-battery.c index a65e8f54157e..4146596d254b 100644 --- a/drivers/power/sbs-battery.c +++ b/drivers/power/sbs-battery.c @@ -759,6 +759,16 @@ static int __devinit sbs_probe(struct i2c_client *client, chip->irq = irq; skip_gpio: + /* + * Before we register, we need to make sure we can actually talk + * to the battery. + */ + rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); + if (rc < 0) { + dev_err(&client->dev, "%s: Failed to get device status\n", + __func__); + goto exit_psupply; + } rc = power_supply_register(&client->dev, &chip->power_supply); if (rc) { -- cgit v1.2.3 From f4f95adf7caa9078e15c55407573208683dcb1b7 Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Wed, 5 Sep 2012 15:34:41 +0300 Subject: da9052-battery: Don't free IRQ that wasn't requested We should decrement "i" before doing the free_irq(). If we call this because request_threaded_irq() failed then we don't want to free the thing which failed. Or in the case where we get here because power_supply_register() failed then the original codes does a read past the end of the array. Signed-off-by: Dan Carpenter Signed-off-by: Anton Vorontsov --- drivers/power/da9052-battery.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/power/da9052-battery.c b/drivers/power/da9052-battery.c index 20b86ed946e3..d9d034d7496f 100644 --- a/drivers/power/da9052-battery.c +++ b/drivers/power/da9052-battery.c @@ -623,7 +623,7 @@ static s32 __devinit da9052_bat_probe(struct platform_device *pdev) return 0; err: - for (; i >= 0; i--) { + while (--i >= 0) { irq = platform_get_irq_byname(pdev, da9052_bat_irqs[i]); free_irq(bat->da9052->irq_base + irq, bat); } -- cgit v1.2.3 From 98a2766493589c18c327ae3dad5243b53fcb5f70 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Wed, 5 Sep 2012 10:43:21 +0000 Subject: power_supply: Add new lp8788 charger driver TI LP8788 PMU supports regulators, battery charger, RTC, ADC, backlight driver and current sinks. This patch supports the charger operations including the charger interrupt handling. The LP8788 charger driver provides configurable platform data. The charger platform data includes ADC input ID, maximum battery voltage, charging parameters and etc. The ADC input is used for getting the battery voltage and temperature. Charging parameters are used for updating the charger operations such like setting charging current and setting End-of-charge conditions. When the charging interrupt occurs, power supply uevents are generated in order to update the user-space information. For platform specific charger action, the charger_event() function can be used in the platform side. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/Kconfig | 7 + drivers/power/Makefile | 1 + drivers/power/lp8788-charger.c | 795 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 803 insertions(+) create mode 100644 drivers/power/lp8788-charger.c (limited to 'drivers') diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig index fcc1bb076217..4ec2ff0ee4a2 100644 --- a/drivers/power/Kconfig +++ b/drivers/power/Kconfig @@ -254,6 +254,13 @@ config CHARGER_LP8727 help Say Y here to enable support for LP8727 Charger Driver. +config CHARGER_LP8788 + tristate "TI LP8788 charger driver" + depends on MFD_LP8788 + depends on LP8788_ADC + help + Say Y to enable support for the LP8788 linear charger. + config CHARGER_GPIO tristate "GPIO charger" depends on GPIOLIB diff --git a/drivers/power/Makefile b/drivers/power/Makefile index ee58afb1e71f..587c5f174626 100644 --- a/drivers/power/Makefile +++ b/drivers/power/Makefile @@ -39,6 +39,7 @@ obj-$(CONFIG_CHARGER_ISP1704) += isp1704_charger.o obj-$(CONFIG_CHARGER_MAX8903) += max8903_charger.o obj-$(CONFIG_CHARGER_TWL4030) += twl4030_charger.o obj-$(CONFIG_CHARGER_LP8727) += lp8727_charger.o +obj-$(CONFIG_CHARGER_LP8788) += lp8788-charger.o obj-$(CONFIG_CHARGER_GPIO) += gpio-charger.o obj-$(CONFIG_CHARGER_MANAGER) += charger-manager.o obj-$(CONFIG_CHARGER_MAX8997) += max8997_charger.o diff --git a/drivers/power/lp8788-charger.c b/drivers/power/lp8788-charger.c new file mode 100644 index 000000000000..e852d12cd077 --- /dev/null +++ b/drivers/power/lp8788-charger.c @@ -0,0 +1,795 @@ +/* + * TI LP8788 MFD - battery charger driver + * + * Copyright 2012 Texas Instruments + * + * Author: Milo(Woogyom) Kim + * + * 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. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* register address */ +#define LP8788_CHG_STATUS 0x07 +#define LP8788_CHG_IDCIN 0x13 +#define LP8788_CHG_IBATT 0x14 +#define LP8788_CHG_VTERM 0x15 +#define LP8788_CHG_EOC 0x16 + +/* mask/shift bits */ +#define LP8788_CHG_INPUT_STATE_M 0x03 /* Addr 07h */ +#define LP8788_CHG_STATE_M 0x3C +#define LP8788_CHG_STATE_S 2 +#define LP8788_NO_BATT_M BIT(6) +#define LP8788_BAD_BATT_M BIT(7) +#define LP8788_CHG_IBATT_M 0x1F /* Addr 14h */ +#define LP8788_CHG_VTERM_M 0x0F /* Addr 15h */ +#define LP8788_CHG_EOC_LEVEL_M 0x30 /* Addr 16h */ +#define LP8788_CHG_EOC_LEVEL_S 4 +#define LP8788_CHG_EOC_TIME_M 0x0E +#define LP8788_CHG_EOC_TIME_S 1 +#define LP8788_CHG_EOC_MODE_M BIT(0) + +#define LP8788_CHARGER_NAME "charger" +#define LP8788_BATTERY_NAME "main_batt" + +#define LP8788_CHG_START 0x11 +#define LP8788_CHG_END 0x1C + +#define LP8788_BUF_SIZE 40 +#define LP8788_ISEL_MAX 23 +#define LP8788_ISEL_STEP 50 +#define LP8788_VTERM_MIN 4100 +#define LP8788_VTERM_STEP 25 +#define LP8788_MAX_BATT_CAPACITY 100 +#define LP8788_MAX_CHG_IRQS 11 + +enum lp8788_charging_state { + LP8788_OFF, + LP8788_WARM_UP, + LP8788_LOW_INPUT = 0x3, + LP8788_PRECHARGE, + LP8788_CC, + LP8788_CV, + LP8788_MAINTENANCE, + LP8788_BATTERY_FAULT, + LP8788_SYSTEM_SUPPORT = 0xC, + LP8788_HIGH_CURRENT = 0xF, + LP8788_MAX_CHG_STATE, +}; + +enum lp8788_charger_adc_sel { + LP8788_VBATT, + LP8788_BATT_TEMP, + LP8788_NUM_CHG_ADC, +}; + +enum lp8788_charger_input_state { + LP8788_SYSTEM_SUPPLY = 1, + LP8788_FULL_FUNCTION, +}; + +/* + * struct lp8788_chg_irq + * @which : lp8788 interrupt id + * @virq : Linux IRQ number from irq_domain + */ +struct lp8788_chg_irq { + enum lp8788_int_id which; + int virq; +}; + +/* + * struct lp8788_charger + * @lp : used for accessing the registers of mfd lp8788 device + * @charger : power supply driver for the battery charger + * @battery : power supply driver for the battery + * @charger_work : work queue for charger input interrupts + * @chan : iio channels for getting adc values + * eg) battery voltage, capacity and temperature + * @irqs : charger dedicated interrupts + * @num_irqs : total numbers of charger interrupts + * @pdata : charger platform specific data + */ +struct lp8788_charger { + struct lp8788 *lp; + struct power_supply charger; + struct power_supply battery; + struct work_struct charger_work; + struct iio_channel *chan[LP8788_NUM_CHG_ADC]; + struct lp8788_chg_irq irqs[LP8788_MAX_CHG_IRQS]; + int num_irqs; + struct lp8788_charger_platform_data *pdata; +}; + +static char *battery_supplied_to[] = { + LP8788_BATTERY_NAME, +}; + +static enum power_supply_property lp8788_charger_prop[] = { + POWER_SUPPLY_PROP_ONLINE, + POWER_SUPPLY_PROP_CURRENT_MAX, +}; + +static enum power_supply_property lp8788_battery_prop[] = { + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_HEALTH, + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_CAPACITY, + POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, + POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, + POWER_SUPPLY_PROP_TEMP, +}; + +static bool lp8788_is_charger_detected(struct lp8788_charger *pchg) +{ + u8 data; + + lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data); + data &= LP8788_CHG_INPUT_STATE_M; + + return data == LP8788_SYSTEM_SUPPLY || data == LP8788_FULL_FUNCTION; +} + +static int lp8788_charger_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct lp8788_charger *pchg = dev_get_drvdata(psy->dev->parent); + u8 read; + + switch (psp) { + case POWER_SUPPLY_PROP_ONLINE: + val->intval = lp8788_is_charger_detected(pchg); + break; + case POWER_SUPPLY_PROP_CURRENT_MAX: + lp8788_read_byte(pchg->lp, LP8788_CHG_IDCIN, &read); + val->intval = LP8788_ISEL_STEP * + (min_t(int, read, LP8788_ISEL_MAX) + 1); + break; + default: + return -EINVAL; + } + + return 0; +} + +static int lp8788_get_battery_status(struct lp8788_charger *pchg, + union power_supply_propval *val) +{ + enum lp8788_charging_state state; + u8 data; + int ret; + + ret = lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data); + if (ret) + return ret; + + state = (data & LP8788_CHG_STATE_M) >> LP8788_CHG_STATE_S; + switch (state) { + case LP8788_OFF: + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; + break; + case LP8788_PRECHARGE: + case LP8788_CC: + case LP8788_CV: + case LP8788_HIGH_CURRENT: + val->intval = POWER_SUPPLY_STATUS_CHARGING; + break; + case LP8788_MAINTENANCE: + val->intval = POWER_SUPPLY_STATUS_FULL; + break; + default: + val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; + break; + } + + return 0; +} + +static int lp8788_get_battery_health(struct lp8788_charger *pchg, + union power_supply_propval *val) +{ + u8 data; + int ret; + + ret = lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data); + if (ret) + return ret; + + if (data & LP8788_NO_BATT_M) + val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; + else if (data & LP8788_BAD_BATT_M) + val->intval = POWER_SUPPLY_HEALTH_DEAD; + else + val->intval = POWER_SUPPLY_HEALTH_GOOD; + + return 0; +} + +static int lp8788_get_battery_present(struct lp8788_charger *pchg, + union power_supply_propval *val) +{ + u8 data; + int ret; + + ret = lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data); + if (ret) + return ret; + + val->intval = !(data & LP8788_NO_BATT_M); + return 0; +} + +static int lp8788_get_vbatt_adc(struct lp8788_charger *pchg, + unsigned int *result) +{ + struct iio_channel *channel = pchg->chan[LP8788_VBATT]; + int scaleint; + int scalepart; + int ret; + + if (!channel) + return -EINVAL; + + ret = iio_read_channel_scale(channel, &scaleint, &scalepart); + if (ret != IIO_VAL_INT_PLUS_MICRO) + return -EINVAL; + + /* unit: mV */ + *result = (scaleint + scalepart * 1000000) / 1000; + + return 0; +} + +static int lp8788_get_battery_voltage(struct lp8788_charger *pchg, + union power_supply_propval *val) +{ + return lp8788_get_vbatt_adc(pchg, &val->intval); +} + +static int lp8788_get_battery_capacity(struct lp8788_charger *pchg, + union power_supply_propval *val) +{ + struct lp8788 *lp = pchg->lp; + struct lp8788_charger_platform_data *pdata = pchg->pdata; + unsigned int max_vbatt; + unsigned int vbatt; + enum lp8788_charging_state state; + u8 data; + int ret; + + if (!pdata) + return -EINVAL; + + max_vbatt = pdata->max_vbatt_mv; + if (max_vbatt == 0) + return -EINVAL; + + ret = lp8788_read_byte(lp, LP8788_CHG_STATUS, &data); + if (ret) + return ret; + + state = (data & LP8788_CHG_STATE_M) >> LP8788_CHG_STATE_S; + + if (state == LP8788_MAINTENANCE) { + val->intval = LP8788_MAX_BATT_CAPACITY; + } else { + ret = lp8788_get_vbatt_adc(pchg, &vbatt); + if (ret) + return ret; + + val->intval = (vbatt * LP8788_MAX_BATT_CAPACITY) / max_vbatt; + val->intval = min(val->intval, LP8788_MAX_BATT_CAPACITY); + } + + return 0; +} + +static int lp8788_get_battery_temperature(struct lp8788_charger *pchg, + union power_supply_propval *val) +{ + struct iio_channel *channel = pchg->chan[LP8788_BATT_TEMP]; + int scaleint; + int scalepart; + int ret; + + if (!channel) + return -EINVAL; + + ret = iio_read_channel_scale(channel, &scaleint, &scalepart); + if (ret != IIO_VAL_INT_PLUS_MICRO) + return -EINVAL; + + /* unit: 0.1 'C */ + val->intval = (scaleint + scalepart * 1000000) / 100; + + return 0; +} + +static int lp8788_get_battery_charging_current(struct lp8788_charger *pchg, + union power_supply_propval *val) +{ + u8 read; + + lp8788_read_byte(pchg->lp, LP8788_CHG_IBATT, &read); + read &= LP8788_CHG_IBATT_M; + val->intval = LP8788_ISEL_STEP * + (min_t(int, read, LP8788_ISEL_MAX) + 1); + + return 0; +} + +static int lp8788_get_charging_termination_voltage(struct lp8788_charger *pchg, + union power_supply_propval *val) +{ + u8 read; + + lp8788_read_byte(pchg->lp, LP8788_CHG_VTERM, &read); + read &= LP8788_CHG_VTERM_M; + val->intval = LP8788_VTERM_MIN + LP8788_VTERM_STEP * read; + + return 0; +} + +static int lp8788_battery_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct lp8788_charger *pchg = dev_get_drvdata(psy->dev->parent); + + switch (psp) { + case POWER_SUPPLY_PROP_STATUS: + return lp8788_get_battery_status(pchg, val); + case POWER_SUPPLY_PROP_HEALTH: + return lp8788_get_battery_health(pchg, val); + case POWER_SUPPLY_PROP_PRESENT: + return lp8788_get_battery_present(pchg, val); + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + return lp8788_get_battery_voltage(pchg, val); + case POWER_SUPPLY_PROP_CAPACITY: + return lp8788_get_battery_capacity(pchg, val); + case POWER_SUPPLY_PROP_TEMP: + return lp8788_get_battery_temperature(pchg, val); + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: + return lp8788_get_battery_charging_current(pchg, val); + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: + return lp8788_get_charging_termination_voltage(pchg, val); + default: + return -EINVAL; + } +} + +static inline bool lp8788_is_valid_charger_register(u8 addr) +{ + return addr >= LP8788_CHG_START && addr <= LP8788_CHG_END; +} + +static int lp8788_update_charger_params(struct lp8788_charger *pchg) +{ + struct lp8788 *lp = pchg->lp; + struct lp8788_charger_platform_data *pdata = pchg->pdata; + struct lp8788_chg_param *param; + int i; + int ret; + + if (!pdata || !pdata->chg_params) { + dev_info(lp->dev, "skip updating charger parameters\n"); + return 0; + } + + /* settting charging parameters */ + for (i = 0; i < pdata->num_chg_params; i++) { + param = pdata->chg_params + i; + + if (!param) + continue; + + if (lp8788_is_valid_charger_register(param->addr)) { + ret = lp8788_write_byte(lp, param->addr, param->val); + if (ret) + return ret; + } + } + + return 0; +} + +static int lp8788_psy_register(struct platform_device *pdev, + struct lp8788_charger *pchg) +{ + pchg->charger.name = LP8788_CHARGER_NAME; + pchg->charger.type = POWER_SUPPLY_TYPE_MAINS; + pchg->charger.properties = lp8788_charger_prop; + pchg->charger.num_properties = ARRAY_SIZE(lp8788_charger_prop); + pchg->charger.get_property = lp8788_charger_get_property; + pchg->charger.supplied_to = battery_supplied_to; + pchg->charger.num_supplicants = ARRAY_SIZE(battery_supplied_to); + + if (power_supply_register(&pdev->dev, &pchg->charger)) + return -EPERM; + + pchg->battery.name = LP8788_BATTERY_NAME; + pchg->battery.type = POWER_SUPPLY_TYPE_BATTERY; + pchg->battery.properties = lp8788_battery_prop; + pchg->battery.num_properties = ARRAY_SIZE(lp8788_battery_prop); + pchg->battery.get_property = lp8788_battery_get_property; + + if (power_supply_register(&pdev->dev, &pchg->battery)) + return -EPERM; + + return 0; +} + +static void lp8788_psy_unregister(struct lp8788_charger *pchg) +{ + power_supply_unregister(&pchg->battery); + power_supply_unregister(&pchg->charger); +} + +static void lp8788_charger_event(struct work_struct *work) +{ + struct lp8788_charger *pchg = + container_of(work, struct lp8788_charger, charger_work); + struct lp8788_charger_platform_data *pdata = pchg->pdata; + enum lp8788_charger_event event = lp8788_is_charger_detected(pchg); + + pdata->charger_event(pchg->lp, event); +} + +static bool lp8788_find_irq_id(struct lp8788_charger *pchg, int virq, int *id) +{ + bool found; + int i; + + for (i = 0; i < pchg->num_irqs; i++) { + if (pchg->irqs[i].virq == virq) { + *id = pchg->irqs[i].which; + found = true; + break; + } + } + + return found; +} + +static irqreturn_t lp8788_charger_irq_thread(int virq, void *ptr) +{ + struct lp8788_charger *pchg = ptr; + struct lp8788_charger_platform_data *pdata = pchg->pdata; + int id = -1; + + if (!lp8788_find_irq_id(pchg, virq, &id)) + return IRQ_NONE; + + switch (id) { + case LP8788_INT_CHG_INPUT_STATE: + case LP8788_INT_CHG_STATE: + case LP8788_INT_EOC: + case LP8788_INT_BATT_LOW: + case LP8788_INT_NO_BATT: + power_supply_changed(&pchg->charger); + power_supply_changed(&pchg->battery); + break; + default: + break; + } + + /* report charger dectection event if used */ + if (!pdata) + goto irq_handled; + + if (pdata->charger_event && id == LP8788_INT_CHG_INPUT_STATE) + schedule_work(&pchg->charger_work); + +irq_handled: + return IRQ_HANDLED; +} + +static int lp8788_set_irqs(struct platform_device *pdev, + struct lp8788_charger *pchg, const char *name) +{ + struct resource *r; + struct irq_domain *irqdm = pchg->lp->irqdm; + int irq_start; + int irq_end; + int virq; + int nr_irq; + int i; + int ret; + + /* no error even if no irq resource */ + r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, name); + if (!r) + return 0; + + irq_start = r->start; + irq_end = r->end; + + for (i = irq_start; i <= irq_end; i++) { + nr_irq = pchg->num_irqs; + + virq = irq_create_mapping(irqdm, i); + pchg->irqs[nr_irq].virq = virq; + pchg->irqs[nr_irq].which = i; + pchg->num_irqs++; + + ret = request_threaded_irq(virq, NULL, + lp8788_charger_irq_thread, + 0, name, pchg); + if (ret) + break; + } + + if (i <= irq_end) + goto err_free_irq; + + return 0; + +err_free_irq: + for (i = 0; i < pchg->num_irqs; i++) + free_irq(pchg->irqs[i].virq, pchg); + return ret; +} + +static int lp8788_irq_register(struct platform_device *pdev, + struct lp8788_charger *pchg) +{ + struct lp8788 *lp = pchg->lp; + const char *name[] = { + LP8788_CHG_IRQ, LP8788_PRSW_IRQ, LP8788_BATT_IRQ + }; + int i; + int ret; + + INIT_WORK(&pchg->charger_work, lp8788_charger_event); + pchg->num_irqs = 0; + + for (i = 0; i < ARRAY_SIZE(name); i++) { + ret = lp8788_set_irqs(pdev, pchg, name[i]); + if (ret) { + dev_warn(lp->dev, "irq setup failed: %s\n", name[i]); + return ret; + } + } + + if (pchg->num_irqs > LP8788_MAX_CHG_IRQS) { + dev_err(lp->dev, "invalid total number of irqs: %d\n", + pchg->num_irqs); + return -EINVAL; + } + + + return 0; +} + +static void lp8788_irq_unregister(struct platform_device *pdev, + struct lp8788_charger *pchg) +{ + int i; + int irq; + + for (i = 0; i < pchg->num_irqs; i++) { + irq = pchg->irqs[i].virq; + if (!irq) + continue; + + free_irq(irq, pchg); + } +} + +static void lp8788_setup_adc_channel(struct lp8788_charger *pchg) +{ + struct lp8788_charger_platform_data *pdata = pchg->pdata; + struct device *dev = pchg->lp->dev; + struct iio_channel *chan; + enum lp8788_adc_id id; + const char *chan_name[LPADC_MAX] = { + [LPADC_VBATT_5P5] = "vbatt-5p5", + [LPADC_VBATT_6P0] = "vbatt-6p0", + [LPADC_VBATT_5P0] = "vbatt-5p0", + [LPADC_ADC1] = "adc1", + [LPADC_ADC2] = "adc2", + [LPADC_ADC3] = "adc3", + [LPADC_ADC4] = "adc4", + }; + + if (!pdata) + return; + + id = pdata->vbatt_adc; + switch (id) { + case LPADC_VBATT_5P5: + case LPADC_VBATT_6P0: + case LPADC_VBATT_5P0: + chan = iio_channel_get(NULL, chan_name[id]); + pchg->chan[LP8788_VBATT] = IS_ERR(chan) ? NULL : chan; + break; + default: + dev_err(dev, "invalid ADC id for VBATT: %d\n", id); + pchg->chan[LP8788_VBATT] = NULL; + break; + } + + id = pdata->batt_temp_adc; + switch (id) { + case LPADC_ADC1: + case LPADC_ADC2: + case LPADC_ADC3: + case LPADC_ADC4: + chan = iio_channel_get(NULL, chan_name[id]); + pchg->chan[LP8788_BATT_TEMP] = IS_ERR(chan) ? NULL : chan; + break; + default: + dev_err(dev, "invalid ADC id for BATT_TEMP : %d\n", id); + pchg->chan[LP8788_BATT_TEMP] = NULL; + break; + } +} + +static void lp8788_release_adc_channel(struct lp8788_charger *pchg) +{ + int i; + + for (i = 0; i < LP8788_NUM_CHG_ADC; i++) { + if (!pchg->chan[i]) + continue; + + iio_channel_release(pchg->chan[i]); + pchg->chan[i] = NULL; + } +} + +static ssize_t lp8788_show_charger_status(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct lp8788_charger *pchg = dev_get_drvdata(dev); + enum lp8788_charging_state state; + char *desc[LP8788_MAX_CHG_STATE] = { + [LP8788_OFF] = "CHARGER OFF", + [LP8788_WARM_UP] = "WARM UP", + [LP8788_LOW_INPUT] = "LOW INPUT STATE", + [LP8788_PRECHARGE] = "CHARGING - PRECHARGE", + [LP8788_CC] = "CHARGING - CC", + [LP8788_CV] = "CHARGING - CV", + [LP8788_MAINTENANCE] = "NO CHARGING - MAINTENANCE", + [LP8788_BATTERY_FAULT] = "BATTERY FAULT", + [LP8788_SYSTEM_SUPPORT] = "SYSTEM SUPPORT", + [LP8788_HIGH_CURRENT] = "HIGH CURRENT", + }; + u8 data; + + lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data); + state = (data & LP8788_CHG_STATE_M) >> LP8788_CHG_STATE_S; + + return scnprintf(buf, LP8788_BUF_SIZE, "%s\n", desc[state]); +} + +static ssize_t lp8788_show_eoc_time(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct lp8788_charger *pchg = dev_get_drvdata(dev); + char *stime[] = { "400ms", "5min", "10min", "15min", + "20min", "25min", "30min" "No timeout" }; + u8 val; + + lp8788_read_byte(pchg->lp, LP8788_CHG_EOC, &val); + val = (val & LP8788_CHG_EOC_TIME_M) >> LP8788_CHG_EOC_TIME_S; + + return scnprintf(buf, LP8788_BUF_SIZE, "End Of Charge Time: %s\n", + stime[val]); +} + +static ssize_t lp8788_show_eoc_level(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct lp8788_charger *pchg = dev_get_drvdata(dev); + char *abs_level[] = { "25mA", "49mA", "75mA", "98mA" }; + char *relative_level[] = { "5%", "10%", "15%", "20%" }; + char *level; + u8 val; + u8 mode; + + lp8788_read_byte(pchg->lp, LP8788_CHG_EOC, &val); + + mode = val & LP8788_CHG_EOC_MODE_M; + val = (val & LP8788_CHG_EOC_LEVEL_M) >> LP8788_CHG_EOC_LEVEL_S; + level = mode ? abs_level[val] : relative_level[val]; + + return scnprintf(buf, LP8788_BUF_SIZE, "End Of Charge Level: %s\n", + level); +} + +static DEVICE_ATTR(charger_status, S_IRUSR, lp8788_show_charger_status, NULL); +static DEVICE_ATTR(eoc_time, S_IRUSR, lp8788_show_eoc_time, NULL); +static DEVICE_ATTR(eoc_level, S_IRUSR, lp8788_show_eoc_level, NULL); + +static struct attribute *lp8788_charger_attr[] = { + &dev_attr_charger_status.attr, + &dev_attr_eoc_time.attr, + &dev_attr_eoc_level.attr, + NULL, +}; + +static const struct attribute_group lp8788_attr_group = { + .attrs = lp8788_charger_attr, +}; + +static __devinit int lp8788_charger_probe(struct platform_device *pdev) +{ + struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent); + struct lp8788_charger *pchg; + int ret; + + pchg = devm_kzalloc(lp->dev, sizeof(struct lp8788_charger), GFP_KERNEL); + if (!pchg) + return -ENOMEM; + + pchg->lp = lp; + pchg->pdata = lp->pdata ? lp->pdata->chg_pdata : NULL; + platform_set_drvdata(pdev, pchg); + + ret = lp8788_update_charger_params(pchg); + if (ret) + return ret; + + lp8788_setup_adc_channel(pchg); + + ret = lp8788_psy_register(pdev, pchg); + if (ret) + return ret; + + ret = sysfs_create_group(&pdev->dev.kobj, &lp8788_attr_group); + if (ret) { + lp8788_psy_unregister(pchg); + return ret; + } + + ret = lp8788_irq_register(pdev, pchg); + if (ret) + dev_warn(lp->dev, "failed to register charger irq: %d\n", ret); + + return 0; +} + +static int __devexit lp8788_charger_remove(struct platform_device *pdev) +{ + struct lp8788_charger *pchg = platform_get_drvdata(pdev); + + flush_work(&pchg->charger_work); + lp8788_irq_unregister(pdev, pchg); + sysfs_remove_group(&pdev->dev.kobj, &lp8788_attr_group); + lp8788_psy_unregister(pchg); + lp8788_release_adc_channel(pchg); + + return 0; +} + +static struct platform_driver lp8788_charger_driver = { + .probe = lp8788_charger_probe, + .remove = __devexit_p(lp8788_charger_remove), + .driver = { + .name = LP8788_DEV_CHARGER, + .owner = THIS_MODULE, + }, +}; +module_platform_driver(lp8788_charger_driver); + +MODULE_DESCRIPTION("TI LP8788 Charger Driver"); +MODULE_AUTHOR("Milo Kim"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:lp8788-charger"); -- cgit v1.2.3 From a830d28b48bf92944e57058e87d17cee5a7cd2a1 Mon Sep 17 00:00:00 2001 From: "Jett.Zhou" Date: Fri, 27 Jul 2012 16:27:16 +0800 Subject: power_supply: Enable battery-charger for 88pm860x There are charger and battery measurement feature for 88pm860x PMIC. For charger, it can support pre-charge with small current when battery is nearly exausted and then changed into fast-charge with CC&CV mode. For battery monitor, it can support battery measurement such as vbat,vsys,vchg and ibat etc,it can aslo accumulating the Coulomb value charged or discharged from battery based on Conlomb Counter, we use it to estimate battery capacity. Signed-off-by: Jett.Zhou Signed-off-by: Anton Vorontsov --- drivers/mfd/88pm860x-core.c | 22 +- drivers/power/88pm860x_battery.c | 1041 ++++++++++++++++++++++++++++++++++++++ drivers/power/88pm860x_charger.c | 746 +++++++++++++++++++++++++++ drivers/power/Kconfig | 12 + drivers/power/Makefile | 2 + include/linux/mfd/88pm860x.h | 84 ++- 6 files changed, 1905 insertions(+), 2 deletions(-) create mode 100644 drivers/power/88pm860x_battery.c create mode 100644 drivers/power/88pm860x_charger.c (limited to 'drivers') diff --git a/drivers/mfd/88pm860x-core.c b/drivers/mfd/88pm860x-core.c index d09918cf1b15..229cb2920089 100644 --- a/drivers/mfd/88pm860x-core.c +++ b/drivers/mfd/88pm860x-core.c @@ -18,6 +18,7 @@ #include #include #include +#include #define INT_STATUS_NUM 3 @@ -84,7 +85,8 @@ static struct resource battery_resources[] __devinitdata = { static struct resource charger_resources[] __devinitdata = { {PM8607_IRQ_CHG, PM8607_IRQ_CHG, "charger detect", IORESOURCE_IRQ,}, {PM8607_IRQ_CHG_DONE, PM8607_IRQ_CHG_DONE, "charging done", IORESOURCE_IRQ,}, - {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging timeout", IORESOURCE_IRQ,}, + {PM8607_IRQ_CHG_FAIL, PM8607_IRQ_CHG_FAIL, "charging timeout", IORESOURCE_IRQ,}, + {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging fault", IORESOURCE_IRQ,}, {PM8607_IRQ_GPADC1, PM8607_IRQ_GPADC1, "battery temperature", IORESOURCE_IRQ,}, {PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,}, {PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage", IORESOURCE_IRQ,}, @@ -155,10 +157,15 @@ static struct regulator_init_data preg_init_data = { .consumer_supplies = &preg_supply[0], }; +static struct regulator_bulk_data chg_desc_regulator_data[] = { + { .supply = "preg", }, +}; + static struct mfd_cell power_devs[] = { {"88pm860x-battery", -1,}, {"88pm860x-charger", -1,}, {"88pm860x-preg", -1,}, + {"charger-manager", -1,}, }; static struct mfd_cell rtc_devs[] = { @@ -791,6 +798,19 @@ static void __devinit device_power_init(struct pm860x_chip *chip, &preg_resources[0], chip->irq_base); if (ret < 0) dev_err(chip->dev, "Failed to add preg subdev\n"); + + if (pdata->chg_desc) { + pdata->chg_desc->charger_regulators = + &chg_desc_regulator_data[0]; + pdata->chg_desc->num_charger_regulators = + ARRAY_SIZE(chg_desc_regulator_data), + power_devs[3].platform_data = pdata->chg_desc; + power_devs[3].pdata_size = sizeof(*pdata->chg_desc); + ret = mfd_add_devices(chip->dev, 0, &power_devs[3], 1, + NULL, chip->irq_base); + if (ret < 0) + dev_err(chip->dev, "Failed to add chg-manager subdev\n"); + } } static void __devinit device_onkey_init(struct pm860x_chip *chip, diff --git a/drivers/power/88pm860x_battery.c b/drivers/power/88pm860x_battery.c new file mode 100644 index 000000000000..5e905f3db4bf --- /dev/null +++ b/drivers/power/88pm860x_battery.c @@ -0,0 +1,1041 @@ +/* + * Battery driver for Marvell 88PM860x PMIC + * + * Copyright (c) 2012 Marvell International Ltd. + * Author: Jett Zhou + * Haojian Zhuang + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* bit definitions of Status Query Interface 2 */ +#define STATUS2_CHG (1 << 2) +#define STATUS2_BAT (1 << 3) +#define STATUS2_VBUS (1 << 4) + +/* bit definitions of Measurement Enable 1 Register */ +#define MEAS1_TINT (1 << 3) +#define MEAS1_GP1 (1 << 5) + +/* bit definitions of Measurement Enable 3 Register */ +#define MEAS3_IBAT (1 << 0) +#define MEAS3_BAT_DET (1 << 1) +#define MEAS3_CC (1 << 2) + +/* bit definitions of Measurement Off Time Register */ +#define MEAS_OFF_SLEEP_EN (1 << 1) + +/* bit definitions of GPADC Bias Current 2 Register */ +#define GPBIAS2_GPADC1_SET (2 << 4) +/* GPADC1 Bias Current value in uA unit */ +#define GPBIAS2_GPADC1_UA ((GPBIAS2_GPADC1_SET >> 4) * 5 + 1) + +/* bit definitions of GPADC Misc 1 Register */ +#define GPMISC1_GPADC_EN (1 << 0) + +/* bit definitions of Charger Control 6 Register */ +#define CC6_BAT_DET_GPADC1 1 + +/* bit definitions of Coulomb Counter Reading Register */ +#define CCNT_AVG_SEL (4 << 3) + +/* bit definitions of RTC miscellaneous Register1 */ +#define RTC_SOC_5LSB (0x1F << 3) + +/* bit definitions of RTC Register1 */ +#define RTC_SOC_3MSB (0x7) + +/* bit definitions of Power up Log register */ +#define BAT_WU_LOG (1<<6) + +/* coulomb counter index */ +#define CCNT_POS1 0 +#define CCNT_POS2 1 +#define CCNT_NEG1 2 +#define CCNT_NEG2 3 +#define CCNT_SPOS 4 +#define CCNT_SNEG 5 + +/* OCV -- Open Circuit Voltage */ +#define OCV_MODE_ACTIVE 0 +#define OCV_MODE_SLEEP 1 + +/* Vbat range of CC for measuring Rbat */ +#define LOW_BAT_THRESHOLD 3600 +#define VBATT_RESISTOR_MIN 3800 +#define VBATT_RESISTOR_MAX 4100 + +/* TBAT for batt, TINT for chip itself */ +#define PM860X_TEMP_TINT (0) +#define PM860X_TEMP_TBAT (1) + +/* + * Battery temperature based on NTC resistor, defined + * corresponding resistor value -- Ohm / C degeree. + */ +#define TBAT_NEG_25D 127773 /* -25 */ +#define TBAT_NEG_10D 54564 /* -10 */ +#define TBAT_0D 32330 /* 0 */ +#define TBAT_10D 19785 /* 10 */ +#define TBAT_20D 12468 /* 20 */ +#define TBAT_30D 8072 /* 30 */ +#define TBAT_40D 5356 /* 40 */ + +struct pm860x_battery_info { + struct pm860x_chip *chip; + struct i2c_client *i2c; + struct device *dev; + + struct power_supply battery; + struct mutex lock; + int status; + int irq_cc; + int irq_batt; + int max_capacity; + int resistor; /* Battery Internal Resistor */ + int last_capacity; + int start_soc; + unsigned present:1; + unsigned temp_type:1; /* TINT or TBAT */ +}; + +struct ccnt { + unsigned long long int pos; + unsigned long long int neg; + unsigned int spos; + unsigned int sneg; + + int total_chg; /* mAh(3.6C) */ + int total_dischg; /* mAh(3.6C) */ +}; + +/* + * State of Charge. + * The first number is mAh(=3.6C), and the second number is percent point. + */ +int array_soc[][2] = { + {4170, 100}, {4154, 99}, {4136, 98}, {4122, 97}, {4107, 96}, + {4102, 95}, {4088, 94}, {4081, 93}, {4070, 92}, {4060, 91}, + {4053, 90}, {4044, 89}, {4035, 88}, {4028, 87}, {4019, 86}, + {4013, 85}, {4006, 84}, {3995, 83}, {3987, 82}, {3982, 81}, + {3976, 80}, {3968, 79}, {3962, 78}, {3954, 77}, {3946, 76}, + {3941, 75}, {3934, 74}, {3929, 73}, {3922, 72}, {3916, 71}, + {3910, 70}, {3904, 69}, {3898, 68}, {3892, 67}, {3887, 66}, + {3880, 65}, {3874, 64}, {3868, 63}, {3862, 62}, {3854, 61}, + {3849, 60}, {3843, 59}, {3840, 58}, {3833, 57}, {3829, 56}, + {3824, 55}, {3818, 54}, {3815, 53}, {3810, 52}, {3808, 51}, + {3804, 50}, {3801, 49}, {3798, 48}, {3796, 47}, {3792, 46}, + {3789, 45}, {3785, 44}, {3784, 43}, {3782, 42}, {3780, 41}, + {3777, 40}, {3776, 39}, {3774, 38}, {3772, 37}, {3771, 36}, + {3769, 35}, {3768, 34}, {3764, 33}, {3763, 32}, {3760, 31}, + {3760, 30}, {3754, 29}, {3750, 28}, {3749, 27}, {3744, 26}, + {3740, 25}, {3734, 24}, {3732, 23}, {3728, 22}, {3726, 21}, + {3720, 20}, {3716, 19}, {3709, 18}, {3703, 17}, {3698, 16}, + {3692, 15}, {3683, 14}, {3675, 13}, {3670, 12}, {3665, 11}, + {3661, 10}, {3649, 9}, {3637, 8}, {3622, 7}, {3609, 6}, + {3580, 5}, {3558, 4}, {3540, 3}, {3510, 2}, {3429, 1}, +}; + +static struct ccnt ccnt_data; + +/* + * register 1 bit[7:0] -- bit[11:4] of measured value of voltage + * register 0 bit[3:0] -- bit[3:0] of measured value of voltage + */ +static int measure_12bit_voltage(struct pm860x_battery_info *info, + int offset, int *data) +{ + unsigned char buf[2]; + int ret; + + ret = pm860x_bulk_read(info->i2c, offset, 2, buf); + if (ret < 0) + return ret; + + *data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f); + /* V_MEAS(mV) = data * 1.8 * 1000 / (2^12) */ + *data = ((*data & 0xfff) * 9 * 25) >> 9; + return 0; +} + +static int measure_vbatt(struct pm860x_battery_info *info, int state, + int *data) +{ + unsigned char buf[5]; + int ret; + + switch (state) { + case OCV_MODE_ACTIVE: + ret = measure_12bit_voltage(info, PM8607_VBAT_MEAS1, data); + if (ret) + return ret; + /* V_BATT_MEAS(mV) = value * 3 * 1.8 * 1000 / (2^12) */ + *data *= 3; + break; + case OCV_MODE_SLEEP: + /* + * voltage value of VBATT in sleep mode is saved in different + * registers. + * bit[11:10] -- bit[7:6] of LDO9(0x18) + * bit[9:8] -- bit[7:6] of LDO8(0x17) + * bit[7:6] -- bit[7:6] of LDO7(0x16) + * bit[5:4] -- bit[7:6] of LDO6(0x15) + * bit[3:0] -- bit[7:4] of LDO5(0x14) + */ + ret = pm860x_bulk_read(info->i2c, PM8607_LDO5, 5, buf); + if (ret < 0) + return ret; + ret = ((buf[4] >> 6) << 10) | ((buf[3] >> 6) << 8) + | ((buf[2] >> 6) << 6) | ((buf[1] >> 6) << 4) + | (buf[0] >> 4); + /* V_BATT_MEAS(mV) = data * 3 * 1.8 * 1000 / (2^12) */ + *data = ((*data & 0xff) * 27 * 25) >> 9; + break; + default: + return -EINVAL; + } + return 0; +} + +/* + * Return value is signed data. + * Negative value means discharging, and positive value means charging. + */ +static int measure_current(struct pm860x_battery_info *info, int *data) +{ + unsigned char buf[2]; + short s; + int ret; + + ret = pm860x_bulk_read(info->i2c, PM8607_IBAT_MEAS1, 2, buf); + if (ret < 0) + return ret; + + s = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff); + /* current(mA) = value * 0.125 */ + *data = s >> 3; + return 0; +} + +static int set_charger_current(struct pm860x_battery_info *info, int data, + int *old) +{ + int ret; + + if (data < 50 || data > 1600 || !old) + return -EINVAL; + + data = ((data - 50) / 50) & 0x1f; + *old = pm860x_reg_read(info->i2c, PM8607_CHG_CTRL2); + *old = (*old & 0x1f) * 50 + 50; + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f, data); + if (ret < 0) + return ret; + return 0; +} + +static int read_ccnt(struct pm860x_battery_info *info, int offset, + int *ccnt) +{ + unsigned char buf[2]; + int ret; + + ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7, offset & 7); + if (ret < 0) + goto out; + ret = pm860x_bulk_read(info->i2c, PM8607_CCNT_MEAS1, 2, buf); + if (ret < 0) + goto out; + *ccnt = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff); + return 0; +out: + return ret; +} + +static int calc_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt) +{ + unsigned int sum; + int ret; + int data; + + ret = read_ccnt(info, CCNT_POS1, &data); + if (ret) + goto out; + sum = data & 0xffff; + ret = read_ccnt(info, CCNT_POS2, &data); + if (ret) + goto out; + sum |= (data & 0xffff) << 16; + ccnt->pos += sum; + + ret = read_ccnt(info, CCNT_NEG1, &data); + if (ret) + goto out; + sum = data & 0xffff; + ret = read_ccnt(info, CCNT_NEG2, &data); + if (ret) + goto out; + sum |= (data & 0xffff) << 16; + sum = ~sum + 1; /* since it's negative */ + ccnt->neg += sum; + + ret = read_ccnt(info, CCNT_SPOS, &data); + if (ret) + goto out; + ccnt->spos += data; + ret = read_ccnt(info, CCNT_SNEG, &data); + if (ret) + goto out; + + /* + * charge(mAh) = count * 1.6984 * 1e(-8) + * = count * 16984 * 1.024 * 1.024 * 1.024 / (2 ^ 40) + * = count * 18236 / (2 ^ 40) + */ + ccnt->total_chg = (int) ((ccnt->pos * 18236) >> 40); + ccnt->total_dischg = (int) ((ccnt->neg * 18236) >> 40); + return 0; +out: + return ret; +} + +static int clear_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt) +{ + int data; + + memset(ccnt, 0, sizeof(*ccnt)); + /* read to clear ccnt */ + read_ccnt(info, CCNT_POS1, &data); + read_ccnt(info, CCNT_POS2, &data); + read_ccnt(info, CCNT_NEG1, &data); + read_ccnt(info, CCNT_NEG2, &data); + read_ccnt(info, CCNT_SPOS, &data); + read_ccnt(info, CCNT_SNEG, &data); + return 0; +} + +/* Calculate Open Circuit Voltage */ +static int calc_ocv(struct pm860x_battery_info *info, int *ocv) +{ + int ret; + int i; + int data; + int vbatt_avg; + int vbatt_sum; + int ibatt_avg; + int ibatt_sum; + + if (!ocv) + return -EINVAL; + + for (i = 0, ibatt_sum = 0, vbatt_sum = 0; i < 10; i++) { + ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data); + if (ret) + goto out; + vbatt_sum += data; + ret = measure_current(info, &data); + if (ret) + goto out; + ibatt_sum += data; + } + vbatt_avg = vbatt_sum / 10; + ibatt_avg = ibatt_sum / 10; + + mutex_lock(&info->lock); + if (info->present) + *ocv = vbatt_avg - ibatt_avg * info->resistor / 1000; + else + *ocv = vbatt_avg; + mutex_unlock(&info->lock); + dev_dbg(info->dev, "VBAT average:%d, OCV:%d\n", vbatt_avg, *ocv); + return 0; +out: + return ret; +} + +/* Calculate State of Charge (percent points) */ +static int calc_soc(struct pm860x_battery_info *info, int state, int *soc) +{ + int i; + int ocv; + int count; + int ret = -EINVAL; + + if (!soc) + return -EINVAL; + + switch (state) { + case OCV_MODE_ACTIVE: + ret = calc_ocv(info, &ocv); + break; + case OCV_MODE_SLEEP: + ret = measure_vbatt(info, OCV_MODE_SLEEP, &ocv); + break; + } + if (ret) + return ret; + + count = ARRAY_SIZE(array_soc); + if (ocv < array_soc[count - 1][0]) { + *soc = 0; + return 0; + } + + for (i = 0; i < count; i++) { + if (ocv >= array_soc[i][0]) { + *soc = array_soc[i][1]; + break; + } + } + return 0; +} + +static irqreturn_t pm860x_coulomb_handler(int irq, void *data) +{ + struct pm860x_battery_info *info = data; + + calc_ccnt(info, &ccnt_data); + return IRQ_HANDLED; +} + +static irqreturn_t pm860x_batt_handler(int irq, void *data) +{ + struct pm860x_battery_info *info = data; + int ret; + + mutex_lock(&info->lock); + ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2); + if (ret & STATUS2_BAT) { + info->present = 1; + info->temp_type = PM860X_TEMP_TBAT; + } else { + info->present = 0; + info->temp_type = PM860X_TEMP_TINT; + } + mutex_unlock(&info->lock); + /* clear ccnt since battery is attached or dettached */ + clear_ccnt(info, &ccnt_data); + return IRQ_HANDLED; +} + +static void pm860x_init_battery(struct pm860x_battery_info *info) +{ + unsigned char buf[2]; + int ret; + int data; + int bat_remove; + int soc; + + /* measure enable on GPADC1 */ + data = MEAS1_GP1; + if (info->temp_type == PM860X_TEMP_TINT) + data |= MEAS1_TINT; + ret = pm860x_set_bits(info->i2c, PM8607_MEAS_EN1, data, data); + if (ret) + goto out; + + /* measure enable on IBAT, BAT_DET, CC. IBAT is depend on CC. */ + data = MEAS3_IBAT | MEAS3_BAT_DET | MEAS3_CC; + ret = pm860x_set_bits(info->i2c, PM8607_MEAS_EN3, data, data); + if (ret) + goto out; + + /* measure disable CC in sleep time */ + ret = pm860x_reg_write(info->i2c, PM8607_MEAS_OFF_TIME1, 0x82); + if (ret) + goto out; + ret = pm860x_reg_write(info->i2c, PM8607_MEAS_OFF_TIME2, 0x6c); + if (ret) + goto out; + + /* enable GPADC */ + ret = pm860x_set_bits(info->i2c, PM8607_GPADC_MISC1, + GPMISC1_GPADC_EN, GPMISC1_GPADC_EN); + if (ret < 0) + goto out; + + /* detect battery via GPADC1 */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL6, + CC6_BAT_DET_GPADC1, CC6_BAT_DET_GPADC1); + if (ret < 0) + goto out; + + ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7 << 3, + CCNT_AVG_SEL); + if (ret < 0) + goto out; + + /* set GPADC1 bias */ + ret = pm860x_set_bits(info->i2c, PM8607_GP_BIAS2, 0xF << 4, + GPBIAS2_GPADC1_SET); + if (ret < 0) + goto out; + + /* check whether battery present) */ + mutex_lock(&info->lock); + ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2); + if (ret < 0) { + mutex_unlock(&info->lock); + goto out; + } + if (ret & STATUS2_BAT) { + info->present = 1; + info->temp_type = PM860X_TEMP_TBAT; + } else { + info->present = 0; + info->temp_type = PM860X_TEMP_TINT; + } + mutex_unlock(&info->lock); + + calc_soc(info, OCV_MODE_ACTIVE, &soc); + + data = pm860x_reg_read(info->i2c, PM8607_POWER_UP_LOG); + bat_remove = data & BAT_WU_LOG; + + dev_dbg(info->dev, "battery wake up? %s\n", + bat_remove != 0 ? "yes" : "no"); + + /* restore SOC from RTC domain register */ + if (bat_remove == 0) { + buf[0] = pm860x_reg_read(info->i2c, PM8607_RTC_MISC2); + buf[1] = pm860x_reg_read(info->i2c, PM8607_RTC1); + data = ((buf[1] & 0x3) << 5) | ((buf[0] >> 3) & 0x1F); + if (data > soc + 15) + info->start_soc = soc; + else if (data < soc - 15) + info->start_soc = soc; + else + info->start_soc = data; + dev_dbg(info->dev, "soc_rtc %d, soc_ocv :%d\n", data, soc); + } else { + pm860x_set_bits(info->i2c, PM8607_POWER_UP_LOG, + BAT_WU_LOG, BAT_WU_LOG); + info->start_soc = soc; + } + info->last_capacity = info->start_soc; + dev_dbg(info->dev, "init soc : %d\n", info->last_capacity); +out: + return; +} + +static void set_temp_threshold(struct pm860x_battery_info *info, + int min, int max) +{ + int data; + + /* (tmp << 8) / 1800 */ + if (min <= 0) + data = 0; + else + data = (min << 8) / 1800; + pm860x_reg_write(info->i2c, PM8607_GPADC1_HIGHTH, data); + dev_dbg(info->dev, "TEMP_HIGHTH : min: %d, 0x%x\n", min, data); + + if (max <= 0) + data = 0xff; + else + data = (max << 8) / 1800; + pm860x_reg_write(info->i2c, PM8607_GPADC1_LOWTH, data); + dev_dbg(info->dev, "TEMP_LOWTH:max : %d, 0x%x\n", max, data); +} + +static int measure_temp(struct pm860x_battery_info *info, int *data) +{ + int ret; + int temp; + int min; + int max; + + if (info->temp_type == PM860X_TEMP_TINT) { + ret = measure_12bit_voltage(info, PM8607_TINT_MEAS1, data); + if (ret) + return ret; + *data = (*data - 884) * 1000 / 3611; + } else { + ret = measure_12bit_voltage(info, PM8607_GPADC1_MEAS1, data); + if (ret) + return ret; + /* meausered Vtbat(mV) / Ibias_current(11uA)*/ + *data = (*data * 1000) / GPBIAS2_GPADC1_UA; + + if (*data > TBAT_NEG_25D) { + temp = -30; /* over cold , suppose -30 roughly */ + max = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, 0, max); + } else if (*data > TBAT_NEG_10D) { + temp = -15; /* -15 degree, code */ + max = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, 0, max); + } else if (*data > TBAT_0D) { + temp = -5; /* -5 degree */ + min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, min, max); + } else if (*data > TBAT_10D) { + temp = 5; /* in range of (0, 10) */ + min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, min, max); + } else if (*data > TBAT_20D) { + temp = 15; /* in range of (10, 20) */ + min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, min, max); + } else if (*data > TBAT_30D) { + temp = 25; /* in range of (20, 30) */ + min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, min, max); + } else if (*data > TBAT_40D) { + temp = 35; /* in range of (30, 40) */ + min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000; + max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, min, max); + } else { + min = TBAT_40D * GPBIAS2_GPADC1_UA / 1000; + set_temp_threshold(info, min, 0); + temp = 45; /* over heat ,suppose 45 roughly */ + } + + dev_dbg(info->dev, "temp_C:%d C,temp_mv:%d mv\n", temp, *data); + *data = temp; + } + return 0; +} + +static int calc_resistor(struct pm860x_battery_info *info) +{ + int vbatt_sum1; + int vbatt_sum2; + int chg_current; + int ibatt_sum1; + int ibatt_sum2; + int data; + int ret; + int i; + + ret = measure_current(info, &data); + /* make sure that charging is launched by data > 0 */ + if (ret || data < 0) + goto out; + + ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data); + if (ret) + goto out; + /* calculate resistor only in CC charge mode */ + if (data < VBATT_RESISTOR_MIN || data > VBATT_RESISTOR_MAX) + goto out; + + /* current is saved */ + if (set_charger_current(info, 500, &chg_current)) + goto out; + + /* + * set charge current as 500mA, wait about 500ms till charging + * process is launched and stable with the newer charging current. + */ + msleep(500); + + for (i = 0, vbatt_sum1 = 0, ibatt_sum1 = 0; i < 10; i++) { + ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data); + if (ret) + goto out_meas; + vbatt_sum1 += data; + ret = measure_current(info, &data); + if (ret) + goto out_meas; + + if (data < 0) + ibatt_sum1 = ibatt_sum1 - data; /* discharging */ + else + ibatt_sum1 = ibatt_sum1 + data; /* charging */ + } + + if (set_charger_current(info, 100, &ret)) + goto out_meas; + /* + * set charge current as 100mA, wait about 500ms till charging + * process is launched and stable with the newer charging current. + */ + msleep(500); + + for (i = 0, vbatt_sum2 = 0, ibatt_sum2 = 0; i < 10; i++) { + ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data); + if (ret) + goto out_meas; + vbatt_sum2 += data; + ret = measure_current(info, &data); + if (ret) + goto out_meas; + + if (data < 0) + ibatt_sum2 = ibatt_sum2 - data; /* discharging */ + else + ibatt_sum2 = ibatt_sum2 + data; /* charging */ + } + + /* restore current setting */ + if (set_charger_current(info, chg_current, &ret)) + goto out_meas; + + if ((vbatt_sum1 > vbatt_sum2) && (ibatt_sum1 > ibatt_sum2) && + (ibatt_sum2 > 0)) { + /* calculate resistor in discharging case */ + data = 1000 * (vbatt_sum1 - vbatt_sum2) + / (ibatt_sum1 - ibatt_sum2); + if ((data - info->resistor > 0) && + (data - info->resistor < info->resistor)) + info->resistor = data; + if ((info->resistor - data > 0) && + (info->resistor - data < data)) + info->resistor = data; + } + return 0; + +out_meas: + set_charger_current(info, chg_current, &ret); +out: + return -EINVAL; +} + +static int calc_capacity(struct pm860x_battery_info *info, int *cap) +{ + int ret; + int data; + int ibat; + int cap_ocv = 0; + int cap_cc = 0; + + ret = calc_ccnt(info, &ccnt_data); + if (ret) + goto out; +soc: + data = info->max_capacity * info->start_soc / 100; + if (ccnt_data.total_dischg - ccnt_data.total_chg <= data) { + cap_cc = + data + ccnt_data.total_chg - ccnt_data.total_dischg; + } else { + clear_ccnt(info, &ccnt_data); + calc_soc(info, OCV_MODE_ACTIVE, &info->start_soc); + dev_dbg(info->dev, "restart soc = %d !\n", + info->start_soc); + goto soc; + } + + cap_cc = cap_cc * 100 / info->max_capacity; + if (cap_cc < 0) + cap_cc = 0; + else if (cap_cc > 100) + cap_cc = 100; + + dev_dbg(info->dev, "%s, last cap : %d", __func__, + info->last_capacity); + + ret = measure_current(info, &ibat); + if (ret) + goto out; + /* Calculate the capacity when discharging(ibat < 0) */ + if (ibat < 0) { + ret = calc_soc(info, OCV_MODE_ACTIVE, &cap_ocv); + if (ret) + cap_ocv = info->last_capacity; + ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data); + if (ret) + goto out; + if (data <= LOW_BAT_THRESHOLD) { + /* choose the lower capacity value to report + * between vbat and CC when vbat < 3.6v; + * than 3.6v; + */ + *cap = min(cap_ocv, cap_cc); + } else { + /* when detect vbat > 3.6v, but cap_cc < 15,and + * cap_ocv is 10% larger than cap_cc, we can think + * CC have some accumulation error, switch to OCV + * to estimate capacity; + * */ + if (cap_cc < 15 && cap_ocv - cap_cc > 10) + *cap = cap_ocv; + else + *cap = cap_cc; + } + /* when discharging, make sure current capacity + * is lower than last*/ + if (*cap > info->last_capacity) + *cap = info->last_capacity; + } else { + *cap = cap_cc; + } + info->last_capacity = *cap; + + dev_dbg(info->dev, "%s, cap_ocv:%d cap_cc:%d, cap:%d\n", + (ibat < 0) ? "discharging" : "charging", + cap_ocv, cap_cc, *cap); + /* + * store the current capacity to RTC domain register, + * after next power up , it will be restored. + */ + pm860x_set_bits(info->i2c, PM8607_RTC_MISC2, RTC_SOC_5LSB, + (*cap & 0x1F) << 3); + pm860x_set_bits(info->i2c, PM8607_RTC1, RTC_SOC_3MSB, + ((*cap >> 5) & 0x3)); + return 0; +out: + return ret; +} + +static void pm860x_external_power_changed(struct power_supply *psy) +{ + struct pm860x_battery_info *info; + + info = container_of(psy, struct pm860x_battery_info, battery); + calc_resistor(info); +} + +static int pm860x_batt_get_prop(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct pm860x_battery_info *info = dev_get_drvdata(psy->dev->parent); + int data; + int ret; + + switch (psp) { + case POWER_SUPPLY_PROP_PRESENT: + val->intval = info->present; + break; + case POWER_SUPPLY_PROP_CAPACITY: + ret = calc_capacity(info, &data); + if (ret) + return ret; + if (data < 0) + data = 0; + else if (data > 100) + data = 100; + /* return 100 if battery is not attached */ + if (!info->present) + data = 100; + val->intval = data; + break; + case POWER_SUPPLY_PROP_TECHNOLOGY: + val->intval = POWER_SUPPLY_TECHNOLOGY_LION; + break; + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + /* return real vbatt Voltage */ + ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data); + if (ret) + return ret; + val->intval = data * 1000; + break; + case POWER_SUPPLY_PROP_VOLTAGE_AVG: + /* return Open Circuit Voltage (not measured voltage) */ + ret = calc_ocv(info, &data); + if (ret) + return ret; + val->intval = data * 1000; + break; + case POWER_SUPPLY_PROP_CURRENT_NOW: + ret = measure_current(info, &data); + if (ret) + return ret; + val->intval = data; + break; + case POWER_SUPPLY_PROP_TEMP: + if (info->present) { + ret = measure_temp(info, &data); + if (ret) + return ret; + data *= 10; + } else { + /* Fake Temp 25C Without Battery */ + data = 250; + } + val->intval = data; + break; + default: + return -ENODEV; + } + return 0; +} + +static int pm860x_batt_set_prop(struct power_supply *psy, + enum power_supply_property psp, + const union power_supply_propval *val) +{ + struct pm860x_battery_info *info = dev_get_drvdata(psy->dev->parent); + + switch (psp) { + case POWER_SUPPLY_PROP_CHARGE_FULL: + clear_ccnt(info, &ccnt_data); + info->start_soc = 100; + dev_dbg(info->dev, "chg done, update soc = %d\n", + info->start_soc); + break; + default: + return -EPERM; + } + + return 0; +} + + +static enum power_supply_property pm860x_batt_props[] = { + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_CAPACITY, + POWER_SUPPLY_PROP_TECHNOLOGY, + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_VOLTAGE_AVG, + POWER_SUPPLY_PROP_CURRENT_NOW, + POWER_SUPPLY_PROP_TEMP, +}; + +static __devinit int pm860x_battery_probe(struct platform_device *pdev) +{ + struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); + struct pm860x_battery_info *info; + struct pm860x_power_pdata *pdata; + int ret; + + info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); + if (!info) + return -ENOMEM; + + info->irq_cc = platform_get_irq(pdev, 0); + if (info->irq_cc <= 0) { + dev_err(&pdev->dev, "No IRQ resource!\n"); + ret = -EINVAL; + goto out; + } + + info->irq_batt = platform_get_irq(pdev, 1); + if (info->irq_batt <= 0) { + dev_err(&pdev->dev, "No IRQ resource!\n"); + ret = -EINVAL; + goto out; + } + + info->chip = chip; + info->i2c = + (chip->id == CHIP_PM8607) ? chip->client : chip->companion; + info->dev = &pdev->dev; + info->status = POWER_SUPPLY_STATUS_UNKNOWN; + pdata = pdev->dev.platform_data; + + mutex_init(&info->lock); + platform_set_drvdata(pdev, info); + + pm860x_init_battery(info); + + info->battery.name = "battery-monitor"; + info->battery.type = POWER_SUPPLY_TYPE_BATTERY; + info->battery.properties = pm860x_batt_props; + info->battery.num_properties = ARRAY_SIZE(pm860x_batt_props); + info->battery.get_property = pm860x_batt_get_prop; + info->battery.set_property = pm860x_batt_set_prop; + info->battery.external_power_changed = pm860x_external_power_changed; + + if (pdata && pdata->max_capacity) + info->max_capacity = pdata->max_capacity; + else + info->max_capacity = 1500; /* set default capacity */ + if (pdata && pdata->resistor) + info->resistor = pdata->resistor; + else + info->resistor = 300; /* set default internal resistor */ + + ret = power_supply_register(&pdev->dev, &info->battery); + if (ret) + goto out; + info->battery.dev->parent = &pdev->dev; + + ret = request_threaded_irq(info->irq_cc, NULL, + pm860x_coulomb_handler, IRQF_ONESHOT, + "coulomb", info); + if (ret < 0) { + dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", + info->irq_cc, ret); + goto out_reg; + } + + ret = request_threaded_irq(info->irq_batt, NULL, pm860x_batt_handler, + IRQF_ONESHOT, "battery", info); + if (ret < 0) { + dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", + info->irq_batt, ret); + goto out_coulomb; + } + + + return 0; + +out_coulomb: + free_irq(info->irq_cc, info); +out_reg: + power_supply_unregister(&info->battery); +out: + kfree(info); + return ret; +} + +static int __devexit pm860x_battery_remove(struct platform_device *pdev) +{ + struct pm860x_battery_info *info = platform_get_drvdata(pdev); + + power_supply_unregister(&info->battery); + free_irq(info->irq_batt, info); + free_irq(info->irq_cc, info); + kfree(info); + platform_set_drvdata(pdev, NULL); + return 0; +} + +#ifdef CONFIG_PM_SLEEP +static int pm860x_battery_suspend(struct device *dev) +{ + struct platform_device *pdev = to_platform_device(dev); + struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); + + if (device_may_wakeup(dev)) + chip->wakeup_flag |= 1 << PM8607_IRQ_CC; + return 0; +} + +static int pm860x_battery_resume(struct device *dev) +{ + struct platform_device *pdev = to_platform_device(dev); + struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); + + if (device_may_wakeup(dev)) + chip->wakeup_flag &= ~(1 << PM8607_IRQ_CC); + return 0; +} +#endif + +static SIMPLE_DEV_PM_OPS(pm860x_battery_pm_ops, + pm860x_battery_suspend, pm860x_battery_resume); + +static struct platform_driver pm860x_battery_driver = { + .driver = { + .name = "88pm860x-battery", + .owner = THIS_MODULE, + .pm = &pm860x_battery_pm_ops, + }, + .probe = pm860x_battery_probe, + .remove = __devexit_p(pm860x_battery_remove), +}; +module_platform_driver(pm860x_battery_driver); + +MODULE_DESCRIPTION("Marvell 88PM860x Battery driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/power/88pm860x_charger.c b/drivers/power/88pm860x_charger.c new file mode 100644 index 000000000000..4fd7614ee839 --- /dev/null +++ b/drivers/power/88pm860x_charger.c @@ -0,0 +1,746 @@ +/* + * Battery driver for Marvell 88PM860x PMIC + * + * Copyright (c) 2012 Marvell International Ltd. + * Author: Jett Zhou + * Haojian Zhuang + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* bit definitions of Status Query Interface 2 */ +#define STATUS2_CHG (1 << 2) + +/* bit definitions of Reset Out Register */ +#define RESET_SW_PD (1 << 7) + +/* bit definitions of PreReg 1 */ +#define PREREG1_90MA (0x0) +#define PREREG1_180MA (0x1) +#define PREREG1_450MA (0x4) +#define PREREG1_540MA (0x5) +#define PREREG1_1350MA (0xE) +#define PREREG1_VSYS_4_5V (3 << 4) + +/* bit definitions of Charger Control 1 Register */ +#define CC1_MODE_OFF (0) +#define CC1_MODE_PRECHARGE (1) +#define CC1_MODE_FASTCHARGE (2) +#define CC1_MODE_PULSECHARGE (3) +#define CC1_ITERM_20MA (0 << 2) +#define CC1_ITERM_60MA (2 << 2) +#define CC1_VFCHG_4_2V (9 << 4) + +/* bit definitions of Charger Control 2 Register */ +#define CC2_ICHG_100MA (0x1) +#define CC2_ICHG_500MA (0x9) +#define CC2_ICHG_1000MA (0x13) + +/* bit definitions of Charger Control 3 Register */ +#define CC3_180MIN_TIMEOUT (0x6 << 4) +#define CC3_270MIN_TIMEOUT (0x7 << 4) +#define CC3_360MIN_TIMEOUT (0xA << 4) +#define CC3_DISABLE_TIMEOUT (0xF << 4) + +/* bit definitions of Charger Control 4 Register */ +#define CC4_IPRE_40MA (7) +#define CC4_VPCHG_3_2V (3 << 4) +#define CC4_IFCHG_MON_EN (1 << 6) +#define CC4_BTEMP_MON_EN (1 << 7) + +/* bit definitions of Charger Control 6 Register */ +#define CC6_BAT_OV_EN (1 << 2) +#define CC6_BAT_UV_EN (1 << 3) +#define CC6_UV_VBAT_SET (0x3 << 6) /* 2.8v */ + +/* bit definitions of Charger Control 7 Register */ +#define CC7_BAT_REM_EN (1 << 3) +#define CC7_IFSM_EN (1 << 7) + +/* bit definitions of Measurement Enable 1 Register */ +#define MEAS1_VBAT (1 << 0) + +/* bit definitions of Measurement Enable 3 Register */ +#define MEAS3_IBAT_EN (1 << 0) +#define MEAS3_CC_EN (1 << 2) + +#define FSM_INIT 0 +#define FSM_DISCHARGE 1 +#define FSM_PRECHARGE 2 +#define FSM_FASTCHARGE 3 + +#define PRECHARGE_THRESHOLD 3100 +#define POWEROFF_THRESHOLD 3400 +#define CHARGE_THRESHOLD 4000 +#define DISCHARGE_THRESHOLD 4180 + +/* over-temperature on PM8606 setting */ +#define OVER_TEMP_FLAG (1 << 6) +#define OVTEMP_AUTORECOVER (1 << 3) + +/* over-voltage protect on vchg setting mv */ +#define VCHG_NORMAL_LOW 4200 +#define VCHG_NORMAL_CHECK 5800 +#define VCHG_NORMAL_HIGH 6000 +#define VCHG_OVP_LOW 5500 + +struct pm860x_charger_info { + struct pm860x_chip *chip; + struct i2c_client *i2c; + struct i2c_client *i2c_8606; + struct device *dev; + + struct power_supply usb; + struct mutex lock; + int irq_nums; + int irq[7]; + unsigned state:3; /* fsm state */ + unsigned online:1; /* usb charger */ + unsigned present:1; /* battery present */ + unsigned allowed:1; +}; + +static char *pm860x_supplied_to[] = { + "battery-monitor", +}; + +static int measure_vchg(struct pm860x_charger_info *info, int *data) +{ + unsigned char buf[2]; + int ret = 0; + + ret = pm860x_bulk_read(info->i2c, PM8607_VCHG_MEAS1, 2, buf); + if (ret < 0) + return ret; + + *data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f); + /* V_BATT_MEAS(mV) = value * 5 * 1.8 * 1000 / (2^12) */ + *data = ((*data & 0xfff) * 9 * 125) >> 9; + + dev_dbg(info->dev, "%s, vchg: %d mv\n", __func__, *data); + + return ret; +} + +static void set_vchg_threshold(struct pm860x_charger_info *info, + int min, int max) +{ + int data; + + /* (tmp << 8) * / 5 / 1800 */ + if (min <= 0) + data = 0; + else + data = (min << 5) / 1125; + pm860x_reg_write(info->i2c, PM8607_VCHG_LOWTH, data); + dev_dbg(info->dev, "VCHG_LOWTH:%dmv, 0x%x\n", min, data); + + if (max <= 0) + data = 0xff; + else + data = (max << 5) / 1125; + pm860x_reg_write(info->i2c, PM8607_VCHG_HIGHTH, data); + dev_dbg(info->dev, "VCHG_HIGHTH:%dmv, 0x%x\n", max, data); + +} + +static void set_vbatt_threshold(struct pm860x_charger_info *info, + int min, int max) +{ + int data; + + /* (tmp << 8) * 3 / 1800 */ + if (min <= 0) + data = 0; + else + data = (min << 5) / 675; + pm860x_reg_write(info->i2c, PM8607_VBAT_LOWTH, data); + dev_dbg(info->dev, "VBAT Min:%dmv, LOWTH:0x%x\n", min, data); + + if (max <= 0) + data = 0xff; + else + data = (max << 5) / 675; + pm860x_reg_write(info->i2c, PM8607_VBAT_HIGHTH, data); + dev_dbg(info->dev, "VBAT Max:%dmv, HIGHTH:0x%x\n", max, data); + + return; +} + +static int start_precharge(struct pm860x_charger_info *info) +{ + int ret; + + dev_dbg(info->dev, "Start Pre-charging!\n"); + set_vbatt_threshold(info, 0, 0); + + ret = pm860x_reg_write(info->i2c_8606, PM8606_PREREGULATORA, + PREREG1_1350MA | PREREG1_VSYS_4_5V); + if (ret < 0) + goto out; + /* stop charging */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3, + CC1_MODE_OFF); + if (ret < 0) + goto out; + /* set 270 minutes timeout */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL3, (0xf << 4), + CC3_270MIN_TIMEOUT); + if (ret < 0) + goto out; + /* set precharge current, termination voltage, IBAT & TBAT monitor */ + ret = pm860x_reg_write(info->i2c, PM8607_CHG_CTRL4, + CC4_IPRE_40MA | CC4_VPCHG_3_2V | + CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN); + if (ret < 0) + goto out; + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL7, + CC7_BAT_REM_EN | CC7_IFSM_EN, + CC7_BAT_REM_EN | CC7_IFSM_EN); + if (ret < 0) + goto out; + /* trigger precharge */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3, + CC1_MODE_PRECHARGE); +out: + return ret; +} + +static int start_fastcharge(struct pm860x_charger_info *info) +{ + int ret; + + dev_dbg(info->dev, "Start Fast-charging!\n"); + + /* set fastcharge termination current & voltage, disable charging */ + ret = pm860x_reg_write(info->i2c, PM8607_CHG_CTRL1, + CC1_MODE_OFF | CC1_ITERM_60MA | + CC1_VFCHG_4_2V); + if (ret < 0) + goto out; + ret = pm860x_reg_write(info->i2c_8606, PM8606_PREREGULATORA, + PREREG1_540MA | PREREG1_VSYS_4_5V); + if (ret < 0) + goto out; + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f, + CC2_ICHG_500MA); + if (ret < 0) + goto out; + /* set 270 minutes timeout */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL3, (0xf << 4), + CC3_270MIN_TIMEOUT); + if (ret < 0) + goto out; + /* set IBAT & TBAT monitor */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL4, + CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN, + CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN); + if (ret < 0) + goto out; + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL6, + CC6_BAT_OV_EN | CC6_BAT_UV_EN | + CC6_UV_VBAT_SET, + CC6_BAT_OV_EN | CC6_BAT_UV_EN | + CC6_UV_VBAT_SET); + if (ret < 0) + goto out; + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL7, + CC7_BAT_REM_EN | CC7_IFSM_EN, + CC7_BAT_REM_EN | CC7_IFSM_EN); + if (ret < 0) + goto out; + /* launch fast-charge */ + ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3, + CC1_MODE_FASTCHARGE); + /* vchg threshold setting */ + set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_NORMAL_HIGH); +out: + return ret; +} + +static void stop_charge(struct pm860x_charger_info *info, int vbatt) +{ + dev_dbg(info->dev, "Stop charging!\n"); + pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3, CC1_MODE_OFF); + if (vbatt > CHARGE_THRESHOLD && info->online) + set_vbatt_threshold(info, CHARGE_THRESHOLD, 0); +} + +static void power_off_notification(struct pm860x_charger_info *info) +{ + dev_dbg(info->dev, "Power-off notification!\n"); +} + +static int set_charging_fsm(struct pm860x_charger_info *info) +{ + struct power_supply *psy; + union power_supply_propval data; + unsigned char fsm_state[][16] = { "init", "discharge", "precharge", + "fastcharge", + }; + int ret; + int vbatt; + + psy = power_supply_get_by_name(pm860x_supplied_to[0]); + if (!psy) + return -EINVAL; + ret = psy->get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW, &data); + if (ret) + return ret; + vbatt = data.intval / 1000; + + ret = psy->get_property(psy, POWER_SUPPLY_PROP_PRESENT, &data); + if (ret) + return ret; + + mutex_lock(&info->lock); + info->present = data.intval; + + dev_dbg(info->dev, "Entering FSM:%s, Charger:%s, Battery:%s, " + "Allowed:%d\n", + &fsm_state[info->state][0], + (info->online) ? "online" : "N/A", + (info->present) ? "present" : "N/A", info->allowed); + dev_dbg(info->dev, "set_charging_fsm:vbatt:%d(mV)\n", vbatt); + + switch (info->state) { + case FSM_INIT: + if (info->online && info->present && info->allowed) { + if (vbatt < PRECHARGE_THRESHOLD) { + info->state = FSM_PRECHARGE; + start_precharge(info); + } else if (vbatt > DISCHARGE_THRESHOLD) { + info->state = FSM_DISCHARGE; + stop_charge(info, vbatt); + } else if (vbatt < DISCHARGE_THRESHOLD) { + info->state = FSM_FASTCHARGE; + start_fastcharge(info); + } + } else { + if (vbatt < POWEROFF_THRESHOLD) { + power_off_notification(info); + } else { + info->state = FSM_DISCHARGE; + stop_charge(info, vbatt); + } + } + break; + case FSM_PRECHARGE: + if (info->online && info->present && info->allowed) { + if (vbatt > PRECHARGE_THRESHOLD) { + info->state = FSM_FASTCHARGE; + start_fastcharge(info); + } + } else { + info->state = FSM_DISCHARGE; + stop_charge(info, vbatt); + } + break; + case FSM_FASTCHARGE: + if (info->online && info->present && info->allowed) { + if (vbatt < PRECHARGE_THRESHOLD) { + info->state = FSM_PRECHARGE; + start_precharge(info); + } + } else { + info->state = FSM_DISCHARGE; + stop_charge(info, vbatt); + } + break; + case FSM_DISCHARGE: + if (info->online && info->present && info->allowed) { + if (vbatt < PRECHARGE_THRESHOLD) { + info->state = FSM_PRECHARGE; + start_precharge(info); + } else if (vbatt < DISCHARGE_THRESHOLD) { + info->state = FSM_FASTCHARGE; + start_fastcharge(info); + } + } else { + if (vbatt < POWEROFF_THRESHOLD) + power_off_notification(info); + else if (vbatt > CHARGE_THRESHOLD && info->online) + set_vbatt_threshold(info, CHARGE_THRESHOLD, 0); + } + break; + default: + dev_warn(info->dev, "FSM meets wrong state:%d\n", + info->state); + break; + } + dev_dbg(info->dev, + "Out FSM:%s, Charger:%s, Battery:%s, Allowed:%d\n", + &fsm_state[info->state][0], + (info->online) ? "online" : "N/A", + (info->present) ? "present" : "N/A", info->allowed); + mutex_unlock(&info->lock); + + return 0; +} + +static irqreturn_t pm860x_charger_handler(int irq, void *data) +{ + struct pm860x_charger_info *info = data; + int ret; + + mutex_lock(&info->lock); + ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2); + if (ret < 0) { + mutex_unlock(&info->lock); + goto out; + } + if (ret & STATUS2_CHG) { + info->online = 1; + info->allowed = 1; + } else { + info->online = 0; + info->allowed = 0; + } + mutex_unlock(&info->lock); + dev_dbg(info->dev, "%s, Charger:%s, Allowed:%d\n", __func__, + (info->online) ? "online" : "N/A", info->allowed); + + set_charging_fsm(info); + + power_supply_changed(&info->usb); +out: + return IRQ_HANDLED; +} + +static irqreturn_t pm860x_temp_handler(int irq, void *data) +{ + struct power_supply *psy; + struct pm860x_charger_info *info = data; + union power_supply_propval temp; + int value; + int ret; + + psy = power_supply_get_by_name(pm860x_supplied_to[0]); + if (!psy) + goto out; + ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &temp); + if (ret) + goto out; + value = temp.intval / 10; + + mutex_lock(&info->lock); + /* Temperature < -10 C or >40 C, Will not allow charge */ + if (value < -10 || value > 40) + info->allowed = 0; + else + info->allowed = 1; + dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed); + mutex_unlock(&info->lock); + + set_charging_fsm(info); +out: + return IRQ_HANDLED; +} + +static irqreturn_t pm860x_exception_handler(int irq, void *data) +{ + struct pm860x_charger_info *info = data; + + mutex_lock(&info->lock); + info->allowed = 0; + mutex_unlock(&info->lock); + dev_dbg(info->dev, "%s, irq: %d\n", __func__, irq); + + set_charging_fsm(info); + return IRQ_HANDLED; +} + +static irqreturn_t pm860x_done_handler(int irq, void *data) +{ + struct pm860x_charger_info *info = data; + struct power_supply *psy; + union power_supply_propval val; + int ret; + int vbatt; + + mutex_lock(&info->lock); + /* pre-charge done, will transimit to fast-charge stage */ + if (info->state == FSM_PRECHARGE) { + info->allowed = 1; + goto out; + } + /* + * Fast charge done, delay to read + * the correct status of CHG_DET. + */ + mdelay(5); + info->allowed = 0; + psy = power_supply_get_by_name(pm860x_supplied_to[0]); + if (!psy) + goto out; + ret = psy->get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW, &val); + if (ret) + goto out; + vbatt = val.intval / 1000; + /* + * CHG_DONE interrupt is faster than CHG_DET interrupt when + * plug in/out usb, So we can not rely on info->online, we + * need check pm8607 status register to check usb is online + * or not, then we can decide it is real charge done + * automatically or it is triggered by usb plug out; + */ + ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2); + if (ret < 0) + goto out; + if (vbatt > CHARGE_THRESHOLD && ret & STATUS2_CHG) + psy->set_property(psy, POWER_SUPPLY_PROP_CHARGE_FULL, &val); + +out: + mutex_unlock(&info->lock); + dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed); + set_charging_fsm(info); + + return IRQ_HANDLED; +} + +static irqreturn_t pm860x_vbattery_handler(int irq, void *data) +{ + struct pm860x_charger_info *info = data; + + mutex_lock(&info->lock); + + set_vbatt_threshold(info, 0, 0); + + if (info->present && info->online) + info->allowed = 1; + else + info->allowed = 0; + mutex_unlock(&info->lock); + dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed); + + set_charging_fsm(info); + + return IRQ_HANDLED; +} + +static irqreturn_t pm860x_vchg_handler(int irq, void *data) +{ + struct pm860x_charger_info *info = data; + int vchg = 0; + + if (info->present) + goto out; + + measure_vchg(info, &vchg); + + mutex_lock(&info->lock); + if (!info->online) { + int status; + /* check if over-temp on pm8606 or not */ + status = pm860x_reg_read(info->i2c_8606, PM8606_FLAGS); + if (status & OVER_TEMP_FLAG) { + /* clear over temp flag and set auto recover */ + pm860x_set_bits(info->i2c_8606, PM8606_FLAGS, + OVER_TEMP_FLAG, OVER_TEMP_FLAG); + pm860x_set_bits(info->i2c_8606, + PM8606_VSYS, + OVTEMP_AUTORECOVER, + OVTEMP_AUTORECOVER); + dev_dbg(info->dev, + "%s, pm8606 over-temp occure\n", __func__); + } + } + + if (vchg > VCHG_NORMAL_CHECK) { + set_vchg_threshold(info, VCHG_OVP_LOW, 0); + info->allowed = 0; + dev_dbg(info->dev, + "%s,pm8607 over-vchg occure,vchg = %dmv\n", + __func__, vchg); + } else if (vchg < VCHG_OVP_LOW) { + set_vchg_threshold(info, VCHG_NORMAL_LOW, + VCHG_NORMAL_HIGH); + info->allowed = 1; + dev_dbg(info->dev, + "%s,pm8607 over-vchg recover,vchg = %dmv\n", + __func__, vchg); + } + mutex_unlock(&info->lock); + + dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed); + set_charging_fsm(info); +out: + return IRQ_HANDLED; +} + +static int pm860x_usb_get_prop(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct pm860x_charger_info *info = + dev_get_drvdata(psy->dev->parent); + + switch (psp) { + case POWER_SUPPLY_PROP_STATUS: + if (info->state == FSM_FASTCHARGE || + info->state == FSM_PRECHARGE) + val->intval = POWER_SUPPLY_STATUS_CHARGING; + else + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; + break; + case POWER_SUPPLY_PROP_ONLINE: + val->intval = info->online; + break; + default: + return -ENODEV; + } + return 0; +} + +static enum power_supply_property pm860x_usb_props[] = { + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_ONLINE, +}; + +static int pm860x_init_charger(struct pm860x_charger_info *info) +{ + int ret; + + ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2); + if (ret < 0) + return ret; + + mutex_lock(&info->lock); + info->state = FSM_INIT; + if (ret & STATUS2_CHG) { + info->online = 1; + info->allowed = 1; + } else { + info->online = 0; + info->allowed = 0; + } + mutex_unlock(&info->lock); + + set_charging_fsm(info); + return 0; +} + +struct pm860x_irq_desc { + const char *name; + irqreturn_t (*handler)(int irq, void *data); +} pm860x_irq_descs[] = { + { "usb supply detect", pm860x_charger_handler }, + { "charge done", pm860x_done_handler }, + { "charge timeout", pm860x_exception_handler }, + { "charge fault", pm860x_exception_handler }, + { "temperature", pm860x_temp_handler }, + { "vbatt", pm860x_vbattery_handler }, + { "vchg", pm860x_vchg_handler }, +}; + +static __devinit int pm860x_charger_probe(struct platform_device *pdev) +{ + struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); + struct pm860x_charger_info *info; + int ret; + int count; + int i; + int j; + + info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); + if (!info) + return -ENOMEM; + + count = pdev->num_resources; + for (i = 0, j = 0; i < count; i++) { + info->irq[j] = platform_get_irq(pdev, i); + if (info->irq[j] < 0) + continue; + j++; + } + info->irq_nums = j; + + info->chip = chip; + info->i2c = + (chip->id == CHIP_PM8607) ? chip->client : chip->companion; + info->i2c_8606 = + (chip->id == CHIP_PM8607) ? chip->companion : chip->client; + if (!info->i2c_8606) { + dev_err(&pdev->dev, "Missed I2C address of 88PM8606!\n"); + ret = -EINVAL; + goto out; + } + info->dev = &pdev->dev; + + /* set init value for the case we are not using battery */ + set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_OVP_LOW); + + mutex_init(&info->lock); + platform_set_drvdata(pdev, info); + + info->usb.name = "usb"; + info->usb.type = POWER_SUPPLY_TYPE_USB; + info->usb.supplied_to = pm860x_supplied_to; + info->usb.num_supplicants = ARRAY_SIZE(pm860x_supplied_to); + info->usb.properties = pm860x_usb_props; + info->usb.num_properties = ARRAY_SIZE(pm860x_usb_props); + info->usb.get_property = pm860x_usb_get_prop; + ret = power_supply_register(&pdev->dev, &info->usb); + if (ret) + goto out; + + pm860x_init_charger(info); + + for (i = 0; i < ARRAY_SIZE(info->irq); i++) { + ret = request_threaded_irq(info->irq[i], NULL, + pm860x_irq_descs[i].handler, + IRQF_ONESHOT, pm860x_irq_descs[i].name, info); + if (ret < 0) { + dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", + info->irq[i], ret); + goto out_irq; + } + } + return 0; + +out_irq: + while (--i >= 0) + free_irq(info->irq[i], info); +out: + kfree(info); + return ret; +} + +static int __devexit pm860x_charger_remove(struct platform_device *pdev) +{ + struct pm860x_charger_info *info = platform_get_drvdata(pdev); + int i; + + platform_set_drvdata(pdev, NULL); + power_supply_unregister(&info->usb); + free_irq(info->irq[0], info); + for (i = 0; i < info->irq_nums; i++) + free_irq(info->irq[i], info); + kfree(info); + return 0; +} + +static struct platform_driver pm860x_charger_driver = { + .driver = { + .name = "88pm860x-charger", + .owner = THIS_MODULE, + }, + .probe = pm860x_charger_probe, + .remove = __devexit_p(pm860x_charger_remove), +}; +module_platform_driver(pm860x_charger_driver); + +MODULE_DESCRIPTION("Marvell 88PM860x Charger driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig index 4ec2ff0ee4a2..9d8047735841 100644 --- a/drivers/power/Kconfig +++ b/drivers/power/Kconfig @@ -62,6 +62,12 @@ config TEST_POWER help This driver is used for testing. It's safe to say M here. +config BATTERY_88PM860X + tristate "Marvell 88PM860x battery driver" + depends on MFD_88PM860X + help + Say Y here to enable battery monitor for Marvell 88PM860x chip. + config BATTERY_DS2760 tristate "DS2760 battery driver (HP iPAQ & others)" depends on W1 && W1_SLAVE_DS2760 @@ -202,6 +208,12 @@ config BATTERY_S3C_ADC help Say Y here to enable support for iPAQ h1930/h1940/rx1950 battery +config CHARGER_88PM860X + tristate "Marvell 88PM860x Charger driver" + depends on MFD_88PM860X && BATTERY_88PM860X + help + Say Y here to enable charger for Marvell 88PM860x chip. + config CHARGER_PCF50633 tristate "NXP PCF50633 MBC" depends on MFD_PCF50633 diff --git a/drivers/power/Makefile b/drivers/power/Makefile index 587c5f174626..059c0a931526 100644 --- a/drivers/power/Makefile +++ b/drivers/power/Makefile @@ -14,6 +14,7 @@ obj-$(CONFIG_WM831X_POWER) += wm831x_power.o obj-$(CONFIG_WM8350_POWER) += wm8350_power.o obj-$(CONFIG_TEST_POWER) += test_power.o +obj-$(CONFIG_BATTERY_88PM860X) += 88pm860x_battery.o obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o obj-$(CONFIG_BATTERY_DS2780) += ds2780_battery.o obj-$(CONFIG_BATTERY_DS2781) += ds2781_battery.o @@ -31,6 +32,7 @@ obj-$(CONFIG_BATTERY_MAX17040) += max17040_battery.o obj-$(CONFIG_BATTERY_MAX17042) += max17042_battery.o obj-$(CONFIG_BATTERY_Z2) += z2_battery.o obj-$(CONFIG_BATTERY_S3C_ADC) += s3c_adc_battery.o +obj-$(CONFIG_CHARGER_88PM860X) += 88pm860x_charger.o obj-$(CONFIG_CHARGER_PCF50633) += pcf50633-charger.o obj-$(CONFIG_BATTERY_JZ4740) += jz4740-battery.o obj-$(CONFIG_BATTERY_INTEL_MID) += intel_mid_battery.o diff --git a/include/linux/mfd/88pm860x.h b/include/linux/mfd/88pm860x.h index 7b24943779fa..b7c5a3c27c98 100644 --- a/include/linux/mfd/88pm860x.h +++ b/include/linux/mfd/88pm860x.h @@ -55,6 +55,21 @@ enum { #define PM8606_DCM_BOOST (0x00) #define PM8606_PWM (0x01) +#define PM8607_MISC2 (0x42) + +/* Power Up Log Register */ +#define PM8607_POWER_UP_LOG (0x3F) + +/* Charger Control Registers */ +#define PM8607_CCNT (0x47) +#define PM8607_CHG_CTRL1 (0x48) +#define PM8607_CHG_CTRL2 (0x49) +#define PM8607_CHG_CTRL3 (0x4A) +#define PM8607_CHG_CTRL4 (0x4B) +#define PM8607_CHG_CTRL5 (0x4C) +#define PM8607_CHG_CTRL6 (0x4D) +#define PM8607_CHG_CTRL7 (0x4E) + /* Backlight Registers */ #define PM8606_WLED1A (0x02) #define PM8606_WLED1B (0x03) @@ -205,6 +220,71 @@ enum { #define PM8607_PD_PREBIAS (0x56) /* prebias time */ #define PM8607_GPADC_MISC1 (0x57) +/* bit definitions of MEAS_EN1*/ +#define PM8607_MEAS_EN1_VBAT (1 << 0) +#define PM8607_MEAS_EN1_VCHG (1 << 1) +#define PM8607_MEAS_EN1_VSYS (1 << 2) +#define PM8607_MEAS_EN1_TINT (1 << 3) +#define PM8607_MEAS_EN1_RFTMP (1 << 4) +#define PM8607_MEAS_EN1_TBAT (1 << 5) +#define PM8607_MEAS_EN1_GPADC2 (1 << 6) +#define PM8607_MEAS_EN1_GPADC3 (1 << 7) + +/* Battery Monitor Registers */ +#define PM8607_GP_BIAS2 (0x5A) +#define PM8607_VBAT_LOWTH (0x5B) +#define PM8607_VCHG_LOWTH (0x5C) +#define PM8607_VSYS_LOWTH (0x5D) +#define PM8607_TINT_LOWTH (0x5E) +#define PM8607_GPADC0_LOWTH (0x5F) +#define PM8607_GPADC1_LOWTH (0x60) +#define PM8607_GPADC2_LOWTH (0x61) +#define PM8607_GPADC3_LOWTH (0x62) +#define PM8607_VBAT_HIGHTH (0x63) +#define PM8607_VCHG_HIGHTH (0x64) +#define PM8607_VSYS_HIGHTH (0x65) +#define PM8607_TINT_HIGHTH (0x66) +#define PM8607_GPADC0_HIGHTH (0x67) +#define PM8607_GPADC1_HIGHTH (0x68) +#define PM8607_GPADC2_HIGHTH (0x69) +#define PM8607_GPADC3_HIGHTH (0x6A) +#define PM8607_IBAT_MEAS1 (0x6B) +#define PM8607_IBAT_MEAS2 (0x6C) +#define PM8607_VBAT_MEAS1 (0x6D) +#define PM8607_VBAT_MEAS2 (0x6E) +#define PM8607_VCHG_MEAS1 (0x6F) +#define PM8607_VCHG_MEAS2 (0x70) +#define PM8607_VSYS_MEAS1 (0x71) +#define PM8607_VSYS_MEAS2 (0x72) +#define PM8607_TINT_MEAS1 (0x73) +#define PM8607_TINT_MEAS2 (0x74) +#define PM8607_GPADC0_MEAS1 (0x75) +#define PM8607_GPADC0_MEAS2 (0x76) +#define PM8607_GPADC1_MEAS1 (0x77) +#define PM8607_GPADC1_MEAS2 (0x78) +#define PM8607_GPADC2_MEAS1 (0x79) +#define PM8607_GPADC2_MEAS2 (0x7A) +#define PM8607_GPADC3_MEAS1 (0x7B) +#define PM8607_GPADC3_MEAS2 (0x7C) +#define PM8607_CCNT_MEAS1 (0x95) +#define PM8607_CCNT_MEAS2 (0x96) +#define PM8607_VBAT_AVG (0x97) +#define PM8607_VCHG_AVG (0x98) +#define PM8607_VSYS_AVG (0x99) +#define PM8607_VBAT_MIN (0x9A) +#define PM8607_VCHG_MIN (0x9B) +#define PM8607_VSYS_MIN (0x9C) +#define PM8607_VBAT_MAX (0x9D) +#define PM8607_VCHG_MAX (0x9E) +#define PM8607_VSYS_MAX (0x9F) + +#define PM8607_GPADC_MISC2 (0x59) +#define PM8607_GPADC0_GP_BIAS_A0 (1 << 0) +#define PM8607_GPADC1_GP_BIAS_A1 (1 << 1) +#define PM8607_GPADC2_GP_BIAS_A2 (1 << 2) +#define PM8607_GPADC3_GP_BIAS_A3 (1 << 3) +#define PM8607_GPADC2_GP_BIAS_OUT2 (1 << 6) + /* RTC Control Registers */ #define PM8607_RTC1 (0xA0) #define PM8607_RTC_COUNTER1 (0xA1) @@ -370,7 +450,8 @@ struct pm860x_touch_pdata { }; struct pm860x_power_pdata { - unsigned fast_charge; /* charge current */ + int max_capacity; + int resistor; }; struct pm860x_platform_data { @@ -379,6 +460,7 @@ struct pm860x_platform_data { struct pm860x_rtc_pdata *rtc; struct pm860x_touch_pdata *touch; struct pm860x_power_pdata *power; + struct charger_desc *chg_desc; struct regulator_init_data *regulator; unsigned short companion_addr; /* I2C address of companion chip */ -- cgit v1.2.3 From f1ade35253f5026170a5fe17f87d0249f03629dd Mon Sep 17 00:00:00 2001 From: Anton Vorontsov Date: Wed, 22 Aug 2012 20:31:33 -0700 Subject: 88pm860x_battery and charger: Fix a few post-merge issues Sparse complains: CHECK drivers/power/88pm860x_battery.c drivers/power/88pm860x_battery.c:128:5: warning: symbol 'array_soc' was not declared. Should it be static? CHECK drivers/power/88pm860x_charger.c drivers/power/88pm860x_charger.c:640:3: warning: symbol 'pm860x_irq_descs' was not declared. Should it be static? CHECK drivers/mfd/88pm860x-core.c drivers/mfd/88pm860x-core.c:803:53: warning: incorrect type in assignment (different base types) drivers/mfd/88pm860x-core.c:803:53: expected struct charger_regulator *charger_regulators drivers/mfd/88pm860x-core.c:803:53: got struct regulator_bulk_data static [toplevel] * The issues are minor, except for the last one. We seemed to use 'regulator_bulk_data' struct (just as charger manager documentation wrongly tells us), but in real it should have been 'struct charger_regulator'. The only reason that it worked is because both 'supply' and 'regulator_name' struct members are the first in these structs. :-) Signed-off-by: Anton Vorontsov --- drivers/mfd/88pm860x-core.c | 4 ++-- drivers/power/88pm860x_battery.c | 2 +- drivers/power/88pm860x_charger.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'drivers') diff --git a/drivers/mfd/88pm860x-core.c b/drivers/mfd/88pm860x-core.c index 229cb2920089..76b5b7daf65d 100644 --- a/drivers/mfd/88pm860x-core.c +++ b/drivers/mfd/88pm860x-core.c @@ -157,8 +157,8 @@ static struct regulator_init_data preg_init_data = { .consumer_supplies = &preg_supply[0], }; -static struct regulator_bulk_data chg_desc_regulator_data[] = { - { .supply = "preg", }, +static struct charger_regulator chg_desc_regulator_data[] = { + { .regulator_name = "preg", }, }; static struct mfd_cell power_devs[] = { diff --git a/drivers/power/88pm860x_battery.c b/drivers/power/88pm860x_battery.c index 5e905f3db4bf..beed5ecf75e1 100644 --- a/drivers/power/88pm860x_battery.c +++ b/drivers/power/88pm860x_battery.c @@ -125,7 +125,7 @@ struct ccnt { * State of Charge. * The first number is mAh(=3.6C), and the second number is percent point. */ -int array_soc[][2] = { +static int array_soc[][2] = { {4170, 100}, {4154, 99}, {4136, 98}, {4122, 97}, {4107, 96}, {4102, 95}, {4088, 94}, {4081, 93}, {4070, 92}, {4060, 91}, {4053, 90}, {4044, 89}, {4035, 88}, {4028, 87}, {4019, 86}, diff --git a/drivers/power/88pm860x_charger.c b/drivers/power/88pm860x_charger.c index 4fd7614ee839..2dbeb1460901 100644 --- a/drivers/power/88pm860x_charger.c +++ b/drivers/power/88pm860x_charger.c @@ -632,7 +632,7 @@ static int pm860x_init_charger(struct pm860x_charger_info *info) return 0; } -struct pm860x_irq_desc { +static struct pm860x_irq_desc { const char *name; irqreturn_t (*handler)(int irq, void *data); } pm860x_irq_descs[] = { -- cgit v1.2.3 From 7c577c0d1ebed58648afb26e7c03f023fa82098e Mon Sep 17 00:00:00 2001 From: Fengguang Wu Date: Thu, 23 Aug 2012 19:42:29 +0800 Subject: lp8727_charger: Use IRQF_ONESHOT Generated by: scripts/coccinelle/misc/irqf_oneshot.cocci ERROR: Threaded IRQ with no primary handler requested without IRQF_ONESHOT Make sure threaded IRQs without a primary handler are always request with IRQF_ONESHOT Signed-off-by: Fengguang Wu Acked-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 4e37b269ac95..5c56d7e46993 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -255,7 +255,7 @@ static int lp8727_intr_config(struct lp8727_chg *pchg) return request_threaded_irq(pchg->client->irq, NULL, lp8727_isr_func, - IRQF_TRIGGER_FALLING, + IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lp8727_irq", pchg); } -- cgit v1.2.3 From a2778b0fe15d7adfe65b092ae842510c6c031370 Mon Sep 17 00:00:00 2001 From: Fengguang Wu Date: Thu, 23 Aug 2012 19:56:43 +0800 Subject: twl4030_charger: Use IRQF_ONESHOT Generated by: scripts/coccinelle/misc/irqf_oneshot.cocci Make sure threaded IRQs without a primary handler are always request with IRQF_ONESHOT Signed-off-by: Fengguang Wu Acked-by: Felipe Balbi Signed-off-by: Anton Vorontsov --- drivers/power/twl4030_charger.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'drivers') diff --git a/drivers/power/twl4030_charger.c b/drivers/power/twl4030_charger.c index 8d6bc6859a92..f9e70cf08199 100644 --- a/drivers/power/twl4030_charger.c +++ b/drivers/power/twl4030_charger.c @@ -534,7 +534,8 @@ static int __init twl4030_bci_probe(struct platform_device *pdev) } ret = request_threaded_irq(bci->irq_chg, NULL, - twl4030_charger_interrupt, 0, pdev->name, bci); + twl4030_charger_interrupt, IRQF_ONESHOT, pdev->name, + bci); if (ret < 0) { dev_err(&pdev->dev, "could not request irq %d, status %d\n", bci->irq_chg, ret); @@ -542,7 +543,7 @@ static int __init twl4030_bci_probe(struct platform_device *pdev) } ret = request_threaded_irq(bci->irq_bci, NULL, - twl4030_bci_interrupt, 0, pdev->name, bci); + twl4030_bci_interrupt, IRQF_ONESHOT, pdev->name, bci); if (ret < 0) { dev_err(&pdev->dev, "could not request irq %d, status %d\n", bci->irq_bci, ret); -- cgit v1.2.3 From 74727c571517692ae24d0562676f2c61a86b9090 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:22:46 +0000 Subject: lp8727_charger: Use devm_kzalloc() Use devm_kzalloc() rather than kzalloc()/kfree() to make allocating/freeing the private data simpler. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 5c56d7e46993..0d3cb1de2eb1 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -360,7 +360,7 @@ static int lp8727_register_psy(struct lp8727_chg *pchg) { struct lp8727_psy *psy; - psy = kzalloc(sizeof(*psy), GFP_KERNEL); + psy = devm_kzalloc(pchg->dev, sizeof(*psy), GFP_KERNEL); if (!psy) return -ENOMEM; @@ -405,7 +405,6 @@ err_psy_batt: err_psy_usb: power_supply_unregister(&psy->ac); err_psy_ac: - kfree(psy); return -EPERM; } @@ -419,7 +418,6 @@ static void lp8727_unregister_psy(struct lp8727_chg *pchg) power_supply_unregister(&psy->ac); power_supply_unregister(&psy->usb); power_supply_unregister(&psy->batt); - kfree(psy); } static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id) @@ -430,7 +428,7 @@ static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id) if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) return -EIO; - pchg = kzalloc(sizeof(*pchg), GFP_KERNEL); + pchg = devm_kzalloc(&cl->dev, sizeof(*pchg), GFP_KERNEL); if (!pchg) return -ENOMEM; @@ -462,7 +460,6 @@ static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id) return 0; error: - kfree(pchg); return ret; } @@ -474,7 +471,6 @@ static int __devexit lp8727_remove(struct i2c_client *cl) free_irq(pchg->client->irq, pchg); flush_workqueue(pchg->irqthread); destroy_workqueue(pchg->irqthread); - kfree(pchg); return 0; } -- cgit v1.2.3 From fb9adc5190d7605506ebc0e05351baa57365cc90 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:23:03 +0000 Subject: lp8727_charger: Cleanup _probe() and _remove() If the lp8727_register_psy() gets failed, registered interrupt handler should be freed, but this is not complete solution. It has still problem. Assume that the IRQ occurs while unregistering power supply devices. Then the ISR will access to freed IRQ. From Anton's opinion, it can be resolved if re-ordering the call sequence. Register power supplies first, then create interrupt handler. Then no need to free the IRQ in _probe(). Additionally goto statements can be removed because those can be replaced with return statements. The _remove() should be changed the sequence - in reverse order of _probe() Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 0d3cb1de2eb1..b2df11458d98 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -442,35 +442,33 @@ static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id) ret = lp8727_init_device(pchg); if (ret) { dev_err(pchg->dev, "i2c communication err: %d", ret); - goto error; + return ret; } - ret = lp8727_intr_config(pchg); + ret = lp8727_register_psy(pchg); if (ret) { - dev_err(pchg->dev, "irq handler err: %d", ret); - goto error; + dev_err(pchg->dev, "power supplies register err: %d", ret); + return ret; } - ret = lp8727_register_psy(pchg); + ret = lp8727_intr_config(pchg); if (ret) { - dev_err(pchg->dev, "power supplies register err: %d", ret); - goto error; + dev_err(pchg->dev, "irq handler err: %d", ret); + lp8727_unregister_psy(pchg); + return ret; } return 0; - -error: - return ret; } static int __devexit lp8727_remove(struct i2c_client *cl) { struct lp8727_chg *pchg = i2c_get_clientdata(cl); - lp8727_unregister_psy(pchg); free_irq(pchg->client->irq, pchg); flush_workqueue(pchg->irqthread); destroy_workqueue(pchg->irqthread); + lp8727_unregister_psy(pchg); return 0; } -- cgit v1.2.3 From 318cb389d020e3107979d7d794ab992e4bcd6665 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:23:12 +0000 Subject: lp8727_charger: Fix buggy code of NULL pdata LP8727 platform data is optional, so the driver should work even the platform data is NULL. To check the platform data, charging parameter data should be changed to the pointer type. Fix NULL point access problem when getting the battery properties. When the data is NULL, just return as invalid value. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 28 +++++++++++++++++++++------- include/linux/platform_data/lp8727.h | 7 ++++--- 2 files changed, 25 insertions(+), 10 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index b2df11458d98..7c19c0927f3a 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -170,20 +170,21 @@ static void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw) static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin) { + struct lp8727_platform_data *pdata = pchg->pdata; u8 devid = ID_NONE; u8 swctrl = SW_DM1_HiZ | SW_DP2_HiZ; switch (id) { case 0x5: devid = ID_TA; - pchg->chg_parm = &pchg->pdata->ac; + pchg->chg_parm = pdata ? pdata->ac : NULL; break; case 0xB: if (lp8727_is_dedicated_charger(pchg)) { - pchg->chg_parm = &pchg->pdata->ac; + pchg->chg_parm = pdata ? pdata->ac : NULL; devid = ID_DEDICATED_CHG; } else if (lp8727_is_usb_charger(pchg)) { - pchg->chg_parm = &pchg->pdata->usb; + pchg->chg_parm = pdata ? pdata->usb : NULL; devid = ID_USB_CHG; swctrl = SW_DM1_DM | SW_DP2_DP; } else if (vbusin) { @@ -295,6 +296,7 @@ static int lp8727_battery_get_property(struct power_supply *psy, union power_supply_propval *val) { struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent); + struct lp8727_platform_data *pdata = pchg->pdata; u8 read; switch (psp) { @@ -318,19 +320,31 @@ static int lp8727_battery_get_property(struct power_supply *psy, val->intval = POWER_SUPPLY_HEALTH_GOOD; break; case POWER_SUPPLY_PROP_PRESENT: - if (pchg->pdata->get_batt_present) + if (!pdata) + return -EINVAL; + + if (pdata->get_batt_present) val->intval = pchg->pdata->get_batt_present(); break; case POWER_SUPPLY_PROP_VOLTAGE_NOW: - if (pchg->pdata->get_batt_level) + if (!pdata) + return -EINVAL; + + if (pdata->get_batt_level) val->intval = pchg->pdata->get_batt_level(); break; case POWER_SUPPLY_PROP_CAPACITY: - if (pchg->pdata->get_batt_capacity) + if (!pdata) + return -EINVAL; + + if (pdata->get_batt_capacity) val->intval = pchg->pdata->get_batt_capacity(); break; case POWER_SUPPLY_PROP_TEMP: - if (pchg->pdata->get_batt_temp) + if (!pdata) + return -EINVAL; + + if (pdata->get_batt_temp) val->intval = pchg->pdata->get_batt_temp(); break; default: diff --git a/include/linux/platform_data/lp8727.h b/include/linux/platform_data/lp8727.h index ea98c6133d32..f4bcdd5f20a4 100644 --- a/include/linux/platform_data/lp8727.h +++ b/include/linux/platform_data/lp8727.h @@ -51,15 +51,16 @@ struct lp8727_chg_param { * @get_batt_level : get battery voltage (mV) * @get_batt_capacity : get battery capacity (%) * @get_batt_temp : get battery temperature - * @ac, @usb : charging parameters each charger type + * @ac : charging parameters for AC type charger + * @usb : charging parameters for USB type charger */ struct lp8727_platform_data { u8 (*get_batt_present)(void); u16 (*get_batt_level)(void); u8 (*get_batt_capacity)(void); u8 (*get_batt_temp)(void); - struct lp8727_chg_param ac; - struct lp8727_chg_param usb; + struct lp8727_chg_param *ac; + struct lp8727_chg_param *usb; }; #endif -- cgit v1.2.3 From 60fd57e06ed7ea13bc0bdf4cb5324d47039105ab Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:23:25 +0000 Subject: lp8727_charger: Add configurable debouce timer Debounce time is configurable in the platform side. If it is not defined, the default value is 270ms. Platform data is msec unit, and this time is converted to jiffies internally. The workqueue uses this jiffies time in the interrupt handling. So debounce_jiffies is added in the private data. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 12 +++++++++--- include/linux/platform_data/lp8727.h | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 7c19c0927f3a..1907b1f7953d 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -17,7 +17,7 @@ #include #include -#define DEBOUNCE_MSEC 270 +#define DEFAULT_DEBOUNCE_MSEC 270 /* Registers */ #define CTRL1 0x1 @@ -90,6 +90,7 @@ struct lp8727_chg { struct lp8727_psy *psy; struct lp8727_chg_param *chg_parm; enum lp8727_dev_id devid; + unsigned long debounce_jiffies; }; static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len) @@ -236,15 +237,18 @@ static void lp8727_delayed_func(struct work_struct *_work) static irqreturn_t lp8727_isr_func(int irq, void *ptr) { struct lp8727_chg *pchg = ptr; - unsigned long delay = msecs_to_jiffies(DEBOUNCE_MSEC); - queue_delayed_work(pchg->irqthread, &pchg->work, delay); + queue_delayed_work(pchg->irqthread, &pchg->work, + pchg->debounce_jiffies); return IRQ_HANDLED; } static int lp8727_intr_config(struct lp8727_chg *pchg) { + unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec : + DEFAULT_DEBOUNCE_MSEC; + INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func); pchg->irqthread = create_singlethread_workqueue("lp8727-irqthd"); @@ -253,6 +257,8 @@ static int lp8727_intr_config(struct lp8727_chg *pchg) return -ENOMEM; } + pchg->debounce_jiffies = msecs_to_jiffies(delay_msec); + return request_threaded_irq(pchg->client->irq, NULL, lp8727_isr_func, diff --git a/include/linux/platform_data/lp8727.h b/include/linux/platform_data/lp8727.h index f4bcdd5f20a4..54b77880ed67 100644 --- a/include/linux/platform_data/lp8727.h +++ b/include/linux/platform_data/lp8727.h @@ -53,6 +53,7 @@ struct lp8727_chg_param { * @get_batt_temp : get battery temperature * @ac : charging parameters for AC type charger * @usb : charging parameters for USB type charger + * @debounce_msec : interrupt debounce time */ struct lp8727_platform_data { u8 (*get_batt_present)(void); @@ -61,6 +62,7 @@ struct lp8727_platform_data { u8 (*get_batt_temp)(void); struct lp8727_chg_param *ac; struct lp8727_chg_param *usb; + unsigned int debounce_msec; }; #endif -- cgit v1.2.3 From 2a0925827be53db6252afadc79c657b14638ff4a Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:23:41 +0000 Subject: lp8727_charger: Remove unnecessary workqueue thread LP8727 has two IRQ threads. One is the I2C HW IRQ pin, the other is for delayed interrupt processing. But this delayed processing can be handled without additional single thread by using schedule_delayed_work() with jiffies time value. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 1907b1f7953d..c33532bbb9c7 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -85,7 +85,6 @@ struct lp8727_chg { struct i2c_client *client; struct mutex xfer_lock; struct delayed_work work; - struct workqueue_struct *irqthread; struct lp8727_platform_data *pdata; struct lp8727_psy *psy; struct lp8727_chg_param *chg_parm; @@ -238,9 +237,7 @@ static irqreturn_t lp8727_isr_func(int irq, void *ptr) { struct lp8727_chg *pchg = ptr; - queue_delayed_work(pchg->irqthread, &pchg->work, - pchg->debounce_jiffies); - + schedule_delayed_work(&pchg->work, pchg->debounce_jiffies); return IRQ_HANDLED; } @@ -251,12 +248,6 @@ static int lp8727_intr_config(struct lp8727_chg *pchg) INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func); - pchg->irqthread = create_singlethread_workqueue("lp8727-irqthd"); - if (!pchg->irqthread) { - dev_err(pchg->dev, "can not create thread for lp8727\n"); - return -ENOMEM; - } - pchg->debounce_jiffies = msecs_to_jiffies(delay_msec); return request_threaded_irq(pchg->client->irq, @@ -486,8 +477,6 @@ static int __devexit lp8727_remove(struct i2c_client *cl) struct lp8727_chg *pchg = i2c_get_clientdata(cl); free_irq(pchg->client->irq, pchg); - flush_workqueue(pchg->irqthread); - destroy_workqueue(pchg->irqthread); lp8727_unregister_psy(pchg); return 0; } -- cgit v1.2.3 From d71fda01610269e3aaedd451f8d3e34cdf550036 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:23:57 +0000 Subject: lp8727_charger: Clean up the interrupt handler For better understanding, function name is changed. (lp8727_intr_config() is replaced with lp8727_setup_irq().) The private IRQ number is set when the IRQ is allocated successfully. This data is used for releasing the IRQ on unloading the driver. Even the IRQ number is not defined, the driver should be operated. In this case, just return as 0. In additional function lp8727_release_irq(), the workqueue is canceled and the allocated IRQ is released. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index c33532bbb9c7..134ed5c7dde5 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -90,6 +90,7 @@ struct lp8727_chg { struct lp8727_chg_param *chg_parm; enum lp8727_dev_id devid; unsigned long debounce_jiffies; + int irq; }; static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len) @@ -241,21 +242,39 @@ static irqreturn_t lp8727_isr_func(int irq, void *ptr) return IRQ_HANDLED; } -static int lp8727_intr_config(struct lp8727_chg *pchg) +static int lp8727_setup_irq(struct lp8727_chg *pchg) { + int ret; + int irq = pchg->client->irq; unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec : DEFAULT_DEBOUNCE_MSEC; INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func); - pchg->debounce_jiffies = msecs_to_jiffies(delay_msec); + if (irq <= 0) { + dev_warn(pchg->dev, "invalid irq number: %d\n", irq); + return 0; + } - return request_threaded_irq(pchg->client->irq, - NULL, - lp8727_isr_func, + ret = request_threaded_irq(irq, NULL, lp8727_isr_func, IRQF_TRIGGER_FALLING | IRQF_ONESHOT, - "lp8727_irq", - pchg); + "lp8727_irq", pchg); + + if (ret) + return ret; + + pchg->irq = irq; + pchg->debounce_jiffies = msecs_to_jiffies(delay_msec); + + return 0; +} + +static void lp8727_release_irq(struct lp8727_chg *pchg) +{ + cancel_delayed_work_sync(&pchg->work); + + if (pchg->irq) + free_irq(pchg->irq, pchg); } static enum power_supply_property lp8727_charger_prop[] = { @@ -462,7 +481,7 @@ static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id) return ret; } - ret = lp8727_intr_config(pchg); + ret = lp8727_setup_irq(pchg); if (ret) { dev_err(pchg->dev, "irq handler err: %d", ret); lp8727_unregister_psy(pchg); @@ -476,7 +495,7 @@ static int __devexit lp8727_remove(struct i2c_client *cl) { struct lp8727_chg *pchg = i2c_get_clientdata(cl); - free_irq(pchg->client->irq, pchg); + lp8727_release_irq(pchg); lp8727_unregister_psy(pchg); return 0; } -- cgit v1.2.3 From 638555d2769b7dfb8654bae513cff295ea9a3837 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:24:09 +0000 Subject: lp8727_charger: Clear interrrupts at inital time To initialize the device, previous interrupts need to be cleared while loading the driver. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 134ed5c7dde5..b5567bc99000 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -17,6 +17,7 @@ #include #include +#define LP8788_NUM_INTREGS 2 #define DEFAULT_DEBOUNCE_MSEC 270 /* Registers */ @@ -136,6 +137,13 @@ static int lp8727_init_device(struct lp8727_chg *pchg) { u8 val; int ret; + u8 intstat[LP8788_NUM_INTREGS]; + + /* clear interrupts */ + ret = lp8727_read_bytes(pchg, INT1, intstat, LP8788_NUM_INTREGS); + if (ret) + return ret; + val = ID200_EN | ADC_EN | CP_EN; ret = lp8727_write_byte(pchg, CTRL1, val); -- cgit v1.2.3 From b1ad079680b24386d12aa99dd9afd4eb7093bbee Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:24:28 +0000 Subject: lp8727_charger: Fix code for getting battery temp For better understanding, use specific function and definitions rather than magic numbers. New enum type for die temperature is matched with hex codes. Specific temperature names are better than 0x1, 0x2 and 0x3. And lp8727_is_high_temperature() function has better readability than comparing raw register values. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index b5567bc99000..1a5f4b27e001 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -58,6 +58,7 @@ /* STATUS2 register */ #define TEMP_STAT (3 << 5) +#define TEMP_SHIFT 5 enum lp8727_dev_id { ID_NONE, @@ -75,6 +76,13 @@ enum lp8727_chg_stat { EOC, }; +enum lp8727_die_temp { + LP8788_TEMP_75C, + LP8788_TEMP_95C, + LP8788_TEMP_115C, + LP8788_TEMP_135C, +}; + struct lp8727_psy { struct power_supply ac; struct power_supply usb; @@ -315,12 +323,25 @@ static int lp8727_charger_get_property(struct power_supply *psy, return 0; } +static bool lp8727_is_high_temperature(enum lp8727_die_temp temp) +{ + switch (temp) { + case LP8788_TEMP_95C: + case LP8788_TEMP_115C: + case LP8788_TEMP_135C: + return true; + default: + return false; + } +} + static int lp8727_battery_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val) { struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent); struct lp8727_platform_data *pdata = pchg->pdata; + enum lp8727_die_temp temp; u8 read; switch (psp) { @@ -337,11 +358,11 @@ static int lp8727_battery_get_property(struct power_supply *psy, break; case POWER_SUPPLY_PROP_HEALTH: lp8727_read_byte(pchg, STATUS2, &read); - read = (read & TEMP_STAT) >> 5; - if (read >= 0x1 && read <= 0x3) - val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; - else - val->intval = POWER_SUPPLY_HEALTH_GOOD; + temp = (read & TEMP_STAT) >> TEMP_SHIFT; + + val->intval = lp8727_is_high_temperature(temp) ? + POWER_SUPPLY_HEALTH_OVERHEAT : + POWER_SUPPLY_HEALTH_GOOD; break; case POWER_SUPPLY_PROP_PRESENT: if (!pdata) -- cgit v1.2.3 From faaae9bbb1f91f8bcd03bd9976620ac08d8b4454 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:24:37 +0000 Subject: lp8727_charger: Use the definition rather than enum Enum lp8727_chg_state can be removed because only one charger status is used - EOC(End Of Charge). To check whether the EOC is reached or not, use simple comparison rather than shift-operation. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 1a5f4b27e001..32bf157f4815 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -55,6 +55,7 @@ #define CHGSTAT (3 << 4) #define CHPORT (1 << 6) #define DCPORT (1 << 7) +#define LP8727_STAT_EOC 0x30 /* STATUS2 register */ #define TEMP_STAT (3 << 5) @@ -69,13 +70,6 @@ enum lp8727_dev_id { ID_MAX, }; -enum lp8727_chg_stat { - PRECHG, - CC, - CV, - EOC, -}; - enum lp8727_die_temp { LP8788_TEMP_75C, LP8788_TEMP_95C, @@ -348,10 +342,10 @@ static int lp8727_battery_get_property(struct power_supply *psy, case POWER_SUPPLY_PROP_STATUS: if (lp8727_is_charger_attached(psy->name, pchg->devid)) { lp8727_read_byte(pchg, STATUS1, &read); - if (((read & CHGSTAT) >> 4) == EOC) - val->intval = POWER_SUPPLY_STATUS_FULL; - else - val->intval = POWER_SUPPLY_STATUS_CHARGING; + + val->intval = (read & CHGSTAT) == LP8727_STAT_EOC ? + POWER_SUPPLY_STATUS_FULL : + POWER_SUPPLY_STATUS_CHARGING; } else { val->intval = POWER_SUPPLY_STATUS_DISCHARGING; } -- cgit v1.2.3 From 6029719f08d2d21624504e326f50c68f89731353 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:24:51 +0000 Subject: lp8727_charger: Clean up lp8727 definitions All definitions should be unique, since they're in the gloabl namespace. So the prefix LP8727_ are added. Additionally, use BIT() macro for bit masks. Remove unnecessary definitions such as SW_DM1_U1 and SW_DP2_U2. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 130 +++++++++++++++++------------------ include/linux/platform_data/lp8727.h | 34 ++++----- 2 files changed, 81 insertions(+), 83 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 32bf157f4815..65c9840a37e1 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -21,53 +21,51 @@ #define DEFAULT_DEBOUNCE_MSEC 270 /* Registers */ -#define CTRL1 0x1 -#define CTRL2 0x2 -#define SWCTRL 0x3 -#define INT1 0x4 -#define INT2 0x5 -#define STATUS1 0x6 -#define STATUS2 0x7 -#define CHGCTRL2 0x9 +#define LP8727_CTRL1 0x1 +#define LP8727_CTRL2 0x2 +#define LP8727_SWCTRL 0x3 +#define LP8727_INT1 0x4 +#define LP8727_INT2 0x5 +#define LP8727_STATUS1 0x6 +#define LP8727_STATUS2 0x7 +#define LP8727_CHGCTRL2 0x9 /* CTRL1 register */ -#define CP_EN (1 << 0) -#define ADC_EN (1 << 1) -#define ID200_EN (1 << 4) +#define LP8727_CP_EN BIT(0) +#define LP8727_ADC_EN BIT(1) +#define LP8727_ID200_EN BIT(4) /* CTRL2 register */ -#define CHGDET_EN (1 << 1) -#define INT_EN (1 << 6) +#define LP8727_CHGDET_EN BIT(1) +#define LP8727_INT_EN BIT(6) /* SWCTRL register */ -#define SW_DM1_DM (0x0 << 0) -#define SW_DM1_U1 (0x1 << 0) -#define SW_DM1_HiZ (0x7 << 0) -#define SW_DP2_DP (0x0 << 3) -#define SW_DP2_U2 (0x1 << 3) -#define SW_DP2_HiZ (0x7 << 3) +#define LP8727_SW_DM1_DM (0x0 << 0) +#define LP8727_SW_DM1_HiZ (0x7 << 0) +#define LP8727_SW_DP2_DP (0x0 << 3) +#define LP8727_SW_DP2_HiZ (0x7 << 3) /* INT1 register */ -#define IDNO (0xF << 0) -#define VBUS (1 << 4) +#define LP8727_IDNO (0xF << 0) +#define LP8727_VBUS BIT(4) /* STATUS1 register */ -#define CHGSTAT (3 << 4) -#define CHPORT (1 << 6) -#define DCPORT (1 << 7) -#define LP8727_STAT_EOC 0x30 +#define LP8727_CHGSTAT (3 << 4) +#define LP8727_CHPORT BIT(6) +#define LP8727_DCPORT BIT(7) +#define LP8727_STAT_EOC 0x30 /* STATUS2 register */ -#define TEMP_STAT (3 << 5) -#define TEMP_SHIFT 5 +#define LP8727_TEMP_STAT (3 << 5) +#define LP8727_TEMP_SHIFT 5 enum lp8727_dev_id { - ID_NONE, - ID_TA, - ID_DEDICATED_CHG, - ID_USB_CHG, - ID_USB_DS, - ID_MAX, + LP8727_ID_NONE, + LP8727_ID_TA, + LP8727_ID_DEDICATED_CHG, + LP8727_ID_USB_CHG, + LP8727_ID_USB_DS, + LP8727_ID_MAX, }; enum lp8727_die_temp { @@ -127,12 +125,12 @@ static int lp8727_is_charger_attached(const char *name, int id) { if (name) { if (!strcmp(name, "ac")) - return (id == ID_TA || id == ID_DEDICATED_CHG) ? 1 : 0; + return (id == LP8727_ID_TA || id == LP8727_ID_DEDICATED_CHG) ? 1 : 0; else if (!strcmp(name, "usb")) - return (id == ID_USB_CHG) ? 1 : 0; + return (id == LP8727_ID_USB_CHG) ? 1 : 0; } - return (id >= ID_TA && id <= ID_USB_CHG) ? 1 : 0; + return (id >= LP8727_ID_TA && id <= LP8727_ID_USB_CHG) ? 1 : 0; } static int lp8727_init_device(struct lp8727_chg *pchg) @@ -142,18 +140,18 @@ static int lp8727_init_device(struct lp8727_chg *pchg) u8 intstat[LP8788_NUM_INTREGS]; /* clear interrupts */ - ret = lp8727_read_bytes(pchg, INT1, intstat, LP8788_NUM_INTREGS); + ret = lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS); if (ret) return ret; - val = ID200_EN | ADC_EN | CP_EN; - ret = lp8727_write_byte(pchg, CTRL1, val); + val = LP8727_ID200_EN | LP8727_ADC_EN | LP8727_CP_EN; + ret = lp8727_write_byte(pchg, LP8727_CTRL1, val); if (ret) return ret; - val = INT_EN | CHGDET_EN; - ret = lp8727_write_byte(pchg, CTRL2, val); + val = LP8727_INT_EN | LP8727_CHGDET_EN; + ret = lp8727_write_byte(pchg, LP8727_CTRL2, val); if (ret) return ret; @@ -163,48 +161,48 @@ static int lp8727_init_device(struct lp8727_chg *pchg) static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg) { u8 val; - lp8727_read_byte(pchg, STATUS1, &val); - return val & DCPORT; + lp8727_read_byte(pchg, LP8727_STATUS1, &val); + return val & LP8727_DCPORT; } static int lp8727_is_usb_charger(struct lp8727_chg *pchg) { u8 val; - lp8727_read_byte(pchg, STATUS1, &val); - return val & CHPORT; + lp8727_read_byte(pchg, LP8727_STATUS1, &val); + return val & LP8727_CHPORT; } static void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw) { - lp8727_write_byte(pchg, SWCTRL, sw); + lp8727_write_byte(pchg, LP8727_SWCTRL, sw); } static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin) { struct lp8727_platform_data *pdata = pchg->pdata; - u8 devid = ID_NONE; - u8 swctrl = SW_DM1_HiZ | SW_DP2_HiZ; + u8 devid = LP8727_ID_NONE; + u8 swctrl = LP8727_SW_DM1_HiZ | LP8727_SW_DP2_HiZ; switch (id) { case 0x5: - devid = ID_TA; + devid = LP8727_ID_TA; pchg->chg_parm = pdata ? pdata->ac : NULL; break; case 0xB: if (lp8727_is_dedicated_charger(pchg)) { pchg->chg_parm = pdata ? pdata->ac : NULL; - devid = ID_DEDICATED_CHG; + devid = LP8727_ID_DEDICATED_CHG; } else if (lp8727_is_usb_charger(pchg)) { pchg->chg_parm = pdata ? pdata->usb : NULL; - devid = ID_USB_CHG; - swctrl = SW_DM1_DM | SW_DP2_DP; + devid = LP8727_ID_USB_CHG; + swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP; } else if (vbusin) { - devid = ID_USB_DS; - swctrl = SW_DM1_DM | SW_DP2_DP; + devid = LP8727_ID_USB_DS; + swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP; } break; default: - devid = ID_NONE; + devid = LP8727_ID_NONE; pchg->chg_parm = NULL; break; } @@ -217,9 +215,9 @@ static void lp8727_enable_chgdet(struct lp8727_chg *pchg) { u8 val; - lp8727_read_byte(pchg, CTRL2, &val); - val |= CHGDET_EN; - lp8727_write_byte(pchg, CTRL2, val); + lp8727_read_byte(pchg, LP8727_CTRL2, &val); + val |= LP8727_CHGDET_EN; + lp8727_write_byte(pchg, LP8727_CTRL2, val); } static void lp8727_delayed_func(struct work_struct *_work) @@ -228,13 +226,13 @@ static void lp8727_delayed_func(struct work_struct *_work) struct lp8727_chg *pchg = container_of(_work, struct lp8727_chg, work.work); - if (lp8727_read_bytes(pchg, INT1, intstat, 2)) { + if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, 2)) { dev_err(pchg->dev, "can not read INT registers\n"); return; } - idno = intstat[0] & IDNO; - vbus = intstat[0] & VBUS; + idno = intstat[0] & LP8727_IDNO; + vbus = intstat[0] & LP8727_VBUS; lp8727_id_detection(pchg, idno, vbus); lp8727_enable_chgdet(pchg); @@ -341,9 +339,9 @@ static int lp8727_battery_get_property(struct power_supply *psy, switch (psp) { case POWER_SUPPLY_PROP_STATUS: if (lp8727_is_charger_attached(psy->name, pchg->devid)) { - lp8727_read_byte(pchg, STATUS1, &read); + lp8727_read_byte(pchg, LP8727_STATUS1, &read); - val->intval = (read & CHGSTAT) == LP8727_STAT_EOC ? + val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ? POWER_SUPPLY_STATUS_FULL : POWER_SUPPLY_STATUS_CHARGING; } else { @@ -351,8 +349,8 @@ static int lp8727_battery_get_property(struct power_supply *psy, } break; case POWER_SUPPLY_PROP_HEALTH: - lp8727_read_byte(pchg, STATUS2, &read); - temp = (read & TEMP_STAT) >> TEMP_SHIFT; + lp8727_read_byte(pchg, LP8727_STATUS2, &read); + temp = (read & LP8727_TEMP_STAT) >> LP8727_TEMP_SHIFT; val->intval = lp8727_is_high_temperature(temp) ? POWER_SUPPLY_HEALTH_OVERHEAT : @@ -404,7 +402,7 @@ static void lp8727_charger_changed(struct power_supply *psy) eoc_level = pchg->chg_parm->eoc_level; ichg = pchg->chg_parm->ichg; val = (ichg << 4) | eoc_level; - lp8727_write_byte(pchg, CHGCTRL2, val); + lp8727_write_byte(pchg, LP8727_CHGCTRL2, val); } } } diff --git a/include/linux/platform_data/lp8727.h b/include/linux/platform_data/lp8727.h index 54b77880ed67..a8b8dbc418cc 100644 --- a/include/linux/platform_data/lp8727.h +++ b/include/linux/platform_data/lp8727.h @@ -13,26 +13,26 @@ #define _LP8727_H enum lp8727_eoc_level { - EOC_5P, - EOC_10P, - EOC_16P, - EOC_20P, - EOC_25P, - EOC_33P, - EOC_50P, + LP8727_EOC_5P, + LP8727_EOC_10P, + LP8727_EOC_16P, + LP8727_EOC_20P, + LP8727_EOC_25P, + LP8727_EOC_33P, + LP8727_EOC_50P, }; enum lp8727_ichg { - ICHG_90mA, - ICHG_100mA, - ICHG_400mA, - ICHG_450mA, - ICHG_500mA, - ICHG_600mA, - ICHG_700mA, - ICHG_800mA, - ICHG_900mA, - ICHG_1000mA, + LP8727_ICHG_90mA, + LP8727_ICHG_100mA, + LP8727_ICHG_400mA, + LP8727_ICHG_450mA, + LP8727_ICHG_500mA, + LP8727_ICHG_600mA, + LP8727_ICHG_700mA, + LP8727_ICHG_800mA, + LP8727_ICHG_900mA, + LP8727_ICHG_1000mA, }; /** -- cgit v1.2.3 From 18763a36d0bae11a916ffe8fa58a26fd0b5eb115 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:25:07 +0000 Subject: lp8727_charger: Use specific definition Add new LP8727_ICHG_SHIFT definition and replace a magic number. Reuse definition for the size of interrupt register buffer. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 65c9840a37e1..254bbe07c269 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -59,6 +59,9 @@ #define LP8727_TEMP_STAT (3 << 5) #define LP8727_TEMP_SHIFT 5 +/* CHGCTRL2 register */ +#define LP8727_ICHG_SHIFT 4 + enum lp8727_dev_id { LP8727_ID_NONE, LP8727_ID_TA, @@ -222,11 +225,11 @@ static void lp8727_enable_chgdet(struct lp8727_chg *pchg) static void lp8727_delayed_func(struct work_struct *_work) { - u8 intstat[2], idno, vbus; + u8 intstat[LP8788_NUM_INTREGS], idno, vbus; struct lp8727_chg *pchg = container_of(_work, struct lp8727_chg, work.work); - if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, 2)) { + if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS)) { dev_err(pchg->dev, "can not read INT registers\n"); return; } @@ -401,7 +404,7 @@ static void lp8727_charger_changed(struct power_supply *psy) if (pchg->chg_parm) { eoc_level = pchg->chg_parm->eoc_level; ichg = pchg->chg_parm->ichg; - val = (ichg << 4) | eoc_level; + val = (ichg << LP8727_ICHG_SHIFT) | eoc_level; lp8727_write_byte(pchg, LP8727_CHGCTRL2, val); } } -- cgit v1.2.3 From 65272bac3a24162b7362eccf9a30a481f2139266 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:25:22 +0000 Subject: lp8727_charger: Clean up lp8727_is_charger_attached() Change return type to boolean. Remove unnecessary check routine for NULL string. (Power supply name is always valid when the function is executed.) Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 254bbe07c269..658518ba675b 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -124,16 +124,14 @@ static int lp8727_write_byte(struct lp8727_chg *pchg, u8 reg, u8 data) return ret; } -static int lp8727_is_charger_attached(const char *name, int id) +static bool lp8727_is_charger_attached(const char *name, int id) { - if (name) { - if (!strcmp(name, "ac")) - return (id == LP8727_ID_TA || id == LP8727_ID_DEDICATED_CHG) ? 1 : 0; - else if (!strcmp(name, "usb")) - return (id == LP8727_ID_USB_CHG) ? 1 : 0; - } + if (!strcmp(name, "ac")) + return id == LP8727_ID_TA || id == LP8727_ID_DEDICATED_CHG; + else if (!strcmp(name, "usb")) + return id == LP8727_ID_USB_CHG; - return (id >= LP8727_ID_TA && id <= LP8727_ID_USB_CHG) ? 1 : 0; + return id >= LP8727_ID_TA && id <= LP8727_ID_USB_CHG; } static int lp8727_init_device(struct lp8727_chg *pchg) -- cgit v1.2.3 From 70be1305294ad50a45a8f4134152d5d6e79c922e Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:25:34 +0000 Subject: lp8727_charger: Make lp8727_init_device() shorter Just return with lp8727_write_byte(), no need to check its value. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 658518ba675b..47cce4166c15 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -152,11 +152,7 @@ static int lp8727_init_device(struct lp8727_chg *pchg) return ret; val = LP8727_INT_EN | LP8727_CHGDET_EN; - ret = lp8727_write_byte(pchg, LP8727_CTRL2, val); - if (ret) - return ret; - - return 0; + return lp8727_write_byte(pchg, LP8727_CTRL2, val); } static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg) -- cgit v1.2.3 From 91a696330ce4137eef7ad5712c9269a52dc261d4 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:25:45 +0000 Subject: lp8727_charger: Make lp8727_ctrl_switch() inline Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 47cce4166c15..880bb7b98233 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -169,7 +169,7 @@ static int lp8727_is_usb_charger(struct lp8727_chg *pchg) return val & LP8727_CHPORT; } -static void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw) +static inline void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw) { lp8727_write_byte(pchg, LP8727_SWCTRL, sw); } -- cgit v1.2.3 From b59c93c0090d14556d988607e5bdb297d6e380a7 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:25:55 +0000 Subject: lp8727_charger: Make lp8727_charger_get_propery() simpler Charger has only one valid property - ONLINE. If the property is not ONLINE, then just return quickly. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 880bb7b98233..2507b2bb9df2 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -305,9 +305,10 @@ static int lp8727_charger_get_property(struct power_supply *psy, { struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent); - if (psp == POWER_SUPPLY_PROP_ONLINE) - val->intval = lp8727_is_charger_attached(psy->name, - pchg->devid); + if (psp != POWER_SUPPLY_PROP_ONLINE) + return -EINVAL; + + val->intval = lp8727_is_charger_attached(psy->name, pchg->devid); return 0; } -- cgit v1.2.3 From e06374b0d6055ef21e49d14d955eb9fc5088f707 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:26:06 +0000 Subject: lp8727_charger: Return if the battery is discharging If the charger is pulled out, just return as DISCHARGING. Then no need for additional 'else' statement. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 2507b2bb9df2..13a787adcbe9 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -336,15 +336,16 @@ static int lp8727_battery_get_property(struct power_supply *psy, switch (psp) { case POWER_SUPPLY_PROP_STATUS: - if (lp8727_is_charger_attached(psy->name, pchg->devid)) { - lp8727_read_byte(pchg, LP8727_STATUS1, &read); + if (!lp8727_is_charger_attached(psy->name, pchg->devid)) { + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; + return 0; + } + + lp8727_read_byte(pchg, LP8727_STATUS1, &read); - val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ? + val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ? POWER_SUPPLY_STATUS_FULL : POWER_SUPPLY_STATUS_CHARGING; - } else { - val->intval = POWER_SUPPLY_STATUS_DISCHARGING; - } break; case POWER_SUPPLY_PROP_HEALTH: lp8727_read_byte(pchg, LP8727_STATUS2, &read); -- cgit v1.2.3 From 20414e2e0e10c2152e44784599e6d66fae39d77c Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:26:22 +0000 Subject: lp8727_charger: Clean up lp8727_charger_changed() Declare a variable at one line. Just return when no charger exists to make code simpler. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 13a787adcbe9..19c21f9b6d3b 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -393,16 +393,20 @@ static int lp8727_battery_get_property(struct power_supply *psy, static void lp8727_charger_changed(struct power_supply *psy) { struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent); + u8 eoc_level; + u8 ichg; u8 val; - u8 eoc_level, ichg; - - if (lp8727_is_charger_attached(psy->name, pchg->devid)) { - if (pchg->chg_parm) { - eoc_level = pchg->chg_parm->eoc_level; - ichg = pchg->chg_parm->ichg; - val = (ichg << LP8727_ICHG_SHIFT) | eoc_level; - lp8727_write_byte(pchg, LP8727_CHGCTRL2, val); - } + + /* skip if no charger exists */ + if (!lp8727_is_charger_attached(psy->name, pchg->devid)) + return; + + /* update charging parameters */ + if (pchg->chg_parm) { + eoc_level = pchg->chg_parm->eoc_level; + ichg = pchg->chg_parm->ichg; + val = (ichg << LP8727_ICHG_SHIFT) | eoc_level; + lp8727_write_byte(pchg, LP8727_CHGCTRL2, val); } } -- cgit v1.2.3 From 5e00b9761b6e56096da8276a51484e5c53df72c6 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:26:33 +0000 Subject: lp8727_charger: Make some cosmetic changes in lp8727_delayed_func() Declare a variable at one line and align lines. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 19c21f9b6d3b..80e38aecc131 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -219,9 +219,11 @@ static void lp8727_enable_chgdet(struct lp8727_chg *pchg) static void lp8727_delayed_func(struct work_struct *_work) { - u8 intstat[LP8788_NUM_INTREGS], idno, vbus; - struct lp8727_chg *pchg = - container_of(_work, struct lp8727_chg, work.work); + struct lp8727_chg *pchg = container_of(_work, struct lp8727_chg, + work.work); + u8 intstat[LP8788_NUM_INTREGS]; + u8 idno; + u8 vbus; if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS)) { dev_err(pchg->dev, "can not read INT registers\n"); -- cgit v1.2.3 From 56d99ed93a52489da3c0f84424489390b6cb0312 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:26:49 +0000 Subject: lp8727_charger: Fix a typo - chg_parm to chg_param Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 80e38aecc131..c0a6feab79ec 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -91,7 +91,7 @@ struct lp8727_chg { struct delayed_work work; struct lp8727_platform_data *pdata; struct lp8727_psy *psy; - struct lp8727_chg_param *chg_parm; + struct lp8727_chg_param *chg_param; enum lp8727_dev_id devid; unsigned long debounce_jiffies; int irq; @@ -183,14 +183,14 @@ static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin) switch (id) { case 0x5: devid = LP8727_ID_TA; - pchg->chg_parm = pdata ? pdata->ac : NULL; + pchg->chg_param = pdata ? pdata->ac : NULL; break; case 0xB: if (lp8727_is_dedicated_charger(pchg)) { - pchg->chg_parm = pdata ? pdata->ac : NULL; + pchg->chg_param = pdata ? pdata->ac : NULL; devid = LP8727_ID_DEDICATED_CHG; } else if (lp8727_is_usb_charger(pchg)) { - pchg->chg_parm = pdata ? pdata->usb : NULL; + pchg->chg_param = pdata ? pdata->usb : NULL; devid = LP8727_ID_USB_CHG; swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP; } else if (vbusin) { @@ -200,7 +200,7 @@ static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin) break; default: devid = LP8727_ID_NONE; - pchg->chg_parm = NULL; + pchg->chg_param = NULL; break; } @@ -404,9 +404,9 @@ static void lp8727_charger_changed(struct power_supply *psy) return; /* update charging parameters */ - if (pchg->chg_parm) { - eoc_level = pchg->chg_parm->eoc_level; - ichg = pchg->chg_parm->ichg; + if (pchg->chg_param) { + eoc_level = pchg->chg_param->eoc_level; + ichg = pchg->chg_param->ichg; val = (ichg << LP8727_ICHG_SHIFT) | eoc_level; lp8727_write_byte(pchg, LP8727_CHGCTRL2, val); } -- cgit v1.2.3 From 1ae9df7d49e964b53f72254b20ab7eb713306396 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:27:04 +0000 Subject: lp8727_charger: Add description in the private data Add description and relocate data. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index c0a6feab79ec..9f25e151a36d 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -88,13 +88,17 @@ struct lp8727_chg { struct device *dev; struct i2c_client *client; struct mutex xfer_lock; - struct delayed_work work; - struct lp8727_platform_data *pdata; struct lp8727_psy *psy; - struct lp8727_chg_param *chg_param; + struct lp8727_platform_data *pdata; + + /* Charger Data */ enum lp8727_dev_id devid; - unsigned long debounce_jiffies; + struct lp8727_chg_param *chg_param; + + /* Interrupt Handling */ int irq; + struct delayed_work work; + unsigned long debounce_jiffies; }; static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len) -- cgit v1.2.3 From 18259cac98884522a46c24a73ef8d4879d824607 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:27:12 +0000 Subject: lp8727_charger: Fix checkpatch warning Fix the warning on MODULE_AUTHOR. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 9f25e151a36d..717a97690dd4 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -547,6 +547,5 @@ static struct i2c_driver lp8727_driver = { module_i2c_driver(lp8727_driver); MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver"); -MODULE_AUTHOR("Woogyom Kim , " - "Daniel Jeong "); +MODULE_AUTHOR("Milo Kim , Daniel Jeong "); MODULE_LICENSE("GPL"); -- cgit v1.2.3 From b9633ef1a9cdf4317d8c4a8db977485e2a8e1cb8 Mon Sep 17 00:00:00 2001 From: "Kim, Milo" Date: Fri, 31 Aug 2012 09:27:22 +0000 Subject: lp8727_charger: More pure cosmetic improvements This is really minor, but it improves the readability. Signed-off-by: Milo(Woogyom) Kim Signed-off-by: Anton Vorontsov --- drivers/power/lp8727_charger.c | 3 ++- include/linux/platform_data/lp8727.h | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'drivers') diff --git a/drivers/power/lp8727_charger.c b/drivers/power/lp8727_charger.c index 717a97690dd4..c628224b7f58 100644 --- a/drivers/power/lp8727_charger.c +++ b/drivers/power/lp8727_charger.c @@ -149,7 +149,6 @@ static int lp8727_init_device(struct lp8727_chg *pchg) if (ret) return ret; - val = LP8727_ID200_EN | LP8727_ADC_EN | LP8727_CP_EN; ret = lp8727_write_byte(pchg, LP8727_CTRL1, val); if (ret) @@ -162,6 +161,7 @@ static int lp8727_init_device(struct lp8727_chg *pchg) static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg) { u8 val; + lp8727_read_byte(pchg, LP8727_STATUS1, &val); return val & LP8727_DCPORT; } @@ -169,6 +169,7 @@ static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg) static int lp8727_is_usb_charger(struct lp8727_chg *pchg) { u8 val; + lp8727_read_byte(pchg, LP8727_STATUS1, &val); return val & LP8727_CHPORT; } diff --git a/include/linux/platform_data/lp8727.h b/include/linux/platform_data/lp8727.h index a8b8dbc418cc..47128a50e04e 100644 --- a/include/linux/platform_data/lp8727.h +++ b/include/linux/platform_data/lp8727.h @@ -38,7 +38,7 @@ enum lp8727_ichg { /** * struct lp8727_chg_param * @eoc_level : end of charge level setting - * @ichg : charging current + * @ichg : charging current */ struct lp8727_chg_param { enum lp8727_eoc_level eoc_level; @@ -47,10 +47,10 @@ struct lp8727_chg_param { /** * struct lp8727_platform_data - * @get_batt_present : check battery status - exists or not - * @get_batt_level : get battery voltage (mV) + * @get_batt_present : check battery status - exists or not + * @get_batt_level : get battery voltage (mV) * @get_batt_capacity : get battery capacity (%) - * @get_batt_temp : get battery temperature + * @get_batt_temp : get battery temperature * @ac : charging parameters for AC type charger * @usb : charging parameters for USB type charger * @debounce_msec : interrupt debounce time -- cgit v1.2.3 From 2ed9e9b6530951b5b96185e6761119361a166d7a Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 21 Aug 2012 17:06:52 +0900 Subject: charger-manager: Check fully charged state of battery periodically This patch check periodically fully charged state of battery to protect overcharge and overheat. If battery is fully charged, stop charging and check droped voltage with 'fullbatt_vchkdrop_ms' period. When voltage of battery is more droped than 'fullbatt_vchkdrop_uV' voltage, charger-manager will restart charging for battery. Signed-off-by: Chanwoo Choi Signed-off-by: Myungjoo Ham Signed-off-by: Kyungmin Park Signed-off-by: Anton Vorontsov --- drivers/power/charger-manager.c | 161 ++++++++++++++++++++++++---------- include/linux/power/charger-manager.h | 8 +- 2 files changed, 123 insertions(+), 46 deletions(-) (limited to 'drivers') diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c index cdf29d2eb7a5..c701f5e6f327 100644 --- a/drivers/power/charger-manager.c +++ b/drivers/power/charger-manager.c @@ -226,6 +226,58 @@ static bool is_charging(struct charger_manager *cm) return charging; } +/** + * is_full_charged - Returns true if the battery is fully charged. + * @cm: the Charger Manager representing the battery. + */ +static bool is_full_charged(struct charger_manager *cm) +{ + struct charger_desc *desc = cm->desc; + union power_supply_propval val; + int ret = 0; + int uV; + + /* If there is no battery, it cannot be charged */ + if (!is_batt_present(cm)) { + val.intval = 0; + goto out; + } + + if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) { + /* Not full if capacity of fuel gauge isn't full */ + ret = cm->fuel_gauge->get_property(cm->fuel_gauge, + POWER_SUPPLY_PROP_CHARGE_FULL, &val); + if (!ret && val.intval > desc->fullbatt_full_capacity) { + val.intval = 1; + goto out; + } + } + + /* Full, if it's over the fullbatt voltage */ + if (desc->fullbatt_uV > 0) { + ret = get_batt_uV(cm, &uV); + if (!ret && uV >= desc->fullbatt_uV) { + val.intval = 1; + goto out; + } + } + + /* Full, if the capacity is more than fullbatt_soc */ + if (cm->fuel_gauge && desc->fullbatt_soc > 0) { + ret = cm->fuel_gauge->get_property(cm->fuel_gauge, + POWER_SUPPLY_PROP_CAPACITY, &val); + if (!ret && val.intval >= desc->fullbatt_soc) { + val.intval = 1; + goto out; + } + } + + val.intval = 0; + +out: + return val.intval ? true : false; +} + /** * is_polling_required - Return true if need to continue polling for this CM. * @cm: the Charger Manager representing the battery. @@ -418,7 +470,7 @@ static void fullbatt_vchk(struct work_struct *work) diff = desc->fullbatt_uV; diff -= batt_uV; - dev_dbg(cm->dev, "VBATT dropped %duV after full-batt.\n", diff); + dev_info(cm->dev, "VBATT dropped %duV after full-batt.\n", diff); if (diff > desc->fullbatt_vchkdrop_uV) { try_charger_restart(cm); @@ -441,10 +493,14 @@ static bool _cm_monitor(struct charger_manager *cm) dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n", cm->last_temp_mC / 1000, cm->last_temp_mC % 1000); - /* It has been stopped or charging already */ - if (!!temp == !!cm->emergency_stop) + /* It has been stopped already */ + if (temp && cm->emergency_stop) return false; + /* + * Check temperature whether overheat or cold. + * If temperature is out of range normal state, stop charging. + */ if (temp) { cm->emergency_stop = temp; if (!try_charger_enable(cm, false)) { @@ -453,10 +509,34 @@ static bool _cm_monitor(struct charger_manager *cm) else uevent_notify(cm, "COLD"); } + + /* + * Check dropped voltage of battery. If battery voltage is more + * dropped than fullbatt_vchkdrop_uV after fully charged state, + * charger-manager have to recharge battery. + */ + } else if (!cm->emergency_stop && is_ext_pwr_online(cm) && + !cm->charger_enabled) { + fullbatt_vchk(&cm->fullbatt_vchk_work.work); + + /* + * Check whether fully charged state to protect overcharge + * if charger-manager is charging for battery. + */ + } else if (!cm->emergency_stop && is_full_charged(cm) && + cm->charger_enabled) { + dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n"); + uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]); + + try_charger_enable(cm, false); + + fullbatt_vchk(&cm->fullbatt_vchk_work.work); } else { cm->emergency_stop = 0; - if (!try_charger_enable(cm, true)) - uevent_notify(cm, "CHARGING"); + if (is_ext_pwr_online(cm)) { + if (!try_charger_enable(cm, true)) + uevent_notify(cm, "CHARGING"); + } } return true; @@ -719,47 +799,10 @@ static int charger_get_property(struct power_supply *psy, val->intval = 0; break; case POWER_SUPPLY_PROP_CHARGE_FULL: - if (cm->fuel_gauge) { - if (cm->fuel_gauge->get_property(cm->fuel_gauge, - POWER_SUPPLY_PROP_CHARGE_FULL, val) == 0) - break; - } - - if (is_ext_pwr_online(cm)) { - /* Not full if it's charging. */ - if (is_charging(cm)) { - val->intval = 0; - break; - } - /* - * Full if it's powered but not charging andi - * not forced stop by emergency - */ - if (!cm->emergency_stop) { - val->intval = 1; - break; - } - } - - /* Full if it's over the fullbatt voltage */ - ret = get_batt_uV(cm, &uV); - if (!ret && desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV && - !is_charging(cm)) { + if (is_full_charged(cm)) val->intval = 1; - break; - } - - /* Full if the cap is 100 */ - if (cm->fuel_gauge) { - ret = cm->fuel_gauge->get_property(cm->fuel_gauge, - POWER_SUPPLY_PROP_CAPACITY, val); - if (!ret && val->intval >= 100 && !is_charging(cm)) { - val->intval = 1; - break; - } - } - - val->intval = 0; + else + val->intval = 0; ret = 0; break; case POWER_SUPPLY_PROP_CHARGE_NOW: @@ -1049,7 +1092,26 @@ static int charger_extcon_notifier(struct notifier_block *self, struct charger_cable *cable = container_of(self, struct charger_cable, nb); + /* + * The newly state of charger cable. + * If cable is attached, cable->attached is true. + */ cable->attached = event; + + /* + * Setup monitoring to check battery state + * when charger cable is attached. + */ + if (cable->attached && is_polling_required(cable->cm)) { + if (work_pending(&setup_polling)) + cancel_work_sync(&setup_polling); + schedule_work(&setup_polling); + } + + /* + * Setup work for controlling charger(regulator) + * according to charger cable. + */ schedule_work(&cable->wq); return NOTIFY_DONE; @@ -1143,6 +1205,15 @@ static int charger_manager_probe(struct platform_device *pdev) desc->fullbatt_vchkdrop_ms = 0; desc->fullbatt_vchkdrop_uV = 0; } + if (desc->fullbatt_soc == 0) { + dev_info(&pdev->dev, "Ignoring full-battery soc(state of" + " charge) threshold as it is not" + " supplied."); + } + if (desc->fullbatt_full_capacity == 0) { + dev_info(&pdev->dev, "Ignoring full-battery full capacity" + " threshold as it is not supplied."); + } if (!desc->charger_regulators || desc->num_charger_regulators < 1) { ret = -EINVAL; diff --git a/include/linux/power/charger-manager.h b/include/linux/power/charger-manager.h index b542270affc1..76b37ef3c071 100644 --- a/include/linux/power/charger-manager.h +++ b/include/linux/power/charger-manager.h @@ -140,7 +140,11 @@ struct charger_regulator { * If it has dropped more than fullbatt_vchkdrop_uV after * fullbatt_vchkdrop_ms, CM will restart charging. * @fullbatt_uV: voltage in microvolt - * If it is not being charged and VBATT >= fullbatt_uV, + * If VBATT >= fullbatt_uV, it is assumed to be full. + * @fullbatt_soc: state of Charge in % + * If state of Charge >= fullbatt_soc, it is assumed to be full. + * @fullbatt_full_capacity: full capacity measure + * If full capacity of battery >= fullbatt_full_capacity, * it is assumed to be full. * @polling_interval_ms: interval in millisecond at which * charger manager will monitor battery health @@ -168,6 +172,8 @@ struct charger_desc { unsigned int fullbatt_vchkdrop_ms; unsigned int fullbatt_vchkdrop_uV; unsigned int fullbatt_uV; + unsigned int fullbatt_soc; + unsigned int fullbatt_full_capacity; enum data_source battery_present; -- cgit v1.2.3 From 8fcfe088e21aa0db9d62eaf565757def673efba6 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Thu, 20 Sep 2012 21:20:05 -0700 Subject: charger-manager: Support limit of maximum possible This patch check maximum possible duration of charging/discharging. If whole charging duration exceed 'desc->charging_max_duration_ms', cm stop charging to prevent overcharge/overheat. And if discharging duration exceed, charger cable is attached, after full-batt, cm start charging to maintain fully charged state for battery. Signed-off-by: Chanwoo Choi Signed-off-by: Myungjoo Ham Signed-off-by: Kyungmin Park Signed-off-by: Anton Vorontsov --- drivers/power/charger-manager.c | 80 ++++++++++++++++++++++++++++++++++- include/linux/power/charger-manager.h | 15 +++++++ 2 files changed, 94 insertions(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c index c701f5e6f327..e92ec55ced91 100644 --- a/drivers/power/charger-manager.c +++ b/drivers/power/charger-manager.c @@ -323,6 +323,14 @@ static int try_charger_enable(struct charger_manager *cm, bool enable) if (enable) { if (cm->emergency_stop) return -EAGAIN; + + /* + * Save start time of charging to limit + * maximum possible charging time. + */ + cm->charging_start_time = ktime_to_ms(ktime_get()); + cm->charging_end_time = 0; + for (i = 0 ; i < desc->num_charger_regulators ; i++) { err = regulator_enable(desc->charger_regulators[i].consumer); if (err < 0) { @@ -332,6 +340,13 @@ static int try_charger_enable(struct charger_manager *cm, bool enable) } } } else { + /* + * Save end time of charging to maintain fully charged state + * of battery after full-batt. + */ + cm->charging_start_time = 0; + cm->charging_end_time = ktime_to_ms(ktime_get()); + for (i = 0 ; i < desc->num_charger_regulators ; i++) { err = regulator_disable(desc->charger_regulators[i].consumer); if (err < 0) { @@ -474,8 +489,55 @@ static void fullbatt_vchk(struct work_struct *work) if (diff > desc->fullbatt_vchkdrop_uV) { try_charger_restart(cm); - uevent_notify(cm, "Recharge"); + uevent_notify(cm, "Recharging"); + } +} + +/** + * check_charging_duration - Monitor charging/discharging duration + * @cm: the Charger Manager representing the battery. + * + * If whole charging duration exceed 'charging_max_duration_ms', + * cm stop charging to prevent overcharge/overheat. If discharging + * duration exceed 'discharging _max_duration_ms', charger cable is + * attached, after full-batt, cm start charging to maintain fully + * charged state for battery. + */ +static int check_charging_duration(struct charger_manager *cm) +{ + struct charger_desc *desc = cm->desc; + u64 curr = ktime_to_ms(ktime_get()); + u64 duration; + int ret = false; + + if (!desc->charging_max_duration_ms && + !desc->discharging_max_duration_ms) + return ret; + + if (cm->charger_enabled) { + duration = curr - cm->charging_start_time; + + if (duration > desc->charging_max_duration_ms) { + dev_info(cm->dev, "Charging duration exceed %lldms", + desc->charging_max_duration_ms); + uevent_notify(cm, "Discharging"); + try_charger_enable(cm, false); + ret = true; + } + } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) { + duration = curr - cm->charging_end_time; + + if (duration > desc->charging_max_duration_ms && + is_ext_pwr_online(cm)) { + dev_info(cm->dev, "DisCharging duration exceed %lldms", + desc->discharging_max_duration_ms); + uevent_notify(cm, "Recharing"); + try_charger_enable(cm, true); + ret = true; + } } + + return ret; } /** @@ -510,6 +572,13 @@ static bool _cm_monitor(struct charger_manager *cm) uevent_notify(cm, "COLD"); } + /* + * Check whole charging duration and discharing duration + * after full-batt. + */ + } else if (!cm->emergency_stop && check_charging_duration(cm)) { + dev_dbg(cm->dev, + "Charging/Discharging duration is out of range"); /* * Check dropped voltage of battery. If battery voltage is more * dropped than fullbatt_vchkdrop_uV after fully charged state, @@ -1271,6 +1340,15 @@ static int charger_manager_probe(struct platform_device *pdev) goto err_chg_stat; } + if (!desc->charging_max_duration_ms || + !desc->discharging_max_duration_ms) { + dev_info(&pdev->dev, "Cannot limit charging duration " + "checking mechanism to prevent overcharge/overheat " + "and control discharging duration"); + desc->charging_max_duration_ms = 0; + desc->discharging_max_duration_ms = 0; + } + platform_set_drvdata(pdev, cm); memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default)); diff --git a/include/linux/power/charger-manager.h b/include/linux/power/charger-manager.h index 76b37ef3c071..a7b388ea1588 100644 --- a/include/linux/power/charger-manager.h +++ b/include/linux/power/charger-manager.h @@ -162,6 +162,13 @@ struct charger_regulator { * @measure_battery_temp: * true: measure battery temperature * false: measure ambient temperature + * @charging_max_duration_ms: Maximum possible duration for charging + * If whole charging duration exceed 'charging_max_duration_ms', + * cm stop charging. + * @discharging_max_duration_ms: + * Maximum possible duration for discharging with charger cable + * after full-batt. If discharging duration exceed 'discharging + * max_duration_ms', cm start charging. */ struct charger_desc { char *psy_name; @@ -186,6 +193,9 @@ struct charger_desc { int (*temperature_out_of_range)(int *mC); bool measure_battery_temp; + + u64 charging_max_duration_ms; + u64 discharging_max_duration_ms; }; #define PSY_NAME_MAX 30 @@ -210,6 +220,8 @@ struct charger_desc { * saved status of external power before entering suspend-to-RAM * @status_save_batt: * saved status of battery before entering suspend-to-RAM + * @charging_start_time: saved start time of enabling charging + * @charging_end_time: saved end time of disabling charging */ struct charger_manager { struct list_head entry; @@ -232,6 +244,9 @@ struct charger_manager { bool status_save_ext_pwr_inserted; bool status_save_batt; + + u64 charging_start_time; + u64 charging_end_time; }; #ifdef CONFIG_CHARGER_MANAGER -- cgit v1.2.3 From 3950c7865cd7c963982a2c94457182b96732f4c9 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Fri, 21 Sep 2012 18:49:37 +0900 Subject: charger-manager: Add support sysfs entry for charger This patch add support sysfs entry for each charger(regulator). Charger-manager use one or more chargers for charging battery but some charger isn't necessary on specific scenario. So, if some charger isn't needed, can disable specific charger through 'externally_control' entry while system is on state and confirm the information(name, state) of charger. The list of added sysfs entry - /sys/class/power_supply/battery/chargers/charger.[index]/name show name of charger(regulator) - /sys/class/power_supply/battery/chargers/charger.[index]/state show either enabled or disabled state of charger - /sys/class/power_supply/battery/chargers/charger.[index]/externally_control If 'externally_control' of specific charger is 1, Charger-manager cannot enable regulator for charging when charger cable is attached and charger must be maintained with disabled state. If 'externally_control' is zero, Charger-manager usually can control to enable/disable regulator. Signed-off-by: Chanwoo Choi Signed-off-by: Myungjoo Ham Signed-off-by: Kyungmin Park Signed-off-by: Anton Vorontsov --- drivers/power/charger-manager.c | 172 ++++++++++++++++++++++++++++++++++ include/linux/power/charger-manager.h | 19 ++++ 2 files changed, 191 insertions(+) (limited to 'drivers') diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c index e92ec55ced91..92dfa5c64876 100644 --- a/drivers/power/charger-manager.c +++ b/drivers/power/charger-manager.c @@ -22,6 +22,7 @@ #include #include #include +#include static const char * const default_event_names[] = { [CM_EVENT_UNKNOWN] = "Unknown", @@ -332,6 +333,9 @@ static int try_charger_enable(struct charger_manager *cm, bool enable) cm->charging_end_time = 0; for (i = 0 ; i < desc->num_charger_regulators ; i++) { + if (desc->charger_regulators[i].externally_control) + continue; + err = regulator_enable(desc->charger_regulators[i].consumer); if (err < 0) { dev_warn(cm->dev, @@ -348,6 +352,9 @@ static int try_charger_enable(struct charger_manager *cm, bool enable) cm->charging_end_time = ktime_to_ms(ktime_get()); for (i = 0 ; i < desc->num_charger_regulators ; i++) { + if (desc->charger_regulators[i].externally_control) + continue; + err = regulator_disable(desc->charger_regulators[i].consumer); if (err < 0) { dev_warn(cm->dev, @@ -1217,12 +1224,101 @@ static int charger_extcon_init(struct charger_manager *cm, return ret; } +/* help function of sysfs node to control charger(regulator) */ +static ssize_t charger_name_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct charger_regulator *charger + = container_of(attr, struct charger_regulator, attr_name); + + return sprintf(buf, "%s\n", charger->regulator_name); +} + +static ssize_t charger_state_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct charger_regulator *charger + = container_of(attr, struct charger_regulator, attr_state); + int state = 0; + + if (!charger->externally_control) + state = regulator_is_enabled(charger->consumer); + + return sprintf(buf, "%s\n", state ? "enabled" : "disabled"); +} + +static ssize_t charger_externally_control_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct charger_regulator *charger = container_of(attr, + struct charger_regulator, attr_externally_control); + + return sprintf(buf, "%d\n", charger->externally_control); +} + +static ssize_t charger_externally_control_store(struct device *dev, + struct device_attribute *attr, const char *buf, + size_t count) +{ + struct charger_regulator *charger + = container_of(attr, struct charger_regulator, + attr_externally_control); + struct charger_manager *cm = charger->cm; + struct charger_desc *desc = cm->desc; + int i; + int ret; + int externally_control; + int chargers_externally_control = 1; + + ret = sscanf(buf, "%d", &externally_control); + if (ret == 0) { + ret = -EINVAL; + return ret; + } + + if (!externally_control) { + charger->externally_control = 0; + return count; + } + + for (i = 0; i < desc->num_charger_regulators; i++) { + if (&desc->charger_regulators[i] != charger && + !desc->charger_regulators[i].externally_control) { + /* + * At least, one charger is controlled by + * charger-manager + */ + chargers_externally_control = 0; + break; + } + } + + if (!chargers_externally_control) { + if (cm->charger_enabled) { + try_charger_enable(charger->cm, false); + charger->externally_control = externally_control; + try_charger_enable(charger->cm, true); + } else { + charger->externally_control = externally_control; + } + } else { + dev_warn(cm->dev, + "'%s' regulator should be controlled " + "in charger-manager because charger-manager " + "must need at least one charger for charging\n", + charger->regulator_name); + } + + return count; +} + static int charger_manager_probe(struct platform_device *pdev) { struct charger_desc *desc = dev_get_platdata(&pdev->dev); struct charger_manager *cm; int ret = 0, i = 0; int j = 0; + int chargers_externally_control = 1; union power_supply_propval val; if (g_desc && !rtc_dev && g_desc->rtc_name) { @@ -1412,6 +1508,8 @@ static int charger_manager_probe(struct platform_device *pdev) for (i = 0 ; i < desc->num_charger_regulators ; i++) { struct charger_regulator *charger = &desc->charger_regulators[i]; + char buf[11]; + char *str; charger->consumer = regulator_get(&pdev->dev, charger->regulator_name); @@ -1421,6 +1519,7 @@ static int charger_manager_probe(struct platform_device *pdev) ret = -EINVAL; goto err_chg_get; } + charger->cm = cm; for (j = 0 ; j < charger->num_cables ; j++) { struct charger_cable *cable = &charger->cables[j]; @@ -1434,6 +1533,71 @@ static int charger_manager_probe(struct platform_device *pdev) cable->charger = charger; cable->cm = cm; } + + /* Create sysfs entry to control charger(regulator) */ + snprintf(buf, 10, "charger.%d", i); + str = kzalloc(sizeof(char) * (strlen(buf) + 1), GFP_KERNEL); + if (!str) { + for (i--; i >= 0; i--) { + charger = &desc->charger_regulators[i]; + kfree(charger->attr_g.name); + } + ret = -ENOMEM; + + goto err_extcon; + } + strcpy(str, buf); + + charger->attrs[0] = &charger->attr_name.attr; + charger->attrs[1] = &charger->attr_state.attr; + charger->attrs[2] = &charger->attr_externally_control.attr; + charger->attrs[3] = NULL; + charger->attr_g.name = str; + charger->attr_g.attrs = charger->attrs; + + sysfs_attr_init(&charger->attr_name.attr); + charger->attr_name.attr.name = "name"; + charger->attr_name.attr.mode = 0444; + charger->attr_name.show = charger_name_show; + + sysfs_attr_init(&charger->attr_state.attr); + charger->attr_state.attr.name = "state"; + charger->attr_state.attr.mode = 0444; + charger->attr_state.show = charger_state_show; + + sysfs_attr_init(&charger->attr_externally_control.attr); + charger->attr_externally_control.attr.name + = "externally_control"; + charger->attr_externally_control.attr.mode = 0644; + charger->attr_externally_control.show + = charger_externally_control_show; + charger->attr_externally_control.store + = charger_externally_control_store; + + if (!desc->charger_regulators[i].externally_control || + !chargers_externally_control) { + chargers_externally_control = 0; + } + dev_info(&pdev->dev, "'%s' regulator's externally_control" + "is %d\n", charger->regulator_name, + charger->externally_control); + + ret = sysfs_create_group(&cm->charger_psy.dev->kobj, + &charger->attr_g); + if (ret < 0) { + dev_info(&pdev->dev, "Cannot create sysfs entry" + "of %s regulator\n", + charger->regulator_name); + } + } + + if (chargers_externally_control) { + dev_err(&pdev->dev, "Cannot register regulator because " + "charger-manager must need at least " + "one charger for charging battery\n"); + + ret = -EINVAL; + goto err_chg_enable; } ret = try_charger_enable(cm, true); @@ -1459,6 +1623,14 @@ static int charger_manager_probe(struct platform_device *pdev) return 0; err_chg_enable: + for (i = 0; i < desc->num_charger_regulators; i++) { + struct charger_regulator *charger; + + charger = &desc->charger_regulators[i]; + sysfs_remove_group(&cm->charger_psy.dev->kobj, + &charger->attr_g); + kfree(charger->attr_g.name); + } err_extcon: for (i = 0 ; i < desc->num_charger_regulators ; i++) { struct charger_regulator *charger diff --git a/include/linux/power/charger-manager.h b/include/linux/power/charger-manager.h index a7b388ea1588..0e86840eb603 100644 --- a/include/linux/power/charger-manager.h +++ b/include/linux/power/charger-manager.h @@ -109,24 +109,43 @@ struct charger_cable { * struct charger_regulator * @regulator_name: the name of regulator for using charger. * @consumer: the regulator consumer for the charger. + * @externally_control: + * Set if the charger-manager cannot control charger, + * the charger will be maintained with disabled state. * @cables: * the array of charger cables to enable/disable charger * and set current limit according to constratint data of * struct charger_cable if only charger cable included * in the array of charger cables is attached/detached. * @num_cables: the number of charger cables. + * @attr_g: Attribute group for the charger(regulator) + * @attr_name: "name" sysfs entry + * @attr_state: "state" sysfs entry + * @attr_externally_control: "externally_control" sysfs entry + * @attrs: Arrays pointing to attr_name/state/externally_control for attr_g */ struct charger_regulator { /* The name of regulator for charging */ const char *regulator_name; struct regulator *consumer; + /* charger never on when system is on */ + int externally_control; + /* * Store constraint information related to current limit, * each cable have different condition for charging. */ struct charger_cable *cables; int num_cables; + + struct attribute_group attr_g; + struct device_attribute attr_name; + struct device_attribute attr_state; + struct device_attribute attr_externally_control; + struct attribute *attrs[4]; + + struct charger_manager *cm; }; /** -- cgit v1.2.3 From c10a002a2b4af557730e71591c529fbdf355433f Mon Sep 17 00:00:00 2001 From: Paul Parsons Date: Fri, 21 Sep 2012 16:49:45 -0700 Subject: pda_power: Remove ac_draw_failed goto and label A previous patch added the ac_draw_failed goto and label to pda_power_probe(). The goto would be invoked after a failed call to regulator_get(). However the way ac_draw is used - always after a check for NULL - suggests that a failed call to regulator_get() was not fatal. This patch removes the ac_draw_failed goto and label, partly reverting the previous patch. This patch also removes the assignment of an error code to ret after a failed call to regulator_get(), since the error code is now never used. Signed-off-by: Paul Parsons Cc: Philipp Zabel Signed-off-by: Anton Vorontsov --- drivers/power/pda_power.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'drivers') diff --git a/drivers/power/pda_power.c b/drivers/power/pda_power.c index 75536ebdb132..7df7c5facc10 100644 --- a/drivers/power/pda_power.c +++ b/drivers/power/pda_power.c @@ -285,8 +285,6 @@ static int pda_power_probe(struct platform_device *pdev) if (IS_ERR(ac_draw)) { dev_dbg(dev, "couldn't get ac_draw regulator\n"); ac_draw = NULL; - ret = PTR_ERR(ac_draw); - goto ac_draw_failed; } update_status(); @@ -416,7 +414,6 @@ ac_supply_failed: regulator_put(ac_draw); ac_draw = NULL; } -ac_draw_failed: if (pdata->exit) pdata->exit(dev); init_failed: -- cgit v1.2.3