power_supply_core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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->desc->name)
  37. return false;
  38. for (i = 0; i < supply->num_supplies; i++)
  39. if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
  40. return true;
  41. } else {
  42. if (!supply->desc->name)
  43. return false;
  44. for (i = 0; i < supplier->num_supplicants; i++)
  45. if (!strcmp(supplier->supplied_to[i], supply->desc->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->desc->external_power_changed)
  56. pst->desc->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->desc->name, epsy->desc->name);
  121. psy->supplied_from[i-1] = (char *)epsy->desc->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,
  198. sizeof(char *) * (cnt - 1),
  199. GFP_KERNEL);
  200. if (!*psy->supplied_from) {
  201. dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
  202. return -ENOMEM;
  203. }
  204. return power_supply_populate_supplied_from(psy);
  205. }
  206. #else
  207. static inline int power_supply_check_supplies(struct power_supply *psy)
  208. {
  209. return 0;
  210. }
  211. #endif
  212. static int __power_supply_am_i_supplied(struct device *dev, void *data)
  213. {
  214. union power_supply_propval ret = {0,};
  215. struct power_supply *psy = data;
  216. struct power_supply *epsy = dev_get_drvdata(dev);
  217. if (__power_supply_is_supplied_by(epsy, psy))
  218. if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
  219. &ret))
  220. return ret.intval;
  221. return 0;
  222. }
  223. int power_supply_am_i_supplied(struct power_supply *psy)
  224. {
  225. int error;
  226. error = class_for_each_device(power_supply_class, NULL, psy,
  227. __power_supply_am_i_supplied);
  228. dev_dbg(&psy->dev, "%s %d\n", __func__, error);
  229. return error;
  230. }
  231. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  232. static int __power_supply_is_system_supplied(struct device *dev, void *data)
  233. {
  234. union power_supply_propval ret = {0,};
  235. struct power_supply *psy = dev_get_drvdata(dev);
  236. unsigned int *count = data;
  237. (*count)++;
  238. if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
  239. if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
  240. &ret))
  241. return ret.intval;
  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 (atomic_read(&psy->use_cnt) >= 0 &&
  262. psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
  263. psy->desc->set_charged) {
  264. psy->desc->set_charged(psy);
  265. return 0;
  266. }
  267. return -EINVAL;
  268. }
  269. EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
  270. static int power_supply_match_device_by_name(struct device *dev, const void *data)
  271. {
  272. const char *name = data;
  273. struct power_supply *psy = dev_get_drvdata(dev);
  274. return strcmp(psy->desc->name, name) == 0;
  275. }
  276. /**
  277. * power_supply_get_by_name() - Search for a power supply and returns its ref
  278. * @name: Power supply name to fetch
  279. *
  280. * If power supply was found, it increases reference count for the
  281. * internal power supply's device. The user should power_supply_put()
  282. * after usage.
  283. *
  284. * Return: On success returns a reference to a power supply with
  285. * matching name equals to @name, a NULL otherwise.
  286. */
  287. struct power_supply *power_supply_get_by_name(const char *name)
  288. {
  289. struct power_supply *psy = NULL;
  290. struct device *dev = class_find_device(power_supply_class, NULL, name,
  291. power_supply_match_device_by_name);
  292. if (dev) {
  293. psy = dev_get_drvdata(dev);
  294. atomic_inc(&psy->use_cnt);
  295. }
  296. return psy;
  297. }
  298. EXPORT_SYMBOL_GPL(power_supply_get_by_name);
  299. /**
  300. * power_supply_put() - Drop reference obtained with power_supply_get_by_name
  301. * @psy: Reference to put
  302. *
  303. * The reference to power supply should be put before unregistering
  304. * the power supply.
  305. */
  306. void power_supply_put(struct power_supply *psy)
  307. {
  308. might_sleep();
  309. atomic_dec(&psy->use_cnt);
  310. put_device(&psy->dev);
  311. }
  312. EXPORT_SYMBOL_GPL(power_supply_put);
  313. #ifdef CONFIG_OF
  314. static int power_supply_match_device_node(struct device *dev, const void *data)
  315. {
  316. return dev->parent && dev->parent->of_node == data;
  317. }
  318. /**
  319. * power_supply_get_by_phandle() - Search for a power supply and returns its ref
  320. * @np: Pointer to device node holding phandle property
  321. * @phandle_name: Name of property holding a power supply name
  322. *
  323. * If power supply was found, it increases reference count for the
  324. * internal power supply's device. The user should power_supply_put()
  325. * after usage.
  326. *
  327. * Return: On success returns a reference to a power supply with
  328. * matching name equals to value under @property, NULL or ERR_PTR otherwise.
  329. */
  330. struct power_supply *power_supply_get_by_phandle(struct device_node *np,
  331. const char *property)
  332. {
  333. struct device_node *power_supply_np;
  334. struct power_supply *psy = NULL;
  335. struct device *dev;
  336. power_supply_np = of_parse_phandle(np, property, 0);
  337. if (!power_supply_np)
  338. return ERR_PTR(-ENODEV);
  339. dev = class_find_device(power_supply_class, NULL, power_supply_np,
  340. power_supply_match_device_node);
  341. of_node_put(power_supply_np);
  342. if (dev) {
  343. psy = dev_get_drvdata(dev);
  344. atomic_inc(&psy->use_cnt);
  345. }
  346. return psy;
  347. }
  348. EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
  349. #endif /* CONFIG_OF */
  350. int power_supply_get_property(struct power_supply *psy,
  351. enum power_supply_property psp,
  352. union power_supply_propval *val)
  353. {
  354. if (atomic_read(&psy->use_cnt) <= 0)
  355. return -ENODEV;
  356. return psy->desc->get_property(psy, psp, val);
  357. }
  358. EXPORT_SYMBOL_GPL(power_supply_get_property);
  359. int power_supply_set_property(struct power_supply *psy,
  360. enum power_supply_property psp,
  361. const union power_supply_propval *val)
  362. {
  363. if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
  364. return -ENODEV;
  365. return psy->desc->set_property(psy, psp, val);
  366. }
  367. EXPORT_SYMBOL_GPL(power_supply_set_property);
  368. int power_supply_property_is_writeable(struct power_supply *psy,
  369. enum power_supply_property psp)
  370. {
  371. if (atomic_read(&psy->use_cnt) <= 0 ||
  372. !psy->desc->property_is_writeable)
  373. return -ENODEV;
  374. return psy->desc->property_is_writeable(psy, psp);
  375. }
  376. EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
  377. void power_supply_external_power_changed(struct power_supply *psy)
  378. {
  379. if (atomic_read(&psy->use_cnt) <= 0 ||
  380. !psy->desc->external_power_changed)
  381. return;
  382. psy->desc->external_power_changed(psy);
  383. }
  384. EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
  385. int power_supply_powers(struct power_supply *psy, struct device *dev)
  386. {
  387. return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
  388. }
  389. EXPORT_SYMBOL_GPL(power_supply_powers);
  390. static void power_supply_dev_release(struct device *dev)
  391. {
  392. struct power_supply *psy = container_of(dev, struct power_supply, dev);
  393. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  394. kfree(psy);
  395. }
  396. int power_supply_reg_notifier(struct notifier_block *nb)
  397. {
  398. return atomic_notifier_chain_register(&power_supply_notifier, nb);
  399. }
  400. EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
  401. void power_supply_unreg_notifier(struct notifier_block *nb)
  402. {
  403. atomic_notifier_chain_unregister(&power_supply_notifier, nb);
  404. }
  405. EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
  406. #ifdef CONFIG_THERMAL
  407. static int power_supply_read_temp(struct thermal_zone_device *tzd,
  408. unsigned long *temp)
  409. {
  410. struct power_supply *psy;
  411. union power_supply_propval val;
  412. int ret;
  413. WARN_ON(tzd == NULL);
  414. psy = tzd->devdata;
  415. ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
  416. /* Convert tenths of degree Celsius to milli degree Celsius. */
  417. if (!ret)
  418. *temp = val.intval * 100;
  419. return ret;
  420. }
  421. static struct thermal_zone_device_ops psy_tzd_ops = {
  422. .get_temp = power_supply_read_temp,
  423. };
  424. static int psy_register_thermal(struct power_supply *psy)
  425. {
  426. int i;
  427. if (psy->desc->no_thermal)
  428. return 0;
  429. /* Register battery zone device psy reports temperature */
  430. for (i = 0; i < psy->desc->num_properties; i++) {
  431. if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
  432. psy->tzd = thermal_zone_device_register(psy->desc->name,
  433. 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
  434. return PTR_ERR_OR_ZERO(psy->tzd);
  435. }
  436. }
  437. return 0;
  438. }
  439. static void psy_unregister_thermal(struct power_supply *psy)
  440. {
  441. if (IS_ERR_OR_NULL(psy->tzd))
  442. return;
  443. thermal_zone_device_unregister(psy->tzd);
  444. }
  445. /* thermal cooling device callbacks */
  446. static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
  447. unsigned long *state)
  448. {
  449. struct power_supply *psy;
  450. union power_supply_propval val;
  451. int ret;
  452. psy = tcd->devdata;
  453. ret = psy->desc->get_property(psy,
  454. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
  455. if (!ret)
  456. *state = val.intval;
  457. return ret;
  458. }
  459. static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
  460. unsigned long *state)
  461. {
  462. struct power_supply *psy;
  463. union power_supply_propval val;
  464. int ret;
  465. psy = tcd->devdata;
  466. ret = psy->desc->get_property(psy,
  467. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  468. if (!ret)
  469. *state = val.intval;
  470. return ret;
  471. }
  472. static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
  473. unsigned long state)
  474. {
  475. struct power_supply *psy;
  476. union power_supply_propval val;
  477. int ret;
  478. psy = tcd->devdata;
  479. val.intval = state;
  480. ret = psy->desc->set_property(psy,
  481. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  482. return ret;
  483. }
  484. static struct thermal_cooling_device_ops psy_tcd_ops = {
  485. .get_max_state = ps_get_max_charge_cntl_limit,
  486. .get_cur_state = ps_get_cur_chrage_cntl_limit,
  487. .set_cur_state = ps_set_cur_charge_cntl_limit,
  488. };
  489. static int psy_register_cooler(struct power_supply *psy)
  490. {
  491. int i;
  492. /* Register for cooling device if psy can control charging */
  493. for (i = 0; i < psy->desc->num_properties; i++) {
  494. if (psy->desc->properties[i] ==
  495. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
  496. psy->tcd = thermal_cooling_device_register(
  497. (char *)psy->desc->name,
  498. psy, &psy_tcd_ops);
  499. return PTR_ERR_OR_ZERO(psy->tcd);
  500. }
  501. }
  502. return 0;
  503. }
  504. static void psy_unregister_cooler(struct power_supply *psy)
  505. {
  506. if (IS_ERR_OR_NULL(psy->tcd))
  507. return;
  508. thermal_cooling_device_unregister(psy->tcd);
  509. }
  510. #else
  511. static int psy_register_thermal(struct power_supply *psy)
  512. {
  513. return 0;
  514. }
  515. static void psy_unregister_thermal(struct power_supply *psy)
  516. {
  517. }
  518. static int psy_register_cooler(struct power_supply *psy)
  519. {
  520. return 0;
  521. }
  522. static void psy_unregister_cooler(struct power_supply *psy)
  523. {
  524. }
  525. #endif
  526. static struct power_supply *__must_check
  527. __power_supply_register(struct device *parent,
  528. const struct power_supply_desc *desc,
  529. const struct power_supply_config *cfg,
  530. bool ws)
  531. {
  532. struct device *dev;
  533. struct power_supply *psy;
  534. int rc;
  535. psy = kzalloc(sizeof(*psy), GFP_KERNEL);
  536. if (!psy)
  537. return ERR_PTR(-ENOMEM);
  538. dev = &psy->dev;
  539. device_initialize(dev);
  540. dev->class = power_supply_class;
  541. dev->type = &power_supply_dev_type;
  542. dev->parent = parent;
  543. dev->release = power_supply_dev_release;
  544. dev_set_drvdata(dev, psy);
  545. psy->desc = desc;
  546. atomic_inc(&psy->use_cnt);
  547. if (cfg) {
  548. psy->drv_data = cfg->drv_data;
  549. psy->of_node = cfg->of_node;
  550. psy->supplied_to = cfg->supplied_to;
  551. psy->num_supplicants = cfg->num_supplicants;
  552. }
  553. rc = dev_set_name(dev, "%s", desc->name);
  554. if (rc)
  555. goto dev_set_name_failed;
  556. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  557. rc = power_supply_check_supplies(psy);
  558. if (rc) {
  559. dev_info(dev, "Not all required supplies found, defer probe\n");
  560. goto check_supplies_failed;
  561. }
  562. spin_lock_init(&psy->changed_lock);
  563. rc = device_init_wakeup(dev, ws);
  564. if (rc)
  565. goto wakeup_init_failed;
  566. rc = device_add(dev);
  567. if (rc)
  568. goto device_add_failed;
  569. rc = psy_register_thermal(psy);
  570. if (rc)
  571. goto register_thermal_failed;
  572. rc = psy_register_cooler(psy);
  573. if (rc)
  574. goto register_cooler_failed;
  575. rc = power_supply_create_triggers(psy);
  576. if (rc)
  577. goto create_triggers_failed;
  578. power_supply_changed(psy);
  579. return psy;
  580. create_triggers_failed:
  581. psy_unregister_cooler(psy);
  582. register_cooler_failed:
  583. psy_unregister_thermal(psy);
  584. register_thermal_failed:
  585. device_del(dev);
  586. device_add_failed:
  587. wakeup_init_failed:
  588. check_supplies_failed:
  589. dev_set_name_failed:
  590. put_device(dev);
  591. return ERR_PTR(rc);
  592. }
  593. /**
  594. * power_supply_register() - Register new power supply
  595. * @parent: Device to be a parent of power supply's device
  596. * @desc: Description of power supply, must be valid through whole
  597. * lifetime of this power supply
  598. * @cfg: Run-time specific configuration accessed during registering,
  599. * may be NULL
  600. *
  601. * Return: A pointer to newly allocated power_supply on success
  602. * or ERR_PTR otherwise.
  603. * Use power_supply_unregister() on returned power_supply pointer to release
  604. * resources.
  605. */
  606. struct power_supply *__must_check power_supply_register(struct device *parent,
  607. const struct power_supply_desc *desc,
  608. const struct power_supply_config *cfg)
  609. {
  610. return __power_supply_register(parent, desc, cfg, true);
  611. }
  612. EXPORT_SYMBOL_GPL(power_supply_register);
  613. /**
  614. * power_supply_register() - Register new non-waking-source power supply
  615. * @parent: Device to be a parent of power supply's device
  616. * @desc: Description of power supply, must be valid through whole
  617. * lifetime of this power supply
  618. * @cfg: Run-time specific configuration accessed during registering,
  619. * may be NULL
  620. *
  621. * Return: A pointer to newly allocated power_supply on success
  622. * or ERR_PTR otherwise.
  623. * Use power_supply_unregister() on returned power_supply pointer to release
  624. * resources.
  625. */
  626. struct power_supply *__must_check
  627. power_supply_register_no_ws(struct device *parent,
  628. const struct power_supply_desc *desc,
  629. const struct power_supply_config *cfg)
  630. {
  631. return __power_supply_register(parent, desc, cfg, false);
  632. }
  633. EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
  634. static void devm_power_supply_release(struct device *dev, void *res)
  635. {
  636. struct power_supply **psy = res;
  637. power_supply_unregister(*psy);
  638. }
  639. /**
  640. * power_supply_register() - Register managed power supply
  641. * @parent: Device to be a parent of power supply's device
  642. * @desc: Description of power supply, must be valid through whole
  643. * lifetime of this power supply
  644. * @cfg: Run-time specific configuration accessed during registering,
  645. * may be NULL
  646. *
  647. * Return: A pointer to newly allocated power_supply on success
  648. * or ERR_PTR otherwise.
  649. * The returned power_supply pointer will be automatically unregistered
  650. * on driver detach.
  651. */
  652. struct power_supply *__must_check
  653. devm_power_supply_register(struct device *parent,
  654. const struct power_supply_desc *desc,
  655. const struct power_supply_config *cfg)
  656. {
  657. struct power_supply **ptr, *psy;
  658. ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
  659. if (!ptr)
  660. return ERR_PTR(-ENOMEM);
  661. psy = __power_supply_register(parent, desc, cfg, true);
  662. if (IS_ERR(psy)) {
  663. devres_free(ptr);
  664. } else {
  665. *ptr = psy;
  666. devres_add(parent, ptr);
  667. }
  668. return psy;
  669. }
  670. EXPORT_SYMBOL_GPL(devm_power_supply_register);
  671. /**
  672. * power_supply_register() - Register managed non-waking-source power supply
  673. * @parent: Device to be a parent of power supply's device
  674. * @desc: Description of power supply, must be valid through whole
  675. * lifetime of this power supply
  676. * @cfg: Run-time specific configuration accessed during registering,
  677. * may be NULL
  678. *
  679. * Return: A pointer to newly allocated power_supply on success
  680. * or ERR_PTR otherwise.
  681. * The returned power_supply pointer will be automatically unregistered
  682. * on driver detach.
  683. */
  684. struct power_supply *__must_check
  685. devm_power_supply_register_no_ws(struct device *parent,
  686. const struct power_supply_desc *desc,
  687. const struct power_supply_config *cfg)
  688. {
  689. struct power_supply **ptr, *psy;
  690. ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
  691. if (!ptr)
  692. return ERR_PTR(-ENOMEM);
  693. psy = __power_supply_register(parent, desc, cfg, false);
  694. if (IS_ERR(psy)) {
  695. devres_free(ptr);
  696. } else {
  697. *ptr = psy;
  698. devres_add(parent, ptr);
  699. }
  700. return psy;
  701. }
  702. EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
  703. /**
  704. * power_supply_unregister() - Remove this power supply from system
  705. * @psy: Pointer to power supply to unregister
  706. *
  707. * Remove this power supply from the system. The resources of power supply
  708. * will be freed here or on last power_supply_put() call.
  709. */
  710. void power_supply_unregister(struct power_supply *psy)
  711. {
  712. WARN_ON(atomic_dec_return(&psy->use_cnt));
  713. cancel_work_sync(&psy->changed_work);
  714. sysfs_remove_link(&psy->dev.kobj, "powers");
  715. power_supply_remove_triggers(psy);
  716. psy_unregister_cooler(psy);
  717. psy_unregister_thermal(psy);
  718. device_init_wakeup(&psy->dev, false);
  719. device_unregister(&psy->dev);
  720. }
  721. EXPORT_SYMBOL_GPL(power_supply_unregister);
  722. void *power_supply_get_drvdata(struct power_supply *psy)
  723. {
  724. return psy->drv_data;
  725. }
  726. EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
  727. static int __init power_supply_class_init(void)
  728. {
  729. power_supply_class = class_create(THIS_MODULE, "power_supply");
  730. if (IS_ERR(power_supply_class))
  731. return PTR_ERR(power_supply_class);
  732. power_supply_class->dev_uevent = power_supply_uevent;
  733. power_supply_init_attrs(&power_supply_dev_type);
  734. return 0;
  735. }
  736. static void __exit power_supply_class_exit(void)
  737. {
  738. class_destroy(power_supply_class);
  739. }
  740. subsys_initcall(power_supply_class_init);
  741. module_exit(power_supply_class_exit);
  742. MODULE_DESCRIPTION("Universal power supply monitor class");
  743. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  744. "Szabolcs Gyurko, "
  745. "Anton Vorontsov <cbou@mail.ru>");
  746. MODULE_LICENSE("GPL");