power_supply_core.c 5.4 KB

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