power_supply_core.c 29 KB

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