Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame^] | 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 | |
| 13 | #include <linux/power_supply.h> |
| 14 | |
| 15 | /* Battery specific LEDs triggers. */ |
| 16 | |
| 17 | static void power_supply_update_bat_leds(struct power_supply *psy) |
| 18 | { |
| 19 | union power_supply_propval status; |
| 20 | |
| 21 | if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status)) |
| 22 | return; |
| 23 | |
| 24 | dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, status.intval); |
| 25 | |
| 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 | break; |
| 32 | case POWER_SUPPLY_STATUS_CHARGING: |
| 33 | led_trigger_event(psy->charging_full_trig, LED_FULL); |
| 34 | led_trigger_event(psy->charging_trig, LED_FULL); |
| 35 | led_trigger_event(psy->full_trig, LED_OFF); |
| 36 | break; |
| 37 | default: |
| 38 | led_trigger_event(psy->charging_full_trig, LED_OFF); |
| 39 | led_trigger_event(psy->charging_trig, LED_OFF); |
| 40 | led_trigger_event(psy->full_trig, LED_OFF); |
| 41 | break; |
| 42 | } |
| 43 | |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | static int power_supply_create_bat_triggers(struct power_supply *psy) |
| 48 | { |
| 49 | int rc = 0; |
| 50 | |
| 51 | psy->charging_full_trig_name = kmalloc(strlen(psy->name) + |
| 52 | sizeof("-charging-or-full"), GFP_KERNEL); |
| 53 | if (!psy->charging_full_trig_name) |
| 54 | goto charging_full_failed; |
| 55 | |
| 56 | psy->charging_trig_name = kmalloc(strlen(psy->name) + |
| 57 | sizeof("-charging"), GFP_KERNEL); |
| 58 | if (!psy->charging_trig_name) |
| 59 | goto charging_failed; |
| 60 | |
| 61 | psy->full_trig_name = kmalloc(strlen(psy->name) + |
| 62 | sizeof("-full"), GFP_KERNEL); |
| 63 | if (!psy->full_trig_name) |
| 64 | goto full_failed; |
| 65 | |
| 66 | strcpy(psy->charging_full_trig_name, psy->name); |
| 67 | strcat(psy->charging_full_trig_name, "-charging-or-full"); |
| 68 | strcpy(psy->charging_trig_name, psy->name); |
| 69 | strcat(psy->charging_trig_name, "-charging"); |
| 70 | strcpy(psy->full_trig_name, psy->name); |
| 71 | strcat(psy->full_trig_name, "-full"); |
| 72 | |
| 73 | led_trigger_register_simple(psy->charging_full_trig_name, |
| 74 | &psy->charging_full_trig); |
| 75 | led_trigger_register_simple(psy->charging_trig_name, |
| 76 | &psy->charging_trig); |
| 77 | led_trigger_register_simple(psy->full_trig_name, |
| 78 | &psy->full_trig); |
| 79 | |
| 80 | goto success; |
| 81 | |
| 82 | full_failed: |
| 83 | kfree(psy->charging_trig_name); |
| 84 | charging_failed: |
| 85 | kfree(psy->charging_full_trig_name); |
| 86 | charging_full_failed: |
| 87 | rc = -ENOMEM; |
| 88 | success: |
| 89 | return rc; |
| 90 | } |
| 91 | |
| 92 | static void power_supply_remove_bat_triggers(struct power_supply *psy) |
| 93 | { |
| 94 | led_trigger_unregister_simple(psy->charging_full_trig); |
| 95 | led_trigger_unregister_simple(psy->charging_trig); |
| 96 | led_trigger_unregister_simple(psy->full_trig); |
| 97 | kfree(psy->full_trig_name); |
| 98 | kfree(psy->charging_trig_name); |
| 99 | kfree(psy->charging_full_trig_name); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | /* Generated power specific LEDs triggers. */ |
| 104 | |
| 105 | static void power_supply_update_gen_leds(struct power_supply *psy) |
| 106 | { |
| 107 | union power_supply_propval online; |
| 108 | |
| 109 | if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online)) |
| 110 | return; |
| 111 | |
| 112 | dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, online.intval); |
| 113 | |
| 114 | if (online.intval) |
| 115 | led_trigger_event(psy->online_trig, LED_FULL); |
| 116 | else |
| 117 | led_trigger_event(psy->online_trig, LED_OFF); |
| 118 | |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | static int power_supply_create_gen_triggers(struct power_supply *psy) |
| 123 | { |
| 124 | int rc = 0; |
| 125 | |
| 126 | psy->online_trig_name = kmalloc(strlen(psy->name) + sizeof("-online"), |
| 127 | GFP_KERNEL); |
| 128 | if (!psy->online_trig_name) |
| 129 | goto online_failed; |
| 130 | |
| 131 | strcpy(psy->online_trig_name, psy->name); |
| 132 | strcat(psy->online_trig_name, "-online"); |
| 133 | |
| 134 | led_trigger_register_simple(psy->online_trig_name, &psy->online_trig); |
| 135 | |
| 136 | goto success; |
| 137 | |
| 138 | online_failed: |
| 139 | rc = -ENOMEM; |
| 140 | success: |
| 141 | return rc; |
| 142 | } |
| 143 | |
| 144 | static void power_supply_remove_gen_triggers(struct power_supply *psy) |
| 145 | { |
| 146 | led_trigger_unregister_simple(psy->online_trig); |
| 147 | kfree(psy->online_trig_name); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | /* Choice what triggers to create&update. */ |
| 152 | |
| 153 | void power_supply_update_leds(struct power_supply *psy) |
| 154 | { |
| 155 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY) |
| 156 | power_supply_update_bat_leds(psy); |
| 157 | else |
| 158 | power_supply_update_gen_leds(psy); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | int power_supply_create_triggers(struct power_supply *psy) |
| 163 | { |
| 164 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY) |
| 165 | return power_supply_create_bat_triggers(psy); |
| 166 | return power_supply_create_gen_triggers(psy); |
| 167 | } |
| 168 | |
| 169 | void power_supply_remove_triggers(struct power_supply *psy) |
| 170 | { |
| 171 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY) |
| 172 | power_supply_remove_bat_triggers(psy); |
| 173 | else |
| 174 | power_supply_remove_gen_triggers(psy); |
| 175 | return; |
| 176 | } |