1 /*
2 * Universal power supply monitor class
3 *
4 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
5 * Copyright © 2004 Szabolcs Gyurko
6 * Copyright © 2003 Ian Molton <spyro@f2s.com>
7 *
8 * Modified: 2004, Oct Szabolcs Gyurko
9 *
10 * You may use this code as per GPL version 2
11 */
12
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/power_supply.h>
20 #include <linux/thermal.h>
21 #include "power_supply.h"
22
23 /* exported for the APM Power driver, APM emulation */
24 struct class *power_supply_class;
25 EXPORT_SYMBOL_GPL(power_supply_class);
26
27 static struct device_type power_supply_dev_type;
28
__power_supply_is_supplied_by(struct power_supply * supplier,struct power_supply * supply)29 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
30 struct power_supply *supply)
31 {
32 int i;
33
34 if (!supply->supplied_from && !supplier->supplied_to)
35 return false;
36
37 /* Support both supplied_to and supplied_from modes */
38 if (supply->supplied_from) {
39 if (!supplier->name)
40 return false;
41 for (i = 0; i < supply->num_supplies; i++)
42 if (!strcmp(supplier->name, supply->supplied_from[i]))
43 return true;
44 } else {
45 if (!supply->name)
46 return false;
47 for (i = 0; i < supplier->num_supplicants; i++)
48 if (!strcmp(supplier->supplied_to[i], supply->name))
49 return true;
50 }
51
52 return false;
53 }
54
__power_supply_changed_work(struct device * dev,void * data)55 static int __power_supply_changed_work(struct device *dev, void *data)
56 {
57 struct power_supply *psy = (struct power_supply *)data;
58 struct power_supply *pst = dev_get_drvdata(dev);
59
60 if (__power_supply_is_supplied_by(psy, pst)) {
61 if (pst->external_power_changed)
62 pst->external_power_changed(pst);
63 }
64
65 return 0;
66 }
67
power_supply_changed_work(struct work_struct * work)68 static void power_supply_changed_work(struct work_struct *work)
69 {
70 unsigned long flags;
71 struct power_supply *psy = container_of(work, struct power_supply,
72 changed_work);
73
74 dev_dbg(psy->dev, "%s\n", __func__);
75
76 spin_lock_irqsave(&psy->changed_lock, flags);
77 if (psy->changed) {
78 psy->changed = false;
79 spin_unlock_irqrestore(&psy->changed_lock, flags);
80
81 class_for_each_device(power_supply_class, NULL, psy,
82 __power_supply_changed_work);
83
84 power_supply_update_leds(psy);
85
86 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
87 spin_lock_irqsave(&psy->changed_lock, flags);
88 }
89 if (!psy->changed)
90 pm_relax(psy->dev);
91 spin_unlock_irqrestore(&psy->changed_lock, flags);
92 }
93
power_supply_changed(struct power_supply * psy)94 void power_supply_changed(struct power_supply *psy)
95 {
96 unsigned long flags;
97
98 dev_dbg(psy->dev, "%s\n", __func__);
99
100 spin_lock_irqsave(&psy->changed_lock, flags);
101 psy->changed = true;
102 pm_stay_awake(psy->dev);
103 spin_unlock_irqrestore(&psy->changed_lock, flags);
104 schedule_work(&psy->changed_work);
105 }
106 EXPORT_SYMBOL_GPL(power_supply_changed);
107
108 #ifdef CONFIG_OF
109 #include <linux/of.h>
110
__power_supply_populate_supplied_from(struct device * dev,void * data)111 static int __power_supply_populate_supplied_from(struct device *dev,
112 void *data)
113 {
114 struct power_supply *psy = (struct power_supply *)data;
115 struct power_supply *epsy = dev_get_drvdata(dev);
116 struct device_node *np;
117 int i = 0;
118
119 do {
120 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
121 if (!np)
122 continue;
123
124 if (np == epsy->of_node) {
125 dev_info(psy->dev, "%s: Found supply : %s\n",
126 psy->name, epsy->name);
127 psy->supplied_from[i-1] = (char *)epsy->name;
128 psy->num_supplies++;
129 break;
130 }
131 } while (np);
132
133 return 0;
134 }
135
power_supply_populate_supplied_from(struct power_supply * psy)136 static int power_supply_populate_supplied_from(struct power_supply *psy)
137 {
138 int error;
139
140 error = class_for_each_device(power_supply_class, NULL, psy,
141 __power_supply_populate_supplied_from);
142
143 dev_dbg(psy->dev, "%s %d\n", __func__, error);
144
145 return error;
146 }
147
__power_supply_find_supply_from_node(struct device * dev,void * data)148 static int __power_supply_find_supply_from_node(struct device *dev,
149 void *data)
150 {
151 struct device_node *np = (struct device_node *)data;
152 struct power_supply *epsy = dev_get_drvdata(dev);
153
154 /* return error breaks out of class_for_each_device loop */
155 if (epsy->of_node == np)
156 return -EINVAL;
157
158 return 0;
159 }
160
power_supply_find_supply_from_node(struct device_node * supply_node)161 static int power_supply_find_supply_from_node(struct device_node *supply_node)
162 {
163 int error;
164 struct device *dev;
165 struct class_dev_iter iter;
166
167 /*
168 * Use iterator to see if any other device is registered.
169 * This is required since class_for_each_device returns 0
170 * if there are no devices registered.
171 */
172 class_dev_iter_init(&iter, power_supply_class, NULL, NULL);
173 dev = class_dev_iter_next(&iter);
174
175 if (!dev)
176 return -EPROBE_DEFER;
177
178 /*
179 * We have to treat the return value as inverted, because if
180 * we return error on not found, then it won't continue looking.
181 * So we trick it by returning error on success to stop looking
182 * once the matching device is found.
183 */
184 error = class_for_each_device(power_supply_class, NULL, supply_node,
185 __power_supply_find_supply_from_node);
186
187 return error ? 0 : -EPROBE_DEFER;
188 }
189
power_supply_check_supplies(struct power_supply * psy)190 static int power_supply_check_supplies(struct power_supply *psy)
191 {
192 struct device_node *np;
193 int cnt = 0;
194
195 /* If there is already a list honor it */
196 if (psy->supplied_from && psy->num_supplies > 0)
197 return 0;
198
199 /* No device node found, nothing to do */
200 if (!psy->of_node)
201 return 0;
202
203 do {
204 int ret;
205
206 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
207 if (!np)
208 continue;
209
210 ret = power_supply_find_supply_from_node(np);
211 if (ret) {
212 dev_dbg(psy->dev, "Failed to find supply, defer!\n");
213 return -EPROBE_DEFER;
214 }
215 } while (np);
216
217 /* All supplies found, allocate char ** array for filling */
218 psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from),
219 GFP_KERNEL);
220 if (!psy->supplied_from) {
221 dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
222 return -ENOMEM;
223 }
224
225 *psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * cnt,
226 GFP_KERNEL);
227 if (!*psy->supplied_from) {
228 dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
229 return -ENOMEM;
230 }
231
232 return power_supply_populate_supplied_from(psy);
233 }
234 #else
power_supply_check_supplies(struct power_supply * psy)235 static inline int power_supply_check_supplies(struct power_supply *psy)
236 {
237 return 0;
238 }
239 #endif
240
__power_supply_am_i_supplied(struct device * dev,void * data)241 static int __power_supply_am_i_supplied(struct device *dev, void *data)
242 {
243 union power_supply_propval ret = {0,};
244 struct power_supply *psy = (struct power_supply *)data;
245 struct power_supply *epsy = dev_get_drvdata(dev);
246
247 if (__power_supply_is_supplied_by(epsy, psy))
248 if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) {
249 if (ret.intval)
250 return ret.intval;
251 }
252
253 return 0;
254 }
255
power_supply_am_i_supplied(struct power_supply * psy)256 int power_supply_am_i_supplied(struct power_supply *psy)
257 {
258 int error;
259
260 error = class_for_each_device(power_supply_class, NULL, psy,
261 __power_supply_am_i_supplied);
262
263 dev_dbg(psy->dev, "%s %d\n", __func__, error);
264
265 return error;
266 }
267 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
268
__power_supply_is_system_supplied(struct device * dev,void * data)269 static int __power_supply_is_system_supplied(struct device *dev, void *data)
270 {
271 union power_supply_propval ret = {0,};
272 struct power_supply *psy = dev_get_drvdata(dev);
273 unsigned int *count = data;
274
275 (*count)++;
276 if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
277 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
278 return 0;
279 if (ret.intval)
280 return ret.intval;
281 }
282 return 0;
283 }
284
power_supply_is_system_supplied(void)285 int power_supply_is_system_supplied(void)
286 {
287 int error;
288 unsigned int count = 0;
289
290 error = class_for_each_device(power_supply_class, NULL, &count,
291 __power_supply_is_system_supplied);
292
293 /*
294 * If no power class device was found at all, most probably we are
295 * running on a desktop system, so assume we are on mains power.
296 */
297 if (count == 0)
298 return 1;
299
300 return error;
301 }
302 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
303
power_supply_set_battery_charged(struct power_supply * psy)304 int power_supply_set_battery_charged(struct power_supply *psy)
305 {
306 if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
307 psy->set_charged(psy);
308 return 0;
309 }
310
311 return -EINVAL;
312 }
313 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
314
power_supply_match_device_by_name(struct device * dev,const void * data)315 static int power_supply_match_device_by_name(struct device *dev, const void *data)
316 {
317 const char *name = data;
318 struct power_supply *psy = dev_get_drvdata(dev);
319
320 return strcmp(psy->name, name) == 0;
321 }
322
power_supply_get_by_name(const char * name)323 struct power_supply *power_supply_get_by_name(const char *name)
324 {
325 struct device *dev = class_find_device(power_supply_class, NULL, name,
326 power_supply_match_device_by_name);
327
328 return dev ? dev_get_drvdata(dev) : NULL;
329 }
330 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
331
power_supply_powers(struct power_supply * psy,struct device * dev)332 int power_supply_powers(struct power_supply *psy, struct device *dev)
333 {
334 return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
335 }
336 EXPORT_SYMBOL_GPL(power_supply_powers);
337
power_supply_dev_release(struct device * dev)338 static void power_supply_dev_release(struct device *dev)
339 {
340 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
341 kfree(dev);
342 }
343
344 #ifdef CONFIG_THERMAL
power_supply_read_temp(struct thermal_zone_device * tzd,unsigned long * temp)345 static int power_supply_read_temp(struct thermal_zone_device *tzd,
346 unsigned long *temp)
347 {
348 struct power_supply *psy;
349 union power_supply_propval val;
350 int ret;
351
352 WARN_ON(tzd == NULL);
353 psy = tzd->devdata;
354 ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
355
356 /* Convert tenths of degree Celsius to milli degree Celsius. */
357 if (!ret)
358 *temp = val.intval * 100;
359
360 return ret;
361 }
362
363 static struct thermal_zone_device_ops psy_tzd_ops = {
364 .get_temp = power_supply_read_temp,
365 };
366
psy_register_thermal(struct power_supply * psy)367 static int psy_register_thermal(struct power_supply *psy)
368 {
369 int i;
370
371 /* Register battery zone device psy reports temperature */
372 for (i = 0; i < psy->num_properties; i++) {
373 if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
374 psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
375 psy, &psy_tzd_ops, NULL, 0, 0);
376 if (IS_ERR(psy->tzd))
377 return PTR_ERR(psy->tzd);
378 break;
379 }
380 }
381 return 0;
382 }
383
psy_unregister_thermal(struct power_supply * psy)384 static void psy_unregister_thermal(struct power_supply *psy)
385 {
386 if (IS_ERR_OR_NULL(psy->tzd))
387 return;
388 thermal_zone_device_unregister(psy->tzd);
389 }
390
391 /* thermal cooling device callbacks */
ps_get_max_charge_cntl_limit(struct thermal_cooling_device * tcd,unsigned long * state)392 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
393 unsigned long *state)
394 {
395 struct power_supply *psy;
396 union power_supply_propval val;
397 int ret;
398
399 psy = tcd->devdata;
400 ret = psy->get_property(psy,
401 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
402 if (!ret)
403 *state = val.intval;
404
405 return ret;
406 }
407
ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device * tcd,unsigned long * state)408 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
409 unsigned long *state)
410 {
411 struct power_supply *psy;
412 union power_supply_propval val;
413 int ret;
414
415 psy = tcd->devdata;
416 ret = psy->get_property(psy,
417 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
418 if (!ret)
419 *state = val.intval;
420
421 return ret;
422 }
423
ps_set_cur_charge_cntl_limit(struct thermal_cooling_device * tcd,unsigned long state)424 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
425 unsigned long state)
426 {
427 struct power_supply *psy;
428 union power_supply_propval val;
429 int ret;
430
431 psy = tcd->devdata;
432 val.intval = state;
433 ret = psy->set_property(psy,
434 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
435
436 return ret;
437 }
438
439 static struct thermal_cooling_device_ops psy_tcd_ops = {
440 .get_max_state = ps_get_max_charge_cntl_limit,
441 .get_cur_state = ps_get_cur_chrage_cntl_limit,
442 .set_cur_state = ps_set_cur_charge_cntl_limit,
443 };
444
psy_register_cooler(struct power_supply * psy)445 static int psy_register_cooler(struct power_supply *psy)
446 {
447 int i;
448
449 /* Register for cooling device if psy can control charging */
450 for (i = 0; i < psy->num_properties; i++) {
451 if (psy->properties[i] ==
452 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
453 psy->tcd = thermal_cooling_device_register(
454 (char *)psy->name,
455 psy, &psy_tcd_ops);
456 if (IS_ERR(psy->tcd))
457 return PTR_ERR(psy->tcd);
458 break;
459 }
460 }
461 return 0;
462 }
463
psy_unregister_cooler(struct power_supply * psy)464 static void psy_unregister_cooler(struct power_supply *psy)
465 {
466 if (IS_ERR_OR_NULL(psy->tcd))
467 return;
468 thermal_cooling_device_unregister(psy->tcd);
469 }
470 #else
psy_register_thermal(struct power_supply * psy)471 static int psy_register_thermal(struct power_supply *psy)
472 {
473 return 0;
474 }
475
psy_unregister_thermal(struct power_supply * psy)476 static void psy_unregister_thermal(struct power_supply *psy)
477 {
478 }
479
psy_register_cooler(struct power_supply * psy)480 static int psy_register_cooler(struct power_supply *psy)
481 {
482 return 0;
483 }
484
psy_unregister_cooler(struct power_supply * psy)485 static void psy_unregister_cooler(struct power_supply *psy)
486 {
487 }
488 #endif
489
power_supply_register(struct device * parent,struct power_supply * psy)490 int power_supply_register(struct device *parent, struct power_supply *psy)
491 {
492 struct device *dev;
493 int rc;
494
495 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
496 if (!dev)
497 return -ENOMEM;
498
499 device_initialize(dev);
500
501 dev->class = power_supply_class;
502 dev->type = &power_supply_dev_type;
503 dev->parent = parent;
504 dev->release = power_supply_dev_release;
505 dev_set_drvdata(dev, psy);
506 psy->dev = dev;
507
508 INIT_WORK(&psy->changed_work, power_supply_changed_work);
509
510 rc = power_supply_check_supplies(psy);
511 if (rc) {
512 dev_info(dev, "Not all required supplies found, defer probe\n");
513 goto check_supplies_failed;
514 }
515
516 rc = kobject_set_name(&dev->kobj, "%s", psy->name);
517 if (rc)
518 goto kobject_set_name_failed;
519
520 rc = device_add(dev);
521 if (rc)
522 goto device_add_failed;
523
524 spin_lock_init(&psy->changed_lock);
525 rc = device_init_wakeup(dev, true);
526 if (rc)
527 goto wakeup_init_failed;
528
529 rc = psy_register_thermal(psy);
530 if (rc)
531 goto register_thermal_failed;
532
533 rc = psy_register_cooler(psy);
534 if (rc)
535 goto register_cooler_failed;
536
537 rc = power_supply_create_triggers(psy);
538 if (rc)
539 goto create_triggers_failed;
540
541 power_supply_changed(psy);
542
543 goto success;
544
545 create_triggers_failed:
546 psy_unregister_cooler(psy);
547 register_cooler_failed:
548 psy_unregister_thermal(psy);
549 register_thermal_failed:
550 wakeup_init_failed:
551 device_del(dev);
552 kobject_set_name_failed:
553 device_add_failed:
554 check_supplies_failed:
555 put_device(dev);
556 success:
557 return rc;
558 }
559 EXPORT_SYMBOL_GPL(power_supply_register);
560
power_supply_unregister(struct power_supply * psy)561 void power_supply_unregister(struct power_supply *psy)
562 {
563 cancel_work_sync(&psy->changed_work);
564 sysfs_remove_link(&psy->dev->kobj, "powers");
565 power_supply_remove_triggers(psy);
566 psy_unregister_cooler(psy);
567 psy_unregister_thermal(psy);
568 device_unregister(psy->dev);
569 }
570 EXPORT_SYMBOL_GPL(power_supply_unregister);
571
power_supply_class_init(void)572 static int __init power_supply_class_init(void)
573 {
574 power_supply_class = class_create(THIS_MODULE, "power_supply");
575
576 if (IS_ERR(power_supply_class))
577 return PTR_ERR(power_supply_class);
578
579 power_supply_class->dev_uevent = power_supply_uevent;
580 power_supply_init_attrs(&power_supply_dev_type);
581
582 return 0;
583 }
584
power_supply_class_exit(void)585 static void __exit power_supply_class_exit(void)
586 {
587 class_destroy(power_supply_class);
588 }
589
590 subsys_initcall(power_supply_class_init);
591 module_exit(power_supply_class_exit);
592
593 MODULE_DESCRIPTION("Universal power supply monitor class");
594 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
595 "Szabolcs Gyurko, "
596 "Anton Vorontsov <cbou@mail.ru>");
597 MODULE_LICENSE("GPL");
598