power_supply_core.c 25 KB

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