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