blob: 2dece40c544f2098dae9b94ae110f31c8f50be31 [file] [log] [blame]
Anton Vorontsov4a11b592007-05-04 00:27:45 +04001/*
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
Akinobu Mita4d244732007-11-17 19:55:58 +090013#include <linux/kernel.h>
Anton Vorontsov4a11b592007-05-04 00:27:45 +040014#include <linux/power_supply.h>
15
Adrian Bunk25f121412007-11-26 00:25:45 +030016#include "power_supply.h"
17
Anton Vorontsov4a11b592007-05-04 00:27:45 +040018/* Battery specific LEDs triggers. */
19
20static void power_supply_update_bat_leds(struct power_supply *psy)
21{
22 union power_supply_propval status;
23
24 if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status))
25 return;
26
Harvey Harrison0cddc0a2008-04-29 00:58:29 -070027 dev_dbg(psy->dev, "%s %d\n", __func__, status.intval);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040028
29 switch (status.intval) {
30 case POWER_SUPPLY_STATUS_FULL:
31 led_trigger_event(psy->charging_full_trig, LED_FULL);
32 led_trigger_event(psy->charging_trig, LED_OFF);
33 led_trigger_event(psy->full_trig, LED_FULL);
34 break;
35 case POWER_SUPPLY_STATUS_CHARGING:
36 led_trigger_event(psy->charging_full_trig, LED_FULL);
37 led_trigger_event(psy->charging_trig, LED_FULL);
38 led_trigger_event(psy->full_trig, LED_OFF);
39 break;
40 default:
41 led_trigger_event(psy->charging_full_trig, LED_OFF);
42 led_trigger_event(psy->charging_trig, LED_OFF);
43 led_trigger_event(psy->full_trig, LED_OFF);
44 break;
45 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +040046}
47
48static int power_supply_create_bat_triggers(struct power_supply *psy)
49{
50 int rc = 0;
51
Akinobu Mita4d244732007-11-17 19:55:58 +090052 psy->charging_full_trig_name = kasprintf(GFP_KERNEL,
53 "%s-charging-or-full", psy->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040054 if (!psy->charging_full_trig_name)
55 goto charging_full_failed;
56
Akinobu Mita4d244732007-11-17 19:55:58 +090057 psy->charging_trig_name = kasprintf(GFP_KERNEL,
58 "%s-charging", psy->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040059 if (!psy->charging_trig_name)
60 goto charging_failed;
61
Akinobu Mita4d244732007-11-17 19:55:58 +090062 psy->full_trig_name = kasprintf(GFP_KERNEL, "%s-full", psy->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040063 if (!psy->full_trig_name)
64 goto full_failed;
65
Anton Vorontsov4a11b592007-05-04 00:27:45 +040066 led_trigger_register_simple(psy->charging_full_trig_name,
67 &psy->charging_full_trig);
68 led_trigger_register_simple(psy->charging_trig_name,
69 &psy->charging_trig);
70 led_trigger_register_simple(psy->full_trig_name,
71 &psy->full_trig);
72
73 goto success;
74
75full_failed:
76 kfree(psy->charging_trig_name);
77charging_failed:
78 kfree(psy->charging_full_trig_name);
79charging_full_failed:
80 rc = -ENOMEM;
81success:
82 return rc;
83}
84
85static 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 kfree(psy->full_trig_name);
91 kfree(psy->charging_trig_name);
92 kfree(psy->charging_full_trig_name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040093}
94
95/* Generated power specific LEDs triggers. */
96
97static void power_supply_update_gen_leds(struct power_supply *psy)
98{
99 union power_supply_propval online;
100
101 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online))
102 return;
103
Harvey Harrison0cddc0a2008-04-29 00:58:29 -0700104 dev_dbg(psy->dev, "%s %d\n", __func__, online.intval);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400105
106 if (online.intval)
107 led_trigger_event(psy->online_trig, LED_FULL);
108 else
109 led_trigger_event(psy->online_trig, LED_OFF);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400110}
111
112static int power_supply_create_gen_triggers(struct power_supply *psy)
113{
114 int rc = 0;
115
Akinobu Mita4d244732007-11-17 19:55:58 +0900116 psy->online_trig_name = kasprintf(GFP_KERNEL, "%s-online", psy->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400117 if (!psy->online_trig_name)
118 goto online_failed;
119
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400120 led_trigger_register_simple(psy->online_trig_name, &psy->online_trig);
121
122 goto success;
123
124online_failed:
125 rc = -ENOMEM;
126success:
127 return rc;
128}
129
130static void power_supply_remove_gen_triggers(struct power_supply *psy)
131{
132 led_trigger_unregister_simple(psy->online_trig);
133 kfree(psy->online_trig_name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400134}
135
136/* Choice what triggers to create&update. */
137
138void power_supply_update_leds(struct power_supply *psy)
139{
140 if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
141 power_supply_update_bat_leds(psy);
142 else
143 power_supply_update_gen_leds(psy);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400144}
145
146int power_supply_create_triggers(struct power_supply *psy)
147{
148 if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
149 return power_supply_create_bat_triggers(psy);
150 return power_supply_create_gen_triggers(psy);
151}
152
153void power_supply_remove_triggers(struct power_supply *psy)
154{
155 if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
156 power_supply_remove_bat_triggers(psy);
157 else
158 power_supply_remove_gen_triggers(psy);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400159}