power_supply_core.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. unsigned int *count = data;
  83. (*count)++;
  84. if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
  85. if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
  86. return 0;
  87. if (ret.intval)
  88. return ret.intval;
  89. }
  90. return 0;
  91. }
  92. int power_supply_is_system_supplied(void)
  93. {
  94. int error;
  95. unsigned int count = 0;
  96. error = class_for_each_device(power_supply_class, NULL, &count,
  97. __power_supply_is_system_supplied);
  98. /*
  99. * If no power class device was found at all, most probably we are
  100. * running on a desktop system, so assume we are on mains power.
  101. */
  102. if (count == 0)
  103. return 1;
  104. return error;
  105. }
  106. EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
  107. int power_supply_set_battery_charged(struct power_supply *psy)
  108. {
  109. if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
  110. psy->set_charged(psy);
  111. return 0;
  112. }
  113. return -EINVAL;
  114. }
  115. EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
  116. static int power_supply_match_device_by_name(struct device *dev, void *data)
  117. {
  118. const char *name = data;
  119. struct power_supply *psy = dev_get_drvdata(dev);
  120. return strcmp(psy->name, name) == 0;
  121. }
  122. struct power_supply *power_supply_get_by_name(char *name)
  123. {
  124. struct device *dev = class_find_device(power_supply_class, NULL, name,
  125. power_supply_match_device_by_name);
  126. return dev ? dev_get_drvdata(dev) : NULL;
  127. }
  128. EXPORT_SYMBOL_GPL(power_supply_get_by_name);
  129. int power_supply_powers(struct power_supply *psy, struct device *dev)
  130. {
  131. return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
  132. }
  133. EXPORT_SYMBOL_GPL(power_supply_powers);
  134. static void power_supply_dev_release(struct device *dev)
  135. {
  136. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  137. kfree(dev);
  138. }
  139. int power_supply_register(struct device *parent, struct power_supply *psy)
  140. {
  141. struct device *dev;
  142. int rc;
  143. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  144. if (!dev)
  145. return -ENOMEM;
  146. device_initialize(dev);
  147. dev->class = power_supply_class;
  148. dev->type = &power_supply_dev_type;
  149. dev->parent = parent;
  150. dev->release = power_supply_dev_release;
  151. dev_set_drvdata(dev, psy);
  152. psy->dev = dev;
  153. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  154. rc = kobject_set_name(&dev->kobj, "%s", psy->name);
  155. if (rc)
  156. goto kobject_set_name_failed;
  157. rc = device_add(dev);
  158. if (rc)
  159. goto device_add_failed;
  160. rc = power_supply_create_triggers(psy);
  161. if (rc)
  162. goto create_triggers_failed;
  163. power_supply_changed(psy);
  164. goto success;
  165. create_triggers_failed:
  166. device_del(dev);
  167. kobject_set_name_failed:
  168. device_add_failed:
  169. put_device(dev);
  170. success:
  171. return rc;
  172. }
  173. EXPORT_SYMBOL_GPL(power_supply_register);
  174. void power_supply_unregister(struct power_supply *psy)
  175. {
  176. cancel_work_sync(&psy->changed_work);
  177. sysfs_remove_link(&psy->dev->kobj, "powers");
  178. power_supply_remove_triggers(psy);
  179. device_unregister(psy->dev);
  180. }
  181. EXPORT_SYMBOL_GPL(power_supply_unregister);
  182. static int __init power_supply_class_init(void)
  183. {
  184. power_supply_class = class_create(THIS_MODULE, "power_supply");
  185. if (IS_ERR(power_supply_class))
  186. return PTR_ERR(power_supply_class);
  187. power_supply_class->dev_uevent = power_supply_uevent;
  188. power_supply_init_attrs(&power_supply_dev_type);
  189. return 0;
  190. }
  191. static void __exit power_supply_class_exit(void)
  192. {
  193. class_destroy(power_supply_class);
  194. }
  195. subsys_initcall(power_supply_class_init);
  196. module_exit(power_supply_class_exit);
  197. MODULE_DESCRIPTION("Universal power supply monitor class");
  198. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  199. "Szabolcs Gyurko, "
  200. "Anton Vorontsov <cbou@mail.ru>");
  201. MODULE_LICENSE("GPL");