| 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> | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 14 | #include <linux/power_supply.h> | 
|  | 15 |  | 
| Adrian Bunk | 25f12141 | 2007-11-26 00:25:45 +0300 | [diff] [blame] | 16 | #include "power_supply.h" | 
|  | 17 |  | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 18 | /* Battery specific LEDs triggers. */ | 
|  | 19 |  | 
|  | 20 | static 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 Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 27 | dev_dbg(psy->dev, "%s %d\n", __func__, status.intval); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 28 |  | 
|  | 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 Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 46 | } | 
|  | 47 |  | 
|  | 48 | static int power_supply_create_bat_triggers(struct power_supply *psy) | 
|  | 49 | { | 
|  | 50 | int rc = 0; | 
|  | 51 |  | 
| Akinobu Mita | 4d24473 | 2007-11-17 19:55:58 +0900 | [diff] [blame] | 52 | psy->charging_full_trig_name = kasprintf(GFP_KERNEL, | 
|  | 53 | "%s-charging-or-full", psy->name); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 54 | if (!psy->charging_full_trig_name) | 
|  | 55 | goto charging_full_failed; | 
|  | 56 |  | 
| Akinobu Mita | 4d24473 | 2007-11-17 19:55:58 +0900 | [diff] [blame] | 57 | psy->charging_trig_name = kasprintf(GFP_KERNEL, | 
|  | 58 | "%s-charging", psy->name); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 59 | if (!psy->charging_trig_name) | 
|  | 60 | goto charging_failed; | 
|  | 61 |  | 
| Akinobu Mita | 4d24473 | 2007-11-17 19:55:58 +0900 | [diff] [blame] | 62 | psy->full_trig_name = kasprintf(GFP_KERNEL, "%s-full", psy->name); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 63 | if (!psy->full_trig_name) | 
|  | 64 | goto full_failed; | 
|  | 65 |  | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 66 | 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 |  | 
|  | 75 | full_failed: | 
|  | 76 | kfree(psy->charging_trig_name); | 
|  | 77 | charging_failed: | 
|  | 78 | kfree(psy->charging_full_trig_name); | 
|  | 79 | charging_full_failed: | 
|  | 80 | rc = -ENOMEM; | 
|  | 81 | success: | 
|  | 82 | return rc; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | static 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 Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
|  | 95 | /* Generated power specific LEDs triggers. */ | 
|  | 96 |  | 
|  | 97 | static 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 Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 104 | dev_dbg(psy->dev, "%s %d\n", __func__, online.intval); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 105 |  | 
|  | 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 Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
|  | 112 | static int power_supply_create_gen_triggers(struct power_supply *psy) | 
|  | 113 | { | 
|  | 114 | int rc = 0; | 
|  | 115 |  | 
| Akinobu Mita | 4d24473 | 2007-11-17 19:55:58 +0900 | [diff] [blame] | 116 | psy->online_trig_name = kasprintf(GFP_KERNEL, "%s-online", psy->name); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 117 | if (!psy->online_trig_name) | 
|  | 118 | goto online_failed; | 
|  | 119 |  | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 120 | led_trigger_register_simple(psy->online_trig_name, &psy->online_trig); | 
|  | 121 |  | 
|  | 122 | goto success; | 
|  | 123 |  | 
|  | 124 | online_failed: | 
|  | 125 | rc = -ENOMEM; | 
|  | 126 | success: | 
|  | 127 | return rc; | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | static 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 Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
|  | 136 | /* Choice what triggers to create&update. */ | 
|  | 137 |  | 
|  | 138 | void 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 Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
|  | 146 | int 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 |  | 
|  | 153 | void 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 Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 159 | } |