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 | |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 33 | static bool __power_supply_is_supplied_by(struct power_supply *supplier, |
| 34 | struct power_supply *supply) |
| 35 | { |
| 36 | int i; |
| 37 | |
| 38 | if (!supply->supplied_from && !supplier->supplied_to) |
| 39 | return false; |
| 40 | |
| 41 | /* Support both supplied_to and supplied_from modes */ |
| 42 | if (supply->supplied_from) { |
| 43 | if (!supplier->name) |
| 44 | return false; |
| 45 | for (i = 0; i < supply->num_supplies; i++) |
| 46 | if (!strcmp(supplier->name, supply->supplied_from[i])) |
| 47 | return true; |
| 48 | } else { |
| 49 | if (!supply->name) |
| 50 | return false; |
| 51 | for (i = 0; i < supplier->num_supplicants; i++) |
| 52 | if (!strcmp(supplier->supplied_to[i], supply->name)) |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 59 | static int __power_supply_changed_work(struct device *dev, void *data) |
| 60 | { |
Viresh Kumar | e80cf42 | 2014-09-04 17:31:26 +0530 | [diff] [blame] | 61 | struct power_supply *psy = data; |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 62 | struct power_supply *pst = dev_get_drvdata(dev); |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 63 | |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 64 | if (__power_supply_is_supplied_by(psy, pst)) { |
| 65 | if (pst->external_power_changed) |
| 66 | pst->external_power_changed(pst); |
| 67 | } |
| 68 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 72 | static void power_supply_changed_work(struct work_struct *work) |
| 73 | { |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 74 | unsigned long flags; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 75 | struct power_supply *psy = container_of(work, struct power_supply, |
| 76 | changed_work); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 77 | |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 78 | dev_dbg(psy->dev, "%s\n", __func__); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 79 | |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 80 | spin_lock_irqsave(&psy->changed_lock, flags); |
Viresh Kumar | 061f380 | 2014-09-04 17:31:32 +0530 | [diff] [blame] | 81 | /* |
| 82 | * Check 'changed' here to avoid issues due to race between |
| 83 | * power_supply_changed() and this routine. In worst case |
| 84 | * power_supply_changed() can be called again just before we take above |
| 85 | * lock. During the first call of this routine we will mark 'changed' as |
| 86 | * false and it will stay false for the next call as well. |
| 87 | */ |
| 88 | if (likely(psy->changed)) { |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 89 | psy->changed = false; |
| 90 | spin_unlock_irqrestore(&psy->changed_lock, flags); |
| 91 | class_for_each_device(power_supply_class, NULL, psy, |
| 92 | __power_supply_changed_work); |
| 93 | power_supply_update_leds(psy); |
Pali Rohár | d36240d | 2013-11-19 11:18:03 +0100 | [diff] [blame] | 94 | atomic_notifier_call_chain(&power_supply_notifier, |
| 95 | PSY_EVENT_PROP_CHANGED, psy); |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 96 | kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE); |
| 97 | spin_lock_irqsave(&psy->changed_lock, flags); |
| 98 | } |
Viresh Kumar | 061f380 | 2014-09-04 17:31:32 +0530 | [diff] [blame] | 99 | |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 100 | /* |
Viresh Kumar | 061f380 | 2014-09-04 17:31:32 +0530 | [diff] [blame] | 101 | * Hold the wakeup_source until all events are processed. |
| 102 | * power_supply_changed() might have called again and have set 'changed' |
| 103 | * to true. |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 104 | */ |
Viresh Kumar | 061f380 | 2014-09-04 17:31:32 +0530 | [diff] [blame] | 105 | if (likely(!psy->changed)) |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 106 | pm_relax(psy->dev); |
| 107 | spin_unlock_irqrestore(&psy->changed_lock, flags); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void power_supply_changed(struct power_supply *psy) |
| 111 | { |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 112 | unsigned long flags; |
| 113 | |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 114 | dev_dbg(psy->dev, "%s\n", __func__); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 115 | |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 116 | spin_lock_irqsave(&psy->changed_lock, flags); |
| 117 | psy->changed = true; |
| 118 | pm_stay_awake(psy->dev); |
| 119 | spin_unlock_irqrestore(&psy->changed_lock, flags); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 120 | schedule_work(&psy->changed_work); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 121 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 122 | EXPORT_SYMBOL_GPL(power_supply_changed); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 123 | |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 124 | #ifdef CONFIG_OF |
| 125 | #include <linux/of.h> |
| 126 | |
| 127 | static int __power_supply_populate_supplied_from(struct device *dev, |
| 128 | void *data) |
| 129 | { |
Viresh Kumar | e80cf42 | 2014-09-04 17:31:26 +0530 | [diff] [blame] | 130 | struct power_supply *psy = data; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 131 | struct power_supply *epsy = dev_get_drvdata(dev); |
| 132 | struct device_node *np; |
| 133 | int i = 0; |
| 134 | |
| 135 | do { |
| 136 | np = of_parse_phandle(psy->of_node, "power-supplies", i++); |
| 137 | if (!np) |
Viresh Kumar | a0f93b4 | 2014-09-04 17:31:27 +0530 | [diff] [blame] | 138 | break; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 139 | |
| 140 | if (np == epsy->of_node) { |
| 141 | dev_info(psy->dev, "%s: Found supply : %s\n", |
| 142 | psy->name, epsy->name); |
| 143 | psy->supplied_from[i-1] = (char *)epsy->name; |
| 144 | psy->num_supplies++; |
Rhyland Klein | 2054d6e | 2013-06-10 17:26:39 -0400 | [diff] [blame] | 145 | of_node_put(np); |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 146 | break; |
| 147 | } |
Rhyland Klein | 2054d6e | 2013-06-10 17:26:39 -0400 | [diff] [blame] | 148 | of_node_put(np); |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 149 | } while (np); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static int power_supply_populate_supplied_from(struct power_supply *psy) |
| 155 | { |
| 156 | int error; |
| 157 | |
| 158 | error = class_for_each_device(power_supply_class, NULL, psy, |
| 159 | __power_supply_populate_supplied_from); |
| 160 | |
| 161 | dev_dbg(psy->dev, "%s %d\n", __func__, error); |
| 162 | |
| 163 | return error; |
| 164 | } |
| 165 | |
| 166 | static int __power_supply_find_supply_from_node(struct device *dev, |
| 167 | void *data) |
| 168 | { |
Viresh Kumar | e80cf42 | 2014-09-04 17:31:26 +0530 | [diff] [blame] | 169 | struct device_node *np = data; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 170 | struct power_supply *epsy = dev_get_drvdata(dev); |
| 171 | |
Viresh Kumar | 585b008 | 2014-09-04 17:31:30 +0530 | [diff] [blame] | 172 | /* returning non-zero breaks out of class_for_each_device loop */ |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 173 | if (epsy->of_node == np) |
Viresh Kumar | 585b008 | 2014-09-04 17:31:30 +0530 | [diff] [blame] | 174 | return 1; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int power_supply_find_supply_from_node(struct device_node *supply_node) |
| 180 | { |
| 181 | int error; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 182 | |
| 183 | /* |
Viresh Kumar | 585b008 | 2014-09-04 17:31:30 +0530 | [diff] [blame] | 184 | * class_for_each_device() either returns its own errors or values |
| 185 | * returned by __power_supply_find_supply_from_node(). |
| 186 | * |
| 187 | * __power_supply_find_supply_from_node() will return 0 (no match) |
| 188 | * or 1 (match). |
| 189 | * |
| 190 | * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if |
| 191 | * it returned 0, or error as returned by it. |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 192 | */ |
| 193 | error = class_for_each_device(power_supply_class, NULL, supply_node, |
| 194 | __power_supply_find_supply_from_node); |
| 195 | |
Viresh Kumar | 585b008 | 2014-09-04 17:31:30 +0530 | [diff] [blame] | 196 | return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static int power_supply_check_supplies(struct power_supply *psy) |
| 200 | { |
| 201 | struct device_node *np; |
| 202 | int cnt = 0; |
| 203 | |
| 204 | /* If there is already a list honor it */ |
| 205 | if (psy->supplied_from && psy->num_supplies > 0) |
| 206 | return 0; |
| 207 | |
| 208 | /* No device node found, nothing to do */ |
| 209 | if (!psy->of_node) |
| 210 | return 0; |
| 211 | |
| 212 | do { |
| 213 | int ret; |
| 214 | |
| 215 | np = of_parse_phandle(psy->of_node, "power-supplies", cnt++); |
| 216 | if (!np) |
Viresh Kumar | a0f93b4 | 2014-09-04 17:31:27 +0530 | [diff] [blame] | 217 | break; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 218 | |
| 219 | ret = power_supply_find_supply_from_node(np); |
Viresh Kumar | 8468b02 | 2014-09-04 17:31:28 +0530 | [diff] [blame] | 220 | of_node_put(np); |
| 221 | |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 222 | if (ret) { |
Viresh Kumar | f5b89af | 2014-09-04 17:31:29 +0530 | [diff] [blame] | 223 | dev_dbg(psy->dev, "Failed to find supply!\n"); |
| 224 | return ret; |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 225 | } |
| 226 | } while (np); |
| 227 | |
Viresh Kumar | f9c8548 | 2014-09-04 17:31:23 +0530 | [diff] [blame] | 228 | /* Missing valid "power-supplies" entries */ |
| 229 | if (cnt == 1) |
| 230 | return 0; |
| 231 | |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 232 | /* All supplies found, allocate char ** array for filling */ |
| 233 | psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from), |
| 234 | GFP_KERNEL); |
| 235 | if (!psy->supplied_from) { |
| 236 | dev_err(psy->dev, "Couldn't allocate memory for supply list\n"); |
| 237 | return -ENOMEM; |
| 238 | } |
| 239 | |
Viresh Kumar | 8f5a37c | 2014-09-04 17:31:22 +0530 | [diff] [blame] | 240 | *psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * (cnt - 1), |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 241 | GFP_KERNEL); |
| 242 | if (!*psy->supplied_from) { |
| 243 | dev_err(psy->dev, "Couldn't allocate memory for supply list\n"); |
| 244 | return -ENOMEM; |
| 245 | } |
| 246 | |
| 247 | return power_supply_populate_supplied_from(psy); |
| 248 | } |
| 249 | #else |
| 250 | static inline int power_supply_check_supplies(struct power_supply *psy) |
| 251 | { |
| 252 | return 0; |
| 253 | } |
| 254 | #endif |
| 255 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 256 | static int __power_supply_am_i_supplied(struct device *dev, void *data) |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 257 | { |
| 258 | union power_supply_propval ret = {0,}; |
Viresh Kumar | e80cf42 | 2014-09-04 17:31:26 +0530 | [diff] [blame] | 259 | struct power_supply *psy = data; |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 260 | struct power_supply *epsy = dev_get_drvdata(dev); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 261 | |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 262 | if (__power_supply_is_supplied_by(epsy, psy)) |
Viresh Kumar | 1c42a38 | 2014-09-04 17:31:31 +0530 | [diff] [blame] | 263 | if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) |
| 264 | return ret.intval; |
Rhyland Klein | 5e0848c | 2013-04-01 17:45:54 -0400 | [diff] [blame] | 265 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 266 | return 0; |
| 267 | } |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 268 | |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 269 | int power_supply_am_i_supplied(struct power_supply *psy) |
| 270 | { |
| 271 | int error; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 272 | |
Greg Kroah-Hartman | 93562b5 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 273 | error = class_for_each_device(power_supply_class, NULL, psy, |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 274 | __power_supply_am_i_supplied); |
| 275 | |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 276 | dev_dbg(psy->dev, "%s %d\n", __func__, error); |
Dave Young | 443cad9 | 2008-01-22 13:58:22 +0800 | [diff] [blame] | 277 | |
| 278 | return error; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 279 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 280 | EXPORT_SYMBOL_GPL(power_supply_am_i_supplied); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 281 | |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 282 | static int __power_supply_is_system_supplied(struct device *dev, void *data) |
| 283 | { |
| 284 | union power_supply_propval ret = {0,}; |
| 285 | struct power_supply *psy = dev_get_drvdata(dev); |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 286 | unsigned int *count = data; |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 287 | |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 288 | (*count)++; |
Viresh Kumar | 1c42a38 | 2014-09-04 17:31:31 +0530 | [diff] [blame] | 289 | if (psy->type != POWER_SUPPLY_TYPE_BATTERY) |
| 290 | if (!psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret)) |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 291 | return ret.intval; |
Viresh Kumar | 1c42a38 | 2014-09-04 17:31:31 +0530 | [diff] [blame] | 292 | |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | int power_supply_is_system_supplied(void) |
| 297 | { |
| 298 | int error; |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 299 | unsigned int count = 0; |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 300 | |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 301 | error = class_for_each_device(power_supply_class, NULL, &count, |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 302 | __power_supply_is_system_supplied); |
| 303 | |
Jean Delvare | 2530daa | 2011-12-10 22:53:36 +0100 | [diff] [blame] | 304 | /* |
| 305 | * If no power class device was found at all, most probably we are |
| 306 | * running on a desktop system, so assume we are on mains power. |
| 307 | */ |
| 308 | if (count == 0) |
| 309 | return 1; |
| 310 | |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 311 | return error; |
| 312 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 313 | EXPORT_SYMBOL_GPL(power_supply_is_system_supplied); |
Matthew Garrett | 942ed16 | 2008-08-26 21:09:59 +0100 | [diff] [blame] | 314 | |
Daniel Mack | e5f5ccb | 2009-07-23 20:35:53 +0200 | [diff] [blame] | 315 | int power_supply_set_battery_charged(struct power_supply *psy) |
| 316 | { |
| 317 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) { |
| 318 | psy->set_charged(psy); |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | return -EINVAL; |
| 323 | } |
| 324 | EXPORT_SYMBOL_GPL(power_supply_set_battery_charged); |
| 325 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 326 | 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] | 327 | { |
| 328 | const char *name = data; |
| 329 | struct power_supply *psy = dev_get_drvdata(dev); |
| 330 | |
| 331 | return strcmp(psy->name, name) == 0; |
| 332 | } |
| 333 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 334 | struct power_supply *power_supply_get_by_name(const char *name) |
Daniel Mack | e5f5ccb | 2009-07-23 20:35:53 +0200 | [diff] [blame] | 335 | { |
| 336 | struct device *dev = class_find_device(power_supply_class, NULL, name, |
| 337 | power_supply_match_device_by_name); |
| 338 | |
| 339 | return dev ? dev_get_drvdata(dev) : NULL; |
| 340 | } |
| 341 | EXPORT_SYMBOL_GPL(power_supply_get_by_name); |
| 342 | |
Sebastian Reichel | abce977 | 2013-11-24 17:49:29 +0100 | [diff] [blame] | 343 | #ifdef CONFIG_OF |
| 344 | static int power_supply_match_device_node(struct device *dev, const void *data) |
| 345 | { |
| 346 | return dev->parent && dev->parent->of_node == data; |
| 347 | } |
| 348 | |
| 349 | struct power_supply *power_supply_get_by_phandle(struct device_node *np, |
| 350 | const char *property) |
| 351 | { |
| 352 | struct device_node *power_supply_np; |
| 353 | struct device *dev; |
| 354 | |
| 355 | power_supply_np = of_parse_phandle(np, property, 0); |
| 356 | if (!power_supply_np) |
| 357 | return ERR_PTR(-ENODEV); |
| 358 | |
| 359 | dev = class_find_device(power_supply_class, NULL, power_supply_np, |
| 360 | power_supply_match_device_node); |
| 361 | |
| 362 | of_node_put(power_supply_np); |
| 363 | |
| 364 | return dev ? dev_get_drvdata(dev) : NULL; |
| 365 | } |
| 366 | EXPORT_SYMBOL_GPL(power_supply_get_by_phandle); |
| 367 | #endif /* CONFIG_OF */ |
| 368 | |
Jeremy Fitzhardinge | 8351665 | 2011-12-07 09:15:45 -0800 | [diff] [blame] | 369 | int power_supply_powers(struct power_supply *psy, struct device *dev) |
| 370 | { |
Anton Vorontsov | 93278d1 | 2012-01-05 19:17:25 +0400 | [diff] [blame] | 371 | return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers"); |
Jeremy Fitzhardinge | 8351665 | 2011-12-07 09:15:45 -0800 | [diff] [blame] | 372 | } |
| 373 | EXPORT_SYMBOL_GPL(power_supply_powers); |
| 374 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 375 | static void power_supply_dev_release(struct device *dev) |
| 376 | { |
| 377 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
| 378 | kfree(dev); |
| 379 | } |
| 380 | |
Pali Rohár | d36240d | 2013-11-19 11:18:03 +0100 | [diff] [blame] | 381 | int power_supply_reg_notifier(struct notifier_block *nb) |
| 382 | { |
| 383 | return atomic_notifier_chain_register(&power_supply_notifier, nb); |
| 384 | } |
| 385 | EXPORT_SYMBOL_GPL(power_supply_reg_notifier); |
| 386 | |
| 387 | void power_supply_unreg_notifier(struct notifier_block *nb) |
| 388 | { |
| 389 | atomic_notifier_chain_unregister(&power_supply_notifier, nb); |
| 390 | } |
| 391 | EXPORT_SYMBOL_GPL(power_supply_unreg_notifier); |
| 392 | |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 393 | #ifdef CONFIG_THERMAL |
| 394 | static int power_supply_read_temp(struct thermal_zone_device *tzd, |
| 395 | unsigned long *temp) |
| 396 | { |
| 397 | struct power_supply *psy; |
| 398 | union power_supply_propval val; |
| 399 | int ret; |
| 400 | |
| 401 | WARN_ON(tzd == NULL); |
| 402 | psy = tzd->devdata; |
| 403 | ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val); |
| 404 | |
| 405 | /* Convert tenths of degree Celsius to milli degree Celsius. */ |
| 406 | if (!ret) |
| 407 | *temp = val.intval * 100; |
| 408 | |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | static struct thermal_zone_device_ops psy_tzd_ops = { |
| 413 | .get_temp = power_supply_read_temp, |
| 414 | }; |
| 415 | |
| 416 | static int psy_register_thermal(struct power_supply *psy) |
| 417 | { |
| 418 | int i; |
| 419 | |
| 420 | /* Register battery zone device psy reports temperature */ |
| 421 | for (i = 0; i < psy->num_properties; i++) { |
| 422 | if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) { |
Anton Vorontsov | e6db06a | 2012-07-31 04:59:42 -0700 | [diff] [blame] | 423 | psy->tzd = thermal_zone_device_register(psy->name, 0, 0, |
Durgadoss R | 50125a9 | 2012-09-18 11:04:56 +0530 | [diff] [blame] | 424 | psy, &psy_tzd_ops, NULL, 0, 0); |
Viresh Kumar | 9d2410c | 2014-09-04 17:31:33 +0530 | [diff] [blame] | 425 | return PTR_ERR_OR_ZERO(psy->tzd); |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static void psy_unregister_thermal(struct power_supply *psy) |
| 432 | { |
| 433 | if (IS_ERR_OR_NULL(psy->tzd)) |
| 434 | return; |
| 435 | thermal_zone_device_unregister(psy->tzd); |
| 436 | } |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 437 | |
| 438 | /* thermal cooling device callbacks */ |
| 439 | static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd, |
| 440 | unsigned long *state) |
| 441 | { |
| 442 | struct power_supply *psy; |
| 443 | union power_supply_propval val; |
| 444 | int ret; |
| 445 | |
| 446 | psy = tcd->devdata; |
| 447 | ret = psy->get_property(psy, |
| 448 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val); |
| 449 | if (!ret) |
| 450 | *state = val.intval; |
| 451 | |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd, |
| 456 | unsigned long *state) |
| 457 | { |
| 458 | struct power_supply *psy; |
| 459 | union power_supply_propval val; |
| 460 | int ret; |
| 461 | |
| 462 | psy = tcd->devdata; |
| 463 | ret = psy->get_property(psy, |
| 464 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); |
| 465 | if (!ret) |
| 466 | *state = val.intval; |
| 467 | |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd, |
| 472 | unsigned long state) |
| 473 | { |
| 474 | struct power_supply *psy; |
| 475 | union power_supply_propval val; |
| 476 | int ret; |
| 477 | |
| 478 | psy = tcd->devdata; |
| 479 | val.intval = state; |
| 480 | ret = psy->set_property(psy, |
| 481 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); |
| 482 | |
| 483 | return ret; |
| 484 | } |
| 485 | |
| 486 | static struct thermal_cooling_device_ops psy_tcd_ops = { |
| 487 | .get_max_state = ps_get_max_charge_cntl_limit, |
| 488 | .get_cur_state = ps_get_cur_chrage_cntl_limit, |
| 489 | .set_cur_state = ps_set_cur_charge_cntl_limit, |
| 490 | }; |
| 491 | |
| 492 | static int psy_register_cooler(struct power_supply *psy) |
| 493 | { |
| 494 | int i; |
| 495 | |
| 496 | /* Register for cooling device if psy can control charging */ |
| 497 | for (i = 0; i < psy->num_properties; i++) { |
| 498 | if (psy->properties[i] == |
| 499 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) { |
| 500 | psy->tcd = thermal_cooling_device_register( |
| 501 | (char *)psy->name, |
| 502 | psy, &psy_tcd_ops); |
Viresh Kumar | 9d2410c | 2014-09-04 17:31:33 +0530 | [diff] [blame] | 503 | return PTR_ERR_OR_ZERO(psy->tcd); |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static void psy_unregister_cooler(struct power_supply *psy) |
| 510 | { |
| 511 | if (IS_ERR_OR_NULL(psy->tcd)) |
| 512 | return; |
| 513 | thermal_cooling_device_unregister(psy->tcd); |
| 514 | } |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 515 | #else |
| 516 | static int psy_register_thermal(struct power_supply *psy) |
| 517 | { |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | static void psy_unregister_thermal(struct power_supply *psy) |
| 522 | { |
| 523 | } |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 524 | |
| 525 | static int psy_register_cooler(struct power_supply *psy) |
| 526 | { |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static void psy_unregister_cooler(struct power_supply *psy) |
| 531 | { |
| 532 | } |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 533 | #endif |
| 534 | |
Wei Yongjun | c128d39 | 2014-07-20 13:31:09 +0800 | [diff] [blame] | 535 | static int __power_supply_register(struct device *parent, |
| 536 | struct power_supply *psy, bool ws) |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 537 | { |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 538 | struct device *dev; |
| 539 | int rc; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 540 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 541 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 542 | if (!dev) |
| 543 | return -ENOMEM; |
| 544 | |
| 545 | device_initialize(dev); |
| 546 | |
| 547 | dev->class = power_supply_class; |
| 548 | dev->type = &power_supply_dev_type; |
| 549 | dev->parent = parent; |
| 550 | dev->release = power_supply_dev_release; |
| 551 | dev_set_drvdata(dev, psy); |
| 552 | psy->dev = dev; |
| 553 | |
Shuah Khan | 80c6463 | 2013-11-22 10:54:28 -0700 | [diff] [blame] | 554 | rc = dev_set_name(dev, "%s", psy->name); |
| 555 | if (rc) |
| 556 | goto dev_set_name_failed; |
| 557 | |
Lars-Peter Clausen | 9777467 | 2011-02-21 15:34:19 +0100 | [diff] [blame] | 558 | INIT_WORK(&psy->changed_work, power_supply_changed_work); |
| 559 | |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 560 | rc = power_supply_check_supplies(psy); |
| 561 | if (rc) { |
| 562 | dev_info(dev, "Not all required supplies found, defer probe\n"); |
| 563 | goto check_supplies_failed; |
| 564 | } |
| 565 | |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 566 | spin_lock_init(&psy->changed_lock); |
Zhang Rui | 9113e26 | 2014-05-28 15:23:37 +0800 | [diff] [blame] | 567 | rc = device_init_wakeup(dev, ws); |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 568 | if (rc) |
| 569 | goto wakeup_init_failed; |
| 570 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 571 | rc = device_add(dev); |
| 572 | if (rc) |
| 573 | goto device_add_failed; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 574 | |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 575 | rc = psy_register_thermal(psy); |
| 576 | if (rc) |
| 577 | goto register_thermal_failed; |
| 578 | |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 579 | rc = psy_register_cooler(psy); |
| 580 | if (rc) |
| 581 | goto register_cooler_failed; |
| 582 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 583 | rc = power_supply_create_triggers(psy); |
| 584 | if (rc) |
| 585 | goto create_triggers_failed; |
| 586 | |
| 587 | power_supply_changed(psy); |
| 588 | |
Viresh Kumar | 464069c | 2014-09-04 17:31:35 +0530 | [diff] [blame] | 589 | return 0; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 590 | |
| 591 | create_triggers_failed: |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 592 | psy_unregister_cooler(psy); |
| 593 | register_cooler_failed: |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 594 | psy_unregister_thermal(psy); |
| 595 | register_thermal_failed: |
Vasiliy Kulikov | 3a2dbd6 | 2010-11-19 21:41:58 +0300 | [diff] [blame] | 596 | device_del(dev); |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 597 | device_add_failed: |
Shuah Khan | 80c6463 | 2013-11-22 10:54:28 -0700 | [diff] [blame] | 598 | wakeup_init_failed: |
Rhyland Klein | f6e0b08 | 2013-04-01 17:45:55 -0400 | [diff] [blame] | 599 | check_supplies_failed: |
Shuah Khan | 80c6463 | 2013-11-22 10:54:28 -0700 | [diff] [blame] | 600 | dev_set_name_failed: |
Vasiliy Kulikov | 3a2dbd6 | 2010-11-19 21:41:58 +0300 | [diff] [blame] | 601 | put_device(dev); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 602 | return rc; |
| 603 | } |
Zhang Rui | 9113e26 | 2014-05-28 15:23:37 +0800 | [diff] [blame] | 604 | |
| 605 | int power_supply_register(struct device *parent, struct power_supply *psy) |
| 606 | { |
| 607 | return __power_supply_register(parent, psy, true); |
| 608 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 609 | EXPORT_SYMBOL_GPL(power_supply_register); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 610 | |
Zhang Rui | 9113e26 | 2014-05-28 15:23:37 +0800 | [diff] [blame] | 611 | int power_supply_register_no_ws(struct device *parent, struct power_supply *psy) |
| 612 | { |
| 613 | return __power_supply_register(parent, psy, false); |
| 614 | } |
| 615 | EXPORT_SYMBOL_GPL(power_supply_register_no_ws); |
| 616 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 617 | void power_supply_unregister(struct power_supply *psy) |
| 618 | { |
Tejun Heo | bc51e7f | 2010-12-11 17:51:45 +0100 | [diff] [blame] | 619 | cancel_work_sync(&psy->changed_work); |
Jeremy Fitzhardinge | 8351665 | 2011-12-07 09:15:45 -0800 | [diff] [blame] | 620 | sysfs_remove_link(&psy->dev->kobj, "powers"); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 621 | power_supply_remove_triggers(psy); |
Ramakrishna Pallala | 952aeeb3 | 2012-10-09 22:25:59 +0530 | [diff] [blame] | 622 | psy_unregister_cooler(psy); |
Jenny TC | 3be330b | 2012-05-09 20:36:47 +0530 | [diff] [blame] | 623 | psy_unregister_thermal(psy); |
Zoran Markovic | 948dcf9 | 2013-08-02 13:38:02 -0700 | [diff] [blame] | 624 | device_init_wakeup(psy->dev, false); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 625 | device_unregister(psy->dev); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 626 | } |
Daniel Mack | ff3417e | 2009-07-30 17:42:31 +0400 | [diff] [blame] | 627 | EXPORT_SYMBOL_GPL(power_supply_unregister); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 628 | |
| 629 | static int __init power_supply_class_init(void) |
| 630 | { |
| 631 | power_supply_class = class_create(THIS_MODULE, "power_supply"); |
| 632 | |
| 633 | if (IS_ERR(power_supply_class)) |
| 634 | return PTR_ERR(power_supply_class); |
| 635 | |
| 636 | power_supply_class->dev_uevent = power_supply_uevent; |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 637 | power_supply_init_attrs(&power_supply_dev_type); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static void __exit power_supply_class_exit(void) |
| 643 | { |
| 644 | class_destroy(power_supply_class); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 645 | } |
| 646 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 647 | subsys_initcall(power_supply_class_init); |
| 648 | module_exit(power_supply_class_exit); |
| 649 | |
| 650 | MODULE_DESCRIPTION("Universal power supply monitor class"); |
| 651 | MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, " |
| 652 | "Szabolcs Gyurko, " |
| 653 | "Anton Vorontsov <cbou@mail.ru>"); |
| 654 | MODULE_LICENSE("GPL"); |