fujitsu-laptop.c 28 KB

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