Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Universal power supply monitor class |
| 3 | * |
| 4 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> |
| 5 | * Copyright © 2004 Szabolcs Gyurko |
| 6 | * Copyright © 2003 Ian Molton <spyro@f2s.com> |
| 7 | * |
| 8 | * Modified: 2004, Oct Szabolcs Gyurko |
| 9 | * |
| 10 | * You may use this code as per GPL version 2 |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/init.h> |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 16 | #include <linux/slab.h> |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 17 | #include <linux/device.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/power_supply.h> |
| 20 | #include "power_supply.h" |
| 21 | |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 22 | /* exported for the APM Power driver, APM emulation */ |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 23 | struct class *power_supply_class; |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 24 | EXPORT_SYMBOL_GPL(power_supply_class); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 25 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 26 | static struct device_type power_supply_dev_type; |
| 27 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 28 | static int __power_supply_changed_work(struct device *dev, void *data) |
| 29 | { |
| 30 | struct power_supply *psy = (struct power_supply *)data; |
| 31 | struct power_supply *pst = dev_get_drvdata(dev); |
| 32 | int i; |
| 33 | |
| 34 | for (i = 0; i < psy->num_supplicants; i++) |
| 35 | if (!strcmp(psy->supplied_to[i], pst->name)) { |
| 36 | if (pst->external_power_changed) |
| 37 | pst->external_power_changed(pst); |
| 38 | } |
| 39 | return 0; |
| 40 | } |
| 41 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 42 | static void power_supply_changed_work(struct work_struct *work) |
| 43 | { |
| 44 | struct power_supply *psy = container_of(work, struct power_supply, |
| 45 | changed_work); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 46 | |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 47 | dev_dbg(psy->dev, "%s\n", __func__); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 48 | |
Greg Kroah-Hartman | 93562b5 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 49 | class_for_each_device(power_supply_class, NULL, psy, |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 50 | __power_supply_changed_work); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 51 | |
| 52 | power_supply_update_leds(psy); |
| 53 | |
| 54 | kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void power_supply_changed(struct power_supply *psy) |
| 58 | { |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 59 | dev_dbg(psy->dev, "%s\n", __func__); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 60 | |
| 61 | schedule_work(&psy->changed_work); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 62 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 63 | EXPORT_SYMBOL_GPL(power_supply_changed); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 64 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 65 | static int __power_supply_am_i_supplied(struct device *dev, void *data) |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 66 | { |
| 67 | union power_supply_propval ret = {0,}; |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 68 | struct power_supply *psy = (struct power_supply *)data; |
| 69 | struct power_supply *epsy = dev_get_drvdata(dev); |
| 70 | int i; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 71 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 72 | for (i = 0; i < epsy->num_supplicants; i++) { |
| 73 | if (!strcmp(epsy->supplied_to[i], psy->name)) { |
| 74 | if (epsy->get_property(epsy, |
| 75 | POWER_SUPPLY_PROP_ONLINE, &ret)) |
| 76 | continue; |
| 77 | if (ret.intval) |
| 78 | return ret.intval; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 79 | } |
| 80 | } |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 81 | return 0; |
| 82 | } |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 83 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 84 | int power_supply_am_i_supplied(struct power_supply *psy) |
| 85 | { |
| 86 | int error; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 87 | |
Greg Kroah-Hartman | 93562b5 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 88 | error = class_for_each_device(power_supply_class, NULL, psy, |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 89 | __power_supply_am_i_supplied); |
| 90 | |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 91 | dev_dbg(psy->dev, "%s %d\n", __func__, error); |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 92 | |
| 93 | return error; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 94 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 95 | EXPORT_SYMBOL_GPL(power_supply_am_i_supplied); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 96 | |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 97 | static int __power_supply_is_system_supplied(struct device *dev, void *data) |
| 98 | { |
| 99 | union power_supply_propval ret = {0,}; |
| 100 | struct power_supply *psy = dev_get_drvdata(dev); |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 101 | unsigned int *count = data; |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 102 | |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 103 | (*count)++; |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 104 | if (psy->type != POWER_SUPPLY_TYPE_BATTERY) { |
| 105 | if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret)) |
| 106 | return 0; |
| 107 | if (ret.intval) |
| 108 | return ret.intval; |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | int power_supply_is_system_supplied(void) |
| 114 | { |
| 115 | int error; |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 116 | unsigned int count = 0; |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 117 | |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 118 | error = class_for_each_device(power_supply_class, NULL, &count, |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 119 | __power_supply_is_system_supplied); |
| 120 | |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 121 | /* |
| 122 | * If no power class device was found at all, most probably we are |
| 123 | * running on a desktop system, so assume we are on mains power. |
| 124 | */ |
| 125 | if (count == 0) |
| 126 | return 1; |
| 127 | |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 128 | return error; |
| 129 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 130 | EXPORT_SYMBOL_GPL(power_supply_is_system_supplied); |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 131 | |
Daniel Mack | e5f5ccb | 2009-07-23 20:35:53 +0200 | [diff] [blame] | 132 | int power_supply_set_battery_charged(struct power_supply *psy) |
| 133 | { |
| 134 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) { |
| 135 | psy->set_charged(psy); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | return -EINVAL; |
| 140 | } |
| 141 | EXPORT_SYMBOL_GPL(power_supply_set_battery_charged); |
| 142 | |
| 143 | static int power_supply_match_device_by_name(struct device *dev, void *data) |
| 144 | { |
| 145 | const char *name = data; |
| 146 | struct power_supply *psy = dev_get_drvdata(dev); |
| 147 | |
| 148 | return strcmp(psy->name, name) == 0; |
| 149 | } |
| 150 | |
| 151 | struct power_supply *power_supply_get_by_name(char *name) |
| 152 | { |
| 153 | struct device *dev = class_find_device(power_supply_class, NULL, name, |
| 154 | power_supply_match_device_by_name); |
| 155 | |
| 156 | return dev ? dev_get_drvdata(dev) : NULL; |
| 157 | } |
| 158 | EXPORT_SYMBOL_GPL(power_supply_get_by_name); |
| 159 | |
Jeremy Fitzhardinge | 8351665 | 2011-12-07 09:15:45 -0800 | [diff] [blame] | 160 | int power_supply_powers(struct power_supply *psy, struct device *dev) |
| 161 | { |
Anton Vorontsov | 93278d1 | 2012-01-05 19:17:25 +0400 | [diff] [blame] | 162 | return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers"); |
Jeremy Fitzhardinge | 8351665 | 2011-12-07 09:15:45 -0800 | [diff] [blame] | 163 | } |
| 164 | EXPORT_SYMBOL_GPL(power_supply_powers); |
| 165 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 166 | static void power_supply_dev_release(struct device *dev) |
| 167 | { |
| 168 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
| 169 | kfree(dev); |
| 170 | } |
| 171 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 172 | int power_supply_register(struct device *parent, struct power_supply *psy) |
| 173 | { |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 174 | struct device *dev; |
| 175 | int rc; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 176 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 177 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 178 | if (!dev) |
| 179 | return -ENOMEM; |
| 180 | |
| 181 | device_initialize(dev); |
| 182 | |
| 183 | dev->class = power_supply_class; |
| 184 | dev->type = &power_supply_dev_type; |
| 185 | dev->parent = parent; |
| 186 | dev->release = power_supply_dev_release; |
| 187 | dev_set_drvdata(dev, psy); |
| 188 | psy->dev = dev; |
| 189 | |
Lars-Peter Clausen | 9777467 | 2011-02-21 15:34:19 +0100 | [diff] [blame] | 190 | INIT_WORK(&psy->changed_work, power_supply_changed_work); |
| 191 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 192 | rc = kobject_set_name(&dev->kobj, "%s", psy->name); |
| 193 | if (rc) |
| 194 | goto kobject_set_name_failed; |
| 195 | |
| 196 | rc = device_add(dev); |
| 197 | if (rc) |
| 198 | goto device_add_failed; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 199 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 200 | rc = power_supply_create_triggers(psy); |
| 201 | if (rc) |
| 202 | goto create_triggers_failed; |
| 203 | |
| 204 | power_supply_changed(psy); |
| 205 | |
| 206 | goto success; |
| 207 | |
| 208 | create_triggers_failed: |
Vasiliy Kulikov | 3a2dbd6 | 2010-11-19 21:41:58 +0300 | [diff] [blame] | 209 | device_del(dev); |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 210 | kobject_set_name_failed: |
| 211 | device_add_failed: |
Vasiliy Kulikov | 3a2dbd6 | 2010-11-19 21:41:58 +0300 | [diff] [blame] | 212 | put_device(dev); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 213 | success: |
| 214 | return rc; |
| 215 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 216 | EXPORT_SYMBOL_GPL(power_supply_register); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 217 | |
| 218 | void power_supply_unregister(struct power_supply *psy) |
| 219 | { |
Tejun Heo | bc51e7f | 2010-12-11 17:51:45 +0100 | [diff] [blame] | 220 | cancel_work_sync(&psy->changed_work); |
Jeremy Fitzhardinge | 8351665 | 2011-12-07 09:15:45 -0800 | [diff] [blame] | 221 | sysfs_remove_link(&psy->dev->kobj, "powers"); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 222 | power_supply_remove_triggers(psy); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 223 | device_unregister(psy->dev); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 224 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 225 | EXPORT_SYMBOL_GPL(power_supply_unregister); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 226 | |
| 227 | static int __init power_supply_class_init(void) |
| 228 | { |
| 229 | power_supply_class = class_create(THIS_MODULE, "power_supply"); |
| 230 | |
| 231 | if (IS_ERR(power_supply_class)) |
| 232 | return PTR_ERR(power_supply_class); |
| 233 | |
| 234 | power_supply_class->dev_uevent = power_supply_uevent; |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 235 | power_supply_init_attrs(&power_supply_dev_type); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static void __exit power_supply_class_exit(void) |
| 241 | { |
| 242 | class_destroy(power_supply_class); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 243 | } |
| 244 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 245 | subsys_initcall(power_supply_class_init); |
| 246 | module_exit(power_supply_class_exit); |
| 247 | |
| 248 | MODULE_DESCRIPTION("Universal power supply monitor class"); |
| 249 | MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, " |
| 250 | "Szabolcs Gyurko, " |
| 251 | "Anton Vorontsov <cbou@mail.ru>"); |
| 252 | MODULE_LICENSE("GPL"); |