V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / power / power_supply_leds.c
blob7f8f3590b02e6934e6066e5d63bb40a6f7440af9
1 /*
2 * LEDs triggers for power supply class
4 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
5 * Copyright © 2004 Szabolcs Gyurko
6 * Copyright © 2003 Ian Molton <spyro@f2s.com>
8 * Modified: 2004, Oct Szabolcs Gyurko
10 * You may use this code as per GPL version 2
13 #include <linux/power_supply.h>
15 /* Battery specific LEDs triggers. */
17 static void power_supply_update_bat_leds(struct power_supply *psy)
19 union power_supply_propval status;
21 if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status))
22 return;
24 dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, 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 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;
45 static int power_supply_create_bat_triggers(struct power_supply *psy)
47 int rc = 0;
49 psy->charging_full_trig_name = kmalloc(strlen(psy->name) +
50 sizeof("-charging-or-full"), GFP_KERNEL);
51 if (!psy->charging_full_trig_name)
52 goto charging_full_failed;
54 psy->charging_trig_name = kmalloc(strlen(psy->name) +
55 sizeof("-charging"), GFP_KERNEL);
56 if (!psy->charging_trig_name)
57 goto charging_failed;
59 psy->full_trig_name = kmalloc(strlen(psy->name) +
60 sizeof("-full"), GFP_KERNEL);
61 if (!psy->full_trig_name)
62 goto full_failed;
64 strcpy(psy->charging_full_trig_name, psy->name);
65 strcat(psy->charging_full_trig_name, "-charging-or-full");
66 strcpy(psy->charging_trig_name, psy->name);
67 strcat(psy->charging_trig_name, "-charging");
68 strcpy(psy->full_trig_name, psy->name);
69 strcat(psy->full_trig_name, "-full");
71 led_trigger_register_simple(psy->charging_full_trig_name,
72 &psy->charging_full_trig);
73 led_trigger_register_simple(psy->charging_trig_name,
74 &psy->charging_trig);
75 led_trigger_register_simple(psy->full_trig_name,
76 &psy->full_trig);
78 goto success;
80 full_failed:
81 kfree(psy->charging_trig_name);
82 charging_failed:
83 kfree(psy->charging_full_trig_name);
84 charging_full_failed:
85 rc = -ENOMEM;
86 success:
87 return rc;
90 static void power_supply_remove_bat_triggers(struct power_supply *psy)
92 led_trigger_unregister_simple(psy->charging_full_trig);
93 led_trigger_unregister_simple(psy->charging_trig);
94 led_trigger_unregister_simple(psy->full_trig);
95 kfree(psy->full_trig_name);
96 kfree(psy->charging_trig_name);
97 kfree(psy->charging_full_trig_name);
100 /* Generated power specific LEDs triggers. */
102 static void power_supply_update_gen_leds(struct power_supply *psy)
104 union power_supply_propval online;
106 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online))
107 return;
109 dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, online.intval);
111 if (online.intval)
112 led_trigger_event(psy->online_trig, LED_FULL);
113 else
114 led_trigger_event(psy->online_trig, LED_OFF);
117 static int power_supply_create_gen_triggers(struct power_supply *psy)
119 int rc = 0;
121 psy->online_trig_name = kmalloc(strlen(psy->name) + sizeof("-online"),
122 GFP_KERNEL);
123 if (!psy->online_trig_name)
124 goto online_failed;
126 strcpy(psy->online_trig_name, psy->name);
127 strcat(psy->online_trig_name, "-online");
129 led_trigger_register_simple(psy->online_trig_name, &psy->online_trig);
131 goto success;
133 online_failed:
134 rc = -ENOMEM;
135 success:
136 return rc;
139 static void power_supply_remove_gen_triggers(struct power_supply *psy)
141 led_trigger_unregister_simple(psy->online_trig);
142 kfree(psy->online_trig_name);
145 /* Choice what triggers to create&update. */
147 void power_supply_update_leds(struct power_supply *psy)
149 if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
150 power_supply_update_bat_leds(psy);
151 else
152 power_supply_update_gen_leds(psy);
155 int power_supply_create_triggers(struct power_supply *psy)
157 if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
158 return power_supply_create_bat_triggers(psy);
159 return power_supply_create_gen_triggers(psy);
162 void power_supply_remove_triggers(struct power_supply *psy)
164 if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
165 power_supply_remove_bat_triggers(psy);
166 else
167 power_supply_remove_gen_triggers(psy);