power_supply_leds.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 (power_supply_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. psy->charging_full_trig_name = kasprintf(GFP_KERNEL,
  53. "%s-charging-or-full", psy->desc->name);
  54. if (!psy->charging_full_trig_name)
  55. goto charging_full_failed;
  56. psy->charging_trig_name = kasprintf(GFP_KERNEL,
  57. "%s-charging", psy->desc->name);
  58. if (!psy->charging_trig_name)
  59. goto charging_failed;
  60. psy->full_trig_name = kasprintf(GFP_KERNEL, "%s-full", psy->desc->name);
  61. if (!psy->full_trig_name)
  62. goto full_failed;
  63. psy->charging_blink_full_solid_trig_name = kasprintf(GFP_KERNEL,
  64. "%s-charging-blink-full-solid", psy->desc->name);
  65. if (!psy->charging_blink_full_solid_trig_name)
  66. goto charging_blink_full_solid_failed;
  67. led_trigger_register_simple(psy->charging_full_trig_name,
  68. &psy->charging_full_trig);
  69. led_trigger_register_simple(psy->charging_trig_name,
  70. &psy->charging_trig);
  71. led_trigger_register_simple(psy->full_trig_name,
  72. &psy->full_trig);
  73. led_trigger_register_simple(psy->charging_blink_full_solid_trig_name,
  74. &psy->charging_blink_full_solid_trig);
  75. return 0;
  76. charging_blink_full_solid_failed:
  77. kfree(psy->full_trig_name);
  78. full_failed:
  79. kfree(psy->charging_trig_name);
  80. charging_failed:
  81. kfree(psy->charging_full_trig_name);
  82. charging_full_failed:
  83. return -ENOMEM;
  84. }
  85. static void power_supply_remove_bat_triggers(struct power_supply *psy)
  86. {
  87. led_trigger_unregister_simple(psy->charging_full_trig);
  88. led_trigger_unregister_simple(psy->charging_trig);
  89. led_trigger_unregister_simple(psy->full_trig);
  90. led_trigger_unregister_simple(psy->charging_blink_full_solid_trig);
  91. kfree(psy->charging_blink_full_solid_trig_name);
  92. kfree(psy->full_trig_name);
  93. kfree(psy->charging_trig_name);
  94. kfree(psy->charging_full_trig_name);
  95. }
  96. /* Generated power specific LEDs triggers. */
  97. static void power_supply_update_gen_leds(struct power_supply *psy)
  98. {
  99. union power_supply_propval online;
  100. if (power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online))
  101. return;
  102. dev_dbg(&psy->dev, "%s %d\n", __func__, online.intval);
  103. if (online.intval)
  104. led_trigger_event(psy->online_trig, LED_FULL);
  105. else
  106. led_trigger_event(psy->online_trig, LED_OFF);
  107. }
  108. static int power_supply_create_gen_triggers(struct power_supply *psy)
  109. {
  110. psy->online_trig_name = kasprintf(GFP_KERNEL, "%s-online",
  111. psy->desc->name);
  112. if (!psy->online_trig_name)
  113. return -ENOMEM;
  114. led_trigger_register_simple(psy->online_trig_name, &psy->online_trig);
  115. return 0;
  116. }
  117. static void power_supply_remove_gen_triggers(struct power_supply *psy)
  118. {
  119. led_trigger_unregister_simple(psy->online_trig);
  120. kfree(psy->online_trig_name);
  121. }
  122. /* Choice what triggers to create&update. */
  123. void power_supply_update_leds(struct power_supply *psy)
  124. {
  125. if (psy->desc->type == POWER_SUPPLY_TYPE_BATTERY)
  126. power_supply_update_bat_leds(psy);
  127. else
  128. power_supply_update_gen_leds(psy);
  129. }
  130. int power_supply_create_triggers(struct power_supply *psy)
  131. {
  132. if (psy->desc->type == POWER_SUPPLY_TYPE_BATTERY)
  133. return power_supply_create_bat_triggers(psy);
  134. return power_supply_create_gen_triggers(psy);
  135. }
  136. void power_supply_remove_triggers(struct power_supply *psy)
  137. {
  138. if (psy->desc->type == POWER_SUPPLY_TYPE_BATTERY)
  139. power_supply_remove_bat_triggers(psy);
  140. else
  141. power_supply_remove_gen_triggers(psy);
  142. }