power_supply_core.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. int power_supply_powers(struct power_supply *psy, struct device *dev)
  121. {
  122. return sysfs_create_link_nowarn(&psy->dev->kobj, &dev->kobj, "powers");
  123. }
  124. EXPORT_SYMBOL_GPL(power_supply_powers);
  125. static void power_supply_dev_release(struct device *dev)
  126. {
  127. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  128. kfree(dev);
  129. }
  130. int power_supply_register(struct device *parent, struct power_supply *psy)
  131. {
  132. struct device *dev;
  133. int rc;
  134. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  135. if (!dev)
  136. return -ENOMEM;
  137. device_initialize(dev);
  138. dev->class = power_supply_class;
  139. dev->type = &power_supply_dev_type;
  140. dev->parent = parent;
  141. dev->release = power_supply_dev_release;
  142. dev_set_drvdata(dev, psy);
  143. psy->dev = dev;
  144. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  145. rc = kobject_set_name(&dev->kobj, "%s", psy->name);
  146. if (rc)
  147. goto kobject_set_name_failed;
  148. rc = device_add(dev);
  149. if (rc)
  150. goto device_add_failed;
  151. rc = power_supply_create_triggers(psy);
  152. if (rc)
  153. goto create_triggers_failed;
  154. power_supply_changed(psy);
  155. goto success;
  156. create_triggers_failed:
  157. device_del(dev);
  158. kobject_set_name_failed:
  159. device_add_failed:
  160. put_device(dev);
  161. success:
  162. return rc;
  163. }
  164. EXPORT_SYMBOL_GPL(power_supply_register);
  165. void power_supply_unregister(struct power_supply *psy)
  166. {
  167. cancel_work_sync(&psy->changed_work);
  168. sysfs_remove_link(&psy->dev->kobj, "powers");
  169. power_supply_remove_triggers(psy);
  170. device_unregister(psy->dev);
  171. }
  172. EXPORT_SYMBOL_GPL(power_supply_unregister);
  173. static int __init power_supply_class_init(void)
  174. {
  175. power_supply_class = class_create(THIS_MODULE, "power_supply");
  176. if (IS_ERR(power_supply_class))
  177. return PTR_ERR(power_supply_class);
  178. power_supply_class->dev_uevent = power_supply_uevent;
  179. power_supply_init_attrs(&power_supply_dev_type);
  180. return 0;
  181. }
  182. static void __exit power_supply_class_exit(void)
  183. {
  184. class_destroy(power_supply_class);
  185. }
  186. subsys_initcall(power_supply_class_init);
  187. module_exit(power_supply_class_exit);
  188. MODULE_DESCRIPTION("Universal power supply monitor class");
  189. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  190. "Szabolcs Gyurko, "
  191. "Anton Vorontsov <cbou@mail.ru>");
  192. MODULE_LICENSE("GPL");