compal-laptop.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*-*-linux-c-*-*/
  2. /*
  3. Copyright (C) 2008 Cezary Jackiewicz <cezary.jackiewicz (at) gmail.com>
  4. based on MSI driver
  5. Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
  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. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. 02110-1301, USA.
  18. */
  19. /*
  20. * comapl-laptop.c - Compal laptop support.
  21. *
  22. * This driver exports a few files in /sys/devices/platform/compal-laptop/:
  23. *
  24. * lcd_level - Screen brightness: contains a single integer in the
  25. * range 0..7. (rw)
  26. *
  27. * wlan - wlan subsystem state: contains 0 or 1 (rw)
  28. *
  29. * bluetooth - Bluetooth subsystem state: contains 0 or 1 (rw)
  30. *
  31. * raw - raw value taken from embedded controller register (ro)
  32. *
  33. * In addition to these platform device attributes the driver
  34. * registers itself in the Linux backlight control subsystem and is
  35. * available to userspace under /sys/class/backlight/compal-laptop/.
  36. *
  37. * This driver might work on other laptops produced by Compal. If you
  38. * want to try it you can pass force=1 as argument to the module which
  39. * will force it to load even when the DMI data doesn't identify the
  40. * laptop as IFL90.
  41. */
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/init.h>
  45. #include <linux/acpi.h>
  46. #include <linux/dmi.h>
  47. #include <linux/backlight.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/autoconf.h>
  50. #define COMPAL_DRIVER_VERSION "0.2.5"
  51. #define COMPAL_LCD_LEVEL_MAX 8
  52. #define COMPAL_EC_COMMAND_WIRELESS 0xBB
  53. #define COMPAL_EC_COMMAND_LCD_LEVEL 0xB9
  54. #define KILLSWITCH_MASK 0x10
  55. #define WLAN_MASK 0x01
  56. #define BT_MASK 0x02
  57. static int force;
  58. module_param(force, bool, 0);
  59. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  60. /* Hardware access */
  61. static int set_lcd_level(int level)
  62. {
  63. if (level < 0 || level >= COMPAL_LCD_LEVEL_MAX)
  64. return -EINVAL;
  65. ec_write(COMPAL_EC_COMMAND_LCD_LEVEL, level);
  66. return 0;
  67. }
  68. static int get_lcd_level(void)
  69. {
  70. u8 result;
  71. ec_read(COMPAL_EC_COMMAND_LCD_LEVEL, &result);
  72. return (int) result;
  73. }
  74. static int set_wlan_state(int state)
  75. {
  76. u8 result, value;
  77. ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
  78. if ((result & KILLSWITCH_MASK) == 0)
  79. return -EINVAL;
  80. else {
  81. if (state)
  82. value = (u8) (result | WLAN_MASK);
  83. else
  84. value = (u8) (result & ~WLAN_MASK);
  85. ec_write(COMPAL_EC_COMMAND_WIRELESS, value);
  86. }
  87. return 0;
  88. }
  89. static int set_bluetooth_state(int state)
  90. {
  91. u8 result, value;
  92. ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
  93. if ((result & KILLSWITCH_MASK) == 0)
  94. return -EINVAL;
  95. else {
  96. if (state)
  97. value = (u8) (result | BT_MASK);
  98. else
  99. value = (u8) (result & ~BT_MASK);
  100. ec_write(COMPAL_EC_COMMAND_WIRELESS, value);
  101. }
  102. return 0;
  103. }
  104. static int get_wireless_state(int *wlan, int *bluetooth)
  105. {
  106. u8 result;
  107. ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
  108. if (wlan) {
  109. if ((result & KILLSWITCH_MASK) == 0)
  110. *wlan = 0;
  111. else
  112. *wlan = result & WLAN_MASK;
  113. }
  114. if (bluetooth) {
  115. if ((result & KILLSWITCH_MASK) == 0)
  116. *bluetooth = 0;
  117. else
  118. *bluetooth = (result & BT_MASK) >> 1;
  119. }
  120. return 0;
  121. }
  122. /* Backlight device stuff */
  123. static int bl_get_brightness(struct backlight_device *b)
  124. {
  125. return get_lcd_level();
  126. }
  127. static int bl_update_status(struct backlight_device *b)
  128. {
  129. return set_lcd_level(b->props.brightness);
  130. }
  131. static struct backlight_ops compalbl_ops = {
  132. .get_brightness = bl_get_brightness,
  133. .update_status = bl_update_status,
  134. };
  135. static struct backlight_device *compalbl_device;
  136. /* Platform device */
  137. static ssize_t show_wlan(struct device *dev,
  138. struct device_attribute *attr, char *buf)
  139. {
  140. int ret, enabled;
  141. ret = get_wireless_state(&enabled, NULL);
  142. if (ret < 0)
  143. return ret;
  144. return sprintf(buf, "%i\n", enabled);
  145. }
  146. static ssize_t show_raw(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. u8 result;
  150. ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
  151. return sprintf(buf, "%i\n", result);
  152. }
  153. static ssize_t show_bluetooth(struct device *dev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. int ret, enabled;
  157. ret = get_wireless_state(NULL, &enabled);
  158. if (ret < 0)
  159. return ret;
  160. return sprintf(buf, "%i\n", enabled);
  161. }
  162. static ssize_t show_lcd_level(struct device *dev,
  163. struct device_attribute *attr, char *buf)
  164. {
  165. int ret;
  166. ret = get_lcd_level();
  167. if (ret < 0)
  168. return ret;
  169. return sprintf(buf, "%i\n", ret);
  170. }
  171. static ssize_t store_lcd_level(struct device *dev,
  172. struct device_attribute *attr, const char *buf, size_t count)
  173. {
  174. int level, ret;
  175. if (sscanf(buf, "%i", &level) != 1 ||
  176. (level < 0 || level >= COMPAL_LCD_LEVEL_MAX))
  177. return -EINVAL;
  178. ret = set_lcd_level(level);
  179. if (ret < 0)
  180. return ret;
  181. return count;
  182. }
  183. static ssize_t store_wlan_state(struct device *dev,
  184. struct device_attribute *attr, const char *buf, size_t count)
  185. {
  186. int state, ret;
  187. if (sscanf(buf, "%i", &state) != 1 || (state < 0 || state > 1))
  188. return -EINVAL;
  189. ret = set_wlan_state(state);
  190. if (ret < 0)
  191. return ret;
  192. return count;
  193. }
  194. static ssize_t store_bluetooth_state(struct device *dev,
  195. struct device_attribute *attr, const char *buf, size_t count)
  196. {
  197. int state, ret;
  198. if (sscanf(buf, "%i", &state) != 1 || (state < 0 || state > 1))
  199. return -EINVAL;
  200. ret = set_bluetooth_state(state);
  201. if (ret < 0)
  202. return ret;
  203. return count;
  204. }
  205. static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
  206. static DEVICE_ATTR(bluetooth, 0644, show_bluetooth, store_bluetooth_state);
  207. static DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan_state);
  208. static DEVICE_ATTR(raw, 0444, show_raw, NULL);
  209. static struct attribute *compal_attributes[] = {
  210. &dev_attr_lcd_level.attr,
  211. &dev_attr_bluetooth.attr,
  212. &dev_attr_wlan.attr,
  213. &dev_attr_raw.attr,
  214. NULL
  215. };
  216. static struct attribute_group compal_attribute_group = {
  217. .attrs = compal_attributes
  218. };
  219. static struct platform_driver compal_driver = {
  220. .driver = {
  221. .name = "compal-laptop",
  222. .owner = THIS_MODULE,
  223. }
  224. };
  225. static struct platform_device *compal_device;
  226. /* Initialization */
  227. static int dmi_check_cb(const struct dmi_system_id *id)
  228. {
  229. printk(KERN_INFO "compal-laptop: Identified laptop model '%s'.\n",
  230. id->ident);
  231. return 0;
  232. }
  233. static struct dmi_system_id __initdata compal_dmi_table[] = {
  234. {
  235. .ident = "FL90/IFL90",
  236. .matches = {
  237. DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
  238. DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
  239. },
  240. .callback = dmi_check_cb
  241. },
  242. {
  243. .ident = "FL90/IFL90",
  244. .matches = {
  245. DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
  246. DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
  247. },
  248. .callback = dmi_check_cb
  249. },
  250. {
  251. .ident = "FL91/IFL91",
  252. .matches = {
  253. DMI_MATCH(DMI_BOARD_NAME, "IFL91"),
  254. DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
  255. },
  256. .callback = dmi_check_cb
  257. },
  258. {
  259. .ident = "FL92/JFL92",
  260. .matches = {
  261. DMI_MATCH(DMI_BOARD_NAME, "JFL92"),
  262. DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
  263. },
  264. .callback = dmi_check_cb
  265. },
  266. {
  267. .ident = "FT00/IFT00",
  268. .matches = {
  269. DMI_MATCH(DMI_BOARD_NAME, "IFT00"),
  270. DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
  271. },
  272. .callback = dmi_check_cb
  273. },
  274. { }
  275. };
  276. static int __init compal_init(void)
  277. {
  278. int ret;
  279. if (acpi_disabled)
  280. return -ENODEV;
  281. if (!force && !dmi_check_system(compal_dmi_table))
  282. return -ENODEV;
  283. /* Register backlight stuff */
  284. compalbl_device = backlight_device_register("compal-laptop", NULL, NULL,
  285. &compalbl_ops);
  286. if (IS_ERR(compalbl_device))
  287. return PTR_ERR(compalbl_device);
  288. compalbl_device->props.max_brightness = COMPAL_LCD_LEVEL_MAX-1;
  289. ret = platform_driver_register(&compal_driver);
  290. if (ret)
  291. goto fail_backlight;
  292. /* Register platform stuff */
  293. compal_device = platform_device_alloc("compal-laptop", -1);
  294. if (!compal_device) {
  295. ret = -ENOMEM;
  296. goto fail_platform_driver;
  297. }
  298. ret = platform_device_add(compal_device);
  299. if (ret)
  300. goto fail_platform_device1;
  301. ret = sysfs_create_group(&compal_device->dev.kobj,
  302. &compal_attribute_group);
  303. if (ret)
  304. goto fail_platform_device2;
  305. printk(KERN_INFO "compal-laptop: driver "COMPAL_DRIVER_VERSION
  306. " successfully loaded.\n");
  307. return 0;
  308. fail_platform_device2:
  309. platform_device_del(compal_device);
  310. fail_platform_device1:
  311. platform_device_put(compal_device);
  312. fail_platform_driver:
  313. platform_driver_unregister(&compal_driver);
  314. fail_backlight:
  315. backlight_device_unregister(compalbl_device);
  316. return ret;
  317. }
  318. static void __exit compal_cleanup(void)
  319. {
  320. sysfs_remove_group(&compal_device->dev.kobj, &compal_attribute_group);
  321. platform_device_unregister(compal_device);
  322. platform_driver_unregister(&compal_driver);
  323. backlight_device_unregister(compalbl_device);
  324. printk(KERN_INFO "compal-laptop: driver unloaded.\n");
  325. }
  326. module_init(compal_init);
  327. module_exit(compal_cleanup);
  328. MODULE_AUTHOR("Cezary Jackiewicz");
  329. MODULE_DESCRIPTION("Compal Laptop Support");
  330. MODULE_VERSION(COMPAL_DRIVER_VERSION);
  331. MODULE_LICENSE("GPL");
  332. MODULE_ALIAS("dmi:*:rnIFL90:rvrIFT00:*");
  333. MODULE_ALIAS("dmi:*:rnIFL90:rvrREFERENCE:*");
  334. MODULE_ALIAS("dmi:*:rnIFL91:rvrIFT00:*");
  335. MODULE_ALIAS("dmi:*:rnJFL92:rvrIFT00:*");
  336. MODULE_ALIAS("dmi:*:rnIFT00:rvrIFT00:*");