Linux 3.12.39
[linux/fpc-iii.git] / drivers / leds / dell-led.c
blobe5c57389efd63680af4b028910f8152667844692
1 /*
2 * dell_led.c - Dell LED Driver
4 * Copyright (C) 2010 Dell Inc.
5 * Louis Davis <louis_davis@dell.com>
6 * Jim Dailey <jim_dailey@dell.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation.
14 #include <linux/acpi.h>
15 #include <linux/leds.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
19 MODULE_AUTHOR("Louis Davis/Jim Dailey");
20 MODULE_DESCRIPTION("Dell LED Control Driver");
21 MODULE_LICENSE("GPL");
23 #define DELL_LED_BIOS_GUID "F6E4FE6E-909D-47cb-8BAB-C9F6F2F8D396"
24 MODULE_ALIAS("wmi:" DELL_LED_BIOS_GUID);
26 /* Error Result Codes: */
27 #define INVALID_DEVICE_ID 250
28 #define INVALID_PARAMETER 251
29 #define INVALID_BUFFER 252
30 #define INTERFACE_ERROR 253
31 #define UNSUPPORTED_COMMAND 254
32 #define UNSPECIFIED_ERROR 255
34 /* Device ID */
35 #define DEVICE_ID_PANEL_BACK 1
37 /* LED Commands */
38 #define CMD_LED_ON 16
39 #define CMD_LED_OFF 17
40 #define CMD_LED_BLINK 18
42 struct bios_args {
43 unsigned char length;
44 unsigned char result_code;
45 unsigned char device_id;
46 unsigned char command;
47 unsigned char on_time;
48 unsigned char off_time;
51 static int dell_led_perform_fn(u8 length,
52 u8 result_code,
53 u8 device_id,
54 u8 command,
55 u8 on_time,
56 u8 off_time)
58 struct bios_args *bios_return;
59 u8 return_code;
60 union acpi_object *obj;
61 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
62 struct acpi_buffer input;
63 acpi_status status;
65 struct bios_args args;
66 args.length = length;
67 args.result_code = result_code;
68 args.device_id = device_id;
69 args.command = command;
70 args.on_time = on_time;
71 args.off_time = off_time;
73 input.length = sizeof(struct bios_args);
74 input.pointer = &args;
76 status = wmi_evaluate_method(DELL_LED_BIOS_GUID,
79 &input,
80 &output);
82 if (ACPI_FAILURE(status))
83 return status;
85 obj = output.pointer;
87 if (!obj)
88 return -EINVAL;
89 else if (obj->type != ACPI_TYPE_BUFFER) {
90 kfree(obj);
91 return -EINVAL;
94 bios_return = ((struct bios_args *)obj->buffer.pointer);
95 return_code = bios_return->result_code;
97 kfree(obj);
99 return return_code;
102 static int led_on(void)
104 return dell_led_perform_fn(3, /* Length of command */
105 INTERFACE_ERROR, /* Init to INTERFACE_ERROR */
106 DEVICE_ID_PANEL_BACK, /* Device ID */
107 CMD_LED_ON, /* Command */
108 0, /* not used */
109 0); /* not used */
112 static int led_off(void)
114 return dell_led_perform_fn(3, /* Length of command */
115 INTERFACE_ERROR, /* Init to INTERFACE_ERROR */
116 DEVICE_ID_PANEL_BACK, /* Device ID */
117 CMD_LED_OFF, /* Command */
118 0, /* not used */
119 0); /* not used */
122 static int led_blink(unsigned char on_eighths,
123 unsigned char off_eighths)
125 return dell_led_perform_fn(5, /* Length of command */
126 INTERFACE_ERROR, /* Init to INTERFACE_ERROR */
127 DEVICE_ID_PANEL_BACK, /* Device ID */
128 CMD_LED_BLINK, /* Command */
129 on_eighths, /* blink on in eigths of a second */
130 off_eighths); /* blink off in eights of a second */
133 static void dell_led_set(struct led_classdev *led_cdev,
134 enum led_brightness value)
136 if (value == LED_OFF)
137 led_off();
138 else
139 led_on();
142 static int dell_led_blink(struct led_classdev *led_cdev,
143 unsigned long *delay_on,
144 unsigned long *delay_off)
146 unsigned long on_eighths;
147 unsigned long off_eighths;
149 /* The Dell LED delay is based on 125ms intervals.
150 Need to round up to next interval. */
152 on_eighths = (*delay_on + 124) / 125;
153 if (0 == on_eighths)
154 on_eighths = 1;
155 if (on_eighths > 255)
156 on_eighths = 255;
157 *delay_on = on_eighths * 125;
159 off_eighths = (*delay_off + 124) / 125;
160 if (0 == off_eighths)
161 off_eighths = 1;
162 if (off_eighths > 255)
163 off_eighths = 255;
164 *delay_off = off_eighths * 125;
166 led_blink(on_eighths, off_eighths);
168 return 0;
171 static struct led_classdev dell_led = {
172 .name = "dell::lid",
173 .brightness = LED_OFF,
174 .max_brightness = 1,
175 .brightness_set = dell_led_set,
176 .blink_set = dell_led_blink,
177 .flags = LED_CORE_SUSPENDRESUME,
180 static int __init dell_led_init(void)
182 int error = 0;
184 if (!wmi_has_guid(DELL_LED_BIOS_GUID))
185 return -ENODEV;
187 error = led_off();
188 if (error != 0)
189 return -ENODEV;
191 return led_classdev_register(NULL, &dell_led);
194 static void __exit dell_led_exit(void)
196 led_classdev_unregister(&dell_led);
198 led_off();
201 module_init(dell_led_init);
202 module_exit(dell_led_exit);