power_supply_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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/err.h>
  18. #include <linux/power_supply.h>
  19. #include <linux/thermal.h>
  20. #include "power_supply.h"
  21. /* exported for the APM Power driver, APM emulation */
  22. struct class *power_supply_class;
  23. EXPORT_SYMBOL_GPL(power_supply_class);
  24. static struct device_type power_supply_dev_type;
  25. static bool __power_supply_is_supplied_by(struct power_supply *supplier,
  26. struct power_supply *supply)
  27. {
  28. int i;
  29. if (!supply->supplied_from && !supplier->supplied_to)
  30. return false;
  31. /* Support both supplied_to and supplied_from modes */
  32. if (supply->supplied_from) {
  33. if (!supplier->name)
  34. return false;
  35. for (i = 0; i < supply->num_supplies; i++)
  36. if (!strcmp(supplier->name, supply->supplied_from[i]))
  37. return true;
  38. } else {
  39. if (!supply->name)
  40. return false;
  41. for (i = 0; i < supplier->num_supplicants; i++)
  42. if (!strcmp(supplier->supplied_to[i], supply->name))
  43. return true;
  44. }
  45. return false;
  46. }
  47. static int __power_supply_changed_work(struct device *dev, void *data)
  48. {
  49. struct power_supply *psy = (struct power_supply *)data;
  50. struct power_supply *pst = dev_get_drvdata(dev);
  51. if (__power_supply_is_supplied_by(psy, pst)) {
  52. if (pst->external_power_changed)
  53. pst->external_power_changed(pst);
  54. }
  55. return 0;
  56. }
  57. static void power_supply_changed_work(struct work_struct *work)
  58. {
  59. struct power_supply *psy = container_of(work, struct power_supply,
  60. changed_work);
  61. dev_dbg(psy->dev, "%s\n", __func__);
  62. class_for_each_device(power_supply_class, NULL, psy,
  63. __power_supply_changed_work);
  64. power_supply_update_leds(psy);
  65. kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
  66. }
  67. void power_supply_changed(struct power_supply *psy)
  68. {
  69. dev_dbg(psy->dev, "%s\n", __func__);
  70. schedule_work(&psy->changed_work);
  71. }
  72. EXPORT_SYMBOL_GPL(power_supply_changed);
  73. #ifdef CONFIG_OF
  74. #include <linux/of.h>
  75. static int __power_supply_populate_supplied_from(struct device *dev,
  76. void *data)
  77. {
  78. struct power_supply *psy = (struct power_supply *)data;
  79. struct power_supply *epsy = dev_get_drvdata(dev);
  80. struct device_node *np;
  81. int i = 0;
  82. do {
  83. np = of_parse_phandle(psy->of_node, "power-supplies", i++);
  84. if (!np)
  85. continue;
  86. if (np == epsy->of_node) {
  87. dev_info(psy->dev, "%s: Found supply : %s\n",
  88. psy->name, epsy->name);
  89. psy->supplied_from[i-1] = (char *)epsy->name;
  90. psy->num_supplies++;
  91. break;
  92. }
  93. } while (np);
  94. return 0;
  95. }
  96. static int power_supply_populate_supplied_from(struct power_supply *psy)
  97. {
  98. int error;
  99. error = class_for_each_device(power_supply_class, NULL, psy,
  100. __power_supply_populate_supplied_from);
  101. dev_dbg(psy->dev, "%s %d\n", __func__, error);
  102. return error;
  103. }
  104. static int __power_supply_find_supply_from_node(struct device *dev,
  105. void *data)
  106. {
  107. struct device_node *np = (struct device_node *)data;
  108. struct power_supply *epsy = dev_get_drvdata(dev);
  109. /* return error breaks out of class_for_each_device loop */
  110. if (epsy->of_node == np)
  111. return -EINVAL;
  112. return 0;
  113. }
  114. static int power_supply_find_supply_from_node(struct device_node *supply_node)
  115. {
  116. int error;
  117. struct device *dev;
  118. struct class_dev_iter iter;
  119. /*
  120. * Use iterator to see if any other device is registered.
  121. * This is required since class_for_each_device returns 0
  122. * if there are no devices registered.
  123. */
  124. class_dev_iter_init(&iter, power_supply_class, NULL, NULL);
  125. dev = class_dev_iter_next(&iter);
  126. if (!dev)
  127. return -EPROBE_DEFER;
  128. /*
  129. * We have to treat the return value as inverted, because if
  130. * we return error on not found, then it won't continue looking.
  131. * So we trick it by returning error on success to stop looking
  132. * once the matching device is found.
  133. */
  134. error = class_for_each_device(power_supply_class, NULL, supply_node,
  135. __power_supply_find_supply_from_node);
  136. return error ? 0 : -EPROBE_DEFER;
  137. }
  138. static int power_supply_check_supplies(struct power_supply *psy)
  139. {
  140. struct device_node *np;
  141. int cnt = 0;
  142. /* If there is already a list honor it */
  143. if (psy->supplied_from && psy->num_supplies > 0)
  144. return 0;
  145. /* No device node found, nothing to do */
  146. if (!psy->of_node)
  147. return 0;
  148. do {
  149. int ret;
  150. np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
  151. if (!np)
  152. continue;
  153. ret = power_supply_find_supply_from_node(np);
  154. if (ret) {
  155. dev_dbg(psy->dev, "Failed to find supply, defer!\n");
  156. return -EPROBE_DEFER;
  157. }
  158. } while (np);
  159. /* All supplies found, allocate char ** array for filling */
  160. psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from),
  161. GFP_KERNEL);
  162. if (!psy->supplied_from) {
  163. dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
  164. return -ENOMEM;
  165. }
  166. *psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * cnt,
  167. GFP_KERNEL);
  168. if (!*psy->supplied_from) {
  169. dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
  170. return -ENOMEM;
  171. }
  172. return power_supply_populate_supplied_from(psy);
  173. }
  174. #else
  175. static inline int power_supply_check_supplies(struct power_supply *psy)
  176. {
  177. return 0;
  178. }
  179. #endif
  180. static int __power_supply_am_i_supplied(struct device *dev, void *data)
  181. {
  182. union power_supply_propval ret = {0,};
  183. struct power_supply *psy = (struct power_supply *)data;
  184. struct power_supply *epsy = dev_get_drvdata(dev);
  185. if (__power_supply_is_supplied_by(epsy, psy))
  186. if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) {
  187. if (ret.intval)
  188. return ret.intval;
  189. }
  190. return 0;
  191. }
  192. int power_supply_am_i_supplied(struct power_supply *psy)
  193. {
  194. int error;
  195. error = class_for_each_device(power_supply_class, NULL, psy,
  196. __power_supply_am_i_supplied);
  197. dev_dbg(psy->dev, "%s %d\n", __func__, error);
  198. return error;
  199. }
  200. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  201. static int __power_supply_is_system_supplied(struct device *dev, void *data)
  202. {
  203. union power_supply_propval ret = {0,};
  204. struct power_supply *psy = dev_get_drvdata(dev);
  205. unsigned int *count = data;
  206. (*count)++;
  207. if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
  208. if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
  209. return 0;
  210. if (ret.intval)
  211. return ret.intval;
  212. }
  213. return 0;
  214. }
  215. int power_supply_is_system_supplied(void)
  216. {
  217. int error;
  218. unsigned int count = 0;
  219. error = class_for_each_device(power_supply_class, NULL, &count,
  220. __power_supply_is_system_supplied);
  221. /*
  222. * If no power class device was found at all, most probably we are
  223. * running on a desktop system, so assume we are on mains power.
  224. */
  225. if (count == 0)
  226. return 1;
  227. return error;
  228. }
  229. EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
  230. int power_supply_set_battery_charged(struct power_supply *psy)
  231. {
  232. if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
  233. psy->set_charged(psy);
  234. return 0;
  235. }
  236. return -EINVAL;
  237. }
  238. EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
  239. static int power_supply_match_device_by_name(struct device *dev, const void *data)
  240. {
  241. const char *name = data;
  242. struct power_supply *psy = dev_get_drvdata(dev);
  243. return strcmp(psy->name, name) == 0;
  244. }
  245. struct power_supply *power_supply_get_by_name(const char *name)
  246. {
  247. struct device *dev = class_find_device(power_supply_class, NULL, name,
  248. power_supply_match_device_by_name);
  249. return dev ? dev_get_drvdata(dev) : NULL;
  250. }
  251. EXPORT_SYMBOL_GPL(power_supply_get_by_name);
  252. int power_supply_powers(struct power_supply *psy, struct device *dev)
  253. {
  254. return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
  255. }
  256. EXPORT_SYMBOL_GPL(power_supply_powers);
  257. static void power_supply_dev_release(struct device *dev)
  258. {
  259. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  260. kfree(dev);
  261. }
  262. #ifdef CONFIG_THERMAL
  263. static int power_supply_read_temp(struct thermal_zone_device *tzd,
  264. unsigned long *temp)
  265. {
  266. struct power_supply *psy;
  267. union power_supply_propval val;
  268. int ret;
  269. WARN_ON(tzd == NULL);
  270. psy = tzd->devdata;
  271. ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
  272. /* Convert tenths of degree Celsius to milli degree Celsius. */
  273. if (!ret)
  274. *temp = val.intval * 100;
  275. return ret;
  276. }
  277. static struct thermal_zone_device_ops psy_tzd_ops = {
  278. .get_temp = power_supply_read_temp,
  279. };
  280. static int psy_register_thermal(struct power_supply *psy)
  281. {
  282. int i;
  283. /* Register battery zone device psy reports temperature */
  284. for (i = 0; i < psy->num_properties; i++) {
  285. if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
  286. psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
  287. psy, &psy_tzd_ops, NULL, 0, 0);
  288. if (IS_ERR(psy->tzd))
  289. return PTR_ERR(psy->tzd);
  290. break;
  291. }
  292. }
  293. return 0;
  294. }
  295. static void psy_unregister_thermal(struct power_supply *psy)
  296. {
  297. if (IS_ERR_OR_NULL(psy->tzd))
  298. return;
  299. thermal_zone_device_unregister(psy->tzd);
  300. }
  301. /* thermal cooling device callbacks */
  302. static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
  303. unsigned long *state)
  304. {
  305. struct power_supply *psy;
  306. union power_supply_propval val;
  307. int ret;
  308. psy = tcd->devdata;
  309. ret = psy->get_property(psy,
  310. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
  311. if (!ret)
  312. *state = val.intval;
  313. return ret;
  314. }
  315. static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
  316. unsigned long *state)
  317. {
  318. struct power_supply *psy;
  319. union power_supply_propval val;
  320. int ret;
  321. psy = tcd->devdata;
  322. ret = psy->get_property(psy,
  323. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  324. if (!ret)
  325. *state = val.intval;
  326. return ret;
  327. }
  328. static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
  329. unsigned long state)
  330. {
  331. struct power_supply *psy;
  332. union power_supply_propval val;
  333. int ret;
  334. psy = tcd->devdata;
  335. val.intval = state;
  336. ret = psy->set_property(psy,
  337. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
  338. return ret;
  339. }
  340. static struct thermal_cooling_device_ops psy_tcd_ops = {
  341. .get_max_state = ps_get_max_charge_cntl_limit,
  342. .get_cur_state = ps_get_cur_chrage_cntl_limit,
  343. .set_cur_state = ps_set_cur_charge_cntl_limit,
  344. };
  345. static int psy_register_cooler(struct power_supply *psy)
  346. {
  347. int i;
  348. /* Register for cooling device if psy can control charging */
  349. for (i = 0; i < psy->num_properties; i++) {
  350. if (psy->properties[i] ==
  351. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
  352. psy->tcd = thermal_cooling_device_register(
  353. (char *)psy->name,
  354. psy, &psy_tcd_ops);
  355. if (IS_ERR(psy->tcd))
  356. return PTR_ERR(psy->tcd);
  357. break;
  358. }
  359. }
  360. return 0;
  361. }
  362. static void psy_unregister_cooler(struct power_supply *psy)
  363. {
  364. if (IS_ERR_OR_NULL(psy->tcd))
  365. return;
  366. thermal_cooling_device_unregister(psy->tcd);
  367. }
  368. #else
  369. static int psy_register_thermal(struct power_supply *psy)
  370. {
  371. return 0;
  372. }
  373. static void psy_unregister_thermal(struct power_supply *psy)
  374. {
  375. }
  376. static int psy_register_cooler(struct power_supply *psy)
  377. {
  378. return 0;
  379. }
  380. static void psy_unregister_cooler(struct power_supply *psy)
  381. {
  382. }
  383. #endif
  384. int power_supply_register(struct device *parent, struct power_supply *psy)
  385. {
  386. struct device *dev;
  387. int rc;
  388. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  389. if (!dev)
  390. return -ENOMEM;
  391. device_initialize(dev);
  392. dev->class = power_supply_class;
  393. dev->type = &power_supply_dev_type;
  394. dev->parent = parent;
  395. dev->release = power_supply_dev_release;
  396. dev_set_drvdata(dev, psy);
  397. psy->dev = dev;
  398. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  399. rc = power_supply_check_supplies(psy);
  400. if (rc) {
  401. dev_info(dev, "Not all required supplies found, defer probe\n");
  402. goto check_supplies_failed;
  403. }
  404. rc = kobject_set_name(&dev->kobj, "%s", psy->name);
  405. if (rc)
  406. goto kobject_set_name_failed;
  407. rc = device_add(dev);
  408. if (rc)
  409. goto device_add_failed;
  410. rc = psy_register_thermal(psy);
  411. if (rc)
  412. goto register_thermal_failed;
  413. rc = psy_register_cooler(psy);
  414. if (rc)
  415. goto register_cooler_failed;
  416. rc = power_supply_create_triggers(psy);
  417. if (rc)
  418. goto create_triggers_failed;
  419. power_supply_changed(psy);
  420. goto success;
  421. create_triggers_failed:
  422. psy_unregister_cooler(psy);
  423. register_cooler_failed:
  424. psy_unregister_thermal(psy);
  425. register_thermal_failed:
  426. device_del(dev);
  427. kobject_set_name_failed:
  428. device_add_failed:
  429. check_supplies_failed:
  430. put_device(dev);
  431. success:
  432. return rc;
  433. }
  434. EXPORT_SYMBOL_GPL(power_supply_register);
  435. void power_supply_unregister(struct power_supply *psy)
  436. {
  437. cancel_work_sync(&psy->changed_work);
  438. sysfs_remove_link(&psy->dev->kobj, "powers");
  439. power_supply_remove_triggers(psy);
  440. psy_unregister_cooler(psy);
  441. psy_unregister_thermal(psy);
  442. device_unregister(psy->dev);
  443. }
  444. EXPORT_SYMBOL_GPL(power_supply_unregister);
  445. static int __init power_supply_class_init(void)
  446. {
  447. power_supply_class = class_create(THIS_MODULE, "power_supply");
  448. if (IS_ERR(power_supply_class))
  449. return PTR_ERR(power_supply_class);
  450. power_supply_class->dev_uevent = power_supply_uevent;
  451. power_supply_init_attrs(&power_supply_dev_type);
  452. return 0;
  453. }
  454. static void __exit power_supply_class_exit(void)
  455. {
  456. class_destroy(power_supply_class);
  457. }
  458. subsys_initcall(power_supply_class_init);
  459. module_exit(power_supply_class_exit);
  460. MODULE_DESCRIPTION("Universal power supply monitor class");
  461. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  462. "Szabolcs Gyurko, "
  463. "Anton Vorontsov <cbou@mail.ru>");
  464. MODULE_LICENSE("GPL");