fujitsu-laptop.c 28 KB

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