power_supply_core.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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. #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
  29. static bool __power_supply_is_supplied_by(struct power_supply *supplier,
  30. struct power_supply *supply)
  31. {
  32. int i;
  33. if (!supply->supplied_from && !supplier->supplied_to)
  34. return false;
  35. /* Support both supplied_to and supplied_from modes */
  36. if (supply->supplied_from) {
  37. if (!supplier->desc->name)
  38. return false;
  39. for (i = 0; i < supply->num_supplies; i++)
  40. if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
  41. return true;
  42. } else {
  43. if (!supply->desc->name)
  44. return false;
  45. for (i = 0; i < supplier->num_supplicants; i++)
  46. if (!strcmp(supplier->supplied_to[i], supply->desc->name))
  47. return true;
  48. }
  49. return false;
  50. }
  51. static int __power_supply_changed_work(struct device *dev, void *data)
  52. {
  53. struct power_supply *psy = data;
  54. struct power_supply *pst = dev_get_drvdata(dev);
  55. if (__power_supply_is_supplied_by(psy, pst)) {
  56. if (pst->desc->external_power_changed)
  57. pst->desc->external_power_changed(pst);
  58. }
  59. return 0;
  60. }
  61. static void power_supply_changed_work(struct work_struct *work)
  62. {
  63. unsigned long flags;
  64. struct power_supply *psy = container_of(work, struct power_supply,
  65. changed_work);
  66. dev_dbg(&psy->dev, "%s\n", __func__);
  67. spin_lock_irqsave(&psy->changed_lock, flags);
  68. /*
  69. * Check 'changed' here to avoid issues due to race between
  70. * power_supply_changed() and this routine. In worst case
  71. * power_supply_changed() can be called again just before we take above
  72. * lock. During the first call of this routine we will mark 'changed' as
  73. * false and it will stay false for the next call as well.
  74. */
  75. if (likely(psy->changed)) {
  76. psy->changed = false;
  77. spin_unlock_irqrestore(&psy->changed_lock, flags);
  78. class_for_each_device(power_supply_class, NULL, psy,
  79. __power_supply_changed_work);
  80. power_supply_update_leds(psy);
  81. atomic_notifier_call_chain(&power_supply_notifier,
  82. PSY_EVENT_PROP_CHANGED, psy);
  83. kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
  84. spin_lock_irqsave(&psy->changed_lock, flags);
  85. }
  86. /*
  87. * Hold the wakeup_source until all events are processed.
  88. * power_supply_changed() might have called again and have set 'changed'
  89. * to true.
  90. */
  91. if (likely(!psy->changed))
  92. pm_relax(&psy->dev);
  93. spin_unlock_irqrestore(&psy->changed_lock, flags);
  94. }
  95. void power_supply_changed(struct power_supply *psy)
  96. {
  97. unsigned long flags;
  98. dev_dbg(&psy->dev, "%s\n", __func__);
  99. spin_lock_irqsave(&psy->changed_lock, flags);
  100. psy->changed = true;
  101. pm_stay_awake(&psy->dev);
  102. spin_unlock_irqrestore(&psy->changed_lock, flags);
  103. schedule_work(&psy->changed_work);
  104. }
  105. EXPORT_SYMBOL_GPL(power_supply_changed);
  106. /*
  107. * Notify that power supply was registered after parent finished the probing.
  108. *
  109. * Often power supply is registered from driver's probe function. However
  110. * calling power_supply_changed() directly from power_supply_register()
  111. * would lead to execution of get_property() function provided by the driver
  112. * too early - before the probe ends.
  113. *
  114. * Avoid that by waiting on parent's mutex.
  115. */
  116. static void power_supply_deferred_register_work(struct work_struct *work)
  117. {
  118. struct power_supply *psy = container_of(work, struct power_supply,
  119. deferred_register_work.work);
  120. if (psy->dev.parent)
  121. mutex_lock(&psy->dev.parent->mutex);
  122. power_supply_changed(psy);
  123. if (psy->dev.parent)
  124. mutex_unlock(&psy->dev.parent->mutex);
  125. }
  126. #ifdef CONFIG_OF
  127. #include <linux/of.h>
  128. static int __power_supply_populate_supplied_from(struct device *dev,
  129. void *data)
  130. {
  131. struct power_supply *psy = data;
  132. struct power_supply *epsy = dev_get_drvdata(dev);
  133. struct device_node *np;
  134. int i = 0;
  135. do {
  136. np = of_parse_phandle(psy->of_node, "power-supplies", i++);
  137. if (!np)
  138. break;
  139. if (np == epsy->of_node) {
  140. dev_info(&psy->dev, "%s: Found supply : %s\n",
  141. psy->desc->name, epsy->desc->name);
  142. psy->supplied_from[i-1] = (char *)epsy->desc->name;
  143. psy->num_supplies++;
  144. of_node_put(np);
  145. break;
  146. }
  147. of_node_put(np);
  148. } while (np);
  149. return 0;
  150. }
  151. static int power_supply_populate_supplied_from(struct power_supply *psy)
  152. {
  153. int error;
  154. error = class_for_each_device(power_supply_class, NULL, psy,
  155. __power_supply_populate_supplied_from);
  156. dev_dbg(&psy->dev, "%s %d\n", __func__, error);
  157. return error;
  158. }
  159. static int __power_supply_find_supply_from_node(struct device *dev,
  160. void *data)
  161. {
  162. struct device_node *np = data;
  163. struct power_supply *epsy = dev_get_drvdata(dev);
  164. /* returning non-zero breaks out of class_for_each_device loop */
  165. if (epsy->of_node == np)
  166. return 1;
  167. return 0;
  168. }
  169. static int power_supply_find_supply_from_node(struct device_node *supply_node)
  170. {
  171. int error;
  172. /*
  173. * class_for_each_device() either returns its own errors or values
  174. * returned by __power_supply_find_supply_from_node().
  175. *
  176. * __power_supply_find_supply_from_node() will return 0 (no match)
  177. * or 1 (match).
  178. *
  179. * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
  180. * it returned 0, or error as returned by it.
  181. */
  182. error = class_for_each_device(power_supply_class, NULL, supply_node,
  183. __power_supply_find_supply_from_node);
  184. return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
  185. }
  186. static int power_supply_check_supplies(struct power_supply *psy)
  187. {
  188. struct device_node *np;
  189. int cnt = 0;
  190. /* If there is already a list honor it */
  191. if (psy->supplied_from && psy->num_supplies > 0)
  192. return 0;
  193. /* No device node found, nothing to do */
  194. if (!psy->of_node)
  195. return 0;
  196. do {
  197. int ret;
  198. np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
  199. if (!np)
  200. break;
  201. ret = power_supply_find_supply_from_node(np);
  202. of_node_put(np);
  203. if (ret) {
  204. dev_dbg(&psy->dev, "Failed to find supply!\n");
  205. return ret;
  206. }
  207. } while (np);
  208. /* Missing valid "power-supplies" entries */
  209. if (cnt == 1)
  210. return 0;
  211. /* All supplies found, allocate char ** array for filling */
  212. psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
  213. GFP_KERNEL);
  214. if (!psy->supplied_from) {
  215. dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
  216. return -ENOMEM;
  217. }
  218. *psy->supplied_from = devm_kzalloc(&psy->dev,
  219. sizeof(char *) * (cnt - 1),
  220. GFP_KERNEL);
  221. if (!*psy->supplied_from) {
  222. dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
  223. return -ENOMEM;
  224. }
  225. return power_supply_populate_supplied_from(psy);
  226. }
  227. #else
  228. static inline int power_supply_check_supplies(struct power_supply *psy)
  229. {
  230. return 0;
  231. }
  232. #endif
  233. static int __power_supply_am_i_supplied(struct device *dev, void *data)
  234. {
  235. union power_supply_propval ret = {0,};
  236. struct power_supply *psy = data;
  237. struct power_supply *epsy = dev_get_drvdata(dev);
  238. if (__power_supply_is_supplied_by(epsy, psy))
  239. if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
  240. &ret))
  241. return ret.intval;
  242. return 0;
  243. }
  244. int power_supply_am_i_supplied(struct power_supply *psy)
  245. {
  246. int error;
  247. error = class_for_each_device(power_supply_class, NULL, psy,
  248. __power_supply_am_i_supplied);
  249. dev_dbg(&psy->dev, "%s %d\n", __func__, error);
  250. return error;
  251. }
  252. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  253. static int __power_supply_is_system_supplied(struct device *dev, void *data)
  254. {
  255. union power_supply_propval ret = {0,};
  256. struct power_supply *psy = dev_get_drvdata(dev);
  257. unsigned int *count = data;
  258. (*count)++;
  259. if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
  260. if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
  261. &ret))
  262. return ret.intval;
  263. return 0;
  264. }
  265. int power_supply_is_system_supplied(void)
  266. {
  267. int error;
  268. unsigned int count = 0;
  269. error = class_for_each_device(power_supply_class, NULL, &count,
  270. __power_supply_is_system_supplied);
  271. /*
  272. * If no power class device was found at all, most probably we are
  273. * running on a desktop system, so assume we are on mains power.
  274. */
  275. if (count == 0)
  276. return 1;
  277. return error;
  278. }
  279. EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
  280. int power_supply_set_battery_charged(struct power_supply *psy)
  281. {
  282. if (atomic_read(&psy->use_cnt) >= 0 &&
  283. psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
  284. psy->desc->set_charged) {
  285. psy->desc->set_charged(psy);
  286. return 0;
  287. }
  288. return -EINVAL;
  289. }
  290. EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
  291. static int power_supply_match_device_by_name(struct device *dev, const void *data)
  292. {
  293. const char *name = data;
  294. struct power_supply *psy = dev_get_drvdata(dev);
  295. return strcmp(psy->desc->name, name) == 0;
  296. }
  297. /**
  298. * power_supply_get_by_name() - Search for a power supply and returns its ref
  299. * @name: Power supply name to fetch
  300. *
  301. * If power supply was found, it increases reference count for the
  302. * internal power supply's device. The user should power_supply_put()
  303. * after usage.
  304. *
  305. * Return: On success returns a reference to a power supply with
  306. * matching name equals to @name, a NULL otherwise.
  307. */
  308. struct power_supply *power_supply_get_by_name(const char *name)
  309. {
  310. struct power_supply *psy = NULL;
  311. struct device *dev = class_find_device(power_supply_class, NULL, name,
  312. power_supply_match_device_by_name);
  313. if (dev) {
  314. psy = dev_get_drvdata(dev);
  315. atomic_inc(&psy->use_cnt);
  316. }
  317. return psy;
  318. }
  319. EXPORT_SYMBOL_GPL(power_supply_get_by_name);
  320. /**
  321. * power_supply_put() - Drop reference obtained with power_supply_get_by_name
  322. * @psy: Reference to put
  323. *
  324. * The reference to power supply should be put before unregistering
  325. * the power supply.
  326. */
  327. void power_supply_put(struct power_supply *psy)
  328. {
  329. might_sleep();
  330. atomic_dec(&psy->use_cnt);
  331. put_device(&psy->dev);
  332. }
  333. EXPORT_SYMBOL_GPL(power_supply_put);
  334. #ifdef CONFIG_OF
  335. static int power_supply_match_device_node(struct device *dev, const void *data)
  336. {
  337. return dev->parent && dev->parent->of_node == data;
  338. }
  339. /**
  340. * power_supply_get_by_phandle() - Search for a power supply and returns its ref
  341. * @np: Pointer to device node holding phandle property
  342. * @phandle_name: Name of property holding a power supply name
  343. *
  344. * If power supply was found, it increases reference count for the
  345. * internal power supply's device. The user should power_supply_put()
  346. * after usage.
  347. *
  348. * Return: On success returns a reference to a power supply with
  349. * matching name equals to value under @property, NULL or ERR_PTR otherwise.
  350. */
  351. struct power_supply *power_supply_get_by_phandle(struct device_node *np,
  352. const char *property)
  353. {
  354. struct device_node *power_supply_np;
  355. struct power_supply *psy = NULL;
  356. struct device *dev;
  357. power_supply_np = of_parse_phandle(np, property, 0);
  358. if (!power_supply_np)
  359. return ERR_PTR(-ENODEV);
  360. dev = class_find_device(power_supply_class, NULL, power_supply_np,
  361. power_supply_match_device_node);
  362. of_node_put(power_supply_np);
  363. if (dev) {
  364. psy = dev_get_drvdata(dev);
  365. atomic_inc(&psy->use_cnt);
  366. }
  367. return psy;
  368. }
  369. EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
  370. static void devm_power_supply_put(struct device *dev, void *res)
  371. {
  372. struct power_supply **psy = res;
  373. power_supply_put(*psy);
  374. }
  375. /**
  376. * devm_power_supply_get_by_phandle() - Resource managed version of
  377. * power_supply_get_by_phandle()
  378. * @dev: Pointer to device holding phandle property
  379. * @phandle_name: Name of property holding a power supply phandle
  380. *
  381. * Return: On success returns a reference to a power supply with
  382. * matching name equals to value under @property, NULL or ERR_PTR otherwise.
  383. */
  384. struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
  385. const char *property)
  386. {
  387. struct power_supply **ptr, *psy;
  388. if (!dev->of_node)
  389. return ERR_PTR(-ENODEV);
  390. ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
  391. if (!ptr)
  392. return ERR_PTR(-ENOMEM);
  393. psy = power_supply_get_by_phandle(dev->of_node, property);
  394. if (IS_ERR_OR_NULL(psy)) {
  395. devres_free(ptr);
  396. } else {
  397. *ptr = psy;
  398. devres_add(dev, ptr);
  399. }
  400. return psy;
  401. }
  402. EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
  403. #endif /* CONFIG_OF */
  404. int power_supply_get_property(struct power_supply *psy,
  405. enum power_supply_property psp,
  406. union power_supply_propval *val)
  407. {
  408. if (atomic_read(&psy->use_cnt) <= 0)
  409. return -ENODEV;
  410. return psy->desc->get_property(psy, psp, val);
  411. }
  412. EXPORT_SYMBOL_GPL(power_supply_get_property);
  413. int power_supply_set_property(struct power_supply *psy,
  414. enum power_supply_property psp,
  415. const union power_supply_propval *val)
  416. {
  417. if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
  418. return -ENODEV;
  419. return psy->desc->set_property(psy, psp, val);
  420. }
  421. EXPORT_SYMBOL_GPL(power_supply_set_property);
  422. int power_supply_property_is_writeable(struct power_supply *psy,
  423. enum power_supply_property psp)
  424. {
  425. if (atomic_read(&psy->use_cnt) <= 0 ||
  426. !psy->desc->property_is_writeable)
  427. return -ENODEV;
  428. return psy->desc->property_is_writeable(psy, psp);
  429. }
  430. EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
  431. void power_supply_external_power_changed(struct power_supply *psy)
  432. {
  433. if (atomic_read(&psy->use_cnt) <= 0 ||
  434. !psy->desc->external_power_changed)
  435. return;
  436. psy->desc->external_power_changed(psy);
  437. }
  438. EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
  439. int power_supply_powers(struct power_supply *psy, struct device *dev)
  440. {
  441. return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
  442. }
  443. EXPORT_SYMBOL_GPL(power_supply_powers);
  444. static void power_supply_dev_release(struct device *dev)
  445. {
  446. struct power_supply *psy = container_of(dev, struct power_supply, dev);
  447. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  448. kfree(psy);
  449. }
  450. int power_supply_reg_notifier(struct notifier_block *nb)
  451. {
  452. return atomic_notifier_chain_register(&power_supply_notifier, nb);
  453. }
  454. EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
  455. void power_supply_unreg_notifier(struct notifier_block *nb)
  456. {
  457. atomic_notifier_chain_unregister(&power_supply_notifier, nb);
  458. }
  459. EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
  460. #ifdef CONFIG_THERMAL
  461. static int power_supply_read_temp(struct thermal_zone_device *tzd,
  462. unsigned long *temp)
  463. {
  464. struct power_supply *psy;
  465. union power_supply_propval val;
  466. int ret;
  467. WARN_ON(tzd == NULL);
  468. psy = tzd->devdata;
  469. ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
  470. /* Convert tenths of degree Celsius to milli degree Celsius. */
  471. if (!ret)
  472. *temp = val.intval * 100;
  473. return ret;
  474. }
  475. static struct thermal_zone_device_ops psy_tzd_ops = {
  476. .get_temp = power_supply_read_temp,
  477. };
  478. static int psy_register_thermal(struct power_supply *psy)
  479. {
  480. int i;
  481. if (psy->desc->no_thermal)
  482. return 0;
  483. /* Register battery zone device psy reports temperature */
  484. for (i = 0; i < psy->desc->num_properties; i++) {
  485. if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
  486. psy->tzd = thermal_zone_device_register(psy->desc->name,
  487. 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
  488. return PTR_ERR_OR_ZERO(psy->tzd);
  489. }
  490. }
  491. return 0;
  492. }
  493. static void psy_unregister_thermal(struct power_supply *psy)
  494. {
  495. if (IS_ERR_OR_NULL(psy->tzd))
  496. return;
  497. thermal_zone_device_unregister(psy->tzd);
  498. }
  499. /* thermal cooling device callbacks */
  500. static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
  501. unsigned long *state)
  502. {
  503. struct power_supply *psy;
  504. union power_supply_propval val;
  505. int ret;
  506. psy = tcd->devdata;
  507. ret = psy->desc->get_property(psy,
  508. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
  509. if (!ret)
  510. *state = val.intval;
  511. return ret;
  512. }
  513. static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
  514. unsigned long *state)
  515. {
  516. struct power_supply *psy;
  517. union power_supply_propval val;
  518. int ret;
  519. psy = tcd->devdata;
  520. ret = psy->desc->get_property(psy,
  521. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  522. if (!ret)
  523. *state = val.intval;
  524. return ret;
  525. }
  526. static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
  527. unsigned long state)
  528. {
  529. struct power_supply *psy;
  530. union power_supply_propval val;
  531. int ret;
  532. psy = tcd->devdata;
  533. val.intval = state;
  534. ret = psy->desc->set_property(psy,
  535. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  536. return ret;
  537. }
  538. static struct thermal_cooling_device_ops psy_tcd_ops = {
  539. .get_max_state = ps_get_max_charge_cntl_limit,
  540. .get_cur_state = ps_get_cur_chrage_cntl_limit,
  541. .set_cur_state = ps_set_cur_charge_cntl_limit,
  542. };
  543. static int psy_register_cooler(struct power_supply *psy)
  544. {
  545. int i;
  546. /* Register for cooling device if psy can control charging */
  547. for (i = 0; i < psy->desc->num_properties; i++) {
  548. if (psy->desc->properties[i] ==
  549. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
  550. psy->tcd = thermal_cooling_device_register(
  551. (char *)psy->desc->name,
  552. psy, &psy_tcd_ops);
  553. return PTR_ERR_OR_ZERO(psy->tcd);
  554. }
  555. }
  556. return 0;
  557. }
  558. static void psy_unregister_cooler(struct power_supply *psy)
  559. {
  560. if (IS_ERR_OR_NULL(psy->tcd))
  561. return;
  562. thermal_cooling_device_unregister(psy->tcd);
  563. }
  564. #else
  565. static int psy_register_thermal(struct power_supply *psy)
  566. {
  567. return 0;
  568. }
  569. static void psy_unregister_thermal(struct power_supply *psy)
  570. {
  571. }
  572. static int psy_register_cooler(struct power_supply *psy)
  573. {
  574. return 0;
  575. }
  576. static void psy_unregister_cooler(struct power_supply *psy)
  577. {
  578. }
  579. #endif
  580. static struct power_supply *__must_check
  581. __power_supply_register(struct device *parent,
  582. const struct power_supply_desc *desc,
  583. const struct power_supply_config *cfg,
  584. bool ws)
  585. {
  586. struct device *dev;
  587. struct power_supply *psy;
  588. int rc;
  589. if (!parent)
  590. pr_warn("%s: Expected proper parent device for '%s'\n",
  591. __func__, desc->name);
  592. psy = kzalloc(sizeof(*psy), GFP_KERNEL);
  593. if (!psy)
  594. return ERR_PTR(-ENOMEM);
  595. dev = &psy->dev;
  596. device_initialize(dev);
  597. dev->class = power_supply_class;
  598. dev->type = &power_supply_dev_type;
  599. dev->parent = parent;
  600. dev->release = power_supply_dev_release;
  601. dev_set_drvdata(dev, psy);
  602. psy->desc = desc;
  603. if (cfg) {
  604. psy->drv_data = cfg->drv_data;
  605. psy->of_node = cfg->of_node;
  606. psy->supplied_to = cfg->supplied_to;
  607. psy->num_supplicants = cfg->num_supplicants;
  608. }
  609. rc = dev_set_name(dev, "%s", desc->name);
  610. if (rc)
  611. goto dev_set_name_failed;
  612. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  613. INIT_DELAYED_WORK(&psy->deferred_register_work,
  614. power_supply_deferred_register_work);
  615. rc = power_supply_check_supplies(psy);
  616. if (rc) {
  617. dev_info(dev, "Not all required supplies found, defer probe\n");
  618. goto check_supplies_failed;
  619. }
  620. spin_lock_init(&psy->changed_lock);
  621. rc = device_init_wakeup(dev, ws);
  622. if (rc)
  623. goto wakeup_init_failed;
  624. rc = device_add(dev);
  625. if (rc)
  626. goto device_add_failed;
  627. rc = psy_register_thermal(psy);
  628. if (rc)
  629. goto register_thermal_failed;
  630. rc = psy_register_cooler(psy);
  631. if (rc)
  632. goto register_cooler_failed;
  633. rc = power_supply_create_triggers(psy);
  634. if (rc)
  635. goto create_triggers_failed;
  636. /*
  637. * Update use_cnt after any uevents (most notably from device_add()).
  638. * We are here still during driver's probe but
  639. * the power_supply_uevent() calls back driver's get_property
  640. * method so:
  641. * 1. Driver did not assigned the returned struct power_supply,
  642. * 2. Driver could not finish initialization (anything in its probe
  643. * after calling power_supply_register()).
  644. */
  645. atomic_inc(&psy->use_cnt);
  646. queue_delayed_work(system_power_efficient_wq,
  647. &psy->deferred_register_work,
  648. POWER_SUPPLY_DEFERRED_REGISTER_TIME);
  649. return psy;
  650. create_triggers_failed:
  651. psy_unregister_cooler(psy);
  652. register_cooler_failed:
  653. psy_unregister_thermal(psy);
  654. register_thermal_failed:
  655. device_del(dev);
  656. device_add_failed:
  657. wakeup_init_failed:
  658. check_supplies_failed:
  659. dev_set_name_failed:
  660. put_device(dev);
  661. return ERR_PTR(rc);
  662. }
  663. /**
  664. * power_supply_register() - Register new power supply
  665. * @parent: Device to be a parent of power supply's device, usually
  666. * the device which probe function calls this
  667. * @desc: Description of power supply, must be valid through whole
  668. * lifetime of this power supply
  669. * @cfg: Run-time specific configuration accessed during registering,
  670. * may be NULL
  671. *
  672. * Return: A pointer to newly allocated power_supply on success
  673. * or ERR_PTR otherwise.
  674. * Use power_supply_unregister() on returned power_supply pointer to release
  675. * resources.
  676. */
  677. struct power_supply *__must_check power_supply_register(struct device *parent,
  678. const struct power_supply_desc *desc,
  679. const struct power_supply_config *cfg)
  680. {
  681. return __power_supply_register(parent, desc, cfg, true);
  682. }
  683. EXPORT_SYMBOL_GPL(power_supply_register);
  684. /**
  685. * power_supply_register_no_ws() - Register new non-waking-source power supply
  686. * @parent: Device to be a parent of power supply's device, usually
  687. * the device which probe function calls this
  688. * @desc: Description of power supply, must be valid through whole
  689. * lifetime of this power supply
  690. * @cfg: Run-time specific configuration accessed during registering,
  691. * may be NULL
  692. *
  693. * Return: A pointer to newly allocated power_supply on success
  694. * or ERR_PTR otherwise.
  695. * Use power_supply_unregister() on returned power_supply pointer to release
  696. * resources.
  697. */
  698. struct power_supply *__must_check
  699. power_supply_register_no_ws(struct device *parent,
  700. const struct power_supply_desc *desc,
  701. const struct power_supply_config *cfg)
  702. {
  703. return __power_supply_register(parent, desc, cfg, false);
  704. }
  705. EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
  706. static void devm_power_supply_release(struct device *dev, void *res)
  707. {
  708. struct power_supply **psy = res;
  709. power_supply_unregister(*psy);
  710. }
  711. /**
  712. * devm_power_supply_register() - Register managed power supply
  713. * @parent: Device to be a parent of power supply's device, usually
  714. * the device which probe function calls this
  715. * @desc: Description of power supply, must be valid through whole
  716. * lifetime of this power supply
  717. * @cfg: Run-time specific configuration accessed during registering,
  718. * may be NULL
  719. *
  720. * Return: A pointer to newly allocated power_supply on success
  721. * or ERR_PTR otherwise.
  722. * The returned power_supply pointer will be automatically unregistered
  723. * on driver detach.
  724. */
  725. struct power_supply *__must_check
  726. devm_power_supply_register(struct device *parent,
  727. const struct power_supply_desc *desc,
  728. const struct power_supply_config *cfg)
  729. {
  730. struct power_supply **ptr, *psy;
  731. ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
  732. if (!ptr)
  733. return ERR_PTR(-ENOMEM);
  734. psy = __power_supply_register(parent, desc, cfg, true);
  735. if (IS_ERR(psy)) {
  736. devres_free(ptr);
  737. } else {
  738. *ptr = psy;
  739. devres_add(parent, ptr);
  740. }
  741. return psy;
  742. }
  743. EXPORT_SYMBOL_GPL(devm_power_supply_register);
  744. /**
  745. * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
  746. * @parent: Device to be a parent of power supply's device, usually
  747. * the device which probe function calls this
  748. * @desc: Description of power supply, must be valid through whole
  749. * lifetime of this power supply
  750. * @cfg: Run-time specific configuration accessed during registering,
  751. * may be NULL
  752. *
  753. * Return: A pointer to newly allocated power_supply on success
  754. * or ERR_PTR otherwise.
  755. * The returned power_supply pointer will be automatically unregistered
  756. * on driver detach.
  757. */
  758. struct power_supply *__must_check
  759. devm_power_supply_register_no_ws(struct device *parent,
  760. const struct power_supply_desc *desc,
  761. const struct power_supply_config *cfg)
  762. {
  763. struct power_supply **ptr, *psy;
  764. ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
  765. if (!ptr)
  766. return ERR_PTR(-ENOMEM);
  767. psy = __power_supply_register(parent, desc, cfg, false);
  768. if (IS_ERR(psy)) {
  769. devres_free(ptr);
  770. } else {
  771. *ptr = psy;
  772. devres_add(parent, ptr);
  773. }
  774. return psy;
  775. }
  776. EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
  777. /**
  778. * power_supply_unregister() - Remove this power supply from system
  779. * @psy: Pointer to power supply to unregister
  780. *
  781. * Remove this power supply from the system. The resources of power supply
  782. * will be freed here or on last power_supply_put() call.
  783. */
  784. void power_supply_unregister(struct power_supply *psy)
  785. {
  786. WARN_ON(atomic_dec_return(&psy->use_cnt));
  787. cancel_work_sync(&psy->changed_work);
  788. cancel_delayed_work_sync(&psy->deferred_register_work);
  789. sysfs_remove_link(&psy->dev.kobj, "powers");
  790. power_supply_remove_triggers(psy);
  791. psy_unregister_cooler(psy);
  792. psy_unregister_thermal(psy);
  793. device_init_wakeup(&psy->dev, false);
  794. device_unregister(&psy->dev);
  795. }
  796. EXPORT_SYMBOL_GPL(power_supply_unregister);
  797. void *power_supply_get_drvdata(struct power_supply *psy)
  798. {
  799. return psy->drv_data;
  800. }
  801. EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
  802. static int __init power_supply_class_init(void)
  803. {
  804. power_supply_class = class_create(THIS_MODULE, "power_supply");
  805. if (IS_ERR(power_supply_class))
  806. return PTR_ERR(power_supply_class);
  807. power_supply_class->dev_uevent = power_supply_uevent;
  808. power_supply_init_attrs(&power_supply_dev_type);
  809. return 0;
  810. }
  811. static void __exit power_supply_class_exit(void)
  812. {
  813. class_destroy(power_supply_class);
  814. }
  815. subsys_initcall(power_supply_class_init);
  816. module_exit(power_supply_class_exit);
  817. MODULE_DESCRIPTION("Universal power supply monitor class");
  818. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  819. "Szabolcs Gyurko, "
  820. "Anton Vorontsov <cbou@mail.ru>");
  821. MODULE_LICENSE("GPL");