xref: /openbmc/linux/drivers/platform/x86/dell/dell-laptop.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1f1e1ea51SMario Limonciello // SPDX-License-Identifier: GPL-2.0-only
2f1e1ea51SMario Limonciello /*
3f1e1ea51SMario Limonciello  *  Driver for Dell laptop extras
4f1e1ea51SMario Limonciello  *
5f1e1ea51SMario Limonciello  *  Copyright (c) Red Hat <mjg@redhat.com>
6f1e1ea51SMario Limonciello  *  Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
7f1e1ea51SMario Limonciello  *  Copyright (c) 2014 Pali Rohár <pali@kernel.org>
8f1e1ea51SMario Limonciello  *
9f1e1ea51SMario Limonciello  *  Based on documentation in the libsmbios package:
10f1e1ea51SMario Limonciello  *  Copyright (C) 2005-2014 Dell Inc.
11f1e1ea51SMario Limonciello  */
12f1e1ea51SMario Limonciello 
13f1e1ea51SMario Limonciello #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14f1e1ea51SMario Limonciello 
15f1e1ea51SMario Limonciello #include <linux/module.h>
16f1e1ea51SMario Limonciello #include <linux/kernel.h>
17f1e1ea51SMario Limonciello #include <linux/init.h>
18f1e1ea51SMario Limonciello #include <linux/platform_device.h>
19f1e1ea51SMario Limonciello #include <linux/backlight.h>
20f1e1ea51SMario Limonciello #include <linux/err.h>
21f1e1ea51SMario Limonciello #include <linux/dmi.h>
22f1e1ea51SMario Limonciello #include <linux/io.h>
23f1e1ea51SMario Limonciello #include <linux/rfkill.h>
24f1e1ea51SMario Limonciello #include <linux/power_supply.h>
25f1e1ea51SMario Limonciello #include <linux/acpi.h>
26f1e1ea51SMario Limonciello #include <linux/mm.h>
27f1e1ea51SMario Limonciello #include <linux/i8042.h>
28f1e1ea51SMario Limonciello #include <linux/debugfs.h>
29f1e1ea51SMario Limonciello #include <linux/seq_file.h>
30f1e1ea51SMario Limonciello #include <acpi/video.h>
31f1e1ea51SMario Limonciello #include "dell-rbtn.h"
32f1e1ea51SMario Limonciello #include "dell-smbios.h"
33f1e1ea51SMario Limonciello 
348af9fa37SPerry Yuan #include "dell-wmi-privacy.h"
358af9fa37SPerry Yuan 
36f1e1ea51SMario Limonciello struct quirk_entry {
37f1e1ea51SMario Limonciello 	bool touchpad_led;
38f1e1ea51SMario Limonciello 	bool kbd_led_not_present;
39f1e1ea51SMario Limonciello 	bool kbd_led_levels_off_1;
40f1e1ea51SMario Limonciello 	bool kbd_missing_ac_tag;
41f1e1ea51SMario Limonciello 
42f1e1ea51SMario Limonciello 	bool needs_kbd_timeouts;
43f1e1ea51SMario Limonciello 	/*
44f1e1ea51SMario Limonciello 	 * Ordered list of timeouts expressed in seconds.
45f1e1ea51SMario Limonciello 	 * The list must end with -1
46f1e1ea51SMario Limonciello 	 */
47f1e1ea51SMario Limonciello 	int kbd_timeouts[];
48f1e1ea51SMario Limonciello };
49f1e1ea51SMario Limonciello 
50f1e1ea51SMario Limonciello static struct quirk_entry *quirks;
51f1e1ea51SMario Limonciello 
52f1e1ea51SMario Limonciello static struct quirk_entry quirk_dell_vostro_v130 = {
53f1e1ea51SMario Limonciello 	.touchpad_led = true,
54f1e1ea51SMario Limonciello };
55f1e1ea51SMario Limonciello 
dmi_matched(const struct dmi_system_id * dmi)56f1e1ea51SMario Limonciello static int __init dmi_matched(const struct dmi_system_id *dmi)
57f1e1ea51SMario Limonciello {
58f1e1ea51SMario Limonciello 	quirks = dmi->driver_data;
59f1e1ea51SMario Limonciello 	return 1;
60f1e1ea51SMario Limonciello }
61f1e1ea51SMario Limonciello 
62f1e1ea51SMario Limonciello /*
63f1e1ea51SMario Limonciello  * These values come from Windows utility provided by Dell. If any other value
64f1e1ea51SMario Limonciello  * is used then BIOS silently set timeout to 0 without any error message.
65f1e1ea51SMario Limonciello  */
66f1e1ea51SMario Limonciello static struct quirk_entry quirk_dell_xps13_9333 = {
67f1e1ea51SMario Limonciello 	.needs_kbd_timeouts = true,
68f1e1ea51SMario Limonciello 	.kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
69f1e1ea51SMario Limonciello };
70f1e1ea51SMario Limonciello 
71f1e1ea51SMario Limonciello static struct quirk_entry quirk_dell_xps13_9370 = {
72f1e1ea51SMario Limonciello 	.kbd_missing_ac_tag = true,
73f1e1ea51SMario Limonciello };
74f1e1ea51SMario Limonciello 
75f1e1ea51SMario Limonciello static struct quirk_entry quirk_dell_latitude_e6410 = {
76f1e1ea51SMario Limonciello 	.kbd_led_levels_off_1 = true,
77f1e1ea51SMario Limonciello };
78f1e1ea51SMario Limonciello 
79f1e1ea51SMario Limonciello static struct quirk_entry quirk_dell_inspiron_1012 = {
80f1e1ea51SMario Limonciello 	.kbd_led_not_present = true,
81f1e1ea51SMario Limonciello };
82f1e1ea51SMario Limonciello 
8389a8f23fSGabriele Mazzotta static struct quirk_entry quirk_dell_latitude_7520 = {
8489a8f23fSGabriele Mazzotta 	.kbd_missing_ac_tag = true,
8589a8f23fSGabriele Mazzotta };
8689a8f23fSGabriele Mazzotta 
87f1e1ea51SMario Limonciello static struct platform_driver platform_driver = {
88f1e1ea51SMario Limonciello 	.driver = {
89f1e1ea51SMario Limonciello 		.name = "dell-laptop",
90f1e1ea51SMario Limonciello 	}
91f1e1ea51SMario Limonciello };
92f1e1ea51SMario Limonciello 
93f1e1ea51SMario Limonciello static struct platform_device *platform_device;
94f1e1ea51SMario Limonciello static struct backlight_device *dell_backlight_device;
95f1e1ea51SMario Limonciello static struct rfkill *wifi_rfkill;
96f1e1ea51SMario Limonciello static struct rfkill *bluetooth_rfkill;
97f1e1ea51SMario Limonciello static struct rfkill *wwan_rfkill;
98f1e1ea51SMario Limonciello static bool force_rfkill;
998af9fa37SPerry Yuan static bool micmute_led_registered;
100*3ed0b880SKoba Ko static bool mute_led_registered;
101f1e1ea51SMario Limonciello 
102f1e1ea51SMario Limonciello module_param(force_rfkill, bool, 0444);
103f1e1ea51SMario Limonciello MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
104f1e1ea51SMario Limonciello 
105f1e1ea51SMario Limonciello static const struct dmi_system_id dell_device_table[] __initconst = {
106f1e1ea51SMario Limonciello 	{
107f1e1ea51SMario Limonciello 		.ident = "Dell laptop",
108f1e1ea51SMario Limonciello 		.matches = {
109f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
110f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
111f1e1ea51SMario Limonciello 		},
112f1e1ea51SMario Limonciello 	},
113f1e1ea51SMario Limonciello 	{
114f1e1ea51SMario Limonciello 		.matches = {
115f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
116f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
117f1e1ea51SMario Limonciello 		},
118f1e1ea51SMario Limonciello 	},
119f1e1ea51SMario Limonciello 	{
120f1e1ea51SMario Limonciello 		.matches = {
121f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
122f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
123f1e1ea51SMario Limonciello 		},
124f1e1ea51SMario Limonciello 	},
125f1e1ea51SMario Limonciello 	{
126f1e1ea51SMario Limonciello 		.matches = {
127f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
128f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
129f1e1ea51SMario Limonciello 		},
130f1e1ea51SMario Limonciello 	},
131f1e1ea51SMario Limonciello 	{
132f1e1ea51SMario Limonciello 		.matches = {
133f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
134f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
135f1e1ea51SMario Limonciello 		},
136f1e1ea51SMario Limonciello 	},
137f1e1ea51SMario Limonciello 	{
138f1e1ea51SMario Limonciello 		.matches = {
139f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
140f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
141f1e1ea51SMario Limonciello 		},
142f1e1ea51SMario Limonciello 	},
143f1e1ea51SMario Limonciello 	{
144f1e1ea51SMario Limonciello 		.ident = "Dell Computer Corporation",
145f1e1ea51SMario Limonciello 		.matches = {
146f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
147f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
148f1e1ea51SMario Limonciello 		},
149f1e1ea51SMario Limonciello 	},
150f1e1ea51SMario Limonciello 	{ }
151f1e1ea51SMario Limonciello };
152f1e1ea51SMario Limonciello MODULE_DEVICE_TABLE(dmi, dell_device_table);
153f1e1ea51SMario Limonciello 
154f1e1ea51SMario Limonciello static const struct dmi_system_id dell_quirks[] __initconst = {
155f1e1ea51SMario Limonciello 	{
156f1e1ea51SMario Limonciello 		.callback = dmi_matched,
157f1e1ea51SMario Limonciello 		.ident = "Dell Vostro V130",
158f1e1ea51SMario Limonciello 		.matches = {
159f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
160f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
161f1e1ea51SMario Limonciello 		},
162f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
163f1e1ea51SMario Limonciello 	},
164f1e1ea51SMario Limonciello 	{
165f1e1ea51SMario Limonciello 		.callback = dmi_matched,
166f1e1ea51SMario Limonciello 		.ident = "Dell Vostro V131",
167f1e1ea51SMario Limonciello 		.matches = {
168f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
169f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
170f1e1ea51SMario Limonciello 		},
171f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
172f1e1ea51SMario Limonciello 	},
173f1e1ea51SMario Limonciello 	{
174f1e1ea51SMario Limonciello 		.callback = dmi_matched,
175f1e1ea51SMario Limonciello 		.ident = "Dell Vostro 3350",
176f1e1ea51SMario Limonciello 		.matches = {
177f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
178f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
179f1e1ea51SMario Limonciello 		},
180f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
181f1e1ea51SMario Limonciello 	},
182f1e1ea51SMario Limonciello 	{
183f1e1ea51SMario Limonciello 		.callback = dmi_matched,
184f1e1ea51SMario Limonciello 		.ident = "Dell Vostro 3555",
185f1e1ea51SMario Limonciello 		.matches = {
186f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
187f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
188f1e1ea51SMario Limonciello 		},
189f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
190f1e1ea51SMario Limonciello 	},
191f1e1ea51SMario Limonciello 	{
192f1e1ea51SMario Limonciello 		.callback = dmi_matched,
193f1e1ea51SMario Limonciello 		.ident = "Dell Inspiron N311z",
194f1e1ea51SMario Limonciello 		.matches = {
195f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
196f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
197f1e1ea51SMario Limonciello 		},
198f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
199f1e1ea51SMario Limonciello 	},
200f1e1ea51SMario Limonciello 	{
201f1e1ea51SMario Limonciello 		.callback = dmi_matched,
202f1e1ea51SMario Limonciello 		.ident = "Dell Inspiron M5110",
203f1e1ea51SMario Limonciello 		.matches = {
204f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
205f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
206f1e1ea51SMario Limonciello 		},
207f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
208f1e1ea51SMario Limonciello 	},
209f1e1ea51SMario Limonciello 	{
210f1e1ea51SMario Limonciello 		.callback = dmi_matched,
211f1e1ea51SMario Limonciello 		.ident = "Dell Vostro 3360",
212f1e1ea51SMario Limonciello 		.matches = {
213f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
214f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
215f1e1ea51SMario Limonciello 		},
216f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
217f1e1ea51SMario Limonciello 	},
218f1e1ea51SMario Limonciello 	{
219f1e1ea51SMario Limonciello 		.callback = dmi_matched,
220f1e1ea51SMario Limonciello 		.ident = "Dell Vostro 3460",
221f1e1ea51SMario Limonciello 		.matches = {
222f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
223f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
224f1e1ea51SMario Limonciello 		},
225f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
226f1e1ea51SMario Limonciello 	},
227f1e1ea51SMario Limonciello 	{
228f1e1ea51SMario Limonciello 		.callback = dmi_matched,
229f1e1ea51SMario Limonciello 		.ident = "Dell Vostro 3560",
230f1e1ea51SMario Limonciello 		.matches = {
231f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
232f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
233f1e1ea51SMario Limonciello 		},
234f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
235f1e1ea51SMario Limonciello 	},
236f1e1ea51SMario Limonciello 	{
237f1e1ea51SMario Limonciello 		.callback = dmi_matched,
238f1e1ea51SMario Limonciello 		.ident = "Dell Vostro 3450",
239f1e1ea51SMario Limonciello 		.matches = {
240f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
241f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
242f1e1ea51SMario Limonciello 		},
243f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
244f1e1ea51SMario Limonciello 	},
245f1e1ea51SMario Limonciello 	{
246f1e1ea51SMario Limonciello 		.callback = dmi_matched,
247f1e1ea51SMario Limonciello 		.ident = "Dell Inspiron 5420",
248f1e1ea51SMario Limonciello 		.matches = {
249f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
250f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
251f1e1ea51SMario Limonciello 		},
252f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
253f1e1ea51SMario Limonciello 	},
254f1e1ea51SMario Limonciello 	{
255f1e1ea51SMario Limonciello 		.callback = dmi_matched,
256f1e1ea51SMario Limonciello 		.ident = "Dell Inspiron 5520",
257f1e1ea51SMario Limonciello 		.matches = {
258f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
259f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
260f1e1ea51SMario Limonciello 		},
261f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
262f1e1ea51SMario Limonciello 	},
263f1e1ea51SMario Limonciello 	{
264f1e1ea51SMario Limonciello 		.callback = dmi_matched,
265f1e1ea51SMario Limonciello 		.ident = "Dell Inspiron 5720",
266f1e1ea51SMario Limonciello 		.matches = {
267f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
268f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
269f1e1ea51SMario Limonciello 		},
270f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
271f1e1ea51SMario Limonciello 	},
272f1e1ea51SMario Limonciello 	{
273f1e1ea51SMario Limonciello 		.callback = dmi_matched,
274f1e1ea51SMario Limonciello 		.ident = "Dell Inspiron 7420",
275f1e1ea51SMario Limonciello 		.matches = {
276f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
277f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
278f1e1ea51SMario Limonciello 		},
279f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
280f1e1ea51SMario Limonciello 	},
281f1e1ea51SMario Limonciello 	{
282f1e1ea51SMario Limonciello 		.callback = dmi_matched,
283f1e1ea51SMario Limonciello 		.ident = "Dell Inspiron 7520",
284f1e1ea51SMario Limonciello 		.matches = {
285f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
286f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
287f1e1ea51SMario Limonciello 		},
288f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
289f1e1ea51SMario Limonciello 	},
290f1e1ea51SMario Limonciello 	{
291f1e1ea51SMario Limonciello 		.callback = dmi_matched,
292f1e1ea51SMario Limonciello 		.ident = "Dell Inspiron 7720",
293f1e1ea51SMario Limonciello 		.matches = {
294f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
295f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
296f1e1ea51SMario Limonciello 		},
297f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_vostro_v130,
298f1e1ea51SMario Limonciello 	},
299f1e1ea51SMario Limonciello 	{
300f1e1ea51SMario Limonciello 		.callback = dmi_matched,
301f1e1ea51SMario Limonciello 		.ident = "Dell XPS13 9333",
302f1e1ea51SMario Limonciello 		.matches = {
303f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
304f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
305f1e1ea51SMario Limonciello 		},
306f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_xps13_9333,
307f1e1ea51SMario Limonciello 	},
308f1e1ea51SMario Limonciello 	{
309f1e1ea51SMario Limonciello 		.callback = dmi_matched,
310f1e1ea51SMario Limonciello 		.ident = "Dell XPS 13 9370",
311f1e1ea51SMario Limonciello 		.matches = {
312f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
313f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9370"),
314f1e1ea51SMario Limonciello 		},
315f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_xps13_9370,
316f1e1ea51SMario Limonciello 	},
317f1e1ea51SMario Limonciello 	{
318f1e1ea51SMario Limonciello 		.callback = dmi_matched,
319f1e1ea51SMario Limonciello 		.ident = "Dell Latitude E6410",
320f1e1ea51SMario Limonciello 		.matches = {
321f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
322f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
323f1e1ea51SMario Limonciello 		},
324f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_latitude_e6410,
325f1e1ea51SMario Limonciello 	},
326f1e1ea51SMario Limonciello 	{
327f1e1ea51SMario Limonciello 		.callback = dmi_matched,
328f1e1ea51SMario Limonciello 		.ident = "Dell Inspiron 1012",
329f1e1ea51SMario Limonciello 		.matches = {
330f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
331f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
332f1e1ea51SMario Limonciello 		},
333f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_inspiron_1012,
334f1e1ea51SMario Limonciello 	},
335f1e1ea51SMario Limonciello 	{
336f1e1ea51SMario Limonciello 		.callback = dmi_matched,
337f1e1ea51SMario Limonciello 		.ident = "Dell Inspiron 1018",
338f1e1ea51SMario Limonciello 		.matches = {
339f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
340f1e1ea51SMario Limonciello 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1018"),
341f1e1ea51SMario Limonciello 		},
342f1e1ea51SMario Limonciello 		.driver_data = &quirk_dell_inspiron_1012,
343f1e1ea51SMario Limonciello 	},
34489a8f23fSGabriele Mazzotta 	{
34589a8f23fSGabriele Mazzotta 		.callback = dmi_matched,
34689a8f23fSGabriele Mazzotta 		.ident = "Dell Latitude 7520",
34789a8f23fSGabriele Mazzotta 		.matches = {
34889a8f23fSGabriele Mazzotta 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
34989a8f23fSGabriele Mazzotta 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude 7520"),
35089a8f23fSGabriele Mazzotta 		},
35189a8f23fSGabriele Mazzotta 		.driver_data = &quirk_dell_latitude_7520,
35289a8f23fSGabriele Mazzotta 	},
353f1e1ea51SMario Limonciello 	{ }
354f1e1ea51SMario Limonciello };
355f1e1ea51SMario Limonciello 
dell_fill_request(struct calling_interface_buffer * buffer,u32 arg0,u32 arg1,u32 arg2,u32 arg3)356f1e1ea51SMario Limonciello static void dell_fill_request(struct calling_interface_buffer *buffer,
357f1e1ea51SMario Limonciello 			       u32 arg0, u32 arg1, u32 arg2, u32 arg3)
358f1e1ea51SMario Limonciello {
359f1e1ea51SMario Limonciello 	memset(buffer, 0, sizeof(struct calling_interface_buffer));
360f1e1ea51SMario Limonciello 	buffer->input[0] = arg0;
361f1e1ea51SMario Limonciello 	buffer->input[1] = arg1;
362f1e1ea51SMario Limonciello 	buffer->input[2] = arg2;
363f1e1ea51SMario Limonciello 	buffer->input[3] = arg3;
364f1e1ea51SMario Limonciello }
365f1e1ea51SMario Limonciello 
dell_send_request(struct calling_interface_buffer * buffer,u16 class,u16 select)366f1e1ea51SMario Limonciello static int dell_send_request(struct calling_interface_buffer *buffer,
367f1e1ea51SMario Limonciello 			     u16 class, u16 select)
368f1e1ea51SMario Limonciello {
369f1e1ea51SMario Limonciello 	int ret;
370f1e1ea51SMario Limonciello 
371f1e1ea51SMario Limonciello 	buffer->cmd_class = class;
372f1e1ea51SMario Limonciello 	buffer->cmd_select = select;
373f1e1ea51SMario Limonciello 	ret = dell_smbios_call(buffer);
374f1e1ea51SMario Limonciello 	if (ret != 0)
375f1e1ea51SMario Limonciello 		return ret;
376f1e1ea51SMario Limonciello 	return dell_smbios_error(buffer->output[0]);
377f1e1ea51SMario Limonciello }
378f1e1ea51SMario Limonciello 
379f1e1ea51SMario Limonciello /*
380f1e1ea51SMario Limonciello  * Derived from information in smbios-wireless-ctl:
381f1e1ea51SMario Limonciello  *
382f1e1ea51SMario Limonciello  * cbSelect 17, Value 11
383f1e1ea51SMario Limonciello  *
384f1e1ea51SMario Limonciello  * Return Wireless Info
385f1e1ea51SMario Limonciello  * cbArg1, byte0 = 0x00
386f1e1ea51SMario Limonciello  *
387f1e1ea51SMario Limonciello  *     cbRes1 Standard return codes (0, -1, -2)
388f1e1ea51SMario Limonciello  *     cbRes2 Info bit flags:
389f1e1ea51SMario Limonciello  *
390f1e1ea51SMario Limonciello  *     0 Hardware switch supported (1)
391f1e1ea51SMario Limonciello  *     1 WiFi locator supported (1)
392f1e1ea51SMario Limonciello  *     2 WLAN supported (1)
393f1e1ea51SMario Limonciello  *     3 Bluetooth (BT) supported (1)
394f1e1ea51SMario Limonciello  *     4 WWAN supported (1)
395f1e1ea51SMario Limonciello  *     5 Wireless KBD supported (1)
396f1e1ea51SMario Limonciello  *     6 Uw b supported (1)
397f1e1ea51SMario Limonciello  *     7 WiGig supported (1)
398f1e1ea51SMario Limonciello  *     8 WLAN installed (1)
399f1e1ea51SMario Limonciello  *     9 BT installed (1)
400f1e1ea51SMario Limonciello  *     10 WWAN installed (1)
401f1e1ea51SMario Limonciello  *     11 Uw b installed (1)
402f1e1ea51SMario Limonciello  *     12 WiGig installed (1)
403f1e1ea51SMario Limonciello  *     13-15 Reserved (0)
404f1e1ea51SMario Limonciello  *     16 Hardware (HW) switch is On (1)
405f1e1ea51SMario Limonciello  *     17 WLAN disabled (1)
406f1e1ea51SMario Limonciello  *     18 BT disabled (1)
407f1e1ea51SMario Limonciello  *     19 WWAN disabled (1)
408f1e1ea51SMario Limonciello  *     20 Uw b disabled (1)
409f1e1ea51SMario Limonciello  *     21 WiGig disabled (1)
410f1e1ea51SMario Limonciello  *     20-31 Reserved (0)
411f1e1ea51SMario Limonciello  *
412f1e1ea51SMario Limonciello  *     cbRes3 NVRAM size in bytes
413f1e1ea51SMario Limonciello  *     cbRes4, byte 0 NVRAM format version number
414f1e1ea51SMario Limonciello  *
415f1e1ea51SMario Limonciello  *
416f1e1ea51SMario Limonciello  * Set QuickSet Radio Disable Flag
417f1e1ea51SMario Limonciello  *     cbArg1, byte0 = 0x01
418f1e1ea51SMario Limonciello  *     cbArg1, byte1
419f1e1ea51SMario Limonciello  *     Radio ID     value:
420f1e1ea51SMario Limonciello  *     0        Radio Status
421f1e1ea51SMario Limonciello  *     1        WLAN ID
422f1e1ea51SMario Limonciello  *     2        BT ID
423f1e1ea51SMario Limonciello  *     3        WWAN ID
424f1e1ea51SMario Limonciello  *     4        UWB ID
425f1e1ea51SMario Limonciello  *     5        WIGIG ID
426f1e1ea51SMario Limonciello  *     cbArg1, byte2    Flag bits:
427f1e1ea51SMario Limonciello  *             0 QuickSet disables radio (1)
428f1e1ea51SMario Limonciello  *             1-7 Reserved (0)
429f1e1ea51SMario Limonciello  *
430f1e1ea51SMario Limonciello  *     cbRes1    Standard return codes (0, -1, -2)
431f1e1ea51SMario Limonciello  *     cbRes2    QuickSet (QS) radio disable bit map:
432f1e1ea51SMario Limonciello  *     0 QS disables WLAN
433f1e1ea51SMario Limonciello  *     1 QS disables BT
434f1e1ea51SMario Limonciello  *     2 QS disables WWAN
435f1e1ea51SMario Limonciello  *     3 QS disables UWB
436f1e1ea51SMario Limonciello  *     4 QS disables WIGIG
437f1e1ea51SMario Limonciello  *     5-31 Reserved (0)
438f1e1ea51SMario Limonciello  *
439f1e1ea51SMario Limonciello  * Wireless Switch Configuration
440f1e1ea51SMario Limonciello  *     cbArg1, byte0 = 0x02
441f1e1ea51SMario Limonciello  *
442f1e1ea51SMario Limonciello  *     cbArg1, byte1
443f1e1ea51SMario Limonciello  *     Subcommand:
444f1e1ea51SMario Limonciello  *     0 Get config
445f1e1ea51SMario Limonciello  *     1 Set config
446f1e1ea51SMario Limonciello  *     2 Set WiFi locator enable/disable
447f1e1ea51SMario Limonciello  *     cbArg1,byte2
448f1e1ea51SMario Limonciello  *     Switch settings (if byte 1==1):
449f1e1ea51SMario Limonciello  *     0 WLAN sw itch control (1)
450f1e1ea51SMario Limonciello  *     1 BT sw itch control (1)
451f1e1ea51SMario Limonciello  *     2 WWAN sw itch control (1)
452f1e1ea51SMario Limonciello  *     3 UWB sw itch control (1)
453f1e1ea51SMario Limonciello  *     4 WiGig sw itch control (1)
454f1e1ea51SMario Limonciello  *     5-7 Reserved (0)
455f1e1ea51SMario Limonciello  *    cbArg1, byte2 Enable bits (if byte 1==2):
456f1e1ea51SMario Limonciello  *     0 Enable WiFi locator (1)
457f1e1ea51SMario Limonciello  *
458f1e1ea51SMario Limonciello  *    cbRes1     Standard return codes (0, -1, -2)
459f1e1ea51SMario Limonciello  *    cbRes2 QuickSet radio disable bit map:
460f1e1ea51SMario Limonciello  *     0 WLAN controlled by sw itch (1)
461f1e1ea51SMario Limonciello  *     1 BT controlled by sw itch (1)
462f1e1ea51SMario Limonciello  *     2 WWAN controlled by sw itch (1)
463f1e1ea51SMario Limonciello  *     3 UWB controlled by sw itch (1)
464f1e1ea51SMario Limonciello  *     4 WiGig controlled by sw itch (1)
465f1e1ea51SMario Limonciello  *     5-6 Reserved (0)
466f1e1ea51SMario Limonciello  *     7 Wireless sw itch config locked (1)
467f1e1ea51SMario Limonciello  *     8 WiFi locator enabled (1)
468f1e1ea51SMario Limonciello  *     9-14 Reserved (0)
469f1e1ea51SMario Limonciello  *     15 WiFi locator setting locked (1)
470f1e1ea51SMario Limonciello  *     16-31 Reserved (0)
471f1e1ea51SMario Limonciello  *
472f1e1ea51SMario Limonciello  * Read Local Config Data (LCD)
473f1e1ea51SMario Limonciello  *     cbArg1, byte0 = 0x10
474f1e1ea51SMario Limonciello  *     cbArg1, byte1 NVRAM index low byte
475f1e1ea51SMario Limonciello  *     cbArg1, byte2 NVRAM index high byte
476f1e1ea51SMario Limonciello  *     cbRes1 Standard return codes (0, -1, -2)
477f1e1ea51SMario Limonciello  *     cbRes2 4 bytes read from LCD[index]
478f1e1ea51SMario Limonciello  *     cbRes3 4 bytes read from LCD[index+4]
479f1e1ea51SMario Limonciello  *     cbRes4 4 bytes read from LCD[index+8]
480f1e1ea51SMario Limonciello  *
481f1e1ea51SMario Limonciello  * Write Local Config Data (LCD)
482f1e1ea51SMario Limonciello  *     cbArg1, byte0 = 0x11
483f1e1ea51SMario Limonciello  *     cbArg1, byte1 NVRAM index low byte
484f1e1ea51SMario Limonciello  *     cbArg1, byte2 NVRAM index high byte
485f1e1ea51SMario Limonciello  *     cbArg2 4 bytes to w rite at LCD[index]
486f1e1ea51SMario Limonciello  *     cbArg3 4 bytes to w rite at LCD[index+4]
487f1e1ea51SMario Limonciello  *     cbArg4 4 bytes to w rite at LCD[index+8]
488f1e1ea51SMario Limonciello  *     cbRes1 Standard return codes (0, -1, -2)
489f1e1ea51SMario Limonciello  *
490f1e1ea51SMario Limonciello  * Populate Local Config Data from NVRAM
491f1e1ea51SMario Limonciello  *     cbArg1, byte0 = 0x12
492f1e1ea51SMario Limonciello  *     cbRes1 Standard return codes (0, -1, -2)
493f1e1ea51SMario Limonciello  *
494f1e1ea51SMario Limonciello  * Commit Local Config Data to NVRAM
495f1e1ea51SMario Limonciello  *     cbArg1, byte0 = 0x13
496f1e1ea51SMario Limonciello  *     cbRes1 Standard return codes (0, -1, -2)
497f1e1ea51SMario Limonciello  */
498f1e1ea51SMario Limonciello 
dell_rfkill_set(void * data,bool blocked)499f1e1ea51SMario Limonciello static int dell_rfkill_set(void *data, bool blocked)
500f1e1ea51SMario Limonciello {
501f1e1ea51SMario Limonciello 	int disable = blocked ? 1 : 0;
502f1e1ea51SMario Limonciello 	unsigned long radio = (unsigned long)data;
503f1e1ea51SMario Limonciello 	int hwswitch_bit = (unsigned long)data - 1;
504f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
505f1e1ea51SMario Limonciello 	int hwswitch;
506f1e1ea51SMario Limonciello 	int status;
507f1e1ea51SMario Limonciello 	int ret;
508f1e1ea51SMario Limonciello 
509f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 0, 0, 0, 0);
510f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
511f1e1ea51SMario Limonciello 	if (ret)
512f1e1ea51SMario Limonciello 		return ret;
513f1e1ea51SMario Limonciello 	status = buffer.output[1];
514f1e1ea51SMario Limonciello 
515f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 0x2, 0, 0, 0);
516f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
517f1e1ea51SMario Limonciello 	if (ret)
518f1e1ea51SMario Limonciello 		return ret;
519f1e1ea51SMario Limonciello 	hwswitch = buffer.output[1];
520f1e1ea51SMario Limonciello 
521f1e1ea51SMario Limonciello 	/* If the hardware switch controls this radio, and the hardware
522f1e1ea51SMario Limonciello 	   switch is disabled, always disable the radio */
523f1e1ea51SMario Limonciello 	if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
524f1e1ea51SMario Limonciello 	    (status & BIT(0)) && !(status & BIT(16)))
525f1e1ea51SMario Limonciello 		disable = 1;
526f1e1ea51SMario Limonciello 
527f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 1 | (radio<<8) | (disable << 16), 0, 0, 0);
528f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
529f1e1ea51SMario Limonciello 	return ret;
530f1e1ea51SMario Limonciello }
531f1e1ea51SMario Limonciello 
dell_rfkill_update_sw_state(struct rfkill * rfkill,int radio,int status)532f1e1ea51SMario Limonciello static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
533f1e1ea51SMario Limonciello 					int status)
534f1e1ea51SMario Limonciello {
535f1e1ea51SMario Limonciello 	if (status & BIT(0)) {
536f1e1ea51SMario Limonciello 		/* Has hw-switch, sync sw_state to BIOS */
537f1e1ea51SMario Limonciello 		struct calling_interface_buffer buffer;
538f1e1ea51SMario Limonciello 		int block = rfkill_blocked(rfkill);
539f1e1ea51SMario Limonciello 		dell_fill_request(&buffer,
540f1e1ea51SMario Limonciello 				   1 | (radio << 8) | (block << 16), 0, 0, 0);
541f1e1ea51SMario Limonciello 		dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
542f1e1ea51SMario Limonciello 	} else {
543f1e1ea51SMario Limonciello 		/* No hw-switch, sync BIOS state to sw_state */
544f1e1ea51SMario Limonciello 		rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
545f1e1ea51SMario Limonciello 	}
546f1e1ea51SMario Limonciello }
547f1e1ea51SMario Limonciello 
dell_rfkill_update_hw_state(struct rfkill * rfkill,int radio,int status,int hwswitch)548f1e1ea51SMario Limonciello static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
549f1e1ea51SMario Limonciello 					int status, int hwswitch)
550f1e1ea51SMario Limonciello {
551f1e1ea51SMario Limonciello 	if (hwswitch & (BIT(radio - 1)))
552f1e1ea51SMario Limonciello 		rfkill_set_hw_state(rfkill, !(status & BIT(16)));
553f1e1ea51SMario Limonciello }
554f1e1ea51SMario Limonciello 
dell_rfkill_query(struct rfkill * rfkill,void * data)555f1e1ea51SMario Limonciello static void dell_rfkill_query(struct rfkill *rfkill, void *data)
556f1e1ea51SMario Limonciello {
557f1e1ea51SMario Limonciello 	int radio = ((unsigned long)data & 0xF);
558f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
559f1e1ea51SMario Limonciello 	int hwswitch;
560f1e1ea51SMario Limonciello 	int status;
561f1e1ea51SMario Limonciello 	int ret;
562f1e1ea51SMario Limonciello 
563f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 0, 0, 0, 0);
564f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
565f1e1ea51SMario Limonciello 	status = buffer.output[1];
566f1e1ea51SMario Limonciello 
567f1e1ea51SMario Limonciello 	if (ret != 0 || !(status & BIT(0))) {
568f1e1ea51SMario Limonciello 		return;
569f1e1ea51SMario Limonciello 	}
570f1e1ea51SMario Limonciello 
571f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 0x2, 0, 0, 0);
572f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
573f1e1ea51SMario Limonciello 	hwswitch = buffer.output[1];
574f1e1ea51SMario Limonciello 
575f1e1ea51SMario Limonciello 	if (ret != 0)
576f1e1ea51SMario Limonciello 		return;
577f1e1ea51SMario Limonciello 
578f1e1ea51SMario Limonciello 	dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
579f1e1ea51SMario Limonciello }
580f1e1ea51SMario Limonciello 
581f1e1ea51SMario Limonciello static const struct rfkill_ops dell_rfkill_ops = {
582f1e1ea51SMario Limonciello 	.set_block = dell_rfkill_set,
583f1e1ea51SMario Limonciello 	.query = dell_rfkill_query,
584f1e1ea51SMario Limonciello };
585f1e1ea51SMario Limonciello 
586f1e1ea51SMario Limonciello static struct dentry *dell_laptop_dir;
587f1e1ea51SMario Limonciello 
dell_debugfs_show(struct seq_file * s,void * data)588f1e1ea51SMario Limonciello static int dell_debugfs_show(struct seq_file *s, void *data)
589f1e1ea51SMario Limonciello {
590f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
591f1e1ea51SMario Limonciello 	int hwswitch_state;
592f1e1ea51SMario Limonciello 	int hwswitch_ret;
593f1e1ea51SMario Limonciello 	int status;
594f1e1ea51SMario Limonciello 	int ret;
595f1e1ea51SMario Limonciello 
596f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 0, 0, 0, 0);
597f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
598f1e1ea51SMario Limonciello 	if (ret)
599f1e1ea51SMario Limonciello 		return ret;
600f1e1ea51SMario Limonciello 	status = buffer.output[1];
601f1e1ea51SMario Limonciello 
602f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 0x2, 0, 0, 0);
603f1e1ea51SMario Limonciello 	hwswitch_ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
604f1e1ea51SMario Limonciello 	if (hwswitch_ret)
605f1e1ea51SMario Limonciello 		return hwswitch_ret;
606f1e1ea51SMario Limonciello 	hwswitch_state = buffer.output[1];
607f1e1ea51SMario Limonciello 
608f1e1ea51SMario Limonciello 	seq_printf(s, "return:\t%d\n", ret);
609f1e1ea51SMario Limonciello 	seq_printf(s, "status:\t0x%X\n", status);
610f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 0 : Hardware switch supported:   %lu\n",
611f1e1ea51SMario Limonciello 		   status & BIT(0));
612f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 1 : Wifi locator supported:      %lu\n",
613f1e1ea51SMario Limonciello 		  (status & BIT(1)) >> 1);
614f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 2 : Wifi is supported:           %lu\n",
615f1e1ea51SMario Limonciello 		  (status & BIT(2)) >> 2);
616f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 3 : Bluetooth is supported:      %lu\n",
617f1e1ea51SMario Limonciello 		  (status & BIT(3)) >> 3);
618f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 4 : WWAN is supported:           %lu\n",
619f1e1ea51SMario Limonciello 		  (status & BIT(4)) >> 4);
620f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
621f1e1ea51SMario Limonciello 		  (status & BIT(5)) >> 5);
622f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 6 : UWB supported:               %lu\n",
623f1e1ea51SMario Limonciello 		  (status & BIT(6)) >> 6);
624f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 7 : WiGig supported:             %lu\n",
625f1e1ea51SMario Limonciello 		  (status & BIT(7)) >> 7);
626f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 8 : Wifi is installed:           %lu\n",
627f1e1ea51SMario Limonciello 		  (status & BIT(8)) >> 8);
628f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 9 : Bluetooth is installed:      %lu\n",
629f1e1ea51SMario Limonciello 		  (status & BIT(9)) >> 9);
630f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 10: WWAN is installed:           %lu\n",
631f1e1ea51SMario Limonciello 		  (status & BIT(10)) >> 10);
632f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 11: UWB installed:               %lu\n",
633f1e1ea51SMario Limonciello 		  (status & BIT(11)) >> 11);
634f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 12: WiGig installed:             %lu\n",
635f1e1ea51SMario Limonciello 		  (status & BIT(12)) >> 12);
636f1e1ea51SMario Limonciello 
637f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 16: Hardware switch is on:       %lu\n",
638f1e1ea51SMario Limonciello 		  (status & BIT(16)) >> 16);
639f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 17: Wifi is blocked:             %lu\n",
640f1e1ea51SMario Limonciello 		  (status & BIT(17)) >> 17);
641f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 18: Bluetooth is blocked:        %lu\n",
642f1e1ea51SMario Limonciello 		  (status & BIT(18)) >> 18);
643f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 19: WWAN is blocked:             %lu\n",
644f1e1ea51SMario Limonciello 		  (status & BIT(19)) >> 19);
645f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 20: UWB is blocked:              %lu\n",
646f1e1ea51SMario Limonciello 		  (status & BIT(20)) >> 20);
647f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 21: WiGig is blocked:            %lu\n",
648f1e1ea51SMario Limonciello 		  (status & BIT(21)) >> 21);
649f1e1ea51SMario Limonciello 
650f1e1ea51SMario Limonciello 	seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
651f1e1ea51SMario Limonciello 	seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
652f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 0 : Wifi controlled by switch:      %lu\n",
653f1e1ea51SMario Limonciello 		   hwswitch_state & BIT(0));
654f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
655f1e1ea51SMario Limonciello 		   (hwswitch_state & BIT(1)) >> 1);
656f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 2 : WWAN controlled by switch:      %lu\n",
657f1e1ea51SMario Limonciello 		   (hwswitch_state & BIT(2)) >> 2);
658f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 3 : UWB controlled by switch:       %lu\n",
659f1e1ea51SMario Limonciello 		   (hwswitch_state & BIT(3)) >> 3);
660f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 4 : WiGig controlled by switch:     %lu\n",
661f1e1ea51SMario Limonciello 		   (hwswitch_state & BIT(4)) >> 4);
662f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 7 : Wireless switch config locked:  %lu\n",
663f1e1ea51SMario Limonciello 		   (hwswitch_state & BIT(7)) >> 7);
664f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 8 : Wifi locator enabled:           %lu\n",
665f1e1ea51SMario Limonciello 		   (hwswitch_state & BIT(8)) >> 8);
666f1e1ea51SMario Limonciello 	seq_printf(s, "Bit 15: Wifi locator setting locked:    %lu\n",
667f1e1ea51SMario Limonciello 		   (hwswitch_state & BIT(15)) >> 15);
668f1e1ea51SMario Limonciello 
669f1e1ea51SMario Limonciello 	return 0;
670f1e1ea51SMario Limonciello }
671f1e1ea51SMario Limonciello DEFINE_SHOW_ATTRIBUTE(dell_debugfs);
672f1e1ea51SMario Limonciello 
dell_update_rfkill(struct work_struct * ignored)673f1e1ea51SMario Limonciello static void dell_update_rfkill(struct work_struct *ignored)
674f1e1ea51SMario Limonciello {
675f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
676f1e1ea51SMario Limonciello 	int hwswitch = 0;
677f1e1ea51SMario Limonciello 	int status;
678f1e1ea51SMario Limonciello 	int ret;
679f1e1ea51SMario Limonciello 
680f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 0, 0, 0, 0);
681f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
682f1e1ea51SMario Limonciello 	status = buffer.output[1];
683f1e1ea51SMario Limonciello 
684f1e1ea51SMario Limonciello 	if (ret != 0)
685f1e1ea51SMario Limonciello 		return;
686f1e1ea51SMario Limonciello 
687f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 0x2, 0, 0, 0);
688f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
689f1e1ea51SMario Limonciello 
690f1e1ea51SMario Limonciello 	if (ret == 0 && (status & BIT(0)))
691f1e1ea51SMario Limonciello 		hwswitch = buffer.output[1];
692f1e1ea51SMario Limonciello 
693f1e1ea51SMario Limonciello 	if (wifi_rfkill) {
694f1e1ea51SMario Limonciello 		dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
695f1e1ea51SMario Limonciello 		dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
696f1e1ea51SMario Limonciello 	}
697f1e1ea51SMario Limonciello 	if (bluetooth_rfkill) {
698f1e1ea51SMario Limonciello 		dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
699f1e1ea51SMario Limonciello 					    hwswitch);
700f1e1ea51SMario Limonciello 		dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
701f1e1ea51SMario Limonciello 	}
702f1e1ea51SMario Limonciello 	if (wwan_rfkill) {
703f1e1ea51SMario Limonciello 		dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
704f1e1ea51SMario Limonciello 		dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
705f1e1ea51SMario Limonciello 	}
706f1e1ea51SMario Limonciello }
707f1e1ea51SMario Limonciello static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
708f1e1ea51SMario Limonciello 
dell_laptop_i8042_filter(unsigned char data,unsigned char str,struct serio * port)709f1e1ea51SMario Limonciello static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
710f1e1ea51SMario Limonciello 			      struct serio *port)
711f1e1ea51SMario Limonciello {
712f1e1ea51SMario Limonciello 	static bool extended;
713f1e1ea51SMario Limonciello 
714f1e1ea51SMario Limonciello 	if (str & I8042_STR_AUXDATA)
715f1e1ea51SMario Limonciello 		return false;
716f1e1ea51SMario Limonciello 
717f1e1ea51SMario Limonciello 	if (unlikely(data == 0xe0)) {
718f1e1ea51SMario Limonciello 		extended = true;
719f1e1ea51SMario Limonciello 		return false;
720f1e1ea51SMario Limonciello 	} else if (unlikely(extended)) {
721f1e1ea51SMario Limonciello 		switch (data) {
722f1e1ea51SMario Limonciello 		case 0x8:
723f1e1ea51SMario Limonciello 			schedule_delayed_work(&dell_rfkill_work,
724f1e1ea51SMario Limonciello 					      round_jiffies_relative(HZ / 4));
725f1e1ea51SMario Limonciello 			break;
726f1e1ea51SMario Limonciello 		}
727f1e1ea51SMario Limonciello 		extended = false;
728f1e1ea51SMario Limonciello 	}
729f1e1ea51SMario Limonciello 
730f1e1ea51SMario Limonciello 	return false;
731f1e1ea51SMario Limonciello }
732f1e1ea51SMario Limonciello 
733f1e1ea51SMario Limonciello static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
734f1e1ea51SMario Limonciello static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
735f1e1ea51SMario Limonciello 
dell_laptop_rbtn_notifier_call(struct notifier_block * nb,unsigned long action,void * data)736f1e1ea51SMario Limonciello static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
737f1e1ea51SMario Limonciello 					  unsigned long action, void *data)
738f1e1ea51SMario Limonciello {
739f1e1ea51SMario Limonciello 	schedule_delayed_work(&dell_rfkill_work, 0);
740f1e1ea51SMario Limonciello 	return NOTIFY_OK;
741f1e1ea51SMario Limonciello }
742f1e1ea51SMario Limonciello 
743f1e1ea51SMario Limonciello static struct notifier_block dell_laptop_rbtn_notifier = {
744f1e1ea51SMario Limonciello 	.notifier_call = dell_laptop_rbtn_notifier_call,
745f1e1ea51SMario Limonciello };
746f1e1ea51SMario Limonciello 
dell_setup_rfkill(void)747f1e1ea51SMario Limonciello static int __init dell_setup_rfkill(void)
748f1e1ea51SMario Limonciello {
749f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
750f1e1ea51SMario Limonciello 	int status, ret, whitelisted;
751f1e1ea51SMario Limonciello 	const char *product;
752f1e1ea51SMario Limonciello 
753f1e1ea51SMario Limonciello 	/*
754f1e1ea51SMario Limonciello 	 * rfkill support causes trouble on various models, mostly Inspirons.
755f1e1ea51SMario Limonciello 	 * So we whitelist certain series, and don't support rfkill on others.
756f1e1ea51SMario Limonciello 	 */
757f1e1ea51SMario Limonciello 	whitelisted = 0;
758f1e1ea51SMario Limonciello 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
759f1e1ea51SMario Limonciello 	if (product &&  (strncmp(product, "Latitude", 8) == 0 ||
760f1e1ea51SMario Limonciello 			 strncmp(product, "Precision", 9) == 0))
761f1e1ea51SMario Limonciello 		whitelisted = 1;
762f1e1ea51SMario Limonciello 	if (!force_rfkill && !whitelisted)
763f1e1ea51SMario Limonciello 		return 0;
764f1e1ea51SMario Limonciello 
765f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 0, 0, 0, 0);
766f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
767f1e1ea51SMario Limonciello 	status = buffer.output[1];
768f1e1ea51SMario Limonciello 
769f1e1ea51SMario Limonciello 	/* dell wireless info smbios call is not supported */
770f1e1ea51SMario Limonciello 	if (ret != 0)
771f1e1ea51SMario Limonciello 		return 0;
772f1e1ea51SMario Limonciello 
773f1e1ea51SMario Limonciello 	/* rfkill is only tested on laptops with a hwswitch */
774f1e1ea51SMario Limonciello 	if (!(status & BIT(0)) && !force_rfkill)
775f1e1ea51SMario Limonciello 		return 0;
776f1e1ea51SMario Limonciello 
777f1e1ea51SMario Limonciello 	if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
778f1e1ea51SMario Limonciello 		wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
779f1e1ea51SMario Limonciello 					   RFKILL_TYPE_WLAN,
780f1e1ea51SMario Limonciello 					   &dell_rfkill_ops, (void *) 1);
781f1e1ea51SMario Limonciello 		if (!wifi_rfkill) {
782f1e1ea51SMario Limonciello 			ret = -ENOMEM;
783f1e1ea51SMario Limonciello 			goto err_wifi;
784f1e1ea51SMario Limonciello 		}
785f1e1ea51SMario Limonciello 		ret = rfkill_register(wifi_rfkill);
786f1e1ea51SMario Limonciello 		if (ret)
787f1e1ea51SMario Limonciello 			goto err_wifi;
788f1e1ea51SMario Limonciello 	}
789f1e1ea51SMario Limonciello 
790f1e1ea51SMario Limonciello 	if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
791f1e1ea51SMario Limonciello 		bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
792f1e1ea51SMario Limonciello 						&platform_device->dev,
793f1e1ea51SMario Limonciello 						RFKILL_TYPE_BLUETOOTH,
794f1e1ea51SMario Limonciello 						&dell_rfkill_ops, (void *) 2);
795f1e1ea51SMario Limonciello 		if (!bluetooth_rfkill) {
796f1e1ea51SMario Limonciello 			ret = -ENOMEM;
797f1e1ea51SMario Limonciello 			goto err_bluetooth;
798f1e1ea51SMario Limonciello 		}
799f1e1ea51SMario Limonciello 		ret = rfkill_register(bluetooth_rfkill);
800f1e1ea51SMario Limonciello 		if (ret)
801f1e1ea51SMario Limonciello 			goto err_bluetooth;
802f1e1ea51SMario Limonciello 	}
803f1e1ea51SMario Limonciello 
804f1e1ea51SMario Limonciello 	if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
805f1e1ea51SMario Limonciello 		wwan_rfkill = rfkill_alloc("dell-wwan",
806f1e1ea51SMario Limonciello 					   &platform_device->dev,
807f1e1ea51SMario Limonciello 					   RFKILL_TYPE_WWAN,
808f1e1ea51SMario Limonciello 					   &dell_rfkill_ops, (void *) 3);
809f1e1ea51SMario Limonciello 		if (!wwan_rfkill) {
810f1e1ea51SMario Limonciello 			ret = -ENOMEM;
811f1e1ea51SMario Limonciello 			goto err_wwan;
812f1e1ea51SMario Limonciello 		}
813f1e1ea51SMario Limonciello 		ret = rfkill_register(wwan_rfkill);
814f1e1ea51SMario Limonciello 		if (ret)
815f1e1ea51SMario Limonciello 			goto err_wwan;
816f1e1ea51SMario Limonciello 	}
817f1e1ea51SMario Limonciello 
818f1e1ea51SMario Limonciello 	/*
819f1e1ea51SMario Limonciello 	 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
820f1e1ea51SMario Limonciello 	 * which can receive events from HW slider switch.
821f1e1ea51SMario Limonciello 	 *
822f1e1ea51SMario Limonciello 	 * Dell SMBIOS on whitelisted models supports controlling radio devices
823f1e1ea51SMario Limonciello 	 * but does not support receiving HW button switch events. We can use
824f1e1ea51SMario Limonciello 	 * i8042 filter hook function to receive keyboard data and handle
825f1e1ea51SMario Limonciello 	 * keycode for HW button.
826f1e1ea51SMario Limonciello 	 *
827f1e1ea51SMario Limonciello 	 * So if it is possible we will use Dell Airplane Mode Switch ACPI
828f1e1ea51SMario Limonciello 	 * driver for receiving HW events and Dell SMBIOS for setting rfkill
829f1e1ea51SMario Limonciello 	 * states. If ACPI driver or device is not available we will fallback to
830f1e1ea51SMario Limonciello 	 * i8042 filter hook function.
831f1e1ea51SMario Limonciello 	 *
832f1e1ea51SMario Limonciello 	 * To prevent duplicate rfkill devices which control and do same thing,
833f1e1ea51SMario Limonciello 	 * dell-rbtn driver will automatically remove its own rfkill devices
834f1e1ea51SMario Limonciello 	 * once function dell_rbtn_notifier_register() is called.
835f1e1ea51SMario Limonciello 	 */
836f1e1ea51SMario Limonciello 
837f1e1ea51SMario Limonciello 	dell_rbtn_notifier_register_func =
838f1e1ea51SMario Limonciello 		symbol_request(dell_rbtn_notifier_register);
839f1e1ea51SMario Limonciello 	if (dell_rbtn_notifier_register_func) {
840f1e1ea51SMario Limonciello 		dell_rbtn_notifier_unregister_func =
841f1e1ea51SMario Limonciello 			symbol_request(dell_rbtn_notifier_unregister);
842f1e1ea51SMario Limonciello 		if (!dell_rbtn_notifier_unregister_func) {
843f1e1ea51SMario Limonciello 			symbol_put(dell_rbtn_notifier_register);
844f1e1ea51SMario Limonciello 			dell_rbtn_notifier_register_func = NULL;
845f1e1ea51SMario Limonciello 		}
846f1e1ea51SMario Limonciello 	}
847f1e1ea51SMario Limonciello 
848f1e1ea51SMario Limonciello 	if (dell_rbtn_notifier_register_func) {
849f1e1ea51SMario Limonciello 		ret = dell_rbtn_notifier_register_func(
850f1e1ea51SMario Limonciello 			&dell_laptop_rbtn_notifier);
851f1e1ea51SMario Limonciello 		symbol_put(dell_rbtn_notifier_register);
852f1e1ea51SMario Limonciello 		dell_rbtn_notifier_register_func = NULL;
853f1e1ea51SMario Limonciello 		if (ret != 0) {
854f1e1ea51SMario Limonciello 			symbol_put(dell_rbtn_notifier_unregister);
855f1e1ea51SMario Limonciello 			dell_rbtn_notifier_unregister_func = NULL;
856f1e1ea51SMario Limonciello 		}
857f1e1ea51SMario Limonciello 	} else {
858f1e1ea51SMario Limonciello 		pr_info("Symbols from dell-rbtn acpi driver are not available\n");
859f1e1ea51SMario Limonciello 		ret = -ENODEV;
860f1e1ea51SMario Limonciello 	}
861f1e1ea51SMario Limonciello 
862f1e1ea51SMario Limonciello 	if (ret == 0) {
863f1e1ea51SMario Limonciello 		pr_info("Using dell-rbtn acpi driver for receiving events\n");
864f1e1ea51SMario Limonciello 	} else if (ret != -ENODEV) {
865f1e1ea51SMario Limonciello 		pr_warn("Unable to register dell rbtn notifier\n");
866f1e1ea51SMario Limonciello 		goto err_filter;
867f1e1ea51SMario Limonciello 	} else {
868f1e1ea51SMario Limonciello 		ret = i8042_install_filter(dell_laptop_i8042_filter);
869f1e1ea51SMario Limonciello 		if (ret) {
870f1e1ea51SMario Limonciello 			pr_warn("Unable to install key filter\n");
871f1e1ea51SMario Limonciello 			goto err_filter;
872f1e1ea51SMario Limonciello 		}
873f1e1ea51SMario Limonciello 		pr_info("Using i8042 filter function for receiving events\n");
874f1e1ea51SMario Limonciello 	}
875f1e1ea51SMario Limonciello 
876f1e1ea51SMario Limonciello 	return 0;
877f1e1ea51SMario Limonciello err_filter:
878f1e1ea51SMario Limonciello 	if (wwan_rfkill)
879f1e1ea51SMario Limonciello 		rfkill_unregister(wwan_rfkill);
880f1e1ea51SMario Limonciello err_wwan:
881f1e1ea51SMario Limonciello 	rfkill_destroy(wwan_rfkill);
882f1e1ea51SMario Limonciello 	if (bluetooth_rfkill)
883f1e1ea51SMario Limonciello 		rfkill_unregister(bluetooth_rfkill);
884f1e1ea51SMario Limonciello err_bluetooth:
885f1e1ea51SMario Limonciello 	rfkill_destroy(bluetooth_rfkill);
886f1e1ea51SMario Limonciello 	if (wifi_rfkill)
887f1e1ea51SMario Limonciello 		rfkill_unregister(wifi_rfkill);
888f1e1ea51SMario Limonciello err_wifi:
889f1e1ea51SMario Limonciello 	rfkill_destroy(wifi_rfkill);
890f1e1ea51SMario Limonciello 
891f1e1ea51SMario Limonciello 	return ret;
892f1e1ea51SMario Limonciello }
893f1e1ea51SMario Limonciello 
dell_cleanup_rfkill(void)894f1e1ea51SMario Limonciello static void dell_cleanup_rfkill(void)
895f1e1ea51SMario Limonciello {
896f1e1ea51SMario Limonciello 	if (dell_rbtn_notifier_unregister_func) {
897f1e1ea51SMario Limonciello 		dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
898f1e1ea51SMario Limonciello 		symbol_put(dell_rbtn_notifier_unregister);
899f1e1ea51SMario Limonciello 		dell_rbtn_notifier_unregister_func = NULL;
900f1e1ea51SMario Limonciello 	} else {
901f1e1ea51SMario Limonciello 		i8042_remove_filter(dell_laptop_i8042_filter);
902f1e1ea51SMario Limonciello 	}
903f1e1ea51SMario Limonciello 	cancel_delayed_work_sync(&dell_rfkill_work);
904f1e1ea51SMario Limonciello 	if (wifi_rfkill) {
905f1e1ea51SMario Limonciello 		rfkill_unregister(wifi_rfkill);
906f1e1ea51SMario Limonciello 		rfkill_destroy(wifi_rfkill);
907f1e1ea51SMario Limonciello 	}
908f1e1ea51SMario Limonciello 	if (bluetooth_rfkill) {
909f1e1ea51SMario Limonciello 		rfkill_unregister(bluetooth_rfkill);
910f1e1ea51SMario Limonciello 		rfkill_destroy(bluetooth_rfkill);
911f1e1ea51SMario Limonciello 	}
912f1e1ea51SMario Limonciello 	if (wwan_rfkill) {
913f1e1ea51SMario Limonciello 		rfkill_unregister(wwan_rfkill);
914f1e1ea51SMario Limonciello 		rfkill_destroy(wwan_rfkill);
915f1e1ea51SMario Limonciello 	}
916f1e1ea51SMario Limonciello }
917f1e1ea51SMario Limonciello 
dell_send_intensity(struct backlight_device * bd)918f1e1ea51SMario Limonciello static int dell_send_intensity(struct backlight_device *bd)
919f1e1ea51SMario Limonciello {
920f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
921f1e1ea51SMario Limonciello 	struct calling_interface_token *token;
922f1e1ea51SMario Limonciello 	int ret;
923f1e1ea51SMario Limonciello 
924f1e1ea51SMario Limonciello 	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
925f1e1ea51SMario Limonciello 	if (!token)
926f1e1ea51SMario Limonciello 		return -ENODEV;
927f1e1ea51SMario Limonciello 
928f1e1ea51SMario Limonciello 	dell_fill_request(&buffer,
929f1e1ea51SMario Limonciello 			   token->location, bd->props.brightness, 0, 0);
930f1e1ea51SMario Limonciello 	if (power_supply_is_system_supplied() > 0)
931f1e1ea51SMario Limonciello 		ret = dell_send_request(&buffer,
932f1e1ea51SMario Limonciello 					CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
933f1e1ea51SMario Limonciello 	else
934f1e1ea51SMario Limonciello 		ret = dell_send_request(&buffer,
935f1e1ea51SMario Limonciello 					CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
936f1e1ea51SMario Limonciello 
937f1e1ea51SMario Limonciello 	return ret;
938f1e1ea51SMario Limonciello }
939f1e1ea51SMario Limonciello 
dell_get_intensity(struct backlight_device * bd)940f1e1ea51SMario Limonciello static int dell_get_intensity(struct backlight_device *bd)
941f1e1ea51SMario Limonciello {
942f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
943f1e1ea51SMario Limonciello 	struct calling_interface_token *token;
944f1e1ea51SMario Limonciello 	int ret;
945f1e1ea51SMario Limonciello 
946f1e1ea51SMario Limonciello 	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
947f1e1ea51SMario Limonciello 	if (!token)
948f1e1ea51SMario Limonciello 		return -ENODEV;
949f1e1ea51SMario Limonciello 
950f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, token->location, 0, 0, 0);
951f1e1ea51SMario Limonciello 	if (power_supply_is_system_supplied() > 0)
952f1e1ea51SMario Limonciello 		ret = dell_send_request(&buffer,
953f1e1ea51SMario Limonciello 					CLASS_TOKEN_READ, SELECT_TOKEN_AC);
954f1e1ea51SMario Limonciello 	else
955f1e1ea51SMario Limonciello 		ret = dell_send_request(&buffer,
956f1e1ea51SMario Limonciello 					CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
957f1e1ea51SMario Limonciello 
958f1e1ea51SMario Limonciello 	if (ret == 0)
959f1e1ea51SMario Limonciello 		ret = buffer.output[1];
960f1e1ea51SMario Limonciello 
961f1e1ea51SMario Limonciello 	return ret;
962f1e1ea51SMario Limonciello }
963f1e1ea51SMario Limonciello 
964f1e1ea51SMario Limonciello static const struct backlight_ops dell_ops = {
965f1e1ea51SMario Limonciello 	.get_brightness = dell_get_intensity,
966f1e1ea51SMario Limonciello 	.update_status  = dell_send_intensity,
967f1e1ea51SMario Limonciello };
968f1e1ea51SMario Limonciello 
touchpad_led_on(void)969f1e1ea51SMario Limonciello static void touchpad_led_on(void)
970f1e1ea51SMario Limonciello {
971f1e1ea51SMario Limonciello 	int command = 0x97;
972f1e1ea51SMario Limonciello 	char data = 1;
973f1e1ea51SMario Limonciello 	i8042_command(&data, command | 1 << 12);
974f1e1ea51SMario Limonciello }
975f1e1ea51SMario Limonciello 
touchpad_led_off(void)976f1e1ea51SMario Limonciello static void touchpad_led_off(void)
977f1e1ea51SMario Limonciello {
978f1e1ea51SMario Limonciello 	int command = 0x97;
979f1e1ea51SMario Limonciello 	char data = 2;
980f1e1ea51SMario Limonciello 	i8042_command(&data, command | 1 << 12);
981f1e1ea51SMario Limonciello }
982f1e1ea51SMario Limonciello 
touchpad_led_set(struct led_classdev * led_cdev,enum led_brightness value)983f1e1ea51SMario Limonciello static void touchpad_led_set(struct led_classdev *led_cdev,
984f1e1ea51SMario Limonciello 	enum led_brightness value)
985f1e1ea51SMario Limonciello {
986f1e1ea51SMario Limonciello 	if (value > 0)
987f1e1ea51SMario Limonciello 		touchpad_led_on();
988f1e1ea51SMario Limonciello 	else
989f1e1ea51SMario Limonciello 		touchpad_led_off();
990f1e1ea51SMario Limonciello }
991f1e1ea51SMario Limonciello 
992f1e1ea51SMario Limonciello static struct led_classdev touchpad_led = {
993f1e1ea51SMario Limonciello 	.name = "dell-laptop::touchpad",
994f1e1ea51SMario Limonciello 	.brightness_set = touchpad_led_set,
995f1e1ea51SMario Limonciello 	.flags = LED_CORE_SUSPENDRESUME,
996f1e1ea51SMario Limonciello };
997f1e1ea51SMario Limonciello 
touchpad_led_init(struct device * dev)998f1e1ea51SMario Limonciello static int __init touchpad_led_init(struct device *dev)
999f1e1ea51SMario Limonciello {
1000f1e1ea51SMario Limonciello 	return led_classdev_register(dev, &touchpad_led);
1001f1e1ea51SMario Limonciello }
1002f1e1ea51SMario Limonciello 
touchpad_led_exit(void)1003f1e1ea51SMario Limonciello static void touchpad_led_exit(void)
1004f1e1ea51SMario Limonciello {
1005f1e1ea51SMario Limonciello 	led_classdev_unregister(&touchpad_led);
1006f1e1ea51SMario Limonciello }
1007f1e1ea51SMario Limonciello 
1008f1e1ea51SMario Limonciello /*
1009f1e1ea51SMario Limonciello  * Derived from information in smbios-keyboard-ctl:
1010f1e1ea51SMario Limonciello  *
1011f1e1ea51SMario Limonciello  * cbClass 4
1012f1e1ea51SMario Limonciello  * cbSelect 11
1013f1e1ea51SMario Limonciello  * Keyboard illumination
1014f1e1ea51SMario Limonciello  * cbArg1 determines the function to be performed
1015f1e1ea51SMario Limonciello  *
1016f1e1ea51SMario Limonciello  * cbArg1 0x0 = Get Feature Information
1017f1e1ea51SMario Limonciello  *  cbRES1         Standard return codes (0, -1, -2)
1018f1e1ea51SMario Limonciello  *  cbRES2, word0  Bitmap of user-selectable modes
1019f1e1ea51SMario Limonciello  *     bit 0     Always off (All systems)
1020f1e1ea51SMario Limonciello  *     bit 1     Always on (Travis ATG, Siberia)
1021f1e1ea51SMario Limonciello  *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1022f1e1ea51SMario Limonciello  *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1023f1e1ea51SMario Limonciello  *     bit 4     Auto: Input-activity-based On; input-activity based Off
1024f1e1ea51SMario Limonciello  *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1025f1e1ea51SMario Limonciello  *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1026f1e1ea51SMario Limonciello  *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1027f1e1ea51SMario Limonciello  *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1028f1e1ea51SMario Limonciello  *     bits 9-15 Reserved for future use
1029f1e1ea51SMario Limonciello  *  cbRES2, byte2  Reserved for future use
1030f1e1ea51SMario Limonciello  *  cbRES2, byte3  Keyboard illumination type
1031f1e1ea51SMario Limonciello  *     0         Reserved
1032f1e1ea51SMario Limonciello  *     1         Tasklight
1033f1e1ea51SMario Limonciello  *     2         Backlight
1034f1e1ea51SMario Limonciello  *     3-255     Reserved for future use
1035f1e1ea51SMario Limonciello  *  cbRES3, byte0  Supported auto keyboard illumination trigger bitmap.
1036f1e1ea51SMario Limonciello  *     bit 0     Any keystroke
1037f1e1ea51SMario Limonciello  *     bit 1     Touchpad activity
1038f1e1ea51SMario Limonciello  *     bit 2     Pointing stick
1039f1e1ea51SMario Limonciello  *     bit 3     Any mouse
1040f1e1ea51SMario Limonciello  *     bits 4-7  Reserved for future use
1041f1e1ea51SMario Limonciello  *  cbRES3, byte1  Supported timeout unit bitmap
1042f1e1ea51SMario Limonciello  *     bit 0     Seconds
1043f1e1ea51SMario Limonciello  *     bit 1     Minutes
1044f1e1ea51SMario Limonciello  *     bit 2     Hours
1045f1e1ea51SMario Limonciello  *     bit 3     Days
1046f1e1ea51SMario Limonciello  *     bits 4-7  Reserved for future use
1047f1e1ea51SMario Limonciello  *  cbRES3, byte2  Number of keyboard light brightness levels
1048f1e1ea51SMario Limonciello  *  cbRES4, byte0  Maximum acceptable seconds value (0 if seconds not supported).
1049f1e1ea51SMario Limonciello  *  cbRES4, byte1  Maximum acceptable minutes value (0 if minutes not supported).
1050f1e1ea51SMario Limonciello  *  cbRES4, byte2  Maximum acceptable hours value (0 if hours not supported).
1051f1e1ea51SMario Limonciello  *  cbRES4, byte3  Maximum acceptable days value (0 if days not supported)
1052f1e1ea51SMario Limonciello  *
1053f1e1ea51SMario Limonciello  * cbArg1 0x1 = Get Current State
1054f1e1ea51SMario Limonciello  *  cbRES1         Standard return codes (0, -1, -2)
1055f1e1ea51SMario Limonciello  *  cbRES2, word0  Bitmap of current mode state
1056f1e1ea51SMario Limonciello  *     bit 0     Always off (All systems)
1057f1e1ea51SMario Limonciello  *     bit 1     Always on (Travis ATG, Siberia)
1058f1e1ea51SMario Limonciello  *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1059f1e1ea51SMario Limonciello  *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1060f1e1ea51SMario Limonciello  *     bit 4     Auto: Input-activity-based On; input-activity based Off
1061f1e1ea51SMario Limonciello  *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1062f1e1ea51SMario Limonciello  *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1063f1e1ea51SMario Limonciello  *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1064f1e1ea51SMario Limonciello  *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1065f1e1ea51SMario Limonciello  *     bits 9-15 Reserved for future use
1066f1e1ea51SMario Limonciello  *     Note: Only One bit can be set
1067f1e1ea51SMario Limonciello  *  cbRES2, byte2  Currently active auto keyboard illumination triggers.
1068f1e1ea51SMario Limonciello  *     bit 0     Any keystroke
1069f1e1ea51SMario Limonciello  *     bit 1     Touchpad activity
1070f1e1ea51SMario Limonciello  *     bit 2     Pointing stick
1071f1e1ea51SMario Limonciello  *     bit 3     Any mouse
1072f1e1ea51SMario Limonciello  *     bits 4-7  Reserved for future use
1073f1e1ea51SMario Limonciello  *  cbRES2, byte3  Current Timeout on battery
1074f1e1ea51SMario Limonciello  *     bits 7:6  Timeout units indicator:
1075f1e1ea51SMario Limonciello  *     00b       Seconds
1076f1e1ea51SMario Limonciello  *     01b       Minutes
1077f1e1ea51SMario Limonciello  *     10b       Hours
1078f1e1ea51SMario Limonciello  *     11b       Days
1079f1e1ea51SMario Limonciello  *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1080f1e1ea51SMario Limonciello  *     NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1081f1e1ea51SMario Limonciello  *     are set upon return from the [Get feature information] call.
1082f1e1ea51SMario Limonciello  *  cbRES3, byte0  Current setting of ALS value that turns the light on or off.
1083f1e1ea51SMario Limonciello  *  cbRES3, byte1  Current ALS reading
1084f1e1ea51SMario Limonciello  *  cbRES3, byte2  Current keyboard light level.
1085f1e1ea51SMario Limonciello  *  cbRES3, byte3  Current timeout on AC Power
1086f1e1ea51SMario Limonciello  *     bits 7:6  Timeout units indicator:
1087f1e1ea51SMario Limonciello  *     00b       Seconds
1088f1e1ea51SMario Limonciello  *     01b       Minutes
1089f1e1ea51SMario Limonciello  *     10b       Hours
1090f1e1ea51SMario Limonciello  *     11b       Days
1091f1e1ea51SMario Limonciello  *     Bits 5:0  Timeout value (0-63) in sec/min/hr/day
1092f1e1ea51SMario Limonciello  *     NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1093f1e1ea51SMario Limonciello  *     are set upon return from the upon return from the [Get Feature information] call.
1094f1e1ea51SMario Limonciello  *
1095f1e1ea51SMario Limonciello  * cbArg1 0x2 = Set New State
1096f1e1ea51SMario Limonciello  *  cbRES1         Standard return codes (0, -1, -2)
1097f1e1ea51SMario Limonciello  *  cbArg2, word0  Bitmap of current mode state
1098f1e1ea51SMario Limonciello  *     bit 0     Always off (All systems)
1099f1e1ea51SMario Limonciello  *     bit 1     Always on (Travis ATG, Siberia)
1100f1e1ea51SMario Limonciello  *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1101f1e1ea51SMario Limonciello  *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1102f1e1ea51SMario Limonciello  *     bit 4     Auto: Input-activity-based On; input-activity based Off
1103f1e1ea51SMario Limonciello  *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1104f1e1ea51SMario Limonciello  *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1105f1e1ea51SMario Limonciello  *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1106f1e1ea51SMario Limonciello  *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1107f1e1ea51SMario Limonciello  *     bits 9-15 Reserved for future use
1108f1e1ea51SMario Limonciello  *     Note: Only One bit can be set
1109f1e1ea51SMario Limonciello  *  cbArg2, byte2  Desired auto keyboard illumination triggers. Must remain inactive to allow
1110f1e1ea51SMario Limonciello  *                 keyboard to turn off automatically.
1111f1e1ea51SMario Limonciello  *     bit 0     Any keystroke
1112f1e1ea51SMario Limonciello  *     bit 1     Touchpad activity
1113f1e1ea51SMario Limonciello  *     bit 2     Pointing stick
1114f1e1ea51SMario Limonciello  *     bit 3     Any mouse
1115f1e1ea51SMario Limonciello  *     bits 4-7  Reserved for future use
1116f1e1ea51SMario Limonciello  *  cbArg2, byte3  Desired Timeout on battery
1117f1e1ea51SMario Limonciello  *     bits 7:6  Timeout units indicator:
1118f1e1ea51SMario Limonciello  *     00b       Seconds
1119f1e1ea51SMario Limonciello  *     01b       Minutes
1120f1e1ea51SMario Limonciello  *     10b       Hours
1121f1e1ea51SMario Limonciello  *     11b       Days
1122f1e1ea51SMario Limonciello  *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1123f1e1ea51SMario Limonciello  *  cbArg3, byte0  Desired setting of ALS value that turns the light on or off.
1124f1e1ea51SMario Limonciello  *  cbArg3, byte2  Desired keyboard light level.
1125f1e1ea51SMario Limonciello  *  cbArg3, byte3  Desired Timeout on AC power
1126f1e1ea51SMario Limonciello  *     bits 7:6  Timeout units indicator:
1127f1e1ea51SMario Limonciello  *     00b       Seconds
1128f1e1ea51SMario Limonciello  *     01b       Minutes
1129f1e1ea51SMario Limonciello  *     10b       Hours
1130f1e1ea51SMario Limonciello  *     11b       Days
1131f1e1ea51SMario Limonciello  *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1132f1e1ea51SMario Limonciello  */
1133f1e1ea51SMario Limonciello 
1134f1e1ea51SMario Limonciello 
1135f1e1ea51SMario Limonciello enum kbd_timeout_unit {
1136f1e1ea51SMario Limonciello 	KBD_TIMEOUT_SECONDS = 0,
1137f1e1ea51SMario Limonciello 	KBD_TIMEOUT_MINUTES,
1138f1e1ea51SMario Limonciello 	KBD_TIMEOUT_HOURS,
1139f1e1ea51SMario Limonciello 	KBD_TIMEOUT_DAYS,
1140f1e1ea51SMario Limonciello };
1141f1e1ea51SMario Limonciello 
1142f1e1ea51SMario Limonciello enum kbd_mode_bit {
1143f1e1ea51SMario Limonciello 	KBD_MODE_BIT_OFF = 0,
1144f1e1ea51SMario Limonciello 	KBD_MODE_BIT_ON,
1145f1e1ea51SMario Limonciello 	KBD_MODE_BIT_ALS,
1146f1e1ea51SMario Limonciello 	KBD_MODE_BIT_TRIGGER_ALS,
1147f1e1ea51SMario Limonciello 	KBD_MODE_BIT_TRIGGER,
1148f1e1ea51SMario Limonciello 	KBD_MODE_BIT_TRIGGER_25,
1149f1e1ea51SMario Limonciello 	KBD_MODE_BIT_TRIGGER_50,
1150f1e1ea51SMario Limonciello 	KBD_MODE_BIT_TRIGGER_75,
1151f1e1ea51SMario Limonciello 	KBD_MODE_BIT_TRIGGER_100,
1152f1e1ea51SMario Limonciello };
1153f1e1ea51SMario Limonciello 
1154f1e1ea51SMario Limonciello #define kbd_is_als_mode_bit(bit) \
1155f1e1ea51SMario Limonciello 	((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1156f1e1ea51SMario Limonciello #define kbd_is_trigger_mode_bit(bit) \
1157f1e1ea51SMario Limonciello 	((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1158f1e1ea51SMario Limonciello #define kbd_is_level_mode_bit(bit) \
1159f1e1ea51SMario Limonciello 	((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1160f1e1ea51SMario Limonciello 
1161f1e1ea51SMario Limonciello struct kbd_info {
1162f1e1ea51SMario Limonciello 	u16 modes;
1163f1e1ea51SMario Limonciello 	u8 type;
1164f1e1ea51SMario Limonciello 	u8 triggers;
1165f1e1ea51SMario Limonciello 	u8 levels;
1166f1e1ea51SMario Limonciello 	u8 seconds;
1167f1e1ea51SMario Limonciello 	u8 minutes;
1168f1e1ea51SMario Limonciello 	u8 hours;
1169f1e1ea51SMario Limonciello 	u8 days;
1170f1e1ea51SMario Limonciello };
1171f1e1ea51SMario Limonciello 
1172f1e1ea51SMario Limonciello struct kbd_state {
1173f1e1ea51SMario Limonciello 	u8 mode_bit;
1174f1e1ea51SMario Limonciello 	u8 triggers;
1175f1e1ea51SMario Limonciello 	u8 timeout_value;
1176f1e1ea51SMario Limonciello 	u8 timeout_unit;
1177f1e1ea51SMario Limonciello 	u8 timeout_value_ac;
1178f1e1ea51SMario Limonciello 	u8 timeout_unit_ac;
1179f1e1ea51SMario Limonciello 	u8 als_setting;
1180f1e1ea51SMario Limonciello 	u8 als_value;
1181f1e1ea51SMario Limonciello 	u8 level;
1182f1e1ea51SMario Limonciello };
1183f1e1ea51SMario Limonciello 
1184f1e1ea51SMario Limonciello static const int kbd_tokens[] = {
1185f1e1ea51SMario Limonciello 	KBD_LED_OFF_TOKEN,
1186f1e1ea51SMario Limonciello 	KBD_LED_AUTO_25_TOKEN,
1187f1e1ea51SMario Limonciello 	KBD_LED_AUTO_50_TOKEN,
1188f1e1ea51SMario Limonciello 	KBD_LED_AUTO_75_TOKEN,
1189f1e1ea51SMario Limonciello 	KBD_LED_AUTO_100_TOKEN,
1190f1e1ea51SMario Limonciello 	KBD_LED_ON_TOKEN,
1191f1e1ea51SMario Limonciello };
1192f1e1ea51SMario Limonciello 
1193f1e1ea51SMario Limonciello static u16 kbd_token_bits;
1194f1e1ea51SMario Limonciello 
1195f1e1ea51SMario Limonciello static struct kbd_info kbd_info;
1196f1e1ea51SMario Limonciello static bool kbd_als_supported;
1197f1e1ea51SMario Limonciello static bool kbd_triggers_supported;
1198f1e1ea51SMario Limonciello static bool kbd_timeout_ac_supported;
1199f1e1ea51SMario Limonciello 
1200f1e1ea51SMario Limonciello static u8 kbd_mode_levels[16];
1201f1e1ea51SMario Limonciello static int kbd_mode_levels_count;
1202f1e1ea51SMario Limonciello 
1203f1e1ea51SMario Limonciello static u8 kbd_previous_level;
1204f1e1ea51SMario Limonciello static u8 kbd_previous_mode_bit;
1205f1e1ea51SMario Limonciello 
1206f1e1ea51SMario Limonciello static bool kbd_led_present;
1207f1e1ea51SMario Limonciello static DEFINE_MUTEX(kbd_led_mutex);
1208f1e1ea51SMario Limonciello static enum led_brightness kbd_led_level;
1209f1e1ea51SMario Limonciello 
1210f1e1ea51SMario Limonciello /*
1211f1e1ea51SMario Limonciello  * NOTE: there are three ways to set the keyboard backlight level.
1212f1e1ea51SMario Limonciello  * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1213f1e1ea51SMario Limonciello  * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1214f1e1ea51SMario Limonciello  * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1215f1e1ea51SMario Limonciello  *
1216f1e1ea51SMario Limonciello  * There are laptops which support only one of these methods. If we want to
1217f1e1ea51SMario Limonciello  * support as many machines as possible we need to implement all three methods.
1218f1e1ea51SMario Limonciello  * The first two methods use the kbd_state structure. The third uses SMBIOS
1219f1e1ea51SMario Limonciello  * tokens. If kbd_info.levels == 0, the machine does not support setting the
1220f1e1ea51SMario Limonciello  * keyboard backlight level via kbd_state.level.
1221f1e1ea51SMario Limonciello  */
1222f1e1ea51SMario Limonciello 
kbd_get_info(struct kbd_info * info)1223f1e1ea51SMario Limonciello static int kbd_get_info(struct kbd_info *info)
1224f1e1ea51SMario Limonciello {
1225f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
1226f1e1ea51SMario Limonciello 	u8 units;
1227f1e1ea51SMario Limonciello 	int ret;
1228f1e1ea51SMario Limonciello 
1229f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 0, 0, 0, 0);
1230f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer,
1231f1e1ea51SMario Limonciello 				CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1232f1e1ea51SMario Limonciello 	if (ret)
1233f1e1ea51SMario Limonciello 		return ret;
1234f1e1ea51SMario Limonciello 
1235f1e1ea51SMario Limonciello 	info->modes = buffer.output[1] & 0xFFFF;
1236f1e1ea51SMario Limonciello 	info->type = (buffer.output[1] >> 24) & 0xFF;
1237f1e1ea51SMario Limonciello 	info->triggers = buffer.output[2] & 0xFF;
1238f1e1ea51SMario Limonciello 	units = (buffer.output[2] >> 8) & 0xFF;
1239f1e1ea51SMario Limonciello 	info->levels = (buffer.output[2] >> 16) & 0xFF;
1240f1e1ea51SMario Limonciello 
1241f1e1ea51SMario Limonciello 	if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1242f1e1ea51SMario Limonciello 		info->levels--;
1243f1e1ea51SMario Limonciello 
1244f1e1ea51SMario Limonciello 	if (units & BIT(0))
1245f1e1ea51SMario Limonciello 		info->seconds = (buffer.output[3] >> 0) & 0xFF;
1246f1e1ea51SMario Limonciello 	if (units & BIT(1))
1247f1e1ea51SMario Limonciello 		info->minutes = (buffer.output[3] >> 8) & 0xFF;
1248f1e1ea51SMario Limonciello 	if (units & BIT(2))
1249f1e1ea51SMario Limonciello 		info->hours = (buffer.output[3] >> 16) & 0xFF;
1250f1e1ea51SMario Limonciello 	if (units & BIT(3))
1251f1e1ea51SMario Limonciello 		info->days = (buffer.output[3] >> 24) & 0xFF;
1252f1e1ea51SMario Limonciello 
1253f1e1ea51SMario Limonciello 	return ret;
1254f1e1ea51SMario Limonciello }
1255f1e1ea51SMario Limonciello 
kbd_get_max_level(void)1256f1e1ea51SMario Limonciello static unsigned int kbd_get_max_level(void)
1257f1e1ea51SMario Limonciello {
1258f1e1ea51SMario Limonciello 	if (kbd_info.levels != 0)
1259f1e1ea51SMario Limonciello 		return kbd_info.levels;
1260f1e1ea51SMario Limonciello 	if (kbd_mode_levels_count > 0)
1261f1e1ea51SMario Limonciello 		return kbd_mode_levels_count - 1;
1262f1e1ea51SMario Limonciello 	return 0;
1263f1e1ea51SMario Limonciello }
1264f1e1ea51SMario Limonciello 
kbd_get_level(struct kbd_state * state)1265f1e1ea51SMario Limonciello static int kbd_get_level(struct kbd_state *state)
1266f1e1ea51SMario Limonciello {
1267f1e1ea51SMario Limonciello 	int i;
1268f1e1ea51SMario Limonciello 
1269f1e1ea51SMario Limonciello 	if (kbd_info.levels != 0)
1270f1e1ea51SMario Limonciello 		return state->level;
1271f1e1ea51SMario Limonciello 
1272f1e1ea51SMario Limonciello 	if (kbd_mode_levels_count > 0) {
1273f1e1ea51SMario Limonciello 		for (i = 0; i < kbd_mode_levels_count; ++i)
1274f1e1ea51SMario Limonciello 			if (kbd_mode_levels[i] == state->mode_bit)
1275f1e1ea51SMario Limonciello 				return i;
1276f1e1ea51SMario Limonciello 		return 0;
1277f1e1ea51SMario Limonciello 	}
1278f1e1ea51SMario Limonciello 
1279f1e1ea51SMario Limonciello 	return -EINVAL;
1280f1e1ea51SMario Limonciello }
1281f1e1ea51SMario Limonciello 
kbd_set_level(struct kbd_state * state,u8 level)1282f1e1ea51SMario Limonciello static int kbd_set_level(struct kbd_state *state, u8 level)
1283f1e1ea51SMario Limonciello {
1284f1e1ea51SMario Limonciello 	if (kbd_info.levels != 0) {
1285f1e1ea51SMario Limonciello 		if (level != 0)
1286f1e1ea51SMario Limonciello 			kbd_previous_level = level;
1287f1e1ea51SMario Limonciello 		if (state->level == level)
1288f1e1ea51SMario Limonciello 			return 0;
1289f1e1ea51SMario Limonciello 		state->level = level;
1290f1e1ea51SMario Limonciello 		if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1291f1e1ea51SMario Limonciello 			state->mode_bit = kbd_previous_mode_bit;
1292f1e1ea51SMario Limonciello 		else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1293f1e1ea51SMario Limonciello 			kbd_previous_mode_bit = state->mode_bit;
1294f1e1ea51SMario Limonciello 			state->mode_bit = KBD_MODE_BIT_OFF;
1295f1e1ea51SMario Limonciello 		}
1296f1e1ea51SMario Limonciello 		return 0;
1297f1e1ea51SMario Limonciello 	}
1298f1e1ea51SMario Limonciello 
1299f1e1ea51SMario Limonciello 	if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1300f1e1ea51SMario Limonciello 		if (level != 0)
1301f1e1ea51SMario Limonciello 			kbd_previous_level = level;
1302f1e1ea51SMario Limonciello 		state->mode_bit = kbd_mode_levels[level];
1303f1e1ea51SMario Limonciello 		return 0;
1304f1e1ea51SMario Limonciello 	}
1305f1e1ea51SMario Limonciello 
1306f1e1ea51SMario Limonciello 	return -EINVAL;
1307f1e1ea51SMario Limonciello }
1308f1e1ea51SMario Limonciello 
kbd_get_state(struct kbd_state * state)1309f1e1ea51SMario Limonciello static int kbd_get_state(struct kbd_state *state)
1310f1e1ea51SMario Limonciello {
1311f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
1312f1e1ea51SMario Limonciello 	int ret;
1313f1e1ea51SMario Limonciello 
1314f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 0x1, 0, 0, 0);
1315f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer,
1316f1e1ea51SMario Limonciello 				CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1317f1e1ea51SMario Limonciello 	if (ret)
1318f1e1ea51SMario Limonciello 		return ret;
1319f1e1ea51SMario Limonciello 
1320f1e1ea51SMario Limonciello 	state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
1321f1e1ea51SMario Limonciello 	if (state->mode_bit != 0)
1322f1e1ea51SMario Limonciello 		state->mode_bit--;
1323f1e1ea51SMario Limonciello 
1324f1e1ea51SMario Limonciello 	state->triggers = (buffer.output[1] >> 16) & 0xFF;
1325f1e1ea51SMario Limonciello 	state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
1326f1e1ea51SMario Limonciello 	state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
1327f1e1ea51SMario Limonciello 	state->als_setting = buffer.output[2] & 0xFF;
1328f1e1ea51SMario Limonciello 	state->als_value = (buffer.output[2] >> 8) & 0xFF;
1329f1e1ea51SMario Limonciello 	state->level = (buffer.output[2] >> 16) & 0xFF;
1330f1e1ea51SMario Limonciello 	state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
1331f1e1ea51SMario Limonciello 	state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;
1332f1e1ea51SMario Limonciello 
1333f1e1ea51SMario Limonciello 	return ret;
1334f1e1ea51SMario Limonciello }
1335f1e1ea51SMario Limonciello 
kbd_set_state(struct kbd_state * state)1336f1e1ea51SMario Limonciello static int kbd_set_state(struct kbd_state *state)
1337f1e1ea51SMario Limonciello {
1338f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
1339f1e1ea51SMario Limonciello 	int ret;
1340f1e1ea51SMario Limonciello 	u32 input1;
1341f1e1ea51SMario Limonciello 	u32 input2;
1342f1e1ea51SMario Limonciello 
1343f1e1ea51SMario Limonciello 	input1 = BIT(state->mode_bit) & 0xFFFF;
1344f1e1ea51SMario Limonciello 	input1 |= (state->triggers & 0xFF) << 16;
1345f1e1ea51SMario Limonciello 	input1 |= (state->timeout_value & 0x3F) << 24;
1346f1e1ea51SMario Limonciello 	input1 |= (state->timeout_unit & 0x3) << 30;
1347f1e1ea51SMario Limonciello 	input2 = state->als_setting & 0xFF;
1348f1e1ea51SMario Limonciello 	input2 |= (state->level & 0xFF) << 16;
1349f1e1ea51SMario Limonciello 	input2 |= (state->timeout_value_ac & 0x3F) << 24;
1350f1e1ea51SMario Limonciello 	input2 |= (state->timeout_unit_ac & 0x3) << 30;
1351f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, 0x2, input1, input2, 0);
1352f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer,
1353f1e1ea51SMario Limonciello 				CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1354f1e1ea51SMario Limonciello 
1355f1e1ea51SMario Limonciello 	return ret;
1356f1e1ea51SMario Limonciello }
1357f1e1ea51SMario Limonciello 
kbd_set_state_safe(struct kbd_state * state,struct kbd_state * old)1358f1e1ea51SMario Limonciello static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1359f1e1ea51SMario Limonciello {
1360f1e1ea51SMario Limonciello 	int ret;
1361f1e1ea51SMario Limonciello 
1362f1e1ea51SMario Limonciello 	ret = kbd_set_state(state);
1363f1e1ea51SMario Limonciello 	if (ret == 0)
1364f1e1ea51SMario Limonciello 		return 0;
1365f1e1ea51SMario Limonciello 
1366f1e1ea51SMario Limonciello 	/*
1367f1e1ea51SMario Limonciello 	 * When setting the new state fails,try to restore the previous one.
1368f1e1ea51SMario Limonciello 	 * This is needed on some machines where BIOS sets a default state when
1369f1e1ea51SMario Limonciello 	 * setting a new state fails. This default state could be all off.
1370f1e1ea51SMario Limonciello 	 */
1371f1e1ea51SMario Limonciello 
1372f1e1ea51SMario Limonciello 	if (kbd_set_state(old))
1373f1e1ea51SMario Limonciello 		pr_err("Setting old previous keyboard state failed\n");
1374f1e1ea51SMario Limonciello 
1375f1e1ea51SMario Limonciello 	return ret;
1376f1e1ea51SMario Limonciello }
1377f1e1ea51SMario Limonciello 
kbd_set_token_bit(u8 bit)1378f1e1ea51SMario Limonciello static int kbd_set_token_bit(u8 bit)
1379f1e1ea51SMario Limonciello {
1380f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
1381f1e1ea51SMario Limonciello 	struct calling_interface_token *token;
1382f1e1ea51SMario Limonciello 	int ret;
1383f1e1ea51SMario Limonciello 
1384f1e1ea51SMario Limonciello 	if (bit >= ARRAY_SIZE(kbd_tokens))
1385f1e1ea51SMario Limonciello 		return -EINVAL;
1386f1e1ea51SMario Limonciello 
1387f1e1ea51SMario Limonciello 	token = dell_smbios_find_token(kbd_tokens[bit]);
1388f1e1ea51SMario Limonciello 	if (!token)
1389f1e1ea51SMario Limonciello 		return -EINVAL;
1390f1e1ea51SMario Limonciello 
1391f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, token->location, token->value, 0, 0);
1392f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1393f1e1ea51SMario Limonciello 
1394f1e1ea51SMario Limonciello 	return ret;
1395f1e1ea51SMario Limonciello }
1396f1e1ea51SMario Limonciello 
kbd_get_token_bit(u8 bit)1397f1e1ea51SMario Limonciello static int kbd_get_token_bit(u8 bit)
1398f1e1ea51SMario Limonciello {
1399f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
1400f1e1ea51SMario Limonciello 	struct calling_interface_token *token;
1401f1e1ea51SMario Limonciello 	int ret;
1402f1e1ea51SMario Limonciello 	int val;
1403f1e1ea51SMario Limonciello 
1404f1e1ea51SMario Limonciello 	if (bit >= ARRAY_SIZE(kbd_tokens))
1405f1e1ea51SMario Limonciello 		return -EINVAL;
1406f1e1ea51SMario Limonciello 
1407f1e1ea51SMario Limonciello 	token = dell_smbios_find_token(kbd_tokens[bit]);
1408f1e1ea51SMario Limonciello 	if (!token)
1409f1e1ea51SMario Limonciello 		return -EINVAL;
1410f1e1ea51SMario Limonciello 
1411f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, token->location, 0, 0, 0);
1412f1e1ea51SMario Limonciello 	ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1413f1e1ea51SMario Limonciello 	val = buffer.output[1];
1414f1e1ea51SMario Limonciello 
1415f1e1ea51SMario Limonciello 	if (ret)
1416f1e1ea51SMario Limonciello 		return ret;
1417f1e1ea51SMario Limonciello 
1418f1e1ea51SMario Limonciello 	return (val == token->value);
1419f1e1ea51SMario Limonciello }
1420f1e1ea51SMario Limonciello 
kbd_get_first_active_token_bit(void)1421f1e1ea51SMario Limonciello static int kbd_get_first_active_token_bit(void)
1422f1e1ea51SMario Limonciello {
1423f1e1ea51SMario Limonciello 	int i;
1424f1e1ea51SMario Limonciello 	int ret;
1425f1e1ea51SMario Limonciello 
1426f1e1ea51SMario Limonciello 	for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1427f1e1ea51SMario Limonciello 		ret = kbd_get_token_bit(i);
1428f1e1ea51SMario Limonciello 		if (ret == 1)
1429f1e1ea51SMario Limonciello 			return i;
1430f1e1ea51SMario Limonciello 	}
1431f1e1ea51SMario Limonciello 
1432f1e1ea51SMario Limonciello 	return ret;
1433f1e1ea51SMario Limonciello }
1434f1e1ea51SMario Limonciello 
kbd_get_valid_token_counts(void)1435f1e1ea51SMario Limonciello static int kbd_get_valid_token_counts(void)
1436f1e1ea51SMario Limonciello {
1437f1e1ea51SMario Limonciello 	return hweight16(kbd_token_bits);
1438f1e1ea51SMario Limonciello }
1439f1e1ea51SMario Limonciello 
kbd_init_info(void)1440f1e1ea51SMario Limonciello static inline int kbd_init_info(void)
1441f1e1ea51SMario Limonciello {
1442f1e1ea51SMario Limonciello 	struct kbd_state state;
1443f1e1ea51SMario Limonciello 	int ret;
1444f1e1ea51SMario Limonciello 	int i;
1445f1e1ea51SMario Limonciello 
1446f1e1ea51SMario Limonciello 	ret = kbd_get_info(&kbd_info);
1447f1e1ea51SMario Limonciello 	if (ret)
1448f1e1ea51SMario Limonciello 		return ret;
1449f1e1ea51SMario Limonciello 
1450f1e1ea51SMario Limonciello 	/* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1451f1e1ea51SMario Limonciello 	 *       timeout value which is shared for both battery and AC power
1452f1e1ea51SMario Limonciello 	 *       settings. So do not try to set AC values on old models.
1453f1e1ea51SMario Limonciello 	 */
1454f1e1ea51SMario Limonciello 	if ((quirks && quirks->kbd_missing_ac_tag) ||
1455f1e1ea51SMario Limonciello 	    dell_smbios_find_token(KBD_LED_AC_TOKEN))
1456f1e1ea51SMario Limonciello 		kbd_timeout_ac_supported = true;
1457f1e1ea51SMario Limonciello 
1458f1e1ea51SMario Limonciello 	kbd_get_state(&state);
1459f1e1ea51SMario Limonciello 
1460f1e1ea51SMario Limonciello 	/* NOTE: timeout value is stored in 6 bits so max value is 63 */
1461f1e1ea51SMario Limonciello 	if (kbd_info.seconds > 63)
1462f1e1ea51SMario Limonciello 		kbd_info.seconds = 63;
1463f1e1ea51SMario Limonciello 	if (kbd_info.minutes > 63)
1464f1e1ea51SMario Limonciello 		kbd_info.minutes = 63;
1465f1e1ea51SMario Limonciello 	if (kbd_info.hours > 63)
1466f1e1ea51SMario Limonciello 		kbd_info.hours = 63;
1467f1e1ea51SMario Limonciello 	if (kbd_info.days > 63)
1468f1e1ea51SMario Limonciello 		kbd_info.days = 63;
1469f1e1ea51SMario Limonciello 
1470f1e1ea51SMario Limonciello 	/* NOTE: On tested machines ON mode did not work and caused
1471f1e1ea51SMario Limonciello 	 *       problems (turned backlight off) so do not use it
1472f1e1ea51SMario Limonciello 	 */
1473f1e1ea51SMario Limonciello 	kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1474f1e1ea51SMario Limonciello 
1475f1e1ea51SMario Limonciello 	kbd_previous_level = kbd_get_level(&state);
1476f1e1ea51SMario Limonciello 	kbd_previous_mode_bit = state.mode_bit;
1477f1e1ea51SMario Limonciello 
1478f1e1ea51SMario Limonciello 	if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1479f1e1ea51SMario Limonciello 		kbd_previous_level = 1;
1480f1e1ea51SMario Limonciello 
1481f1e1ea51SMario Limonciello 	if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1482f1e1ea51SMario Limonciello 		kbd_previous_mode_bit =
1483f1e1ea51SMario Limonciello 			ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1484f1e1ea51SMario Limonciello 		if (kbd_previous_mode_bit != 0)
1485f1e1ea51SMario Limonciello 			kbd_previous_mode_bit--;
1486f1e1ea51SMario Limonciello 	}
1487f1e1ea51SMario Limonciello 
1488f1e1ea51SMario Limonciello 	if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1489f1e1ea51SMario Limonciello 			      BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1490f1e1ea51SMario Limonciello 		kbd_als_supported = true;
1491f1e1ea51SMario Limonciello 
1492f1e1ea51SMario Limonciello 	if (kbd_info.modes & (
1493f1e1ea51SMario Limonciello 	    BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1494f1e1ea51SMario Limonciello 	    BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1495f1e1ea51SMario Limonciello 	    BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1496f1e1ea51SMario Limonciello 	   ))
1497f1e1ea51SMario Limonciello 		kbd_triggers_supported = true;
1498f1e1ea51SMario Limonciello 
1499f1e1ea51SMario Limonciello 	/* kbd_mode_levels[0] is reserved, see below */
1500f1e1ea51SMario Limonciello 	for (i = 0; i < 16; ++i)
1501f1e1ea51SMario Limonciello 		if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1502f1e1ea51SMario Limonciello 			kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1503f1e1ea51SMario Limonciello 
1504f1e1ea51SMario Limonciello 	/*
1505f1e1ea51SMario Limonciello 	 * Find the first supported mode and assign to kbd_mode_levels[0].
1506f1e1ea51SMario Limonciello 	 * This should be 0 (off), but we cannot depend on the BIOS to
1507f1e1ea51SMario Limonciello 	 * support 0.
1508f1e1ea51SMario Limonciello 	 */
1509f1e1ea51SMario Limonciello 	if (kbd_mode_levels_count > 0) {
1510f1e1ea51SMario Limonciello 		for (i = 0; i < 16; ++i) {
1511f1e1ea51SMario Limonciello 			if (BIT(i) & kbd_info.modes) {
1512f1e1ea51SMario Limonciello 				kbd_mode_levels[0] = i;
1513f1e1ea51SMario Limonciello 				break;
1514f1e1ea51SMario Limonciello 			}
1515f1e1ea51SMario Limonciello 		}
1516f1e1ea51SMario Limonciello 		kbd_mode_levels_count++;
1517f1e1ea51SMario Limonciello 	}
1518f1e1ea51SMario Limonciello 
1519f1e1ea51SMario Limonciello 	return 0;
1520f1e1ea51SMario Limonciello 
1521f1e1ea51SMario Limonciello }
1522f1e1ea51SMario Limonciello 
kbd_init_tokens(void)1523f1e1ea51SMario Limonciello static inline void kbd_init_tokens(void)
1524f1e1ea51SMario Limonciello {
1525f1e1ea51SMario Limonciello 	int i;
1526f1e1ea51SMario Limonciello 
1527f1e1ea51SMario Limonciello 	for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1528f1e1ea51SMario Limonciello 		if (dell_smbios_find_token(kbd_tokens[i]))
1529f1e1ea51SMario Limonciello 			kbd_token_bits |= BIT(i);
1530f1e1ea51SMario Limonciello }
1531f1e1ea51SMario Limonciello 
kbd_init(void)1532f1e1ea51SMario Limonciello static void kbd_init(void)
1533f1e1ea51SMario Limonciello {
1534f1e1ea51SMario Limonciello 	int ret;
1535f1e1ea51SMario Limonciello 
1536f1e1ea51SMario Limonciello 	if (quirks && quirks->kbd_led_not_present)
1537f1e1ea51SMario Limonciello 		return;
1538f1e1ea51SMario Limonciello 
1539f1e1ea51SMario Limonciello 	ret = kbd_init_info();
1540f1e1ea51SMario Limonciello 	kbd_init_tokens();
1541f1e1ea51SMario Limonciello 
1542f1e1ea51SMario Limonciello 	/*
1543f1e1ea51SMario Limonciello 	 * Only supports keyboard backlight when it has at least two modes.
1544f1e1ea51SMario Limonciello 	 */
1545f1e1ea51SMario Limonciello 	if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1546f1e1ea51SMario Limonciello 	    || kbd_get_valid_token_counts() >= 2)
1547f1e1ea51SMario Limonciello 		kbd_led_present = true;
1548f1e1ea51SMario Limonciello }
1549f1e1ea51SMario Limonciello 
kbd_led_timeout_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1550f1e1ea51SMario Limonciello static ssize_t kbd_led_timeout_store(struct device *dev,
1551f1e1ea51SMario Limonciello 				     struct device_attribute *attr,
1552f1e1ea51SMario Limonciello 				     const char *buf, size_t count)
1553f1e1ea51SMario Limonciello {
1554f1e1ea51SMario Limonciello 	struct kbd_state new_state;
1555f1e1ea51SMario Limonciello 	struct kbd_state state;
1556f1e1ea51SMario Limonciello 	bool convert;
1557f1e1ea51SMario Limonciello 	int value;
1558f1e1ea51SMario Limonciello 	int ret;
1559f1e1ea51SMario Limonciello 	char ch;
1560f1e1ea51SMario Limonciello 	u8 unit;
1561f1e1ea51SMario Limonciello 	int i;
1562f1e1ea51SMario Limonciello 
1563f1e1ea51SMario Limonciello 	ret = sscanf(buf, "%d %c", &value, &ch);
1564f1e1ea51SMario Limonciello 	if (ret < 1)
1565f1e1ea51SMario Limonciello 		return -EINVAL;
1566f1e1ea51SMario Limonciello 	else if (ret == 1)
1567f1e1ea51SMario Limonciello 		ch = 's';
1568f1e1ea51SMario Limonciello 
1569f1e1ea51SMario Limonciello 	if (value < 0)
1570f1e1ea51SMario Limonciello 		return -EINVAL;
1571f1e1ea51SMario Limonciello 
1572f1e1ea51SMario Limonciello 	convert = false;
1573f1e1ea51SMario Limonciello 
1574f1e1ea51SMario Limonciello 	switch (ch) {
1575f1e1ea51SMario Limonciello 	case 's':
1576f1e1ea51SMario Limonciello 		if (value > kbd_info.seconds)
1577f1e1ea51SMario Limonciello 			convert = true;
1578f1e1ea51SMario Limonciello 		unit = KBD_TIMEOUT_SECONDS;
1579f1e1ea51SMario Limonciello 		break;
1580f1e1ea51SMario Limonciello 	case 'm':
1581f1e1ea51SMario Limonciello 		if (value > kbd_info.minutes)
1582f1e1ea51SMario Limonciello 			convert = true;
1583f1e1ea51SMario Limonciello 		unit = KBD_TIMEOUT_MINUTES;
1584f1e1ea51SMario Limonciello 		break;
1585f1e1ea51SMario Limonciello 	case 'h':
1586f1e1ea51SMario Limonciello 		if (value > kbd_info.hours)
1587f1e1ea51SMario Limonciello 			convert = true;
1588f1e1ea51SMario Limonciello 		unit = KBD_TIMEOUT_HOURS;
1589f1e1ea51SMario Limonciello 		break;
1590f1e1ea51SMario Limonciello 	case 'd':
1591f1e1ea51SMario Limonciello 		if (value > kbd_info.days)
1592f1e1ea51SMario Limonciello 			convert = true;
1593f1e1ea51SMario Limonciello 		unit = KBD_TIMEOUT_DAYS;
1594f1e1ea51SMario Limonciello 		break;
1595f1e1ea51SMario Limonciello 	default:
1596f1e1ea51SMario Limonciello 		return -EINVAL;
1597f1e1ea51SMario Limonciello 	}
1598f1e1ea51SMario Limonciello 
1599f1e1ea51SMario Limonciello 	if (quirks && quirks->needs_kbd_timeouts)
1600f1e1ea51SMario Limonciello 		convert = true;
1601f1e1ea51SMario Limonciello 
1602f1e1ea51SMario Limonciello 	if (convert) {
1603f1e1ea51SMario Limonciello 		/* Convert value from current units to seconds */
1604f1e1ea51SMario Limonciello 		switch (unit) {
1605f1e1ea51SMario Limonciello 		case KBD_TIMEOUT_DAYS:
1606f1e1ea51SMario Limonciello 			value *= 24;
1607f1e1ea51SMario Limonciello 			fallthrough;
1608f1e1ea51SMario Limonciello 		case KBD_TIMEOUT_HOURS:
1609f1e1ea51SMario Limonciello 			value *= 60;
1610f1e1ea51SMario Limonciello 			fallthrough;
1611f1e1ea51SMario Limonciello 		case KBD_TIMEOUT_MINUTES:
1612f1e1ea51SMario Limonciello 			value *= 60;
1613f1e1ea51SMario Limonciello 			unit = KBD_TIMEOUT_SECONDS;
1614f1e1ea51SMario Limonciello 		}
1615f1e1ea51SMario Limonciello 
1616f1e1ea51SMario Limonciello 		if (quirks && quirks->needs_kbd_timeouts) {
1617f1e1ea51SMario Limonciello 			for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1618f1e1ea51SMario Limonciello 				if (value <= quirks->kbd_timeouts[i]) {
1619f1e1ea51SMario Limonciello 					value = quirks->kbd_timeouts[i];
1620f1e1ea51SMario Limonciello 					break;
1621f1e1ea51SMario Limonciello 				}
1622f1e1ea51SMario Limonciello 			}
1623f1e1ea51SMario Limonciello 		}
1624f1e1ea51SMario Limonciello 
1625f1e1ea51SMario Limonciello 		if (value <= kbd_info.seconds && kbd_info.seconds) {
1626f1e1ea51SMario Limonciello 			unit = KBD_TIMEOUT_SECONDS;
1627f1e1ea51SMario Limonciello 		} else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1628f1e1ea51SMario Limonciello 			value /= 60;
1629f1e1ea51SMario Limonciello 			unit = KBD_TIMEOUT_MINUTES;
1630f1e1ea51SMario Limonciello 		} else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1631f1e1ea51SMario Limonciello 			value /= (60 * 60);
1632f1e1ea51SMario Limonciello 			unit = KBD_TIMEOUT_HOURS;
1633f1e1ea51SMario Limonciello 		} else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1634f1e1ea51SMario Limonciello 			value /= (60 * 60 * 24);
1635f1e1ea51SMario Limonciello 			unit = KBD_TIMEOUT_DAYS;
1636f1e1ea51SMario Limonciello 		} else {
1637f1e1ea51SMario Limonciello 			return -EINVAL;
1638f1e1ea51SMario Limonciello 		}
1639f1e1ea51SMario Limonciello 	}
1640f1e1ea51SMario Limonciello 
1641f1e1ea51SMario Limonciello 	mutex_lock(&kbd_led_mutex);
1642f1e1ea51SMario Limonciello 
1643f1e1ea51SMario Limonciello 	ret = kbd_get_state(&state);
1644f1e1ea51SMario Limonciello 	if (ret)
1645f1e1ea51SMario Limonciello 		goto out;
1646f1e1ea51SMario Limonciello 
1647f1e1ea51SMario Limonciello 	new_state = state;
1648f1e1ea51SMario Limonciello 
1649f1e1ea51SMario Limonciello 	if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1650f1e1ea51SMario Limonciello 		new_state.timeout_value_ac = value;
1651f1e1ea51SMario Limonciello 		new_state.timeout_unit_ac = unit;
1652f1e1ea51SMario Limonciello 	} else {
1653f1e1ea51SMario Limonciello 		new_state.timeout_value = value;
1654f1e1ea51SMario Limonciello 		new_state.timeout_unit = unit;
1655f1e1ea51SMario Limonciello 	}
1656f1e1ea51SMario Limonciello 
1657f1e1ea51SMario Limonciello 	ret = kbd_set_state_safe(&new_state, &state);
1658f1e1ea51SMario Limonciello 	if (ret)
1659f1e1ea51SMario Limonciello 		goto out;
1660f1e1ea51SMario Limonciello 
1661f1e1ea51SMario Limonciello 	ret = count;
1662f1e1ea51SMario Limonciello out:
1663f1e1ea51SMario Limonciello 	mutex_unlock(&kbd_led_mutex);
1664f1e1ea51SMario Limonciello 	return ret;
1665f1e1ea51SMario Limonciello }
1666f1e1ea51SMario Limonciello 
kbd_led_timeout_show(struct device * dev,struct device_attribute * attr,char * buf)1667f1e1ea51SMario Limonciello static ssize_t kbd_led_timeout_show(struct device *dev,
1668f1e1ea51SMario Limonciello 				    struct device_attribute *attr, char *buf)
1669f1e1ea51SMario Limonciello {
1670f1e1ea51SMario Limonciello 	struct kbd_state state;
1671f1e1ea51SMario Limonciello 	int value;
1672f1e1ea51SMario Limonciello 	int ret;
1673f1e1ea51SMario Limonciello 	int len;
1674f1e1ea51SMario Limonciello 	u8 unit;
1675f1e1ea51SMario Limonciello 
1676f1e1ea51SMario Limonciello 	ret = kbd_get_state(&state);
1677f1e1ea51SMario Limonciello 	if (ret)
1678f1e1ea51SMario Limonciello 		return ret;
1679f1e1ea51SMario Limonciello 
1680f1e1ea51SMario Limonciello 	if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1681f1e1ea51SMario Limonciello 		value = state.timeout_value_ac;
1682f1e1ea51SMario Limonciello 		unit = state.timeout_unit_ac;
1683f1e1ea51SMario Limonciello 	} else {
1684f1e1ea51SMario Limonciello 		value = state.timeout_value;
1685f1e1ea51SMario Limonciello 		unit = state.timeout_unit;
1686f1e1ea51SMario Limonciello 	}
1687f1e1ea51SMario Limonciello 
1688f1e1ea51SMario Limonciello 	len = sprintf(buf, "%d", value);
1689f1e1ea51SMario Limonciello 
1690f1e1ea51SMario Limonciello 	switch (unit) {
1691f1e1ea51SMario Limonciello 	case KBD_TIMEOUT_SECONDS:
1692f1e1ea51SMario Limonciello 		return len + sprintf(buf+len, "s\n");
1693f1e1ea51SMario Limonciello 	case KBD_TIMEOUT_MINUTES:
1694f1e1ea51SMario Limonciello 		return len + sprintf(buf+len, "m\n");
1695f1e1ea51SMario Limonciello 	case KBD_TIMEOUT_HOURS:
1696f1e1ea51SMario Limonciello 		return len + sprintf(buf+len, "h\n");
1697f1e1ea51SMario Limonciello 	case KBD_TIMEOUT_DAYS:
1698f1e1ea51SMario Limonciello 		return len + sprintf(buf+len, "d\n");
1699f1e1ea51SMario Limonciello 	default:
1700f1e1ea51SMario Limonciello 		return -EINVAL;
1701f1e1ea51SMario Limonciello 	}
1702f1e1ea51SMario Limonciello 
1703f1e1ea51SMario Limonciello 	return len;
1704f1e1ea51SMario Limonciello }
1705f1e1ea51SMario Limonciello 
1706f1e1ea51SMario Limonciello static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1707f1e1ea51SMario Limonciello 		   kbd_led_timeout_show, kbd_led_timeout_store);
1708f1e1ea51SMario Limonciello 
1709f1e1ea51SMario Limonciello static const char * const kbd_led_triggers[] = {
1710f1e1ea51SMario Limonciello 	"keyboard",
1711f1e1ea51SMario Limonciello 	"touchpad",
1712f1e1ea51SMario Limonciello 	/*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1713f1e1ea51SMario Limonciello 	"mouse",
1714f1e1ea51SMario Limonciello };
1715f1e1ea51SMario Limonciello 
kbd_led_triggers_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1716f1e1ea51SMario Limonciello static ssize_t kbd_led_triggers_store(struct device *dev,
1717f1e1ea51SMario Limonciello 				      struct device_attribute *attr,
1718f1e1ea51SMario Limonciello 				      const char *buf, size_t count)
1719f1e1ea51SMario Limonciello {
1720f1e1ea51SMario Limonciello 	struct kbd_state new_state;
1721f1e1ea51SMario Limonciello 	struct kbd_state state;
1722f1e1ea51SMario Limonciello 	bool triggers_enabled = false;
1723f1e1ea51SMario Limonciello 	int trigger_bit = -1;
1724f1e1ea51SMario Limonciello 	char trigger[21];
1725f1e1ea51SMario Limonciello 	int i, ret;
1726f1e1ea51SMario Limonciello 
1727f1e1ea51SMario Limonciello 	ret = sscanf(buf, "%20s", trigger);
1728f1e1ea51SMario Limonciello 	if (ret != 1)
1729f1e1ea51SMario Limonciello 		return -EINVAL;
1730f1e1ea51SMario Limonciello 
1731f1e1ea51SMario Limonciello 	if (trigger[0] != '+' && trigger[0] != '-')
1732f1e1ea51SMario Limonciello 		return -EINVAL;
1733f1e1ea51SMario Limonciello 
1734f1e1ea51SMario Limonciello 	mutex_lock(&kbd_led_mutex);
1735f1e1ea51SMario Limonciello 
1736f1e1ea51SMario Limonciello 	ret = kbd_get_state(&state);
1737f1e1ea51SMario Limonciello 	if (ret)
1738f1e1ea51SMario Limonciello 		goto out;
1739f1e1ea51SMario Limonciello 
1740f1e1ea51SMario Limonciello 	if (kbd_triggers_supported)
1741f1e1ea51SMario Limonciello 		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1742f1e1ea51SMario Limonciello 
1743f1e1ea51SMario Limonciello 	if (kbd_triggers_supported) {
1744f1e1ea51SMario Limonciello 		for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1745f1e1ea51SMario Limonciello 			if (!(kbd_info.triggers & BIT(i)))
1746f1e1ea51SMario Limonciello 				continue;
1747f1e1ea51SMario Limonciello 			if (!kbd_led_triggers[i])
1748f1e1ea51SMario Limonciello 				continue;
1749f1e1ea51SMario Limonciello 			if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1750f1e1ea51SMario Limonciello 				continue;
1751f1e1ea51SMario Limonciello 			if (trigger[0] == '+' &&
1752f1e1ea51SMario Limonciello 			    triggers_enabled && (state.triggers & BIT(i))) {
1753f1e1ea51SMario Limonciello 				ret = count;
1754f1e1ea51SMario Limonciello 				goto out;
1755f1e1ea51SMario Limonciello 			}
1756f1e1ea51SMario Limonciello 			if (trigger[0] == '-' &&
1757f1e1ea51SMario Limonciello 			    (!triggers_enabled || !(state.triggers & BIT(i)))) {
1758f1e1ea51SMario Limonciello 				ret = count;
1759f1e1ea51SMario Limonciello 				goto out;
1760f1e1ea51SMario Limonciello 			}
1761f1e1ea51SMario Limonciello 			trigger_bit = i;
1762f1e1ea51SMario Limonciello 			break;
1763f1e1ea51SMario Limonciello 		}
1764f1e1ea51SMario Limonciello 	}
1765f1e1ea51SMario Limonciello 
1766f1e1ea51SMario Limonciello 	if (trigger_bit == -1) {
1767f1e1ea51SMario Limonciello 		ret = -EINVAL;
1768f1e1ea51SMario Limonciello 		goto out;
1769f1e1ea51SMario Limonciello 	}
1770f1e1ea51SMario Limonciello 
1771f1e1ea51SMario Limonciello 	new_state = state;
1772f1e1ea51SMario Limonciello 	if (trigger[0] == '+')
1773f1e1ea51SMario Limonciello 		new_state.triggers |= BIT(trigger_bit);
1774f1e1ea51SMario Limonciello 	else {
1775f1e1ea51SMario Limonciello 		new_state.triggers &= ~BIT(trigger_bit);
1776f1e1ea51SMario Limonciello 		/*
1777f1e1ea51SMario Limonciello 		 * NOTE: trackstick bit (2) must be disabled when
1778f1e1ea51SMario Limonciello 		 *       disabling touchpad bit (1), otherwise touchpad
1779f1e1ea51SMario Limonciello 		 *       bit (1) will not be disabled
1780f1e1ea51SMario Limonciello 		 */
1781f1e1ea51SMario Limonciello 		if (trigger_bit == 1)
1782f1e1ea51SMario Limonciello 			new_state.triggers &= ~BIT(2);
1783f1e1ea51SMario Limonciello 	}
1784f1e1ea51SMario Limonciello 	if ((kbd_info.triggers & new_state.triggers) !=
1785f1e1ea51SMario Limonciello 	    new_state.triggers) {
1786f1e1ea51SMario Limonciello 		ret = -EINVAL;
1787f1e1ea51SMario Limonciello 		goto out;
1788f1e1ea51SMario Limonciello 	}
1789f1e1ea51SMario Limonciello 	if (new_state.triggers && !triggers_enabled) {
1790f1e1ea51SMario Limonciello 		new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1791f1e1ea51SMario Limonciello 		kbd_set_level(&new_state, kbd_previous_level);
1792f1e1ea51SMario Limonciello 	} else if (new_state.triggers == 0) {
1793f1e1ea51SMario Limonciello 		kbd_set_level(&new_state, 0);
1794f1e1ea51SMario Limonciello 	}
1795f1e1ea51SMario Limonciello 	if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1796f1e1ea51SMario Limonciello 		ret = -EINVAL;
1797f1e1ea51SMario Limonciello 		goto out;
1798f1e1ea51SMario Limonciello 	}
1799f1e1ea51SMario Limonciello 	ret = kbd_set_state_safe(&new_state, &state);
1800f1e1ea51SMario Limonciello 	if (ret)
1801f1e1ea51SMario Limonciello 		goto out;
1802f1e1ea51SMario Limonciello 	if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1803f1e1ea51SMario Limonciello 		kbd_previous_mode_bit = new_state.mode_bit;
1804f1e1ea51SMario Limonciello 	ret = count;
1805f1e1ea51SMario Limonciello out:
1806f1e1ea51SMario Limonciello 	mutex_unlock(&kbd_led_mutex);
1807f1e1ea51SMario Limonciello 	return ret;
1808f1e1ea51SMario Limonciello }
1809f1e1ea51SMario Limonciello 
kbd_led_triggers_show(struct device * dev,struct device_attribute * attr,char * buf)1810f1e1ea51SMario Limonciello static ssize_t kbd_led_triggers_show(struct device *dev,
1811f1e1ea51SMario Limonciello 				     struct device_attribute *attr, char *buf)
1812f1e1ea51SMario Limonciello {
1813f1e1ea51SMario Limonciello 	struct kbd_state state;
1814f1e1ea51SMario Limonciello 	bool triggers_enabled;
1815f1e1ea51SMario Limonciello 	int level, i, ret;
1816f1e1ea51SMario Limonciello 	int len = 0;
1817f1e1ea51SMario Limonciello 
1818f1e1ea51SMario Limonciello 	ret = kbd_get_state(&state);
1819f1e1ea51SMario Limonciello 	if (ret)
1820f1e1ea51SMario Limonciello 		return ret;
1821f1e1ea51SMario Limonciello 
1822f1e1ea51SMario Limonciello 	len = 0;
1823f1e1ea51SMario Limonciello 
1824f1e1ea51SMario Limonciello 	if (kbd_triggers_supported) {
1825f1e1ea51SMario Limonciello 		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1826f1e1ea51SMario Limonciello 		level = kbd_get_level(&state);
1827f1e1ea51SMario Limonciello 		for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1828f1e1ea51SMario Limonciello 			if (!(kbd_info.triggers & BIT(i)))
1829f1e1ea51SMario Limonciello 				continue;
1830f1e1ea51SMario Limonciello 			if (!kbd_led_triggers[i])
1831f1e1ea51SMario Limonciello 				continue;
1832f1e1ea51SMario Limonciello 			if ((triggers_enabled || level <= 0) &&
1833f1e1ea51SMario Limonciello 			    (state.triggers & BIT(i)))
1834f1e1ea51SMario Limonciello 				buf[len++] = '+';
1835f1e1ea51SMario Limonciello 			else
1836f1e1ea51SMario Limonciello 				buf[len++] = '-';
1837f1e1ea51SMario Limonciello 			len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1838f1e1ea51SMario Limonciello 		}
1839f1e1ea51SMario Limonciello 	}
1840f1e1ea51SMario Limonciello 
1841f1e1ea51SMario Limonciello 	if (len)
1842f1e1ea51SMario Limonciello 		buf[len - 1] = '\n';
1843f1e1ea51SMario Limonciello 
1844f1e1ea51SMario Limonciello 	return len;
1845f1e1ea51SMario Limonciello }
1846f1e1ea51SMario Limonciello 
1847f1e1ea51SMario Limonciello static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1848f1e1ea51SMario Limonciello 		   kbd_led_triggers_show, kbd_led_triggers_store);
1849f1e1ea51SMario Limonciello 
kbd_led_als_enabled_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1850f1e1ea51SMario Limonciello static ssize_t kbd_led_als_enabled_store(struct device *dev,
1851f1e1ea51SMario Limonciello 					 struct device_attribute *attr,
1852f1e1ea51SMario Limonciello 					 const char *buf, size_t count)
1853f1e1ea51SMario Limonciello {
1854f1e1ea51SMario Limonciello 	struct kbd_state new_state;
1855f1e1ea51SMario Limonciello 	struct kbd_state state;
1856f1e1ea51SMario Limonciello 	bool triggers_enabled = false;
1857f1e1ea51SMario Limonciello 	int enable;
1858f1e1ea51SMario Limonciello 	int ret;
1859f1e1ea51SMario Limonciello 
1860f1e1ea51SMario Limonciello 	ret = kstrtoint(buf, 0, &enable);
1861f1e1ea51SMario Limonciello 	if (ret)
1862f1e1ea51SMario Limonciello 		return ret;
1863f1e1ea51SMario Limonciello 
1864f1e1ea51SMario Limonciello 	mutex_lock(&kbd_led_mutex);
1865f1e1ea51SMario Limonciello 
1866f1e1ea51SMario Limonciello 	ret = kbd_get_state(&state);
1867f1e1ea51SMario Limonciello 	if (ret)
1868f1e1ea51SMario Limonciello 		goto out;
1869f1e1ea51SMario Limonciello 
1870f1e1ea51SMario Limonciello 	if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1871f1e1ea51SMario Limonciello 		ret = count;
1872f1e1ea51SMario Limonciello 		goto out;
1873f1e1ea51SMario Limonciello 	}
1874f1e1ea51SMario Limonciello 
1875f1e1ea51SMario Limonciello 	new_state = state;
1876f1e1ea51SMario Limonciello 
1877f1e1ea51SMario Limonciello 	if (kbd_triggers_supported)
1878f1e1ea51SMario Limonciello 		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1879f1e1ea51SMario Limonciello 
1880f1e1ea51SMario Limonciello 	if (enable) {
1881f1e1ea51SMario Limonciello 		if (triggers_enabled)
1882f1e1ea51SMario Limonciello 			new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1883f1e1ea51SMario Limonciello 		else
1884f1e1ea51SMario Limonciello 			new_state.mode_bit = KBD_MODE_BIT_ALS;
1885f1e1ea51SMario Limonciello 	} else {
1886f1e1ea51SMario Limonciello 		if (triggers_enabled) {
1887f1e1ea51SMario Limonciello 			new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1888f1e1ea51SMario Limonciello 			kbd_set_level(&new_state, kbd_previous_level);
1889f1e1ea51SMario Limonciello 		} else {
1890f1e1ea51SMario Limonciello 			new_state.mode_bit = KBD_MODE_BIT_ON;
1891f1e1ea51SMario Limonciello 		}
1892f1e1ea51SMario Limonciello 	}
1893f1e1ea51SMario Limonciello 	if (!(kbd_info.modes & BIT(new_state.mode_bit)))  {
1894f1e1ea51SMario Limonciello 		ret = -EINVAL;
1895f1e1ea51SMario Limonciello 		goto out;
1896f1e1ea51SMario Limonciello 	}
1897f1e1ea51SMario Limonciello 
1898f1e1ea51SMario Limonciello 	ret = kbd_set_state_safe(&new_state, &state);
1899f1e1ea51SMario Limonciello 	if (ret)
1900f1e1ea51SMario Limonciello 		goto out;
1901f1e1ea51SMario Limonciello 	kbd_previous_mode_bit = new_state.mode_bit;
1902f1e1ea51SMario Limonciello 
1903f1e1ea51SMario Limonciello 	ret = count;
1904f1e1ea51SMario Limonciello out:
1905f1e1ea51SMario Limonciello 	mutex_unlock(&kbd_led_mutex);
1906f1e1ea51SMario Limonciello 	return ret;
1907f1e1ea51SMario Limonciello }
1908f1e1ea51SMario Limonciello 
kbd_led_als_enabled_show(struct device * dev,struct device_attribute * attr,char * buf)1909f1e1ea51SMario Limonciello static ssize_t kbd_led_als_enabled_show(struct device *dev,
1910f1e1ea51SMario Limonciello 					struct device_attribute *attr,
1911f1e1ea51SMario Limonciello 					char *buf)
1912f1e1ea51SMario Limonciello {
1913f1e1ea51SMario Limonciello 	struct kbd_state state;
1914f1e1ea51SMario Limonciello 	bool enabled = false;
1915f1e1ea51SMario Limonciello 	int ret;
1916f1e1ea51SMario Limonciello 
1917f1e1ea51SMario Limonciello 	ret = kbd_get_state(&state);
1918f1e1ea51SMario Limonciello 	if (ret)
1919f1e1ea51SMario Limonciello 		return ret;
1920f1e1ea51SMario Limonciello 	enabled = kbd_is_als_mode_bit(state.mode_bit);
1921f1e1ea51SMario Limonciello 
1922f1e1ea51SMario Limonciello 	return sprintf(buf, "%d\n", enabled ? 1 : 0);
1923f1e1ea51SMario Limonciello }
1924f1e1ea51SMario Limonciello 
1925f1e1ea51SMario Limonciello static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1926f1e1ea51SMario Limonciello 		   kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1927f1e1ea51SMario Limonciello 
kbd_led_als_setting_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1928f1e1ea51SMario Limonciello static ssize_t kbd_led_als_setting_store(struct device *dev,
1929f1e1ea51SMario Limonciello 					 struct device_attribute *attr,
1930f1e1ea51SMario Limonciello 					 const char *buf, size_t count)
1931f1e1ea51SMario Limonciello {
1932f1e1ea51SMario Limonciello 	struct kbd_state state;
1933f1e1ea51SMario Limonciello 	struct kbd_state new_state;
1934f1e1ea51SMario Limonciello 	u8 setting;
1935f1e1ea51SMario Limonciello 	int ret;
1936f1e1ea51SMario Limonciello 
1937f1e1ea51SMario Limonciello 	ret = kstrtou8(buf, 10, &setting);
1938f1e1ea51SMario Limonciello 	if (ret)
1939f1e1ea51SMario Limonciello 		return ret;
1940f1e1ea51SMario Limonciello 
1941f1e1ea51SMario Limonciello 	mutex_lock(&kbd_led_mutex);
1942f1e1ea51SMario Limonciello 
1943f1e1ea51SMario Limonciello 	ret = kbd_get_state(&state);
1944f1e1ea51SMario Limonciello 	if (ret)
1945f1e1ea51SMario Limonciello 		goto out;
1946f1e1ea51SMario Limonciello 
1947f1e1ea51SMario Limonciello 	new_state = state;
1948f1e1ea51SMario Limonciello 	new_state.als_setting = setting;
1949f1e1ea51SMario Limonciello 
1950f1e1ea51SMario Limonciello 	ret = kbd_set_state_safe(&new_state, &state);
1951f1e1ea51SMario Limonciello 	if (ret)
1952f1e1ea51SMario Limonciello 		goto out;
1953f1e1ea51SMario Limonciello 
1954f1e1ea51SMario Limonciello 	ret = count;
1955f1e1ea51SMario Limonciello out:
1956f1e1ea51SMario Limonciello 	mutex_unlock(&kbd_led_mutex);
1957f1e1ea51SMario Limonciello 	return ret;
1958f1e1ea51SMario Limonciello }
1959f1e1ea51SMario Limonciello 
kbd_led_als_setting_show(struct device * dev,struct device_attribute * attr,char * buf)1960f1e1ea51SMario Limonciello static ssize_t kbd_led_als_setting_show(struct device *dev,
1961f1e1ea51SMario Limonciello 					struct device_attribute *attr,
1962f1e1ea51SMario Limonciello 					char *buf)
1963f1e1ea51SMario Limonciello {
1964f1e1ea51SMario Limonciello 	struct kbd_state state;
1965f1e1ea51SMario Limonciello 	int ret;
1966f1e1ea51SMario Limonciello 
1967f1e1ea51SMario Limonciello 	ret = kbd_get_state(&state);
1968f1e1ea51SMario Limonciello 	if (ret)
1969f1e1ea51SMario Limonciello 		return ret;
1970f1e1ea51SMario Limonciello 
1971f1e1ea51SMario Limonciello 	return sprintf(buf, "%d\n", state.als_setting);
1972f1e1ea51SMario Limonciello }
1973f1e1ea51SMario Limonciello 
1974f1e1ea51SMario Limonciello static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1975f1e1ea51SMario Limonciello 		   kbd_led_als_setting_show, kbd_led_als_setting_store);
1976f1e1ea51SMario Limonciello 
1977f1e1ea51SMario Limonciello static struct attribute *kbd_led_attrs[] = {
1978f1e1ea51SMario Limonciello 	&dev_attr_stop_timeout.attr,
1979f1e1ea51SMario Limonciello 	&dev_attr_start_triggers.attr,
1980f1e1ea51SMario Limonciello 	NULL,
1981f1e1ea51SMario Limonciello };
1982f1e1ea51SMario Limonciello 
1983f1e1ea51SMario Limonciello static const struct attribute_group kbd_led_group = {
1984f1e1ea51SMario Limonciello 	.attrs = kbd_led_attrs,
1985f1e1ea51SMario Limonciello };
1986f1e1ea51SMario Limonciello 
1987f1e1ea51SMario Limonciello static struct attribute *kbd_led_als_attrs[] = {
1988f1e1ea51SMario Limonciello 	&dev_attr_als_enabled.attr,
1989f1e1ea51SMario Limonciello 	&dev_attr_als_setting.attr,
1990f1e1ea51SMario Limonciello 	NULL,
1991f1e1ea51SMario Limonciello };
1992f1e1ea51SMario Limonciello 
1993f1e1ea51SMario Limonciello static const struct attribute_group kbd_led_als_group = {
1994f1e1ea51SMario Limonciello 	.attrs = kbd_led_als_attrs,
1995f1e1ea51SMario Limonciello };
1996f1e1ea51SMario Limonciello 
1997f1e1ea51SMario Limonciello static const struct attribute_group *kbd_led_groups[] = {
1998f1e1ea51SMario Limonciello 	&kbd_led_group,
1999f1e1ea51SMario Limonciello 	&kbd_led_als_group,
2000f1e1ea51SMario Limonciello 	NULL,
2001f1e1ea51SMario Limonciello };
2002f1e1ea51SMario Limonciello 
kbd_led_level_get(struct led_classdev * led_cdev)2003f1e1ea51SMario Limonciello static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
2004f1e1ea51SMario Limonciello {
2005f1e1ea51SMario Limonciello 	int ret;
2006f1e1ea51SMario Limonciello 	u16 num;
2007f1e1ea51SMario Limonciello 	struct kbd_state state;
2008f1e1ea51SMario Limonciello 
2009f1e1ea51SMario Limonciello 	if (kbd_get_max_level()) {
2010f1e1ea51SMario Limonciello 		ret = kbd_get_state(&state);
2011f1e1ea51SMario Limonciello 		if (ret)
2012f1e1ea51SMario Limonciello 			return 0;
2013f1e1ea51SMario Limonciello 		ret = kbd_get_level(&state);
2014f1e1ea51SMario Limonciello 		if (ret < 0)
2015f1e1ea51SMario Limonciello 			return 0;
2016f1e1ea51SMario Limonciello 		return ret;
2017f1e1ea51SMario Limonciello 	}
2018f1e1ea51SMario Limonciello 
2019f1e1ea51SMario Limonciello 	if (kbd_get_valid_token_counts()) {
2020f1e1ea51SMario Limonciello 		ret = kbd_get_first_active_token_bit();
2021f1e1ea51SMario Limonciello 		if (ret < 0)
2022f1e1ea51SMario Limonciello 			return 0;
2023f1e1ea51SMario Limonciello 		for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
2024f1e1ea51SMario Limonciello 			num &= num - 1; /* clear the first bit set */
2025f1e1ea51SMario Limonciello 		if (num == 0)
2026f1e1ea51SMario Limonciello 			return 0;
2027f1e1ea51SMario Limonciello 		return ffs(num) - 1;
2028f1e1ea51SMario Limonciello 	}
2029f1e1ea51SMario Limonciello 
2030f1e1ea51SMario Limonciello 	pr_warn("Keyboard brightness level control not supported\n");
2031f1e1ea51SMario Limonciello 	return 0;
2032f1e1ea51SMario Limonciello }
2033f1e1ea51SMario Limonciello 
kbd_led_level_set(struct led_classdev * led_cdev,enum led_brightness value)2034f1e1ea51SMario Limonciello static int kbd_led_level_set(struct led_classdev *led_cdev,
2035f1e1ea51SMario Limonciello 			     enum led_brightness value)
2036f1e1ea51SMario Limonciello {
2037f1e1ea51SMario Limonciello 	enum led_brightness new_value = value;
2038f1e1ea51SMario Limonciello 	struct kbd_state state;
2039f1e1ea51SMario Limonciello 	struct kbd_state new_state;
2040f1e1ea51SMario Limonciello 	u16 num;
2041f1e1ea51SMario Limonciello 	int ret;
2042f1e1ea51SMario Limonciello 
2043f1e1ea51SMario Limonciello 	mutex_lock(&kbd_led_mutex);
2044f1e1ea51SMario Limonciello 
2045f1e1ea51SMario Limonciello 	if (kbd_get_max_level()) {
2046f1e1ea51SMario Limonciello 		ret = kbd_get_state(&state);
2047f1e1ea51SMario Limonciello 		if (ret)
2048f1e1ea51SMario Limonciello 			goto out;
2049f1e1ea51SMario Limonciello 		new_state = state;
2050f1e1ea51SMario Limonciello 		ret = kbd_set_level(&new_state, value);
2051f1e1ea51SMario Limonciello 		if (ret)
2052f1e1ea51SMario Limonciello 			goto out;
2053f1e1ea51SMario Limonciello 		ret = kbd_set_state_safe(&new_state, &state);
2054f1e1ea51SMario Limonciello 	} else if (kbd_get_valid_token_counts()) {
2055f1e1ea51SMario Limonciello 		for (num = kbd_token_bits; num != 0 && value > 0; --value)
2056f1e1ea51SMario Limonciello 			num &= num - 1; /* clear the first bit set */
2057f1e1ea51SMario Limonciello 		if (num == 0)
2058f1e1ea51SMario Limonciello 			ret = 0;
2059f1e1ea51SMario Limonciello 		else
2060f1e1ea51SMario Limonciello 			ret = kbd_set_token_bit(ffs(num) - 1);
2061f1e1ea51SMario Limonciello 	} else {
2062f1e1ea51SMario Limonciello 		pr_warn("Keyboard brightness level control not supported\n");
2063f1e1ea51SMario Limonciello 		ret = -ENXIO;
2064f1e1ea51SMario Limonciello 	}
2065f1e1ea51SMario Limonciello 
2066f1e1ea51SMario Limonciello out:
2067f1e1ea51SMario Limonciello 	if (ret == 0)
2068f1e1ea51SMario Limonciello 		kbd_led_level = new_value;
2069f1e1ea51SMario Limonciello 
2070f1e1ea51SMario Limonciello 	mutex_unlock(&kbd_led_mutex);
2071f1e1ea51SMario Limonciello 	return ret;
2072f1e1ea51SMario Limonciello }
2073f1e1ea51SMario Limonciello 
2074f1e1ea51SMario Limonciello static struct led_classdev kbd_led = {
2075f1e1ea51SMario Limonciello 	.name           = "dell::kbd_backlight",
2076f1e1ea51SMario Limonciello 	.flags		= LED_BRIGHT_HW_CHANGED,
2077f1e1ea51SMario Limonciello 	.brightness_set_blocking = kbd_led_level_set,
2078f1e1ea51SMario Limonciello 	.brightness_get = kbd_led_level_get,
2079f1e1ea51SMario Limonciello 	.groups         = kbd_led_groups,
2080f1e1ea51SMario Limonciello };
2081f1e1ea51SMario Limonciello 
kbd_led_init(struct device * dev)2082f1e1ea51SMario Limonciello static int __init kbd_led_init(struct device *dev)
2083f1e1ea51SMario Limonciello {
2084f1e1ea51SMario Limonciello 	int ret;
2085f1e1ea51SMario Limonciello 
2086f1e1ea51SMario Limonciello 	kbd_init();
2087f1e1ea51SMario Limonciello 	if (!kbd_led_present)
2088f1e1ea51SMario Limonciello 		return -ENODEV;
2089f1e1ea51SMario Limonciello 	if (!kbd_als_supported)
2090f1e1ea51SMario Limonciello 		kbd_led_groups[1] = NULL;
2091f1e1ea51SMario Limonciello 	kbd_led.max_brightness = kbd_get_max_level();
2092f1e1ea51SMario Limonciello 	if (!kbd_led.max_brightness) {
2093f1e1ea51SMario Limonciello 		kbd_led.max_brightness = kbd_get_valid_token_counts();
2094f1e1ea51SMario Limonciello 		if (kbd_led.max_brightness)
2095f1e1ea51SMario Limonciello 			kbd_led.max_brightness--;
2096f1e1ea51SMario Limonciello 	}
2097f1e1ea51SMario Limonciello 
2098f1e1ea51SMario Limonciello 	kbd_led_level = kbd_led_level_get(NULL);
2099f1e1ea51SMario Limonciello 
2100f1e1ea51SMario Limonciello 	ret = led_classdev_register(dev, &kbd_led);
2101f1e1ea51SMario Limonciello 	if (ret)
2102f1e1ea51SMario Limonciello 		kbd_led_present = false;
2103f1e1ea51SMario Limonciello 
2104f1e1ea51SMario Limonciello 	return ret;
2105f1e1ea51SMario Limonciello }
2106f1e1ea51SMario Limonciello 
brightness_set_exit(struct led_classdev * led_cdev,enum led_brightness value)2107f1e1ea51SMario Limonciello static void brightness_set_exit(struct led_classdev *led_cdev,
2108f1e1ea51SMario Limonciello 				enum led_brightness value)
2109f1e1ea51SMario Limonciello {
2110f1e1ea51SMario Limonciello 	/* Don't change backlight level on exit */
2111f1e1ea51SMario Limonciello };
2112f1e1ea51SMario Limonciello 
kbd_led_exit(void)2113f1e1ea51SMario Limonciello static void kbd_led_exit(void)
2114f1e1ea51SMario Limonciello {
2115f1e1ea51SMario Limonciello 	if (!kbd_led_present)
2116f1e1ea51SMario Limonciello 		return;
2117f1e1ea51SMario Limonciello 	kbd_led.brightness_set = brightness_set_exit;
2118f1e1ea51SMario Limonciello 	led_classdev_unregister(&kbd_led);
2119f1e1ea51SMario Limonciello }
2120f1e1ea51SMario Limonciello 
dell_laptop_notifier_call(struct notifier_block * nb,unsigned long action,void * data)2121f1e1ea51SMario Limonciello static int dell_laptop_notifier_call(struct notifier_block *nb,
2122f1e1ea51SMario Limonciello 				     unsigned long action, void *data)
2123f1e1ea51SMario Limonciello {
2124f1e1ea51SMario Limonciello 	bool changed = false;
2125f1e1ea51SMario Limonciello 	enum led_brightness new_kbd_led_level;
2126f1e1ea51SMario Limonciello 
2127f1e1ea51SMario Limonciello 	switch (action) {
2128f1e1ea51SMario Limonciello 	case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2129f1e1ea51SMario Limonciello 		if (!kbd_led_present)
2130f1e1ea51SMario Limonciello 			break;
2131f1e1ea51SMario Limonciello 
2132f1e1ea51SMario Limonciello 		mutex_lock(&kbd_led_mutex);
2133f1e1ea51SMario Limonciello 		new_kbd_led_level = kbd_led_level_get(&kbd_led);
2134f1e1ea51SMario Limonciello 		if (kbd_led_level != new_kbd_led_level) {
2135f1e1ea51SMario Limonciello 			kbd_led_level = new_kbd_led_level;
2136f1e1ea51SMario Limonciello 			changed = true;
2137f1e1ea51SMario Limonciello 		}
2138f1e1ea51SMario Limonciello 		mutex_unlock(&kbd_led_mutex);
2139f1e1ea51SMario Limonciello 
2140f1e1ea51SMario Limonciello 		if (changed)
2141f1e1ea51SMario Limonciello 			led_classdev_notify_brightness_hw_changed(&kbd_led,
2142f1e1ea51SMario Limonciello 								kbd_led_level);
2143f1e1ea51SMario Limonciello 		break;
2144f1e1ea51SMario Limonciello 	}
2145f1e1ea51SMario Limonciello 
2146f1e1ea51SMario Limonciello 	return NOTIFY_OK;
2147f1e1ea51SMario Limonciello }
2148f1e1ea51SMario Limonciello 
2149f1e1ea51SMario Limonciello static struct notifier_block dell_laptop_notifier = {
2150f1e1ea51SMario Limonciello 	.notifier_call = dell_laptop_notifier_call,
2151f1e1ea51SMario Limonciello };
2152f1e1ea51SMario Limonciello 
micmute_led_set(struct led_classdev * led_cdev,enum led_brightness brightness)2153f1e1ea51SMario Limonciello static int micmute_led_set(struct led_classdev *led_cdev,
2154f1e1ea51SMario Limonciello 			   enum led_brightness brightness)
2155f1e1ea51SMario Limonciello {
2156f1e1ea51SMario Limonciello 	struct calling_interface_buffer buffer;
2157f1e1ea51SMario Limonciello 	struct calling_interface_token *token;
2158f1e1ea51SMario Limonciello 	int state = brightness != LED_OFF;
2159f1e1ea51SMario Limonciello 
2160f1e1ea51SMario Limonciello 	if (state == 0)
2161f1e1ea51SMario Limonciello 		token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2162f1e1ea51SMario Limonciello 	else
2163f1e1ea51SMario Limonciello 		token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2164f1e1ea51SMario Limonciello 
2165f1e1ea51SMario Limonciello 	if (!token)
2166f1e1ea51SMario Limonciello 		return -ENODEV;
2167f1e1ea51SMario Limonciello 
2168f1e1ea51SMario Limonciello 	dell_fill_request(&buffer, token->location, token->value, 0, 0);
2169f1e1ea51SMario Limonciello 	dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2170f1e1ea51SMario Limonciello 
2171f1e1ea51SMario Limonciello 	return 0;
2172f1e1ea51SMario Limonciello }
2173f1e1ea51SMario Limonciello 
2174f1e1ea51SMario Limonciello static struct led_classdev micmute_led_cdev = {
2175f1e1ea51SMario Limonciello 	.name = "platform::micmute",
2176f1e1ea51SMario Limonciello 	.max_brightness = 1,
2177f1e1ea51SMario Limonciello 	.brightness_set_blocking = micmute_led_set,
2178f1e1ea51SMario Limonciello 	.default_trigger = "audio-micmute",
2179f1e1ea51SMario Limonciello };
2180f1e1ea51SMario Limonciello 
mute_led_set(struct led_classdev * led_cdev,enum led_brightness brightness)2181*3ed0b880SKoba Ko static int mute_led_set(struct led_classdev *led_cdev,
2182*3ed0b880SKoba Ko 			   enum led_brightness brightness)
2183*3ed0b880SKoba Ko {
2184*3ed0b880SKoba Ko 	struct calling_interface_buffer buffer;
2185*3ed0b880SKoba Ko 	struct calling_interface_token *token;
2186*3ed0b880SKoba Ko 	int state = brightness != LED_OFF;
2187*3ed0b880SKoba Ko 
2188*3ed0b880SKoba Ko 	if (state == 0)
2189*3ed0b880SKoba Ko 		token = dell_smbios_find_token(GLOBAL_MUTE_DISABLE);
2190*3ed0b880SKoba Ko 	else
2191*3ed0b880SKoba Ko 		token = dell_smbios_find_token(GLOBAL_MUTE_ENABLE);
2192*3ed0b880SKoba Ko 
2193*3ed0b880SKoba Ko 	if (!token)
2194*3ed0b880SKoba Ko 		return -ENODEV;
2195*3ed0b880SKoba Ko 
2196*3ed0b880SKoba Ko 	dell_fill_request(&buffer, token->location, token->value, 0, 0);
2197*3ed0b880SKoba Ko 	dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2198*3ed0b880SKoba Ko 
2199*3ed0b880SKoba Ko 	return 0;
2200*3ed0b880SKoba Ko }
2201*3ed0b880SKoba Ko 
2202*3ed0b880SKoba Ko static struct led_classdev mute_led_cdev = {
2203*3ed0b880SKoba Ko 	.name = "platform::mute",
2204*3ed0b880SKoba Ko 	.max_brightness = 1,
2205*3ed0b880SKoba Ko 	.brightness_set_blocking = mute_led_set,
2206*3ed0b880SKoba Ko 	.default_trigger = "audio-mute",
2207*3ed0b880SKoba Ko };
2208*3ed0b880SKoba Ko 
dell_init(void)2209f1e1ea51SMario Limonciello static int __init dell_init(void)
2210f1e1ea51SMario Limonciello {
2211f1e1ea51SMario Limonciello 	struct calling_interface_token *token;
2212f1e1ea51SMario Limonciello 	int max_intensity = 0;
2213f1e1ea51SMario Limonciello 	int ret;
2214f1e1ea51SMario Limonciello 
2215f1e1ea51SMario Limonciello 	if (!dmi_check_system(dell_device_table))
2216f1e1ea51SMario Limonciello 		return -ENODEV;
2217f1e1ea51SMario Limonciello 
2218f1e1ea51SMario Limonciello 	quirks = NULL;
2219f1e1ea51SMario Limonciello 	/* find if this machine support other functions */
2220f1e1ea51SMario Limonciello 	dmi_check_system(dell_quirks);
2221f1e1ea51SMario Limonciello 
2222f1e1ea51SMario Limonciello 	ret = platform_driver_register(&platform_driver);
2223f1e1ea51SMario Limonciello 	if (ret)
2224f1e1ea51SMario Limonciello 		goto fail_platform_driver;
22258d05fc03SBarnabás Pőcze 	platform_device = platform_device_alloc("dell-laptop", PLATFORM_DEVID_NONE);
2226f1e1ea51SMario Limonciello 	if (!platform_device) {
2227f1e1ea51SMario Limonciello 		ret = -ENOMEM;
2228f1e1ea51SMario Limonciello 		goto fail_platform_device1;
2229f1e1ea51SMario Limonciello 	}
2230f1e1ea51SMario Limonciello 	ret = platform_device_add(platform_device);
2231f1e1ea51SMario Limonciello 	if (ret)
2232f1e1ea51SMario Limonciello 		goto fail_platform_device2;
2233f1e1ea51SMario Limonciello 
2234f1e1ea51SMario Limonciello 	ret = dell_setup_rfkill();
2235f1e1ea51SMario Limonciello 
2236f1e1ea51SMario Limonciello 	if (ret) {
2237f1e1ea51SMario Limonciello 		pr_warn("Unable to setup rfkill\n");
2238f1e1ea51SMario Limonciello 		goto fail_rfkill;
2239f1e1ea51SMario Limonciello 	}
2240f1e1ea51SMario Limonciello 
2241f1e1ea51SMario Limonciello 	if (quirks && quirks->touchpad_led)
2242f1e1ea51SMario Limonciello 		touchpad_led_init(&platform_device->dev);
2243f1e1ea51SMario Limonciello 
2244f1e1ea51SMario Limonciello 	kbd_led_init(&platform_device->dev);
2245f1e1ea51SMario Limonciello 
2246f1e1ea51SMario Limonciello 	dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2247f1e1ea51SMario Limonciello 	debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2248f1e1ea51SMario Limonciello 			    &dell_debugfs_fops);
2249f1e1ea51SMario Limonciello 
2250f1e1ea51SMario Limonciello 	dell_laptop_register_notifier(&dell_laptop_notifier);
2251f1e1ea51SMario Limonciello 
2252f1e1ea51SMario Limonciello 	if (dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE) &&
22538af9fa37SPerry Yuan 	    dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE) &&
22548af9fa37SPerry Yuan 	    !dell_privacy_has_mic_mute()) {
2255f1e1ea51SMario Limonciello 		micmute_led_cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
2256f1e1ea51SMario Limonciello 		ret = led_classdev_register(&platform_device->dev, &micmute_led_cdev);
2257f1e1ea51SMario Limonciello 		if (ret < 0)
2258f1e1ea51SMario Limonciello 			goto fail_led;
22598af9fa37SPerry Yuan 		micmute_led_registered = true;
2260f1e1ea51SMario Limonciello 	}
2261f1e1ea51SMario Limonciello 
2262*3ed0b880SKoba Ko 	if (dell_smbios_find_token(GLOBAL_MUTE_DISABLE) &&
2263*3ed0b880SKoba Ko 	    dell_smbios_find_token(GLOBAL_MUTE_ENABLE)) {
2264*3ed0b880SKoba Ko 		mute_led_cdev.brightness = ledtrig_audio_get(LED_AUDIO_MUTE);
2265*3ed0b880SKoba Ko 		ret = led_classdev_register(&platform_device->dev, &mute_led_cdev);
2266*3ed0b880SKoba Ko 		if (ret < 0)
2267*3ed0b880SKoba Ko 			goto fail_backlight;
2268*3ed0b880SKoba Ko 		mute_led_registered = true;
2269*3ed0b880SKoba Ko 	}
2270*3ed0b880SKoba Ko 
2271f1e1ea51SMario Limonciello 	if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2272f1e1ea51SMario Limonciello 		return 0;
2273f1e1ea51SMario Limonciello 
2274f1e1ea51SMario Limonciello 	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2275f1e1ea51SMario Limonciello 	if (token) {
2276f1e1ea51SMario Limonciello 		struct calling_interface_buffer buffer;
2277f1e1ea51SMario Limonciello 
2278f1e1ea51SMario Limonciello 		dell_fill_request(&buffer, token->location, 0, 0, 0);
2279f1e1ea51SMario Limonciello 		ret = dell_send_request(&buffer,
2280f1e1ea51SMario Limonciello 					CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2281f1e1ea51SMario Limonciello 		if (ret == 0)
2282f1e1ea51SMario Limonciello 			max_intensity = buffer.output[3];
2283f1e1ea51SMario Limonciello 	}
2284f1e1ea51SMario Limonciello 
2285f1e1ea51SMario Limonciello 	if (max_intensity) {
2286f1e1ea51SMario Limonciello 		struct backlight_properties props;
2287f1e1ea51SMario Limonciello 		memset(&props, 0, sizeof(struct backlight_properties));
2288f1e1ea51SMario Limonciello 		props.type = BACKLIGHT_PLATFORM;
2289f1e1ea51SMario Limonciello 		props.max_brightness = max_intensity;
2290f1e1ea51SMario Limonciello 		dell_backlight_device = backlight_device_register("dell_backlight",
2291f1e1ea51SMario Limonciello 								  &platform_device->dev,
2292f1e1ea51SMario Limonciello 								  NULL,
2293f1e1ea51SMario Limonciello 								  &dell_ops,
2294f1e1ea51SMario Limonciello 								  &props);
2295f1e1ea51SMario Limonciello 
2296f1e1ea51SMario Limonciello 		if (IS_ERR(dell_backlight_device)) {
2297f1e1ea51SMario Limonciello 			ret = PTR_ERR(dell_backlight_device);
2298f1e1ea51SMario Limonciello 			dell_backlight_device = NULL;
2299f1e1ea51SMario Limonciello 			goto fail_backlight;
2300f1e1ea51SMario Limonciello 		}
2301f1e1ea51SMario Limonciello 
2302f1e1ea51SMario Limonciello 		dell_backlight_device->props.brightness =
2303f1e1ea51SMario Limonciello 			dell_get_intensity(dell_backlight_device);
2304f1e1ea51SMario Limonciello 		if (dell_backlight_device->props.brightness < 0) {
2305f1e1ea51SMario Limonciello 			ret = dell_backlight_device->props.brightness;
2306f1e1ea51SMario Limonciello 			goto fail_get_brightness;
2307f1e1ea51SMario Limonciello 		}
2308f1e1ea51SMario Limonciello 		backlight_update_status(dell_backlight_device);
2309f1e1ea51SMario Limonciello 	}
2310f1e1ea51SMario Limonciello 
2311f1e1ea51SMario Limonciello 	return 0;
2312f1e1ea51SMario Limonciello 
2313f1e1ea51SMario Limonciello fail_get_brightness:
2314f1e1ea51SMario Limonciello 	backlight_device_unregister(dell_backlight_device);
2315f1e1ea51SMario Limonciello fail_backlight:
23168af9fa37SPerry Yuan 	if (micmute_led_registered)
2317f1e1ea51SMario Limonciello 		led_classdev_unregister(&micmute_led_cdev);
2318*3ed0b880SKoba Ko 	if (mute_led_registered)
2319*3ed0b880SKoba Ko 		led_classdev_unregister(&mute_led_cdev);
2320f1e1ea51SMario Limonciello fail_led:
2321f1e1ea51SMario Limonciello 	dell_cleanup_rfkill();
2322f1e1ea51SMario Limonciello fail_rfkill:
2323f1e1ea51SMario Limonciello 	platform_device_del(platform_device);
2324f1e1ea51SMario Limonciello fail_platform_device2:
2325f1e1ea51SMario Limonciello 	platform_device_put(platform_device);
2326f1e1ea51SMario Limonciello fail_platform_device1:
2327f1e1ea51SMario Limonciello 	platform_driver_unregister(&platform_driver);
2328f1e1ea51SMario Limonciello fail_platform_driver:
2329f1e1ea51SMario Limonciello 	return ret;
2330f1e1ea51SMario Limonciello }
2331f1e1ea51SMario Limonciello 
dell_exit(void)2332f1e1ea51SMario Limonciello static void __exit dell_exit(void)
2333f1e1ea51SMario Limonciello {
2334f1e1ea51SMario Limonciello 	dell_laptop_unregister_notifier(&dell_laptop_notifier);
2335f1e1ea51SMario Limonciello 	debugfs_remove_recursive(dell_laptop_dir);
2336f1e1ea51SMario Limonciello 	if (quirks && quirks->touchpad_led)
2337f1e1ea51SMario Limonciello 		touchpad_led_exit();
2338f1e1ea51SMario Limonciello 	kbd_led_exit();
2339f1e1ea51SMario Limonciello 	backlight_device_unregister(dell_backlight_device);
23408af9fa37SPerry Yuan 	if (micmute_led_registered)
2341f1e1ea51SMario Limonciello 		led_classdev_unregister(&micmute_led_cdev);
2342*3ed0b880SKoba Ko 	if (mute_led_registered)
2343*3ed0b880SKoba Ko 		led_classdev_unregister(&mute_led_cdev);
2344f1e1ea51SMario Limonciello 	dell_cleanup_rfkill();
2345f1e1ea51SMario Limonciello 	if (platform_device) {
2346f1e1ea51SMario Limonciello 		platform_device_unregister(platform_device);
2347f1e1ea51SMario Limonciello 		platform_driver_unregister(&platform_driver);
2348f1e1ea51SMario Limonciello 	}
2349f1e1ea51SMario Limonciello }
2350f1e1ea51SMario Limonciello 
2351f1e1ea51SMario Limonciello /* dell-rbtn.c driver export functions which will not work correctly (and could
2352f1e1ea51SMario Limonciello  * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2353f1e1ea51SMario Limonciello  * not problem when dell-rbtn.c is compiled as external module. When both files
2354f1e1ea51SMario Limonciello  * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2355f1e1ea51SMario Limonciello  * need to ensure that dell_init() will be called after initializing dell-rbtn.
2356f1e1ea51SMario Limonciello  * This can be achieved by late_initcall() instead module_init().
2357f1e1ea51SMario Limonciello  */
2358f1e1ea51SMario Limonciello late_initcall(dell_init);
2359f1e1ea51SMario Limonciello module_exit(dell_exit);
2360f1e1ea51SMario Limonciello 
2361f1e1ea51SMario Limonciello MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2362f1e1ea51SMario Limonciello MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2363f1e1ea51SMario Limonciello MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
2364f1e1ea51SMario Limonciello MODULE_DESCRIPTION("Dell laptop driver");
2365f1e1ea51SMario Limonciello MODULE_LICENSE("GPL");
2366