Merge branch 'urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/rric/oprofile...
[firefly-linux-kernel-4.4.55.git] / drivers / platform / x86 / dell-laptop.c
1 /*
2  *  Driver for Dell laptop extras
3  *
4  *  Copyright (c) Red Hat <mjg@redhat.com>
5  *
6  *  Based on documentation in the libsmbios package, Copyright (C) 2005 Dell
7  *  Inc.
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2 as
11  *  published by the Free Software Foundation.
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/platform_device.h>
20 #include <linux/backlight.h>
21 #include <linux/err.h>
22 #include <linux/dmi.h>
23 #include <linux/io.h>
24 #include <linux/power_supply.h>
25 #include <linux/acpi.h>
26 #include <linux/mm.h>
27 #include <linux/i8042.h>
28 #include <linux/slab.h>
29 #include <linux/debugfs.h>
30 #include <linux/seq_file.h>
31 #include "../../firmware/dcdbas.h"
32
33 #define BRIGHTNESS_TOKEN 0x7d
34
35 /* This structure will be modified by the firmware when we enter
36  * system management mode, hence the volatiles */
37
38 struct calling_interface_buffer {
39         u16 class;
40         u16 select;
41         volatile u32 input[4];
42         volatile u32 output[4];
43 } __packed;
44
45 struct calling_interface_token {
46         u16 tokenID;
47         u16 location;
48         union {
49                 u16 value;
50                 u16 stringlength;
51         };
52 };
53
54 struct calling_interface_structure {
55         struct dmi_header header;
56         u16 cmdIOAddress;
57         u8 cmdIOCode;
58         u32 supportedCmds;
59         struct calling_interface_token tokens[];
60 } __packed;
61
62 struct quirk_entry {
63         u8 touchpad_led;
64 };
65
66 static struct quirk_entry *quirks;
67
68 static struct quirk_entry quirk_dell_vostro_v130 = {
69         .touchpad_led = 1,
70 };
71
72 static int dmi_matched(const struct dmi_system_id *dmi)
73 {
74         quirks = dmi->driver_data;
75         return 1;
76 }
77
78 static int da_command_address;
79 static int da_command_code;
80 static int da_num_tokens;
81 static struct calling_interface_token *da_tokens;
82
83 static struct platform_driver platform_driver = {
84         .driver = {
85                 .name = "dell-laptop",
86                 .owner = THIS_MODULE,
87         }
88 };
89
90 static struct platform_device *platform_device;
91 static struct backlight_device *dell_backlight_device;
92
93 static const struct dmi_system_id dell_device_table[] __initconst = {
94         {
95                 .ident = "Dell laptop",
96                 .matches = {
97                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
98                         DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
99                 },
100         },
101         {
102                 .matches = {
103                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
104                         DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
105                 },
106         },
107         {
108                 .ident = "Dell Computer Corporation",
109                 .matches = {
110                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
111                         DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
112                 },
113         },
114         { }
115 };
116 MODULE_DEVICE_TABLE(dmi, dell_device_table);
117
118 static struct dmi_system_id __devinitdata dell_quirks[] = {
119         {
120                 .callback = dmi_matched,
121                 .ident = "Dell Vostro V130",
122                 .matches = {
123                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
124                         DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
125                 },
126                 .driver_data = &quirk_dell_vostro_v130,
127         },
128         {
129                 .callback = dmi_matched,
130                 .ident = "Dell Vostro V131",
131                 .matches = {
132                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
133                         DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
134                 },
135                 .driver_data = &quirk_dell_vostro_v130,
136         },
137         {
138                 .callback = dmi_matched,
139                 .ident = "Dell Vostro 3350",
140                 .matches = {
141                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
142                         DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
143                 },
144                 .driver_data = &quirk_dell_vostro_v130,
145         },
146         {
147                 .callback = dmi_matched,
148                 .ident = "Dell Vostro 3555",
149                 .matches = {
150                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
151                         DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
152                 },
153                 .driver_data = &quirk_dell_vostro_v130,
154         },
155         {
156                 .callback = dmi_matched,
157                 .ident = "Dell Inspiron N311z",
158                 .matches = {
159                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
160                         DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
161                 },
162                 .driver_data = &quirk_dell_vostro_v130,
163         },
164         {
165                 .callback = dmi_matched,
166                 .ident = "Dell Inspiron M5110",
167                 .matches = {
168                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
169                         DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
170                 },
171                 .driver_data = &quirk_dell_vostro_v130,
172         },
173         {
174                 .callback = dmi_matched,
175                 .ident = "Dell Vostro 3360",
176                 .matches = {
177                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
178                         DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
179                 },
180                 .driver_data = &quirk_dell_vostro_v130,
181         },
182         {
183                 .callback = dmi_matched,
184                 .ident = "Dell Vostro 3460",
185                 .matches = {
186                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
187                         DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
188                 },
189                 .driver_data = &quirk_dell_vostro_v130,
190         },
191         {
192                 .callback = dmi_matched,
193                 .ident = "Dell Vostro 3560",
194                 .matches = {
195                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
196                         DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
197                 },
198                 .driver_data = &quirk_dell_vostro_v130,
199         },
200         {
201                 .callback = dmi_matched,
202                 .ident = "Dell Vostro 3450",
203                 .matches = {
204                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
205                         DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
206                 },
207                 .driver_data = &quirk_dell_vostro_v130,
208         },
209         { }
210 };
211
212 static struct calling_interface_buffer *buffer;
213 static struct page *bufferpage;
214 static DEFINE_MUTEX(buffer_mutex);
215
216 static int hwswitch_state;
217
218 static void get_buffer(void)
219 {
220         mutex_lock(&buffer_mutex);
221         memset(buffer, 0, sizeof(struct calling_interface_buffer));
222 }
223
224 static void release_buffer(void)
225 {
226         mutex_unlock(&buffer_mutex);
227 }
228
229 static void __init parse_da_table(const struct dmi_header *dm)
230 {
231         /* Final token is a terminator, so we don't want to copy it */
232         int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
233         struct calling_interface_structure *table =
234                 container_of(dm, struct calling_interface_structure, header);
235
236         /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
237            6 bytes of entry */
238
239         if (dm->length < 17)
240                 return;
241
242         da_command_address = table->cmdIOAddress;
243         da_command_code = table->cmdIOCode;
244
245         da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
246                              sizeof(struct calling_interface_token),
247                              GFP_KERNEL);
248
249         if (!da_tokens)
250                 return;
251
252         memcpy(da_tokens+da_num_tokens, table->tokens,
253                sizeof(struct calling_interface_token) * tokens);
254
255         da_num_tokens += tokens;
256 }
257
258 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
259 {
260         switch (dm->type) {
261         case 0xd4: /* Indexed IO */
262         case 0xd5: /* Protected Area Type 1 */
263         case 0xd6: /* Protected Area Type 2 */
264                 break;
265         case 0xda: /* Calling interface */
266                 parse_da_table(dm);
267                 break;
268         }
269 }
270
271 static int find_token_location(int tokenid)
272 {
273         int i;
274         for (i = 0; i < da_num_tokens; i++) {
275                 if (da_tokens[i].tokenID == tokenid)
276                         return da_tokens[i].location;
277         }
278
279         return -1;
280 }
281
282 static struct calling_interface_buffer *
283 dell_send_request(struct calling_interface_buffer *buffer, int class,
284                   int select)
285 {
286         struct smi_cmd command;
287
288         command.magic = SMI_CMD_MAGIC;
289         command.command_address = da_command_address;
290         command.command_code = da_command_code;
291         command.ebx = virt_to_phys(buffer);
292         command.ecx = 0x42534931;
293
294         buffer->class = class;
295         buffer->select = select;
296
297         dcdbas_smi_request(&command);
298
299         return buffer;
300 }
301
302 static struct dentry *dell_laptop_dir;
303
304 static int dell_debugfs_show(struct seq_file *s, void *data)
305 {
306         int status;
307
308         get_buffer();
309         dell_send_request(buffer, 17, 11);
310         status = buffer->output[1];
311         release_buffer();
312
313         seq_printf(s, "status:\t0x%X\n", status);
314         seq_printf(s, "Bit 0 : Hardware switch supported:   %lu\n",
315                    status & BIT(0));
316         seq_printf(s, "Bit 1 : Wifi locator supported:      %lu\n",
317                   (status & BIT(1)) >> 1);
318         seq_printf(s, "Bit 2 : Wifi is supported:           %lu\n",
319                   (status & BIT(2)) >> 2);
320         seq_printf(s, "Bit 3 : Bluetooth is supported:      %lu\n",
321                   (status & BIT(3)) >> 3);
322         seq_printf(s, "Bit 4 : WWAN is supported:           %lu\n",
323                   (status & BIT(4)) >> 4);
324         seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
325                   (status & BIT(5)) >> 5);
326         seq_printf(s, "Bit 8 : Wifi is installed:           %lu\n",
327                   (status & BIT(8)) >> 8);
328         seq_printf(s, "Bit 9 : Bluetooth is installed:      %lu\n",
329                   (status & BIT(9)) >> 9);
330         seq_printf(s, "Bit 10: WWAN is installed:           %lu\n",
331                   (status & BIT(10)) >> 10);
332         seq_printf(s, "Bit 16: Hardware switch is on:       %lu\n",
333                   (status & BIT(16)) >> 16);
334         seq_printf(s, "Bit 17: Wifi is blocked:             %lu\n",
335                   (status & BIT(17)) >> 17);
336         seq_printf(s, "Bit 18: Bluetooth is blocked:        %lu\n",
337                   (status & BIT(18)) >> 18);
338         seq_printf(s, "Bit 19: WWAN is blocked:             %lu\n",
339                   (status & BIT(19)) >> 19);
340
341         seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
342         seq_printf(s, "Bit 0 : Wifi controlled by switch:      %lu\n",
343                    hwswitch_state & BIT(0));
344         seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
345                    (hwswitch_state & BIT(1)) >> 1);
346         seq_printf(s, "Bit 2 : WWAN controlled by switch:      %lu\n",
347                    (hwswitch_state & BIT(2)) >> 2);
348         seq_printf(s, "Bit 7 : Wireless switch config locked:  %lu\n",
349                    (hwswitch_state & BIT(7)) >> 7);
350         seq_printf(s, "Bit 8 : Wifi locator enabled:           %lu\n",
351                    (hwswitch_state & BIT(8)) >> 8);
352         seq_printf(s, "Bit 15: Wifi locator setting locked:    %lu\n",
353                    (hwswitch_state & BIT(15)) >> 15);
354
355         return 0;
356 }
357
358 static int dell_debugfs_open(struct inode *inode, struct file *file)
359 {
360         return single_open(file, dell_debugfs_show, inode->i_private);
361 }
362
363 static const struct file_operations dell_debugfs_fops = {
364         .owner = THIS_MODULE,
365         .open = dell_debugfs_open,
366         .read = seq_read,
367         .llseek = seq_lseek,
368         .release = single_release,
369 };
370
371 static int dell_send_intensity(struct backlight_device *bd)
372 {
373         int ret = 0;
374
375         get_buffer();
376         buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
377         buffer->input[1] = bd->props.brightness;
378
379         if (buffer->input[0] == -1) {
380                 ret = -ENODEV;
381                 goto out;
382         }
383
384         if (power_supply_is_system_supplied() > 0)
385                 dell_send_request(buffer, 1, 2);
386         else
387                 dell_send_request(buffer, 1, 1);
388
389 out:
390         release_buffer();
391         return 0;
392 }
393
394 static int dell_get_intensity(struct backlight_device *bd)
395 {
396         int ret = 0;
397
398         get_buffer();
399         buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
400
401         if (buffer->input[0] == -1) {
402                 ret = -ENODEV;
403                 goto out;
404         }
405
406         if (power_supply_is_system_supplied() > 0)
407                 dell_send_request(buffer, 0, 2);
408         else
409                 dell_send_request(buffer, 0, 1);
410
411         ret = buffer->output[1];
412
413 out:
414         release_buffer();
415         return ret;
416 }
417
418 static const struct backlight_ops dell_ops = {
419         .get_brightness = dell_get_intensity,
420         .update_status  = dell_send_intensity,
421 };
422
423 static void touchpad_led_on(void)
424 {
425         int command = 0x97;
426         char data = 1;
427         i8042_command(&data, command | 1 << 12);
428 }
429
430 static void touchpad_led_off(void)
431 {
432         int command = 0x97;
433         char data = 2;
434         i8042_command(&data, command | 1 << 12);
435 }
436
437 static void touchpad_led_set(struct led_classdev *led_cdev,
438         enum led_brightness value)
439 {
440         if (value > 0)
441                 touchpad_led_on();
442         else
443                 touchpad_led_off();
444 }
445
446 static struct led_classdev touchpad_led = {
447         .name = "dell-laptop::touchpad",
448         .brightness_set = touchpad_led_set,
449         .flags = LED_CORE_SUSPENDRESUME,
450 };
451
452 static int __devinit touchpad_led_init(struct device *dev)
453 {
454         return led_classdev_register(dev, &touchpad_led);
455 }
456
457 static void touchpad_led_exit(void)
458 {
459         led_classdev_unregister(&touchpad_led);
460 }
461
462 static int __init dell_init(void)
463 {
464         int max_intensity = 0;
465         int ret;
466
467         if (!dmi_check_system(dell_device_table))
468                 return -ENODEV;
469
470         quirks = NULL;
471         /* find if this machine support other functions */
472         dmi_check_system(dell_quirks);
473
474         dmi_walk(find_tokens, NULL);
475
476         if (!da_tokens)  {
477                 pr_info("Unable to find dmi tokens\n");
478                 return -ENODEV;
479         }
480
481         ret = platform_driver_register(&platform_driver);
482         if (ret)
483                 goto fail_platform_driver;
484         platform_device = platform_device_alloc("dell-laptop", -1);
485         if (!platform_device) {
486                 ret = -ENOMEM;
487                 goto fail_platform_device1;
488         }
489         ret = platform_device_add(platform_device);
490         if (ret)
491                 goto fail_platform_device2;
492
493         /*
494          * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
495          * is passed to SMI handler.
496          */
497         bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
498
499         if (!bufferpage)
500                 goto fail_buffer;
501         buffer = page_address(bufferpage);
502
503         if (quirks && quirks->touchpad_led)
504                 touchpad_led_init(&platform_device->dev);
505
506         dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
507
508 #ifdef CONFIG_ACPI
509         /* In the event of an ACPI backlight being available, don't
510          * register the platform controller.
511          */
512         if (acpi_video_backlight_support())
513                 return 0;
514 #endif
515
516         get_buffer();
517         buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
518         if (buffer->input[0] != -1) {
519                 dell_send_request(buffer, 0, 2);
520                 max_intensity = buffer->output[3];
521         }
522         release_buffer();
523
524         if (max_intensity) {
525                 struct backlight_properties props;
526                 memset(&props, 0, sizeof(struct backlight_properties));
527                 props.type = BACKLIGHT_PLATFORM;
528                 props.max_brightness = max_intensity;
529                 dell_backlight_device = backlight_device_register("dell_backlight",
530                                                                   &platform_device->dev,
531                                                                   NULL,
532                                                                   &dell_ops,
533                                                                   &props);
534
535                 if (IS_ERR(dell_backlight_device)) {
536                         ret = PTR_ERR(dell_backlight_device);
537                         dell_backlight_device = NULL;
538                         goto fail_backlight;
539                 }
540
541                 dell_backlight_device->props.brightness =
542                         dell_get_intensity(dell_backlight_device);
543                 backlight_update_status(dell_backlight_device);
544         }
545
546         return 0;
547
548 fail_backlight:
549         free_page((unsigned long)bufferpage);
550 fail_buffer:
551         platform_device_del(platform_device);
552 fail_platform_device2:
553         platform_device_put(platform_device);
554 fail_platform_device1:
555         platform_driver_unregister(&platform_driver);
556 fail_platform_driver:
557         kfree(da_tokens);
558         return ret;
559 }
560
561 static void __exit dell_exit(void)
562 {
563         debugfs_remove_recursive(dell_laptop_dir);
564         if (quirks && quirks->touchpad_led)
565                 touchpad_led_exit();
566         backlight_device_unregister(dell_backlight_device);
567         if (platform_device) {
568                 platform_device_unregister(platform_device);
569                 platform_driver_unregister(&platform_driver);
570         }
571         kfree(da_tokens);
572         free_page((unsigned long)buffer);
573 }
574
575 module_init(dell_init);
576 module_exit(dell_exit);
577
578 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
579 MODULE_DESCRIPTION("Dell laptop driver");
580 MODULE_LICENSE("GPL");