123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- /*
- * Universal power supply monitor class
- *
- * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
- * Copyright © 2004 Szabolcs Gyurko
- * Copyright © 2003 Ian Molton <spyro@f2s.com>
- *
- * Modified: 2004, Oct Szabolcs Gyurko
- *
- * You may use this code as per GPL version 2
- */
- #include <linux/module.h>
- #include <linux/types.h>
- #include <linux/init.h>
- #include <linux/slab.h>
- #include <linux/device.h>
- #include <linux/err.h>
- #include <linux/power_supply.h>
- #include "power_supply.h"
- /* exported for the APM Power driver, APM emulation */
- struct class *power_supply_class;
- EXPORT_SYMBOL_GPL(power_supply_class);
- static struct device_type power_supply_dev_type;
- /**
- * power_supply_set_voltage_limit - set current limit
- * @psy: the power supply to control
- * @limit: current limit in uV from the power supply.
- * 0 will disable the power supply.
- *
- * This function will set a maximum supply current from a source
- * and it will disable the charger when limit is 0.
- */
- int power_supply_set_voltage_limit(struct power_supply *psy, int limit)
- {
- const union power_supply_propval ret = {limit,};
- if (psy->set_property)
- return psy->set_property(psy, POWER_SUPPLY_PROP_VOLTAGE_MAX,
- &ret);
- return -ENXIO;
- }
- EXPORT_SYMBOL(power_supply_set_voltage_limit);
- /**
- * power_supply_set_current_limit - set current limit
- * @psy: the power supply to control
- * @limit: current limit in uA from the power supply.
- * 0 will disable the power supply.
- *
- * This function will set a maximum supply current from a source
- * and it will disable the charger when limit is 0.
- */
- int power_supply_set_current_limit(struct power_supply *psy, int limit)
- {
- const union power_supply_propval ret = {limit,};
- if (psy->set_property)
- return psy->set_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX,
- &ret);
- return -ENXIO;
- }
- EXPORT_SYMBOL_GPL(power_supply_set_current_limit);
- /**
- * power_supply_set_charging_enabled - enable or disable charging
- * @psy: the power supply to control
- * @enable: sets enable property of power supply
- */
- int power_supply_set_charging_enabled(struct power_supply *psy, bool enable)
- {
- const union power_supply_propval ret = {enable,};
- if (psy->set_property)
- return psy->set_property(psy,
- POWER_SUPPLY_PROP_CHARGING_ENABLED,
- &ret);
- return -ENXIO;
- }
- EXPORT_SYMBOL_GPL(power_supply_set_charging_enabled);
- /**
- * power_supply_set_present - set present state of the power supply
- * @psy: the power supply to control
- * @enable: sets present property of power supply
- */
- int power_supply_set_present(struct power_supply *psy, bool enable)
- {
- const union power_supply_propval ret = {enable,};
- if (psy->set_property)
- return psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT,
- &ret);
- return -ENXIO;
- }
- EXPORT_SYMBOL_GPL(power_supply_set_present);
- /**
- * power_supply_set_online - set online state of the power supply
- * @psy: the power supply to control
- * @enable: sets online property of power supply
- */
- int power_supply_set_online(struct power_supply *psy, bool enable)
- {
- const union power_supply_propval ret = {enable,};
- if (psy->set_property)
- return psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE,
- &ret);
- return -ENXIO;
- }
- EXPORT_SYMBOL_GPL(power_supply_set_online);
- /** power_supply_set_health_state - set health state of the power supply
- * @psy: the power supply to control
- * @health: sets health property of power supply
- */
- int power_supply_set_health_state(struct power_supply *psy, int health)
- {
- const union power_supply_propval ret = {health,};
- if (psy->set_property)
- return psy->set_property(psy, POWER_SUPPLY_PROP_HEALTH,
- &ret);
- return -ENXIO;
- }
- EXPORT_SYMBOL(power_supply_set_health_state);
- /**
- * power_supply_set_scope - set scope of the power supply
- * @psy: the power supply to control
- * @scope: value to set the scope property to, should be from
- * the SCOPE enum in power_supply.h
- */
- int power_supply_set_scope(struct power_supply *psy, int scope)
- {
- const union power_supply_propval ret = {scope, };
- if (psy->set_property)
- return psy->set_property(psy, POWER_SUPPLY_PROP_SCOPE,
- &ret);
- return -ENXIO;
- }
- EXPORT_SYMBOL_GPL(power_supply_set_scope);
- /**
- * power_supply_set_supply_type - set type of the power supply
- * @psy: the power supply to control
- * @supply_type: sets type property of power supply
- */
- int power_supply_set_supply_type(struct power_supply *psy,
- enum power_supply_type supply_type)
- {
- const union power_supply_propval ret = {supply_type,};
- if (psy->set_property)
- return psy->set_property(psy, POWER_SUPPLY_PROP_TYPE,
- &ret);
- return -ENXIO;
- }
- EXPORT_SYMBOL_GPL(power_supply_set_supply_type);
- /**
- * power_supply_set_charge_type - set charge type of the power supply
- * @psy: the power supply to control
- * @enable: sets charge type property of power supply
- */
- int power_supply_set_charge_type(struct power_supply *psy, int charge_type)
- {
- const union power_supply_propval ret = {charge_type,};
- if (psy->set_property)
- return psy->set_property(psy, POWER_SUPPLY_PROP_CHARGE_TYPE,
- &ret);
- return -ENXIO;
- }
- EXPORT_SYMBOL_GPL(power_supply_set_charge_type);
- static int __power_supply_changed_work(struct device *dev, void *data)
- {
- struct power_supply *psy = (struct power_supply *)data;
- struct power_supply *pst = dev_get_drvdata(dev);
- int i;
- for (i = 0; i < psy->num_supplicants; i++)
- if (!strcmp(psy->supplied_to[i], pst->name)) {
- if (pst->external_power_changed)
- pst->external_power_changed(pst);
- }
- return 0;
- }
- 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__);
- 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);
- }
- 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);
- static int __power_supply_am_i_supplied(struct device *dev, void *data)
- {
- union power_supply_propval ret = {0,};
- struct power_supply *psy = (struct power_supply *)data;
- struct power_supply *epsy = dev_get_drvdata(dev);
- int i;
- for (i = 0; i < epsy->num_supplicants; i++) {
- if (!strcmp(epsy->supplied_to[i], psy->name)) {
- if (epsy->get_property(epsy,
- POWER_SUPPLY_PROP_ONLINE, &ret))
- continue;
- if (ret.intval)
- return ret.intval;
- }
- }
- return 0;
- }
- int power_supply_am_i_supplied(struct power_supply *psy)
- {
- int error;
- error = class_for_each_device(power_supply_class, NULL, psy,
- __power_supply_am_i_supplied);
- dev_dbg(psy->dev, "%s %d\n", __func__, error);
- return error;
- }
- EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
- static int __power_supply_is_system_supplied(struct device *dev, void *data)
- {
- union power_supply_propval ret = {0,};
- struct power_supply *psy = dev_get_drvdata(dev);
- unsigned int *count = data;
- (*count)++;
- if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
- if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
- return 0;
- if (ret.intval)
- return ret.intval;
- }
- return 0;
- }
- int power_supply_is_system_supplied(void)
- {
- int error;
- unsigned int count = 0;
- error = class_for_each_device(power_supply_class, NULL, &count,
- __power_supply_is_system_supplied);
- /*
- * If no power class device was found at all, most probably we are
- * running on a desktop system, so assume we are on mains power.
- */
- if (count == 0)
- return 1;
- return error;
- }
- EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
- int power_supply_set_battery_charged(struct power_supply *psy)
- {
- if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
- psy->set_charged(psy);
- return 0;
- }
- return -EINVAL;
- }
- EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
- static int power_supply_match_device_by_name(struct device *dev, void *data)
- {
- const char *name = data;
- struct power_supply *psy = dev_get_drvdata(dev);
- return strcmp(psy->name, name) == 0;
- }
- struct power_supply *power_supply_get_by_name(char *name)
- {
- struct device *dev = class_find_device(power_supply_class, NULL, name,
- power_supply_match_device_by_name);
- return dev ? dev_get_drvdata(dev) : NULL;
- }
- EXPORT_SYMBOL_GPL(power_supply_get_by_name);
- int power_supply_powers(struct power_supply *psy, struct device *dev)
- {
- return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
- }
- EXPORT_SYMBOL_GPL(power_supply_powers);
- static void power_supply_dev_release(struct device *dev)
- {
- pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
- kfree(dev);
- }
- int power_supply_register(struct device *parent, struct power_supply *psy)
- {
- struct device *dev;
- int rc;
- dev = kzalloc(sizeof(*dev), GFP_KERNEL);
- if (!dev)
- return -ENOMEM;
- device_initialize(dev);
- dev->class = power_supply_class;
- dev->type = &power_supply_dev_type;
- dev->parent = parent;
- dev->release = power_supply_dev_release;
- dev_set_drvdata(dev, psy);
- psy->dev = dev;
- INIT_WORK(&psy->changed_work, power_supply_changed_work);
- rc = kobject_set_name(&dev->kobj, "%s", psy->name);
- if (rc)
- goto kobject_set_name_failed;
- rc = device_add(dev);
- if (rc)
- goto device_add_failed;
- spin_lock_init(&psy->changed_lock);
- rc = device_init_wakeup(dev, true);
- if (rc)
- goto wakeup_init_failed;
- rc = power_supply_create_triggers(psy);
- if (rc)
- goto create_triggers_failed;
- power_supply_changed(psy);
- goto success;
- create_triggers_failed:
- wakeup_init_failed:
- device_del(dev);
- kobject_set_name_failed:
- device_add_failed:
- put_device(dev);
- success:
- return rc;
- }
- EXPORT_SYMBOL_GPL(power_supply_register);
- void power_supply_unregister(struct power_supply *psy)
- {
- cancel_work_sync(&psy->changed_work);
- sysfs_remove_link(&psy->dev->kobj, "powers");
- power_supply_remove_triggers(psy);
- device_unregister(psy->dev);
- }
- EXPORT_SYMBOL_GPL(power_supply_unregister);
- static int __init power_supply_class_init(void)
- {
- power_supply_class = class_create(THIS_MODULE, "power_supply");
- if (IS_ERR(power_supply_class))
- return PTR_ERR(power_supply_class);
- power_supply_class->dev_uevent = power_supply_uevent;
- power_supply_init_attrs(&power_supply_dev_type);
- return 0;
- }
- static void __exit power_supply_class_exit(void)
- {
- class_destroy(power_supply_class);
- }
- subsys_initcall(power_supply_class_init);
- module_exit(power_supply_class_exit);
- MODULE_DESCRIPTION("Universal power supply monitor class");
- MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
- "Szabolcs Gyurko, "
- "Anton Vorontsov <cbou@mail.ru>");
- MODULE_LICENSE("GPL");
|