power_supply_leds.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * LEDs triggers for power supply 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/kernel.h>
  13. #include <linux/device.h>
  14. #include <linux/power_supply.h>
  15. #include <linux/slab.h>
  16. #include "power_supply.h"
  17. /* Battery specific LEDs triggers. */
  18. static void power_supply_update_bat_leds(struct power_supply *psy)
  19. {
  20. union power_supply_propval status;
  21. unsigned long delay_on = 0;
  22. unsigned long delay_off = 0;
  23. if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status))
  24. return;
  25. dev_dbg(psy->dev, "%s %d\n", __func__, status.intval);
  26. switch (status.intval) {
  27. case POWER_SUPPLY_STATUS_FULL:
  28. led_trigger_event(psy->charging_full_trig, LED_FULL);
  29. led_trigger_event(psy->charging_trig, LED_OFF);
  30. led_trigger_event(psy->full_trig, LED_FULL);
  31. led_trigger_event(psy->charging_blink_full_solid_trig,
  32. LED_FULL);
  33. break;
  34. case POWER_SUPPLY_STATUS_CHARGING:
  35. led_trigger_event(psy->charging_full_trig, LED_FULL);
  36. led_trigger_event(psy->charging_trig, LED_FULL);
  37. led_trigger_event(psy->full_trig, LED_OFF);
  38. led_trigger_blink(psy->charging_blink_full_solid_trig,
  39. &delay_on, &delay_off);
  40. break;
  41. default:
  42. led_trigger_event(psy->charging_full_trig, LED_OFF);
  43. led_trigger_event(psy->charging_trig, LED_OFF);
  44. led_trigger_event(psy->full_trig, LED_OFF);
  45. led_trigger_event(psy->charging_blink_full_solid_trig,
  46. LED_OFF);
  47. break;
  48. }
  49. }
  50. static int power_supply_create_bat_triggers(struct power_supply *psy)
  51. {
  52. int rc = 0;
  53. psy->charging_full_trig_name = kasprintf(GFP_KERNEL,
  54. "%s-charging-or-full", psy->name);
  55. if (!psy->charging_full_trig_name)
  56. goto charging_full_failed;
  57. psy->charging_trig_name = kasprintf(GFP_KERNEL,
  58. "%s-charging", psy->name);
  59. if (!psy->charging_trig_name)
  60. goto charging_failed;
  61. psy->full_trig_name = kasprintf(GFP_KERNEL, "%s-full", psy->name);
  62. if (!psy->full_trig_name)
  63. goto full_failed;
  64. psy->charging_blink_full_solid_trig_name = kasprintf(GFP_KERNEL,
  65. "%s-charging-blink-full-solid", psy->name);
  66. if (!psy->charging_blink_full_solid_trig_name)
  67. goto charging_blink_full_solid_failed;
  68. led_trigger_register_simple(psy->charging_full_trig_name,
  69. &psy->charging_full_trig);
  70. led_trigger_register_simple(psy->charging_trig_name,
  71. &psy->charging_trig);
  72. led_trigger_register_simple(psy->full_trig_name,
  73. &psy->full_trig);
  74. led_trigger_register_simple(psy->charging_blink_full_solid_trig_name,
  75. &psy->charging_blink_full_solid_trig);
  76. goto success;
  77. charging_blink_full_solid_failed:
  78. kfree(psy->full_trig_name);
  79. full_failed:
  80. kfree(psy->charging_trig_name);
  81. charging_failed:
  82. kfree(psy->charging_full_trig_name);
  83. charging_full_failed:
  84. rc = -ENOMEM;
  85. success:
  86. return rc;
  87. }
  88. static void power_supply_remove_bat_triggers(struct power_supply *psy)
  89. {
  90. led_trigger_unregister_simple(psy->charging_full_trig);
  91. led_trigger_unregister_simple(psy->charging_trig);
  92. led_trigger_unregister_simple(psy->full_trig);
  93. led_trigger_unregister_simple(psy->charging_blink_full_solid_trig);
  94. kfree(psy->charging_blink_full_solid_trig_name);
  95. kfree(psy->full_trig_name);
  96. kfree(psy->charging_trig_name);
  97. kfree(psy->charging_full_trig_name);
  98. }
  99. /* Generated power specific LEDs triggers. */
  100. static void power_supply_update_gen_leds(struct power_supply *psy)
  101. {
  102. union power_supply_propval online;
  103. if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online))
  104. return;
  105. dev_dbg(psy->dev, "%s %d\n", __func__, online.intval);
  106. if (online.intval)
  107. led_trigger_event(psy->online_trig, LED_FULL);
  108. else
  109. led_trigger_event(psy->online_trig, LED_OFF);
  110. }
  111. static int power_supply_create_gen_triggers(struct power_supply *psy)
  112. {
  113. int rc = 0;
  114. psy->online_trig_name = kasprintf(GFP_KERNEL, "%s-online", psy->name);
  115. if (!psy->online_trig_name)
  116. goto online_failed;
  117. led_trigger_register_simple(psy->online_trig_name, &psy->online_trig);
  118. goto success;
  119. online_failed:
  120. rc = -ENOMEM;
  121. success:
  122. return rc;
  123. }
  124. static void power_supply_remove_gen_triggers(struct power_supply *psy)
  125. {
  126. led_trigger_unregister_simple(psy->online_trig);
  127. kfree(psy->online_trig_name);
  128. }
  129. /* Choice what triggers to create&update. */
  130. void power_supply_update_leds(struct power_supply *psy)
  131. {
  132. if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
  133. power_supply_update_bat_leds(psy);
  134. else
  135. power_supply_update_gen_leds(psy);
  136. }
  137. int power_supply_create_triggers(struct power_supply *psy)
  138. {
  139. if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
  140. return power_supply_create_bat_triggers(psy);
  141. return power_supply_create_gen_triggers(psy);
  142. }
  143. void power_supply_remove_triggers(struct power_supply *psy)
  144. {
  145. if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
  146. power_supply_remove_bat_triggers(psy);
  147. else
  148. power_supply_remove_gen_triggers(psy);
  149. }