power_supply_core.c 29 KB

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