power_supply_core.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <linux/thermal.h>
  20. #include "power_supply.h"
  21. /* exported for the APM Power driver, APM emulation */
  22. struct class *power_supply_class;
  23. EXPORT_SYMBOL_GPL(power_supply_class);
  24. static struct device_type power_supply_dev_type;
  25. static int __power_supply_changed_work(struct device *dev, void *data)
  26. {
  27. struct power_supply *psy = (struct power_supply *)data;
  28. struct power_supply *pst = dev_get_drvdata(dev);
  29. int i;
  30. for (i = 0; i < psy->num_supplicants; i++)
  31. if (!strcmp(psy->supplied_to[i], pst->name)) {
  32. if (pst->external_power_changed)
  33. pst->external_power_changed(pst);
  34. }
  35. return 0;
  36. }
  37. static void power_supply_changed_work(struct work_struct *work)
  38. {
  39. struct power_supply *psy = container_of(work, struct power_supply,
  40. changed_work);
  41. dev_dbg(psy->dev, "%s\n", __func__);
  42. class_for_each_device(power_supply_class, NULL, psy,
  43. __power_supply_changed_work);
  44. power_supply_update_leds(psy);
  45. kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
  46. }
  47. void power_supply_changed(struct power_supply *psy)
  48. {
  49. dev_dbg(psy->dev, "%s\n", __func__);
  50. schedule_work(&psy->changed_work);
  51. }
  52. EXPORT_SYMBOL_GPL(power_supply_changed);
  53. static int __power_supply_am_i_supplied(struct device *dev, void *data)
  54. {
  55. union power_supply_propval ret = {0,};
  56. struct power_supply *psy = (struct power_supply *)data;
  57. struct power_supply *epsy = dev_get_drvdata(dev);
  58. int i;
  59. for (i = 0; i < epsy->num_supplicants; i++) {
  60. if (!strcmp(epsy->supplied_to[i], psy->name)) {
  61. if (epsy->get_property(epsy,
  62. POWER_SUPPLY_PROP_ONLINE, &ret))
  63. continue;
  64. if (ret.intval)
  65. return ret.intval;
  66. }
  67. }
  68. return 0;
  69. }
  70. int power_supply_am_i_supplied(struct power_supply *psy)
  71. {
  72. int error;
  73. error = class_for_each_device(power_supply_class, NULL, psy,
  74. __power_supply_am_i_supplied);
  75. dev_dbg(psy->dev, "%s %d\n", __func__, error);
  76. return error;
  77. }
  78. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  79. static int __power_supply_is_system_supplied(struct device *dev, void *data)
  80. {
  81. union power_supply_propval ret = {0,};
  82. struct power_supply *psy = dev_get_drvdata(dev);
  83. unsigned int *count = data;
  84. (*count)++;
  85. if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
  86. if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
  87. return 0;
  88. if (ret.intval)
  89. return ret.intval;
  90. }
  91. return 0;
  92. }
  93. int power_supply_is_system_supplied(void)
  94. {
  95. int error;
  96. unsigned int count = 0;
  97. error = class_for_each_device(power_supply_class, NULL, &count,
  98. __power_supply_is_system_supplied);
  99. /*
  100. * If no power class device was found at all, most probably we are
  101. * running on a desktop system, so assume we are on mains power.
  102. */
  103. if (count == 0)
  104. return 1;
  105. return error;
  106. }
  107. EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
  108. int power_supply_set_battery_charged(struct power_supply *psy)
  109. {
  110. if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
  111. psy->set_charged(psy);
  112. return 0;
  113. }
  114. return -EINVAL;
  115. }
  116. EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
  117. static int power_supply_match_device_by_name(struct device *dev, void *data)
  118. {
  119. const char *name = data;
  120. struct power_supply *psy = dev_get_drvdata(dev);
  121. return strcmp(psy->name, name) == 0;
  122. }
  123. struct power_supply *power_supply_get_by_name(char *name)
  124. {
  125. struct device *dev = class_find_device(power_supply_class, NULL, name,
  126. power_supply_match_device_by_name);
  127. return dev ? dev_get_drvdata(dev) : NULL;
  128. }
  129. EXPORT_SYMBOL_GPL(power_supply_get_by_name);
  130. int power_supply_powers(struct power_supply *psy, struct device *dev)
  131. {
  132. return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
  133. }
  134. EXPORT_SYMBOL_GPL(power_supply_powers);
  135. static void power_supply_dev_release(struct device *dev)
  136. {
  137. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  138. kfree(dev);
  139. }
  140. #ifdef CONFIG_THERMAL
  141. static int power_supply_read_temp(struct thermal_zone_device *tzd,
  142. unsigned long *temp)
  143. {
  144. struct power_supply *psy;
  145. union power_supply_propval val;
  146. int ret;
  147. WARN_ON(tzd == NULL);
  148. psy = tzd->devdata;
  149. ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
  150. /* Convert tenths of degree Celsius to milli degree Celsius. */
  151. if (!ret)
  152. *temp = val.intval * 100;
  153. return ret;
  154. }
  155. static struct thermal_zone_device_ops psy_tzd_ops = {
  156. .get_temp = power_supply_read_temp,
  157. };
  158. static int psy_register_thermal(struct power_supply *psy)
  159. {
  160. int i;
  161. /* Register battery zone device psy reports temperature */
  162. for (i = 0; i < psy->num_properties; i++) {
  163. if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
  164. psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
  165. psy, &psy_tzd_ops, 0, 0, 0, 0);
  166. if (IS_ERR(psy->tzd))
  167. return PTR_ERR(psy->tzd);
  168. break;
  169. }
  170. }
  171. return 0;
  172. }
  173. static void psy_unregister_thermal(struct power_supply *psy)
  174. {
  175. if (IS_ERR_OR_NULL(psy->tzd))
  176. return;
  177. thermal_zone_device_unregister(psy->tzd);
  178. }
  179. #else
  180. static int psy_register_thermal(struct power_supply *psy)
  181. {
  182. return 0;
  183. }
  184. static void psy_unregister_thermal(struct power_supply *psy)
  185. {
  186. }
  187. #endif
  188. int power_supply_register(struct device *parent, struct power_supply *psy)
  189. {
  190. struct device *dev;
  191. int rc;
  192. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  193. if (!dev)
  194. return -ENOMEM;
  195. device_initialize(dev);
  196. dev->class = power_supply_class;
  197. dev->type = &power_supply_dev_type;
  198. dev->parent = parent;
  199. dev->release = power_supply_dev_release;
  200. dev_set_drvdata(dev, psy);
  201. psy->dev = dev;
  202. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  203. rc = kobject_set_name(&dev->kobj, "%s", psy->name);
  204. if (rc)
  205. goto kobject_set_name_failed;
  206. rc = device_add(dev);
  207. if (rc)
  208. goto device_add_failed;
  209. rc = psy_register_thermal(psy);
  210. if (rc)
  211. goto register_thermal_failed;
  212. rc = power_supply_create_triggers(psy);
  213. if (rc)
  214. goto create_triggers_failed;
  215. power_supply_changed(psy);
  216. goto success;
  217. create_triggers_failed:
  218. psy_unregister_thermal(psy);
  219. register_thermal_failed:
  220. device_del(dev);
  221. kobject_set_name_failed:
  222. device_add_failed:
  223. put_device(dev);
  224. success:
  225. return rc;
  226. }
  227. EXPORT_SYMBOL_GPL(power_supply_register);
  228. void power_supply_unregister(struct power_supply *psy)
  229. {
  230. cancel_work_sync(&psy->changed_work);
  231. sysfs_remove_link(&psy->dev->kobj, "powers");
  232. power_supply_remove_triggers(psy);
  233. psy_unregister_thermal(psy);
  234. device_unregister(psy->dev);
  235. }
  236. EXPORT_SYMBOL_GPL(power_supply_unregister);
  237. static int __init power_supply_class_init(void)
  238. {
  239. power_supply_class = class_create(THIS_MODULE, "power_supply");
  240. if (IS_ERR(power_supply_class))
  241. return PTR_ERR(power_supply_class);
  242. power_supply_class->dev_uevent = power_supply_uevent;
  243. power_supply_init_attrs(&power_supply_dev_type);
  244. return 0;
  245. }
  246. static void __exit power_supply_class_exit(void)
  247. {
  248. class_destroy(power_supply_class);
  249. }
  250. subsys_initcall(power_supply_class_init);
  251. module_exit(power_supply_class_exit);
  252. MODULE_DESCRIPTION("Universal power supply monitor class");
  253. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  254. "Szabolcs Gyurko, "
  255. "Anton Vorontsov <cbou@mail.ru>");
  256. MODULE_LICENSE("GPL");