power_supply_core.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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, const 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(const 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, NULL, 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. /* thermal cooling device callbacks */
  180. static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
  181. unsigned long *state)
  182. {
  183. struct power_supply *psy;
  184. union power_supply_propval val;
  185. int ret;
  186. psy = tcd->devdata;
  187. ret = psy->get_property(psy,
  188. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
  189. if (!ret)
  190. *state = val.intval;
  191. return ret;
  192. }
  193. static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
  194. unsigned long *state)
  195. {
  196. struct power_supply *psy;
  197. union power_supply_propval val;
  198. int ret;
  199. psy = tcd->devdata;
  200. ret = psy->get_property(psy,
  201. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  202. if (!ret)
  203. *state = val.intval;
  204. return ret;
  205. }
  206. static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
  207. unsigned long state)
  208. {
  209. struct power_supply *psy;
  210. union power_supply_propval val;
  211. int ret;
  212. psy = tcd->devdata;
  213. val.intval = state;
  214. ret = psy->set_property(psy,
  215. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  216. return ret;
  217. }
  218. static struct thermal_cooling_device_ops psy_tcd_ops = {
  219. .get_max_state = ps_get_max_charge_cntl_limit,
  220. .get_cur_state = ps_get_cur_chrage_cntl_limit,
  221. .set_cur_state = ps_set_cur_charge_cntl_limit,
  222. };
  223. static int psy_register_cooler(struct power_supply *psy)
  224. {
  225. int i;
  226. /* Register for cooling device if psy can control charging */
  227. for (i = 0; i < psy->num_properties; i++) {
  228. if (psy->properties[i] ==
  229. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
  230. psy->tcd = thermal_cooling_device_register(
  231. (char *)psy->name,
  232. psy, &psy_tcd_ops);
  233. if (IS_ERR(psy->tcd))
  234. return PTR_ERR(psy->tcd);
  235. break;
  236. }
  237. }
  238. return 0;
  239. }
  240. static void psy_unregister_cooler(struct power_supply *psy)
  241. {
  242. if (IS_ERR_OR_NULL(psy->tcd))
  243. return;
  244. thermal_cooling_device_unregister(psy->tcd);
  245. }
  246. #else
  247. static int psy_register_thermal(struct power_supply *psy)
  248. {
  249. return 0;
  250. }
  251. static void psy_unregister_thermal(struct power_supply *psy)
  252. {
  253. }
  254. static int psy_register_cooler(struct power_supply *psy)
  255. {
  256. return 0;
  257. }
  258. static void psy_unregister_cooler(struct power_supply *psy)
  259. {
  260. }
  261. #endif
  262. int power_supply_register(struct device *parent, struct power_supply *psy)
  263. {
  264. struct device *dev;
  265. int rc;
  266. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  267. if (!dev)
  268. return -ENOMEM;
  269. device_initialize(dev);
  270. dev->class = power_supply_class;
  271. dev->type = &power_supply_dev_type;
  272. dev->parent = parent;
  273. dev->release = power_supply_dev_release;
  274. dev_set_drvdata(dev, psy);
  275. psy->dev = dev;
  276. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  277. rc = kobject_set_name(&dev->kobj, "%s", psy->name);
  278. if (rc)
  279. goto kobject_set_name_failed;
  280. rc = device_add(dev);
  281. if (rc)
  282. goto device_add_failed;
  283. rc = psy_register_thermal(psy);
  284. if (rc)
  285. goto register_thermal_failed;
  286. rc = psy_register_cooler(psy);
  287. if (rc)
  288. goto register_cooler_failed;
  289. rc = power_supply_create_triggers(psy);
  290. if (rc)
  291. goto create_triggers_failed;
  292. power_supply_changed(psy);
  293. goto success;
  294. create_triggers_failed:
  295. psy_unregister_cooler(psy);
  296. register_cooler_failed:
  297. psy_unregister_thermal(psy);
  298. register_thermal_failed:
  299. device_del(dev);
  300. kobject_set_name_failed:
  301. device_add_failed:
  302. put_device(dev);
  303. success:
  304. return rc;
  305. }
  306. EXPORT_SYMBOL_GPL(power_supply_register);
  307. void power_supply_unregister(struct power_supply *psy)
  308. {
  309. cancel_work_sync(&psy->changed_work);
  310. sysfs_remove_link(&psy->dev->kobj, "powers");
  311. power_supply_remove_triggers(psy);
  312. psy_unregister_cooler(psy);
  313. psy_unregister_thermal(psy);
  314. device_unregister(psy->dev);
  315. }
  316. EXPORT_SYMBOL_GPL(power_supply_unregister);
  317. static int __init power_supply_class_init(void)
  318. {
  319. power_supply_class = class_create(THIS_MODULE, "power_supply");
  320. if (IS_ERR(power_supply_class))
  321. return PTR_ERR(power_supply_class);
  322. power_supply_class->dev_uevent = power_supply_uevent;
  323. power_supply_init_attrs(&power_supply_dev_type);
  324. return 0;
  325. }
  326. static void __exit power_supply_class_exit(void)
  327. {
  328. class_destroy(power_supply_class);
  329. }
  330. subsys_initcall(power_supply_class_init);
  331. module_exit(power_supply_class_exit);
  332. MODULE_DESCRIPTION("Universal power supply monitor class");
  333. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  334. "Szabolcs Gyurko, "
  335. "Anton Vorontsov <cbou@mail.ru>");
  336. MODULE_LICENSE("GPL");