Commit | Line | Data |
---|---|---|
a63a5fa9 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
4a11b59d AV |
2 | /* |
3 | * Universal power supply monitor class | |
4 | * | |
5 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> | |
6 | * Copyright © 2004 Szabolcs Gyurko | |
7 | * Copyright © 2003 Ian Molton <spyro@f2s.com> | |
8 | * | |
9 | * Modified: 2004, Oct Szabolcs Gyurko | |
4a11b59d AV |
10 | */ |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/types.h> | |
14 | #include <linux/init.h> | |
5f487cd3 | 15 | #include <linux/slab.h> |
3ffa6583 | 16 | #include <linux/delay.h> |
4a11b59d | 17 | #include <linux/device.h> |
d36240d2 | 18 | #include <linux/notifier.h> |
4a11b59d | 19 | #include <linux/err.h> |
c08b1f45 | 20 | #include <linux/of.h> |
4a11b59d | 21 | #include <linux/power_supply.h> |
ece711b5 | 22 | #include <linux/property.h> |
3be330bf | 23 | #include <linux/thermal.h> |
a4585ba2 | 24 | #include <linux/fixp-arith.h> |
4a11b59d | 25 | #include "power_supply.h" |
c8aee3f4 | 26 | #include "samsung-sdi-battery.h" |
4a11b59d | 27 | |
68ade097 | 28 | static const struct class power_supply_class = { |
71c2cc5c RM |
29 | .name = "power_supply", |
30 | .dev_uevent = power_supply_uevent, | |
31 | }; | |
4a11b59d | 32 | |
12e94aee | 33 | static BLOCKING_NOTIFIER_HEAD(power_supply_notifier); |
d36240d2 | 34 | |
7b46b609 RM |
35 | __ATTRIBUTE_GROUPS(power_supply_attr); |
36 | static const struct device_type power_supply_dev_type = { | |
37 | .name = "power_supply", | |
38 | .groups = power_supply_attr_groups, | |
39 | }; | |
5f487cd3 | 40 | |
7f1a57fd KK |
41 | #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10) |
42 | ||
5e0848c6 RK |
43 | static bool __power_supply_is_supplied_by(struct power_supply *supplier, |
44 | struct power_supply *supply) | |
45 | { | |
46 | int i; | |
47 | ||
48 | if (!supply->supplied_from && !supplier->supplied_to) | |
49 | return false; | |
50 | ||
51 | /* Support both supplied_to and supplied_from modes */ | |
52 | if (supply->supplied_from) { | |
297d716f | 53 | if (!supplier->desc->name) |
5e0848c6 RK |
54 | return false; |
55 | for (i = 0; i < supply->num_supplies; i++) | |
297d716f | 56 | if (!strcmp(supplier->desc->name, supply->supplied_from[i])) |
5e0848c6 RK |
57 | return true; |
58 | } else { | |
297d716f | 59 | if (!supply->desc->name) |
5e0848c6 RK |
60 | return false; |
61 | for (i = 0; i < supplier->num_supplicants; i++) | |
297d716f | 62 | if (!strcmp(supplier->supplied_to[i], supply->desc->name)) |
5e0848c6 RK |
63 | return true; |
64 | } | |
65 | ||
66 | return false; | |
67 | } | |
68 | ||
443cad92 DY |
69 | static int __power_supply_changed_work(struct device *dev, void *data) |
70 | { | |
e80cf421 | 71 | struct power_supply *psy = data; |
443cad92 | 72 | struct power_supply *pst = dev_get_drvdata(dev); |
443cad92 | 73 | |
5e0848c6 | 74 | if (__power_supply_is_supplied_by(psy, pst)) { |
297d716f KK |
75 | if (pst->desc->external_power_changed) |
76 | pst->desc->external_power_changed(pst); | |
5e0848c6 RK |
77 | } |
78 | ||
443cad92 DY |
79 | return 0; |
80 | } | |
81 | ||
4a11b59d AV |
82 | static void power_supply_changed_work(struct work_struct *work) |
83 | { | |
948dcf96 | 84 | unsigned long flags; |
4a11b59d AV |
85 | struct power_supply *psy = container_of(work, struct power_supply, |
86 | changed_work); | |
4a11b59d | 87 | |
297d716f | 88 | dev_dbg(&psy->dev, "%s\n", __func__); |
4a11b59d | 89 | |
948dcf96 | 90 | spin_lock_irqsave(&psy->changed_lock, flags); |
061f3806 VK |
91 | /* |
92 | * Check 'changed' here to avoid issues due to race between | |
93 | * power_supply_changed() and this routine. In worst case | |
94 | * power_supply_changed() can be called again just before we take above | |
95 | * lock. During the first call of this routine we will mark 'changed' as | |
96 | * false and it will stay false for the next call as well. | |
97 | */ | |
98 | if (likely(psy->changed)) { | |
948dcf96 ZM |
99 | psy->changed = false; |
100 | spin_unlock_irqrestore(&psy->changed_lock, flags); | |
68ade097 | 101 | power_supply_for_each_device(psy, __power_supply_changed_work); |
948dcf96 | 102 | power_supply_update_leds(psy); |
bbaa6ffa | 103 | blocking_notifier_call_chain(&power_supply_notifier, |
d36240d2 | 104 | PSY_EVENT_PROP_CHANGED, psy); |
297d716f | 105 | kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE); |
948dcf96 ZM |
106 | spin_lock_irqsave(&psy->changed_lock, flags); |
107 | } | |
061f3806 | 108 | |
948dcf96 | 109 | /* |
061f3806 VK |
110 | * Hold the wakeup_source until all events are processed. |
111 | * power_supply_changed() might have called again and have set 'changed' | |
112 | * to true. | |
948dcf96 | 113 | */ |
061f3806 | 114 | if (likely(!psy->changed)) |
297d716f | 115 | pm_relax(&psy->dev); |
948dcf96 | 116 | spin_unlock_irqrestore(&psy->changed_lock, flags); |
4a11b59d AV |
117 | } |
118 | ||
68ade097 SR |
119 | int power_supply_for_each_device(void *data, int (*fn)(struct device *dev, void *data)) |
120 | { | |
121 | return class_for_each_device(&power_supply_class, NULL, data, fn); | |
122 | } | |
123 | EXPORT_SYMBOL_GPL(power_supply_for_each_device); | |
124 | ||
4a11b59d AV |
125 | void power_supply_changed(struct power_supply *psy) |
126 | { | |
948dcf96 ZM |
127 | unsigned long flags; |
128 | ||
297d716f | 129 | dev_dbg(&psy->dev, "%s\n", __func__); |
4a11b59d | 130 | |
948dcf96 ZM |
131 | spin_lock_irqsave(&psy->changed_lock, flags); |
132 | psy->changed = true; | |
297d716f | 133 | pm_stay_awake(&psy->dev); |
948dcf96 | 134 | spin_unlock_irqrestore(&psy->changed_lock, flags); |
4a11b59d | 135 | schedule_work(&psy->changed_work); |
4a11b59d | 136 | } |
ff3417e7 | 137 | EXPORT_SYMBOL_GPL(power_supply_changed); |
4a11b59d | 138 | |
7f1a57fd KK |
139 | /* |
140 | * Notify that power supply was registered after parent finished the probing. | |
141 | * | |
142 | * Often power supply is registered from driver's probe function. However | |
143 | * calling power_supply_changed() directly from power_supply_register() | |
144 | * would lead to execution of get_property() function provided by the driver | |
145 | * too early - before the probe ends. | |
146 | * | |
147 | * Avoid that by waiting on parent's mutex. | |
148 | */ | |
149 | static void power_supply_deferred_register_work(struct work_struct *work) | |
150 | { | |
151 | struct power_supply *psy = container_of(work, struct power_supply, | |
152 | deferred_register_work.work); | |
153 | ||
3ffa6583 BT |
154 | if (psy->dev.parent) { |
155 | while (!mutex_trylock(&psy->dev.parent->mutex)) { | |
156 | if (psy->removing) | |
157 | return; | |
158 | msleep(10); | |
159 | } | |
160 | } | |
7f1a57fd KK |
161 | |
162 | power_supply_changed(psy); | |
163 | ||
164 | if (psy->dev.parent) | |
165 | mutex_unlock(&psy->dev.parent->mutex); | |
166 | } | |
167 | ||
f6e0b081 | 168 | #ifdef CONFIG_OF |
f6e0b081 RK |
169 | static int __power_supply_populate_supplied_from(struct device *dev, |
170 | void *data) | |
171 | { | |
e80cf421 | 172 | struct power_supply *psy = data; |
f6e0b081 RK |
173 | struct power_supply *epsy = dev_get_drvdata(dev); |
174 | struct device_node *np; | |
175 | int i = 0; | |
176 | ||
177 | do { | |
178 | np = of_parse_phandle(psy->of_node, "power-supplies", i++); | |
179 | if (!np) | |
a0f93b42 | 180 | break; |
f6e0b081 RK |
181 | |
182 | if (np == epsy->of_node) { | |
3af26e2a | 183 | dev_dbg(&psy->dev, "%s: Found supply : %s\n", |
297d716f KK |
184 | psy->desc->name, epsy->desc->name); |
185 | psy->supplied_from[i-1] = (char *)epsy->desc->name; | |
f6e0b081 | 186 | psy->num_supplies++; |
2054d6e9 | 187 | of_node_put(np); |
f6e0b081 RK |
188 | break; |
189 | } | |
2054d6e9 | 190 | of_node_put(np); |
f6e0b081 RK |
191 | } while (np); |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | static int power_supply_populate_supplied_from(struct power_supply *psy) | |
197 | { | |
198 | int error; | |
199 | ||
68ade097 | 200 | error = power_supply_for_each_device(psy, __power_supply_populate_supplied_from); |
f6e0b081 | 201 | |
297d716f | 202 | dev_dbg(&psy->dev, "%s %d\n", __func__, error); |
f6e0b081 RK |
203 | |
204 | return error; | |
205 | } | |
206 | ||
207 | static int __power_supply_find_supply_from_node(struct device *dev, | |
208 | void *data) | |
209 | { | |
e80cf421 | 210 | struct device_node *np = data; |
f6e0b081 RK |
211 | struct power_supply *epsy = dev_get_drvdata(dev); |
212 | ||
68ade097 | 213 | /* returning non-zero breaks out of power_supply_for_each_device loop */ |
f6e0b081 | 214 | if (epsy->of_node == np) |
585b0087 | 215 | return 1; |
f6e0b081 RK |
216 | |
217 | return 0; | |
218 | } | |
219 | ||
220 | static int power_supply_find_supply_from_node(struct device_node *supply_node) | |
221 | { | |
222 | int error; | |
f6e0b081 RK |
223 | |
224 | /* | |
68ade097 | 225 | * power_supply_for_each_device() either returns its own errors or values |
585b0087 VK |
226 | * returned by __power_supply_find_supply_from_node(). |
227 | * | |
228 | * __power_supply_find_supply_from_node() will return 0 (no match) | |
229 | * or 1 (match). | |
230 | * | |
68ade097 | 231 | * We return 0 if power_supply_for_each_device() returned 1, -EPROBE_DEFER if |
585b0087 | 232 | * it returned 0, or error as returned by it. |
f6e0b081 | 233 | */ |
68ade097 | 234 | error = power_supply_for_each_device(supply_node, __power_supply_find_supply_from_node); |
f6e0b081 | 235 | |
585b0087 | 236 | return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER; |
f6e0b081 RK |
237 | } |
238 | ||
239 | static int power_supply_check_supplies(struct power_supply *psy) | |
240 | { | |
241 | struct device_node *np; | |
242 | int cnt = 0; | |
243 | ||
244 | /* If there is already a list honor it */ | |
245 | if (psy->supplied_from && psy->num_supplies > 0) | |
246 | return 0; | |
247 | ||
248 | /* No device node found, nothing to do */ | |
249 | if (!psy->of_node) | |
250 | return 0; | |
251 | ||
252 | do { | |
253 | int ret; | |
254 | ||
255 | np = of_parse_phandle(psy->of_node, "power-supplies", cnt++); | |
256 | if (!np) | |
a0f93b42 | 257 | break; |
f6e0b081 RK |
258 | |
259 | ret = power_supply_find_supply_from_node(np); | |
8468b029 VK |
260 | of_node_put(np); |
261 | ||
f6e0b081 | 262 | if (ret) { |
297d716f | 263 | dev_dbg(&psy->dev, "Failed to find supply!\n"); |
f5b89aff | 264 | return ret; |
f6e0b081 RK |
265 | } |
266 | } while (np); | |
267 | ||
f9c85486 VK |
268 | /* Missing valid "power-supplies" entries */ |
269 | if (cnt == 1) | |
270 | return 0; | |
271 | ||
f6e0b081 | 272 | /* All supplies found, allocate char ** array for filling */ |
966f6551 | 273 | psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(*psy->supplied_from), |
f6e0b081 | 274 | GFP_KERNEL); |
1e743997 | 275 | if (!psy->supplied_from) |
f6e0b081 | 276 | return -ENOMEM; |
f6e0b081 | 277 | |
a86854d0 | 278 | *psy->supplied_from = devm_kcalloc(&psy->dev, |
966f6551 | 279 | cnt - 1, sizeof(**psy->supplied_from), |
f6e0b081 | 280 | GFP_KERNEL); |
1e743997 | 281 | if (!*psy->supplied_from) |
f6e0b081 | 282 | return -ENOMEM; |
f6e0b081 RK |
283 | |
284 | return power_supply_populate_supplied_from(psy); | |
285 | } | |
286 | #else | |
58a36bb0 | 287 | static int power_supply_check_supplies(struct power_supply *psy) |
f6e0b081 | 288 | { |
58a36bb0 HG |
289 | int nval, ret; |
290 | ||
291 | if (!psy->dev.parent) | |
292 | return 0; | |
293 | ||
1c97db17 | 294 | nval = device_property_string_array_count(psy->dev.parent, "supplied-from"); |
58a36bb0 HG |
295 | if (nval <= 0) |
296 | return 0; | |
297 | ||
298 | psy->supplied_from = devm_kmalloc_array(&psy->dev, nval, | |
299 | sizeof(char *), GFP_KERNEL); | |
300 | if (!psy->supplied_from) | |
301 | return -ENOMEM; | |
302 | ||
303 | ret = device_property_read_string_array(psy->dev.parent, | |
304 | "supplied-from", (const char **)psy->supplied_from, nval); | |
305 | if (ret < 0) | |
306 | return ret; | |
307 | ||
308 | psy->num_supplies = nval; | |
309 | ||
f6e0b081 RK |
310 | return 0; |
311 | } | |
312 | #endif | |
313 | ||
2848e039 HG |
314 | struct psy_am_i_supplied_data { |
315 | struct power_supply *psy; | |
316 | unsigned int count; | |
317 | }; | |
318 | ||
319 | static int __power_supply_am_i_supplied(struct device *dev, void *_data) | |
4a11b59d AV |
320 | { |
321 | union power_supply_propval ret = {0,}; | |
443cad92 | 322 | struct power_supply *epsy = dev_get_drvdata(dev); |
2848e039 | 323 | struct psy_am_i_supplied_data *data = _data; |
443cad92 | 324 | |
46cecd13 HG |
325 | if (__power_supply_is_supplied_by(epsy, data->psy)) { |
326 | data->count++; | |
297d716f KK |
327 | if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, |
328 | &ret)) | |
1c42a389 | 329 | return ret.intval; |
46cecd13 | 330 | } |
5e0848c6 | 331 | |
443cad92 DY |
332 | return 0; |
333 | } | |
334 | ||
335 | int power_supply_am_i_supplied(struct power_supply *psy) | |
336 | { | |
2848e039 | 337 | struct psy_am_i_supplied_data data = { psy, 0 }; |
443cad92 DY |
338 | int error; |
339 | ||
68ade097 | 340 | error = power_supply_for_each_device(&data, __power_supply_am_i_supplied); |
4a11b59d | 341 | |
2848e039 HG |
342 | dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error); |
343 | ||
344 | if (data.count == 0) | |
345 | return -ENODEV; | |
4a11b59d | 346 | |
443cad92 | 347 | return error; |
4a11b59d | 348 | } |
ff3417e7 | 349 | EXPORT_SYMBOL_GPL(power_supply_am_i_supplied); |
4a11b59d | 350 | |
942ed161 MG |
351 | static int __power_supply_is_system_supplied(struct device *dev, void *data) |
352 | { | |
353 | union power_supply_propval ret = {0,}; | |
354 | struct power_supply *psy = dev_get_drvdata(dev); | |
2530daa1 | 355 | unsigned int *count = data; |
942ed161 | 356 | |
95339f40 ML |
357 | if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_SCOPE, &ret)) |
358 | if (ret.intval == POWER_SUPPLY_SCOPE_DEVICE) | |
359 | return 0; | |
360 | ||
2530daa1 | 361 | (*count)++; |
297d716f KK |
362 | if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY) |
363 | if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE, | |
364 | &ret)) | |
942ed161 | 365 | return ret.intval; |
1c42a389 | 366 | |
942ed161 MG |
367 | return 0; |
368 | } | |
369 | ||
370 | int power_supply_is_system_supplied(void) | |
371 | { | |
372 | int error; | |
2530daa1 | 373 | unsigned int count = 0; |
942ed161 | 374 | |
68ade097 | 375 | error = power_supply_for_each_device(&count, __power_supply_is_system_supplied); |
942ed161 | 376 | |
2530daa1 | 377 | /* |
95339f40 ML |
378 | * If no system scope power class device was found at all, most probably we |
379 | * are running on a desktop system, so assume we are on mains power. | |
2530daa1 JD |
380 | */ |
381 | if (count == 0) | |
382 | return 1; | |
383 | ||
942ed161 MG |
384 | return error; |
385 | } | |
ff3417e7 | 386 | EXPORT_SYMBOL_GPL(power_supply_is_system_supplied); |
942ed161 | 387 | |
2220af8c HG |
388 | struct psy_get_supplier_prop_data { |
389 | struct power_supply *psy; | |
390 | enum power_supply_property psp; | |
391 | union power_supply_propval *val; | |
392 | }; | |
393 | ||
394 | static int __power_supply_get_supplier_property(struct device *dev, void *_data) | |
c3142dd8 | 395 | { |
c3142dd8 | 396 | struct power_supply *epsy = dev_get_drvdata(dev); |
2220af8c | 397 | struct psy_get_supplier_prop_data *data = _data; |
c3142dd8 | 398 | |
2220af8c | 399 | if (__power_supply_is_supplied_by(epsy, data->psy)) |
27a2195e | 400 | if (!power_supply_get_property(epsy, data->psp, data->val)) |
2220af8c | 401 | return 1; /* Success */ |
c3142dd8 | 402 | |
2220af8c | 403 | return 0; /* Continue iterating */ |
c3142dd8 HG |
404 | } |
405 | ||
2220af8c HG |
406 | int power_supply_get_property_from_supplier(struct power_supply *psy, |
407 | enum power_supply_property psp, | |
408 | union power_supply_propval *val) | |
c3142dd8 | 409 | { |
2220af8c HG |
410 | struct psy_get_supplier_prop_data data = { |
411 | .psy = psy, | |
412 | .psp = psp, | |
413 | .val = val, | |
414 | }; | |
415 | int ret; | |
c3142dd8 HG |
416 | |
417 | /* | |
418 | * This function is not intended for use with a supply with multiple | |
2220af8c | 419 | * suppliers, we simply pick the first supply to report the psp. |
c3142dd8 | 420 | */ |
68ade097 | 421 | ret = power_supply_for_each_device(&data, __power_supply_get_supplier_property); |
2220af8c HG |
422 | if (ret < 0) |
423 | return ret; | |
424 | if (ret == 0) | |
425 | return -ENODEV; | |
c3142dd8 | 426 | |
2220af8c | 427 | return 0; |
c3142dd8 | 428 | } |
2220af8c | 429 | EXPORT_SYMBOL_GPL(power_supply_get_property_from_supplier); |
c3142dd8 | 430 | |
e5f5ccb6 DM |
431 | int power_supply_set_battery_charged(struct power_supply *psy) |
432 | { | |
bc154056 | 433 | if (atomic_read(&psy->use_cnt) >= 0 && |
297d716f KK |
434 | psy->desc->type == POWER_SUPPLY_TYPE_BATTERY && |
435 | psy->desc->set_charged) { | |
436 | psy->desc->set_charged(psy); | |
e5f5ccb6 DM |
437 | return 0; |
438 | } | |
439 | ||
440 | return -EINVAL; | |
441 | } | |
442 | EXPORT_SYMBOL_GPL(power_supply_set_battery_charged); | |
443 | ||
9f3b795a | 444 | static int power_supply_match_device_by_name(struct device *dev, const void *data) |
e5f5ccb6 DM |
445 | { |
446 | const char *name = data; | |
447 | struct power_supply *psy = dev_get_drvdata(dev); | |
448 | ||
297d716f | 449 | return strcmp(psy->desc->name, name) == 0; |
e5f5ccb6 DM |
450 | } |
451 | ||
1a352462 KK |
452 | /** |
453 | * power_supply_get_by_name() - Search for a power supply and returns its ref | |
454 | * @name: Power supply name to fetch | |
455 | * | |
456 | * If power supply was found, it increases reference count for the | |
457 | * internal power supply's device. The user should power_supply_put() | |
458 | * after usage. | |
459 | * | |
460 | * Return: On success returns a reference to a power supply with | |
461 | * matching name equals to @name, a NULL otherwise. | |
462 | */ | |
9f3b795a | 463 | struct power_supply *power_supply_get_by_name(const char *name) |
e5f5ccb6 | 464 | { |
03e81acc | 465 | struct power_supply *psy = NULL; |
71c2cc5c RM |
466 | struct device *dev = class_find_device(&power_supply_class, NULL, name, |
467 | power_supply_match_device_by_name); | |
e5f5ccb6 | 468 | |
03e81acc KK |
469 | if (dev) { |
470 | psy = dev_get_drvdata(dev); | |
471 | atomic_inc(&psy->use_cnt); | |
472 | } | |
473 | ||
474 | return psy; | |
e5f5ccb6 DM |
475 | } |
476 | EXPORT_SYMBOL_GPL(power_supply_get_by_name); | |
477 | ||
1a352462 KK |
478 | /** |
479 | * power_supply_put() - Drop reference obtained with power_supply_get_by_name | |
480 | * @psy: Reference to put | |
481 | * | |
482 | * The reference to power supply should be put before unregistering | |
483 | * the power supply. | |
484 | */ | |
485 | void power_supply_put(struct power_supply *psy) | |
486 | { | |
487 | might_sleep(); | |
488 | ||
03e81acc | 489 | atomic_dec(&psy->use_cnt); |
1a352462 KK |
490 | put_device(&psy->dev); |
491 | } | |
492 | EXPORT_SYMBOL_GPL(power_supply_put); | |
493 | ||
abce9770 SR |
494 | #ifdef CONFIG_OF |
495 | static int power_supply_match_device_node(struct device *dev, const void *data) | |
496 | { | |
497 | return dev->parent && dev->parent->of_node == data; | |
498 | } | |
499 | ||
1a352462 KK |
500 | /** |
501 | * power_supply_get_by_phandle() - Search for a power supply and returns its ref | |
502 | * @np: Pointer to device node holding phandle property | |
32c52eff | 503 | * @property: Name of property holding a power supply name |
1a352462 KK |
504 | * |
505 | * If power supply was found, it increases reference count for the | |
506 | * internal power supply's device. The user should power_supply_put() | |
507 | * after usage. | |
508 | * | |
509 | * Return: On success returns a reference to a power supply with | |
510 | * matching name equals to value under @property, NULL or ERR_PTR otherwise. | |
511 | */ | |
abce9770 SR |
512 | struct power_supply *power_supply_get_by_phandle(struct device_node *np, |
513 | const char *property) | |
514 | { | |
515 | struct device_node *power_supply_np; | |
03e81acc | 516 | struct power_supply *psy = NULL; |
abce9770 SR |
517 | struct device *dev; |
518 | ||
519 | power_supply_np = of_parse_phandle(np, property, 0); | |
520 | if (!power_supply_np) | |
521 | return ERR_PTR(-ENODEV); | |
522 | ||
71c2cc5c RM |
523 | dev = class_find_device(&power_supply_class, NULL, power_supply_np, |
524 | power_supply_match_device_node); | |
abce9770 SR |
525 | |
526 | of_node_put(power_supply_np); | |
527 | ||
03e81acc KK |
528 | if (dev) { |
529 | psy = dev_get_drvdata(dev); | |
530 | atomic_inc(&psy->use_cnt); | |
531 | } | |
532 | ||
533 | return psy; | |
abce9770 SR |
534 | } |
535 | EXPORT_SYMBOL_GPL(power_supply_get_by_phandle); | |
fe27e1df HG |
536 | |
537 | static void devm_power_supply_put(struct device *dev, void *res) | |
538 | { | |
539 | struct power_supply **psy = res; | |
540 | ||
541 | power_supply_put(*psy); | |
542 | } | |
543 | ||
544 | /** | |
545 | * devm_power_supply_get_by_phandle() - Resource managed version of | |
546 | * power_supply_get_by_phandle() | |
547 | * @dev: Pointer to device holding phandle property | |
32c52eff | 548 | * @property: Name of property holding a power supply phandle |
fe27e1df HG |
549 | * |
550 | * Return: On success returns a reference to a power supply with | |
551 | * matching name equals to value under @property, NULL or ERR_PTR otherwise. | |
552 | */ | |
553 | struct power_supply *devm_power_supply_get_by_phandle(struct device *dev, | |
554 | const char *property) | |
555 | { | |
556 | struct power_supply **ptr, *psy; | |
557 | ||
558 | if (!dev->of_node) | |
559 | return ERR_PTR(-ENODEV); | |
560 | ||
561 | ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL); | |
562 | if (!ptr) | |
563 | return ERR_PTR(-ENOMEM); | |
564 | ||
565 | psy = power_supply_get_by_phandle(dev->of_node, property); | |
566 | if (IS_ERR_OR_NULL(psy)) { | |
567 | devres_free(ptr); | |
568 | } else { | |
569 | *ptr = psy; | |
570 | devres_add(dev, ptr); | |
571 | } | |
572 | return psy; | |
573 | } | |
574 | EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle); | |
abce9770 SR |
575 | #endif /* CONFIG_OF */ |
576 | ||
c08b1f45 | 577 | int power_supply_get_battery_info(struct power_supply *psy, |
25fd3303 | 578 | struct power_supply_battery_info **info_out) |
c08b1f45 | 579 | { |
65dbad71 | 580 | struct power_supply_resistance_temp_table *resist_table; |
25fd3303 | 581 | struct power_supply_battery_info *info; |
c76787b0 HG |
582 | struct device_node *battery_np = NULL; |
583 | struct fwnode_reference_args args; | |
44c524b6 | 584 | struct fwnode_handle *fwnode = NULL; |
c08b1f45 | 585 | const char *value; |
3afb50d7 | 586 | int err, len, index; |
65dbad71 | 587 | const __be32 *list; |
7562ccd8 | 588 | u32 min_max[2]; |
c08b1f45 | 589 | |
c8aee3f4 LW |
590 | if (psy->of_node) { |
591 | battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0); | |
592 | if (!battery_np) | |
593 | return -ENODEV; | |
594 | ||
595 | fwnode = fwnode_handle_get(of_fwnode_handle(battery_np)); | |
44c524b6 | 596 | } else if (psy->dev.parent) { |
c8aee3f4 LW |
597 | err = fwnode_property_get_reference_args( |
598 | dev_fwnode(psy->dev.parent), | |
599 | "monitored-battery", NULL, 0, 0, &args); | |
600 | if (err) | |
601 | return err; | |
602 | ||
603 | fwnode = args.fwnode; | |
604 | } | |
605 | ||
44c524b6 HC |
606 | if (!fwnode) |
607 | return -ENOENT; | |
608 | ||
c8aee3f4 LW |
609 | err = fwnode_property_read_string(fwnode, "compatible", &value); |
610 | if (err) | |
611 | goto out_put_node; | |
612 | ||
613 | ||
614 | /* Try static batteries first */ | |
615 | err = samsung_sdi_battery_get_info(&psy->dev, value, &info); | |
616 | if (!err) | |
617 | goto out_ret_pointer; | |
581045ed YO |
618 | else if (err == -ENODEV) |
619 | /* | |
620 | * Device does not have a static battery. | |
621 | * Proceed to look for a simple battery. | |
622 | */ | |
623 | err = 0; | |
c8aee3f4 LW |
624 | |
625 | if (strcmp("simple-battery", value)) { | |
626 | err = -ENODEV; | |
627 | goto out_put_node; | |
628 | } | |
629 | ||
e56a4be2 | 630 | info = devm_kzalloc(&psy->dev, sizeof(*info), GFP_KERNEL); |
c8aee3f4 LW |
631 | if (!info) { |
632 | err = -ENOMEM; | |
633 | goto out_put_node; | |
634 | } | |
25fd3303 | 635 | |
4eef766b | 636 | info->technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; |
c08b1f45 LB |
637 | info->energy_full_design_uwh = -EINVAL; |
638 | info->charge_full_design_uah = -EINVAL; | |
639 | info->voltage_min_design_uv = -EINVAL; | |
04fb5310 | 640 | info->voltage_max_design_uv = -EINVAL; |
c08b1f45 LB |
641 | info->precharge_current_ua = -EINVAL; |
642 | info->charge_term_current_ua = -EINVAL; | |
643 | info->constant_charge_current_max_ua = -EINVAL; | |
644 | info->constant_charge_voltage_max_uv = -EINVAL; | |
25fd3303 LW |
645 | info->tricklecharge_current_ua = -EINVAL; |
646 | info->precharge_voltage_max_uv = -EINVAL; | |
647 | info->charge_restart_voltage_uv = -EINVAL; | |
648 | info->overvoltage_limit_uv = -EINVAL; | |
d72ce7d3 | 649 | info->maintenance_charge = NULL; |
0e8b903b LW |
650 | info->alert_low_temp_charge_current_ua = -EINVAL; |
651 | info->alert_low_temp_charge_voltage_uv = -EINVAL; | |
652 | info->alert_high_temp_charge_current_ua = -EINVAL; | |
653 | info->alert_high_temp_charge_voltage_uv = -EINVAL; | |
00cda13e DO |
654 | info->temp_ambient_alert_min = INT_MIN; |
655 | info->temp_ambient_alert_max = INT_MAX; | |
656 | info->temp_alert_min = INT_MIN; | |
657 | info->temp_alert_max = INT_MAX; | |
658 | info->temp_min = INT_MIN; | |
659 | info->temp_max = INT_MAX; | |
86131d93 | 660 | info->factory_internal_resistance_uohm = -EINVAL; |
1f918e0f LW |
661 | info->resist_table = NULL; |
662 | info->bti_resistance_ohm = -EINVAL; | |
663 | info->bti_resistance_tolerance = -EINVAL; | |
c08b1f45 | 664 | |
3afb50d7 BW |
665 | for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) { |
666 | info->ocv_table[index] = NULL; | |
667 | info->ocv_temp[index] = -EINVAL; | |
668 | info->ocv_table_size[index] = -EINVAL; | |
669 | } | |
670 | ||
c08b1f45 LB |
671 | /* The property and field names below must correspond to elements |
672 | * in enum power_supply_property. For reasoning, see | |
151f4e2b | 673 | * Documentation/power/power_supply_class.rst. |
c08b1f45 LB |
674 | */ |
675 | ||
7562ccd8 | 676 | if (!fwnode_property_read_string(fwnode, "device-chemistry", &value)) { |
4eef766b LW |
677 | if (!strcmp("nickel-cadmium", value)) |
678 | info->technology = POWER_SUPPLY_TECHNOLOGY_NiCd; | |
679 | else if (!strcmp("nickel-metal-hydride", value)) | |
680 | info->technology = POWER_SUPPLY_TECHNOLOGY_NiMH; | |
681 | else if (!strcmp("lithium-ion", value)) | |
682 | /* Imprecise lithium-ion type */ | |
683 | info->technology = POWER_SUPPLY_TECHNOLOGY_LION; | |
684 | else if (!strcmp("lithium-ion-polymer", value)) | |
685 | info->technology = POWER_SUPPLY_TECHNOLOGY_LIPO; | |
686 | else if (!strcmp("lithium-ion-iron-phosphate", value)) | |
687 | info->technology = POWER_SUPPLY_TECHNOLOGY_LiFe; | |
688 | else if (!strcmp("lithium-ion-manganese-oxide", value)) | |
689 | info->technology = POWER_SUPPLY_TECHNOLOGY_LiMn; | |
690 | else | |
691 | dev_warn(&psy->dev, "%s unknown battery type\n", value); | |
692 | } | |
693 | ||
7562ccd8 | 694 | fwnode_property_read_u32(fwnode, "energy-full-design-microwatt-hours", |
c08b1f45 | 695 | &info->energy_full_design_uwh); |
7562ccd8 | 696 | fwnode_property_read_u32(fwnode, "charge-full-design-microamp-hours", |
c08b1f45 | 697 | &info->charge_full_design_uah); |
7562ccd8 | 698 | fwnode_property_read_u32(fwnode, "voltage-min-design-microvolt", |
c08b1f45 | 699 | &info->voltage_min_design_uv); |
7562ccd8 | 700 | fwnode_property_read_u32(fwnode, "voltage-max-design-microvolt", |
04fb5310 | 701 | &info->voltage_max_design_uv); |
7562ccd8 | 702 | fwnode_property_read_u32(fwnode, "trickle-charge-current-microamp", |
5a63b7ba | 703 | &info->tricklecharge_current_ua); |
7562ccd8 | 704 | fwnode_property_read_u32(fwnode, "precharge-current-microamp", |
c08b1f45 | 705 | &info->precharge_current_ua); |
7562ccd8 | 706 | fwnode_property_read_u32(fwnode, "precharge-upper-limit-microvolt", |
5a63b7ba | 707 | &info->precharge_voltage_max_uv); |
7562ccd8 | 708 | fwnode_property_read_u32(fwnode, "charge-term-current-microamp", |
c08b1f45 | 709 | &info->charge_term_current_ua); |
7562ccd8 | 710 | fwnode_property_read_u32(fwnode, "re-charge-voltage-microvolt", |
5a63b7ba | 711 | &info->charge_restart_voltage_uv); |
7562ccd8 | 712 | fwnode_property_read_u32(fwnode, "over-voltage-threshold-microvolt", |
5a63b7ba | 713 | &info->overvoltage_limit_uv); |
7562ccd8 | 714 | fwnode_property_read_u32(fwnode, "constant-charge-current-max-microamp", |
c08b1f45 | 715 | &info->constant_charge_current_max_ua); |
7562ccd8 | 716 | fwnode_property_read_u32(fwnode, "constant-charge-voltage-max-microvolt", |
c08b1f45 | 717 | &info->constant_charge_voltage_max_uv); |
7562ccd8 | 718 | fwnode_property_read_u32(fwnode, "factory-internal-resistance-micro-ohms", |
86131d93 | 719 | &info->factory_internal_resistance_uohm); |
c08b1f45 | 720 | |
7562ccd8 HG |
721 | if (!fwnode_property_read_u32_array(fwnode, "ambient-celsius", |
722 | min_max, ARRAY_SIZE(min_max))) { | |
723 | info->temp_ambient_alert_min = min_max[0]; | |
724 | info->temp_ambient_alert_max = min_max[1]; | |
725 | } | |
726 | if (!fwnode_property_read_u32_array(fwnode, "alert-celsius", | |
727 | min_max, ARRAY_SIZE(min_max))) { | |
728 | info->temp_alert_min = min_max[0]; | |
729 | info->temp_alert_max = min_max[1]; | |
730 | } | |
731 | if (!fwnode_property_read_u32_array(fwnode, "operating-range-celsius", | |
732 | min_max, ARRAY_SIZE(min_max))) { | |
733 | info->temp_min = min_max[0]; | |
734 | info->temp_max = min_max[1]; | |
735 | } | |
736 | ||
737 | /* | |
738 | * The below code uses raw of-data parsing to parse | |
739 | * /schemas/types.yaml#/definitions/uint32-matrix | |
740 | * data, so for now this is only support with of. | |
741 | */ | |
742 | if (!battery_np) | |
743 | goto out_ret_pointer; | |
00cda13e | 744 | |
3afb50d7 BW |
745 | len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius"); |
746 | if (len < 0 && len != -EINVAL) { | |
2143bf6d WY |
747 | err = len; |
748 | goto out_put_node; | |
3afb50d7 BW |
749 | } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) { |
750 | dev_err(&psy->dev, "Too many temperature values\n"); | |
2143bf6d WY |
751 | err = -EINVAL; |
752 | goto out_put_node; | |
3afb50d7 BW |
753 | } else if (len > 0) { |
754 | of_property_read_u32_array(battery_np, "ocv-capacity-celsius", | |
755 | info->ocv_temp, len); | |
756 | } | |
757 | ||
758 | for (index = 0; index < len; index++) { | |
759 | struct power_supply_battery_ocv_table *table; | |
760 | char *propname; | |
3afb50d7 BW |
761 | int i, tab_len, size; |
762 | ||
763 | propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index); | |
104bb8a6 | 764 | if (!propname) { |
765 | power_supply_put_battery_info(psy, info); | |
766 | err = -ENOMEM; | |
767 | goto out_put_node; | |
768 | } | |
3afb50d7 BW |
769 | list = of_get_property(battery_np, propname, &size); |
770 | if (!list || !size) { | |
771 | dev_err(&psy->dev, "failed to get %s\n", propname); | |
772 | kfree(propname); | |
773 | power_supply_put_battery_info(psy, info); | |
2143bf6d WY |
774 | err = -EINVAL; |
775 | goto out_put_node; | |
3afb50d7 BW |
776 | } |
777 | ||
778 | kfree(propname); | |
779 | tab_len = size / (2 * sizeof(__be32)); | |
780 | info->ocv_table_size[index] = tab_len; | |
781 | ||
782 | table = info->ocv_table[index] = | |
783 | devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL); | |
784 | if (!info->ocv_table[index]) { | |
785 | power_supply_put_battery_info(psy, info); | |
2143bf6d WY |
786 | err = -ENOMEM; |
787 | goto out_put_node; | |
3afb50d7 BW |
788 | } |
789 | ||
790 | for (i = 0; i < tab_len; i++) { | |
caee2849 PT |
791 | table[i].ocv = be32_to_cpu(*list); |
792 | list++; | |
793 | table[i].capacity = be32_to_cpu(*list); | |
794 | list++; | |
3afb50d7 BW |
795 | } |
796 | } | |
797 | ||
65dbad71 BW |
798 | list = of_get_property(battery_np, "resistance-temp-table", &len); |
799 | if (!list || !len) | |
25fd3303 | 800 | goto out_ret_pointer; |
65dbad71 BW |
801 | |
802 | info->resist_table_size = len / (2 * sizeof(__be32)); | |
803 | resist_table = info->resist_table = devm_kcalloc(&psy->dev, | |
804 | info->resist_table_size, | |
805 | sizeof(*resist_table), | |
806 | GFP_KERNEL); | |
807 | if (!info->resist_table) { | |
808 | power_supply_put_battery_info(psy, info); | |
809 | err = -ENOMEM; | |
810 | goto out_put_node; | |
811 | } | |
812 | ||
813 | for (index = 0; index < info->resist_table_size; index++) { | |
814 | resist_table[index].temp = be32_to_cpu(*list++); | |
815 | resist_table[index].resistance = be32_to_cpu(*list++); | |
816 | } | |
817 | ||
25fd3303 LW |
818 | out_ret_pointer: |
819 | /* Finally return the whole thing */ | |
820 | *info_out = info; | |
821 | ||
2143bf6d | 822 | out_put_node: |
c76787b0 | 823 | fwnode_handle_put(fwnode); |
2143bf6d WY |
824 | of_node_put(battery_np); |
825 | return err; | |
c08b1f45 LB |
826 | } |
827 | EXPORT_SYMBOL_GPL(power_supply_get_battery_info); | |
828 | ||
3afb50d7 BW |
829 | void power_supply_put_battery_info(struct power_supply *psy, |
830 | struct power_supply_battery_info *info) | |
831 | { | |
832 | int i; | |
833 | ||
834 | for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) { | |
835 | if (info->ocv_table[i]) | |
836 | devm_kfree(&psy->dev, info->ocv_table[i]); | |
837 | } | |
65dbad71 BW |
838 | |
839 | if (info->resist_table) | |
840 | devm_kfree(&psy->dev, info->resist_table); | |
25fd3303 LW |
841 | |
842 | devm_kfree(&psy->dev, info); | |
3afb50d7 BW |
843 | } |
844 | EXPORT_SYMBOL_GPL(power_supply_put_battery_info); | |
845 | ||
27a2195e SR |
846 | const enum power_supply_property power_supply_battery_info_properties[] = { |
847 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
848 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, | |
849 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | |
850 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, | |
851 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, | |
852 | POWER_SUPPLY_PROP_PRECHARGE_CURRENT, | |
853 | POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, | |
854 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, | |
855 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, | |
856 | POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN, | |
857 | POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX, | |
858 | POWER_SUPPLY_PROP_TEMP_ALERT_MIN, | |
859 | POWER_SUPPLY_PROP_TEMP_ALERT_MAX, | |
860 | POWER_SUPPLY_PROP_TEMP_MIN, | |
861 | POWER_SUPPLY_PROP_TEMP_MAX, | |
862 | }; | |
863 | EXPORT_SYMBOL_GPL(power_supply_battery_info_properties); | |
864 | ||
865 | const size_t power_supply_battery_info_properties_size = ARRAY_SIZE(power_supply_battery_info_properties); | |
866 | EXPORT_SYMBOL_GPL(power_supply_battery_info_properties_size); | |
867 | ||
868 | bool power_supply_battery_info_has_prop(struct power_supply_battery_info *info, | |
c73cc447 | 869 | enum power_supply_property psp) |
27a2195e SR |
870 | { |
871 | if (!info) | |
872 | return false; | |
873 | ||
874 | switch (psp) { | |
c73cc447 CM |
875 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
876 | return info->technology != POWER_SUPPLY_TECHNOLOGY_UNKNOWN; | |
877 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: | |
878 | return info->energy_full_design_uwh >= 0; | |
879 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | |
880 | return info->charge_full_design_uah >= 0; | |
881 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: | |
882 | return info->voltage_min_design_uv >= 0; | |
883 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: | |
884 | return info->voltage_max_design_uv >= 0; | |
885 | case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: | |
886 | return info->precharge_current_ua >= 0; | |
887 | case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: | |
888 | return info->charge_term_current_ua >= 0; | |
889 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: | |
890 | return info->constant_charge_current_max_ua >= 0; | |
891 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: | |
892 | return info->constant_charge_voltage_max_uv >= 0; | |
893 | case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN: | |
894 | return info->temp_ambient_alert_min > INT_MIN; | |
895 | case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX: | |
896 | return info->temp_ambient_alert_max < INT_MAX; | |
897 | case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: | |
898 | return info->temp_alert_min > INT_MIN; | |
899 | case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: | |
900 | return info->temp_alert_max < INT_MAX; | |
901 | case POWER_SUPPLY_PROP_TEMP_MIN: | |
902 | return info->temp_min > INT_MIN; | |
903 | case POWER_SUPPLY_PROP_TEMP_MAX: | |
904 | return info->temp_max < INT_MAX; | |
905 | default: | |
906 | return false; | |
27a2195e SR |
907 | } |
908 | } | |
909 | EXPORT_SYMBOL_GPL(power_supply_battery_info_has_prop); | |
910 | ||
911 | int power_supply_battery_info_get_prop(struct power_supply_battery_info *info, | |
912 | enum power_supply_property psp, | |
913 | union power_supply_propval *val) | |
914 | { | |
915 | if (!info) | |
916 | return -EINVAL; | |
917 | ||
918 | if (!power_supply_battery_info_has_prop(info, psp)) | |
919 | return -EINVAL; | |
920 | ||
921 | switch (psp) { | |
c73cc447 CM |
922 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
923 | val->intval = info->technology; | |
924 | return 0; | |
925 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: | |
926 | val->intval = info->energy_full_design_uwh; | |
927 | return 0; | |
928 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | |
929 | val->intval = info->charge_full_design_uah; | |
930 | return 0; | |
931 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: | |
932 | val->intval = info->voltage_min_design_uv; | |
933 | return 0; | |
934 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: | |
935 | val->intval = info->voltage_max_design_uv; | |
936 | return 0; | |
937 | case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: | |
938 | val->intval = info->precharge_current_ua; | |
939 | return 0; | |
940 | case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: | |
941 | val->intval = info->charge_term_current_ua; | |
942 | return 0; | |
943 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: | |
944 | val->intval = info->constant_charge_current_max_ua; | |
945 | return 0; | |
946 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: | |
947 | val->intval = info->constant_charge_voltage_max_uv; | |
948 | return 0; | |
949 | case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN: | |
950 | val->intval = info->temp_ambient_alert_min; | |
951 | return 0; | |
952 | case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX: | |
953 | val->intval = info->temp_ambient_alert_max; | |
954 | return 0; | |
955 | case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: | |
956 | val->intval = info->temp_alert_min; | |
957 | return 0; | |
958 | case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: | |
959 | val->intval = info->temp_alert_max; | |
960 | return 0; | |
961 | case POWER_SUPPLY_PROP_TEMP_MIN: | |
962 | val->intval = info->temp_min; | |
963 | return 0; | |
964 | case POWER_SUPPLY_PROP_TEMP_MAX: | |
965 | val->intval = info->temp_max; | |
966 | return 0; | |
967 | default: | |
968 | return -EINVAL; | |
27a2195e SR |
969 | } |
970 | } | |
971 | EXPORT_SYMBOL_GPL(power_supply_battery_info_get_prop); | |
972 | ||
65dbad71 BW |
973 | /** |
974 | * power_supply_temp2resist_simple() - find the battery internal resistance | |
e9e7d165 | 975 | * percent from temperature |
65dbad71 BW |
976 | * @table: Pointer to battery resistance temperature table |
977 | * @table_len: The table length | |
152ee3d1 | 978 | * @temp: Current temperature |
65dbad71 BW |
979 | * |
980 | * This helper function is used to look up battery internal resistance percent | |
981 | * according to current temperature value from the resistance temperature table, | |
982 | * and the table must be ordered descending. Then the actual battery internal | |
983 | * resistance = the ideal battery internal resistance * percent / 100. | |
984 | * | |
985 | * Return: the battery internal resistance percent | |
986 | */ | |
987 | int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table, | |
988 | int table_len, int temp) | |
989 | { | |
a4585ba2 | 990 | int i, high, low; |
65dbad71 | 991 | |
093d27bb | 992 | for (i = 0; i < table_len; i++) |
65dbad71 BW |
993 | if (temp > table[i].temp) |
994 | break; | |
995 | ||
a4585ba2 | 996 | /* The library function will deal with high == low */ |
093d27bb DR |
997 | if (i == 0) |
998 | high = low = i; | |
999 | else if (i == table_len) | |
1000 | high = low = i - 1; | |
a4585ba2 | 1001 | else |
093d27bb | 1002 | high = (low = i) - 1; |
a4585ba2 LW |
1003 | |
1004 | return fixp_linear_interpolate(table[low].temp, | |
1005 | table[low].resistance, | |
1006 | table[high].temp, | |
1007 | table[high].resistance, | |
1008 | temp); | |
65dbad71 BW |
1009 | } |
1010 | EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple); | |
1011 | ||
e9e7d165 LW |
1012 | /** |
1013 | * power_supply_vbat2ri() - find the battery internal resistance | |
1014 | * from the battery voltage | |
1015 | * @info: The battery information container | |
e9e7d165 LW |
1016 | * @vbat_uv: The battery voltage in microvolt |
1017 | * @charging: If we are charging (true) or not (false) | |
1018 | * | |
1019 | * This helper function is used to look up battery internal resistance | |
1020 | * according to current battery voltage. Depending on whether the battery | |
1021 | * is currently charging or not, different resistance will be returned. | |
1022 | * | |
1023 | * Returns the internal resistance in microohm or negative error code. | |
1024 | */ | |
1025 | int power_supply_vbat2ri(struct power_supply_battery_info *info, | |
1026 | int vbat_uv, bool charging) | |
1027 | { | |
1028 | struct power_supply_vbat_ri_table *vbat2ri; | |
1029 | int table_len; | |
1030 | int i, high, low; | |
1031 | ||
1032 | /* | |
1033 | * If we are charging, and the battery supplies a separate table | |
1034 | * for this state, we use that in order to compensate for the | |
1035 | * charging voltage. Otherwise we use the main table. | |
1036 | */ | |
1037 | if (charging && info->vbat2ri_charging) { | |
1038 | vbat2ri = info->vbat2ri_charging; | |
1039 | table_len = info->vbat2ri_charging_size; | |
1040 | } else { | |
1041 | vbat2ri = info->vbat2ri_discharging; | |
1042 | table_len = info->vbat2ri_discharging_size; | |
1043 | } | |
1044 | ||
1045 | /* | |
1046 | * If no tables are specified, or if we are above the highest voltage in | |
1047 | * the voltage table, just return the factory specified internal resistance. | |
1048 | */ | |
1049 | if (!vbat2ri || (table_len <= 0) || (vbat_uv > vbat2ri[0].vbat_uv)) { | |
1050 | if (charging && (info->factory_internal_resistance_charging_uohm > 0)) | |
1051 | return info->factory_internal_resistance_charging_uohm; | |
1052 | else | |
1053 | return info->factory_internal_resistance_uohm; | |
1054 | } | |
1055 | ||
1056 | /* Break loop at table_len - 1 because that is the highest index */ | |
1057 | for (i = 0; i < table_len - 1; i++) | |
1058 | if (vbat_uv > vbat2ri[i].vbat_uv) | |
1059 | break; | |
1060 | ||
1061 | /* The library function will deal with high == low */ | |
1062 | if ((i == 0) || (i == (table_len - 1))) | |
1063 | high = i; | |
1064 | else | |
1065 | high = i - 1; | |
1066 | low = i; | |
1067 | ||
1068 | return fixp_linear_interpolate(vbat2ri[low].vbat_uv, | |
1069 | vbat2ri[low].ri_uohm, | |
1070 | vbat2ri[high].vbat_uv, | |
1071 | vbat2ri[high].ri_uohm, | |
1072 | vbat_uv); | |
1073 | } | |
1074 | EXPORT_SYMBOL_GPL(power_supply_vbat2ri); | |
1075 | ||
d72ce7d3 LW |
1076 | struct power_supply_maintenance_charge_table * |
1077 | power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info, | |
1078 | int index) | |
1079 | { | |
1080 | if (index >= info->maintenance_charge_size) | |
1081 | return NULL; | |
1082 | return &info->maintenance_charge[index]; | |
1083 | } | |
1084 | EXPORT_SYMBOL_GPL(power_supply_get_maintenance_charging_setting); | |
1085 | ||
3afb50d7 BW |
1086 | /** |
1087 | * power_supply_ocv2cap_simple() - find the battery capacity | |
1088 | * @table: Pointer to battery OCV lookup table | |
1089 | * @table_len: OCV table length | |
1090 | * @ocv: Current OCV value | |
1091 | * | |
1092 | * This helper function is used to look up battery capacity according to | |
1093 | * current OCV value from one OCV table, and the OCV table must be ordered | |
1094 | * descending. | |
1095 | * | |
1096 | * Return: the battery capacity. | |
1097 | */ | |
1098 | int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table, | |
1099 | int table_len, int ocv) | |
1100 | { | |
a4585ba2 | 1101 | int i, high, low; |
3afb50d7 | 1102 | |
093d27bb | 1103 | for (i = 0; i < table_len; i++) |
3afb50d7 BW |
1104 | if (ocv > table[i].ocv) |
1105 | break; | |
1106 | ||
a4585ba2 | 1107 | /* The library function will deal with high == low */ |
093d27bb DR |
1108 | if (i == 0) |
1109 | high = low = i; | |
1110 | else if (i == table_len) | |
1111 | high = low = i - 1; | |
a4585ba2 | 1112 | else |
093d27bb | 1113 | high = (low = i) - 1; |
a4585ba2 LW |
1114 | |
1115 | return fixp_linear_interpolate(table[low].ocv, | |
1116 | table[low].capacity, | |
1117 | table[high].ocv, | |
1118 | table[high].capacity, | |
1119 | ocv); | |
3afb50d7 BW |
1120 | } |
1121 | EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple); | |
1122 | ||
1123 | struct power_supply_battery_ocv_table * | |
1124 | power_supply_find_ocv2cap_table(struct power_supply_battery_info *info, | |
1125 | int temp, int *table_len) | |
1126 | { | |
1127 | int best_temp_diff = INT_MAX, temp_diff; | |
1128 | u8 i, best_index = 0; | |
1129 | ||
1130 | if (!info->ocv_table[0]) | |
1131 | return NULL; | |
1132 | ||
1133 | for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) { | |
51c7b6a0 LW |
1134 | /* Out of capacity tables */ |
1135 | if (!info->ocv_table[i]) | |
1136 | break; | |
1137 | ||
3afb50d7 BW |
1138 | temp_diff = abs(info->ocv_temp[i] - temp); |
1139 | ||
1140 | if (temp_diff < best_temp_diff) { | |
1141 | best_temp_diff = temp_diff; | |
1142 | best_index = i; | |
1143 | } | |
1144 | } | |
1145 | ||
1146 | *table_len = info->ocv_table_size[best_index]; | |
1147 | return info->ocv_table[best_index]; | |
1148 | } | |
1149 | EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table); | |
1150 | ||
1151 | int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, | |
1152 | int ocv, int temp) | |
1153 | { | |
1154 | struct power_supply_battery_ocv_table *table; | |
1155 | int table_len; | |
1156 | ||
1157 | table = power_supply_find_ocv2cap_table(info, temp, &table_len); | |
1158 | if (!table) | |
1159 | return -EINVAL; | |
1160 | ||
1161 | return power_supply_ocv2cap_simple(table, table_len, ocv); | |
1162 | } | |
1163 | EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap); | |
1164 | ||
1f918e0f LW |
1165 | bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info, |
1166 | int resistance) | |
1167 | { | |
1168 | int low, high; | |
1169 | ||
1170 | /* Nothing like this can be checked */ | |
1171 | if (info->bti_resistance_ohm <= 0) | |
1172 | return false; | |
1173 | ||
1174 | /* This will be extremely strict and unlikely to work */ | |
1175 | if (info->bti_resistance_tolerance <= 0) | |
1176 | return (info->bti_resistance_ohm == resistance); | |
1177 | ||
1178 | low = info->bti_resistance_ohm - | |
1179 | (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100; | |
1180 | high = info->bti_resistance_ohm + | |
1181 | (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100; | |
1182 | ||
1183 | return ((resistance >= low) && (resistance <= high)); | |
1184 | } | |
1185 | EXPORT_SYMBOL_GPL(power_supply_battery_bti_in_range); | |
1186 | ||
27a2195e SR |
1187 | static bool psy_has_property(const struct power_supply_desc *psy_desc, |
1188 | enum power_supply_property psp) | |
1189 | { | |
1190 | bool found = false; | |
1191 | int i; | |
1192 | ||
1193 | for (i = 0; i < psy_desc->num_properties; i++) { | |
1194 | if (psy_desc->properties[i] == psp) { | |
1195 | found = true; | |
1196 | break; | |
1197 | } | |
1198 | } | |
1199 | ||
1200 | return found; | |
1201 | } | |
1202 | ||
bc154056 KK |
1203 | int power_supply_get_property(struct power_supply *psy, |
1204 | enum power_supply_property psp, | |
1205 | union power_supply_propval *val) | |
1206 | { | |
e3805385 RK |
1207 | if (atomic_read(&psy->use_cnt) <= 0) { |
1208 | if (!psy->initialized) | |
1209 | return -EAGAIN; | |
bc154056 | 1210 | return -ENODEV; |
e3805385 | 1211 | } |
bc154056 | 1212 | |
27a2195e SR |
1213 | if (psy_has_property(psy->desc, psp)) |
1214 | return psy->desc->get_property(psy, psp, val); | |
1215 | else if (power_supply_battery_info_has_prop(psy->battery_info, psp)) | |
1216 | return power_supply_battery_info_get_prop(psy->battery_info, psp, val); | |
1217 | else | |
1218 | return -EINVAL; | |
bc154056 KK |
1219 | } |
1220 | EXPORT_SYMBOL_GPL(power_supply_get_property); | |
1221 | ||
1222 | int power_supply_set_property(struct power_supply *psy, | |
1223 | enum power_supply_property psp, | |
1224 | const union power_supply_propval *val) | |
1225 | { | |
297d716f | 1226 | if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property) |
bc154056 KK |
1227 | return -ENODEV; |
1228 | ||
297d716f | 1229 | return psy->desc->set_property(psy, psp, val); |
bc154056 KK |
1230 | } |
1231 | EXPORT_SYMBOL_GPL(power_supply_set_property); | |
1232 | ||
1233 | int power_supply_property_is_writeable(struct power_supply *psy, | |
1234 | enum power_supply_property psp) | |
1235 | { | |
297d716f KK |
1236 | if (atomic_read(&psy->use_cnt) <= 0 || |
1237 | !psy->desc->property_is_writeable) | |
bc154056 KK |
1238 | return -ENODEV; |
1239 | ||
297d716f | 1240 | return psy->desc->property_is_writeable(psy, psp); |
bc154056 KK |
1241 | } |
1242 | EXPORT_SYMBOL_GPL(power_supply_property_is_writeable); | |
1243 | ||
1244 | void power_supply_external_power_changed(struct power_supply *psy) | |
1245 | { | |
297d716f KK |
1246 | if (atomic_read(&psy->use_cnt) <= 0 || |
1247 | !psy->desc->external_power_changed) | |
bc154056 KK |
1248 | return; |
1249 | ||
297d716f | 1250 | psy->desc->external_power_changed(psy); |
bc154056 KK |
1251 | } |
1252 | EXPORT_SYMBOL_GPL(power_supply_external_power_changed); | |
1253 | ||
83516651 JF |
1254 | int power_supply_powers(struct power_supply *psy, struct device *dev) |
1255 | { | |
297d716f | 1256 | return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers"); |
83516651 JF |
1257 | } |
1258 | EXPORT_SYMBOL_GPL(power_supply_powers); | |
1259 | ||
5f487cd3 AV |
1260 | static void power_supply_dev_release(struct device *dev) |
1261 | { | |
285995d1 | 1262 | struct power_supply *psy = to_power_supply(dev); |
c73cc447 | 1263 | |
33a70677 | 1264 | dev_dbg(dev, "%s\n", __func__); |
297d716f | 1265 | kfree(psy); |
5f487cd3 AV |
1266 | } |
1267 | ||
d36240d2 PR |
1268 | int power_supply_reg_notifier(struct notifier_block *nb) |
1269 | { | |
bbaa6ffa | 1270 | return blocking_notifier_chain_register(&power_supply_notifier, nb); |
d36240d2 PR |
1271 | } |
1272 | EXPORT_SYMBOL_GPL(power_supply_reg_notifier); | |
1273 | ||
1274 | void power_supply_unreg_notifier(struct notifier_block *nb) | |
1275 | { | |
bbaa6ffa | 1276 | blocking_notifier_chain_unregister(&power_supply_notifier, nb); |
d36240d2 PR |
1277 | } |
1278 | EXPORT_SYMBOL_GPL(power_supply_unreg_notifier); | |
1279 | ||
3be330bf JT |
1280 | #ifdef CONFIG_THERMAL |
1281 | static int power_supply_read_temp(struct thermal_zone_device *tzd, | |
17e8351a | 1282 | int *temp) |
3be330bf JT |
1283 | { |
1284 | struct power_supply *psy; | |
1285 | union power_supply_propval val; | |
1286 | int ret; | |
1287 | ||
1288 | WARN_ON(tzd == NULL); | |
3d4e1bad | 1289 | psy = thermal_zone_device_priv(tzd); |
5bc28b93 RK |
1290 | ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val); |
1291 | if (ret) | |
1292 | return ret; | |
3be330bf JT |
1293 | |
1294 | /* Convert tenths of degree Celsius to milli degree Celsius. */ | |
5bc28b93 | 1295 | *temp = val.intval * 100; |
3be330bf JT |
1296 | |
1297 | return ret; | |
1298 | } | |
1299 | ||
1300 | static struct thermal_zone_device_ops psy_tzd_ops = { | |
1301 | .get_temp = power_supply_read_temp, | |
1302 | }; | |
1303 | ||
1304 | static int psy_register_thermal(struct power_supply *psy) | |
1305 | { | |
9ba533eb | 1306 | int ret; |
3be330bf | 1307 | |
297d716f | 1308 | if (psy->desc->no_thermal) |
a69d82b9 KK |
1309 | return 0; |
1310 | ||
3be330bf | 1311 | /* Register battery zone device psy reports temperature */ |
9ba533eb | 1312 | if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) { |
b52d51d8 CYT |
1313 | /* Prefer our hwmon device and avoid duplicates */ |
1314 | struct thermal_zone_params tzp = { | |
1315 | .no_hwmon = IS_ENABLED(CONFIG_POWER_SUPPLY_HWMON) | |
1316 | }; | |
cbcd51e8 RW |
1317 | psy->tzd = thermal_tripless_zone_device_register(psy->desc->name, |
1318 | psy, &psy_tzd_ops, &tzp); | |
9ba533eb MK |
1319 | if (IS_ERR(psy->tzd)) |
1320 | return PTR_ERR(psy->tzd); | |
1321 | ret = thermal_zone_device_enable(psy->tzd); | |
1322 | if (ret) | |
1323 | thermal_zone_device_unregister(psy->tzd); | |
1324 | return ret; | |
3be330bf | 1325 | } |
9ba533eb | 1326 | |
3be330bf JT |
1327 | return 0; |
1328 | } | |
1329 | ||
1330 | static void psy_unregister_thermal(struct power_supply *psy) | |
1331 | { | |
1332 | if (IS_ERR_OR_NULL(psy->tzd)) | |
1333 | return; | |
1334 | thermal_zone_device_unregister(psy->tzd); | |
1335 | } | |
952aeeb3 | 1336 | |
3be330bf JT |
1337 | #else |
1338 | static int psy_register_thermal(struct power_supply *psy) | |
1339 | { | |
1340 | return 0; | |
1341 | } | |
1342 | ||
1343 | static void psy_unregister_thermal(struct power_supply *psy) | |
1344 | { | |
1345 | } | |
1346 | #endif | |
1347 | ||
297d716f KK |
1348 | static struct power_supply *__must_check |
1349 | __power_supply_register(struct device *parent, | |
1350 | const struct power_supply_desc *desc, | |
2dc9215d KK |
1351 | const struct power_supply_config *cfg, |
1352 | bool ws) | |
4a11b59d | 1353 | { |
5f487cd3 | 1354 | struct device *dev; |
297d716f | 1355 | struct power_supply *psy; |
9ba533eb | 1356 | int rc; |
4a11b59d | 1357 | |
bfaecf46 | 1358 | if (!desc || !desc->name || !desc->properties || !desc->num_properties) |
1359 | return ERR_PTR(-EINVAL); | |
1360 | ||
7f1a57fd KK |
1361 | if (!parent) |
1362 | pr_warn("%s: Expected proper parent device for '%s'\n", | |
1363 | __func__, desc->name); | |
1364 | ||
9ba533eb MK |
1365 | if (psy_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) && |
1366 | (!desc->usb_types || !desc->num_usb_types)) | |
1367 | return ERR_PTR(-EINVAL); | |
cf450041 | 1368 | |
297d716f KK |
1369 | psy = kzalloc(sizeof(*psy), GFP_KERNEL); |
1370 | if (!psy) | |
1371 | return ERR_PTR(-ENOMEM); | |
1372 | ||
1373 | dev = &psy->dev; | |
4a11b59d | 1374 | |
5f487cd3 | 1375 | device_initialize(dev); |
4a11b59d | 1376 | |
71c2cc5c | 1377 | dev->class = &power_supply_class; |
5f487cd3 AV |
1378 | dev->type = &power_supply_dev_type; |
1379 | dev->parent = parent; | |
1380 | dev->release = power_supply_dev_release; | |
1381 | dev_set_drvdata(dev, psy); | |
297d716f | 1382 | psy->desc = desc; |
2dc9215d | 1383 | if (cfg) { |
cef8fe6a | 1384 | dev->groups = cfg->attr_grp; |
2dc9215d | 1385 | psy->drv_data = cfg->drv_data; |
ece711b5 AT |
1386 | psy->of_node = |
1387 | cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node; | |
58e4aacb | 1388 | dev->of_node = psy->of_node; |
2dc9215d KK |
1389 | psy->supplied_to = cfg->supplied_to; |
1390 | psy->num_supplicants = cfg->num_supplicants; | |
1391 | } | |
5f487cd3 | 1392 | |
297d716f | 1393 | rc = dev_set_name(dev, "%s", desc->name); |
80c6463e SK |
1394 | if (rc) |
1395 | goto dev_set_name_failed; | |
1396 | ||
97774672 | 1397 | INIT_WORK(&psy->changed_work, power_supply_changed_work); |
7f1a57fd KK |
1398 | INIT_DELAYED_WORK(&psy->deferred_register_work, |
1399 | power_supply_deferred_register_work); | |
97774672 | 1400 | |
f6e0b081 RK |
1401 | rc = power_supply_check_supplies(psy); |
1402 | if (rc) { | |
3af26e2a | 1403 | dev_dbg(dev, "Not all required supplies found, defer probe\n"); |
f6e0b081 RK |
1404 | goto check_supplies_failed; |
1405 | } | |
1406 | ||
27a2195e SR |
1407 | /* |
1408 | * Expose constant battery info, if it is available. While there are | |
1409 | * some chargers accessing constant battery data, we only want to | |
1410 | * expose battery data to userspace for battery devices. | |
1411 | */ | |
1412 | if (desc->type == POWER_SUPPLY_TYPE_BATTERY) { | |
1413 | rc = power_supply_get_battery_info(psy, &psy->battery_info); | |
1414 | if (rc && rc != -ENODEV && rc != -ENOENT) | |
1415 | goto check_supplies_failed; | |
1416 | } | |
1417 | ||
948dcf96 | 1418 | spin_lock_init(&psy->changed_lock); |
5f487cd3 | 1419 | rc = device_add(dev); |
4a11b59d | 1420 | if (rc) |
5f487cd3 AV |
1421 | goto device_add_failed; |
1422 | ||
82880222 SB |
1423 | rc = device_init_wakeup(dev, ws); |
1424 | if (rc) | |
1425 | goto wakeup_init_failed; | |
1426 | ||
3be330bf JT |
1427 | rc = psy_register_thermal(psy); |
1428 | if (rc) | |
1429 | goto register_thermal_failed; | |
1430 | ||
4a11b59d AV |
1431 | rc = power_supply_create_triggers(psy); |
1432 | if (rc) | |
1433 | goto create_triggers_failed; | |
1434 | ||
e67d4dfc AS |
1435 | rc = power_supply_add_hwmon_sysfs(psy); |
1436 | if (rc) | |
1437 | goto add_hwmon_sysfs_failed; | |
1438 | ||
8e59c7f2 KK |
1439 | /* |
1440 | * Update use_cnt after any uevents (most notably from device_add()). | |
1441 | * We are here still during driver's probe but | |
1442 | * the power_supply_uevent() calls back driver's get_property | |
1443 | * method so: | |
1444 | * 1. Driver did not assigned the returned struct power_supply, | |
1445 | * 2. Driver could not finish initialization (anything in its probe | |
1446 | * after calling power_supply_register()). | |
1447 | */ | |
1448 | atomic_inc(&psy->use_cnt); | |
e3805385 | 1449 | psy->initialized = true; |
7f1a57fd KK |
1450 | |
1451 | queue_delayed_work(system_power_efficient_wq, | |
1452 | &psy->deferred_register_work, | |
1453 | POWER_SUPPLY_DEFERRED_REGISTER_TIME); | |
4a11b59d | 1454 | |
297d716f | 1455 | return psy; |
4a11b59d | 1456 | |
e67d4dfc AS |
1457 | add_hwmon_sysfs_failed: |
1458 | power_supply_remove_triggers(psy); | |
4a11b59d | 1459 | create_triggers_failed: |
3be330bf JT |
1460 | psy_unregister_thermal(psy); |
1461 | register_thermal_failed: | |
80c6463e | 1462 | wakeup_init_failed: |
5b79480c | 1463 | device_del(dev); |
82880222 | 1464 | device_add_failed: |
f6e0b081 | 1465 | check_supplies_failed: |
80c6463e | 1466 | dev_set_name_failed: |
3a2dbd61 | 1467 | put_device(dev); |
297d716f | 1468 | return ERR_PTR(rc); |
4a11b59d | 1469 | } |
9113e260 | 1470 | |
297d716f KK |
1471 | /** |
1472 | * power_supply_register() - Register new power supply | |
7f1a57fd KK |
1473 | * @parent: Device to be a parent of power supply's device, usually |
1474 | * the device which probe function calls this | |
297d716f KK |
1475 | * @desc: Description of power supply, must be valid through whole |
1476 | * lifetime of this power supply | |
1477 | * @cfg: Run-time specific configuration accessed during registering, | |
1478 | * may be NULL | |
1479 | * | |
1480 | * Return: A pointer to newly allocated power_supply on success | |
1481 | * or ERR_PTR otherwise. | |
1482 | * Use power_supply_unregister() on returned power_supply pointer to release | |
1483 | * resources. | |
1484 | */ | |
1485 | struct power_supply *__must_check power_supply_register(struct device *parent, | |
1486 | const struct power_supply_desc *desc, | |
2dc9215d | 1487 | const struct power_supply_config *cfg) |
9113e260 | 1488 | { |
297d716f | 1489 | return __power_supply_register(parent, desc, cfg, true); |
9113e260 | 1490 | } |
ff3417e7 | 1491 | EXPORT_SYMBOL_GPL(power_supply_register); |
4a11b59d | 1492 | |
297d716f | 1493 | /** |
43df6105 | 1494 | * power_supply_register_no_ws() - Register new non-waking-source power supply |
7f1a57fd KK |
1495 | * @parent: Device to be a parent of power supply's device, usually |
1496 | * the device which probe function calls this | |
297d716f KK |
1497 | * @desc: Description of power supply, must be valid through whole |
1498 | * lifetime of this power supply | |
1499 | * @cfg: Run-time specific configuration accessed during registering, | |
1500 | * may be NULL | |
1501 | * | |
1502 | * Return: A pointer to newly allocated power_supply on success | |
1503 | * or ERR_PTR otherwise. | |
1504 | * Use power_supply_unregister() on returned power_supply pointer to release | |
1505 | * resources. | |
1506 | */ | |
1507 | struct power_supply *__must_check | |
1508 | power_supply_register_no_ws(struct device *parent, | |
1509 | const struct power_supply_desc *desc, | |
2dc9215d | 1510 | const struct power_supply_config *cfg) |
9113e260 | 1511 | { |
297d716f | 1512 | return __power_supply_register(parent, desc, cfg, false); |
9113e260 ZR |
1513 | } |
1514 | EXPORT_SYMBOL_GPL(power_supply_register_no_ws); | |
1515 | ||
5d8a4219 N |
1516 | static void devm_power_supply_release(struct device *dev, void *res) |
1517 | { | |
1518 | struct power_supply **psy = res; | |
1519 | ||
1520 | power_supply_unregister(*psy); | |
1521 | } | |
1522 | ||
297d716f | 1523 | /** |
43df6105 | 1524 | * devm_power_supply_register() - Register managed power supply |
7f1a57fd KK |
1525 | * @parent: Device to be a parent of power supply's device, usually |
1526 | * the device which probe function calls this | |
297d716f KK |
1527 | * @desc: Description of power supply, must be valid through whole |
1528 | * lifetime of this power supply | |
1529 | * @cfg: Run-time specific configuration accessed during registering, | |
1530 | * may be NULL | |
1531 | * | |
1532 | * Return: A pointer to newly allocated power_supply on success | |
1533 | * or ERR_PTR otherwise. | |
1534 | * The returned power_supply pointer will be automatically unregistered | |
1535 | * on driver detach. | |
1536 | */ | |
1537 | struct power_supply *__must_check | |
1538 | devm_power_supply_register(struct device *parent, | |
1539 | const struct power_supply_desc *desc, | |
2dc9215d | 1540 | const struct power_supply_config *cfg) |
5d8a4219 | 1541 | { |
297d716f KK |
1542 | struct power_supply **ptr, *psy; |
1543 | ||
1544 | ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL); | |
5d8a4219 N |
1545 | |
1546 | if (!ptr) | |
297d716f KK |
1547 | return ERR_PTR(-ENOMEM); |
1548 | psy = __power_supply_register(parent, desc, cfg, true); | |
1549 | if (IS_ERR(psy)) { | |
5d8a4219 | 1550 | devres_free(ptr); |
297d716f | 1551 | } else { |
5d8a4219 N |
1552 | *ptr = psy; |
1553 | devres_add(parent, ptr); | |
1554 | } | |
297d716f | 1555 | return psy; |
5d8a4219 N |
1556 | } |
1557 | EXPORT_SYMBOL_GPL(devm_power_supply_register); | |
1558 | ||
297d716f | 1559 | /** |
43df6105 | 1560 | * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply |
7f1a57fd KK |
1561 | * @parent: Device to be a parent of power supply's device, usually |
1562 | * the device which probe function calls this | |
297d716f KK |
1563 | * @desc: Description of power supply, must be valid through whole |
1564 | * lifetime of this power supply | |
1565 | * @cfg: Run-time specific configuration accessed during registering, | |
1566 | * may be NULL | |
1567 | * | |
1568 | * Return: A pointer to newly allocated power_supply on success | |
1569 | * or ERR_PTR otherwise. | |
1570 | * The returned power_supply pointer will be automatically unregistered | |
1571 | * on driver detach. | |
1572 | */ | |
1573 | struct power_supply *__must_check | |
1574 | devm_power_supply_register_no_ws(struct device *parent, | |
1575 | const struct power_supply_desc *desc, | |
2dc9215d | 1576 | const struct power_supply_config *cfg) |
5d8a4219 | 1577 | { |
297d716f KK |
1578 | struct power_supply **ptr, *psy; |
1579 | ||
1580 | ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL); | |
5d8a4219 N |
1581 | |
1582 | if (!ptr) | |
297d716f KK |
1583 | return ERR_PTR(-ENOMEM); |
1584 | psy = __power_supply_register(parent, desc, cfg, false); | |
1585 | if (IS_ERR(psy)) { | |
5d8a4219 | 1586 | devres_free(ptr); |
297d716f | 1587 | } else { |
5d8a4219 N |
1588 | *ptr = psy; |
1589 | devres_add(parent, ptr); | |
1590 | } | |
297d716f | 1591 | return psy; |
5d8a4219 N |
1592 | } |
1593 | EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws); | |
1594 | ||
297d716f KK |
1595 | /** |
1596 | * power_supply_unregister() - Remove this power supply from system | |
1597 | * @psy: Pointer to power supply to unregister | |
1598 | * | |
1599 | * Remove this power supply from the system. The resources of power supply | |
1600 | * will be freed here or on last power_supply_put() call. | |
1601 | */ | |
4a11b59d AV |
1602 | void power_supply_unregister(struct power_supply *psy) |
1603 | { | |
bc154056 | 1604 | WARN_ON(atomic_dec_return(&psy->use_cnt)); |
3ffa6583 | 1605 | psy->removing = true; |
bc51e7ff | 1606 | cancel_work_sync(&psy->changed_work); |
7f1a57fd | 1607 | cancel_delayed_work_sync(&psy->deferred_register_work); |
297d716f | 1608 | sysfs_remove_link(&psy->dev.kobj, "powers"); |
e67d4dfc | 1609 | power_supply_remove_hwmon_sysfs(psy); |
4a11b59d | 1610 | power_supply_remove_triggers(psy); |
3be330bf | 1611 | psy_unregister_thermal(psy); |
297d716f KK |
1612 | device_init_wakeup(&psy->dev, false); |
1613 | device_unregister(&psy->dev); | |
4a11b59d | 1614 | } |
ff3417e7 | 1615 | EXPORT_SYMBOL_GPL(power_supply_unregister); |
4a11b59d | 1616 | |
e44ea364 KK |
1617 | void *power_supply_get_drvdata(struct power_supply *psy) |
1618 | { | |
1619 | return psy->drv_data; | |
1620 | } | |
1621 | EXPORT_SYMBOL_GPL(power_supply_get_drvdata); | |
1622 | ||
4a11b59d AV |
1623 | static int __init power_supply_class_init(void) |
1624 | { | |
71c2cc5c | 1625 | int err; |
4a11b59d | 1626 | |
71c2cc5c RM |
1627 | err = class_register(&power_supply_class); |
1628 | if (err) | |
1629 | return err; | |
4a11b59d | 1630 | |
7b46b609 | 1631 | power_supply_init_attrs(); |
4a11b59d AV |
1632 | |
1633 | return 0; | |
1634 | } | |
1635 | ||
1636 | static void __exit power_supply_class_exit(void) | |
1637 | { | |
71c2cc5c | 1638 | class_unregister(&power_supply_class); |
4a11b59d AV |
1639 | } |
1640 | ||
4a11b59d AV |
1641 | subsys_initcall(power_supply_class_init); |
1642 | module_exit(power_supply_class_exit); | |
1643 | ||
1644 | MODULE_DESCRIPTION("Universal power supply monitor class"); | |
3cbbe1be CM |
1645 | MODULE_AUTHOR("Ian Molton <spyro@f2s.com>"); |
1646 | MODULE_AUTHOR("Szabolcs Gyurko"); | |
1647 | MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>"); |