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> |
Pali Rohár | d36240d | 2013-11-19 11:18:03 +0100 | [diff] [blame] | 18 | #include <linux/notifier.h> |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <linux/power_supply.h> |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 21 | #include <linux/thermal.h> |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 22 | #include "power_supply.h" |
| 23 | |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 24 | /* exported for the APM Power driver, APM emulation */ |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 25 | struct class *power_supply_class; |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 26 | EXPORT_SYMBOL_GPL(power_supply_class); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 27 | |
Pali Rohár | d36240d | 2013-11-19 11:18:03 +0100 | [diff] [blame] | 28 | ATOMIC_NOTIFIER_HEAD(power_supply_notifier); |
| 29 | EXPORT_SYMBOL_GPL(power_supply_notifier); |
| 30 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 31 | static struct device_type power_supply_dev_type; |
| 32 | |
Krzysztof Kozlowski | 7f1a57f | 2015-05-19 16:13:02 +0900 | [diff] [blame] | 33 | #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10) |
| 34 | |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 35 | static bool __power_supply_is_supplied_by(struct power_supply *supplier, |
| 36 | struct power_supply *supply) |
| 37 | { |
| 38 | int i; |
| 39 | |
| 40 | if (!supply->supplied_from && !supplier->supplied_to) |
| 41 | return false; |
| 42 | |
| 43 | /* Support both supplied_to and supplied_from modes */ |
| 44 | if (supply->supplied_from) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 45 | if (!supplier->desc->name) |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 46 | return false; |
| 47 | for (i = 0; i < supply->num_supplies; i++) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 48 | if (!strcmp(supplier->desc->name, supply->supplied_from[i])) |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 49 | return true; |
| 50 | } else { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 51 | if (!supply->desc->name) |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 52 | return false; |
| 53 | for (i = 0; i < supplier->num_supplicants; i++) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 54 | if (!strcmp(supplier->supplied_to[i], supply->desc->name)) |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
| 58 | return false; |
| 59 | } |
| 60 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 61 | static int __power_supply_changed_work(struct device *dev, void *data) |
| 62 | { |
Viresh Kumar | e80cf42 | 2014-09-04 17:31:26 +0530 | [diff] [blame] | 63 | struct power_supply *psy = data; |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 64 | struct power_supply *pst = dev_get_drvdata(dev); |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 65 | |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 66 | if (__power_supply_is_supplied_by(psy, pst)) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 67 | if (pst->desc->external_power_changed) |
| 68 | pst->desc->external_power_changed(pst); |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 74 | static void power_supply_changed_work(struct work_struct *work) |
| 75 | { |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 76 | unsigned long flags; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 77 | struct power_supply *psy = container_of(work, struct power_supply, |
| 78 | changed_work); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 79 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 80 | dev_dbg(&psy->dev, "%s\n", __func__); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 81 | |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 82 | spin_lock_irqsave(&psy->changed_lock, flags); |
Viresh Kumar | 061f380 | 2014-09-04 17:31:32 +0530 | [diff] [blame] | 83 | /* |
| 84 | * Check 'changed' here to avoid issues due to race between |
| 85 | * power_supply_changed() and this routine. In worst case |
| 86 | * power_supply_changed() can be called again just before we take above |
| 87 | * lock. During the first call of this routine we will mark 'changed' as |
| 88 | * false and it will stay false for the next call as well. |
| 89 | */ |
| 90 | if (likely(psy->changed)) { |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 91 | psy->changed = false; |
| 92 | spin_unlock_irqrestore(&psy->changed_lock, flags); |
| 93 | class_for_each_device(power_supply_class, NULL, psy, |
| 94 | __power_supply_changed_work); |
| 95 | power_supply_update_leds(psy); |
Pali Rohár | d36240d | 2013-11-19 11:18:03 +0100 | [diff] [blame] | 96 | atomic_notifier_call_chain(&power_supply_notifier, |
| 97 | PSY_EVENT_PROP_CHANGED, psy); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 98 | kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE); |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 99 | spin_lock_irqsave(&psy->changed_lock, flags); |
| 100 | } |
Viresh Kumar | 061f380 | 2014-09-04 17:31:32 +0530 | [diff] [blame] | 101 | |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 102 | /* |
Viresh Kumar | 061f380 | 2014-09-04 17:31:32 +0530 | [diff] [blame] | 103 | * Hold the wakeup_source until all events are processed. |
| 104 | * power_supply_changed() might have called again and have set 'changed' |
| 105 | * to true. |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 106 | */ |
Viresh Kumar | 061f380 | 2014-09-04 17:31:32 +0530 | [diff] [blame] | 107 | if (likely(!psy->changed)) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 108 | pm_relax(&psy->dev); |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 109 | spin_unlock_irqrestore(&psy->changed_lock, flags); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void power_supply_changed(struct power_supply *psy) |
| 113 | { |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 114 | unsigned long flags; |
| 115 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 116 | dev_dbg(&psy->dev, "%s\n", __func__); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 117 | |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 118 | spin_lock_irqsave(&psy->changed_lock, flags); |
| 119 | psy->changed = true; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 120 | pm_stay_awake(&psy->dev); |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 121 | spin_unlock_irqrestore(&psy->changed_lock, flags); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 122 | schedule_work(&psy->changed_work); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 123 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 124 | EXPORT_SYMBOL_GPL(power_supply_changed); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 125 | |
Krzysztof Kozlowski | 7f1a57f | 2015-05-19 16:13:02 +0900 | [diff] [blame] | 126 | /* |
| 127 | * Notify that power supply was registered after parent finished the probing. |
| 128 | * |
| 129 | * Often power supply is registered from driver's probe function. However |
| 130 | * calling power_supply_changed() directly from power_supply_register() |
| 131 | * would lead to execution of get_property() function provided by the driver |
| 132 | * too early - before the probe ends. |
| 133 | * |
| 134 | * Avoid that by waiting on parent's mutex. |
| 135 | */ |
| 136 | static void power_supply_deferred_register_work(struct work_struct *work) |
| 137 | { |
| 138 | struct power_supply *psy = container_of(work, struct power_supply, |
| 139 | deferred_register_work.work); |
| 140 | |
| 141 | if (psy->dev.parent) |
| 142 | mutex_lock(&psy->dev.parent->mutex); |
| 143 | |
| 144 | power_supply_changed(psy); |
| 145 | |
| 146 | if (psy->dev.parent) |
| 147 | mutex_unlock(&psy->dev.parent->mutex); |
| 148 | } |
| 149 | |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 150 | #ifdef CONFIG_OF |
| 151 | #include <linux/of.h> |
| 152 | |
| 153 | static int __power_supply_populate_supplied_from(struct device *dev, |
| 154 | void *data) |
| 155 | { |
Viresh Kumar | e80cf42 | 2014-09-04 17:31:26 +0530 | [diff] [blame] | 156 | struct power_supply *psy = data; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 157 | struct power_supply *epsy = dev_get_drvdata(dev); |
| 158 | struct device_node *np; |
| 159 | int i = 0; |
| 160 | |
| 161 | do { |
| 162 | np = of_parse_phandle(psy->of_node, "power-supplies", i++); |
| 163 | if (!np) |
Viresh Kumar | a0f93b4 | 2014-09-04 17:31:27 +0530 | [diff] [blame] | 164 | break; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 165 | |
| 166 | if (np == epsy->of_node) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 167 | dev_info(&psy->dev, "%s: Found supply : %s\n", |
| 168 | psy->desc->name, epsy->desc->name); |
| 169 | psy->supplied_from[i-1] = (char *)epsy->desc->name; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 170 | psy->num_supplies++; |
Rhyland Klein | 2054d6e | 2013-06-10 17:26:39 -0400 | [diff] [blame] | 171 | of_node_put(np); |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 172 | break; |
| 173 | } |
Rhyland Klein | 2054d6e | 2013-06-10 17:26:39 -0400 | [diff] [blame] | 174 | of_node_put(np); |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 175 | } while (np); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int power_supply_populate_supplied_from(struct power_supply *psy) |
| 181 | { |
| 182 | int error; |
| 183 | |
| 184 | error = class_for_each_device(power_supply_class, NULL, psy, |
| 185 | __power_supply_populate_supplied_from); |
| 186 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 187 | dev_dbg(&psy->dev, "%s %d\n", __func__, error); |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 188 | |
| 189 | return error; |
| 190 | } |
| 191 | |
| 192 | static int __power_supply_find_supply_from_node(struct device *dev, |
| 193 | void *data) |
| 194 | { |
Viresh Kumar | e80cf42 | 2014-09-04 17:31:26 +0530 | [diff] [blame] | 195 | struct device_node *np = data; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 196 | struct power_supply *epsy = dev_get_drvdata(dev); |
| 197 | |
Viresh Kumar | 585b008 | 2014-09-04 17:31:30 +0530 | [diff] [blame] | 198 | /* returning non-zero breaks out of class_for_each_device loop */ |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 199 | if (epsy->of_node == np) |
Viresh Kumar | 585b008 | 2014-09-04 17:31:30 +0530 | [diff] [blame] | 200 | return 1; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int power_supply_find_supply_from_node(struct device_node *supply_node) |
| 206 | { |
| 207 | int error; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 208 | |
| 209 | /* |
Viresh Kumar | 585b008 | 2014-09-04 17:31:30 +0530 | [diff] [blame] | 210 | * class_for_each_device() either returns its own errors or values |
| 211 | * returned by __power_supply_find_supply_from_node(). |
| 212 | * |
| 213 | * __power_supply_find_supply_from_node() will return 0 (no match) |
| 214 | * or 1 (match). |
| 215 | * |
| 216 | * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if |
| 217 | * it returned 0, or error as returned by it. |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 218 | */ |
| 219 | error = class_for_each_device(power_supply_class, NULL, supply_node, |
| 220 | __power_supply_find_supply_from_node); |
| 221 | |
Viresh Kumar | 585b008 | 2014-09-04 17:31:30 +0530 | [diff] [blame] | 222 | return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static int power_supply_check_supplies(struct power_supply *psy) |
| 226 | { |
| 227 | struct device_node *np; |
| 228 | int cnt = 0; |
| 229 | |
| 230 | /* If there is already a list honor it */ |
| 231 | if (psy->supplied_from && psy->num_supplies > 0) |
| 232 | return 0; |
| 233 | |
| 234 | /* No device node found, nothing to do */ |
| 235 | if (!psy->of_node) |
| 236 | return 0; |
| 237 | |
| 238 | do { |
| 239 | int ret; |
| 240 | |
| 241 | np = of_parse_phandle(psy->of_node, "power-supplies", cnt++); |
| 242 | if (!np) |
Viresh Kumar | a0f93b4 | 2014-09-04 17:31:27 +0530 | [diff] [blame] | 243 | break; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 244 | |
| 245 | ret = power_supply_find_supply_from_node(np); |
Viresh Kumar | 8468b02 | 2014-09-04 17:31:28 +0530 | [diff] [blame] | 246 | of_node_put(np); |
| 247 | |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 248 | if (ret) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 249 | dev_dbg(&psy->dev, "Failed to find supply!\n"); |
Viresh Kumar | f5b89af | 2014-09-04 17:31:29 +0530 | [diff] [blame] | 250 | return ret; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 251 | } |
| 252 | } while (np); |
| 253 | |
Viresh Kumar | f9c8548 | 2014-09-04 17:31:23 +0530 | [diff] [blame] | 254 | /* Missing valid "power-supplies" entries */ |
| 255 | if (cnt == 1) |
| 256 | return 0; |
| 257 | |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 258 | /* All supplies found, allocate char ** array for filling */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 259 | psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from), |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 260 | GFP_KERNEL); |
| 261 | if (!psy->supplied_from) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 262 | dev_err(&psy->dev, "Couldn't allocate memory for supply list\n"); |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 263 | return -ENOMEM; |
| 264 | } |
| 265 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 266 | *psy->supplied_from = devm_kzalloc(&psy->dev, |
| 267 | sizeof(char *) * (cnt - 1), |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 268 | GFP_KERNEL); |
| 269 | if (!*psy->supplied_from) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 270 | dev_err(&psy->dev, "Couldn't allocate memory for supply list\n"); |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 271 | return -ENOMEM; |
| 272 | } |
| 273 | |
| 274 | return power_supply_populate_supplied_from(psy); |
| 275 | } |
| 276 | #else |
| 277 | static inline int power_supply_check_supplies(struct power_supply *psy) |
| 278 | { |
| 279 | return 0; |
| 280 | } |
| 281 | #endif |
| 282 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 283 | static int __power_supply_am_i_supplied(struct device *dev, void *data) |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 284 | { |
| 285 | union power_supply_propval ret = {0,}; |
Viresh Kumar | e80cf42 | 2014-09-04 17:31:26 +0530 | [diff] [blame] | 286 | struct power_supply *psy = data; |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 287 | struct power_supply *epsy = dev_get_drvdata(dev); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 288 | |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 289 | if (__power_supply_is_supplied_by(epsy, psy)) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 290 | if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, |
| 291 | &ret)) |
Viresh Kumar | 1c42a38 | 2014-09-04 17:31:31 +0530 | [diff] [blame] | 292 | return ret.intval; |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 293 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 294 | return 0; |
| 295 | } |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 296 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 297 | int power_supply_am_i_supplied(struct power_supply *psy) |
| 298 | { |
| 299 | int error; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 300 | |
Greg Kroah-Hartman | 93562b5 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 301 | error = class_for_each_device(power_supply_class, NULL, psy, |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 302 | __power_supply_am_i_supplied); |
| 303 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 304 | dev_dbg(&psy->dev, "%s %d\n", __func__, error); |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 305 | |
| 306 | return error; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 307 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 308 | EXPORT_SYMBOL_GPL(power_supply_am_i_supplied); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 309 | |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 310 | static int __power_supply_is_system_supplied(struct device *dev, void *data) |
| 311 | { |
| 312 | union power_supply_propval ret = {0,}; |
| 313 | struct power_supply *psy = dev_get_drvdata(dev); |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 314 | unsigned int *count = data; |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 315 | |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 316 | (*count)++; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 317 | if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY) |
| 318 | if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE, |
| 319 | &ret)) |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 320 | return ret.intval; |
Viresh Kumar | 1c42a38 | 2014-09-04 17:31:31 +0530 | [diff] [blame] | 321 | |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | int power_supply_is_system_supplied(void) |
| 326 | { |
| 327 | int error; |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 328 | unsigned int count = 0; |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 329 | |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 330 | error = class_for_each_device(power_supply_class, NULL, &count, |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 331 | __power_supply_is_system_supplied); |
| 332 | |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 333 | /* |
| 334 | * If no power class device was found at all, most probably we are |
| 335 | * running on a desktop system, so assume we are on mains power. |
| 336 | */ |
| 337 | if (count == 0) |
| 338 | return 1; |
| 339 | |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 340 | return error; |
| 341 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 342 | EXPORT_SYMBOL_GPL(power_supply_is_system_supplied); |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 343 | |
Daniel Mack | e5f5ccb | 2009-07-23 20:35:53 +0200 | [diff] [blame] | 344 | int power_supply_set_battery_charged(struct power_supply *psy) |
| 345 | { |
Krzysztof Kozlowski | bc15405 | 2015-03-12 08:44:03 +0100 | [diff] [blame] | 346 | if (atomic_read(&psy->use_cnt) >= 0 && |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 347 | psy->desc->type == POWER_SUPPLY_TYPE_BATTERY && |
| 348 | psy->desc->set_charged) { |
| 349 | psy->desc->set_charged(psy); |
Daniel Mack | e5f5ccb | 2009-07-23 20:35:53 +0200 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | return -EINVAL; |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(power_supply_set_battery_charged); |
| 356 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 357 | static int power_supply_match_device_by_name(struct device *dev, const void *data) |
Daniel Mack | e5f5ccb | 2009-07-23 20:35:53 +0200 | [diff] [blame] | 358 | { |
| 359 | const char *name = data; |
| 360 | struct power_supply *psy = dev_get_drvdata(dev); |
| 361 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 362 | return strcmp(psy->desc->name, name) == 0; |
Daniel Mack | e5f5ccb | 2009-07-23 20:35:53 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Krzysztof Kozlowski | 1a35246 | 2015-03-12 08:44:12 +0100 | [diff] [blame] | 365 | /** |
| 366 | * power_supply_get_by_name() - Search for a power supply and returns its ref |
| 367 | * @name: Power supply name to fetch |
| 368 | * |
| 369 | * If power supply was found, it increases reference count for the |
| 370 | * internal power supply's device. The user should power_supply_put() |
| 371 | * after usage. |
| 372 | * |
| 373 | * Return: On success returns a reference to a power supply with |
| 374 | * matching name equals to @name, a NULL otherwise. |
| 375 | */ |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 376 | struct power_supply *power_supply_get_by_name(const char *name) |
Daniel Mack | e5f5ccb | 2009-07-23 20:35:53 +0200 | [diff] [blame] | 377 | { |
Krzysztof Kozlowski | 03e81ac | 2015-03-12 08:44:13 +0100 | [diff] [blame] | 378 | struct power_supply *psy = NULL; |
Daniel Mack | e5f5ccb | 2009-07-23 20:35:53 +0200 | [diff] [blame] | 379 | struct device *dev = class_find_device(power_supply_class, NULL, name, |
| 380 | power_supply_match_device_by_name); |
| 381 | |
Krzysztof Kozlowski | 03e81ac | 2015-03-12 08:44:13 +0100 | [diff] [blame] | 382 | if (dev) { |
| 383 | psy = dev_get_drvdata(dev); |
| 384 | atomic_inc(&psy->use_cnt); |
| 385 | } |
| 386 | |
| 387 | return psy; |
Daniel Mack | e5f5ccb | 2009-07-23 20:35:53 +0200 | [diff] [blame] | 388 | } |
| 389 | EXPORT_SYMBOL_GPL(power_supply_get_by_name); |
| 390 | |
Krzysztof Kozlowski | 1a35246 | 2015-03-12 08:44:12 +0100 | [diff] [blame] | 391 | /** |
| 392 | * power_supply_put() - Drop reference obtained with power_supply_get_by_name |
| 393 | * @psy: Reference to put |
| 394 | * |
| 395 | * The reference to power supply should be put before unregistering |
| 396 | * the power supply. |
| 397 | */ |
| 398 | void power_supply_put(struct power_supply *psy) |
| 399 | { |
| 400 | might_sleep(); |
| 401 | |
Krzysztof Kozlowski | 03e81ac | 2015-03-12 08:44:13 +0100 | [diff] [blame] | 402 | atomic_dec(&psy->use_cnt); |
Krzysztof Kozlowski | 1a35246 | 2015-03-12 08:44:12 +0100 | [diff] [blame] | 403 | put_device(&psy->dev); |
| 404 | } |
| 405 | EXPORT_SYMBOL_GPL(power_supply_put); |
| 406 | |
Sebastian Reichel | abce977 | 2013-11-24 17:49:29 +0100 | [diff] [blame] | 407 | #ifdef CONFIG_OF |
| 408 | static int power_supply_match_device_node(struct device *dev, const void *data) |
| 409 | { |
| 410 | return dev->parent && dev->parent->of_node == data; |
| 411 | } |
| 412 | |
Krzysztof Kozlowski | 1a35246 | 2015-03-12 08:44:12 +0100 | [diff] [blame] | 413 | /** |
| 414 | * power_supply_get_by_phandle() - Search for a power supply and returns its ref |
| 415 | * @np: Pointer to device node holding phandle property |
| 416 | * @phandle_name: Name of property holding a power supply name |
| 417 | * |
| 418 | * If power supply was found, it increases reference count for the |
| 419 | * internal power supply's device. The user should power_supply_put() |
| 420 | * after usage. |
| 421 | * |
| 422 | * Return: On success returns a reference to a power supply with |
| 423 | * matching name equals to value under @property, NULL or ERR_PTR otherwise. |
| 424 | */ |
Sebastian Reichel | abce977 | 2013-11-24 17:49:29 +0100 | [diff] [blame] | 425 | struct power_supply *power_supply_get_by_phandle(struct device_node *np, |
| 426 | const char *property) |
| 427 | { |
| 428 | struct device_node *power_supply_np; |
Krzysztof Kozlowski | 03e81ac | 2015-03-12 08:44:13 +0100 | [diff] [blame] | 429 | struct power_supply *psy = NULL; |
Sebastian Reichel | abce977 | 2013-11-24 17:49:29 +0100 | [diff] [blame] | 430 | struct device *dev; |
| 431 | |
| 432 | power_supply_np = of_parse_phandle(np, property, 0); |
| 433 | if (!power_supply_np) |
| 434 | return ERR_PTR(-ENODEV); |
| 435 | |
| 436 | dev = class_find_device(power_supply_class, NULL, power_supply_np, |
| 437 | power_supply_match_device_node); |
| 438 | |
| 439 | of_node_put(power_supply_np); |
| 440 | |
Krzysztof Kozlowski | 03e81ac | 2015-03-12 08:44:13 +0100 | [diff] [blame] | 441 | if (dev) { |
| 442 | psy = dev_get_drvdata(dev); |
| 443 | atomic_inc(&psy->use_cnt); |
| 444 | } |
| 445 | |
| 446 | return psy; |
Sebastian Reichel | abce977 | 2013-11-24 17:49:29 +0100 | [diff] [blame] | 447 | } |
| 448 | EXPORT_SYMBOL_GPL(power_supply_get_by_phandle); |
Hans de Goede | fe27e1d | 2015-06-09 23:37:56 +0200 | [diff] [blame] | 449 | |
| 450 | static void devm_power_supply_put(struct device *dev, void *res) |
| 451 | { |
| 452 | struct power_supply **psy = res; |
| 453 | |
| 454 | power_supply_put(*psy); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * devm_power_supply_get_by_phandle() - Resource managed version of |
| 459 | * power_supply_get_by_phandle() |
| 460 | * @dev: Pointer to device holding phandle property |
| 461 | * @phandle_name: Name of property holding a power supply phandle |
| 462 | * |
| 463 | * Return: On success returns a reference to a power supply with |
| 464 | * matching name equals to value under @property, NULL or ERR_PTR otherwise. |
| 465 | */ |
| 466 | struct power_supply *devm_power_supply_get_by_phandle(struct device *dev, |
| 467 | const char *property) |
| 468 | { |
| 469 | struct power_supply **ptr, *psy; |
| 470 | |
| 471 | if (!dev->of_node) |
| 472 | return ERR_PTR(-ENODEV); |
| 473 | |
| 474 | ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL); |
| 475 | if (!ptr) |
| 476 | return ERR_PTR(-ENOMEM); |
| 477 | |
| 478 | psy = power_supply_get_by_phandle(dev->of_node, property); |
| 479 | if (IS_ERR_OR_NULL(psy)) { |
| 480 | devres_free(ptr); |
| 481 | } else { |
| 482 | *ptr = psy; |
| 483 | devres_add(dev, ptr); |
| 484 | } |
| 485 | return psy; |
| 486 | } |
| 487 | EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle); |
Sebastian Reichel | abce977 | 2013-11-24 17:49:29 +0100 | [diff] [blame] | 488 | #endif /* CONFIG_OF */ |
| 489 | |
Krzysztof Kozlowski | bc15405 | 2015-03-12 08:44:03 +0100 | [diff] [blame] | 490 | int power_supply_get_property(struct power_supply *psy, |
| 491 | enum power_supply_property psp, |
| 492 | union power_supply_propval *val) |
| 493 | { |
| 494 | if (atomic_read(&psy->use_cnt) <= 0) |
| 495 | return -ENODEV; |
| 496 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 497 | return psy->desc->get_property(psy, psp, val); |
Krzysztof Kozlowski | bc15405 | 2015-03-12 08:44:03 +0100 | [diff] [blame] | 498 | } |
| 499 | EXPORT_SYMBOL_GPL(power_supply_get_property); |
| 500 | |
| 501 | int power_supply_set_property(struct power_supply *psy, |
| 502 | enum power_supply_property psp, |
| 503 | const union power_supply_propval *val) |
| 504 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 505 | if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property) |
Krzysztof Kozlowski | bc15405 | 2015-03-12 08:44:03 +0100 | [diff] [blame] | 506 | return -ENODEV; |
| 507 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 508 | return psy->desc->set_property(psy, psp, val); |
Krzysztof Kozlowski | bc15405 | 2015-03-12 08:44:03 +0100 | [diff] [blame] | 509 | } |
| 510 | EXPORT_SYMBOL_GPL(power_supply_set_property); |
| 511 | |
| 512 | int power_supply_property_is_writeable(struct power_supply *psy, |
| 513 | enum power_supply_property psp) |
| 514 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 515 | if (atomic_read(&psy->use_cnt) <= 0 || |
| 516 | !psy->desc->property_is_writeable) |
Krzysztof Kozlowski | bc15405 | 2015-03-12 08:44:03 +0100 | [diff] [blame] | 517 | return -ENODEV; |
| 518 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 519 | return psy->desc->property_is_writeable(psy, psp); |
Krzysztof Kozlowski | bc15405 | 2015-03-12 08:44:03 +0100 | [diff] [blame] | 520 | } |
| 521 | EXPORT_SYMBOL_GPL(power_supply_property_is_writeable); |
| 522 | |
| 523 | void power_supply_external_power_changed(struct power_supply *psy) |
| 524 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 525 | if (atomic_read(&psy->use_cnt) <= 0 || |
| 526 | !psy->desc->external_power_changed) |
Krzysztof Kozlowski | bc15405 | 2015-03-12 08:44:03 +0100 | [diff] [blame] | 527 | return; |
| 528 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 529 | psy->desc->external_power_changed(psy); |
Krzysztof Kozlowski | bc15405 | 2015-03-12 08:44:03 +0100 | [diff] [blame] | 530 | } |
| 531 | EXPORT_SYMBOL_GPL(power_supply_external_power_changed); |
| 532 | |
Jeremy Fitzhardinge | 8351665 | 2011-12-07 09:15:45 -0800 | [diff] [blame] | 533 | int power_supply_powers(struct power_supply *psy, struct device *dev) |
| 534 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 535 | return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers"); |
Jeremy Fitzhardinge | 8351665 | 2011-12-07 09:15:45 -0800 | [diff] [blame] | 536 | } |
| 537 | EXPORT_SYMBOL_GPL(power_supply_powers); |
| 538 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 539 | static void power_supply_dev_release(struct device *dev) |
| 540 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 541 | struct power_supply *psy = container_of(dev, struct power_supply, dev); |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 542 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 543 | kfree(psy); |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Pali Rohár | d36240d | 2013-11-19 11:18:03 +0100 | [diff] [blame] | 546 | int power_supply_reg_notifier(struct notifier_block *nb) |
| 547 | { |
| 548 | return atomic_notifier_chain_register(&power_supply_notifier, nb); |
| 549 | } |
| 550 | EXPORT_SYMBOL_GPL(power_supply_reg_notifier); |
| 551 | |
| 552 | void power_supply_unreg_notifier(struct notifier_block *nb) |
| 553 | { |
| 554 | atomic_notifier_chain_unregister(&power_supply_notifier, nb); |
| 555 | } |
| 556 | EXPORT_SYMBOL_GPL(power_supply_unreg_notifier); |
| 557 | |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 558 | #ifdef CONFIG_THERMAL |
| 559 | static int power_supply_read_temp(struct thermal_zone_device *tzd, |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 560 | int *temp) |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 561 | { |
| 562 | struct power_supply *psy; |
| 563 | union power_supply_propval val; |
| 564 | int ret; |
| 565 | |
| 566 | WARN_ON(tzd == NULL); |
| 567 | psy = tzd->devdata; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 568 | ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val); |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 569 | |
| 570 | /* Convert tenths of degree Celsius to milli degree Celsius. */ |
| 571 | if (!ret) |
| 572 | *temp = val.intval * 100; |
| 573 | |
| 574 | return ret; |
| 575 | } |
| 576 | |
| 577 | static struct thermal_zone_device_ops psy_tzd_ops = { |
| 578 | .get_temp = power_supply_read_temp, |
| 579 | }; |
| 580 | |
| 581 | static int psy_register_thermal(struct power_supply *psy) |
| 582 | { |
| 583 | int i; |
| 584 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 585 | if (psy->desc->no_thermal) |
Krzysztof Kozlowski | a69d82b | 2014-10-07 17:47:36 +0200 | [diff] [blame] | 586 | return 0; |
| 587 | |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 588 | /* Register battery zone device psy reports temperature */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 589 | for (i = 0; i < psy->desc->num_properties; i++) { |
| 590 | if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) { |
| 591 | psy->tzd = thermal_zone_device_register(psy->desc->name, |
| 592 | 0, 0, psy, &psy_tzd_ops, NULL, 0, 0); |
Viresh Kumar | 9d2410c | 2014-09-04 17:31:33 +0530 | [diff] [blame] | 593 | return PTR_ERR_OR_ZERO(psy->tzd); |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static void psy_unregister_thermal(struct power_supply *psy) |
| 600 | { |
| 601 | if (IS_ERR_OR_NULL(psy->tzd)) |
| 602 | return; |
| 603 | thermal_zone_device_unregister(psy->tzd); |
| 604 | } |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 605 | |
| 606 | /* thermal cooling device callbacks */ |
| 607 | static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd, |
| 608 | unsigned long *state) |
| 609 | { |
| 610 | struct power_supply *psy; |
| 611 | union power_supply_propval val; |
| 612 | int ret; |
| 613 | |
| 614 | psy = tcd->devdata; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 615 | ret = psy->desc->get_property(psy, |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 616 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val); |
| 617 | if (!ret) |
| 618 | *state = val.intval; |
| 619 | |
| 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd, |
| 624 | unsigned long *state) |
| 625 | { |
| 626 | struct power_supply *psy; |
| 627 | union power_supply_propval val; |
| 628 | int ret; |
| 629 | |
| 630 | psy = tcd->devdata; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 631 | ret = psy->desc->get_property(psy, |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 632 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); |
| 633 | if (!ret) |
| 634 | *state = val.intval; |
| 635 | |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd, |
| 640 | unsigned long state) |
| 641 | { |
| 642 | struct power_supply *psy; |
| 643 | union power_supply_propval val; |
| 644 | int ret; |
| 645 | |
| 646 | psy = tcd->devdata; |
| 647 | val.intval = state; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 648 | ret = psy->desc->set_property(psy, |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 649 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); |
| 650 | |
| 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | static struct thermal_cooling_device_ops psy_tcd_ops = { |
| 655 | .get_max_state = ps_get_max_charge_cntl_limit, |
| 656 | .get_cur_state = ps_get_cur_chrage_cntl_limit, |
| 657 | .set_cur_state = ps_set_cur_charge_cntl_limit, |
| 658 | }; |
| 659 | |
| 660 | static int psy_register_cooler(struct power_supply *psy) |
| 661 | { |
| 662 | int i; |
| 663 | |
| 664 | /* Register for cooling device if psy can control charging */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 665 | for (i = 0; i < psy->desc->num_properties; i++) { |
| 666 | if (psy->desc->properties[i] == |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 667 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) { |
| 668 | psy->tcd = thermal_cooling_device_register( |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 669 | (char *)psy->desc->name, |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 670 | psy, &psy_tcd_ops); |
Viresh Kumar | 9d2410c | 2014-09-04 17:31:33 +0530 | [diff] [blame] | 671 | return PTR_ERR_OR_ZERO(psy->tcd); |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static void psy_unregister_cooler(struct power_supply *psy) |
| 678 | { |
| 679 | if (IS_ERR_OR_NULL(psy->tcd)) |
| 680 | return; |
| 681 | thermal_cooling_device_unregister(psy->tcd); |
| 682 | } |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 683 | #else |
| 684 | static int psy_register_thermal(struct power_supply *psy) |
| 685 | { |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static void psy_unregister_thermal(struct power_supply *psy) |
| 690 | { |
| 691 | } |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 692 | |
| 693 | static int psy_register_cooler(struct power_supply *psy) |
| 694 | { |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | static void psy_unregister_cooler(struct power_supply *psy) |
| 699 | { |
| 700 | } |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 701 | #endif |
| 702 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 703 | static struct power_supply *__must_check |
| 704 | __power_supply_register(struct device *parent, |
| 705 | const struct power_supply_desc *desc, |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 706 | const struct power_supply_config *cfg, |
| 707 | bool ws) |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 708 | { |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 709 | struct device *dev; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 710 | struct power_supply *psy; |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 711 | int rc; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 712 | |
Krzysztof Kozlowski | 7f1a57f | 2015-05-19 16:13:02 +0900 | [diff] [blame] | 713 | if (!parent) |
| 714 | pr_warn("%s: Expected proper parent device for '%s'\n", |
| 715 | __func__, desc->name); |
| 716 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 717 | psy = kzalloc(sizeof(*psy), GFP_KERNEL); |
| 718 | if (!psy) |
| 719 | return ERR_PTR(-ENOMEM); |
| 720 | |
| 721 | dev = &psy->dev; |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 722 | |
| 723 | device_initialize(dev); |
| 724 | |
| 725 | dev->class = power_supply_class; |
| 726 | dev->type = &power_supply_dev_type; |
| 727 | dev->parent = parent; |
| 728 | dev->release = power_supply_dev_release; |
| 729 | dev_set_drvdata(dev, psy); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 730 | psy->desc = desc; |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 731 | if (cfg) { |
| 732 | psy->drv_data = cfg->drv_data; |
| 733 | psy->of_node = cfg->of_node; |
| 734 | psy->supplied_to = cfg->supplied_to; |
| 735 | psy->num_supplicants = cfg->num_supplicants; |
| 736 | } |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 737 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 738 | rc = dev_set_name(dev, "%s", desc->name); |
Shuah Khan | 80c6463 | 2013-11-22 10:54:28 -0700 | [diff] [blame] | 739 | if (rc) |
| 740 | goto dev_set_name_failed; |
| 741 | |
Lars-Peter Clausen | 9777467 | 2011-02-21 15:34:19 +0100 | [diff] [blame] | 742 | INIT_WORK(&psy->changed_work, power_supply_changed_work); |
Krzysztof Kozlowski | 7f1a57f | 2015-05-19 16:13:02 +0900 | [diff] [blame] | 743 | INIT_DELAYED_WORK(&psy->deferred_register_work, |
| 744 | power_supply_deferred_register_work); |
Lars-Peter Clausen | 9777467 | 2011-02-21 15:34:19 +0100 | [diff] [blame] | 745 | |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 746 | rc = power_supply_check_supplies(psy); |
| 747 | if (rc) { |
| 748 | dev_info(dev, "Not all required supplies found, defer probe\n"); |
| 749 | goto check_supplies_failed; |
| 750 | } |
| 751 | |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 752 | spin_lock_init(&psy->changed_lock); |
Zhang Rui | 9113e26 | 2014-05-28 15:23:37 +0800 | [diff] [blame] | 753 | rc = device_init_wakeup(dev, ws); |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 754 | if (rc) |
| 755 | goto wakeup_init_failed; |
| 756 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 757 | rc = device_add(dev); |
| 758 | if (rc) |
| 759 | goto device_add_failed; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 760 | |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 761 | rc = psy_register_thermal(psy); |
| 762 | if (rc) |
| 763 | goto register_thermal_failed; |
| 764 | |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 765 | rc = psy_register_cooler(psy); |
| 766 | if (rc) |
| 767 | goto register_cooler_failed; |
| 768 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 769 | rc = power_supply_create_triggers(psy); |
| 770 | if (rc) |
| 771 | goto create_triggers_failed; |
| 772 | |
Krzysztof Kozlowski | 8e59c7f | 2015-05-19 16:13:01 +0900 | [diff] [blame] | 773 | /* |
| 774 | * Update use_cnt after any uevents (most notably from device_add()). |
| 775 | * We are here still during driver's probe but |
| 776 | * the power_supply_uevent() calls back driver's get_property |
| 777 | * method so: |
| 778 | * 1. Driver did not assigned the returned struct power_supply, |
| 779 | * 2. Driver could not finish initialization (anything in its probe |
| 780 | * after calling power_supply_register()). |
| 781 | */ |
| 782 | atomic_inc(&psy->use_cnt); |
Krzysztof Kozlowski | 7f1a57f | 2015-05-19 16:13:02 +0900 | [diff] [blame] | 783 | |
| 784 | queue_delayed_work(system_power_efficient_wq, |
| 785 | &psy->deferred_register_work, |
| 786 | POWER_SUPPLY_DEFERRED_REGISTER_TIME); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 787 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 788 | return psy; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 789 | |
| 790 | create_triggers_failed: |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 791 | psy_unregister_cooler(psy); |
| 792 | register_cooler_failed: |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 793 | psy_unregister_thermal(psy); |
| 794 | register_thermal_failed: |
Vasiliy Kulikov | 3a2dbd6 | 2010-11-19 21:41:58 +0300 | [diff] [blame] | 795 | device_del(dev); |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 796 | device_add_failed: |
Shuah Khan | 80c6463 | 2013-11-22 10:54:28 -0700 | [diff] [blame] | 797 | wakeup_init_failed: |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 798 | check_supplies_failed: |
Shuah Khan | 80c6463 | 2013-11-22 10:54:28 -0700 | [diff] [blame] | 799 | dev_set_name_failed: |
Vasiliy Kulikov | 3a2dbd6 | 2010-11-19 21:41:58 +0300 | [diff] [blame] | 800 | put_device(dev); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 801 | return ERR_PTR(rc); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 802 | } |
Zhang Rui | 9113e26 | 2014-05-28 15:23:37 +0800 | [diff] [blame] | 803 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 804 | /** |
| 805 | * power_supply_register() - Register new power supply |
Krzysztof Kozlowski | 7f1a57f | 2015-05-19 16:13:02 +0900 | [diff] [blame] | 806 | * @parent: Device to be a parent of power supply's device, usually |
| 807 | * the device which probe function calls this |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 808 | * @desc: Description of power supply, must be valid through whole |
| 809 | * lifetime of this power supply |
| 810 | * @cfg: Run-time specific configuration accessed during registering, |
| 811 | * may be NULL |
| 812 | * |
| 813 | * Return: A pointer to newly allocated power_supply on success |
| 814 | * or ERR_PTR otherwise. |
| 815 | * Use power_supply_unregister() on returned power_supply pointer to release |
| 816 | * resources. |
| 817 | */ |
| 818 | struct power_supply *__must_check power_supply_register(struct device *parent, |
| 819 | const struct power_supply_desc *desc, |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 820 | const struct power_supply_config *cfg) |
Zhang Rui | 9113e26 | 2014-05-28 15:23:37 +0800 | [diff] [blame] | 821 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 822 | return __power_supply_register(parent, desc, cfg, true); |
Zhang Rui | 9113e26 | 2014-05-28 15:23:37 +0800 | [diff] [blame] | 823 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 824 | EXPORT_SYMBOL_GPL(power_supply_register); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 825 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 826 | /** |
Bjorn Andersson | 43df610 | 2015-06-18 12:35:26 -0700 | [diff] [blame] | 827 | * power_supply_register_no_ws() - Register new non-waking-source power supply |
Krzysztof Kozlowski | 7f1a57f | 2015-05-19 16:13:02 +0900 | [diff] [blame] | 828 | * @parent: Device to be a parent of power supply's device, usually |
| 829 | * the device which probe function calls this |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 830 | * @desc: Description of power supply, must be valid through whole |
| 831 | * lifetime of this power supply |
| 832 | * @cfg: Run-time specific configuration accessed during registering, |
| 833 | * may be NULL |
| 834 | * |
| 835 | * Return: A pointer to newly allocated power_supply on success |
| 836 | * or ERR_PTR otherwise. |
| 837 | * Use power_supply_unregister() on returned power_supply pointer to release |
| 838 | * resources. |
| 839 | */ |
| 840 | struct power_supply *__must_check |
| 841 | power_supply_register_no_ws(struct device *parent, |
| 842 | const struct power_supply_desc *desc, |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 843 | const struct power_supply_config *cfg) |
Zhang Rui | 9113e26 | 2014-05-28 15:23:37 +0800 | [diff] [blame] | 844 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 845 | return __power_supply_register(parent, desc, cfg, false); |
Zhang Rui | 9113e26 | 2014-05-28 15:23:37 +0800 | [diff] [blame] | 846 | } |
| 847 | EXPORT_SYMBOL_GPL(power_supply_register_no_ws); |
| 848 | |
NeilBrown | 5d8a421 | 2015-02-24 15:33:50 +1100 | [diff] [blame] | 849 | static void devm_power_supply_release(struct device *dev, void *res) |
| 850 | { |
| 851 | struct power_supply **psy = res; |
| 852 | |
| 853 | power_supply_unregister(*psy); |
| 854 | } |
| 855 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 856 | /** |
Bjorn Andersson | 43df610 | 2015-06-18 12:35:26 -0700 | [diff] [blame] | 857 | * devm_power_supply_register() - Register managed power supply |
Krzysztof Kozlowski | 7f1a57f | 2015-05-19 16:13:02 +0900 | [diff] [blame] | 858 | * @parent: Device to be a parent of power supply's device, usually |
| 859 | * the device which probe function calls this |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 860 | * @desc: Description of power supply, must be valid through whole |
| 861 | * lifetime of this power supply |
| 862 | * @cfg: Run-time specific configuration accessed during registering, |
| 863 | * may be NULL |
| 864 | * |
| 865 | * Return: A pointer to newly allocated power_supply on success |
| 866 | * or ERR_PTR otherwise. |
| 867 | * The returned power_supply pointer will be automatically unregistered |
| 868 | * on driver detach. |
| 869 | */ |
| 870 | struct power_supply *__must_check |
| 871 | devm_power_supply_register(struct device *parent, |
| 872 | const struct power_supply_desc *desc, |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 873 | const struct power_supply_config *cfg) |
NeilBrown | 5d8a421 | 2015-02-24 15:33:50 +1100 | [diff] [blame] | 874 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 875 | struct power_supply **ptr, *psy; |
| 876 | |
| 877 | ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL); |
NeilBrown | 5d8a421 | 2015-02-24 15:33:50 +1100 | [diff] [blame] | 878 | |
| 879 | if (!ptr) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 880 | return ERR_PTR(-ENOMEM); |
| 881 | psy = __power_supply_register(parent, desc, cfg, true); |
| 882 | if (IS_ERR(psy)) { |
NeilBrown | 5d8a421 | 2015-02-24 15:33:50 +1100 | [diff] [blame] | 883 | devres_free(ptr); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 884 | } else { |
NeilBrown | 5d8a421 | 2015-02-24 15:33:50 +1100 | [diff] [blame] | 885 | *ptr = psy; |
| 886 | devres_add(parent, ptr); |
| 887 | } |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 888 | return psy; |
NeilBrown | 5d8a421 | 2015-02-24 15:33:50 +1100 | [diff] [blame] | 889 | } |
| 890 | EXPORT_SYMBOL_GPL(devm_power_supply_register); |
| 891 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 892 | /** |
Bjorn Andersson | 43df610 | 2015-06-18 12:35:26 -0700 | [diff] [blame] | 893 | * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply |
Krzysztof Kozlowski | 7f1a57f | 2015-05-19 16:13:02 +0900 | [diff] [blame] | 894 | * @parent: Device to be a parent of power supply's device, usually |
| 895 | * the device which probe function calls this |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 896 | * @desc: Description of power supply, must be valid through whole |
| 897 | * lifetime of this power supply |
| 898 | * @cfg: Run-time specific configuration accessed during registering, |
| 899 | * may be NULL |
| 900 | * |
| 901 | * Return: A pointer to newly allocated power_supply on success |
| 902 | * or ERR_PTR otherwise. |
| 903 | * The returned power_supply pointer will be automatically unregistered |
| 904 | * on driver detach. |
| 905 | */ |
| 906 | struct power_supply *__must_check |
| 907 | devm_power_supply_register_no_ws(struct device *parent, |
| 908 | const struct power_supply_desc *desc, |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 909 | const struct power_supply_config *cfg) |
NeilBrown | 5d8a421 | 2015-02-24 15:33:50 +1100 | [diff] [blame] | 910 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 911 | struct power_supply **ptr, *psy; |
| 912 | |
| 913 | ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL); |
NeilBrown | 5d8a421 | 2015-02-24 15:33:50 +1100 | [diff] [blame] | 914 | |
| 915 | if (!ptr) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 916 | return ERR_PTR(-ENOMEM); |
| 917 | psy = __power_supply_register(parent, desc, cfg, false); |
| 918 | if (IS_ERR(psy)) { |
NeilBrown | 5d8a421 | 2015-02-24 15:33:50 +1100 | [diff] [blame] | 919 | devres_free(ptr); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 920 | } else { |
NeilBrown | 5d8a421 | 2015-02-24 15:33:50 +1100 | [diff] [blame] | 921 | *ptr = psy; |
| 922 | devres_add(parent, ptr); |
| 923 | } |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 924 | return psy; |
NeilBrown | 5d8a421 | 2015-02-24 15:33:50 +1100 | [diff] [blame] | 925 | } |
| 926 | EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws); |
| 927 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 928 | /** |
| 929 | * power_supply_unregister() - Remove this power supply from system |
| 930 | * @psy: Pointer to power supply to unregister |
| 931 | * |
| 932 | * Remove this power supply from the system. The resources of power supply |
| 933 | * will be freed here or on last power_supply_put() call. |
| 934 | */ |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 935 | void power_supply_unregister(struct power_supply *psy) |
| 936 | { |
Krzysztof Kozlowski | bc15405 | 2015-03-12 08:44:03 +0100 | [diff] [blame] | 937 | WARN_ON(atomic_dec_return(&psy->use_cnt)); |
Tejun Heo | bc51e7f | 2010-12-11 17:51:45 +0100 | [diff] [blame] | 938 | cancel_work_sync(&psy->changed_work); |
Krzysztof Kozlowski | 7f1a57f | 2015-05-19 16:13:02 +0900 | [diff] [blame] | 939 | cancel_delayed_work_sync(&psy->deferred_register_work); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 940 | sysfs_remove_link(&psy->dev.kobj, "powers"); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 941 | power_supply_remove_triggers(psy); |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 942 | psy_unregister_cooler(psy); |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 943 | psy_unregister_thermal(psy); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 944 | device_init_wakeup(&psy->dev, false); |
| 945 | device_unregister(&psy->dev); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 946 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 947 | EXPORT_SYMBOL_GPL(power_supply_unregister); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 948 | |
Krzysztof Kozlowski | e44ea36 | 2015-03-12 08:44:01 +0100 | [diff] [blame] | 949 | void *power_supply_get_drvdata(struct power_supply *psy) |
| 950 | { |
| 951 | return psy->drv_data; |
| 952 | } |
| 953 | EXPORT_SYMBOL_GPL(power_supply_get_drvdata); |
| 954 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 955 | static int __init power_supply_class_init(void) |
| 956 | { |
| 957 | power_supply_class = class_create(THIS_MODULE, "power_supply"); |
| 958 | |
| 959 | if (IS_ERR(power_supply_class)) |
| 960 | return PTR_ERR(power_supply_class); |
| 961 | |
| 962 | power_supply_class->dev_uevent = power_supply_uevent; |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 963 | power_supply_init_attrs(&power_supply_dev_type); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 964 | |
| 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | static void __exit power_supply_class_exit(void) |
| 969 | { |
| 970 | class_destroy(power_supply_class); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 971 | } |
| 972 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 973 | subsys_initcall(power_supply_class_init); |
| 974 | module_exit(power_supply_class_exit); |
| 975 | |
| 976 | MODULE_DESCRIPTION("Universal power supply monitor class"); |
| 977 | MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, " |
| 978 | "Szabolcs Gyurko, " |
| 979 | "Anton Vorontsov <cbou@mail.ru>"); |
| 980 | MODULE_LICENSE("GPL"); |