power_supply_core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 = 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. /*
  68. * Check 'changed' here to avoid issues due to race between
  69. * power_supply_changed() and this routine. In worst case
  70. * power_supply_changed() can be called again just before we take above
  71. * lock. During the first call of this routine we will mark 'changed' as
  72. * false and it will stay false for the next call as well.
  73. */
  74. if (likely(psy->changed)) {
  75. psy->changed = false;
  76. spin_unlock_irqrestore(&psy->changed_lock, flags);
  77. class_for_each_device(power_supply_class, NULL, psy,
  78. __power_supply_changed_work);
  79. power_supply_update_leds(psy);
  80. atomic_notifier_call_chain(&power_supply_notifier,
  81. PSY_EVENT_PROP_CHANGED, psy);
  82. kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
  83. spin_lock_irqsave(&psy->changed_lock, flags);
  84. }
  85. /*
  86. * Hold the wakeup_source until all events are processed.
  87. * power_supply_changed() might have called again and have set 'changed'
  88. * to true.
  89. */
  90. if (likely(!psy->changed))
  91. pm_relax(psy->dev);
  92. spin_unlock_irqrestore(&psy->changed_lock, flags);
  93. }
  94. void power_supply_changed(struct power_supply *psy)
  95. {
  96. unsigned long flags;
  97. dev_dbg(psy->dev, "%s\n", __func__);
  98. spin_lock_irqsave(&psy->changed_lock, flags);
  99. psy->changed = true;
  100. pm_stay_awake(psy->dev);
  101. spin_unlock_irqrestore(&psy->changed_lock, flags);
  102. schedule_work(&psy->changed_work);
  103. }
  104. EXPORT_SYMBOL_GPL(power_supply_changed);
  105. #ifdef CONFIG_OF
  106. #include <linux/of.h>
  107. static int __power_supply_populate_supplied_from(struct device *dev,
  108. void *data)
  109. {
  110. struct power_supply *psy = data;
  111. struct power_supply *epsy = dev_get_drvdata(dev);
  112. struct device_node *np;
  113. int i = 0;
  114. do {
  115. np = of_parse_phandle(psy->of_node, "power-supplies", i++);
  116. if (!np)
  117. break;
  118. if (np == epsy->of_node) {
  119. dev_info(psy->dev, "%s: Found supply : %s\n",
  120. psy->name, epsy->name);
  121. psy->supplied_from[i-1] = (char *)epsy->name;
  122. psy->num_supplies++;
  123. of_node_put(np);
  124. break;
  125. }
  126. of_node_put(np);
  127. } while (np);
  128. return 0;
  129. }
  130. static int power_supply_populate_supplied_from(struct power_supply *psy)
  131. {
  132. int error;
  133. error = class_for_each_device(power_supply_class, NULL, psy,
  134. __power_supply_populate_supplied_from);
  135. dev_dbg(psy->dev, "%s %d\n", __func__, error);
  136. return error;
  137. }
  138. static int __power_supply_find_supply_from_node(struct device *dev,
  139. void *data)
  140. {
  141. struct device_node *np = data;
  142. struct power_supply *epsy = dev_get_drvdata(dev);
  143. /* returning non-zero breaks out of class_for_each_device loop */
  144. if (epsy->of_node == np)
  145. return 1;
  146. return 0;
  147. }
  148. static int power_supply_find_supply_from_node(struct device_node *supply_node)
  149. {
  150. int error;
  151. /*
  152. * class_for_each_device() either returns its own errors or values
  153. * returned by __power_supply_find_supply_from_node().
  154. *
  155. * __power_supply_find_supply_from_node() will return 0 (no match)
  156. * or 1 (match).
  157. *
  158. * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
  159. * it returned 0, or error as returned by it.
  160. */
  161. error = class_for_each_device(power_supply_class, NULL, supply_node,
  162. __power_supply_find_supply_from_node);
  163. return error ? (error == 1 ? 0 : error) : -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. break;
  180. ret = power_supply_find_supply_from_node(np);
  181. of_node_put(np);
  182. if (ret) {
  183. dev_dbg(psy->dev, "Failed to find supply!\n");
  184. return ret;
  185. }
  186. } while (np);
  187. /* Missing valid "power-supplies" entries */
  188. if (cnt == 1)
  189. return 0;
  190. /* All supplies found, allocate char ** array for filling */
  191. psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from),
  192. GFP_KERNEL);
  193. if (!psy->supplied_from) {
  194. dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
  195. return -ENOMEM;
  196. }
  197. *psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * (cnt - 1),
  198. GFP_KERNEL);
  199. if (!*psy->supplied_from) {
  200. dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
  201. return -ENOMEM;
  202. }
  203. return power_supply_populate_supplied_from(psy);
  204. }
  205. #else
  206. static inline int power_supply_check_supplies(struct power_supply *psy)
  207. {
  208. return 0;
  209. }
  210. #endif
  211. static int __power_supply_am_i_supplied(struct device *dev, void *data)
  212. {
  213. union power_supply_propval ret = {0,};
  214. struct power_supply *psy = data;
  215. struct power_supply *epsy = dev_get_drvdata(dev);
  216. if (__power_supply_is_supplied_by(epsy, psy))
  217. if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret))
  218. return ret.intval;
  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 ret.intval;
  239. return 0;
  240. }
  241. int power_supply_is_system_supplied(void)
  242. {
  243. int error;
  244. unsigned int count = 0;
  245. error = class_for_each_device(power_supply_class, NULL, &count,
  246. __power_supply_is_system_supplied);
  247. /*
  248. * If no power class device was found at all, most probably we are
  249. * running on a desktop system, so assume we are on mains power.
  250. */
  251. if (count == 0)
  252. return 1;
  253. return error;
  254. }
  255. EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
  256. int power_supply_set_battery_charged(struct power_supply *psy)
  257. {
  258. if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
  259. psy->set_charged(psy);
  260. return 0;
  261. }
  262. return -EINVAL;
  263. }
  264. EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
  265. static int power_supply_match_device_by_name(struct device *dev, const void *data)
  266. {
  267. const char *name = data;
  268. struct power_supply *psy = dev_get_drvdata(dev);
  269. return strcmp(psy->name, name) == 0;
  270. }
  271. struct power_supply *power_supply_get_by_name(const char *name)
  272. {
  273. struct device *dev = class_find_device(power_supply_class, NULL, name,
  274. power_supply_match_device_by_name);
  275. return dev ? dev_get_drvdata(dev) : NULL;
  276. }
  277. EXPORT_SYMBOL_GPL(power_supply_get_by_name);
  278. #ifdef CONFIG_OF
  279. static int power_supply_match_device_node(struct device *dev, const void *data)
  280. {
  281. return dev->parent && dev->parent->of_node == data;
  282. }
  283. struct power_supply *power_supply_get_by_phandle(struct device_node *np,
  284. const char *property)
  285. {
  286. struct device_node *power_supply_np;
  287. struct device *dev;
  288. power_supply_np = of_parse_phandle(np, property, 0);
  289. if (!power_supply_np)
  290. return ERR_PTR(-ENODEV);
  291. dev = class_find_device(power_supply_class, NULL, power_supply_np,
  292. power_supply_match_device_node);
  293. of_node_put(power_supply_np);
  294. return dev ? dev_get_drvdata(dev) : NULL;
  295. }
  296. EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
  297. #endif /* CONFIG_OF */
  298. int power_supply_powers(struct power_supply *psy, struct device *dev)
  299. {
  300. return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
  301. }
  302. EXPORT_SYMBOL_GPL(power_supply_powers);
  303. static void power_supply_dev_release(struct device *dev)
  304. {
  305. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  306. kfree(dev);
  307. }
  308. int power_supply_reg_notifier(struct notifier_block *nb)
  309. {
  310. return atomic_notifier_chain_register(&power_supply_notifier, nb);
  311. }
  312. EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
  313. void power_supply_unreg_notifier(struct notifier_block *nb)
  314. {
  315. atomic_notifier_chain_unregister(&power_supply_notifier, nb);
  316. }
  317. EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
  318. #ifdef CONFIG_THERMAL
  319. static int power_supply_read_temp(struct thermal_zone_device *tzd,
  320. unsigned long *temp)
  321. {
  322. struct power_supply *psy;
  323. union power_supply_propval val;
  324. int ret;
  325. WARN_ON(tzd == NULL);
  326. psy = tzd->devdata;
  327. ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
  328. /* Convert tenths of degree Celsius to milli degree Celsius. */
  329. if (!ret)
  330. *temp = val.intval * 100;
  331. return ret;
  332. }
  333. static struct thermal_zone_device_ops psy_tzd_ops = {
  334. .get_temp = power_supply_read_temp,
  335. };
  336. static int psy_register_thermal(struct power_supply *psy)
  337. {
  338. int i;
  339. if (psy->no_thermal)
  340. return 0;
  341. /* Register battery zone device psy reports temperature */
  342. for (i = 0; i < psy->num_properties; i++) {
  343. if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
  344. psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
  345. psy, &psy_tzd_ops, NULL, 0, 0);
  346. return PTR_ERR_OR_ZERO(psy->tzd);
  347. }
  348. }
  349. return 0;
  350. }
  351. static void psy_unregister_thermal(struct power_supply *psy)
  352. {
  353. if (IS_ERR_OR_NULL(psy->tzd))
  354. return;
  355. thermal_zone_device_unregister(psy->tzd);
  356. }
  357. /* thermal cooling device callbacks */
  358. static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
  359. unsigned long *state)
  360. {
  361. struct power_supply *psy;
  362. union power_supply_propval val;
  363. int ret;
  364. psy = tcd->devdata;
  365. ret = psy->get_property(psy,
  366. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
  367. if (!ret)
  368. *state = val.intval;
  369. return ret;
  370. }
  371. static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
  372. unsigned long *state)
  373. {
  374. struct power_supply *psy;
  375. union power_supply_propval val;
  376. int ret;
  377. psy = tcd->devdata;
  378. ret = psy->get_property(psy,
  379. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  380. if (!ret)
  381. *state = val.intval;
  382. return ret;
  383. }
  384. static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
  385. unsigned long state)
  386. {
  387. struct power_supply *psy;
  388. union power_supply_propval val;
  389. int ret;
  390. psy = tcd->devdata;
  391. val.intval = state;
  392. ret = psy->set_property(psy,
  393. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  394. return ret;
  395. }
  396. static struct thermal_cooling_device_ops psy_tcd_ops = {
  397. .get_max_state = ps_get_max_charge_cntl_limit,
  398. .get_cur_state = ps_get_cur_chrage_cntl_limit,
  399. .set_cur_state = ps_set_cur_charge_cntl_limit,
  400. };
  401. static int psy_register_cooler(struct power_supply *psy)
  402. {
  403. int i;
  404. /* Register for cooling device if psy can control charging */
  405. for (i = 0; i < psy->num_properties; i++) {
  406. if (psy->properties[i] ==
  407. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
  408. psy->tcd = thermal_cooling_device_register(
  409. (char *)psy->name,
  410. psy, &psy_tcd_ops);
  411. return PTR_ERR_OR_ZERO(psy->tcd);
  412. }
  413. }
  414. return 0;
  415. }
  416. static void psy_unregister_cooler(struct power_supply *psy)
  417. {
  418. if (IS_ERR_OR_NULL(psy->tcd))
  419. return;
  420. thermal_cooling_device_unregister(psy->tcd);
  421. }
  422. #else
  423. static int psy_register_thermal(struct power_supply *psy)
  424. {
  425. return 0;
  426. }
  427. static void psy_unregister_thermal(struct power_supply *psy)
  428. {
  429. }
  430. static int psy_register_cooler(struct power_supply *psy)
  431. {
  432. return 0;
  433. }
  434. static void psy_unregister_cooler(struct power_supply *psy)
  435. {
  436. }
  437. #endif
  438. static int __power_supply_register(struct device *parent,
  439. struct power_supply *psy, bool ws)
  440. {
  441. struct device *dev;
  442. int rc;
  443. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  444. if (!dev)
  445. return -ENOMEM;
  446. device_initialize(dev);
  447. dev->class = power_supply_class;
  448. dev->type = &power_supply_dev_type;
  449. dev->parent = parent;
  450. dev->release = power_supply_dev_release;
  451. dev_set_drvdata(dev, psy);
  452. psy->dev = dev;
  453. rc = dev_set_name(dev, "%s", psy->name);
  454. if (rc)
  455. goto dev_set_name_failed;
  456. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  457. rc = power_supply_check_supplies(psy);
  458. if (rc) {
  459. dev_info(dev, "Not all required supplies found, defer probe\n");
  460. goto check_supplies_failed;
  461. }
  462. spin_lock_init(&psy->changed_lock);
  463. rc = device_init_wakeup(dev, ws);
  464. if (rc)
  465. goto wakeup_init_failed;
  466. rc = device_add(dev);
  467. if (rc)
  468. goto device_add_failed;
  469. rc = psy_register_thermal(psy);
  470. if (rc)
  471. goto register_thermal_failed;
  472. rc = psy_register_cooler(psy);
  473. if (rc)
  474. goto register_cooler_failed;
  475. rc = power_supply_create_triggers(psy);
  476. if (rc)
  477. goto create_triggers_failed;
  478. power_supply_changed(psy);
  479. return 0;
  480. create_triggers_failed:
  481. psy_unregister_cooler(psy);
  482. register_cooler_failed:
  483. psy_unregister_thermal(psy);
  484. register_thermal_failed:
  485. device_del(dev);
  486. device_add_failed:
  487. wakeup_init_failed:
  488. check_supplies_failed:
  489. dev_set_name_failed:
  490. put_device(dev);
  491. return rc;
  492. }
  493. int power_supply_register(struct device *parent, struct power_supply *psy)
  494. {
  495. return __power_supply_register(parent, psy, true);
  496. }
  497. EXPORT_SYMBOL_GPL(power_supply_register);
  498. int power_supply_register_no_ws(struct device *parent, struct power_supply *psy)
  499. {
  500. return __power_supply_register(parent, psy, false);
  501. }
  502. EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
  503. void power_supply_unregister(struct power_supply *psy)
  504. {
  505. cancel_work_sync(&psy->changed_work);
  506. sysfs_remove_link(&psy->dev->kobj, "powers");
  507. power_supply_remove_triggers(psy);
  508. psy_unregister_cooler(psy);
  509. psy_unregister_thermal(psy);
  510. device_init_wakeup(psy->dev, false);
  511. device_unregister(psy->dev);
  512. }
  513. EXPORT_SYMBOL_GPL(power_supply_unregister);
  514. static int __init power_supply_class_init(void)
  515. {
  516. power_supply_class = class_create(THIS_MODULE, "power_supply");
  517. if (IS_ERR(power_supply_class))
  518. return PTR_ERR(power_supply_class);
  519. power_supply_class->dev_uevent = power_supply_uevent;
  520. power_supply_init_attrs(&power_supply_dev_type);
  521. return 0;
  522. }
  523. static void __exit power_supply_class_exit(void)
  524. {
  525. class_destroy(power_supply_class);
  526. }
  527. subsys_initcall(power_supply_class_init);
  528. module_exit(power_supply_class_exit);
  529. MODULE_DESCRIPTION("Universal power supply monitor class");
  530. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  531. "Szabolcs Gyurko, "
  532. "Anton Vorontsov <cbou@mail.ru>");
  533. MODULE_LICENSE("GPL");