msi-laptop: Support standard ec 66/62 command on MSI notebook and nebook
[profile/ivi/kernel-adaptation-intel-automotive.git] / drivers / platform / x86 / msi-laptop.c
1 /*-*-linux-c-*-*/
2
3 /*
4   Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or
9   (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19   02110-1301, USA.
20  */
21
22 /*
23  * msi-laptop.c - MSI S270 laptop support. This laptop is sold under
24  * various brands, including "Cytron/TCM/Medion/Tchibo MD96100".
25  *
26  * Driver also supports S271, S420 models.
27  *
28  * This driver exports a few files in /sys/devices/platform/msi-laptop-pf/:
29  *
30  *   lcd_level - Screen brightness: contains a single integer in the
31  *   range 0..8. (rw)
32  *
33  *   auto_brightness - Enable automatic brightness control: contains
34  *   either 0 or 1. If set to 1 the hardware adjusts the screen
35  *   brightness automatically when the power cord is
36  *   plugged/unplugged. (rw)
37  *
38  *   wlan - WLAN subsystem enabled: contains either 0 or 1. (ro)
39  *
40  *   bluetooth - Bluetooth subsystem enabled: contains either 0 or 1
41  *   Please note that this file is constantly 0 if no Bluetooth
42  *   hardware is available. (ro)
43  *
44  * In addition to these platform device attributes the driver
45  * registers itself in the Linux backlight control subsystem and is
46  * available to userspace under /sys/class/backlight/msi-laptop-bl/.
47  *
48  * This driver might work on other laptops produced by MSI. If you
49  * want to try it you can pass force=1 as argument to the module which
50  * will force it to load even when the DMI data doesn't identify the
51  * laptop as MSI S270. YMMV.
52  */
53
54 #include <linux/module.h>
55 #include <linux/kernel.h>
56 #include <linux/init.h>
57 #include <linux/acpi.h>
58 #include <linux/dmi.h>
59 #include <linux/backlight.h>
60 #include <linux/platform_device.h>
61
62 #define MSI_DRIVER_VERSION "0.5"
63
64 #define MSI_LCD_LEVEL_MAX 9
65
66 #define MSI_EC_COMMAND_WIRELESS 0x10
67 #define MSI_EC_COMMAND_LCD_LEVEL 0x11
68
69 #define MSI_STANDARD_EC_COMMAND_ADDRESS 0x2e
70 #define MSI_STANDARD_EC_BLUETOOTH_MASK  (1 << 0)
71 #define MSI_STANDARD_EC_WEBCAM_MASK     (1 << 1)
72 #define MSI_STANDARD_EC_WLAN_MASK       (1 << 3)
73
74 static int force;
75 module_param(force, bool, 0);
76 MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
77
78 static int auto_brightness;
79 module_param(auto_brightness, int, 0);
80 MODULE_PARM_DESC(auto_brightness, "Enable automatic brightness control (0: disabled; 1: enabled; 2: don't touch)");
81
82 static bool old_ec_model;
83 static int wlan_s, bluetooth_s;
84
85 /* Hardware access */
86
87 static int set_lcd_level(int level)
88 {
89         u8 buf[2];
90
91         if (level < 0 || level >= MSI_LCD_LEVEL_MAX)
92                 return -EINVAL;
93
94         buf[0] = 0x80;
95         buf[1] = (u8) (level*31);
96
97         return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, buf, sizeof(buf), NULL, 0, 1);
98 }
99
100 static int get_lcd_level(void)
101 {
102         u8 wdata = 0, rdata;
103         int result;
104
105         result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
106         if (result < 0)
107                 return result;
108
109         return (int) rdata / 31;
110 }
111
112 static int get_auto_brightness(void)
113 {
114         u8 wdata = 4, rdata;
115         int result;
116
117         result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
118         if (result < 0)
119                 return result;
120
121         return !!(rdata & 8);
122 }
123
124 static int set_auto_brightness(int enable)
125 {
126         u8 wdata[2], rdata;
127         int result;
128
129         wdata[0] = 4;
130
131         result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 1, &rdata, 1, 1);
132         if (result < 0)
133                 return result;
134
135         wdata[0] = 0x84;
136         wdata[1] = (rdata & 0xF7) | (enable ? 8 : 0);
137
138         return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 2, NULL, 0, 1);
139 }
140
141 static int get_wireless_state(int *wlan, int *bluetooth)
142 {
143         u8 wdata = 0, rdata;
144         int result;
145
146         result = ec_transaction(MSI_EC_COMMAND_WIRELESS, &wdata, 1, &rdata, 1, 1);
147         if (result < 0)
148                 return -1;
149
150         if (wlan)
151                 *wlan = !!(rdata & 8);
152
153         if (bluetooth)
154                 *bluetooth = !!(rdata & 128);
155
156         return 0;
157 }
158
159 static int get_wireless_state_ec_standard(void)
160 {
161         u8 rdata;
162         int result;
163
164         result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
165         if (result < 0)
166                 return -1;
167
168         wlan_s = !!(rdata & MSI_STANDARD_EC_WLAN_MASK);
169
170         bluetooth_s = !!(rdata & MSI_STANDARD_EC_BLUETOOTH_MASK);
171
172         return 0;
173 }
174
175 /* Backlight device stuff */
176
177 static int bl_get_brightness(struct backlight_device *b)
178 {
179         return get_lcd_level();
180 }
181
182
183 static int bl_update_status(struct backlight_device *b)
184 {
185         return set_lcd_level(b->props.brightness);
186 }
187
188 static struct backlight_ops msibl_ops = {
189         .get_brightness = bl_get_brightness,
190         .update_status  = bl_update_status,
191 };
192
193 static struct backlight_device *msibl_device;
194
195 /* Platform device */
196
197 static ssize_t show_wlan(struct device *dev,
198         struct device_attribute *attr, char *buf)
199 {
200
201         int ret, enabled;
202
203         if (old_ec_model) {
204                 ret = get_wireless_state(&enabled, NULL);
205         } else {
206                 ret = get_wireless_state_ec_standard();
207                 enabled = wlan_s;
208         }
209         if (ret < 0)
210                 return ret;
211
212         return sprintf(buf, "%i\n", enabled);
213 }
214
215 static ssize_t show_bluetooth(struct device *dev,
216         struct device_attribute *attr, char *buf)
217 {
218
219         int ret, enabled;
220
221         if (old_ec_model) {
222                 ret = get_wireless_state(NULL, &enabled);
223         } else {
224                 ret = get_wireless_state_ec_standard();
225                 enabled = bluetooth_s;
226         }
227         if (ret < 0)
228                 return ret;
229
230         return sprintf(buf, "%i\n", enabled);
231 }
232
233 static ssize_t show_lcd_level(struct device *dev,
234         struct device_attribute *attr, char *buf)
235 {
236
237         int ret;
238
239         ret = get_lcd_level();
240         if (ret < 0)
241                 return ret;
242
243         return sprintf(buf, "%i\n", ret);
244 }
245
246 static ssize_t store_lcd_level(struct device *dev,
247         struct device_attribute *attr, const char *buf, size_t count)
248 {
249
250         int level, ret;
251
252         if (sscanf(buf, "%i", &level) != 1 || (level < 0 || level >= MSI_LCD_LEVEL_MAX))
253                 return -EINVAL;
254
255         ret = set_lcd_level(level);
256         if (ret < 0)
257                 return ret;
258
259         return count;
260 }
261
262 static ssize_t show_auto_brightness(struct device *dev,
263         struct device_attribute *attr, char *buf)
264 {
265
266         int ret;
267
268         ret = get_auto_brightness();
269         if (ret < 0)
270                 return ret;
271
272         return sprintf(buf, "%i\n", ret);
273 }
274
275 static ssize_t store_auto_brightness(struct device *dev,
276         struct device_attribute *attr, const char *buf, size_t count)
277 {
278
279         int enable, ret;
280
281         if (sscanf(buf, "%i", &enable) != 1 || (enable != (enable & 1)))
282                 return -EINVAL;
283
284         ret = set_auto_brightness(enable);
285         if (ret < 0)
286                 return ret;
287
288         return count;
289 }
290
291 static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
292 static DEVICE_ATTR(auto_brightness, 0644, show_auto_brightness, store_auto_brightness);
293 static DEVICE_ATTR(bluetooth, 0444, show_bluetooth, NULL);
294 static DEVICE_ATTR(wlan, 0444, show_wlan, NULL);
295
296 static struct attribute *msipf_attributes[] = {
297         &dev_attr_lcd_level.attr,
298         &dev_attr_auto_brightness.attr,
299         &dev_attr_bluetooth.attr,
300         &dev_attr_wlan.attr,
301         NULL
302 };
303
304 static struct attribute_group msipf_attribute_group = {
305         .attrs = msipf_attributes
306 };
307
308 static struct platform_driver msipf_driver = {
309         .driver = {
310                 .name = "msi-laptop-pf",
311                 .owner = THIS_MODULE,
312         }
313 };
314
315 static struct platform_device *msipf_device;
316
317 /* Initialization */
318
319 static int dmi_check_cb(const struct dmi_system_id *id)
320 {
321         printk("msi-laptop: Identified laptop model '%s'.\n", id->ident);
322         return 0;
323 }
324
325 static struct dmi_system_id __initdata msi_dmi_table[] = {
326         {
327                 .ident = "MSI S270",
328                 .matches = {
329                         DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
330                         DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
331                         DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
332                         DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR INT'L CO.,LTD")
333                 },
334                 .callback = dmi_check_cb
335         },
336         {
337                 .ident = "MSI S271",
338                 .matches = {
339                         DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
340                         DMI_MATCH(DMI_PRODUCT_NAME, "MS-1058"),
341                         DMI_MATCH(DMI_PRODUCT_VERSION, "0581"),
342                         DMI_MATCH(DMI_BOARD_NAME, "MS-1058")
343                 },
344                 .callback = dmi_check_cb
345         },
346         {
347                 .ident = "MSI S420",
348                 .matches = {
349                         DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
350                         DMI_MATCH(DMI_PRODUCT_NAME, "MS-1412"),
351                         DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
352                         DMI_MATCH(DMI_BOARD_NAME, "MS-1412")
353                 },
354                 .callback = dmi_check_cb
355         },
356         {
357                 .ident = "Medion MD96100",
358                 .matches = {
359                         DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
360                         DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
361                         DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
362                         DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR INT'L CO.,LTD")
363                 },
364                 .callback = dmi_check_cb
365         },
366         { }
367 };
368
369 static int __init msi_init(void)
370 {
371         int ret;
372
373         if (acpi_disabled)
374                 return -ENODEV;
375
376         if (force || dmi_check_system(msi_dmi_table))
377                 old_ec_model = 1;
378
379         if (auto_brightness < 0 || auto_brightness > 2)
380                 return -EINVAL;
381
382         /* Register backlight stuff */
383
384         if (acpi_video_backlight_support()) {
385                 printk(KERN_INFO "MSI: Brightness ignored, must be controlled "
386                        "by ACPI video driver\n");
387         } else {
388                 msibl_device = backlight_device_register("msi-laptop-bl", NULL,
389                                                          NULL, &msibl_ops);
390                 if (IS_ERR(msibl_device))
391                         return PTR_ERR(msibl_device);
392                 msibl_device->props.max_brightness = MSI_LCD_LEVEL_MAX-1;
393         }
394
395         ret = platform_driver_register(&msipf_driver);
396         if (ret)
397                 goto fail_backlight;
398
399         /* Register platform stuff */
400
401         msipf_device = platform_device_alloc("msi-laptop-pf", -1);
402         if (!msipf_device) {
403                 ret = -ENOMEM;
404                 goto fail_platform_driver;
405         }
406
407         ret = platform_device_add(msipf_device);
408         if (ret)
409                 goto fail_platform_device1;
410
411         ret = sysfs_create_group(&msipf_device->dev.kobj, &msipf_attribute_group);
412         if (ret)
413                 goto fail_platform_device2;
414
415         /* Disable automatic brightness control by default because
416          * this module was probably loaded to do brightness control in
417          * software. */
418
419         if (auto_brightness != 2)
420                 set_auto_brightness(auto_brightness);
421
422         printk(KERN_INFO "msi-laptop: driver "MSI_DRIVER_VERSION" successfully loaded.\n");
423
424         return 0;
425
426 fail_platform_device2:
427
428         platform_device_del(msipf_device);
429
430 fail_platform_device1:
431
432         platform_device_put(msipf_device);
433
434 fail_platform_driver:
435
436         platform_driver_unregister(&msipf_driver);
437
438 fail_backlight:
439
440         backlight_device_unregister(msibl_device);
441
442         return ret;
443 }
444
445 static void __exit msi_cleanup(void)
446 {
447
448         sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
449         platform_device_unregister(msipf_device);
450         platform_driver_unregister(&msipf_driver);
451         backlight_device_unregister(msibl_device);
452
453         /* Enable automatic brightness control again */
454         if (auto_brightness != 2)
455                 set_auto_brightness(1);
456
457         printk(KERN_INFO "msi-laptop: driver unloaded.\n");
458 }
459
460 module_init(msi_init);
461 module_exit(msi_cleanup);
462
463 MODULE_AUTHOR("Lennart Poettering");
464 MODULE_DESCRIPTION("MSI Laptop Support");
465 MODULE_VERSION(MSI_DRIVER_VERSION);
466 MODULE_LICENSE("GPL");
467
468 MODULE_ALIAS("dmi:*:svnMICRO-STARINT'LCO.,LTD:pnMS-1013:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
469 MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1058:pvr0581:rvnMSI:rnMS-1058:*:ct10:*");
470 MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1412:*:rvnMSI:rnMS-1412:*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
471 MODULE_ALIAS("dmi:*:svnNOTEBOOK:pnSAM2000:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
472 MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N034:*");