power_supply_core.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/init.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/power_supply.h>
  18. #include "power_supply.h"
  19. /* exported for the APM Power driver, APM emulation */
  20. struct class *power_supply_class;
  21. EXPORT_SYMBOL_GPL(power_supply_class);
  22. static int __power_supply_changed_work(struct device *dev, void *data)
  23. {
  24. struct power_supply *psy = (struct power_supply *)data;
  25. struct power_supply *pst = dev_get_drvdata(dev);
  26. int i;
  27. for (i = 0; i < psy->num_supplicants; i++)
  28. if (!strcmp(psy->supplied_to[i], pst->name)) {
  29. if (pst->external_power_changed)
  30. pst->external_power_changed(pst);
  31. }
  32. return 0;
  33. }
  34. static void power_supply_changed_work(struct work_struct *work)
  35. {
  36. struct power_supply *psy = container_of(work, struct power_supply,
  37. changed_work);
  38. dev_dbg(psy->dev, "%s\n", __func__);
  39. class_for_each_device(power_supply_class, NULL, psy,
  40. __power_supply_changed_work);
  41. power_supply_update_leds(psy);
  42. kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
  43. }
  44. void power_supply_changed(struct power_supply *psy)
  45. {
  46. dev_dbg(psy->dev, "%s\n", __func__);
  47. schedule_work(&psy->changed_work);
  48. }
  49. EXPORT_SYMBOL_GPL(power_supply_changed);
  50. static int __power_supply_am_i_supplied(struct device *dev, void *data)
  51. {
  52. union power_supply_propval ret = {0,};
  53. struct power_supply *psy = (struct power_supply *)data;
  54. struct power_supply *epsy = dev_get_drvdata(dev);
  55. int i;
  56. for (i = 0; i < epsy->num_supplicants; i++) {
  57. if (!strcmp(epsy->supplied_to[i], psy->name)) {
  58. if (epsy->get_property(epsy,
  59. POWER_SUPPLY_PROP_ONLINE, &ret))
  60. continue;
  61. if (ret.intval)
  62. return ret.intval;
  63. }
  64. }
  65. return 0;
  66. }
  67. int power_supply_am_i_supplied(struct power_supply *psy)
  68. {
  69. int error;
  70. error = class_for_each_device(power_supply_class, NULL, psy,
  71. __power_supply_am_i_supplied);
  72. dev_dbg(psy->dev, "%s %d\n", __func__, error);
  73. return error;
  74. }
  75. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  76. static int __power_supply_is_system_supplied(struct device *dev, void *data)
  77. {
  78. union power_supply_propval ret = {0,};
  79. struct power_supply *psy = dev_get_drvdata(dev);
  80. if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
  81. if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
  82. return 0;
  83. if (ret.intval)
  84. return ret.intval;
  85. }
  86. return 0;
  87. }
  88. int power_supply_is_system_supplied(void)
  89. {
  90. int error;
  91. error = class_for_each_device(power_supply_class, NULL, NULL,
  92. __power_supply_is_system_supplied);
  93. return error;
  94. }
  95. EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
  96. int power_supply_set_battery_charged(struct power_supply *psy)
  97. {
  98. if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
  99. psy->set_charged(psy);
  100. return 0;
  101. }
  102. return -EINVAL;
  103. }
  104. EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
  105. static int power_supply_match_device_by_name(struct device *dev, void *data)
  106. {
  107. const char *name = data;
  108. struct power_supply *psy = dev_get_drvdata(dev);
  109. return strcmp(psy->name, name) == 0;
  110. }
  111. struct power_supply *power_supply_get_by_name(char *name)
  112. {
  113. struct device *dev = class_find_device(power_supply_class, NULL, name,
  114. power_supply_match_device_by_name);
  115. return dev ? dev_get_drvdata(dev) : NULL;
  116. }
  117. EXPORT_SYMBOL_GPL(power_supply_get_by_name);
  118. int power_supply_register(struct device *parent, struct power_supply *psy)
  119. {
  120. int rc = 0;
  121. psy->dev = device_create(power_supply_class, parent, 0, psy,
  122. "%s", psy->name);
  123. if (IS_ERR(psy->dev)) {
  124. rc = PTR_ERR(psy->dev);
  125. goto dev_create_failed;
  126. }
  127. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  128. rc = power_supply_create_attrs(psy);
  129. if (rc)
  130. goto create_attrs_failed;
  131. rc = power_supply_create_triggers(psy);
  132. if (rc)
  133. goto create_triggers_failed;
  134. power_supply_changed(psy);
  135. goto success;
  136. create_triggers_failed:
  137. power_supply_remove_attrs(psy);
  138. create_attrs_failed:
  139. device_unregister(psy->dev);
  140. dev_create_failed:
  141. success:
  142. return rc;
  143. }
  144. EXPORT_SYMBOL_GPL(power_supply_register);
  145. void power_supply_unregister(struct power_supply *psy)
  146. {
  147. flush_scheduled_work();
  148. power_supply_remove_triggers(psy);
  149. power_supply_remove_attrs(psy);
  150. device_unregister(psy->dev);
  151. }
  152. EXPORT_SYMBOL_GPL(power_supply_unregister);
  153. static int __init power_supply_class_init(void)
  154. {
  155. power_supply_class = class_create(THIS_MODULE, "power_supply");
  156. if (IS_ERR(power_supply_class))
  157. return PTR_ERR(power_supply_class);
  158. power_supply_class->dev_uevent = power_supply_uevent;
  159. return 0;
  160. }
  161. static void __exit power_supply_class_exit(void)
  162. {
  163. class_destroy(power_supply_class);
  164. }
  165. subsys_initcall(power_supply_class_init);
  166. module_exit(power_supply_class_exit);
  167. MODULE_DESCRIPTION("Universal power supply monitor class");
  168. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  169. "Szabolcs Gyurko, "
  170. "Anton Vorontsov <cbou@mail.ru>");
  171. MODULE_LICENSE("GPL");