power_supply_core.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Universal power supply monitor class
  4. *
  5. * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
  6. * Copyright © 2004 Szabolcs Gyurko
  7. * Copyright © 2003 Ian Molton <spyro@f2s.com>
  8. *
  9. * Modified: 2004, Oct Szabolcs Gyurko
  10. */
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/notifier.h>
  18. #include <linux/err.h>
  19. #include <linux/of.h>
  20. #include <linux/power_supply.h>
  21. #include <linux/property.h>
  22. #include <linux/thermal.h>
  23. #include "power_supply.h"
  24. /* exported for the APM Power driver, APM emulation */
  25. struct class *power_supply_class;
  26. EXPORT_SYMBOL_GPL(power_supply_class);
  27. ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
  28. EXPORT_SYMBOL_GPL(power_supply_notifier);
  29. static struct device_type power_supply_dev_type;
  30. struct match_device_node_array_param {
  31. struct device_node *parent_of_node;
  32. struct power_supply **psy;
  33. ssize_t psy_size;
  34. ssize_t psy_count;
  35. };
  36. #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
  37. static bool __power_supply_is_supplied_by(struct power_supply *supplier,
  38. struct power_supply *supply)
  39. {
  40. int i;
  41. if (!supply->supplied_from && !supplier->supplied_to)
  42. return false;
  43. /* Support both supplied_to and supplied_from modes */
  44. if (supply->supplied_from) {
  45. if (!supplier->desc->name)
  46. return false;
  47. for (i = 0; i < supply->num_supplies; i++)
  48. if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
  49. return true;
  50. } else {
  51. if (!supply->desc->name)
  52. return false;
  53. for (i = 0; i < supplier->num_supplicants; i++)
  54. if (!strcmp(supplier->supplied_to[i], supply->desc->name))
  55. return true;
  56. }
  57. return false;
  58. }
  59. static int __power_supply_changed_work(struct device *dev, void *data)
  60. {
  61. struct power_supply *psy = data;
  62. struct power_supply *pst = dev_get_drvdata(dev);
  63. if (__power_supply_is_supplied_by(psy, pst)) {
  64. if (pst->desc->external_power_changed)
  65. pst->desc->external_power_changed(pst);
  66. }
  67. return 0;
  68. }
  69. static void power_supply_changed_work(struct work_struct *work)
  70. {
  71. unsigned long flags;
  72. struct power_supply *psy = container_of(work, struct power_supply,
  73. changed_work);
  74. dev_dbg(&psy->dev, "%s\n", __func__);
  75. spin_lock_irqsave(&psy->changed_lock, flags);
  76. /*
  77. * Check 'changed' here to avoid issues due to race between
  78. * power_supply_changed() and this routine. In worst case
  79. * power_supply_changed() can be called again just before we take above
  80. * lock. During the first call of this routine we will mark 'changed' as
  81. * false and it will stay false for the next call as well.
  82. */
  83. if (likely(psy->changed)) {
  84. psy->changed = false;
  85. spin_unlock_irqrestore(&psy->changed_lock, flags);
  86. class_for_each_device(power_supply_class, NULL, psy,
  87. __power_supply_changed_work);
  88. power_supply_update_leds(psy);
  89. atomic_notifier_call_chain(&power_supply_notifier,
  90. PSY_EVENT_PROP_CHANGED, psy);
  91. kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
  92. spin_lock_irqsave(&psy->changed_lock, flags);
  93. }
  94. /*
  95. * Hold the wakeup_source until all events are processed.
  96. * power_supply_changed() might have called again and have set 'changed'
  97. * to true.
  98. */
  99. if (likely(!psy->changed))
  100. pm_relax(&psy->dev);
  101. spin_unlock_irqrestore(&psy->changed_lock, flags);
  102. }
  103. void power_supply_changed(struct power_supply *psy)
  104. {
  105. unsigned long flags;
  106. dev_dbg(&psy->dev, "%s\n", __func__);
  107. spin_lock_irqsave(&psy->changed_lock, flags);
  108. psy->changed = true;
  109. pm_stay_awake(&psy->dev);
  110. spin_unlock_irqrestore(&psy->changed_lock, flags);
  111. schedule_work(&psy->changed_work);
  112. }
  113. EXPORT_SYMBOL_GPL(power_supply_changed);
  114. static int psy_register_cooler(struct power_supply *psy);
  115. /*
  116. * Notify that power supply was registered after parent finished the probing.
  117. *
  118. * Often power supply is registered from driver's probe function. However
  119. * calling power_supply_changed() directly from power_supply_register()
  120. * would lead to execution of get_property() function provided by the driver
  121. * too early - before the probe ends.
  122. * Also, registering cooling device from the probe will execute the
  123. * get_property() function. So register the cooling device after the probe.
  124. *
  125. * Avoid that by waiting on parent's mutex.
  126. */
  127. static void power_supply_deferred_register_work(struct work_struct *work)
  128. {
  129. struct power_supply *psy = container_of(work, struct power_supply,
  130. deferred_register_work.work);
  131. if (psy->dev.parent) {
  132. while (!mutex_trylock(&psy->dev.parent->mutex)) {
  133. if (psy->removing)
  134. return;
  135. msleep(10);
  136. }
  137. }
  138. power_supply_changed(psy);
  139. psy_register_cooler(psy);
  140. if (psy->dev.parent)
  141. mutex_unlock(&psy->dev.parent->mutex);
  142. }
  143. #ifdef CONFIG_OF
  144. static int __power_supply_populate_supplied_from(struct device *dev,
  145. void *data)
  146. {
  147. struct power_supply *psy = data;
  148. struct power_supply *epsy = dev_get_drvdata(dev);
  149. struct device_node *np;
  150. int i = 0;
  151. do {
  152. np = of_parse_phandle(psy->of_node, "power-supplies", i++);
  153. if (!np)
  154. break;
  155. if (np == epsy->of_node) {
  156. dev_info(&psy->dev, "%s: Found supply : %s\n",
  157. psy->desc->name, epsy->desc->name);
  158. psy->supplied_from[i-1] = (char *)epsy->desc->name;
  159. psy->num_supplies++;
  160. of_node_put(np);
  161. break;
  162. }
  163. of_node_put(np);
  164. } while (np);
  165. return 0;
  166. }
  167. static int power_supply_populate_supplied_from(struct power_supply *psy)
  168. {
  169. int error;
  170. error = class_for_each_device(power_supply_class, NULL, psy,
  171. __power_supply_populate_supplied_from);
  172. dev_dbg(&psy->dev, "%s %d\n", __func__, error);
  173. return error;
  174. }
  175. static int __power_supply_find_supply_from_node(struct device *dev,
  176. void *data)
  177. {
  178. struct device_node *np = data;
  179. struct power_supply *epsy = dev_get_drvdata(dev);
  180. /* returning non-zero breaks out of class_for_each_device loop */
  181. if (epsy->of_node == np)
  182. return 1;
  183. return 0;
  184. }
  185. static int power_supply_find_supply_from_node(struct device_node *supply_node)
  186. {
  187. int error;
  188. /*
  189. * class_for_each_device() either returns its own errors or values
  190. * returned by __power_supply_find_supply_from_node().
  191. *
  192. * __power_supply_find_supply_from_node() will return 0 (no match)
  193. * or 1 (match).
  194. *
  195. * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
  196. * it returned 0, or error as returned by it.
  197. */
  198. error = class_for_each_device(power_supply_class, NULL, supply_node,
  199. __power_supply_find_supply_from_node);
  200. return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
  201. }
  202. static int power_supply_check_supplies(struct power_supply *psy)
  203. {
  204. struct device_node *np;
  205. int cnt = 0;
  206. /* If there is already a list honor it */
  207. if (psy->supplied_from && psy->num_supplies > 0)
  208. return 0;
  209. /* No device node found, nothing to do */
  210. if (!psy->of_node)
  211. return 0;
  212. do {
  213. int ret;
  214. np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
  215. if (!np)
  216. break;
  217. ret = power_supply_find_supply_from_node(np);
  218. of_node_put(np);
  219. if (ret) {
  220. dev_dbg(&psy->dev, "Failed to find supply!\n");
  221. return ret;
  222. }
  223. } while (np);
  224. /* Missing valid "power-supplies" entries */
  225. if (cnt == 1)
  226. return 0;
  227. /* All supplies found, allocate char ** array for filling */
  228. psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
  229. GFP_KERNEL);
  230. if (!psy->supplied_from)
  231. return -ENOMEM;
  232. *psy->supplied_from = devm_kcalloc(&psy->dev,
  233. cnt - 1, sizeof(char *),
  234. GFP_KERNEL);
  235. if (!*psy->supplied_from)
  236. return -ENOMEM;
  237. return power_supply_populate_supplied_from(psy);
  238. }
  239. #else
  240. static int power_supply_check_supplies(struct power_supply *psy)
  241. {
  242. int nval, ret;
  243. if (!psy->dev.parent)
  244. return 0;
  245. nval = device_property_read_string_array(psy->dev.parent,
  246. "supplied-from", NULL, 0);
  247. if (nval <= 0)
  248. return 0;
  249. psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
  250. sizeof(char *), GFP_KERNEL);
  251. if (!psy->supplied_from)
  252. return -ENOMEM;
  253. ret = device_property_read_string_array(psy->dev.parent,
  254. "supplied-from", (const char **)psy->supplied_from, nval);
  255. if (ret < 0)
  256. return ret;
  257. psy->num_supplies = nval;
  258. return 0;
  259. }
  260. #endif
  261. struct psy_am_i_supplied_data {
  262. struct power_supply *psy;
  263. unsigned int count;
  264. };
  265. static int __power_supply_am_i_supplied(struct device *dev, void *_data)
  266. {
  267. union power_supply_propval ret = {0,};
  268. struct power_supply *epsy = dev_get_drvdata(dev);
  269. struct psy_am_i_supplied_data *data = _data;
  270. if (__power_supply_is_supplied_by(epsy, data->psy)) {
  271. data->count++;
  272. if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
  273. &ret))
  274. return ret.intval;
  275. }
  276. return 0;
  277. }
  278. int power_supply_am_i_supplied(struct power_supply *psy)
  279. {
  280. struct psy_am_i_supplied_data data = { psy, 0 };
  281. int error;
  282. error = class_for_each_device(power_supply_class, NULL, &data,
  283. __power_supply_am_i_supplied);
  284. dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
  285. if (data.count == 0)
  286. return -ENODEV;
  287. return error;
  288. }
  289. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  290. static int __power_supply_is_system_supplied(struct device *dev, void *data)
  291. {
  292. union power_supply_propval ret = {0,};
  293. struct power_supply *psy = dev_get_drvdata(dev);
  294. unsigned int *count = data;
  295. (*count)++;
  296. if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
  297. if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
  298. &ret))
  299. return ret.intval;
  300. return 0;
  301. }
  302. int power_supply_is_system_supplied(void)
  303. {
  304. int error;
  305. unsigned int count = 0;
  306. error = class_for_each_device(power_supply_class, NULL, &count,
  307. __power_supply_is_system_supplied);
  308. /*
  309. * If no power class device was found at all, most probably we are
  310. * running on a desktop system, so assume we are on mains power.
  311. */
  312. if (count == 0)
  313. return 1;
  314. return error;
  315. }
  316. EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
  317. static int __power_supply_get_supplier_max_current(struct device *dev,
  318. void *data)
  319. {
  320. union power_supply_propval ret = {0,};
  321. struct power_supply *epsy = dev_get_drvdata(dev);
  322. struct power_supply *psy = data;
  323. if (__power_supply_is_supplied_by(epsy, psy))
  324. if (!epsy->desc->get_property(epsy,
  325. POWER_SUPPLY_PROP_CURRENT_MAX,
  326. &ret))
  327. return ret.intval;
  328. return 0;
  329. }
  330. int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
  331. {
  332. union power_supply_propval val = {0,};
  333. int curr;
  334. if (!psy->desc->set_property)
  335. return -EINVAL;
  336. /*
  337. * This function is not intended for use with a supply with multiple
  338. * suppliers, we simply pick the first supply to report a non 0
  339. * max-current.
  340. */
  341. curr = class_for_each_device(power_supply_class, NULL, psy,
  342. __power_supply_get_supplier_max_current);
  343. if (curr <= 0)
  344. return (curr == 0) ? -ENODEV : curr;
  345. val.intval = curr;
  346. return psy->desc->set_property(psy,
  347. POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
  348. }
  349. EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
  350. int power_supply_set_battery_charged(struct power_supply *psy)
  351. {
  352. if (atomic_read(&psy->use_cnt) >= 0 &&
  353. psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
  354. psy->desc->set_charged) {
  355. psy->desc->set_charged(psy);
  356. return 0;
  357. }
  358. return -EINVAL;
  359. }
  360. EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
  361. static int power_supply_match_device_by_name(struct device *dev, const void *data)
  362. {
  363. const char *name = data;
  364. struct power_supply *psy = dev_get_drvdata(dev);
  365. return strcmp(psy->desc->name, name) == 0;
  366. }
  367. /**
  368. * power_supply_get_by_name() - Search for a power supply and returns its ref
  369. * @name: Power supply name to fetch
  370. *
  371. * If power supply was found, it increases reference count for the
  372. * internal power supply's device. The user should power_supply_put()
  373. * after usage.
  374. *
  375. * Return: On success returns a reference to a power supply with
  376. * matching name equals to @name, a NULL otherwise.
  377. */
  378. struct power_supply *power_supply_get_by_name(const char *name)
  379. {
  380. struct power_supply *psy = NULL;
  381. struct device *dev = class_find_device(power_supply_class, NULL, name,
  382. power_supply_match_device_by_name);
  383. if (dev) {
  384. psy = dev_get_drvdata(dev);
  385. atomic_inc(&psy->use_cnt);
  386. }
  387. return psy;
  388. }
  389. EXPORT_SYMBOL_GPL(power_supply_get_by_name);
  390. /**
  391. * power_supply_put() - Drop reference obtained with power_supply_get_by_name
  392. * @psy: Reference to put
  393. *
  394. * The reference to power supply should be put before unregistering
  395. * the power supply.
  396. */
  397. void power_supply_put(struct power_supply *psy)
  398. {
  399. might_sleep();
  400. atomic_dec(&psy->use_cnt);
  401. put_device(&psy->dev);
  402. }
  403. EXPORT_SYMBOL_GPL(power_supply_put);
  404. #ifdef CONFIG_OF
  405. static int power_supply_match_device_node(struct device *dev, const void *data)
  406. {
  407. return dev->parent && dev->parent->of_node == data;
  408. }
  409. /**
  410. * power_supply_get_by_phandle() - Search for a power supply and returns its ref
  411. * @np: Pointer to device node holding phandle property
  412. * @property: Name of property holding a power supply name
  413. *
  414. * If power supply was found, it increases reference count for the
  415. * internal power supply's device. The user should power_supply_put()
  416. * after usage.
  417. *
  418. * Return: On success returns a reference to a power supply with
  419. * matching name equals to value under @property, NULL or ERR_PTR otherwise.
  420. */
  421. struct power_supply *power_supply_get_by_phandle(struct device_node *np,
  422. const char *property)
  423. {
  424. struct device_node *power_supply_np;
  425. struct power_supply *psy = NULL;
  426. struct device *dev;
  427. power_supply_np = of_parse_phandle(np, property, 0);
  428. if (!power_supply_np)
  429. return ERR_PTR(-ENODEV);
  430. dev = class_find_device(power_supply_class, NULL, power_supply_np,
  431. power_supply_match_device_node);
  432. of_node_put(power_supply_np);
  433. if (dev) {
  434. psy = dev_get_drvdata(dev);
  435. atomic_inc(&psy->use_cnt);
  436. }
  437. return psy;
  438. }
  439. EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
  440. static int power_supply_match_device_node_array(struct device *dev,
  441. void *data)
  442. {
  443. struct match_device_node_array_param *param =
  444. (struct match_device_node_array_param *)data;
  445. struct power_supply **psy = param->psy;
  446. ssize_t size = param->psy_size;
  447. ssize_t *count = &param->psy_count;
  448. if (!dev->parent || dev->parent->of_node != param->parent_of_node)
  449. return 0;
  450. if (*count >= size)
  451. return -EOVERFLOW;
  452. psy[*count] = dev_get_drvdata(dev);
  453. atomic_inc(&psy[*count]->use_cnt);
  454. (*count)++;
  455. return 0;
  456. }
  457. /**
  458. * power_supply_get_by_phandle_array() - Similar to
  459. * power_supply_get_by_phandle but returns an array of power supply
  460. * objects which are associated with the phandle.
  461. * @np: Pointer to device node holding phandle property.
  462. * @property: Name of property holding a power supply name.
  463. * @psy: Array of power_supply pointers provided by the client which is
  464. * filled by power_supply_get_by_phandle_array.
  465. * @size: size of power_supply pointer array.
  466. *
  467. * If power supply was found, it increases reference count for the
  468. * internal power supply's device. The user should power_supply_put()
  469. * after usage.
  470. *
  471. * Return: On success returns the number of power supply objects filled
  472. * in the @psy array.
  473. * -EOVERFLOW when size of @psy array is not suffice.
  474. * -EINVAL when @psy is NULL or @size is 0.
  475. * -ENODEV when matching device_node is not found.
  476. */
  477. int power_supply_get_by_phandle_array(struct device_node *np,
  478. const char *property,
  479. struct power_supply **psy,
  480. ssize_t size)
  481. {
  482. struct device_node *power_supply_np;
  483. int ret;
  484. struct match_device_node_array_param param;
  485. if (!psy || !size)
  486. return -EINVAL;
  487. power_supply_np = of_parse_phandle(np, property, 0);
  488. if (!power_supply_np)
  489. return -ENODEV;
  490. param.parent_of_node = power_supply_np;
  491. param.psy = psy;
  492. param.psy_size = size;
  493. param.psy_count = 0;
  494. ret = class_for_each_device(power_supply_class, NULL, &param,
  495. power_supply_match_device_node_array);
  496. of_node_put(power_supply_np);
  497. return param.psy_count;
  498. }
  499. EXPORT_SYMBOL_GPL(power_supply_get_by_phandle_array);
  500. static void devm_power_supply_put(struct device *dev, void *res)
  501. {
  502. struct power_supply **psy = res;
  503. power_supply_put(*psy);
  504. }
  505. /**
  506. * devm_power_supply_get_by_phandle() - Resource managed version of
  507. * power_supply_get_by_phandle()
  508. * @dev: Pointer to device holding phandle property
  509. * @property: Name of property holding a power supply phandle
  510. *
  511. * Return: On success returns a reference to a power supply with
  512. * matching name equals to value under @property, NULL or ERR_PTR otherwise.
  513. */
  514. struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
  515. const char *property)
  516. {
  517. struct power_supply **ptr, *psy;
  518. if (!dev->of_node)
  519. return ERR_PTR(-ENODEV);
  520. ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
  521. if (!ptr)
  522. return ERR_PTR(-ENOMEM);
  523. psy = power_supply_get_by_phandle(dev->of_node, property);
  524. if (IS_ERR_OR_NULL(psy)) {
  525. devres_free(ptr);
  526. } else {
  527. *ptr = psy;
  528. devres_add(dev, ptr);
  529. }
  530. return psy;
  531. }
  532. EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
  533. #endif /* CONFIG_OF */
  534. int power_supply_get_battery_info(struct power_supply *psy,
  535. struct power_supply_battery_info *info)
  536. {
  537. struct power_supply_resistance_temp_table *resist_table;
  538. struct device_node *battery_np;
  539. const char *value;
  540. int err, len, index;
  541. const __be32 *list;
  542. info->energy_full_design_uwh = -EINVAL;
  543. info->charge_full_design_uah = -EINVAL;
  544. info->voltage_min_design_uv = -EINVAL;
  545. info->voltage_max_design_uv = -EINVAL;
  546. info->precharge_current_ua = -EINVAL;
  547. info->charge_term_current_ua = -EINVAL;
  548. info->constant_charge_current_max_ua = -EINVAL;
  549. info->constant_charge_voltage_max_uv = -EINVAL;
  550. info->temp_ambient_alert_min = INT_MIN;
  551. info->temp_ambient_alert_max = INT_MAX;
  552. info->temp_alert_min = INT_MIN;
  553. info->temp_alert_max = INT_MAX;
  554. info->temp_min = INT_MIN;
  555. info->temp_max = INT_MAX;
  556. info->factory_internal_resistance_uohm = -EINVAL;
  557. info->resist_table = NULL;
  558. for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
  559. info->ocv_table[index] = NULL;
  560. info->ocv_temp[index] = -EINVAL;
  561. info->ocv_table_size[index] = -EINVAL;
  562. }
  563. if (!psy->of_node) {
  564. dev_warn(&psy->dev, "%s currently only supports devicetree\n",
  565. __func__);
  566. return -ENXIO;
  567. }
  568. battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
  569. if (!battery_np)
  570. return -ENODEV;
  571. err = of_property_read_string(battery_np, "compatible", &value);
  572. if (err)
  573. goto out_put_node;
  574. if (strcmp("simple-battery", value)) {
  575. err = -ENODEV;
  576. goto out_put_node;
  577. }
  578. /* The property and field names below must correspond to elements
  579. * in enum power_supply_property. For reasoning, see
  580. * Documentation/power/power_supply_class.rst.
  581. */
  582. of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
  583. &info->energy_full_design_uwh);
  584. of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
  585. &info->charge_full_design_uah);
  586. of_property_read_u32(battery_np, "voltage-min-design-microvolt",
  587. &info->voltage_min_design_uv);
  588. of_property_read_u32(battery_np, "voltage-max-design-microvolt",
  589. &info->voltage_max_design_uv);
  590. of_property_read_u32(battery_np, "trickle-charge-current-microamp",
  591. &info->tricklecharge_current_ua);
  592. of_property_read_u32(battery_np, "precharge-current-microamp",
  593. &info->precharge_current_ua);
  594. of_property_read_u32(battery_np, "precharge-upper-limit-microvolt",
  595. &info->precharge_voltage_max_uv);
  596. of_property_read_u32(battery_np, "charge-term-current-microamp",
  597. &info->charge_term_current_ua);
  598. of_property_read_u32(battery_np, "re-charge-voltage-microvolt",
  599. &info->charge_restart_voltage_uv);
  600. of_property_read_u32(battery_np, "over-voltage-threshold-microvolt",
  601. &info->overvoltage_limit_uv);
  602. of_property_read_u32(battery_np, "constant-charge-current-max-microamp",
  603. &info->constant_charge_current_max_ua);
  604. of_property_read_u32(battery_np, "constant-charge-voltage-max-microvolt",
  605. &info->constant_charge_voltage_max_uv);
  606. of_property_read_u32(battery_np, "factory-internal-resistance-micro-ohms",
  607. &info->factory_internal_resistance_uohm);
  608. of_property_read_u32_index(battery_np, "ambient-celsius",
  609. 0, &info->temp_ambient_alert_min);
  610. of_property_read_u32_index(battery_np, "ambient-celsius",
  611. 1, &info->temp_ambient_alert_max);
  612. of_property_read_u32_index(battery_np, "alert-celsius",
  613. 0, &info->temp_alert_min);
  614. of_property_read_u32_index(battery_np, "alert-celsius",
  615. 1, &info->temp_alert_max);
  616. of_property_read_u32_index(battery_np, "operating-range-celsius",
  617. 0, &info->temp_min);
  618. of_property_read_u32_index(battery_np, "operating-range-celsius",
  619. 1, &info->temp_max);
  620. len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
  621. if (len < 0 && len != -EINVAL) {
  622. err = len;
  623. goto out_put_node;
  624. } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {
  625. dev_err(&psy->dev, "Too many temperature values\n");
  626. err = -EINVAL;
  627. goto out_put_node;
  628. } else if (len > 0) {
  629. of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
  630. info->ocv_temp, len);
  631. }
  632. for (index = 0; index < len; index++) {
  633. struct power_supply_battery_ocv_table *table;
  634. char *propname;
  635. int i, tab_len, size;
  636. propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
  637. list = of_get_property(battery_np, propname, &size);
  638. if (!list || !size) {
  639. dev_err(&psy->dev, "failed to get %s\n", propname);
  640. kfree(propname);
  641. power_supply_put_battery_info(psy, info);
  642. err = -EINVAL;
  643. goto out_put_node;
  644. }
  645. kfree(propname);
  646. tab_len = size / (2 * sizeof(__be32));
  647. info->ocv_table_size[index] = tab_len;
  648. table = info->ocv_table[index] =
  649. devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
  650. if (!info->ocv_table[index]) {
  651. power_supply_put_battery_info(psy, info);
  652. err = -ENOMEM;
  653. goto out_put_node;
  654. }
  655. for (i = 0; i < tab_len; i++) {
  656. table[i].ocv = be32_to_cpu(*list);
  657. list++;
  658. table[i].capacity = be32_to_cpu(*list);
  659. list++;
  660. }
  661. }
  662. list = of_get_property(battery_np, "resistance-temp-table", &len);
  663. if (!list || !len)
  664. goto out_put_node;
  665. info->resist_table_size = len / (2 * sizeof(__be32));
  666. resist_table = info->resist_table = devm_kcalloc(&psy->dev,
  667. info->resist_table_size,
  668. sizeof(*resist_table),
  669. GFP_KERNEL);
  670. if (!info->resist_table) {
  671. power_supply_put_battery_info(psy, info);
  672. err = -ENOMEM;
  673. goto out_put_node;
  674. }
  675. for (index = 0; index < info->resist_table_size; index++) {
  676. resist_table[index].temp = be32_to_cpu(*list++);
  677. resist_table[index].resistance = be32_to_cpu(*list++);
  678. }
  679. out_put_node:
  680. of_node_put(battery_np);
  681. return err;
  682. }
  683. EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
  684. void power_supply_put_battery_info(struct power_supply *psy,
  685. struct power_supply_battery_info *info)
  686. {
  687. int i;
  688. for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
  689. if (info->ocv_table[i])
  690. devm_kfree(&psy->dev, info->ocv_table[i]);
  691. }
  692. if (info->resist_table)
  693. devm_kfree(&psy->dev, info->resist_table);
  694. }
  695. EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
  696. /**
  697. * power_supply_temp2resist_simple() - find the battery internal resistance
  698. * percent
  699. * @table: Pointer to battery resistance temperature table
  700. * @table_len: The table length
  701. * @temp: Current temperature
  702. *
  703. * This helper function is used to look up battery internal resistance percent
  704. * according to current temperature value from the resistance temperature table,
  705. * and the table must be ordered descending. Then the actual battery internal
  706. * resistance = the ideal battery internal resistance * percent / 100.
  707. *
  708. * Return: the battery internal resistance percent
  709. */
  710. int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
  711. int table_len, int temp)
  712. {
  713. int i, resist;
  714. for (i = 0; i < table_len; i++)
  715. if (temp > table[i].temp)
  716. break;
  717. if (i > 0 && i < table_len) {
  718. int tmp;
  719. tmp = (table[i - 1].resistance - table[i].resistance) *
  720. (temp - table[i].temp);
  721. tmp /= table[i - 1].temp - table[i].temp;
  722. resist = tmp + table[i].resistance;
  723. } else if (i == 0) {
  724. resist = table[0].resistance;
  725. } else {
  726. resist = table[table_len - 1].resistance;
  727. }
  728. return resist;
  729. }
  730. EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
  731. /**
  732. * power_supply_ocv2cap_simple() - find the battery capacity
  733. * @table: Pointer to battery OCV lookup table
  734. * @table_len: OCV table length
  735. * @ocv: Current OCV value
  736. *
  737. * This helper function is used to look up battery capacity according to
  738. * current OCV value from one OCV table, and the OCV table must be ordered
  739. * descending.
  740. *
  741. * Return: the battery capacity.
  742. */
  743. int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
  744. int table_len, int ocv)
  745. {
  746. int i, cap, tmp;
  747. for (i = 0; i < table_len; i++)
  748. if (ocv > table[i].ocv)
  749. break;
  750. if (i > 0 && i < table_len) {
  751. tmp = (table[i - 1].capacity - table[i].capacity) *
  752. (ocv - table[i].ocv);
  753. tmp /= table[i - 1].ocv - table[i].ocv;
  754. cap = tmp + table[i].capacity;
  755. } else if (i == 0) {
  756. cap = table[0].capacity;
  757. } else {
  758. cap = table[table_len - 1].capacity;
  759. }
  760. return cap;
  761. }
  762. EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);
  763. struct power_supply_battery_ocv_table *
  764. power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
  765. int temp, int *table_len)
  766. {
  767. int best_temp_diff = INT_MAX, temp_diff;
  768. u8 i, best_index = 0;
  769. if (!info->ocv_table[0])
  770. return NULL;
  771. for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
  772. /* Out of capacity tables */
  773. if (!info->ocv_table[i])
  774. break;
  775. temp_diff = abs(info->ocv_temp[i] - temp);
  776. if (temp_diff < best_temp_diff) {
  777. best_temp_diff = temp_diff;
  778. best_index = i;
  779. }
  780. }
  781. *table_len = info->ocv_table_size[best_index];
  782. return info->ocv_table[best_index];
  783. }
  784. EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);
  785. int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
  786. int ocv, int temp)
  787. {
  788. struct power_supply_battery_ocv_table *table;
  789. int table_len;
  790. table = power_supply_find_ocv2cap_table(info, temp, &table_len);
  791. if (!table)
  792. return -EINVAL;
  793. return power_supply_ocv2cap_simple(table, table_len, ocv);
  794. }
  795. EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);
  796. int power_supply_get_property(struct power_supply *psy,
  797. enum power_supply_property psp,
  798. union power_supply_propval *val)
  799. {
  800. if (atomic_read(&psy->use_cnt) <= 0) {
  801. if (!psy->initialized)
  802. return -EAGAIN;
  803. return -ENODEV;
  804. }
  805. return psy->desc->get_property(psy, psp, val);
  806. }
  807. EXPORT_SYMBOL_GPL(power_supply_get_property);
  808. int power_supply_set_property(struct power_supply *psy,
  809. enum power_supply_property psp,
  810. const union power_supply_propval *val)
  811. {
  812. if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
  813. return -ENODEV;
  814. return psy->desc->set_property(psy, psp, val);
  815. }
  816. EXPORT_SYMBOL_GPL(power_supply_set_property);
  817. int power_supply_property_is_writeable(struct power_supply *psy,
  818. enum power_supply_property psp)
  819. {
  820. if (atomic_read(&psy->use_cnt) <= 0 ||
  821. !psy->desc->property_is_writeable)
  822. return -ENODEV;
  823. return psy->desc->property_is_writeable(psy, psp);
  824. }
  825. EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
  826. void power_supply_external_power_changed(struct power_supply *psy)
  827. {
  828. if (atomic_read(&psy->use_cnt) <= 0 ||
  829. !psy->desc->external_power_changed)
  830. return;
  831. psy->desc->external_power_changed(psy);
  832. }
  833. EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
  834. int power_supply_powers(struct power_supply *psy, struct device *dev)
  835. {
  836. return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
  837. }
  838. EXPORT_SYMBOL_GPL(power_supply_powers);
  839. static void power_supply_dev_release(struct device *dev)
  840. {
  841. struct power_supply *psy = to_power_supply(dev);
  842. dev_dbg(dev, "%s\n", __func__);
  843. kfree(psy);
  844. }
  845. int power_supply_reg_notifier(struct notifier_block *nb)
  846. {
  847. return atomic_notifier_chain_register(&power_supply_notifier, nb);
  848. }
  849. EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
  850. void power_supply_unreg_notifier(struct notifier_block *nb)
  851. {
  852. atomic_notifier_chain_unregister(&power_supply_notifier, nb);
  853. }
  854. EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
  855. #ifdef CONFIG_THERMAL
  856. static int power_supply_read_temp(struct thermal_zone_device *tzd,
  857. int *temp)
  858. {
  859. struct power_supply *psy;
  860. union power_supply_propval val;
  861. int ret;
  862. WARN_ON(tzd == NULL);
  863. psy = tzd->devdata;
  864. ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
  865. if (ret)
  866. return ret;
  867. /* Convert tenths of degree Celsius to milli degree Celsius. */
  868. *temp = val.intval * 100;
  869. return ret;
  870. }
  871. static struct thermal_zone_device_ops psy_tzd_ops = {
  872. .get_temp = power_supply_read_temp,
  873. };
  874. static int psy_register_thermal(struct power_supply *psy)
  875. {
  876. int i, ret;
  877. if (psy->desc->no_thermal)
  878. return 0;
  879. /* Register battery zone device psy reports temperature */
  880. for (i = 0; i < psy->desc->num_properties; i++) {
  881. if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
  882. psy->tzd = thermal_zone_device_register(psy->desc->name,
  883. 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
  884. if (IS_ERR(psy->tzd))
  885. return PTR_ERR(psy->tzd);
  886. ret = thermal_zone_device_enable(psy->tzd);
  887. if (ret)
  888. thermal_zone_device_unregister(psy->tzd);
  889. return ret;
  890. }
  891. }
  892. return 0;
  893. }
  894. static void psy_unregister_thermal(struct power_supply *psy)
  895. {
  896. if (IS_ERR_OR_NULL(psy->tzd))
  897. return;
  898. thermal_zone_device_unregister(psy->tzd);
  899. }
  900. /* thermal cooling device callbacks */
  901. static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
  902. unsigned long *state)
  903. {
  904. struct power_supply *psy;
  905. union power_supply_propval val;
  906. int ret;
  907. psy = tcd->devdata;
  908. ret = power_supply_get_property(psy,
  909. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
  910. if (ret)
  911. return ret;
  912. *state = val.intval;
  913. return ret;
  914. }
  915. static int ps_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
  916. unsigned long *state)
  917. {
  918. struct power_supply *psy;
  919. union power_supply_propval val;
  920. int ret;
  921. psy = tcd->devdata;
  922. ret = power_supply_get_property(psy,
  923. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  924. if (ret)
  925. return ret;
  926. *state = val.intval;
  927. return ret;
  928. }
  929. static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
  930. unsigned long state)
  931. {
  932. struct power_supply *psy;
  933. union power_supply_propval val;
  934. int ret;
  935. psy = tcd->devdata;
  936. val.intval = state;
  937. ret = psy->desc->set_property(psy,
  938. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  939. return ret;
  940. }
  941. static const struct thermal_cooling_device_ops psy_tcd_ops = {
  942. .get_max_state = ps_get_max_charge_cntl_limit,
  943. .get_cur_state = ps_get_cur_charge_cntl_limit,
  944. .set_cur_state = ps_set_cur_charge_cntl_limit,
  945. };
  946. static int psy_register_cooler(struct power_supply *psy)
  947. {
  948. int i;
  949. /* Register for cooling device if psy can control charging */
  950. for (i = 0; i < psy->desc->num_properties; i++) {
  951. if (psy->desc->properties[i] ==
  952. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
  953. if (psy->dev.parent)
  954. psy->tcd = thermal_of_cooling_device_register(
  955. dev_of_node(psy->dev.parent),
  956. (char *)psy->desc->name,
  957. psy, &psy_tcd_ops);
  958. else
  959. psy->tcd = thermal_cooling_device_register(
  960. (char *)psy->desc->name,
  961. psy, &psy_tcd_ops);
  962. return PTR_ERR_OR_ZERO(psy->tcd);
  963. }
  964. }
  965. return 0;
  966. }
  967. static void psy_unregister_cooler(struct power_supply *psy)
  968. {
  969. if (IS_ERR_OR_NULL(psy->tcd))
  970. return;
  971. thermal_cooling_device_unregister(psy->tcd);
  972. }
  973. #else
  974. static int psy_register_thermal(struct power_supply *psy)
  975. {
  976. return 0;
  977. }
  978. static void psy_unregister_thermal(struct power_supply *psy)
  979. {
  980. }
  981. static int psy_register_cooler(struct power_supply *psy)
  982. {
  983. return 0;
  984. }
  985. static void psy_unregister_cooler(struct power_supply *psy)
  986. {
  987. }
  988. #endif
  989. static struct power_supply *__must_check
  990. __power_supply_register(struct device *parent,
  991. const struct power_supply_desc *desc,
  992. const struct power_supply_config *cfg,
  993. bool ws)
  994. {
  995. struct device *dev;
  996. struct power_supply *psy;
  997. int i, rc;
  998. if (!parent)
  999. pr_warn("%s: Expected proper parent device for '%s'\n",
  1000. __func__, desc->name);
  1001. if (!desc || !desc->name || !desc->properties || !desc->num_properties)
  1002. return ERR_PTR(-EINVAL);
  1003. for (i = 0; i < desc->num_properties; ++i) {
  1004. if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&
  1005. (!desc->usb_types || !desc->num_usb_types))
  1006. return ERR_PTR(-EINVAL);
  1007. }
  1008. psy = kzalloc(sizeof(*psy), GFP_KERNEL);
  1009. if (!psy)
  1010. return ERR_PTR(-ENOMEM);
  1011. dev = &psy->dev;
  1012. device_initialize(dev);
  1013. dev->class = power_supply_class;
  1014. dev->type = &power_supply_dev_type;
  1015. dev->parent = parent;
  1016. dev->release = power_supply_dev_release;
  1017. dev_set_drvdata(dev, psy);
  1018. psy->desc = desc;
  1019. if (cfg) {
  1020. dev->groups = cfg->attr_grp;
  1021. psy->drv_data = cfg->drv_data;
  1022. psy->of_node =
  1023. cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
  1024. psy->supplied_to = cfg->supplied_to;
  1025. psy->num_supplicants = cfg->num_supplicants;
  1026. }
  1027. rc = dev_set_name(dev, "%s", desc->name);
  1028. if (rc)
  1029. goto dev_set_name_failed;
  1030. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  1031. INIT_DELAYED_WORK(&psy->deferred_register_work,
  1032. power_supply_deferred_register_work);
  1033. rc = power_supply_check_supplies(psy);
  1034. if (rc) {
  1035. dev_info(dev, "Not all required supplies found, defer probe\n");
  1036. goto check_supplies_failed;
  1037. }
  1038. spin_lock_init(&psy->changed_lock);
  1039. rc = device_add(dev);
  1040. if (rc)
  1041. goto device_add_failed;
  1042. rc = device_init_wakeup(dev, ws);
  1043. if (rc)
  1044. goto wakeup_init_failed;
  1045. rc = psy_register_thermal(psy);
  1046. if (rc)
  1047. goto register_thermal_failed;
  1048. rc = power_supply_create_triggers(psy);
  1049. if (rc)
  1050. goto create_triggers_failed;
  1051. rc = power_supply_add_hwmon_sysfs(psy);
  1052. if (rc)
  1053. goto add_hwmon_sysfs_failed;
  1054. /*
  1055. * Update use_cnt after any uevents (most notably from device_add()).
  1056. * We are here still during driver's probe but
  1057. * the power_supply_uevent() calls back driver's get_property
  1058. * method so:
  1059. * 1. Driver did not assigned the returned struct power_supply,
  1060. * 2. Driver could not finish initialization (anything in its probe
  1061. * after calling power_supply_register()).
  1062. */
  1063. atomic_inc(&psy->use_cnt);
  1064. psy->initialized = true;
  1065. queue_delayed_work(system_power_efficient_wq,
  1066. &psy->deferred_register_work,
  1067. POWER_SUPPLY_DEFERRED_REGISTER_TIME);
  1068. return psy;
  1069. add_hwmon_sysfs_failed:
  1070. power_supply_remove_triggers(psy);
  1071. create_triggers_failed:
  1072. psy_unregister_thermal(psy);
  1073. register_thermal_failed:
  1074. device_del(dev);
  1075. wakeup_init_failed:
  1076. device_add_failed:
  1077. check_supplies_failed:
  1078. dev_set_name_failed:
  1079. put_device(dev);
  1080. return ERR_PTR(rc);
  1081. }
  1082. /**
  1083. * power_supply_register() - Register new power supply
  1084. * @parent: Device to be a parent of power supply's device, usually
  1085. * the device which probe function calls this
  1086. * @desc: Description of power supply, must be valid through whole
  1087. * lifetime of this power supply
  1088. * @cfg: Run-time specific configuration accessed during registering,
  1089. * may be NULL
  1090. *
  1091. * Return: A pointer to newly allocated power_supply on success
  1092. * or ERR_PTR otherwise.
  1093. * Use power_supply_unregister() on returned power_supply pointer to release
  1094. * resources.
  1095. */
  1096. struct power_supply *__must_check power_supply_register(struct device *parent,
  1097. const struct power_supply_desc *desc,
  1098. const struct power_supply_config *cfg)
  1099. {
  1100. return __power_supply_register(parent, desc, cfg, true);
  1101. }
  1102. EXPORT_SYMBOL_GPL(power_supply_register);
  1103. /**
  1104. * power_supply_register_no_ws() - Register new non-waking-source power supply
  1105. * @parent: Device to be a parent of power supply's device, usually
  1106. * the device which probe function calls this
  1107. * @desc: Description of power supply, must be valid through whole
  1108. * lifetime of this power supply
  1109. * @cfg: Run-time specific configuration accessed during registering,
  1110. * may be NULL
  1111. *
  1112. * Return: A pointer to newly allocated power_supply on success
  1113. * or ERR_PTR otherwise.
  1114. * Use power_supply_unregister() on returned power_supply pointer to release
  1115. * resources.
  1116. */
  1117. struct power_supply *__must_check
  1118. power_supply_register_no_ws(struct device *parent,
  1119. const struct power_supply_desc *desc,
  1120. const struct power_supply_config *cfg)
  1121. {
  1122. return __power_supply_register(parent, desc, cfg, false);
  1123. }
  1124. EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
  1125. static void devm_power_supply_release(struct device *dev, void *res)
  1126. {
  1127. struct power_supply **psy = res;
  1128. power_supply_unregister(*psy);
  1129. }
  1130. /**
  1131. * devm_power_supply_register() - Register managed power supply
  1132. * @parent: Device to be a parent of power supply's device, usually
  1133. * the device which probe function calls this
  1134. * @desc: Description of power supply, must be valid through whole
  1135. * lifetime of this power supply
  1136. * @cfg: Run-time specific configuration accessed during registering,
  1137. * may be NULL
  1138. *
  1139. * Return: A pointer to newly allocated power_supply on success
  1140. * or ERR_PTR otherwise.
  1141. * The returned power_supply pointer will be automatically unregistered
  1142. * on driver detach.
  1143. */
  1144. struct power_supply *__must_check
  1145. devm_power_supply_register(struct device *parent,
  1146. const struct power_supply_desc *desc,
  1147. const struct power_supply_config *cfg)
  1148. {
  1149. struct power_supply **ptr, *psy;
  1150. ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
  1151. if (!ptr)
  1152. return ERR_PTR(-ENOMEM);
  1153. psy = __power_supply_register(parent, desc, cfg, true);
  1154. if (IS_ERR(psy)) {
  1155. devres_free(ptr);
  1156. } else {
  1157. *ptr = psy;
  1158. devres_add(parent, ptr);
  1159. }
  1160. return psy;
  1161. }
  1162. EXPORT_SYMBOL_GPL(devm_power_supply_register);
  1163. /**
  1164. * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
  1165. * @parent: Device to be a parent of power supply's device, usually
  1166. * the device which probe function calls this
  1167. * @desc: Description of power supply, must be valid through whole
  1168. * lifetime of this power supply
  1169. * @cfg: Run-time specific configuration accessed during registering,
  1170. * may be NULL
  1171. *
  1172. * Return: A pointer to newly allocated power_supply on success
  1173. * or ERR_PTR otherwise.
  1174. * The returned power_supply pointer will be automatically unregistered
  1175. * on driver detach.
  1176. */
  1177. struct power_supply *__must_check
  1178. devm_power_supply_register_no_ws(struct device *parent,
  1179. const struct power_supply_desc *desc,
  1180. const struct power_supply_config *cfg)
  1181. {
  1182. struct power_supply **ptr, *psy;
  1183. ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
  1184. if (!ptr)
  1185. return ERR_PTR(-ENOMEM);
  1186. psy = __power_supply_register(parent, desc, cfg, false);
  1187. if (IS_ERR(psy)) {
  1188. devres_free(ptr);
  1189. } else {
  1190. *ptr = psy;
  1191. devres_add(parent, ptr);
  1192. }
  1193. return psy;
  1194. }
  1195. EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
  1196. /**
  1197. * power_supply_unregister() - Remove this power supply from system
  1198. * @psy: Pointer to power supply to unregister
  1199. *
  1200. * Remove this power supply from the system. The resources of power supply
  1201. * will be freed here or on last power_supply_put() call.
  1202. */
  1203. void power_supply_unregister(struct power_supply *psy)
  1204. {
  1205. WARN_ON(atomic_dec_return(&psy->use_cnt));
  1206. psy->removing = true;
  1207. cancel_work_sync(&psy->changed_work);
  1208. cancel_delayed_work_sync(&psy->deferred_register_work);
  1209. sysfs_remove_link(&psy->dev.kobj, "powers");
  1210. power_supply_remove_hwmon_sysfs(psy);
  1211. power_supply_remove_triggers(psy);
  1212. psy_unregister_cooler(psy);
  1213. psy_unregister_thermal(psy);
  1214. device_init_wakeup(&psy->dev, false);
  1215. device_unregister(&psy->dev);
  1216. }
  1217. EXPORT_SYMBOL_GPL(power_supply_unregister);
  1218. void *power_supply_get_drvdata(struct power_supply *psy)
  1219. {
  1220. return psy->drv_data;
  1221. }
  1222. EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
  1223. static int __init power_supply_class_init(void)
  1224. {
  1225. power_supply_class = class_create(THIS_MODULE, "power_supply");
  1226. if (IS_ERR(power_supply_class))
  1227. return PTR_ERR(power_supply_class);
  1228. power_supply_class->dev_uevent = power_supply_uevent;
  1229. power_supply_init_attrs(&power_supply_dev_type);
  1230. return 0;
  1231. }
  1232. static void __exit power_supply_class_exit(void)
  1233. {
  1234. class_destroy(power_supply_class);
  1235. }
  1236. subsys_initcall(power_supply_class_init);
  1237. module_exit(power_supply_class_exit);
  1238. MODULE_DESCRIPTION("Universal power supply monitor class");
  1239. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  1240. "Szabolcs Gyurko, "
  1241. "Anton Vorontsov <cbou@mail.ru>");
  1242. MODULE_LICENSE("GPL");