power_supply_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /**
  25. * power_supply_set_voltage_limit - set current limit
  26. * @psy: the power supply to control
  27. * @limit: current limit in uV from the power supply.
  28. * 0 will disable the power supply.
  29. *
  30. * This function will set a maximum supply current from a source
  31. * and it will disable the charger when limit is 0.
  32. */
  33. int power_supply_set_voltage_limit(struct power_supply *psy, int limit)
  34. {
  35. const union power_supply_propval ret = {limit,};
  36. if (psy->set_property)
  37. return psy->set_property(psy, POWER_SUPPLY_PROP_VOLTAGE_MAX,
  38. &ret);
  39. return -ENXIO;
  40. }
  41. EXPORT_SYMBOL(power_supply_set_voltage_limit);
  42. /**
  43. * power_supply_set_current_limit - set current limit
  44. * @psy: the power supply to control
  45. * @limit: current limit in uA from the power supply.
  46. * 0 will disable the power supply.
  47. *
  48. * This function will set a maximum supply current from a source
  49. * and it will disable the charger when limit is 0.
  50. */
  51. int power_supply_set_current_limit(struct power_supply *psy, int limit)
  52. {
  53. const union power_supply_propval ret = {limit,};
  54. if (psy->set_property)
  55. return psy->set_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX,
  56. &ret);
  57. return -ENXIO;
  58. }
  59. EXPORT_SYMBOL_GPL(power_supply_set_current_limit);
  60. /**
  61. * power_supply_set_charging_enabled - enable or disable charging
  62. * @psy: the power supply to control
  63. * @enable: sets enable property of power supply
  64. */
  65. int power_supply_set_charging_enabled(struct power_supply *psy, bool enable)
  66. {
  67. const union power_supply_propval ret = {enable,};
  68. if (psy->set_property)
  69. return psy->set_property(psy,
  70. POWER_SUPPLY_PROP_CHARGING_ENABLED,
  71. &ret);
  72. return -ENXIO;
  73. }
  74. EXPORT_SYMBOL_GPL(power_supply_set_charging_enabled);
  75. /**
  76. * power_supply_set_present - set present state of the power supply
  77. * @psy: the power supply to control
  78. * @enable: sets present property of power supply
  79. */
  80. int power_supply_set_present(struct power_supply *psy, bool enable)
  81. {
  82. const union power_supply_propval ret = {enable,};
  83. if (psy->set_property)
  84. return psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT,
  85. &ret);
  86. return -ENXIO;
  87. }
  88. EXPORT_SYMBOL_GPL(power_supply_set_present);
  89. /**
  90. * power_supply_set_online - set online state of the power supply
  91. * @psy: the power supply to control
  92. * @enable: sets online property of power supply
  93. */
  94. int power_supply_set_online(struct power_supply *psy, bool enable)
  95. {
  96. const union power_supply_propval ret = {enable,};
  97. if (psy->set_property)
  98. return psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE,
  99. &ret);
  100. return -ENXIO;
  101. }
  102. EXPORT_SYMBOL_GPL(power_supply_set_online);
  103. /** power_supply_set_health_state - set health state of the power supply
  104. * @psy: the power supply to control
  105. * @health: sets health property of power supply
  106. */
  107. int power_supply_set_health_state(struct power_supply *psy, int health)
  108. {
  109. const union power_supply_propval ret = {health,};
  110. if (psy->set_property)
  111. return psy->set_property(psy, POWER_SUPPLY_PROP_HEALTH,
  112. &ret);
  113. return -ENXIO;
  114. }
  115. EXPORT_SYMBOL(power_supply_set_health_state);
  116. /**
  117. * power_supply_set_scope - set scope of the power supply
  118. * @psy: the power supply to control
  119. * @scope: value to set the scope property to, should be from
  120. * the SCOPE enum in power_supply.h
  121. */
  122. int power_supply_set_scope(struct power_supply *psy, int scope)
  123. {
  124. const union power_supply_propval ret = {scope, };
  125. if (psy->set_property)
  126. return psy->set_property(psy, POWER_SUPPLY_PROP_SCOPE,
  127. &ret);
  128. return -ENXIO;
  129. }
  130. EXPORT_SYMBOL_GPL(power_supply_set_scope);
  131. /**
  132. * power_supply_set_supply_type - set type of the power supply
  133. * @psy: the power supply to control
  134. * @supply_type: sets type property of power supply
  135. */
  136. int power_supply_set_supply_type(struct power_supply *psy,
  137. enum power_supply_type supply_type)
  138. {
  139. const union power_supply_propval ret = {supply_type,};
  140. if (psy->set_property)
  141. return psy->set_property(psy, POWER_SUPPLY_PROP_TYPE,
  142. &ret);
  143. return -ENXIO;
  144. }
  145. EXPORT_SYMBOL_GPL(power_supply_set_supply_type);
  146. /**
  147. * power_supply_set_charge_type - set charge type of the power supply
  148. * @psy: the power supply to control
  149. * @enable: sets charge type property of power supply
  150. */
  151. int power_supply_set_charge_type(struct power_supply *psy, int charge_type)
  152. {
  153. const union power_supply_propval ret = {charge_type,};
  154. if (psy->set_property)
  155. return psy->set_property(psy, POWER_SUPPLY_PROP_CHARGE_TYPE,
  156. &ret);
  157. return -ENXIO;
  158. }
  159. EXPORT_SYMBOL_GPL(power_supply_set_charge_type);
  160. static int __power_supply_changed_work(struct device *dev, void *data)
  161. {
  162. struct power_supply *psy = (struct power_supply *)data;
  163. struct power_supply *pst = dev_get_drvdata(dev);
  164. int i;
  165. for (i = 0; i < psy->num_supplicants; i++)
  166. if (!strcmp(psy->supplied_to[i], pst->name)) {
  167. if (pst->external_power_changed)
  168. pst->external_power_changed(pst);
  169. }
  170. return 0;
  171. }
  172. static void power_supply_changed_work(struct work_struct *work)
  173. {
  174. unsigned long flags;
  175. struct power_supply *psy = container_of(work, struct power_supply,
  176. changed_work);
  177. dev_dbg(psy->dev, "%s\n", __func__);
  178. spin_lock_irqsave(&psy->changed_lock, flags);
  179. if (psy->changed) {
  180. psy->changed = false;
  181. spin_unlock_irqrestore(&psy->changed_lock, flags);
  182. class_for_each_device(power_supply_class, NULL, psy,
  183. __power_supply_changed_work);
  184. power_supply_update_leds(psy);
  185. kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
  186. spin_lock_irqsave(&psy->changed_lock, flags);
  187. }
  188. if (!psy->changed)
  189. pm_relax(psy->dev);
  190. spin_unlock_irqrestore(&psy->changed_lock, flags);
  191. }
  192. void power_supply_changed(struct power_supply *psy)
  193. {
  194. unsigned long flags;
  195. dev_dbg(psy->dev, "%s\n", __func__);
  196. spin_lock_irqsave(&psy->changed_lock, flags);
  197. psy->changed = true;
  198. pm_stay_awake(psy->dev);
  199. spin_unlock_irqrestore(&psy->changed_lock, flags);
  200. schedule_work(&psy->changed_work);
  201. }
  202. EXPORT_SYMBOL_GPL(power_supply_changed);
  203. static int __power_supply_am_i_supplied(struct device *dev, void *data)
  204. {
  205. union power_supply_propval ret = {0,};
  206. struct power_supply *psy = (struct power_supply *)data;
  207. struct power_supply *epsy = dev_get_drvdata(dev);
  208. int i;
  209. for (i = 0; i < epsy->num_supplicants; i++) {
  210. if (!strcmp(epsy->supplied_to[i], psy->name)) {
  211. if (epsy->get_property(epsy,
  212. POWER_SUPPLY_PROP_ONLINE, &ret))
  213. continue;
  214. if (ret.intval)
  215. return ret.intval;
  216. }
  217. }
  218. return 0;
  219. }
  220. int power_supply_am_i_supplied(struct power_supply *psy)
  221. {
  222. int error;
  223. error = class_for_each_device(power_supply_class, NULL, psy,
  224. __power_supply_am_i_supplied);
  225. dev_dbg(psy->dev, "%s %d\n", __func__, error);
  226. return error;
  227. }
  228. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  229. static int __power_supply_is_system_supplied(struct device *dev, void *data)
  230. {
  231. union power_supply_propval ret = {0,};
  232. struct power_supply *psy = dev_get_drvdata(dev);
  233. unsigned int *count = data;
  234. (*count)++;
  235. if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
  236. if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
  237. return 0;
  238. if (ret.intval)
  239. return ret.intval;
  240. }
  241. return 0;
  242. }
  243. int power_supply_is_system_supplied(void)
  244. {
  245. int error;
  246. unsigned int count = 0;
  247. error = class_for_each_device(power_supply_class, NULL, &count,
  248. __power_supply_is_system_supplied);
  249. /*
  250. * If no power class device was found at all, most probably we are
  251. * running on a desktop system, so assume we are on mains power.
  252. */
  253. if (count == 0)
  254. return 1;
  255. return error;
  256. }
  257. EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
  258. int power_supply_set_battery_charged(struct power_supply *psy)
  259. {
  260. if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
  261. psy->set_charged(psy);
  262. return 0;
  263. }
  264. return -EINVAL;
  265. }
  266. EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
  267. static int power_supply_match_device_by_name(struct device *dev, void *data)
  268. {
  269. const char *name = data;
  270. struct power_supply *psy = dev_get_drvdata(dev);
  271. return strcmp(psy->name, name) == 0;
  272. }
  273. struct power_supply *power_supply_get_by_name(char *name)
  274. {
  275. struct device *dev = class_find_device(power_supply_class, NULL, name,
  276. power_supply_match_device_by_name);
  277. return dev ? dev_get_drvdata(dev) : NULL;
  278. }
  279. EXPORT_SYMBOL_GPL(power_supply_get_by_name);
  280. int power_supply_powers(struct power_supply *psy, struct device *dev)
  281. {
  282. return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
  283. }
  284. EXPORT_SYMBOL_GPL(power_supply_powers);
  285. static void power_supply_dev_release(struct device *dev)
  286. {
  287. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  288. kfree(dev);
  289. }
  290. int power_supply_register(struct device *parent, struct power_supply *psy)
  291. {
  292. struct device *dev;
  293. int rc;
  294. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  295. if (!dev)
  296. return -ENOMEM;
  297. device_initialize(dev);
  298. dev->class = power_supply_class;
  299. dev->type = &power_supply_dev_type;
  300. dev->parent = parent;
  301. dev->release = power_supply_dev_release;
  302. dev_set_drvdata(dev, psy);
  303. psy->dev = dev;
  304. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  305. rc = kobject_set_name(&dev->kobj, "%s", psy->name);
  306. if (rc)
  307. goto kobject_set_name_failed;
  308. rc = device_add(dev);
  309. if (rc)
  310. goto device_add_failed;
  311. spin_lock_init(&psy->changed_lock);
  312. rc = device_init_wakeup(dev, true);
  313. if (rc)
  314. goto wakeup_init_failed;
  315. rc = power_supply_create_triggers(psy);
  316. if (rc)
  317. goto create_triggers_failed;
  318. power_supply_changed(psy);
  319. goto success;
  320. create_triggers_failed:
  321. wakeup_init_failed:
  322. device_del(dev);
  323. kobject_set_name_failed:
  324. device_add_failed:
  325. put_device(dev);
  326. success:
  327. return rc;
  328. }
  329. EXPORT_SYMBOL_GPL(power_supply_register);
  330. void power_supply_unregister(struct power_supply *psy)
  331. {
  332. cancel_work_sync(&psy->changed_work);
  333. sysfs_remove_link(&psy->dev->kobj, "powers");
  334. power_supply_remove_triggers(psy);
  335. device_unregister(psy->dev);
  336. }
  337. EXPORT_SYMBOL_GPL(power_supply_unregister);
  338. static int __init power_supply_class_init(void)
  339. {
  340. power_supply_class = class_create(THIS_MODULE, "power_supply");
  341. if (IS_ERR(power_supply_class))
  342. return PTR_ERR(power_supply_class);
  343. power_supply_class->dev_uevent = power_supply_uevent;
  344. power_supply_init_attrs(&power_supply_dev_type);
  345. return 0;
  346. }
  347. static void __exit power_supply_class_exit(void)
  348. {
  349. class_destroy(power_supply_class);
  350. }
  351. subsys_initcall(power_supply_class_init);
  352. module_exit(power_supply_class_exit);
  353. MODULE_DESCRIPTION("Universal power supply monitor class");
  354. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  355. "Szabolcs Gyurko, "
  356. "Anton Vorontsov <cbou@mail.ru>");
  357. MODULE_LICENSE("GPL");