fujitsu-laptop.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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/dmi.h>
  49. #include <linux/backlight.h>
  50. #include <linux/fb.h>
  51. #include <linux/input.h>
  52. #include <linux/input/sparse-keymap.h>
  53. #include <linux/kfifo.h>
  54. #include <linux/leds.h>
  55. #include <linux/platform_device.h>
  56. #include <linux/slab.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_CODE1 0x80
  68. /* FUNC interface - command values */
  69. #define FUNC_FLAGS 0x1000
  70. #define FUNC_LEDS 0x1001
  71. #define FUNC_BUTTONS 0x1002
  72. #define FUNC_BACKLIGHT 0x1004
  73. /* FUNC interface - responses */
  74. #define UNSUPPORTED_CMD 0x80000000
  75. /* FUNC interface - status flags */
  76. #define FLAG_RFKILL 0x020
  77. #define FLAG_LID 0x100
  78. #define FLAG_DOCK 0x200
  79. /* FUNC interface - LED control */
  80. #define FUNC_LED_OFF 0x1
  81. #define FUNC_LED_ON 0x30001
  82. #define KEYBOARD_LAMPS 0x100
  83. #define LOGOLAMP_POWERON 0x2000
  84. #define LOGOLAMP_ALWAYS 0x4000
  85. #define RADIO_LED_ON 0x20
  86. #define ECO_LED 0x10000
  87. #define ECO_LED_ON 0x80000
  88. /* Hotkey details */
  89. #define KEY1_CODE 0x410 /* codes for the keys in the GIRB register */
  90. #define KEY2_CODE 0x411
  91. #define KEY3_CODE 0x412
  92. #define KEY4_CODE 0x413
  93. #define KEY5_CODE 0x420
  94. #define MAX_HOTKEY_RINGBUFFER_SIZE 100
  95. #define RINGBUFFERSIZE 40
  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. static int use_alt_lcd_levels = -1;
  106. static bool disable_brightness_adjust;
  107. /* Device used to access hotkeys and other features on the laptop */
  108. struct fujitsu_laptop {
  109. struct input_dev *input;
  110. char phys[32];
  111. struct platform_device *pf_device;
  112. struct kfifo fifo;
  113. spinlock_t fifo_lock;
  114. int flags_supported;
  115. int flags_state;
  116. };
  117. static struct acpi_device *fext;
  118. /* Fujitsu ACPI interface function */
  119. static int call_fext_func(struct acpi_device *device,
  120. int func, int op, int feature, int state)
  121. {
  122. union acpi_object params[4] = {
  123. { .integer.type = ACPI_TYPE_INTEGER, .integer.value = func },
  124. { .integer.type = ACPI_TYPE_INTEGER, .integer.value = op },
  125. { .integer.type = ACPI_TYPE_INTEGER, .integer.value = feature },
  126. { .integer.type = ACPI_TYPE_INTEGER, .integer.value = state }
  127. };
  128. struct acpi_object_list arg_list = { 4, params };
  129. unsigned long long value;
  130. acpi_status status;
  131. status = acpi_evaluate_integer(device->handle, "FUNC", &arg_list,
  132. &value);
  133. if (ACPI_FAILURE(status)) {
  134. acpi_handle_err(device->handle, "Failed to evaluate FUNC\n");
  135. return -ENODEV;
  136. }
  137. acpi_handle_debug(device->handle,
  138. "FUNC 0x%x (args 0x%x, 0x%x, 0x%x) returned 0x%x\n",
  139. func, op, feature, state, (int)value);
  140. return value;
  141. }
  142. /* Hardware access for LCD brightness control */
  143. static int set_lcd_level(struct acpi_device *device, int level)
  144. {
  145. struct fujitsu_bl *priv = acpi_driver_data(device);
  146. acpi_status status;
  147. char *method;
  148. switch (use_alt_lcd_levels) {
  149. case -1:
  150. if (acpi_has_method(device->handle, "SBL2"))
  151. method = "SBL2";
  152. else
  153. method = "SBLL";
  154. break;
  155. case 1:
  156. method = "SBL2";
  157. break;
  158. default:
  159. method = "SBLL";
  160. break;
  161. }
  162. acpi_handle_debug(device->handle, "set lcd level via %s [%d]\n", method,
  163. level);
  164. if (level < 0 || level >= priv->max_brightness)
  165. return -EINVAL;
  166. status = acpi_execute_simple_method(device->handle, method, level);
  167. if (ACPI_FAILURE(status)) {
  168. acpi_handle_err(device->handle, "Failed to evaluate %s\n",
  169. method);
  170. return -ENODEV;
  171. }
  172. priv->brightness_level = level;
  173. return 0;
  174. }
  175. static int get_lcd_level(struct acpi_device *device)
  176. {
  177. struct fujitsu_bl *priv = acpi_driver_data(device);
  178. unsigned long long state = 0;
  179. acpi_status status = AE_OK;
  180. acpi_handle_debug(device->handle, "get lcd level via GBLL\n");
  181. status = acpi_evaluate_integer(device->handle, "GBLL", NULL, &state);
  182. if (ACPI_FAILURE(status))
  183. return 0;
  184. priv->brightness_level = state & 0x0fffffff;
  185. return priv->brightness_level;
  186. }
  187. static int get_max_brightness(struct acpi_device *device)
  188. {
  189. struct fujitsu_bl *priv = acpi_driver_data(device);
  190. unsigned long long state = 0;
  191. acpi_status status = AE_OK;
  192. acpi_handle_debug(device->handle, "get max lcd level via RBLL\n");
  193. status = acpi_evaluate_integer(device->handle, "RBLL", NULL, &state);
  194. if (ACPI_FAILURE(status))
  195. return -1;
  196. priv->max_brightness = state;
  197. return priv->max_brightness;
  198. }
  199. /* Backlight device stuff */
  200. static int bl_get_brightness(struct backlight_device *b)
  201. {
  202. struct acpi_device *device = bl_get_data(b);
  203. return b->props.power == FB_BLANK_POWERDOWN ? 0 : get_lcd_level(device);
  204. }
  205. static int bl_update_status(struct backlight_device *b)
  206. {
  207. struct acpi_device *device = bl_get_data(b);
  208. if (fext) {
  209. if (b->props.power == FB_BLANK_POWERDOWN)
  210. call_fext_func(fext, FUNC_BACKLIGHT, 0x1, 0x4, 0x3);
  211. else
  212. call_fext_func(fext, FUNC_BACKLIGHT, 0x1, 0x4, 0x0);
  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 error;
  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. error = acpi_fujitsu_bl_input_setup(device);
  325. if (error)
  326. return error;
  327. pr_info("ACPI: %s [%s]\n",
  328. acpi_device_name(device), acpi_device_bid(device));
  329. if (get_max_brightness(device) <= 0)
  330. priv->max_brightness = FUJITSU_LCD_N_LEVELS;
  331. get_lcd_level(device);
  332. error = fujitsu_backlight_register(device);
  333. if (error)
  334. return error;
  335. return 0;
  336. }
  337. /* Brightness notify */
  338. static void acpi_fujitsu_bl_notify(struct acpi_device *device, u32 event)
  339. {
  340. struct fujitsu_bl *priv = acpi_driver_data(device);
  341. int oldb, newb;
  342. if (event != ACPI_FUJITSU_NOTIFY_CODE1) {
  343. acpi_handle_info(device->handle, "unsupported event [0x%x]\n",
  344. event);
  345. sparse_keymap_report_event(priv->input, -1, 1, true);
  346. return;
  347. }
  348. oldb = priv->brightness_level;
  349. get_lcd_level(device);
  350. newb = priv->brightness_level;
  351. acpi_handle_debug(device->handle,
  352. "brightness button event [%i -> %i]\n", oldb, newb);
  353. if (oldb == newb)
  354. return;
  355. if (!disable_brightness_adjust)
  356. set_lcd_level(device, newb);
  357. sparse_keymap_report_event(priv->input, oldb < newb, 1, true);
  358. }
  359. /* ACPI device for hotkey handling */
  360. static const struct key_entry keymap_default[] = {
  361. { KE_KEY, KEY1_CODE, { KEY_PROG1 } },
  362. { KE_KEY, KEY2_CODE, { KEY_PROG2 } },
  363. { KE_KEY, KEY3_CODE, { KEY_PROG3 } },
  364. { KE_KEY, KEY4_CODE, { KEY_PROG4 } },
  365. { KE_KEY, KEY5_CODE, { KEY_RFKILL } },
  366. { KE_KEY, BIT(26), { KEY_TOUCHPAD_TOGGLE } },
  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 led_classdev *led;
  557. int result;
  558. if (call_fext_func(device,
  559. FUNC_LEDS, 0x0, 0x0, 0x0) & LOGOLAMP_POWERON) {
  560. led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL);
  561. if (!led)
  562. return -ENOMEM;
  563. led->name = "fujitsu::logolamp";
  564. led->brightness_set_blocking = logolamp_set;
  565. led->brightness_get = logolamp_get;
  566. result = devm_led_classdev_register(&device->dev, led);
  567. if (result)
  568. return result;
  569. }
  570. if ((call_fext_func(device,
  571. FUNC_LEDS, 0x0, 0x0, 0x0) & KEYBOARD_LAMPS) &&
  572. (call_fext_func(device, FUNC_BUTTONS, 0x0, 0x0, 0x0) == 0x0)) {
  573. led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL);
  574. if (!led)
  575. return -ENOMEM;
  576. led->name = "fujitsu::kblamps";
  577. led->brightness_set_blocking = kblamps_set;
  578. led->brightness_get = kblamps_get;
  579. result = devm_led_classdev_register(&device->dev, led);
  580. if (result)
  581. return result;
  582. }
  583. /*
  584. * BTNI bit 24 seems to indicate the presence of a radio toggle
  585. * button in place of a slide switch, and all such machines appear
  586. * to also have an RF LED. Therefore use bit 24 as an indicator
  587. * that an RF LED is present.
  588. */
  589. if (call_fext_func(device, FUNC_BUTTONS, 0x0, 0x0, 0x0) & BIT(24)) {
  590. led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL);
  591. if (!led)
  592. return -ENOMEM;
  593. led->name = "fujitsu::radio_led";
  594. led->brightness_set_blocking = radio_led_set;
  595. led->brightness_get = radio_led_get;
  596. led->default_trigger = "rfkill-any";
  597. result = devm_led_classdev_register(&device->dev, led);
  598. if (result)
  599. return result;
  600. }
  601. /* Support for eco led is not always signaled in bit corresponding
  602. * to the bit used to control the led. According to the DSDT table,
  603. * bit 14 seems to indicate presence of said led as well.
  604. * Confirm by testing the status.
  605. */
  606. if ((call_fext_func(device, FUNC_LEDS, 0x0, 0x0, 0x0) & BIT(14)) &&
  607. (call_fext_func(device,
  608. FUNC_LEDS, 0x2, ECO_LED, 0x0) != UNSUPPORTED_CMD)) {
  609. led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL);
  610. if (!led)
  611. return -ENOMEM;
  612. led->name = "fujitsu::eco_led";
  613. led->brightness_set_blocking = eco_led_set;
  614. led->brightness_get = eco_led_get;
  615. result = devm_led_classdev_register(&device->dev, led);
  616. if (result)
  617. return result;
  618. }
  619. return 0;
  620. }
  621. static int acpi_fujitsu_laptop_add(struct acpi_device *device)
  622. {
  623. struct fujitsu_laptop *priv;
  624. int error;
  625. int i;
  626. priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
  627. if (!priv)
  628. return -ENOMEM;
  629. WARN_ONCE(fext, "More than one FUJ02E3 ACPI device was found. Driver may not work as intended.");
  630. fext = device;
  631. strcpy(acpi_device_name(device), ACPI_FUJITSU_LAPTOP_DEVICE_NAME);
  632. strcpy(acpi_device_class(device), ACPI_FUJITSU_CLASS);
  633. device->driver_data = priv;
  634. /* kfifo */
  635. spin_lock_init(&priv->fifo_lock);
  636. error = kfifo_alloc(&priv->fifo, RINGBUFFERSIZE * sizeof(int),
  637. GFP_KERNEL);
  638. if (error) {
  639. pr_err("kfifo_alloc failed\n");
  640. goto err_stop;
  641. }
  642. error = acpi_fujitsu_laptop_input_setup(device);
  643. if (error)
  644. goto err_free_fifo;
  645. pr_info("ACPI: %s [%s]\n",
  646. acpi_device_name(device), acpi_device_bid(device));
  647. i = 0;
  648. while (call_fext_func(device, FUNC_BUTTONS, 0x1, 0x0, 0x0) != 0
  649. && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE)
  650. ; /* No action, result is discarded */
  651. acpi_handle_debug(device->handle, "Discarded %i ringbuffer entries\n",
  652. i);
  653. priv->flags_supported = call_fext_func(device, FUNC_FLAGS, 0x0, 0x0,
  654. 0x0);
  655. /* Make sure our bitmask of supported functions is cleared if the
  656. RFKILL function block is not implemented, like on the S7020. */
  657. if (priv->flags_supported == UNSUPPORTED_CMD)
  658. priv->flags_supported = 0;
  659. if (priv->flags_supported)
  660. priv->flags_state = call_fext_func(device, FUNC_FLAGS, 0x4, 0x0,
  661. 0x0);
  662. /* Suspect this is a keymap of the application panel, print it */
  663. acpi_handle_info(device->handle, "BTNI: [0x%x]\n",
  664. call_fext_func(device, FUNC_BUTTONS, 0x0, 0x0, 0x0));
  665. /* Sync backlight power status */
  666. if (fujitsu_bl && fujitsu_bl->bl_device &&
  667. acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  668. if (call_fext_func(fext, FUNC_BACKLIGHT, 0x2, 0x4, 0x0) == 3)
  669. fujitsu_bl->bl_device->props.power = FB_BLANK_POWERDOWN;
  670. else
  671. fujitsu_bl->bl_device->props.power = FB_BLANK_UNBLANK;
  672. }
  673. error = acpi_fujitsu_laptop_leds_register(device);
  674. if (error)
  675. goto err_free_fifo;
  676. error = fujitsu_laptop_platform_add(device);
  677. if (error)
  678. goto err_free_fifo;
  679. return 0;
  680. err_free_fifo:
  681. kfifo_free(&priv->fifo);
  682. err_stop:
  683. return error;
  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 status;
  696. status = kfifo_in_locked(&priv->fifo, (unsigned char *)&scancode,
  697. sizeof(scancode), &priv->fifo_lock);
  698. if (status != 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, status;
  711. while (true) {
  712. status = kfifo_out_locked(&priv->fifo,
  713. (unsigned char *)&scancode,
  714. sizeof(scancode), &priv->fifo_lock);
  715. if (status != sizeof(scancode))
  716. return;
  717. sparse_keymap_report_event(priv->input, scancode, 0, false);
  718. dev_dbg(&priv->input->dev,
  719. "Pop scancode from ringbuffer [0x%x]\n", scancode);
  720. }
  721. }
  722. static void acpi_fujitsu_laptop_notify(struct acpi_device *device, u32 event)
  723. {
  724. struct fujitsu_laptop *priv = acpi_driver_data(device);
  725. int scancode, i = 0;
  726. unsigned int irb;
  727. if (event != ACPI_FUJITSU_NOTIFY_CODE1) {
  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. /* On some models (first seen on the Skylake-based Lifebook
  749. * E736/E746/E756), the touchpad toggle hotkey (Fn+F4) is
  750. * handled in software; its state is queried using FUNC_FLAGS
  751. */
  752. if ((priv->flags_supported & BIT(26)) &&
  753. (call_fext_func(device, FUNC_FLAGS, 0x1, 0x0, 0x0) & BIT(26)))
  754. sparse_keymap_report_event(priv->input, BIT(26), 1, true);
  755. }
  756. /* Initialization */
  757. static const struct acpi_device_id fujitsu_bl_device_ids[] = {
  758. {ACPI_FUJITSU_BL_HID, 0},
  759. {"", 0},
  760. };
  761. static struct acpi_driver acpi_fujitsu_bl_driver = {
  762. .name = ACPI_FUJITSU_BL_DRIVER_NAME,
  763. .class = ACPI_FUJITSU_CLASS,
  764. .ids = fujitsu_bl_device_ids,
  765. .ops = {
  766. .add = acpi_fujitsu_bl_add,
  767. .notify = acpi_fujitsu_bl_notify,
  768. },
  769. };
  770. static const struct acpi_device_id fujitsu_laptop_device_ids[] = {
  771. {ACPI_FUJITSU_LAPTOP_HID, 0},
  772. {"", 0},
  773. };
  774. static struct acpi_driver acpi_fujitsu_laptop_driver = {
  775. .name = ACPI_FUJITSU_LAPTOP_DRIVER_NAME,
  776. .class = ACPI_FUJITSU_CLASS,
  777. .ids = fujitsu_laptop_device_ids,
  778. .ops = {
  779. .add = acpi_fujitsu_laptop_add,
  780. .remove = acpi_fujitsu_laptop_remove,
  781. .notify = acpi_fujitsu_laptop_notify,
  782. },
  783. };
  784. static const struct acpi_device_id fujitsu_ids[] __used = {
  785. {ACPI_FUJITSU_BL_HID, 0},
  786. {ACPI_FUJITSU_LAPTOP_HID, 0},
  787. {"", 0}
  788. };
  789. MODULE_DEVICE_TABLE(acpi, fujitsu_ids);
  790. static int __init fujitsu_init(void)
  791. {
  792. int ret;
  793. ret = acpi_bus_register_driver(&acpi_fujitsu_bl_driver);
  794. if (ret)
  795. return ret;
  796. /* Register platform stuff */
  797. ret = platform_driver_register(&fujitsu_pf_driver);
  798. if (ret)
  799. goto err_unregister_acpi;
  800. /* Register laptop driver */
  801. ret = acpi_bus_register_driver(&acpi_fujitsu_laptop_driver);
  802. if (ret)
  803. goto err_unregister_platform_driver;
  804. pr_info("driver " FUJITSU_DRIVER_VERSION " successfully loaded\n");
  805. return 0;
  806. err_unregister_platform_driver:
  807. platform_driver_unregister(&fujitsu_pf_driver);
  808. err_unregister_acpi:
  809. acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
  810. return ret;
  811. }
  812. static void __exit fujitsu_cleanup(void)
  813. {
  814. acpi_bus_unregister_driver(&acpi_fujitsu_laptop_driver);
  815. platform_driver_unregister(&fujitsu_pf_driver);
  816. acpi_bus_unregister_driver(&acpi_fujitsu_bl_driver);
  817. pr_info("driver unloaded\n");
  818. }
  819. module_init(fujitsu_init);
  820. module_exit(fujitsu_cleanup);
  821. module_param(use_alt_lcd_levels, int, 0644);
  822. MODULE_PARM_DESC(use_alt_lcd_levels, "Interface used for setting LCD brightness level (-1 = auto, 0 = force SBLL, 1 = force SBL2)");
  823. module_param(disable_brightness_adjust, bool, 0644);
  824. MODULE_PARM_DESC(disable_brightness_adjust, "Disable LCD brightness adjustment");
  825. MODULE_AUTHOR("Jonathan Woithe, Peter Gruber, Tony Vroon");
  826. MODULE_DESCRIPTION("Fujitsu laptop extras support");
  827. MODULE_VERSION(FUJITSU_DRIVER_VERSION);
  828. MODULE_LICENSE("GPL");