fujitsu-laptop.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*-*-linux-c-*-*/
  2. /*
  3. Copyright (C) 2007,2008 Jonathan Woithe <jwoithe@just42.net>
  4. Copyright (C) 2008 Peter Gruber <nokos@gmx.net>
  5. Copyright (C) 2008 Tony Vroon <tony@linx.net>
  6. Based on earlier work:
  7. Copyright (C) 2003 Shane Spencer <shane@bogomip.com>
  8. Adrian Yee <brewt-fujitsu@brewt.org>
  9. Templated from msi-laptop.c and thinkpad_acpi.c which is copyright
  10. by its respective authors.
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful, but
  16. WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. 02110-1301, USA.
  23. */
  24. /*
  25. * fujitsu-laptop.c - Fujitsu laptop support, providing access to additional
  26. * features made available on a range of Fujitsu laptops including the
  27. * P2xxx/P5xxx/S6xxx/S7xxx series.
  28. *
  29. * This driver implements a vendor-specific backlight control interface for
  30. * Fujitsu laptops and provides support for hotkeys present on certain Fujitsu
  31. * laptops.
  32. *
  33. * This driver has been tested on a Fujitsu Lifebook S6410, S7020 and
  34. * P8010. It should work on most P-series and S-series Lifebooks, but
  35. * YMMV.
  36. *
  37. * The module parameter use_alt_lcd_levels switches between different ACPI
  38. * brightness controls which are used by different Fujitsu laptops. In most
  39. * cases the correct method is automatically detected. "use_alt_lcd_levels=1"
  40. * is applicable for a Fujitsu Lifebook S6410 if autodetection fails.
  41. *
  42. */
  43. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  44. #include <linux/module.h>
  45. #include <linux/kernel.h>
  46. #include <linux/init.h>
  47. #include <linux/acpi.h>
  48. #include <linux/bitops.h>
  49. #include <linux/dmi.h>
  50. #include <linux/backlight.h>
  51. #include <linux/fb.h>
  52. #include <linux/input.h>
  53. #include <linux/input/sparse-keymap.h>
  54. #include <linux/kfifo.h>
  55. #include <linux/leds.h>
  56. #include <linux/platform_device.h>
  57. #include <acpi/video.h>
  58. #define FUJITSU_DRIVER_VERSION "0.6.0"
  59. #define FUJITSU_LCD_N_LEVELS 8
  60. #define ACPI_FUJITSU_CLASS "fujitsu"
  61. #define ACPI_FUJITSU_BL_HID "FUJ02B1"
  62. #define ACPI_FUJITSU_BL_DRIVER_NAME "Fujitsu laptop FUJ02B1 ACPI brightness driver"
  63. #define ACPI_FUJITSU_BL_DEVICE_NAME "Fujitsu FUJ02B1"
  64. #define ACPI_FUJITSU_LAPTOP_HID "FUJ02E3"
  65. #define ACPI_FUJITSU_LAPTOP_DRIVER_NAME "Fujitsu laptop FUJ02E3 ACPI hotkeys driver"
  66. #define ACPI_FUJITSU_LAPTOP_DEVICE_NAME "Fujitsu FUJ02E3"
  67. #define ACPI_FUJITSU_NOTIFY_CODE 0x80
  68. /* FUNC interface - command values */
  69. #define FUNC_FLAGS BIT(12)
  70. #define FUNC_LEDS (BIT(12) | BIT(0))
  71. #define FUNC_BUTTONS (BIT(12) | BIT(1))
  72. #define FUNC_BACKLIGHT (BIT(12) | BIT(2))
  73. /* FUNC interface - responses */
  74. #define UNSUPPORTED_CMD 0x80000000
  75. /* FUNC interface - status flags */
  76. #define FLAG_RFKILL BIT(5)
  77. #define FLAG_LID BIT(8)
  78. #define FLAG_DOCK BIT(9)
  79. /* FUNC interface - LED control */
  80. #define FUNC_LED_OFF BIT(0)
  81. #define FUNC_LED_ON (BIT(0) | BIT(16) | BIT(17))
  82. #define LOGOLAMP_POWERON BIT(13)
  83. #define LOGOLAMP_ALWAYS BIT(14)
  84. #define KEYBOARD_LAMPS BIT(8)
  85. #define RADIO_LED_ON BIT(5)
  86. #define ECO_LED BIT(16)
  87. #define ECO_LED_ON BIT(19)
  88. /* FUNC interface - backlight power control */
  89. #define BACKLIGHT_PARAM_POWER BIT(2)
  90. #define BACKLIGHT_OFF (BIT(0) | BIT(1))
  91. #define BACKLIGHT_ON 0
  92. /* Scancodes read from the GIRB register */
  93. #define KEY1_CODE 0x410
  94. #define KEY2_CODE 0x411
  95. #define KEY3_CODE 0x412
  96. #define KEY4_CODE 0x413
  97. #define KEY5_CODE 0x420
  98. /* Hotkey ringbuffer limits */
  99. #define MAX_HOTKEY_RINGBUFFER_SIZE 100
  100. #define RINGBUFFERSIZE 40
  101. /* Module parameters */
  102. static int use_alt_lcd_levels = -1;
  103. static bool disable_brightness_adjust;
  104. /* Device controlling the backlight and associated keys */
  105. struct fujitsu_bl {
  106. struct input_dev *input;
  107. char phys[32];
  108. struct backlight_device *bl_device;
  109. unsigned int max_brightness;
  110. unsigned int brightness_level;
  111. };
  112. static struct fujitsu_bl *fujitsu_bl;
  113. /* Device used to access hotkeys and other features on the laptop */
  114. struct fujitsu_laptop {
  115. struct input_dev *input;
  116. char phys[32];
  117. struct platform_device *pf_device;
  118. struct kfifo fifo;
  119. spinlock_t fifo_lock;
  120. int flags_supported;
  121. int flags_state;
  122. };
  123. static struct acpi_device *fext;
  124. /* Fujitsu ACPI interface function */
  125. static int call_fext_func(struct acpi_device *device,
  126. int func, int op, int feature, int state)
  127. {
  128. union acpi_object params[4] = {
  129. { .integer.type = ACPI_TYPE_INTEGER, .integer.value = func },
  130. { .integer.type = ACPI_TYPE_INTEGER, .integer.value = op },
  131. { .integer.type = ACPI_TYPE_INTEGER, .integer.value = feature },
  132. { .integer.type = ACPI_TYPE_INTEGER, .integer.value = state }
  133. };
  134. struct acpi_object_list arg_list = { 4, params };
  135. unsigned long long value;
  136. acpi_status status;
  137. status = acpi_evaluate_integer(device->handle, "FUNC", &arg_list,
  138. &value);
  139. if (ACPI_FAILURE(status)) {
  140. acpi_handle_err(device->handle, "Failed to evaluate FUNC\n");
  141. return -ENODEV;
  142. }
  143. acpi_handle_debug(device->handle,
  144. "FUNC 0x%x (args 0x%x, 0x%x, 0x%x) returned 0x%x\n",
  145. func, op, feature, state, (int)value);
  146. return value;
  147. }
  148. /* Hardware access for LCD brightness control */
  149. static int set_lcd_level(struct acpi_device *device, int level)
  150. {
  151. struct fujitsu_bl *priv = acpi_driver_data(device);
  152. acpi_status status;
  153. char *method;
  154. switch (use_alt_lcd_levels) {
  155. case -1:
  156. if (acpi_has_method(device->handle, "SBL2"))
  157. method = "SBL2";
  158. else
  159. method = "SBLL";
  160. break;
  161. case 1:
  162. method = "SBL2";
  163. break;
  164. default:
  165. method = "SBLL";
  166. break;
  167. }
  168. acpi_handle_debug(device->handle, "set lcd level via %s [%d]\n", method,
  169. level);
  170. if (level < 0 || level >= priv->max_brightness)
  171. return -EINVAL;
  172. status = acpi_execute_simple_method(device->handle, method, level);
  173. if (ACPI_FAILURE(status)) {
  174. acpi_handle_err(device->handle, "Failed to evaluate %s\n",
  175. method);
  176. return -ENODEV;
  177. }
  178. priv->brightness_level = level;
  179. return 0;
  180. }
  181. static int get_lcd_level(struct acpi_device *device)
  182. {
  183. struct fujitsu_bl *priv = acpi_driver_data(device);
  184. unsigned long long state = 0;
  185. acpi_status status = AE_OK;
  186. acpi_handle_debug(device->handle, "get lcd level via GBLL\n");
  187. status = acpi_evaluate_integer(device->handle, "GBLL", NULL, &state);
  188. if (ACPI_FAILURE(status))
  189. return 0;
  190. priv->brightness_level = state & 0x0fffffff;
  191. return priv->brightness_level;
  192. }
  193. static int get_max_brightness(struct acpi_device *device)
  194. {
  195. struct fujitsu_bl *priv = acpi_driver_data(device);
  196. unsigned long long state = 0;
  197. acpi_status status = AE_OK;
  198. acpi_handle_debug(device->handle, "get max lcd level via RBLL\n");
  199. status = acpi_evaluate_integer(device->handle, "RBLL", NULL, &state);
  200. if (ACPI_FAILURE(status))
  201. return -1;
  202. priv->max_brightness = state;
  203. return priv->max_brightness;
  204. }
  205. /* Backlight device stuff */
  206. static int bl_get_brightness(struct backlight_device *b)
  207. {
  208. struct acpi_device *device = bl_get_data(b);
  209. return b->props.power == FB_BLANK_POWERDOWN ? 0 : get_lcd_level(device);
  210. }
  211. static int bl_update_status(struct backlight_device *b)
  212. {
  213. struct acpi_device *device = bl_get_data(b);
  214. if (fext) {
  215. if (b->props.power == FB_BLANK_POWERDOWN)
  216. call_fext_func(fext, FUNC_BACKLIGHT, 0x1,
  217. BACKLIGHT_PARAM_POWER, BACKLIGHT_OFF);
  218. else
  219. call_fext_func(fext, FUNC_BACKLIGHT, 0x1,
  220. BACKLIGHT_PARAM_POWER, BACKLIGHT_ON);
  221. }
  222. return set_lcd_level(device, b->props.brightness);
  223. }
  224. static const struct backlight_ops fujitsu_bl_ops = {
  225. .get_brightness = bl_get_brightness,
  226. .update_status = bl_update_status,
  227. };
  228. static ssize_t lid_show(struct device *dev, struct device_attribute *attr,
  229. char *buf)
  230. {
  231. struct fujitsu_laptop *priv = dev_get_drvdata(dev);
  232. if (!(priv->flags_supported & FLAG_LID))
  233. return sprintf(buf, "unknown\n");
  234. if (priv->flags_state & FLAG_LID)
  235. return sprintf(buf, "open\n");
  236. else
  237. return sprintf(buf, "closed\n");
  238. }
  239. static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
  240. char *buf)
  241. {
  242. struct fujitsu_laptop *priv = dev_get_drvdata(dev);
  243. if (!(priv->flags_supported & FLAG_DOCK))
  244. return sprintf(buf, "unknown\n");
  245. if (priv->flags_state & FLAG_DOCK)
  246. return sprintf(buf, "docked\n");
  247. else
  248. return sprintf(buf, "undocked\n");
  249. }
  250. static ssize_t radios_show(struct device *dev, struct device_attribute *attr,
  251. char *buf)
  252. {
  253. struct fujitsu_laptop *priv = dev_get_drvdata(dev);
  254. if (!(priv->flags_supported & FLAG_RFKILL))
  255. return sprintf(buf, "unknown\n");
  256. if (priv->flags_state & FLAG_RFKILL)
  257. return sprintf(buf, "on\n");
  258. else
  259. return sprintf(buf, "killed\n");
  260. }
  261. static DEVICE_ATTR_RO(lid);
  262. static DEVICE_ATTR_RO(dock);
  263. static DEVICE_ATTR_RO(radios);
  264. static struct attribute *fujitsu_pf_attributes[] = {
  265. &dev_attr_lid.attr,
  266. &dev_attr_dock.attr,
  267. &dev_attr_radios.attr,
  268. NULL
  269. };
  270. static const struct attribute_group fujitsu_pf_attribute_group = {
  271. .attrs = fujitsu_pf_attributes
  272. };
  273. static struct platform_driver fujitsu_pf_driver = {
  274. .driver = {
  275. .name = "fujitsu-laptop",
  276. }
  277. };
  278. /* ACPI device for LCD brightness control */
  279. static const struct key_entry keymap_backlight[] = {
  280. { KE_KEY, true, { KEY_BRIGHTNESSUP } },
  281. { KE_KEY, false, { KEY_BRIGHTNESSDOWN } },
  282. { KE_END, 0 }
  283. };
  284. static int acpi_fujitsu_bl_input_setup(struct acpi_device *device)
  285. {
  286. struct fujitsu_bl *priv = acpi_driver_data(device);
  287. int ret;
  288. priv->input = devm_input_allocate_device(&device->dev);
  289. if (!priv->input)
  290. return -ENOMEM;
  291. snprintf(priv->phys, sizeof(priv->phys), "%s/video/input0",
  292. acpi_device_hid(device));
  293. priv->input->name = acpi_device_name(device);
  294. priv->input->phys = priv->phys;
  295. priv->input->id.bustype = BUS_HOST;
  296. priv->input->id.product = 0x06;
  297. ret = sparse_keymap_setup(priv->input, keymap_backlight, NULL);
  298. if (ret)
  299. return ret;
  300. return input_register_device(priv->input);
  301. }
  302. static int fujitsu_backlight_register(struct acpi_device *device)
  303. {
  304. struct fujitsu_bl *priv = acpi_driver_data(device);
  305. const struct backlight_properties props = {
  306. .brightness = priv->brightness_level,
  307. .max_brightness = priv->max_brightness - 1,
  308. .type = BACKLIGHT_PLATFORM
  309. };
  310. struct backlight_device *bd;
  311. bd = devm_backlight_device_register(&device->dev, "fujitsu-laptop",
  312. &device->dev, device,
  313. &fujitsu_bl_ops, &props);
  314. if (IS_ERR(bd))
  315. return PTR_ERR(bd);
  316. priv->bl_device = bd;
  317. return 0;
  318. }
  319. static int acpi_fujitsu_bl_add(struct acpi_device *device)
  320. {
  321. struct fujitsu_bl *priv;
  322. int ret;
  323. if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
  324. return -ENODEV;
  325. priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
  326. if (!priv)
  327. return -ENOMEM;
  328. fujitsu_bl = priv;
  329. strcpy(acpi_device_name(device), ACPI_FUJITSU_BL_DEVICE_NAME);
  330. strcpy(acpi_device_class(device), ACPI_FUJITSU_CLASS);
  331. device->driver_data = priv;
  332. pr_info("ACPI: %s [%s]\n",
  333. acpi_device_name(device), acpi_device_bid(device));
  334. if (get_max_brightness(device) <= 0)
  335. priv->max_brightness = FUJITSU_LCD_N_LEVELS;
  336. get_lcd_level(device);
  337. ret = acpi_fujitsu_bl_input_setup(device);
  338. if (ret)
  339. return ret;
  340. return fujitsu_backlight_register(device);
  341. }
  342. /* Brightness notify */
  343. static void acpi_fujitsu_bl_notify(struct acpi_device *device, u32 event)
  344. {
  345. struct fujitsu_bl *priv = acpi_driver_data(device);
  346. int oldb, newb;
  347. if (event != ACPI_FUJITSU_NOTIFY_CODE) {
  348. acpi_handle_info(device->handle, "unsupported event [0x%x]\n",
  349. event);
  350. sparse_keymap_report_event(priv->input, -1, 1, true);
  351. return;
  352. }
  353. oldb = priv->brightness_level;
  354. get_lcd_level(device);
  355. newb = priv->brightness_level;
  356. acpi_handle_debug(device->handle,
  357. "brightness button event [%i -> %i]\n", oldb, newb);
  358. if (oldb == newb)
  359. return;
  360. if (!disable_brightness_adjust)
  361. set_lcd_level(device, newb);
  362. sparse_keymap_report_event(priv->input, oldb < newb, 1, true);
  363. }
  364. /* ACPI device for hotkey handling */
  365. static const struct key_entry keymap_default[] = {
  366. { KE_KEY, KEY1_CODE, { KEY_PROG1 } },
  367. { KE_KEY, KEY2_CODE, { KEY_PROG2 } },
  368. { KE_KEY, KEY3_CODE, { KEY_PROG3 } },
  369. { KE_KEY, KEY4_CODE, { KEY_PROG4 } },
  370. { KE_KEY, KEY5_CODE, { KEY_RFKILL } },
  371. { KE_KEY, BIT(5), { KEY_RFKILL } },
  372. { KE_KEY, BIT(26), { KEY_TOUCHPAD_TOGGLE } },
  373. { KE_KEY, BIT(29), { KEY_MICMUTE } },
  374. { KE_END, 0 }
  375. };
  376. static const struct key_entry keymap_s64x0[] = {
  377. { KE_KEY, KEY1_CODE, { KEY_SCREENLOCK } }, /* "Lock" */
  378. { KE_KEY, KEY2_CODE, { KEY_HELP } }, /* "Mobility Center */
  379. { KE_KEY, KEY3_CODE, { KEY_PROG3 } },
  380. { KE_KEY, KEY4_CODE, { KEY_PROG4 } },
  381. { KE_END, 0 }
  382. };
  383. static const struct key_entry keymap_p8010[] = {
  384. { KE_KEY, KEY1_CODE, { KEY_HELP } }, /* "Support" */
  385. { KE_KEY, KEY2_CODE, { KEY_PROG2 } },
  386. { KE_KEY, KEY3_CODE, { KEY_SWITCHVIDEOMODE } }, /* "Presentation" */
  387. { KE_KEY, KEY4_CODE, { KEY_WWW } }, /* "WWW" */
  388. { KE_END, 0 }
  389. };
  390. static const struct key_entry *keymap = keymap_default;
  391. static int fujitsu_laptop_dmi_keymap_override(const struct dmi_system_id *id)
  392. {
  393. pr_info("Identified laptop model '%s'\n", id->ident);
  394. keymap = id->driver_data;
  395. return 1;
  396. }
  397. static const struct dmi_system_id fujitsu_laptop_dmi_table[] = {
  398. {
  399. .callback = fujitsu_laptop_dmi_keymap_override,
  400. .ident = "Fujitsu Siemens S6410",
  401. .matches = {
  402. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
  403. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6410"),
  404. },
  405. .driver_data = (void *)keymap_s64x0
  406. },
  407. {
  408. .callback = fujitsu_laptop_dmi_keymap_override,
  409. .ident = "Fujitsu Siemens S6420",
  410. .matches = {
  411. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
  412. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6420"),
  413. },
  414. .driver_data = (void *)keymap_s64x0
  415. },
  416. {
  417. .callback = fujitsu_laptop_dmi_keymap_override,
  418. .ident = "Fujitsu LifeBook P8010",
  419. .matches = {
  420. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  421. DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook P8010"),
  422. },
  423. .driver_data = (void *)keymap_p8010
  424. },
  425. {}
  426. };
  427. static int acpi_fujitsu_laptop_input_setup(struct acpi_device *device)
  428. {
  429. struct fujitsu_laptop *priv = acpi_driver_data(device);
  430. int ret;
  431. priv->input = devm_input_allocate_device(&device->dev);
  432. if (!priv->input)
  433. return -ENOMEM;
  434. snprintf(priv->phys, sizeof(priv->phys), "%s/input0",
  435. acpi_device_hid(device));
  436. priv->input->name = acpi_device_name(device);
  437. priv->input->phys = priv->phys;
  438. priv->input->id.bustype = BUS_HOST;
  439. dmi_check_system(fujitsu_laptop_dmi_table);
  440. ret = sparse_keymap_setup(priv->input, keymap, NULL);
  441. if (ret)
  442. return ret;
  443. return input_register_device(priv->input);
  444. }
  445. static int fujitsu_laptop_platform_add(struct acpi_device *device)
  446. {
  447. struct fujitsu_laptop *priv = acpi_driver_data(device);
  448. int ret;
  449. priv->pf_device = platform_device_alloc("fujitsu-laptop", -1);
  450. if (!priv->pf_device)
  451. return -ENOMEM;
  452. platform_set_drvdata(priv->pf_device, priv);
  453. ret = platform_device_add(priv->pf_device);
  454. if (ret)
  455. goto err_put_platform_device;
  456. ret = sysfs_create_group(&priv->pf_device->dev.kobj,
  457. &fujitsu_pf_attribute_group);
  458. if (ret)
  459. goto err_del_platform_device;
  460. return 0;
  461. err_del_platform_device:
  462. platform_device_del(priv->pf_device);
  463. err_put_platform_device:
  464. platform_device_put(priv->pf_device);
  465. return ret;
  466. }
  467. static void fujitsu_laptop_platform_remove(struct acpi_device *device)
  468. {
  469. struct fujitsu_laptop *priv = acpi_driver_data(device);
  470. sysfs_remove_group(&priv->pf_device->dev.kobj,
  471. &fujitsu_pf_attribute_group);
  472. platform_device_unregister(priv->pf_device);
  473. }
  474. static int logolamp_set(struct led_classdev *cdev,
  475. enum led_brightness brightness)
  476. {
  477. struct acpi_device *device = to_acpi_device(cdev->dev->parent);
  478. int poweron = FUNC_LED_ON, always = FUNC_LED_ON;
  479. int ret;
  480. if (brightness < LED_HALF)
  481. poweron = FUNC_LED_OFF;
  482. if (brightness < LED_FULL)
  483. always = FUNC_LED_OFF;
  484. ret = call_fext_func(device, FUNC_LEDS, 0x1, LOGOLAMP_POWERON, poweron);
  485. if (ret < 0)
  486. return ret;
  487. return call_fext_func(device, FUNC_LEDS, 0x1, LOGOLAMP_ALWAYS, always);
  488. }
  489. static enum led_brightness logolamp_get(struct led_classdev *cdev)
  490. {
  491. struct acpi_device *device = to_acpi_device(cdev->dev->parent);
  492. int ret;
  493. ret = call_fext_func(device, FUNC_LEDS, 0x2, LOGOLAMP_ALWAYS, 0x0);
  494. if (ret == FUNC_LED_ON)
  495. return LED_FULL;
  496. ret = call_fext_func(device, FUNC_LEDS, 0x2, LOGOLAMP_POWERON, 0x0);
  497. if (ret == FUNC_LED_ON)
  498. return LED_HALF;
  499. return LED_OFF;
  500. }
  501. static int kblamps_set(struct led_classdev *cdev,
  502. enum led_brightness brightness)
  503. {
  504. struct acpi_device *device = to_acpi_device(cdev->dev->parent);
  505. if (brightness >= LED_FULL)
  506. return call_fext_func(device, FUNC_LEDS, 0x1, KEYBOARD_LAMPS,
  507. FUNC_LED_ON);
  508. else
  509. return call_fext_func(device, FUNC_LEDS, 0x1, KEYBOARD_LAMPS,
  510. FUNC_LED_OFF);
  511. }
  512. static enum led_brightness kblamps_get(struct led_classdev *cdev)
  513. {
  514. struct acpi_device *device = to_acpi_device(cdev->dev->parent);
  515. enum led_brightness brightness = LED_OFF;
  516. if (call_fext_func(device,
  517. FUNC_LEDS, 0x2, KEYBOARD_LAMPS, 0x0) == FUNC_LED_ON)
  518. brightness = LED_FULL;
  519. return brightness;
  520. }
  521. static int radio_led_set(struct led_classdev *cdev,
  522. enum led_brightness brightness)
  523. {
  524. struct acpi_device *device = to_acpi_device(cdev->dev->parent);
  525. if (brightness >= LED_FULL)
  526. return call_fext_func(device, FUNC_FLAGS, 0x5, RADIO_LED_ON,
  527. RADIO_LED_ON);
  528. else
  529. return call_fext_func(device, FUNC_FLAGS, 0x5, RADIO_LED_ON,
  530. 0x0);
  531. }
  532. static enum led_brightness radio_led_get(struct led_classdev *cdev)
  533. {
  534. struct acpi_device *device = to_acpi_device(cdev->dev->parent);
  535. enum led_brightness brightness = LED_OFF;
  536. if (call_fext_func(device, FUNC_FLAGS, 0x4, 0x0, 0x0) & RADIO_LED_ON)
  537. brightness = LED_FULL;
  538. return brightness;
  539. }
  540. static int eco_led_set(struct led_classdev *cdev,
  541. enum led_brightness brightness)
  542. {
  543. struct acpi_device *device = to_acpi_device(cdev->dev->parent);
  544. int curr;
  545. curr = call_fext_func(device, FUNC_LEDS, 0x2, ECO_LED, 0x0);
  546. if (brightness >= LED_FULL)
  547. return call_fext_func(device, FUNC_LEDS, 0x1, ECO_LED,
  548. curr | ECO_LED_ON);
  549. else
  550. return call_fext_func(device, FUNC_LEDS, 0x1, ECO_LED,
  551. curr & ~ECO_LED_ON);
  552. }
  553. static enum led_brightness eco_led_get(struct led_classdev *cdev)
  554. {
  555. struct acpi_device *device = to_acpi_device(cdev->dev->parent);
  556. enum led_brightness brightness = LED_OFF;
  557. if (call_fext_func(device, FUNC_LEDS, 0x2, ECO_LED, 0x0) & ECO_LED_ON)
  558. brightness = LED_FULL;
  559. return brightness;
  560. }
  561. static int acpi_fujitsu_laptop_leds_register(struct acpi_device *device)
  562. {
  563. struct fujitsu_laptop *priv = acpi_driver_data(device);
  564. struct led_classdev *led;
  565. int ret;
  566. if (call_fext_func(device,
  567. FUNC_LEDS, 0x0, 0x0, 0x0) & LOGOLAMP_POWERON) {
  568. led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL);
  569. if (!led)
  570. return -ENOMEM;
  571. led->name = "fujitsu::logolamp";
  572. led->brightness_set_blocking = logolamp_set;
  573. led->brightness_get = logolamp_get;
  574. ret = devm_led_classdev_register(&device->dev, led);
  575. if (ret)
  576. return ret;
  577. }
  578. if ((call_fext_func(device,
  579. FUNC_LEDS, 0x0, 0x0, 0x0) & KEYBOARD_LAMPS) &&
  580. (call_fext_func(device, FUNC_BUTTONS, 0x0, 0x0, 0x0) == 0x0)) {
  581. led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL);
  582. if (!led)
  583. return -ENOMEM;
  584. led->name = "fujitsu::kblamps";
  585. led->brightness_set_blocking = kblamps_set;
  586. led->brightness_get = kblamps_get;
  587. ret = devm_led_classdev_register(&device->dev, led);
  588. if (ret)
  589. return ret;
  590. }
  591. /*
  592. * Some Fujitsu laptops have a radio toggle button in place of a slide
  593. * switch and all such machines appear to also have an RF LED. Based on
  594. * comparing DSDT tables of four Fujitsu Lifebook models (E744, E751,
  595. * S7110, S8420; the first one has a radio toggle button, the other
  596. * three have slide switches), bit 17 of flags_supported (the value
  597. * returned by method S000 of ACPI device FUJ02E3) seems to indicate
  598. * whether given model has a radio toggle button.
  599. */
  600. if (priv->flags_supported & BIT(17)) {
  601. led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL);
  602. if (!led)
  603. return -ENOMEM;
  604. led->name = "fujitsu::radio_led";
  605. led->brightness_set_blocking = radio_led_set;
  606. led->brightness_get = radio_led_get;
  607. led->default_trigger = "rfkill-any";
  608. ret = devm_led_classdev_register(&device->dev, led);
  609. if (ret)
  610. return ret;
  611. }
  612. /* Support for eco led is not always signaled in bit corresponding
  613. * to the bit used to control the led. According to the DSDT table,
  614. * bit 14 seems to indicate presence of said led as well.
  615. * Confirm by testing the status.
  616. */
  617. if ((call_fext_func(device, FUNC_LEDS, 0x0, 0x0, 0x0) & BIT(14)) &&
  618. (call_fext_func(device,
  619. FUNC_LEDS, 0x2, ECO_LED, 0x0) != UNSUPPORTED_CMD)) {
  620. led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL);
  621. if (!led)
  622. return -ENOMEM;
  623. led->name = "fujitsu::eco_led";
  624. led->brightness_set_blocking = eco_led_set;
  625. led->brightness_get = eco_led_get;
  626. ret = devm_led_classdev_register(&device->dev, led);
  627. if (ret)
  628. return ret;
  629. }
  630. return 0;
  631. }
  632. static int acpi_fujitsu_laptop_add(struct acpi_device *device)
  633. {
  634. struct fujitsu_laptop *priv;
  635. int ret, i = 0;
  636. priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
  637. if (!priv)
  638. return -ENOMEM;
  639. WARN_ONCE(fext, "More than one FUJ02E3 ACPI device was found. Driver may not work as intended.");
  640. fext = device;
  641. strcpy(acpi_device_name(device), ACPI_FUJITSU_LAPTOP_DEVICE_NAME);
  642. strcpy(acpi_device_class(device), ACPI_FUJITSU_CLASS);
  643. device->driver_data = priv;
  644. /* kfifo */
  645. spin_lock_init(&priv->fifo_lock);
  646. ret = kfifo_alloc(&priv->fifo, RINGBUFFERSIZE * sizeof(int),
  647. GFP_KERNEL);
  648. if (ret)
  649. return ret;
  650. pr_info("ACPI: %s [%s]\n",
  651. acpi_device_name(device), acpi_device_bid(device));
  652. while (call_fext_func(device, FUNC_BUTTONS, 0x1, 0x0, 0x0) != 0 &&
  653. i++ < MAX_HOTKEY_RINGBUFFER_SIZE)
  654. ; /* No action, result is discarded */
  655. acpi_handle_debug(device->handle, "Discarded %i ringbuffer entries\n",
  656. i);
  657. priv->flags_supported = call_fext_func(device, FUNC_FLAGS, 0x0, 0x0,
  658. 0x0);
  659. /* Make sure our bitmask of supported functions is cleared if the
  660. RFKILL function block is not implemented, like on the S7020. */
  661. if (priv->flags_supported == UNSUPPORTED_CMD)
  662. priv->flags_supported = 0;
  663. if (priv->flags_supported)
  664. priv->flags_state = call_fext_func(device, FUNC_FLAGS, 0x4, 0x0,
  665. 0x0);
  666. /* Suspect this is a keymap of the application panel, print it */
  667. acpi_handle_info(device->handle, "BTNI: [0x%x]\n",
  668. call_fext_func(device, FUNC_BUTTONS, 0x0, 0x0, 0x0));
  669. /* Sync backlight power status */
  670. if (fujitsu_bl && fujitsu_bl->bl_device &&
  671. acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  672. if (call_fext_func(fext, FUNC_BACKLIGHT, 0x2,
  673. BACKLIGHT_PARAM_POWER, 0x0) == BACKLIGHT_OFF)
  674. fujitsu_bl->bl_device->props.power = FB_BLANK_POWERDOWN;
  675. else
  676. fujitsu_bl->bl_device->props.power = FB_BLANK_UNBLANK;
  677. }
  678. ret = acpi_fujitsu_laptop_input_setup(device);
  679. if (ret)
  680. goto err_free_fifo;
  681. ret = acpi_fujitsu_laptop_leds_register(device);
  682. if (ret)
  683. goto err_free_fifo;
  684. ret = fujitsu_laptop_platform_add(device);
  685. if (ret)
  686. goto err_free_fifo;
  687. return 0;
  688. err_free_fifo:
  689. kfifo_free(&priv->fifo);
  690. return ret;
  691. }
  692. static int acpi_fujitsu_laptop_remove(struct acpi_device *device)
  693. {
  694. struct fujitsu_laptop *priv = acpi_driver_data(device);
  695. fujitsu_laptop_platform_remove(device);
  696. kfifo_free(&priv->fifo);
  697. return 0;
  698. }
  699. static void acpi_fujitsu_laptop_press(struct acpi_device *device, int scancode)
  700. {
  701. struct fujitsu_laptop *priv = acpi_driver_data(device);
  702. int ret;
  703. ret = kfifo_in_locked(&priv->fifo, (unsigned char *)&scancode,
  704. sizeof(scancode), &priv->fifo_lock);
  705. if (ret != sizeof(scancode)) {
  706. dev_info(&priv->input->dev, "Could not push scancode [0x%x]\n",
  707. scancode);
  708. return;
  709. }
  710. sparse_keymap_report_event(priv->input, scancode, 1, false);
  711. dev_dbg(&priv->input->dev, "Push scancode into ringbuffer [0x%x]\n",
  712. scancode);
  713. }
  714. static void acpi_fujitsu_laptop_release(struct acpi_device *device)
  715. {
  716. struct fujitsu_laptop *priv = acpi_driver_data(device);
  717. int scancode, ret;
  718. while (true) {
  719. ret = kfifo_out_locked(&priv->fifo, (unsigned char *)&scancode,
  720. sizeof(scancode), &priv->fifo_lock);
  721. if (ret != sizeof(scancode))
  722. return;
  723. sparse_keymap_report_event(priv->input, scancode, 0, false);
  724. dev_dbg(&priv->input->dev,
  725. "Pop scancode from ringbuffer [0x%x]\n", scancode);
  726. }
  727. }
  728. static void acpi_fujitsu_laptop_notify(struct acpi_device *device, u32 event)
  729. {
  730. struct fujitsu_laptop *priv = acpi_driver_data(device);
  731. int scancode, i = 0, ret;
  732. unsigned int irb;
  733. if (event != ACPI_FUJITSU_NOTIFY_CODE) {
  734. acpi_handle_info(device->handle, "Unsupported event [0x%x]\n",
  735. event);
  736. sparse_keymap_report_event(priv->input, -1, 1, true);
  737. return;
  738. }
  739. if (priv->flags_supported)
  740. priv->flags_state = call_fext_func(device, FUNC_FLAGS, 0x4, 0x0,
  741. 0x0);
  742. while ((irb = call_fext_func(device,
  743. FUNC_BUTTONS, 0x1, 0x0, 0x0)) != 0 &&
  744. i++ < MAX_HOTKEY_RINGBUFFER_SIZE) {
  745. scancode = irb & 0x4ff;
  746. if (sparse_keymap_entry_from_scancode(priv->input, scancode))
  747. acpi_fujitsu_laptop_press(device, scancode);
  748. else if (scancode == 0)
  749. acpi_fujitsu_laptop_release(device);
  750. else
  751. acpi_handle_info(device->handle,
  752. "Unknown GIRB result [%x]\n", irb);
  753. }
  754. /* On some models (first seen on the Skylake-based Lifebook
  755. * E736/E746/E756), the touchpad toggle hotkey (Fn+F4) is
  756. * handled in software; its state is queried using FUNC_FLAGS
  757. */
  758. if (priv->flags_supported & (BIT(5) | BIT(26) | BIT(29))) {
  759. ret = call_fext_func(device, FUNC_FLAGS, 0x1, 0x0, 0x0);
  760. if (ret & BIT(5))
  761. sparse_keymap_report_event(priv->input,
  762. BIT(5), 1, true);
  763. if (ret & BIT(26))
  764. sparse_keymap_report_event(priv->input,
  765. BIT(26), 1, true);
  766. if (ret & BIT(29))
  767. sparse_keymap_report_event(priv->input,
  768. BIT(29), 1, true);
  769. }
  770. }
  771. /* Initialization */
  772. static const struct acpi_device_id fujitsu_bl_device_ids[] = {
  773. {ACPI_FUJITSU_BL_HID, 0},
  774. {"", 0},
  775. };
  776. static struct acpi_driver acpi_fujitsu_bl_driver = {
  777. .name = ACPI_FUJITSU_BL_DRIVER_NAME,
  778. .class = ACPI_FUJITSU_CLASS,
  779. .ids = fujitsu_bl_device_ids,
  780. .ops = {
  781. .add = acpi_fujitsu_bl_add,
  782. .notify = acpi_fujitsu_bl_notify,
  783. },
  784. };
  785. static const struct acpi_device_id fujitsu_laptop_device_ids[] = {
  786. {ACPI_FUJITSU_LAPTOP_HID, 0},
  787. {"", 0},
  788. };
  789. static struct acpi_driver acpi_fujitsu_laptop_driver = {
  790. .name = ACPI_FUJITSU_LAPTOP_DRIVER_NAME,
  791. .class = ACPI_FUJITSU_CLASS,
  792. .ids = fujitsu_laptop_device_ids,
  793. .ops = {
  794. .add = acpi_fujitsu_laptop_add,
  795. .remove = acpi_fujitsu_laptop_remove,
  796. .notify = acpi_fujitsu_laptop_notify,
  797. },
  798. };
  799. static const struct acpi_device_id fujitsu_ids[] __used = {
  800. {ACPI_FUJITSU_BL_HID, 0},
  801. {ACPI_FUJITSU_LAPTOP_HID, 0},
  802. {"", 0}
  803. };
  804. MODULE_DEVICE_TABLE(acpi, fujitsu_ids);
  805. static int __init fujitsu_init(void)
  806. {
  807. int ret;
  808. ret = acpi_bus_register_driver(&acpi_fujitsu_bl_driver);
  809. if (ret)
  810. return ret;
  811. /* Register platform stuff */
  812. ret = platform_driver_register(&fujitsu_pf_driver);
  813. if (ret)
  814. goto err_unregister_acpi;
  815. /* Register laptop driver */
  816. ret = acpi_bus_register_driver(&acpi_fujitsu_laptop_driver);
  817. if (ret)
  818. goto err_unregister_platform_driver;
  819. pr_info("driver " FUJITSU_DRIVER_VERSION " successfully loaded\n");
  820. return 0;
  821. err_unregister_platform_driver:
  822. platform_driver_unregister(&fujitsu_pf_driver);
  823. err_unregister_acpi:
  824. acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
  825. return ret;
  826. }
  827. static void __exit fujitsu_cleanup(void)
  828. {
  829. acpi_bus_unregister_driver(&acpi_fujitsu_laptop_driver);
  830. platform_driver_unregister(&fujitsu_pf_driver);
  831. acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
  832. pr_info("driver unloaded\n");
  833. }
  834. module_init(fujitsu_init);
  835. module_exit(fujitsu_cleanup);
  836. module_param(use_alt_lcd_levels, int, 0644);
  837. MODULE_PARM_DESC(use_alt_lcd_levels, "Interface used for setting LCD brightness level (-1 = auto, 0 = force SBLL, 1 = force SBL2)");
  838. module_param(disable_brightness_adjust, bool, 0644);
  839. MODULE_PARM_DESC(disable_brightness_adjust, "Disable LCD brightness adjustment");
  840. MODULE_AUTHOR("Jonathan Woithe, Peter Gruber, Tony Vroon");
  841. MODULE_DESCRIPTION("Fujitsu laptop extras support");
  842. MODULE_VERSION(FUJITSU_DRIVER_VERSION);
  843. MODULE_LICENSE("GPL");