Skip to content

Commit

Permalink
power_supply: Prevent suspend until power supply events are processed
Browse files Browse the repository at this point in the history
This patch, originally authored by Arve Hjonnevag and Todd Poynor,
prevents the system from entering suspend mode until the power supply
plug, unplug, or any other change of state event is fully processed. This
guarantees that the screen lights up and displays the battery charging
state. The implementation uses the power supply wakeup_source object.

Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Arve Hjonnevag <arve@android.com>
Cc: Todd Poynor <toddpoynor@google.com>
Cc: John Stultz <john.stultz@linaro.org>
Signed-off-by: Zoran Markovic <zoran.markovic@linaro.org>
Signed-off-by: Anton Vorontsov <anton@enomsg.org>
  • Loading branch information
Zoran Markovic authored and Anton Vorontsov committed Aug 28, 2013
1 parent a2c0206 commit 948dcf9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
38 changes: 32 additions & 6 deletions drivers/power/power_supply_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,42 @@ static int __power_supply_changed_work(struct device *dev, void *data)

static void power_supply_changed_work(struct work_struct *work)
{
unsigned long flags;
struct power_supply *psy = container_of(work, struct power_supply,
changed_work);

dev_dbg(psy->dev, "%s\n", __func__);

class_for_each_device(power_supply_class, NULL, psy,
__power_supply_changed_work);

power_supply_update_leds(psy);

kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
spin_lock_irqsave(&psy->changed_lock, flags);
if (psy->changed) {
psy->changed = false;
spin_unlock_irqrestore(&psy->changed_lock, flags);
class_for_each_device(power_supply_class, NULL, psy,
__power_supply_changed_work);
power_supply_update_leds(psy);
kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
spin_lock_irqsave(&psy->changed_lock, flags);
}
/*
* Dependent power supplies (e.g. battery) may have changed state
* as a result of this event, so poll again and hold the
* wakeup_source until all events are processed.
*/
if (!psy->changed)
pm_relax(psy->dev);
spin_unlock_irqrestore(&psy->changed_lock, flags);
}

void power_supply_changed(struct power_supply *psy)
{
unsigned long flags;

dev_dbg(psy->dev, "%s\n", __func__);

spin_lock_irqsave(&psy->changed_lock, flags);
psy->changed = true;
pm_stay_awake(psy->dev);
spin_unlock_irqrestore(&psy->changed_lock, flags);
schedule_work(&psy->changed_work);
}
EXPORT_SYMBOL_GPL(power_supply_changed);
Expand Down Expand Up @@ -500,6 +519,11 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
goto check_supplies_failed;
}

spin_lock_init(&psy->changed_lock);
rc = device_init_wakeup(dev, true);
if (rc)
goto wakeup_init_failed;

rc = kobject_set_name(&dev->kobj, "%s", psy->name);
if (rc)
goto kobject_set_name_failed;
Expand Down Expand Up @@ -529,6 +553,7 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
register_cooler_failed:
psy_unregister_thermal(psy);
register_thermal_failed:
wakeup_init_failed:
device_del(dev);
kobject_set_name_failed:
device_add_failed:
Expand All @@ -546,6 +571,7 @@ void power_supply_unregister(struct power_supply *psy)
power_supply_remove_triggers(psy);
psy_unregister_cooler(psy);
psy_unregister_thermal(psy);
device_init_wakeup(psy->dev, false);
device_unregister(psy->dev);
}
EXPORT_SYMBOL_GPL(power_supply_unregister);
Expand Down
3 changes: 3 additions & 0 deletions include/linux/power_supply.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <linux/workqueue.h>
#include <linux/leds.h>
#include <linux/spinlock.h>

struct device;

Expand Down Expand Up @@ -194,6 +195,8 @@ struct power_supply {
/* private */
struct device *dev;
struct work_struct changed_work;
spinlock_t changed_lock;
bool changed;
#ifdef CONFIG_THERMAL
struct thermal_zone_device *tzd;
struct thermal_cooling_device *tcd;
Expand Down

0 comments on commit 948dcf9

Please sign in to comment.