| /* |
| * LEDs triggers for power supply class |
| * |
| * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> |
| * Copyright © 2004 Szabolcs Gyurko |
| * Copyright © 2003 Ian Molton <spyro@f2s.com> |
| * |
| * Modified: 2004, Oct Szabolcs Gyurko |
| * |
| * You may use this code as per GPL version 2 |
| */ |
| |
| #include <linux/kernel.h> |
| #include <linux/device.h> |
| #include <linux/power_supply.h> |
| #include <linux/slab.h> |
| |
| #include "power_supply.h" |
| |
| #define BATTERY_LOW 15 |
| #define BATTERY_HIGH 90 |
| |
| static bool off_charge_flag; |
| |
| /* Battery specific LEDs triggers. */ |
| |
| static void power_supply_update_bat_leds(struct power_supply *psy) |
| { |
| union power_supply_propval status; |
| union power_supply_propval capacity; |
| union power_supply_propval profile_status; |
| |
| /* only control the leds in off charge mode */ |
| if (!off_charge_flag) |
| return; |
| |
| /* set the leds color after battery proflie loaded */ |
| if (psy->get_property(psy, POWER_SUPPLY_PROP_PROFILE_STATUS, |
| &profile_status)) |
| return; |
| |
| if (profile_status.intval == 0) |
| return; |
| |
| if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status)) |
| return; |
| |
| /* turn off the leds while not charging */ |
| if (status.intval != POWER_SUPPLY_STATUS_FULL |
| && status.intval != POWER_SUPPLY_STATUS_CHARGING){ |
| led_trigger_event(psy->charging_full_trig, LED_OFF); |
| led_trigger_event(psy->charging_trig, LED_OFF); |
| led_trigger_event(psy->full_trig, LED_OFF); |
| led_trigger_event(psy->charging_blink_full_solid_trig, LED_OFF); |
| return; |
| } |
| |
| if (psy->get_property(psy, POWER_SUPPLY_PROP_CAPACITY, &capacity)) |
| return; |
| |
| if (capacity.intval < 0) { |
| return; |
| } else if (capacity.intval < BATTERY_LOW) { |
| /* set led red */ |
| led_trigger_event(psy->charging_full_trig, LED_OFF); |
| led_trigger_event(psy->charging_trig, LED_FULL); |
| led_trigger_event(psy->full_trig, LED_OFF); |
| led_trigger_event(psy->charging_blink_full_solid_trig, LED_OFF); |
| } else if (capacity.intval < BATTERY_HIGH) { |
| /* set led yellow */ |
| led_trigger_event(psy->charging_full_trig, LED_OFF); |
| led_trigger_event(psy->charging_trig, LED_FULL); |
| led_trigger_event(psy->full_trig, LED_FULL); |
| led_trigger_event(psy->charging_blink_full_solid_trig, LED_OFF); |
| } else { |
| /* set led green */ |
| led_trigger_event(psy->charging_full_trig, LED_OFF); |
| led_trigger_event(psy->charging_trig, LED_OFF); |
| led_trigger_event(psy->full_trig, LED_FULL); |
| led_trigger_event(psy->charging_blink_full_solid_trig, LED_OFF); |
| } |
| } |
| |
| static int __init early_parse_off_charge_flag(char *p) |
| { |
| if (p) { |
| if (!strcmp(p, "charger")) |
| off_charge_flag = true; |
| } |
| return 0; |
| } |
| early_param("androidboot.mode", early_parse_off_charge_flag); |
| |
| static int power_supply_create_bat_triggers(struct power_supply *psy) |
| { |
| int rc = 0; |
| |
| psy->charging_full_trig_name = kasprintf(GFP_KERNEL, |
| "%s-charging-or-full", psy->name); |
| if (!psy->charging_full_trig_name) |
| goto charging_full_failed; |
| |
| psy->charging_trig_name = kasprintf(GFP_KERNEL, |
| "%s-charging", psy->name); |
| if (!psy->charging_trig_name) |
| goto charging_failed; |
| |
| psy->full_trig_name = kasprintf(GFP_KERNEL, "%s-full", psy->name); |
| if (!psy->full_trig_name) |
| goto full_failed; |
| |
| psy->charging_blink_full_solid_trig_name = kasprintf(GFP_KERNEL, |
| "%s-charging-blink-full-solid", psy->name); |
| if (!psy->charging_blink_full_solid_trig_name) |
| goto charging_blink_full_solid_failed; |
| |
| led_trigger_register_simple(psy->charging_full_trig_name, |
| &psy->charging_full_trig); |
| led_trigger_register_simple(psy->charging_trig_name, |
| &psy->charging_trig); |
| led_trigger_register_simple(psy->full_trig_name, |
| &psy->full_trig); |
| led_trigger_register_simple(psy->charging_blink_full_solid_trig_name, |
| &psy->charging_blink_full_solid_trig); |
| |
| goto success; |
| |
| charging_blink_full_solid_failed: |
| kfree(psy->full_trig_name); |
| full_failed: |
| kfree(psy->charging_trig_name); |
| charging_failed: |
| kfree(psy->charging_full_trig_name); |
| charging_full_failed: |
| rc = -ENOMEM; |
| success: |
| return rc; |
| } |
| |
| static void power_supply_remove_bat_triggers(struct power_supply *psy) |
| { |
| led_trigger_unregister_simple(psy->charging_full_trig); |
| led_trigger_unregister_simple(psy->charging_trig); |
| led_trigger_unregister_simple(psy->full_trig); |
| led_trigger_unregister_simple(psy->charging_blink_full_solid_trig); |
| kfree(psy->charging_blink_full_solid_trig_name); |
| kfree(psy->full_trig_name); |
| kfree(psy->charging_trig_name); |
| kfree(psy->charging_full_trig_name); |
| } |
| |
| /* Generated power specific LEDs triggers. */ |
| |
| static void power_supply_update_gen_leds(struct power_supply *psy) |
| { |
| union power_supply_propval online; |
| |
| if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online)) |
| return; |
| |
| dev_dbg(psy->dev, "%s %d\n", __func__, online.intval); |
| |
| if (online.intval) |
| led_trigger_event(psy->online_trig, LED_FULL); |
| else |
| led_trigger_event(psy->online_trig, LED_OFF); |
| } |
| |
| static int power_supply_create_gen_triggers(struct power_supply *psy) |
| { |
| int rc = 0; |
| |
| psy->online_trig_name = kasprintf(GFP_KERNEL, "%s-online", psy->name); |
| if (!psy->online_trig_name) |
| goto online_failed; |
| |
| led_trigger_register_simple(psy->online_trig_name, &psy->online_trig); |
| |
| goto success; |
| |
| online_failed: |
| rc = -ENOMEM; |
| success: |
| return rc; |
| } |
| |
| static void power_supply_remove_gen_triggers(struct power_supply *psy) |
| { |
| led_trigger_unregister_simple(psy->online_trig); |
| kfree(psy->online_trig_name); |
| } |
| |
| /* Choice what triggers to create&update. */ |
| |
| void power_supply_update_leds(struct power_supply *psy) |
| { |
| if (psy->type == POWER_SUPPLY_TYPE_BATTERY) |
| power_supply_update_bat_leds(psy); |
| else |
| power_supply_update_gen_leds(psy); |
| } |
| |
| int power_supply_create_triggers(struct power_supply *psy) |
| { |
| if (psy->type == POWER_SUPPLY_TYPE_BATTERY) |
| return power_supply_create_bat_triggers(psy); |
| return power_supply_create_gen_triggers(psy); |
| } |
| |
| void power_supply_remove_triggers(struct power_supply *psy) |
| { |
| if (psy->type == POWER_SUPPLY_TYPE_BATTERY) |
| power_supply_remove_bat_triggers(psy); |
| else |
| power_supply_remove_gen_triggers(psy); |
| } |