blob: 2d41a43fc81acb967639b012b4e7811bab29572e [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>
Paul Gortmaker51990e82012-01-22 11:23:42 -050014#include <linux/device.h>
Anton Vorontsov4a11b592007-05-04 00:27:45 +040015#include <linux/power_supply.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Anton Vorontsov4a11b592007-05-04 00:27:45 +040017
Adrian Bunk25f121412007-11-26 00:25:45 +030018#include "power_supply.h"
19
Anton Vorontsov4a11b592007-05-04 00:27:45 +040020/* Battery specific LEDs triggers. */
21
22static void power_supply_update_bat_leds(struct power_supply *psy)
23{
24 union power_supply_propval status;
Vasily Khoruzhick6501f722011-01-07 18:28:17 +020025 unsigned long delay_on = 0;
26 unsigned long delay_off = 0;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040027
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010028 if (psy->desc->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status))
Anton Vorontsov4a11b592007-05-04 00:27:45 +040029 return;
30
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010031 dev_dbg(&psy->dev, "%s %d\n", __func__, status.intval);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040032
33 switch (status.intval) {
34 case POWER_SUPPLY_STATUS_FULL:
35 led_trigger_event(psy->charging_full_trig, LED_FULL);
36 led_trigger_event(psy->charging_trig, LED_OFF);
37 led_trigger_event(psy->full_trig, LED_FULL);
Vasily Khoruzhick6501f722011-01-07 18:28:17 +020038 led_trigger_event(psy->charging_blink_full_solid_trig,
39 LED_FULL);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040040 break;
41 case POWER_SUPPLY_STATUS_CHARGING:
42 led_trigger_event(psy->charging_full_trig, LED_FULL);
43 led_trigger_event(psy->charging_trig, LED_FULL);
44 led_trigger_event(psy->full_trig, LED_OFF);
Vasily Khoruzhick6501f722011-01-07 18:28:17 +020045 led_trigger_blink(psy->charging_blink_full_solid_trig,
46 &delay_on, &delay_off);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040047 break;
48 default:
49 led_trigger_event(psy->charging_full_trig, LED_OFF);
50 led_trigger_event(psy->charging_trig, LED_OFF);
51 led_trigger_event(psy->full_trig, LED_OFF);
Vasily Khoruzhick6501f722011-01-07 18:28:17 +020052 led_trigger_event(psy->charging_blink_full_solid_trig,
53 LED_OFF);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040054 break;
55 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +040056}
57
58static int power_supply_create_bat_triggers(struct power_supply *psy)
59{
Akinobu Mita4d244732007-11-17 19:55:58 +090060 psy->charging_full_trig_name = kasprintf(GFP_KERNEL,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010061 "%s-charging-or-full", psy->desc->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040062 if (!psy->charging_full_trig_name)
63 goto charging_full_failed;
64
Akinobu Mita4d244732007-11-17 19:55:58 +090065 psy->charging_trig_name = kasprintf(GFP_KERNEL,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010066 "%s-charging", psy->desc->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040067 if (!psy->charging_trig_name)
68 goto charging_failed;
69
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010070 psy->full_trig_name = kasprintf(GFP_KERNEL, "%s-full", psy->desc->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040071 if (!psy->full_trig_name)
72 goto full_failed;
73
Vasily Khoruzhick6501f722011-01-07 18:28:17 +020074 psy->charging_blink_full_solid_trig_name = kasprintf(GFP_KERNEL,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010075 "%s-charging-blink-full-solid", psy->desc->name);
Vasily Khoruzhick6501f722011-01-07 18:28:17 +020076 if (!psy->charging_blink_full_solid_trig_name)
77 goto charging_blink_full_solid_failed;
78
Anton Vorontsov4a11b592007-05-04 00:27:45 +040079 led_trigger_register_simple(psy->charging_full_trig_name,
80 &psy->charging_full_trig);
81 led_trigger_register_simple(psy->charging_trig_name,
82 &psy->charging_trig);
83 led_trigger_register_simple(psy->full_trig_name,
84 &psy->full_trig);
Vasily Khoruzhick6501f722011-01-07 18:28:17 +020085 led_trigger_register_simple(psy->charging_blink_full_solid_trig_name,
86 &psy->charging_blink_full_solid_trig);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040087
Viresh Kumar464069c2014-09-04 17:31:35 +053088 return 0;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040089
Vasily Khoruzhick6501f722011-01-07 18:28:17 +020090charging_blink_full_solid_failed:
91 kfree(psy->full_trig_name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040092full_failed:
93 kfree(psy->charging_trig_name);
94charging_failed:
95 kfree(psy->charging_full_trig_name);
96charging_full_failed:
Viresh Kumar464069c2014-09-04 17:31:35 +053097 return -ENOMEM;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040098}
99
100static void power_supply_remove_bat_triggers(struct power_supply *psy)
101{
102 led_trigger_unregister_simple(psy->charging_full_trig);
103 led_trigger_unregister_simple(psy->charging_trig);
104 led_trigger_unregister_simple(psy->full_trig);
Vasily Khoruzhick6501f722011-01-07 18:28:17 +0200105 led_trigger_unregister_simple(psy->charging_blink_full_solid_trig);
106 kfree(psy->charging_blink_full_solid_trig_name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400107 kfree(psy->full_trig_name);
108 kfree(psy->charging_trig_name);
109 kfree(psy->charging_full_trig_name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400110}
111
112/* Generated power specific LEDs triggers. */
113
114static void power_supply_update_gen_leds(struct power_supply *psy)
115{
116 union power_supply_propval online;
117
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100118 if (psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online))
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400119 return;
120
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100121 dev_dbg(&psy->dev, "%s %d\n", __func__, online.intval);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400122
123 if (online.intval)
124 led_trigger_event(psy->online_trig, LED_FULL);
125 else
126 led_trigger_event(psy->online_trig, LED_OFF);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400127}
128
129static int power_supply_create_gen_triggers(struct power_supply *psy)
130{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100131 psy->online_trig_name = kasprintf(GFP_KERNEL, "%s-online",
132 psy->desc->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400133 if (!psy->online_trig_name)
Viresh Kumar464069c2014-09-04 17:31:35 +0530134 return -ENOMEM;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400135
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400136 led_trigger_register_simple(psy->online_trig_name, &psy->online_trig);
137
Viresh Kumar464069c2014-09-04 17:31:35 +0530138 return 0;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400139}
140
141static void power_supply_remove_gen_triggers(struct power_supply *psy)
142{
143 led_trigger_unregister_simple(psy->online_trig);
144 kfree(psy->online_trig_name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400145}
146
147/* Choice what triggers to create&update. */
148
149void power_supply_update_leds(struct power_supply *psy)
150{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100151 if (psy->desc->type == POWER_SUPPLY_TYPE_BATTERY)
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400152 power_supply_update_bat_leds(psy);
153 else
154 power_supply_update_gen_leds(psy);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400155}
156
157int power_supply_create_triggers(struct power_supply *psy)
158{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100159 if (psy->desc->type == POWER_SUPPLY_TYPE_BATTERY)
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400160 return power_supply_create_bat_triggers(psy);
161 return power_supply_create_gen_triggers(psy);
162}
163
164void power_supply_remove_triggers(struct power_supply *psy)
165{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100166 if (psy->desc->type == POWER_SUPPLY_TYPE_BATTERY)
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400167 power_supply_remove_bat_triggers(psy);
168 else
169 power_supply_remove_gen_triggers(psy);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400170}