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 | |
Akinobu Mita | 4d24473 | 2007-11-17 19:55:58 +0900 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 14 | #include <linux/device.h> |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 15 | #include <linux/power_supply.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 17 | |
Adrian Bunk | 25f12141 | 2007-11-26 00:25:45 +0300 | [diff] [blame] | 18 | #include "power_supply.h" |
| 19 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 20 | /* Battery specific LEDs triggers. */ |
| 21 | |
| 22 | static void power_supply_update_bat_leds(struct power_supply *psy) |
| 23 | { |
| 24 | union power_supply_propval status; |
Vasily Khoruzhick | 6501f72 | 2011-01-07 18:28:17 +0200 | [diff] [blame] | 25 | unsigned long delay_on = 0; |
| 26 | unsigned long delay_off = 0; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 27 | |
| 28 | if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status)) |
| 29 | return; |
| 30 | |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 31 | dev_dbg(psy->dev, "%s %d\n", __func__, status.intval); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 32 | |
| 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 Khoruzhick | 6501f72 | 2011-01-07 18:28:17 +0200 | [diff] [blame] | 38 | led_trigger_event(psy->charging_blink_full_solid_trig, |
| 39 | LED_FULL); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 40 | 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 Khoruzhick | 6501f72 | 2011-01-07 18:28:17 +0200 | [diff] [blame] | 45 | led_trigger_blink(psy->charging_blink_full_solid_trig, |
| 46 | &delay_on, &delay_off); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 47 | 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 Khoruzhick | 6501f72 | 2011-01-07 18:28:17 +0200 | [diff] [blame] | 52 | led_trigger_event(psy->charging_blink_full_solid_trig, |
| 53 | LED_OFF); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 54 | break; |
| 55 | } |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static int power_supply_create_bat_triggers(struct power_supply *psy) |
| 59 | { |
Akinobu Mita | 4d24473 | 2007-11-17 19:55:58 +0900 | [diff] [blame] | 60 | psy->charging_full_trig_name = kasprintf(GFP_KERNEL, |
| 61 | "%s-charging-or-full", psy->name); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 62 | if (!psy->charging_full_trig_name) |
| 63 | goto charging_full_failed; |
| 64 | |
Akinobu Mita | 4d24473 | 2007-11-17 19:55:58 +0900 | [diff] [blame] | 65 | psy->charging_trig_name = kasprintf(GFP_KERNEL, |
| 66 | "%s-charging", psy->name); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 67 | if (!psy->charging_trig_name) |
| 68 | goto charging_failed; |
| 69 | |
Akinobu Mita | 4d24473 | 2007-11-17 19:55:58 +0900 | [diff] [blame] | 70 | psy->full_trig_name = kasprintf(GFP_KERNEL, "%s-full", psy->name); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 71 | if (!psy->full_trig_name) |
| 72 | goto full_failed; |
| 73 | |
Vasily Khoruzhick | 6501f72 | 2011-01-07 18:28:17 +0200 | [diff] [blame] | 74 | psy->charging_blink_full_solid_trig_name = kasprintf(GFP_KERNEL, |
| 75 | "%s-charging-blink-full-solid", psy->name); |
| 76 | if (!psy->charging_blink_full_solid_trig_name) |
| 77 | goto charging_blink_full_solid_failed; |
| 78 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 79 | 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 Khoruzhick | 6501f72 | 2011-01-07 18:28:17 +0200 | [diff] [blame] | 85 | led_trigger_register_simple(psy->charging_blink_full_solid_trig_name, |
| 86 | &psy->charging_blink_full_solid_trig); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 87 | |
Viresh Kumar | 464069c | 2014-09-04 17:31:35 +0530 | [diff] [blame^] | 88 | return 0; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 89 | |
Vasily Khoruzhick | 6501f72 | 2011-01-07 18:28:17 +0200 | [diff] [blame] | 90 | charging_blink_full_solid_failed: |
| 91 | kfree(psy->full_trig_name); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 92 | full_failed: |
| 93 | kfree(psy->charging_trig_name); |
| 94 | charging_failed: |
| 95 | kfree(psy->charging_full_trig_name); |
| 96 | charging_full_failed: |
Viresh Kumar | 464069c | 2014-09-04 17:31:35 +0530 | [diff] [blame^] | 97 | return -ENOMEM; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static 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 Khoruzhick | 6501f72 | 2011-01-07 18:28:17 +0200 | [diff] [blame] | 105 | led_trigger_unregister_simple(psy->charging_blink_full_solid_trig); |
| 106 | kfree(psy->charging_blink_full_solid_trig_name); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 107 | kfree(psy->full_trig_name); |
| 108 | kfree(psy->charging_trig_name); |
| 109 | kfree(psy->charging_full_trig_name); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* Generated power specific LEDs triggers. */ |
| 113 | |
| 114 | static void power_supply_update_gen_leds(struct power_supply *psy) |
| 115 | { |
| 116 | union power_supply_propval online; |
| 117 | |
| 118 | if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online)) |
| 119 | return; |
| 120 | |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 121 | dev_dbg(psy->dev, "%s %d\n", __func__, online.intval); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 122 | |
| 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 Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static int power_supply_create_gen_triggers(struct power_supply *psy) |
| 130 | { |
Akinobu Mita | 4d24473 | 2007-11-17 19:55:58 +0900 | [diff] [blame] | 131 | psy->online_trig_name = kasprintf(GFP_KERNEL, "%s-online", psy->name); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 132 | if (!psy->online_trig_name) |
Viresh Kumar | 464069c | 2014-09-04 17:31:35 +0530 | [diff] [blame^] | 133 | return -ENOMEM; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 134 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 135 | led_trigger_register_simple(psy->online_trig_name, &psy->online_trig); |
| 136 | |
Viresh Kumar | 464069c | 2014-09-04 17:31:35 +0530 | [diff] [blame^] | 137 | return 0; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static void power_supply_remove_gen_triggers(struct power_supply *psy) |
| 141 | { |
| 142 | led_trigger_unregister_simple(psy->online_trig); |
| 143 | kfree(psy->online_trig_name); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* Choice what triggers to create&update. */ |
| 147 | |
| 148 | void power_supply_update_leds(struct power_supply *psy) |
| 149 | { |
| 150 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY) |
| 151 | power_supply_update_bat_leds(psy); |
| 152 | else |
| 153 | power_supply_update_gen_leds(psy); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | int power_supply_create_triggers(struct power_supply *psy) |
| 157 | { |
| 158 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY) |
| 159 | return power_supply_create_bat_triggers(psy); |
| 160 | return power_supply_create_gen_triggers(psy); |
| 161 | } |
| 162 | |
| 163 | void power_supply_remove_triggers(struct power_supply *psy) |
| 164 | { |
| 165 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY) |
| 166 | power_supply_remove_bat_triggers(psy); |
| 167 | else |
| 168 | power_supply_remove_gen_triggers(psy); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 169 | } |