power_supply_core.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. struct psy_am_i_supplied_data {
  234. struct power_supply *psy;
  235. unsigned int count;
  236. };
  237. static int __power_supply_am_i_supplied(struct device *dev, void *_data)
  238. {
  239. union power_supply_propval ret = {0,};
  240. struct power_supply *epsy = dev_get_drvdata(dev);
  241. struct psy_am_i_supplied_data *data = _data;
  242. data->count++;
  243. if (__power_supply_is_supplied_by(epsy, data->psy))
  244. if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
  245. &ret))
  246. return ret.intval;
  247. return 0;
  248. }
  249. int power_supply_am_i_supplied(struct power_supply *psy)
  250. {
  251. struct psy_am_i_supplied_data data = { psy, 0 };
  252. int error;
  253. error = class_for_each_device(power_supply_class, NULL, &data,
  254. __power_supply_am_i_supplied);
  255. dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
  256. if (data.count == 0)
  257. return -ENODEV;
  258. return error;
  259. }
  260. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  261. static int __power_supply_is_system_supplied(struct device *dev, void *data)
  262. {
  263. union power_supply_propval ret = {0,};
  264. struct power_supply *psy = dev_get_drvdata(dev);
  265. unsigned int *count = data;
  266. (*count)++;
  267. if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
  268. if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
  269. &ret))
  270. return ret.intval;
  271. return 0;
  272. }
  273. int power_supply_is_system_supplied(void)
  274. {
  275. int error;
  276. unsigned int count = 0;
  277. error = class_for_each_device(power_supply_class, NULL, &count,
  278. __power_supply_is_system_supplied);
  279. /*
  280. * If no power class device was found at all, most probably we are
  281. * running on a desktop system, so assume we are on mains power.
  282. */
  283. if (count == 0)
  284. return 1;
  285. return error;
  286. }
  287. EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
  288. int power_supply_set_battery_charged(struct power_supply *psy)
  289. {
  290. if (atomic_read(&psy->use_cnt) >= 0 &&
  291. psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
  292. psy->desc->set_charged) {
  293. psy->desc->set_charged(psy);
  294. return 0;
  295. }
  296. return -EINVAL;
  297. }
  298. EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
  299. static int power_supply_match_device_by_name(struct device *dev, const void *data)
  300. {
  301. const char *name = data;
  302. struct power_supply *psy = dev_get_drvdata(dev);
  303. return strcmp(psy->desc->name, name) == 0;
  304. }
  305. /**
  306. * power_supply_get_by_name() - Search for a power supply and returns its ref
  307. * @name: Power supply name to fetch
  308. *
  309. * If power supply was found, it increases reference count for the
  310. * internal power supply's device. The user should power_supply_put()
  311. * after usage.
  312. *
  313. * Return: On success returns a reference to a power supply with
  314. * matching name equals to @name, a NULL otherwise.
  315. */
  316. struct power_supply *power_supply_get_by_name(const char *name)
  317. {
  318. struct power_supply *psy = NULL;
  319. struct device *dev = class_find_device(power_supply_class, NULL, name,
  320. power_supply_match_device_by_name);
  321. if (dev) {
  322. psy = dev_get_drvdata(dev);
  323. atomic_inc(&psy->use_cnt);
  324. }
  325. return psy;
  326. }
  327. EXPORT_SYMBOL_GPL(power_supply_get_by_name);
  328. /**
  329. * power_supply_put() - Drop reference obtained with power_supply_get_by_name
  330. * @psy: Reference to put
  331. *
  332. * The reference to power supply should be put before unregistering
  333. * the power supply.
  334. */
  335. void power_supply_put(struct power_supply *psy)
  336. {
  337. might_sleep();
  338. atomic_dec(&psy->use_cnt);
  339. put_device(&psy->dev);
  340. }
  341. EXPORT_SYMBOL_GPL(power_supply_put);
  342. #ifdef CONFIG_OF
  343. static int power_supply_match_device_node(struct device *dev, const void *data)
  344. {
  345. return dev->parent && dev->parent->of_node == data;
  346. }
  347. /**
  348. * power_supply_get_by_phandle() - Search for a power supply and returns its ref
  349. * @np: Pointer to device node holding phandle property
  350. * @property: Name of property holding a power supply name
  351. *
  352. * If power supply was found, it increases reference count for the
  353. * internal power supply's device. The user should power_supply_put()
  354. * after usage.
  355. *
  356. * Return: On success returns a reference to a power supply with
  357. * matching name equals to value under @property, NULL or ERR_PTR otherwise.
  358. */
  359. struct power_supply *power_supply_get_by_phandle(struct device_node *np,
  360. const char *property)
  361. {
  362. struct device_node *power_supply_np;
  363. struct power_supply *psy = NULL;
  364. struct device *dev;
  365. power_supply_np = of_parse_phandle(np, property, 0);
  366. if (!power_supply_np)
  367. return ERR_PTR(-ENODEV);
  368. dev = class_find_device(power_supply_class, NULL, power_supply_np,
  369. power_supply_match_device_node);
  370. of_node_put(power_supply_np);
  371. if (dev) {
  372. psy = dev_get_drvdata(dev);
  373. atomic_inc(&psy->use_cnt);
  374. }
  375. return psy;
  376. }
  377. EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
  378. static void devm_power_supply_put(struct device *dev, void *res)
  379. {
  380. struct power_supply **psy = res;
  381. power_supply_put(*psy);
  382. }
  383. /**
  384. * devm_power_supply_get_by_phandle() - Resource managed version of
  385. * power_supply_get_by_phandle()
  386. * @dev: Pointer to device holding phandle property
  387. * @property: Name of property holding a power supply phandle
  388. *
  389. * Return: On success returns a reference to a power supply with
  390. * matching name equals to value under @property, NULL or ERR_PTR otherwise.
  391. */
  392. struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
  393. const char *property)
  394. {
  395. struct power_supply **ptr, *psy;
  396. if (!dev->of_node)
  397. return ERR_PTR(-ENODEV);
  398. ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
  399. if (!ptr)
  400. return ERR_PTR(-ENOMEM);
  401. psy = power_supply_get_by_phandle(dev->of_node, property);
  402. if (IS_ERR_OR_NULL(psy)) {
  403. devres_free(ptr);
  404. } else {
  405. *ptr = psy;
  406. devres_add(dev, ptr);
  407. }
  408. return psy;
  409. }
  410. EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
  411. #endif /* CONFIG_OF */
  412. int power_supply_get_property(struct power_supply *psy,
  413. enum power_supply_property psp,
  414. union power_supply_propval *val)
  415. {
  416. if (atomic_read(&psy->use_cnt) <= 0) {
  417. if (!psy->initialized)
  418. return -EAGAIN;
  419. return -ENODEV;
  420. }
  421. return psy->desc->get_property(psy, psp, val);
  422. }
  423. EXPORT_SYMBOL_GPL(power_supply_get_property);
  424. int power_supply_set_property(struct power_supply *psy,
  425. enum power_supply_property psp,
  426. const union power_supply_propval *val)
  427. {
  428. if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
  429. return -ENODEV;
  430. return psy->desc->set_property(psy, psp, val);
  431. }
  432. EXPORT_SYMBOL_GPL(power_supply_set_property);
  433. int power_supply_property_is_writeable(struct power_supply *psy,
  434. enum power_supply_property psp)
  435. {
  436. if (atomic_read(&psy->use_cnt) <= 0 ||
  437. !psy->desc->property_is_writeable)
  438. return -ENODEV;
  439. return psy->desc->property_is_writeable(psy, psp);
  440. }
  441. EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
  442. void power_supply_external_power_changed(struct power_supply *psy)
  443. {
  444. if (atomic_read(&psy->use_cnt) <= 0 ||
  445. !psy->desc->external_power_changed)
  446. return;
  447. psy->desc->external_power_changed(psy);
  448. }
  449. EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
  450. int power_supply_powers(struct power_supply *psy, struct device *dev)
  451. {
  452. return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
  453. }
  454. EXPORT_SYMBOL_GPL(power_supply_powers);
  455. static void power_supply_dev_release(struct device *dev)
  456. {
  457. struct power_supply *psy = container_of(dev, struct power_supply, dev);
  458. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  459. kfree(psy);
  460. }
  461. int power_supply_reg_notifier(struct notifier_block *nb)
  462. {
  463. return atomic_notifier_chain_register(&power_supply_notifier, nb);
  464. }
  465. EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
  466. void power_supply_unreg_notifier(struct notifier_block *nb)
  467. {
  468. atomic_notifier_chain_unregister(&power_supply_notifier, nb);
  469. }
  470. EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
  471. #ifdef CONFIG_THERMAL
  472. static int power_supply_read_temp(struct thermal_zone_device *tzd,
  473. int *temp)
  474. {
  475. struct power_supply *psy;
  476. union power_supply_propval val;
  477. int ret;
  478. WARN_ON(tzd == NULL);
  479. psy = tzd->devdata;
  480. ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
  481. if (ret)
  482. return ret;
  483. /* Convert tenths of degree Celsius to milli degree Celsius. */
  484. *temp = val.intval * 100;
  485. return ret;
  486. }
  487. static struct thermal_zone_device_ops psy_tzd_ops = {
  488. .get_temp = power_supply_read_temp,
  489. };
  490. static int psy_register_thermal(struct power_supply *psy)
  491. {
  492. int i;
  493. if (psy->desc->no_thermal)
  494. return 0;
  495. /* Register battery zone device psy reports temperature */
  496. for (i = 0; i < psy->desc->num_properties; i++) {
  497. if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
  498. psy->tzd = thermal_zone_device_register(psy->desc->name,
  499. 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
  500. return PTR_ERR_OR_ZERO(psy->tzd);
  501. }
  502. }
  503. return 0;
  504. }
  505. static void psy_unregister_thermal(struct power_supply *psy)
  506. {
  507. if (IS_ERR_OR_NULL(psy->tzd))
  508. return;
  509. thermal_zone_device_unregister(psy->tzd);
  510. }
  511. /* thermal cooling device callbacks */
  512. static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
  513. unsigned long *state)
  514. {
  515. struct power_supply *psy;
  516. union power_supply_propval val;
  517. int ret;
  518. psy = tcd->devdata;
  519. ret = power_supply_get_property(psy,
  520. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
  521. if (ret)
  522. return ret;
  523. *state = val.intval;
  524. return ret;
  525. }
  526. static int ps_get_cur_chrage_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. ret = power_supply_get_property(psy,
  534. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  535. if (ret)
  536. return ret;
  537. *state = val.intval;
  538. return ret;
  539. }
  540. static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
  541. unsigned long state)
  542. {
  543. struct power_supply *psy;
  544. union power_supply_propval val;
  545. int ret;
  546. psy = tcd->devdata;
  547. val.intval = state;
  548. ret = psy->desc->set_property(psy,
  549. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  550. return ret;
  551. }
  552. static struct thermal_cooling_device_ops psy_tcd_ops = {
  553. .get_max_state = ps_get_max_charge_cntl_limit,
  554. .get_cur_state = ps_get_cur_chrage_cntl_limit,
  555. .set_cur_state = ps_set_cur_charge_cntl_limit,
  556. };
  557. static int psy_register_cooler(struct power_supply *psy)
  558. {
  559. int i;
  560. /* Register for cooling device if psy can control charging */
  561. for (i = 0; i < psy->desc->num_properties; i++) {
  562. if (psy->desc->properties[i] ==
  563. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
  564. psy->tcd = thermal_cooling_device_register(
  565. (char *)psy->desc->name,
  566. psy, &psy_tcd_ops);
  567. return PTR_ERR_OR_ZERO(psy->tcd);
  568. }
  569. }
  570. return 0;
  571. }
  572. static void psy_unregister_cooler(struct power_supply *psy)
  573. {
  574. if (IS_ERR_OR_NULL(psy->tcd))
  575. return;
  576. thermal_cooling_device_unregister(psy->tcd);
  577. }
  578. #else
  579. static int psy_register_thermal(struct power_supply *psy)
  580. {
  581. return 0;
  582. }
  583. static void psy_unregister_thermal(struct power_supply *psy)
  584. {
  585. }
  586. static int psy_register_cooler(struct power_supply *psy)
  587. {
  588. return 0;
  589. }
  590. static void psy_unregister_cooler(struct power_supply *psy)
  591. {
  592. }
  593. #endif
  594. static struct power_supply *__must_check
  595. __power_supply_register(struct device *parent,
  596. const struct power_supply_desc *desc,
  597. const struct power_supply_config *cfg,
  598. bool ws)
  599. {
  600. struct device *dev;
  601. struct power_supply *psy;
  602. int rc;
  603. if (!parent)
  604. pr_warn("%s: Expected proper parent device for '%s'\n",
  605. __func__, desc->name);
  606. psy = kzalloc(sizeof(*psy), GFP_KERNEL);
  607. if (!psy)
  608. return ERR_PTR(-ENOMEM);
  609. dev = &psy->dev;
  610. device_initialize(dev);
  611. dev->class = power_supply_class;
  612. dev->type = &power_supply_dev_type;
  613. dev->parent = parent;
  614. dev->release = power_supply_dev_release;
  615. dev_set_drvdata(dev, psy);
  616. psy->desc = desc;
  617. if (cfg) {
  618. psy->drv_data = cfg->drv_data;
  619. psy->of_node = cfg->of_node;
  620. psy->supplied_to = cfg->supplied_to;
  621. psy->num_supplicants = cfg->num_supplicants;
  622. }
  623. rc = dev_set_name(dev, "%s", desc->name);
  624. if (rc)
  625. goto dev_set_name_failed;
  626. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  627. INIT_DELAYED_WORK(&psy->deferred_register_work,
  628. power_supply_deferred_register_work);
  629. rc = power_supply_check_supplies(psy);
  630. if (rc) {
  631. dev_info(dev, "Not all required supplies found, defer probe\n");
  632. goto check_supplies_failed;
  633. }
  634. spin_lock_init(&psy->changed_lock);
  635. rc = device_init_wakeup(dev, ws);
  636. if (rc)
  637. goto wakeup_init_failed;
  638. rc = device_add(dev);
  639. if (rc)
  640. goto device_add_failed;
  641. rc = psy_register_thermal(psy);
  642. if (rc)
  643. goto register_thermal_failed;
  644. rc = psy_register_cooler(psy);
  645. if (rc)
  646. goto register_cooler_failed;
  647. rc = power_supply_create_triggers(psy);
  648. if (rc)
  649. goto create_triggers_failed;
  650. /*
  651. * Update use_cnt after any uevents (most notably from device_add()).
  652. * We are here still during driver's probe but
  653. * the power_supply_uevent() calls back driver's get_property
  654. * method so:
  655. * 1. Driver did not assigned the returned struct power_supply,
  656. * 2. Driver could not finish initialization (anything in its probe
  657. * after calling power_supply_register()).
  658. */
  659. atomic_inc(&psy->use_cnt);
  660. psy->initialized = true;
  661. queue_delayed_work(system_power_efficient_wq,
  662. &psy->deferred_register_work,
  663. POWER_SUPPLY_DEFERRED_REGISTER_TIME);
  664. return psy;
  665. create_triggers_failed:
  666. psy_unregister_cooler(psy);
  667. register_cooler_failed:
  668. psy_unregister_thermal(psy);
  669. register_thermal_failed:
  670. device_del(dev);
  671. device_add_failed:
  672. wakeup_init_failed:
  673. check_supplies_failed:
  674. dev_set_name_failed:
  675. put_device(dev);
  676. return ERR_PTR(rc);
  677. }
  678. /**
  679. * power_supply_register() - Register new power supply
  680. * @parent: Device to be a parent of power supply's device, usually
  681. * the device which probe function calls this
  682. * @desc: Description of power supply, must be valid through whole
  683. * lifetime of this power supply
  684. * @cfg: Run-time specific configuration accessed during registering,
  685. * may be NULL
  686. *
  687. * Return: A pointer to newly allocated power_supply on success
  688. * or ERR_PTR otherwise.
  689. * Use power_supply_unregister() on returned power_supply pointer to release
  690. * resources.
  691. */
  692. struct power_supply *__must_check power_supply_register(struct device *parent,
  693. const struct power_supply_desc *desc,
  694. const struct power_supply_config *cfg)
  695. {
  696. return __power_supply_register(parent, desc, cfg, true);
  697. }
  698. EXPORT_SYMBOL_GPL(power_supply_register);
  699. /**
  700. * power_supply_register_no_ws() - Register new non-waking-source power supply
  701. * @parent: Device to be a parent of power supply's device, usually
  702. * the device which probe function calls this
  703. * @desc: Description of power supply, must be valid through whole
  704. * lifetime of this power supply
  705. * @cfg: Run-time specific configuration accessed during registering,
  706. * may be NULL
  707. *
  708. * Return: A pointer to newly allocated power_supply on success
  709. * or ERR_PTR otherwise.
  710. * Use power_supply_unregister() on returned power_supply pointer to release
  711. * resources.
  712. */
  713. struct power_supply *__must_check
  714. power_supply_register_no_ws(struct device *parent,
  715. const struct power_supply_desc *desc,
  716. const struct power_supply_config *cfg)
  717. {
  718. return __power_supply_register(parent, desc, cfg, false);
  719. }
  720. EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
  721. static void devm_power_supply_release(struct device *dev, void *res)
  722. {
  723. struct power_supply **psy = res;
  724. power_supply_unregister(*psy);
  725. }
  726. /**
  727. * devm_power_supply_register() - Register managed power supply
  728. * @parent: Device to be a parent of power supply's device, usually
  729. * the device which probe function calls this
  730. * @desc: Description of power supply, must be valid through whole
  731. * lifetime of this power supply
  732. * @cfg: Run-time specific configuration accessed during registering,
  733. * may be NULL
  734. *
  735. * Return: A pointer to newly allocated power_supply on success
  736. * or ERR_PTR otherwise.
  737. * The returned power_supply pointer will be automatically unregistered
  738. * on driver detach.
  739. */
  740. struct power_supply *__must_check
  741. devm_power_supply_register(struct device *parent,
  742. const struct power_supply_desc *desc,
  743. const struct power_supply_config *cfg)
  744. {
  745. struct power_supply **ptr, *psy;
  746. ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
  747. if (!ptr)
  748. return ERR_PTR(-ENOMEM);
  749. psy = __power_supply_register(parent, desc, cfg, true);
  750. if (IS_ERR(psy)) {
  751. devres_free(ptr);
  752. } else {
  753. *ptr = psy;
  754. devres_add(parent, ptr);
  755. }
  756. return psy;
  757. }
  758. EXPORT_SYMBOL_GPL(devm_power_supply_register);
  759. /**
  760. * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
  761. * @parent: Device to be a parent of power supply's device, usually
  762. * the device which probe function calls this
  763. * @desc: Description of power supply, must be valid through whole
  764. * lifetime of this power supply
  765. * @cfg: Run-time specific configuration accessed during registering,
  766. * may be NULL
  767. *
  768. * Return: A pointer to newly allocated power_supply on success
  769. * or ERR_PTR otherwise.
  770. * The returned power_supply pointer will be automatically unregistered
  771. * on driver detach.
  772. */
  773. struct power_supply *__must_check
  774. devm_power_supply_register_no_ws(struct device *parent,
  775. const struct power_supply_desc *desc,
  776. const struct power_supply_config *cfg)
  777. {
  778. struct power_supply **ptr, *psy;
  779. ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
  780. if (!ptr)
  781. return ERR_PTR(-ENOMEM);
  782. psy = __power_supply_register(parent, desc, cfg, false);
  783. if (IS_ERR(psy)) {
  784. devres_free(ptr);
  785. } else {
  786. *ptr = psy;
  787. devres_add(parent, ptr);
  788. }
  789. return psy;
  790. }
  791. EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
  792. /**
  793. * power_supply_unregister() - Remove this power supply from system
  794. * @psy: Pointer to power supply to unregister
  795. *
  796. * Remove this power supply from the system. The resources of power supply
  797. * will be freed here or on last power_supply_put() call.
  798. */
  799. void power_supply_unregister(struct power_supply *psy)
  800. {
  801. WARN_ON(atomic_dec_return(&psy->use_cnt));
  802. cancel_work_sync(&psy->changed_work);
  803. cancel_delayed_work_sync(&psy->deferred_register_work);
  804. sysfs_remove_link(&psy->dev.kobj, "powers");
  805. power_supply_remove_triggers(psy);
  806. psy_unregister_cooler(psy);
  807. psy_unregister_thermal(psy);
  808. device_init_wakeup(&psy->dev, false);
  809. device_unregister(&psy->dev);
  810. }
  811. EXPORT_SYMBOL_GPL(power_supply_unregister);
  812. void *power_supply_get_drvdata(struct power_supply *psy)
  813. {
  814. return psy->drv_data;
  815. }
  816. EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
  817. static int __init power_supply_class_init(void)
  818. {
  819. power_supply_class = class_create(THIS_MODULE, "power_supply");
  820. if (IS_ERR(power_supply_class))
  821. return PTR_ERR(power_supply_class);
  822. power_supply_class->dev_uevent = power_supply_uevent;
  823. power_supply_init_attrs(&power_supply_dev_type);
  824. return 0;
  825. }
  826. static void __exit power_supply_class_exit(void)
  827. {
  828. class_destroy(power_supply_class);
  829. }
  830. subsys_initcall(power_supply_class_init);
  831. module_exit(power_supply_class_exit);
  832. MODULE_DESCRIPTION("Universal power supply monitor class");
  833. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  834. "Szabolcs Gyurko, "
  835. "Anton Vorontsov <cbou@mail.ru>");
  836. MODULE_LICENSE("GPL");