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 | |
Willie Ruan | e6bee11 | 2011-11-06 15:52:52 -0800 | [diff] [blame] | 28 | /** |
David Keitel | 3ff5447 | 2014-03-31 15:00:36 -0700 | [diff] [blame] | 29 | * power_supply_set_voltage_limit - set current limit |
| 30 | * @psy: the power supply to control |
| 31 | * @limit: current limit in uV from the power supply. |
| 32 | * 0 will disable the power supply. |
| 33 | * |
| 34 | * This function will set a maximum supply current from a source |
| 35 | * and it will disable the charger when limit is 0. |
| 36 | */ |
| 37 | int power_supply_set_voltage_limit(struct power_supply *psy, int limit) |
| 38 | { |
| 39 | const union power_supply_propval ret = {limit,}; |
| 40 | |
| 41 | if (psy->set_property) |
| 42 | return psy->set_property(psy, POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| 43 | &ret); |
| 44 | |
| 45 | return -ENXIO; |
| 46 | } |
| 47 | EXPORT_SYMBOL(power_supply_set_voltage_limit); |
| 48 | |
| 49 | |
| 50 | /** |
Willie Ruan | e6bee11 | 2011-11-06 15:52:52 -0800 | [diff] [blame] | 51 | * power_supply_set_current_limit - set current limit |
| 52 | * @psy: the power supply to control |
| 53 | * @limit: current limit in uA from the power supply. |
| 54 | * 0 will disable the power supply. |
| 55 | * |
| 56 | * This function will set a maximum supply current from a source |
| 57 | * and it will disable the charger when limit is 0. |
| 58 | */ |
| 59 | int power_supply_set_current_limit(struct power_supply *psy, int limit) |
| 60 | { |
| 61 | const union power_supply_propval ret = {limit,}; |
| 62 | |
| 63 | if (psy->set_property) |
| 64 | return psy->set_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX, |
| 65 | &ret); |
| 66 | |
| 67 | return -ENXIO; |
| 68 | } |
| 69 | EXPORT_SYMBOL_GPL(power_supply_set_current_limit); |
| 70 | |
| 71 | /** |
David Keitel | 5018778 | 2012-10-15 10:41:16 -0700 | [diff] [blame] | 72 | * power_supply_set_charging_enabled - enable or disable charging |
| 73 | * @psy: the power supply to control |
| 74 | * @enable: sets enable property of power supply |
| 75 | */ |
| 76 | int power_supply_set_charging_enabled(struct power_supply *psy, bool enable) |
| 77 | { |
| 78 | const union power_supply_propval ret = {enable,}; |
| 79 | |
| 80 | if (psy->set_property) |
| 81 | return psy->set_property(psy, |
| 82 | POWER_SUPPLY_PROP_CHARGING_ENABLED, |
| 83 | &ret); |
| 84 | |
| 85 | return -ENXIO; |
| 86 | } |
| 87 | EXPORT_SYMBOL_GPL(power_supply_set_charging_enabled); |
| 88 | |
| 89 | /** |
David Keitel | 5cec16d | 2012-10-16 16:55:24 -0700 | [diff] [blame] | 90 | * power_supply_set_present - set present state of the power supply |
Willie Ruan | e6bee11 | 2011-11-06 15:52:52 -0800 | [diff] [blame] | 91 | * @psy: the power supply to control |
David Keitel | 5cec16d | 2012-10-16 16:55:24 -0700 | [diff] [blame] | 92 | * @enable: sets present property of power supply |
Willie Ruan | e6bee11 | 2011-11-06 15:52:52 -0800 | [diff] [blame] | 93 | */ |
David Keitel | 272ce52 | 2012-08-17 16:25:24 -0700 | [diff] [blame] | 94 | int power_supply_set_present(struct power_supply *psy, bool enable) |
| 95 | { |
| 96 | const union power_supply_propval ret = {enable,}; |
| 97 | |
| 98 | if (psy->set_property) |
| 99 | return psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT, |
| 100 | &ret); |
| 101 | |
| 102 | return -ENXIO; |
| 103 | } |
| 104 | EXPORT_SYMBOL_GPL(power_supply_set_present); |
| 105 | |
| 106 | /** |
| 107 | * power_supply_set_online - set online state of the power supply |
| 108 | * @psy: the power supply to control |
| 109 | * @enable: sets online property of power supply |
| 110 | */ |
David Keitel | 65ddd84 | 2012-01-11 11:59:41 -0800 | [diff] [blame] | 111 | int power_supply_set_online(struct power_supply *psy, bool enable) |
Willie Ruan | e6bee11 | 2011-11-06 15:52:52 -0800 | [diff] [blame] | 112 | { |
| 113 | const union power_supply_propval ret = {enable,}; |
| 114 | |
| 115 | if (psy->set_property) |
| 116 | return psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, |
| 117 | &ret); |
| 118 | |
| 119 | return -ENXIO; |
| 120 | } |
David Keitel | 65ddd84 | 2012-01-11 11:59:41 -0800 | [diff] [blame] | 121 | EXPORT_SYMBOL_GPL(power_supply_set_online); |
| 122 | |
Wu Fenglin | 04ae6de | 2013-09-09 19:15:06 +0800 | [diff] [blame] | 123 | |
| 124 | /** power_supply_set_health_state - set health state of the power supply |
| 125 | * @psy: the power supply to control |
| 126 | * @health: sets health property of power supply |
| 127 | */ |
| 128 | int power_supply_set_health_state(struct power_supply *psy, int health) |
| 129 | { |
| 130 | const union power_supply_propval ret = {health,}; |
| 131 | |
| 132 | if (psy->set_property) |
| 133 | return psy->set_property(psy, POWER_SUPPLY_PROP_HEALTH, |
| 134 | &ret); |
| 135 | return -ENXIO; |
| 136 | } |
| 137 | EXPORT_SYMBOL(power_supply_set_health_state); |
| 138 | |
| 139 | |
David Keitel | 65ddd84 | 2012-01-11 11:59:41 -0800 | [diff] [blame] | 140 | /** |
Abhijeet Dharmapurikar | 59d890f | 2012-06-26 11:19:17 -0700 | [diff] [blame] | 141 | * power_supply_set_scope - set scope of the power supply |
| 142 | * @psy: the power supply to control |
| 143 | * @scope: value to set the scope property to, should be from |
| 144 | * the SCOPE enum in power_supply.h |
| 145 | */ |
| 146 | int power_supply_set_scope(struct power_supply *psy, int scope) |
| 147 | { |
| 148 | const union power_supply_propval ret = {scope, }; |
| 149 | |
| 150 | if (psy->set_property) |
| 151 | return psy->set_property(psy, POWER_SUPPLY_PROP_SCOPE, |
| 152 | &ret); |
Abhijeet Dharmapurikar | 59d890f | 2012-06-26 11:19:17 -0700 | [diff] [blame] | 153 | return -ENXIO; |
| 154 | } |
| 155 | EXPORT_SYMBOL_GPL(power_supply_set_scope); |
| 156 | |
| 157 | /** |
David Keitel | 01a5ff0 | 2012-06-01 18:14:50 -0700 | [diff] [blame] | 158 | * power_supply_set_supply_type - set type of the power supply |
| 159 | * @psy: the power supply to control |
| 160 | * @supply_type: sets type property of power supply |
| 161 | */ |
| 162 | int power_supply_set_supply_type(struct power_supply *psy, |
| 163 | enum power_supply_type supply_type) |
| 164 | { |
| 165 | const union power_supply_propval ret = {supply_type,}; |
| 166 | |
| 167 | if (psy->set_property) |
| 168 | return psy->set_property(psy, POWER_SUPPLY_PROP_TYPE, |
| 169 | &ret); |
| 170 | |
| 171 | return -ENXIO; |
| 172 | } |
| 173 | EXPORT_SYMBOL_GPL(power_supply_set_supply_type); |
| 174 | |
| 175 | /** |
David Keitel | 65ddd84 | 2012-01-11 11:59:41 -0800 | [diff] [blame] | 176 | * power_supply_set_charge_type - set charge type of the power supply |
| 177 | * @psy: the power supply to control |
| 178 | * @enable: sets charge type property of power supply |
| 179 | */ |
| 180 | int power_supply_set_charge_type(struct power_supply *psy, int charge_type) |
| 181 | { |
| 182 | const union power_supply_propval ret = {charge_type,}; |
| 183 | |
| 184 | if (psy->set_property) |
| 185 | return psy->set_property(psy, POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 186 | &ret); |
| 187 | |
| 188 | return -ENXIO; |
| 189 | } |
| 190 | EXPORT_SYMBOL_GPL(power_supply_set_charge_type); |
Willie Ruan | e6bee11 | 2011-11-06 15:52:52 -0800 | [diff] [blame] | 191 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 192 | static int __power_supply_changed_work(struct device *dev, void *data) |
| 193 | { |
| 194 | struct power_supply *psy = (struct power_supply *)data; |
| 195 | struct power_supply *pst = dev_get_drvdata(dev); |
| 196 | int i; |
| 197 | |
| 198 | for (i = 0; i < psy->num_supplicants; i++) |
| 199 | if (!strcmp(psy->supplied_to[i], pst->name)) { |
| 200 | if (pst->external_power_changed) |
| 201 | pst->external_power_changed(pst); |
| 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 206 | static void power_supply_changed_work(struct work_struct *work) |
| 207 | { |
Arve Hjønnevåg | 836ad48 | 2009-09-21 17:26:47 -0700 | [diff] [blame] | 208 | unsigned long flags; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 209 | struct power_supply *psy = container_of(work, struct power_supply, |
| 210 | changed_work); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 211 | |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 212 | dev_dbg(psy->dev, "%s\n", __func__); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 213 | |
Arve Hjønnevåg | 836ad48 | 2009-09-21 17:26:47 -0700 | [diff] [blame] | 214 | spin_lock_irqsave(&psy->changed_lock, flags); |
| 215 | if (psy->changed) { |
| 216 | psy->changed = false; |
| 217 | spin_unlock_irqrestore(&psy->changed_lock, flags); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 218 | |
Arve Hjønnevåg | 836ad48 | 2009-09-21 17:26:47 -0700 | [diff] [blame] | 219 | class_for_each_device(power_supply_class, NULL, psy, |
| 220 | __power_supply_changed_work); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 221 | |
Arve Hjønnevåg | 836ad48 | 2009-09-21 17:26:47 -0700 | [diff] [blame] | 222 | power_supply_update_leds(psy); |
| 223 | |
| 224 | kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE); |
| 225 | spin_lock_irqsave(&psy->changed_lock, flags); |
| 226 | } |
| 227 | if (!psy->changed) |
| 228 | wake_unlock(&psy->work_wake_lock); |
| 229 | spin_unlock_irqrestore(&psy->changed_lock, flags); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void power_supply_changed(struct power_supply *psy) |
| 233 | { |
Arve Hjønnevåg | 836ad48 | 2009-09-21 17:26:47 -0700 | [diff] [blame] | 234 | unsigned long flags; |
| 235 | |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 236 | dev_dbg(psy->dev, "%s\n", __func__); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 237 | |
Arve Hjønnevåg | 836ad48 | 2009-09-21 17:26:47 -0700 | [diff] [blame] | 238 | spin_lock_irqsave(&psy->changed_lock, flags); |
| 239 | psy->changed = true; |
| 240 | wake_lock(&psy->work_wake_lock); |
| 241 | spin_unlock_irqrestore(&psy->changed_lock, flags); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 242 | schedule_work(&psy->changed_work); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 243 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 244 | EXPORT_SYMBOL_GPL(power_supply_changed); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 245 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 246 | static int __power_supply_am_i_supplied(struct device *dev, void *data) |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 247 | { |
| 248 | union power_supply_propval ret = {0,}; |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 249 | struct power_supply *psy = (struct power_supply *)data; |
| 250 | struct power_supply *epsy = dev_get_drvdata(dev); |
| 251 | int i; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 252 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 253 | for (i = 0; i < epsy->num_supplicants; i++) { |
| 254 | if (!strcmp(epsy->supplied_to[i], psy->name)) { |
| 255 | if (epsy->get_property(epsy, |
| 256 | POWER_SUPPLY_PROP_ONLINE, &ret)) |
| 257 | continue; |
| 258 | if (ret.intval) |
| 259 | return ret.intval; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 260 | } |
| 261 | } |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 262 | return 0; |
| 263 | } |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 264 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 265 | int power_supply_am_i_supplied(struct power_supply *psy) |
| 266 | { |
| 267 | int error; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 268 | |
Greg Kroah-Hartman | 93562b5 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 269 | error = class_for_each_device(power_supply_class, NULL, psy, |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 270 | __power_supply_am_i_supplied); |
| 271 | |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 272 | dev_dbg(psy->dev, "%s %d\n", __func__, error); |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 273 | |
| 274 | return error; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 275 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 276 | EXPORT_SYMBOL_GPL(power_supply_am_i_supplied); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 277 | |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 278 | static int __power_supply_is_system_supplied(struct device *dev, void *data) |
| 279 | { |
| 280 | union power_supply_propval ret = {0,}; |
| 281 | struct power_supply *psy = dev_get_drvdata(dev); |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 282 | unsigned int *count = data; |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 283 | |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 284 | (*count)++; |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 285 | if (psy->type != POWER_SUPPLY_TYPE_BATTERY) { |
| 286 | if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret)) |
| 287 | return 0; |
| 288 | if (ret.intval) |
| 289 | return ret.intval; |
| 290 | } |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | int power_supply_is_system_supplied(void) |
| 295 | { |
| 296 | int error; |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 297 | unsigned int count = 0; |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 298 | |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 299 | error = class_for_each_device(power_supply_class, NULL, &count, |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 300 | __power_supply_is_system_supplied); |
| 301 | |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 302 | /* |
| 303 | * If no power class device was found at all, most probably we are |
| 304 | * running on a desktop system, so assume we are on mains power. |
| 305 | */ |
| 306 | if (count == 0) |
| 307 | return 1; |
| 308 | |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 309 | return error; |
| 310 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 311 | EXPORT_SYMBOL_GPL(power_supply_is_system_supplied); |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 312 | |
Daniel Mack | e5f5ccb | 2009-07-23 20:35:53 +0200 | [diff] [blame] | 313 | int power_supply_set_battery_charged(struct power_supply *psy) |
| 314 | { |
| 315 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) { |
| 316 | psy->set_charged(psy); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | return -EINVAL; |
| 321 | } |
| 322 | EXPORT_SYMBOL_GPL(power_supply_set_battery_charged); |
| 323 | |
| 324 | static int power_supply_match_device_by_name(struct device *dev, void *data) |
| 325 | { |
| 326 | const char *name = data; |
| 327 | struct power_supply *psy = dev_get_drvdata(dev); |
| 328 | |
| 329 | return strcmp(psy->name, name) == 0; |
| 330 | } |
| 331 | |
| 332 | struct power_supply *power_supply_get_by_name(char *name) |
| 333 | { |
| 334 | struct device *dev = class_find_device(power_supply_class, NULL, name, |
| 335 | power_supply_match_device_by_name); |
| 336 | |
| 337 | return dev ? dev_get_drvdata(dev) : NULL; |
| 338 | } |
| 339 | EXPORT_SYMBOL_GPL(power_supply_get_by_name); |
| 340 | |
Jeremy Fitzhardinge | 8351665 | 2011-12-07 09:15:45 -0800 | [diff] [blame] | 341 | int power_supply_powers(struct power_supply *psy, struct device *dev) |
| 342 | { |
Anton Vorontsov | 93278d1 | 2012-01-05 19:17:25 +0400 | [diff] [blame] | 343 | return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers"); |
Jeremy Fitzhardinge | 8351665 | 2011-12-07 09:15:45 -0800 | [diff] [blame] | 344 | } |
| 345 | EXPORT_SYMBOL_GPL(power_supply_powers); |
| 346 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 347 | static void power_supply_dev_release(struct device *dev) |
| 348 | { |
| 349 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
| 350 | kfree(dev); |
| 351 | } |
| 352 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 353 | int power_supply_register(struct device *parent, struct power_supply *psy) |
| 354 | { |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 355 | struct device *dev; |
| 356 | int rc; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 357 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 358 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 359 | if (!dev) |
| 360 | return -ENOMEM; |
| 361 | |
| 362 | device_initialize(dev); |
| 363 | |
| 364 | dev->class = power_supply_class; |
| 365 | dev->type = &power_supply_dev_type; |
| 366 | dev->parent = parent; |
| 367 | dev->release = power_supply_dev_release; |
| 368 | dev_set_drvdata(dev, psy); |
| 369 | psy->dev = dev; |
| 370 | |
Lars-Peter Clausen | 9777467 | 2011-02-21 15:34:19 +0100 | [diff] [blame] | 371 | INIT_WORK(&psy->changed_work, power_supply_changed_work); |
| 372 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 373 | rc = kobject_set_name(&dev->kobj, "%s", psy->name); |
| 374 | if (rc) |
| 375 | goto kobject_set_name_failed; |
| 376 | |
| 377 | rc = device_add(dev); |
| 378 | if (rc) |
| 379 | goto device_add_failed; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 380 | |
Arve Hjønnevåg | 836ad48 | 2009-09-21 17:26:47 -0700 | [diff] [blame] | 381 | spin_lock_init(&psy->changed_lock); |
| 382 | wake_lock_init(&psy->work_wake_lock, WAKE_LOCK_SUSPEND, "power-supply"); |
| 383 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 384 | rc = power_supply_create_triggers(psy); |
| 385 | if (rc) |
| 386 | goto create_triggers_failed; |
| 387 | |
| 388 | power_supply_changed(psy); |
| 389 | |
| 390 | goto success; |
| 391 | |
| 392 | create_triggers_failed: |
Arve Hjønnevåg | 836ad48 | 2009-09-21 17:26:47 -0700 | [diff] [blame] | 393 | wake_lock_destroy(&psy->work_wake_lock); |
Vasiliy Kulikov | 3a2dbd6 | 2010-11-19 21:41:58 +0300 | [diff] [blame] | 394 | device_del(dev); |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 395 | kobject_set_name_failed: |
| 396 | device_add_failed: |
Vasiliy Kulikov | 3a2dbd6 | 2010-11-19 21:41:58 +0300 | [diff] [blame] | 397 | put_device(dev); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 398 | success: |
| 399 | return rc; |
| 400 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 401 | EXPORT_SYMBOL_GPL(power_supply_register); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 402 | |
| 403 | void power_supply_unregister(struct power_supply *psy) |
| 404 | { |
Tejun Heo | bc51e7f | 2010-12-11 17:51:45 +0100 | [diff] [blame] | 405 | cancel_work_sync(&psy->changed_work); |
Jeremy Fitzhardinge | 8351665 | 2011-12-07 09:15:45 -0800 | [diff] [blame] | 406 | sysfs_remove_link(&psy->dev->kobj, "powers"); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 407 | power_supply_remove_triggers(psy); |
Arve Hjønnevåg | 836ad48 | 2009-09-21 17:26:47 -0700 | [diff] [blame] | 408 | wake_lock_destroy(&psy->work_wake_lock); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 409 | device_unregister(psy->dev); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 410 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 411 | EXPORT_SYMBOL_GPL(power_supply_unregister); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 412 | |
| 413 | static int __init power_supply_class_init(void) |
| 414 | { |
| 415 | power_supply_class = class_create(THIS_MODULE, "power_supply"); |
| 416 | |
| 417 | if (IS_ERR(power_supply_class)) |
| 418 | return PTR_ERR(power_supply_class); |
| 419 | |
| 420 | power_supply_class->dev_uevent = power_supply_uevent; |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 421 | power_supply_init_attrs(&power_supply_dev_type); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | static void __exit power_supply_class_exit(void) |
| 427 | { |
| 428 | class_destroy(power_supply_class); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 429 | } |
| 430 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 431 | subsys_initcall(power_supply_class_init); |
| 432 | module_exit(power_supply_class_exit); |
| 433 | |
| 434 | MODULE_DESCRIPTION("Universal power supply monitor class"); |
| 435 | MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, " |
| 436 | "Szabolcs Gyurko, " |
| 437 | "Anton Vorontsov <cbou@mail.ru>"); |
| 438 | MODULE_LICENSE("GPL"); |