power_supply_core.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. if (!psy->initialized)
  410. return -EAGAIN;
  411. return -ENODEV;
  412. }
  413. return psy->desc->get_property(psy, psp, val);
  414. }
  415. EXPORT_SYMBOL_GPL(power_supply_get_property);
  416. int power_supply_set_property(struct power_supply *psy,
  417. enum power_supply_property psp,
  418. const union power_supply_propval *val)
  419. {
  420. if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
  421. return -ENODEV;
  422. return psy->desc->set_property(psy, psp, val);
  423. }
  424. EXPORT_SYMBOL_GPL(power_supply_set_property);
  425. int power_supply_property_is_writeable(struct power_supply *psy,
  426. enum power_supply_property psp)
  427. {
  428. if (atomic_read(&psy->use_cnt) <= 0 ||
  429. !psy->desc->property_is_writeable)
  430. return -ENODEV;
  431. return psy->desc->property_is_writeable(psy, psp);
  432. }
  433. EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
  434. void power_supply_external_power_changed(struct power_supply *psy)
  435. {
  436. if (atomic_read(&psy->use_cnt) <= 0 ||
  437. !psy->desc->external_power_changed)
  438. return;
  439. psy->desc->external_power_changed(psy);
  440. }
  441. EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
  442. int power_supply_powers(struct power_supply *psy, struct device *dev)
  443. {
  444. return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
  445. }
  446. EXPORT_SYMBOL_GPL(power_supply_powers);
  447. static void power_supply_dev_release(struct device *dev)
  448. {
  449. struct power_supply *psy = container_of(dev, struct power_supply, dev);
  450. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  451. kfree(psy);
  452. }
  453. int power_supply_reg_notifier(struct notifier_block *nb)
  454. {
  455. return atomic_notifier_chain_register(&power_supply_notifier, nb);
  456. }
  457. EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
  458. void power_supply_unreg_notifier(struct notifier_block *nb)
  459. {
  460. atomic_notifier_chain_unregister(&power_supply_notifier, nb);
  461. }
  462. EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
  463. #ifdef CONFIG_THERMAL
  464. static int power_supply_read_temp(struct thermal_zone_device *tzd,
  465. int *temp)
  466. {
  467. struct power_supply *psy;
  468. union power_supply_propval val;
  469. int ret;
  470. WARN_ON(tzd == NULL);
  471. psy = tzd->devdata;
  472. ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
  473. if (ret)
  474. return ret;
  475. /* Convert tenths of degree Celsius to milli degree Celsius. */
  476. *temp = val.intval * 100;
  477. return ret;
  478. }
  479. static struct thermal_zone_device_ops psy_tzd_ops = {
  480. .get_temp = power_supply_read_temp,
  481. };
  482. static int psy_register_thermal(struct power_supply *psy)
  483. {
  484. int i;
  485. if (psy->desc->no_thermal)
  486. return 0;
  487. /* Register battery zone device psy reports temperature */
  488. for (i = 0; i < psy->desc->num_properties; i++) {
  489. if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
  490. psy->tzd = thermal_zone_device_register(psy->desc->name,
  491. 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
  492. return PTR_ERR_OR_ZERO(psy->tzd);
  493. }
  494. }
  495. return 0;
  496. }
  497. static void psy_unregister_thermal(struct power_supply *psy)
  498. {
  499. if (IS_ERR_OR_NULL(psy->tzd))
  500. return;
  501. thermal_zone_device_unregister(psy->tzd);
  502. }
  503. /* thermal cooling device callbacks */
  504. static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
  505. unsigned long *state)
  506. {
  507. struct power_supply *psy;
  508. union power_supply_propval val;
  509. int ret;
  510. psy = tcd->devdata;
  511. ret = power_supply_get_property(psy,
  512. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
  513. if (ret)
  514. return ret;
  515. *state = val.intval;
  516. return ret;
  517. }
  518. static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
  519. unsigned long *state)
  520. {
  521. struct power_supply *psy;
  522. union power_supply_propval val;
  523. int ret;
  524. psy = tcd->devdata;
  525. ret = power_supply_get_property(psy,
  526. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  527. if (ret)
  528. return ret;
  529. *state = val.intval;
  530. return ret;
  531. }
  532. static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
  533. unsigned long state)
  534. {
  535. struct power_supply *psy;
  536. union power_supply_propval val;
  537. int ret;
  538. psy = tcd->devdata;
  539. val.intval = state;
  540. ret = psy->desc->set_property(psy,
  541. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  542. return ret;
  543. }
  544. static struct thermal_cooling_device_ops psy_tcd_ops = {
  545. .get_max_state = ps_get_max_charge_cntl_limit,
  546. .get_cur_state = ps_get_cur_chrage_cntl_limit,
  547. .set_cur_state = ps_set_cur_charge_cntl_limit,
  548. };
  549. static int psy_register_cooler(struct power_supply *psy)
  550. {
  551. int i;
  552. /* Register for cooling device if psy can control charging */
  553. for (i = 0; i < psy->desc->num_properties; i++) {
  554. if (psy->desc->properties[i] ==
  555. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
  556. psy->tcd = thermal_cooling_device_register(
  557. (char *)psy->desc->name,
  558. psy, &psy_tcd_ops);
  559. return PTR_ERR_OR_ZERO(psy->tcd);
  560. }
  561. }
  562. return 0;
  563. }
  564. static void psy_unregister_cooler(struct power_supply *psy)
  565. {
  566. if (IS_ERR_OR_NULL(psy->tcd))
  567. return;
  568. thermal_cooling_device_unregister(psy->tcd);
  569. }
  570. #else
  571. static int psy_register_thermal(struct power_supply *psy)
  572. {
  573. return 0;
  574. }
  575. static void psy_unregister_thermal(struct power_supply *psy)
  576. {
  577. }
  578. static int psy_register_cooler(struct power_supply *psy)
  579. {
  580. return 0;
  581. }
  582. static void psy_unregister_cooler(struct power_supply *psy)
  583. {
  584. }
  585. #endif
  586. static struct power_supply *__must_check
  587. __power_supply_register(struct device *parent,
  588. const struct power_supply_desc *desc,
  589. const struct power_supply_config *cfg,
  590. bool ws)
  591. {
  592. struct device *dev;
  593. struct power_supply *psy;
  594. int rc;
  595. if (!parent)
  596. pr_warn("%s: Expected proper parent device for '%s'\n",
  597. __func__, desc->name);
  598. psy = kzalloc(sizeof(*psy), GFP_KERNEL);
  599. if (!psy)
  600. return ERR_PTR(-ENOMEM);
  601. dev = &psy->dev;
  602. device_initialize(dev);
  603. dev->class = power_supply_class;
  604. dev->type = &power_supply_dev_type;
  605. dev->parent = parent;
  606. dev->release = power_supply_dev_release;
  607. dev_set_drvdata(dev, psy);
  608. psy->desc = desc;
  609. if (cfg) {
  610. psy->drv_data = cfg->drv_data;
  611. psy->of_node = cfg->of_node;
  612. psy->supplied_to = cfg->supplied_to;
  613. psy->num_supplicants = cfg->num_supplicants;
  614. }
  615. rc = dev_set_name(dev, "%s", desc->name);
  616. if (rc)
  617. goto dev_set_name_failed;
  618. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  619. INIT_DELAYED_WORK(&psy->deferred_register_work,
  620. power_supply_deferred_register_work);
  621. rc = power_supply_check_supplies(psy);
  622. if (rc) {
  623. dev_info(dev, "Not all required supplies found, defer probe\n");
  624. goto check_supplies_failed;
  625. }
  626. spin_lock_init(&psy->changed_lock);
  627. rc = device_init_wakeup(dev, ws);
  628. if (rc)
  629. goto wakeup_init_failed;
  630. rc = device_add(dev);
  631. if (rc)
  632. goto device_add_failed;
  633. rc = psy_register_thermal(psy);
  634. if (rc)
  635. goto register_thermal_failed;
  636. rc = psy_register_cooler(psy);
  637. if (rc)
  638. goto register_cooler_failed;
  639. rc = power_supply_create_triggers(psy);
  640. if (rc)
  641. goto create_triggers_failed;
  642. /*
  643. * Update use_cnt after any uevents (most notably from device_add()).
  644. * We are here still during driver's probe but
  645. * the power_supply_uevent() calls back driver's get_property
  646. * method so:
  647. * 1. Driver did not assigned the returned struct power_supply,
  648. * 2. Driver could not finish initialization (anything in its probe
  649. * after calling power_supply_register()).
  650. */
  651. atomic_inc(&psy->use_cnt);
  652. psy->initialized = true;
  653. queue_delayed_work(system_power_efficient_wq,
  654. &psy->deferred_register_work,
  655. POWER_SUPPLY_DEFERRED_REGISTER_TIME);
  656. return psy;
  657. create_triggers_failed:
  658. psy_unregister_cooler(psy);
  659. register_cooler_failed:
  660. psy_unregister_thermal(psy);
  661. register_thermal_failed:
  662. device_del(dev);
  663. device_add_failed:
  664. wakeup_init_failed:
  665. check_supplies_failed:
  666. dev_set_name_failed:
  667. put_device(dev);
  668. return ERR_PTR(rc);
  669. }
  670. /**
  671. * power_supply_register() - Register new power supply
  672. * @parent: Device to be a parent of power supply's device, usually
  673. * the device which probe function calls this
  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. * Use power_supply_unregister() on returned power_supply pointer to release
  682. * resources.
  683. */
  684. struct power_supply *__must_check power_supply_register(struct device *parent,
  685. const struct power_supply_desc *desc,
  686. const struct power_supply_config *cfg)
  687. {
  688. return __power_supply_register(parent, desc, cfg, true);
  689. }
  690. EXPORT_SYMBOL_GPL(power_supply_register);
  691. /**
  692. * power_supply_register_no_ws() - Register new non-waking-source power supply
  693. * @parent: Device to be a parent of power supply's device, usually
  694. * the device which probe function calls this
  695. * @desc: Description of power supply, must be valid through whole
  696. * lifetime of this power supply
  697. * @cfg: Run-time specific configuration accessed during registering,
  698. * may be NULL
  699. *
  700. * Return: A pointer to newly allocated power_supply on success
  701. * or ERR_PTR otherwise.
  702. * Use power_supply_unregister() on returned power_supply pointer to release
  703. * resources.
  704. */
  705. struct power_supply *__must_check
  706. power_supply_register_no_ws(struct device *parent,
  707. const struct power_supply_desc *desc,
  708. const struct power_supply_config *cfg)
  709. {
  710. return __power_supply_register(parent, desc, cfg, false);
  711. }
  712. EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
  713. static void devm_power_supply_release(struct device *dev, void *res)
  714. {
  715. struct power_supply **psy = res;
  716. power_supply_unregister(*psy);
  717. }
  718. /**
  719. * devm_power_supply_register() - Register managed power supply
  720. * @parent: Device to be a parent of power supply's device, usually
  721. * the device which probe function calls this
  722. * @desc: Description of power supply, must be valid through whole
  723. * lifetime of this power supply
  724. * @cfg: Run-time specific configuration accessed during registering,
  725. * may be NULL
  726. *
  727. * Return: A pointer to newly allocated power_supply on success
  728. * or ERR_PTR otherwise.
  729. * The returned power_supply pointer will be automatically unregistered
  730. * on driver detach.
  731. */
  732. struct power_supply *__must_check
  733. devm_power_supply_register(struct device *parent,
  734. const struct power_supply_desc *desc,
  735. const struct power_supply_config *cfg)
  736. {
  737. struct power_supply **ptr, *psy;
  738. ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
  739. if (!ptr)
  740. return ERR_PTR(-ENOMEM);
  741. psy = __power_supply_register(parent, desc, cfg, true);
  742. if (IS_ERR(psy)) {
  743. devres_free(ptr);
  744. } else {
  745. *ptr = psy;
  746. devres_add(parent, ptr);
  747. }
  748. return psy;
  749. }
  750. EXPORT_SYMBOL_GPL(devm_power_supply_register);
  751. /**
  752. * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
  753. * @parent: Device to be a parent of power supply's device, usually
  754. * the device which probe function calls this
  755. * @desc: Description of power supply, must be valid through whole
  756. * lifetime of this power supply
  757. * @cfg: Run-time specific configuration accessed during registering,
  758. * may be NULL
  759. *
  760. * Return: A pointer to newly allocated power_supply on success
  761. * or ERR_PTR otherwise.
  762. * The returned power_supply pointer will be automatically unregistered
  763. * on driver detach.
  764. */
  765. struct power_supply *__must_check
  766. devm_power_supply_register_no_ws(struct device *parent,
  767. const struct power_supply_desc *desc,
  768. const struct power_supply_config *cfg)
  769. {
  770. struct power_supply **ptr, *psy;
  771. ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
  772. if (!ptr)
  773. return ERR_PTR(-ENOMEM);
  774. psy = __power_supply_register(parent, desc, cfg, false);
  775. if (IS_ERR(psy)) {
  776. devres_free(ptr);
  777. } else {
  778. *ptr = psy;
  779. devres_add(parent, ptr);
  780. }
  781. return psy;
  782. }
  783. EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
  784. /**
  785. * power_supply_unregister() - Remove this power supply from system
  786. * @psy: Pointer to power supply to unregister
  787. *
  788. * Remove this power supply from the system. The resources of power supply
  789. * will be freed here or on last power_supply_put() call.
  790. */
  791. void power_supply_unregister(struct power_supply *psy)
  792. {
  793. WARN_ON(atomic_dec_return(&psy->use_cnt));
  794. cancel_work_sync(&psy->changed_work);
  795. cancel_delayed_work_sync(&psy->deferred_register_work);
  796. sysfs_remove_link(&psy->dev.kobj, "powers");
  797. power_supply_remove_triggers(psy);
  798. psy_unregister_cooler(psy);
  799. psy_unregister_thermal(psy);
  800. device_init_wakeup(&psy->dev, false);
  801. device_unregister(&psy->dev);
  802. }
  803. EXPORT_SYMBOL_GPL(power_supply_unregister);
  804. void *power_supply_get_drvdata(struct power_supply *psy)
  805. {
  806. return psy->drv_data;
  807. }
  808. EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
  809. static int __init power_supply_class_init(void)
  810. {
  811. power_supply_class = class_create(THIS_MODULE, "power_supply");
  812. if (IS_ERR(power_supply_class))
  813. return PTR_ERR(power_supply_class);
  814. power_supply_class->dev_uevent = power_supply_uevent;
  815. power_supply_init_attrs(&power_supply_dev_type);
  816. return 0;
  817. }
  818. static void __exit power_supply_class_exit(void)
  819. {
  820. class_destroy(power_supply_class);
  821. }
  822. subsys_initcall(power_supply_class_init);
  823. module_exit(power_supply_class_exit);
  824. MODULE_DESCRIPTION("Universal power supply monitor class");
  825. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  826. "Szabolcs Gyurko, "
  827. "Anton Vorontsov <cbou@mail.ru>");
  828. MODULE_LICENSE("GPL");