power_supply_core.c 16 KB

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