1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for controlling LEDs for cameras connected to the Intel atomisp2
4  * The main purpose of this driver is to turn off LEDs which are on at boot.
5  *
6  * Copyright (C) 2020 Hans de Goede <hdegoede@redhat.com>
7  */
8 
9 #include <linux/dmi.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/gpio/machine.h>
12 #include <linux/leds.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/platform_device.h>
16 #include <linux/workqueue.h>
17 
18 /* This must be leds-gpio as the leds-gpio driver binds to the name */
19 #define DEV_NAME		"leds-gpio"
20 
21 static const struct gpio_led atomisp2_leds[] = {
22 	{
23 		.name = "atomisp2::camera",
24 		.default_state  = LEDS_GPIO_DEFSTATE_OFF,
25 	},
26 };
27 
28 static const struct gpio_led_platform_data atomisp2_leds_pdata = {
29 	.num_leds	= ARRAY_SIZE(atomisp2_leds),
30 	.leds		= atomisp2_leds,
31 };
32 
33 static struct gpiod_lookup_table asus_t100ta_lookup = {
34 	.dev_id = DEV_NAME,
35 	.table = {
36 		GPIO_LOOKUP_IDX("INT33FC:02", 8, NULL, 0, GPIO_ACTIVE_HIGH),
37 		{ }
38 	}
39 };
40 
41 static struct gpiod_lookup_table asus_t100chi_lookup = {
42 	.dev_id = DEV_NAME,
43 	.table = {
44 		GPIO_LOOKUP_IDX("INT33FC:01", 24, NULL, 0, GPIO_ACTIVE_HIGH),
45 		{ }
46 	}
47 };
48 
49 static const struct dmi_system_id atomisp2_led_systems[] __initconst = {
50 	{
51 		.matches = {
52 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
53 			/* Non exact match to also match T100TAF */
54 			DMI_MATCH(DMI_PRODUCT_NAME, "T100TA"),
55 		},
56 		.driver_data = &asus_t100ta_lookup,
57 	},
58 	{
59 		.matches = {
60 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
61 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T200TA"),
62 		},
63 		.driver_data = &asus_t100ta_lookup,
64 	},
65 	{
66 		.matches = {
67 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
68 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100CHI"),
69 		},
70 		.driver_data = &asus_t100chi_lookup,
71 	},
72 	{} /* Terminating entry */
73 };
74 MODULE_DEVICE_TABLE(dmi, atomisp2_led_systems);
75 
76 static struct gpiod_lookup_table *gpio_lookup;
77 static struct platform_device *pdev;
78 
atomisp2_led_init(void)79 static int __init atomisp2_led_init(void)
80 {
81 	const struct dmi_system_id *system;
82 
83 	system = dmi_first_match(atomisp2_led_systems);
84 	if (!system)
85 		return -ENODEV;
86 
87 	gpio_lookup = system->driver_data;
88 	gpiod_add_lookup_table(gpio_lookup);
89 
90 	pdev = platform_device_register_resndata(NULL,
91 						 DEV_NAME, PLATFORM_DEVID_NONE,
92 						 NULL, 0, &atomisp2_leds_pdata,
93 						 sizeof(atomisp2_leds_pdata));
94 	if (IS_ERR(pdev))
95 		gpiod_remove_lookup_table(gpio_lookup);
96 
97 	return PTR_ERR_OR_ZERO(pdev);
98 }
99 
atomisp2_led_cleanup(void)100 static void __exit atomisp2_led_cleanup(void)
101 {
102 	platform_device_unregister(pdev);
103 	gpiod_remove_lookup_table(gpio_lookup);
104 }
105 
106 module_init(atomisp2_led_init);
107 module_exit(atomisp2_led_cleanup);
108 
109 /*
110  * The ACPI INIT method from Asus WMI's code on the T100TA and T200TA turns the
111  * LED on (without the WMI interface allowing further control over the LED).
112  * Ensure we are loaded after asus-nb-wmi so that we turn the LED off again.
113  */
114 MODULE_SOFTDEP("pre: asus_nb_wmi");
115 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com");
116 MODULE_DESCRIPTION("Intel atomisp2 camera LED driver");
117 MODULE_LICENSE("GPL");
118