power_supply_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 bool __power_supply_is_supplied_by(struct power_supply *supplier,
  26. struct power_supply *supply)
  27. {
  28. int i;
  29. if (!supply->supplied_from && !supplier->supplied_to)
  30. return false;
  31. /* Support both supplied_to and supplied_from modes */
  32. if (supply->supplied_from) {
  33. if (!supplier->name)
  34. return false;
  35. for (i = 0; i < supply->num_supplies; i++)
  36. if (!strcmp(supplier->name, supply->supplied_from[i]))
  37. return true;
  38. } else {
  39. if (!supply->name)
  40. return false;
  41. for (i = 0; i < supplier->num_supplicants; i++)
  42. if (!strcmp(supplier->supplied_to[i], supply->name))
  43. return true;
  44. }
  45. return false;
  46. }
  47. static int __power_supply_changed_work(struct device *dev, void *data)
  48. {
  49. struct power_supply *psy = (struct power_supply *)data;
  50. struct power_supply *pst = dev_get_drvdata(dev);
  51. if (__power_supply_is_supplied_by(psy, pst)) {
  52. if (pst->external_power_changed)
  53. pst->external_power_changed(pst);
  54. }
  55. return 0;
  56. }
  57. static void power_supply_changed_work(struct work_struct *work)
  58. {
  59. unsigned long flags;
  60. struct power_supply *psy = container_of(work, struct power_supply,
  61. changed_work);
  62. dev_dbg(psy->dev, "%s\n", __func__);
  63. spin_lock_irqsave(&psy->changed_lock, flags);
  64. if (psy->changed) {
  65. psy->changed = false;
  66. spin_unlock_irqrestore(&psy->changed_lock, flags);
  67. class_for_each_device(power_supply_class, NULL, psy,
  68. __power_supply_changed_work);
  69. power_supply_update_leds(psy);
  70. kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
  71. spin_lock_irqsave(&psy->changed_lock, flags);
  72. }
  73. /*
  74. * Dependent power supplies (e.g. battery) may have changed state
  75. * as a result of this event, so poll again and hold the
  76. * wakeup_source until all events are processed.
  77. */
  78. if (!psy->changed)
  79. pm_relax(psy->dev);
  80. spin_unlock_irqrestore(&psy->changed_lock, flags);
  81. }
  82. void power_supply_changed(struct power_supply *psy)
  83. {
  84. unsigned long flags;
  85. dev_dbg(psy->dev, "%s\n", __func__);
  86. spin_lock_irqsave(&psy->changed_lock, flags);
  87. psy->changed = true;
  88. pm_stay_awake(psy->dev);
  89. spin_unlock_irqrestore(&psy->changed_lock, flags);
  90. schedule_work(&psy->changed_work);
  91. }
  92. EXPORT_SYMBOL_GPL(power_supply_changed);
  93. #ifdef CONFIG_OF
  94. #include <linux/of.h>
  95. static int __power_supply_populate_supplied_from(struct device *dev,
  96. void *data)
  97. {
  98. struct power_supply *psy = (struct power_supply *)data;
  99. struct power_supply *epsy = dev_get_drvdata(dev);
  100. struct device_node *np;
  101. int i = 0;
  102. do {
  103. np = of_parse_phandle(psy->of_node, "power-supplies", i++);
  104. if (!np)
  105. continue;
  106. if (np == epsy->of_node) {
  107. dev_info(psy->dev, "%s: Found supply : %s\n",
  108. psy->name, epsy->name);
  109. psy->supplied_from[i-1] = (char *)epsy->name;
  110. psy->num_supplies++;
  111. of_node_put(np);
  112. break;
  113. }
  114. of_node_put(np);
  115. } while (np);
  116. return 0;
  117. }
  118. static int power_supply_populate_supplied_from(struct power_supply *psy)
  119. {
  120. int error;
  121. error = class_for_each_device(power_supply_class, NULL, psy,
  122. __power_supply_populate_supplied_from);
  123. dev_dbg(psy->dev, "%s %d\n", __func__, error);
  124. return error;
  125. }
  126. static int __power_supply_find_supply_from_node(struct device *dev,
  127. void *data)
  128. {
  129. struct device_node *np = (struct device_node *)data;
  130. struct power_supply *epsy = dev_get_drvdata(dev);
  131. /* return error breaks out of class_for_each_device loop */
  132. if (epsy->of_node == np)
  133. return -EINVAL;
  134. return 0;
  135. }
  136. static int power_supply_find_supply_from_node(struct device_node *supply_node)
  137. {
  138. int error;
  139. struct device *dev;
  140. struct class_dev_iter iter;
  141. /*
  142. * Use iterator to see if any other device is registered.
  143. * This is required since class_for_each_device returns 0
  144. * if there are no devices registered.
  145. */
  146. class_dev_iter_init(&iter, power_supply_class, NULL, NULL);
  147. dev = class_dev_iter_next(&iter);
  148. if (!dev)
  149. return -EPROBE_DEFER;
  150. /*
  151. * We have to treat the return value as inverted, because if
  152. * we return error on not found, then it won't continue looking.
  153. * So we trick it by returning error on success to stop looking
  154. * once the matching device is found.
  155. */
  156. error = class_for_each_device(power_supply_class, NULL, supply_node,
  157. __power_supply_find_supply_from_node);
  158. return error ? 0 : -EPROBE_DEFER;
  159. }
  160. static int power_supply_check_supplies(struct power_supply *psy)
  161. {
  162. struct device_node *np;
  163. int cnt = 0;
  164. /* If there is already a list honor it */
  165. if (psy->supplied_from && psy->num_supplies > 0)
  166. return 0;
  167. /* No device node found, nothing to do */
  168. if (!psy->of_node)
  169. return 0;
  170. do {
  171. int ret;
  172. np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
  173. if (!np)
  174. continue;
  175. ret = power_supply_find_supply_from_node(np);
  176. if (ret) {
  177. dev_dbg(psy->dev, "Failed to find supply, defer!\n");
  178. of_node_put(np);
  179. return -EPROBE_DEFER;
  180. }
  181. of_node_put(np);
  182. } while (np);
  183. /* All supplies found, allocate char ** array for filling */
  184. psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from),
  185. GFP_KERNEL);
  186. if (!psy->supplied_from) {
  187. dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
  188. return -ENOMEM;
  189. }
  190. *psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * cnt,
  191. GFP_KERNEL);
  192. if (!*psy->supplied_from) {
  193. dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
  194. return -ENOMEM;
  195. }
  196. return power_supply_populate_supplied_from(psy);
  197. }
  198. #else
  199. static inline int power_supply_check_supplies(struct power_supply *psy)
  200. {
  201. return 0;
  202. }
  203. #endif
  204. static int __power_supply_am_i_supplied(struct device *dev, void *data)
  205. {
  206. union power_supply_propval ret = {0,};
  207. struct power_supply *psy = (struct power_supply *)data;
  208. struct power_supply *epsy = dev_get_drvdata(dev);
  209. if (__power_supply_is_supplied_by(epsy, psy))
  210. if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) {
  211. if (ret.intval)
  212. return ret.intval;
  213. }
  214. return 0;
  215. }
  216. int power_supply_am_i_supplied(struct power_supply *psy)
  217. {
  218. int error;
  219. error = class_for_each_device(power_supply_class, NULL, psy,
  220. __power_supply_am_i_supplied);
  221. dev_dbg(psy->dev, "%s %d\n", __func__, error);
  222. return error;
  223. }
  224. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  225. static int __power_supply_is_system_supplied(struct device *dev, void *data)
  226. {
  227. union power_supply_propval ret = {0,};
  228. struct power_supply *psy = dev_get_drvdata(dev);
  229. unsigned int *count = data;
  230. (*count)++;
  231. if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
  232. if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
  233. return 0;
  234. if (ret.intval)
  235. return ret.intval;
  236. }
  237. return 0;
  238. }
  239. int power_supply_is_system_supplied(void)
  240. {
  241. int error;
  242. unsigned int count = 0;
  243. error = class_for_each_device(power_supply_class, NULL, &count,
  244. __power_supply_is_system_supplied);
  245. /*
  246. * If no power class device was found at all, most probably we are
  247. * running on a desktop system, so assume we are on mains power.
  248. */
  249. if (count == 0)
  250. return 1;
  251. return error;
  252. }
  253. EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
  254. int power_supply_set_battery_charged(struct power_supply *psy)
  255. {
  256. if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
  257. psy->set_charged(psy);
  258. return 0;
  259. }
  260. return -EINVAL;
  261. }
  262. EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
  263. static int power_supply_match_device_by_name(struct device *dev, const void *data)
  264. {
  265. const char *name = data;
  266. struct power_supply *psy = dev_get_drvdata(dev);
  267. return strcmp(psy->name, name) == 0;
  268. }
  269. struct power_supply *power_supply_get_by_name(const char *name)
  270. {
  271. struct device *dev = class_find_device(power_supply_class, NULL, name,
  272. power_supply_match_device_by_name);
  273. return dev ? dev_get_drvdata(dev) : NULL;
  274. }
  275. EXPORT_SYMBOL_GPL(power_supply_get_by_name);
  276. int power_supply_powers(struct power_supply *psy, struct device *dev)
  277. {
  278. return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
  279. }
  280. EXPORT_SYMBOL_GPL(power_supply_powers);
  281. static void power_supply_dev_release(struct device *dev)
  282. {
  283. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  284. kfree(dev);
  285. }
  286. #ifdef CONFIG_THERMAL
  287. static int power_supply_read_temp(struct thermal_zone_device *tzd,
  288. unsigned long *temp)
  289. {
  290. struct power_supply *psy;
  291. union power_supply_propval val;
  292. int ret;
  293. WARN_ON(tzd == NULL);
  294. psy = tzd->devdata;
  295. ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
  296. /* Convert tenths of degree Celsius to milli degree Celsius. */
  297. if (!ret)
  298. *temp = val.intval * 100;
  299. return ret;
  300. }
  301. static struct thermal_zone_device_ops psy_tzd_ops = {
  302. .get_temp = power_supply_read_temp,
  303. };
  304. static int psy_register_thermal(struct power_supply *psy)
  305. {
  306. int i;
  307. /* Register battery zone device psy reports temperature */
  308. for (i = 0; i < psy->num_properties; i++) {
  309. if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
  310. psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
  311. psy, &psy_tzd_ops, NULL, 0, 0);
  312. if (IS_ERR(psy->tzd))
  313. return PTR_ERR(psy->tzd);
  314. break;
  315. }
  316. }
  317. return 0;
  318. }
  319. static void psy_unregister_thermal(struct power_supply *psy)
  320. {
  321. if (IS_ERR_OR_NULL(psy->tzd))
  322. return;
  323. thermal_zone_device_unregister(psy->tzd);
  324. }
  325. /* thermal cooling device callbacks */
  326. static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
  327. unsigned long *state)
  328. {
  329. struct power_supply *psy;
  330. union power_supply_propval val;
  331. int ret;
  332. psy = tcd->devdata;
  333. ret = psy->get_property(psy,
  334. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
  335. if (!ret)
  336. *state = val.intval;
  337. return ret;
  338. }
  339. static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
  340. unsigned long *state)
  341. {
  342. struct power_supply *psy;
  343. union power_supply_propval val;
  344. int ret;
  345. psy = tcd->devdata;
  346. ret = psy->get_property(psy,
  347. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  348. if (!ret)
  349. *state = val.intval;
  350. return ret;
  351. }
  352. static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
  353. unsigned long state)
  354. {
  355. struct power_supply *psy;
  356. union power_supply_propval val;
  357. int ret;
  358. psy = tcd->devdata;
  359. val.intval = state;
  360. ret = psy->set_property(psy,
  361. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  362. return ret;
  363. }
  364. static struct thermal_cooling_device_ops psy_tcd_ops = {
  365. .get_max_state = ps_get_max_charge_cntl_limit,
  366. .get_cur_state = ps_get_cur_chrage_cntl_limit,
  367. .set_cur_state = ps_set_cur_charge_cntl_limit,
  368. };
  369. static int psy_register_cooler(struct power_supply *psy)
  370. {
  371. int i;
  372. /* Register for cooling device if psy can control charging */
  373. for (i = 0; i < psy->num_properties; i++) {
  374. if (psy->properties[i] ==
  375. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
  376. psy->tcd = thermal_cooling_device_register(
  377. (char *)psy->name,
  378. psy, &psy_tcd_ops);
  379. if (IS_ERR(psy->tcd))
  380. return PTR_ERR(psy->tcd);
  381. break;
  382. }
  383. }
  384. return 0;
  385. }
  386. static void psy_unregister_cooler(struct power_supply *psy)
  387. {
  388. if (IS_ERR_OR_NULL(psy->tcd))
  389. return;
  390. thermal_cooling_device_unregister(psy->tcd);
  391. }
  392. #else
  393. static int psy_register_thermal(struct power_supply *psy)
  394. {
  395. return 0;
  396. }
  397. static void psy_unregister_thermal(struct power_supply *psy)
  398. {
  399. }
  400. static int psy_register_cooler(struct power_supply *psy)
  401. {
  402. return 0;
  403. }
  404. static void psy_unregister_cooler(struct power_supply *psy)
  405. {
  406. }
  407. #endif
  408. int power_supply_register(struct device *parent, struct power_supply *psy)
  409. {
  410. struct device *dev;
  411. int rc;
  412. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  413. if (!dev)
  414. return -ENOMEM;
  415. device_initialize(dev);
  416. dev->class = power_supply_class;
  417. dev->type = &power_supply_dev_type;
  418. dev->parent = parent;
  419. dev->release = power_supply_dev_release;
  420. dev_set_drvdata(dev, psy);
  421. psy->dev = dev;
  422. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  423. rc = power_supply_check_supplies(psy);
  424. if (rc) {
  425. dev_info(dev, "Not all required supplies found, defer probe\n");
  426. goto check_supplies_failed;
  427. }
  428. spin_lock_init(&psy->changed_lock);
  429. rc = device_init_wakeup(dev, true);
  430. if (rc)
  431. goto wakeup_init_failed;
  432. rc = kobject_set_name(&dev->kobj, "%s", psy->name);
  433. if (rc)
  434. goto kobject_set_name_failed;
  435. rc = device_add(dev);
  436. if (rc)
  437. goto device_add_failed;
  438. rc = psy_register_thermal(psy);
  439. if (rc)
  440. goto register_thermal_failed;
  441. rc = psy_register_cooler(psy);
  442. if (rc)
  443. goto register_cooler_failed;
  444. rc = power_supply_create_triggers(psy);
  445. if (rc)
  446. goto create_triggers_failed;
  447. power_supply_changed(psy);
  448. goto success;
  449. create_triggers_failed:
  450. psy_unregister_cooler(psy);
  451. register_cooler_failed:
  452. psy_unregister_thermal(psy);
  453. register_thermal_failed:
  454. wakeup_init_failed:
  455. device_del(dev);
  456. kobject_set_name_failed:
  457. device_add_failed:
  458. check_supplies_failed:
  459. put_device(dev);
  460. success:
  461. return rc;
  462. }
  463. EXPORT_SYMBOL_GPL(power_supply_register);
  464. void power_supply_unregister(struct power_supply *psy)
  465. {
  466. cancel_work_sync(&psy->changed_work);
  467. sysfs_remove_link(&psy->dev->kobj, "powers");
  468. power_supply_remove_triggers(psy);
  469. psy_unregister_cooler(psy);
  470. psy_unregister_thermal(psy);
  471. device_init_wakeup(psy->dev, false);
  472. device_unregister(psy->dev);
  473. }
  474. EXPORT_SYMBOL_GPL(power_supply_unregister);
  475. static int __init power_supply_class_init(void)
  476. {
  477. power_supply_class = class_create(THIS_MODULE, "power_supply");
  478. if (IS_ERR(power_supply_class))
  479. return PTR_ERR(power_supply_class);
  480. power_supply_class->dev_uevent = power_supply_uevent;
  481. power_supply_init_attrs(&power_supply_dev_type);
  482. return 0;
  483. }
  484. static void __exit power_supply_class_exit(void)
  485. {
  486. class_destroy(power_supply_class);
  487. }
  488. subsys_initcall(power_supply_class_init);
  489. module_exit(power_supply_class_exit);
  490. MODULE_DESCRIPTION("Universal power supply monitor class");
  491. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  492. "Szabolcs Gyurko, "
  493. "Anton Vorontsov <cbou@mail.ru>");
  494. MODULE_LICENSE("GPL");