1 // SPDX-License-Identifier: GPL-2.0+
3 * Surface Book (2 and later) hot-plug driver.
5 * Surface Book devices (can) have a hot-pluggable discrete GPU (dGPU). This
6 * driver is responsible for out-of-band hot-plug event signaling on these
7 * devices. It is specifically required when the hot-plug device is in D3cold
8 * and can thus not generate PCIe hot-plug events itself.
10 * Event signaling is handled via ACPI, which will generate the appropriate
11 * device-check notifications to be picked up by the PCIe hot-plug driver.
16 #include <linux/acpi.h>
17 #include <linux/gpio.h>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/platform_device.h>
24 static const struct acpi_gpio_params shps_base_presence_int = { 0, 0, false };
25 static const struct acpi_gpio_params shps_base_presence = { 1, 0, false };
26 static const struct acpi_gpio_params shps_device_power_int = { 2, 0, false };
27 static const struct acpi_gpio_params shps_device_power = { 3, 0, false };
28 static const struct acpi_gpio_params shps_device_presence_int = { 4, 0, false };
29 static const struct acpi_gpio_params shps_device_presence = { 5, 0, false };
31 static const struct acpi_gpio_mapping shps_acpi_gpios[] = {
32 { "base_presence-int-gpio", &shps_base_presence_int, 1 },
33 { "base_presence-gpio", &shps_base_presence, 1 },
34 { "device_power-int-gpio", &shps_device_power_int, 1 },
35 { "device_power-gpio", &shps_device_power, 1 },
36 { "device_presence-int-gpio", &shps_device_presence_int, 1 },
37 { "device_presence-gpio", &shps_device_presence, 1 },
41 /* 5515a847-ed55-4b27-8352-cd320e10360a */
42 static const guid_t shps_dsm_guid =
43 GUID_INIT(0x5515a847, 0xed55, 0x4b27, 0x83, 0x52, 0xcd, 0x32, 0x0e, 0x10, 0x36, 0x0a);
45 #define SHPS_DSM_REVISION 1
48 SHPS_DSM_FN_PCI_NUM_ENTRIES = 0x01,
49 SHPS_DSM_FN_PCI_GET_ENTRIES = 0x02,
50 SHPS_DSM_FN_IRQ_BASE_PRESENCE = 0x03,
51 SHPS_DSM_FN_IRQ_DEVICE_POWER = 0x04,
52 SHPS_DSM_FN_IRQ_DEVICE_PRESENCE = 0x05,
56 /* NOTE: Must be in order of enum shps_dsm_fn above. */
57 SHPS_IRQ_TYPE_BASE_PRESENCE = 0,
58 SHPS_IRQ_TYPE_DEVICE_POWER = 1,
59 SHPS_IRQ_TYPE_DEVICE_PRESENCE = 2,
63 static const char *const shps_gpio_names[] = {
64 [SHPS_IRQ_TYPE_BASE_PRESENCE] = "base_presence",
65 [SHPS_IRQ_TYPE_DEVICE_POWER] = "device_power",
66 [SHPS_IRQ_TYPE_DEVICE_PRESENCE] = "device_presence",
70 struct mutex lock[SHPS_NUM_IRQS]; /* Protects update in shps_dsm_notify_irq() */
71 struct gpio_desc *gpio[SHPS_NUM_IRQS];
72 unsigned int irq[SHPS_NUM_IRQS];
75 #define SHPS_IRQ_NOT_PRESENT ((unsigned int)-1)
77 static enum shps_dsm_fn shps_dsm_fn_for_irq(enum shps_irq_type type)
79 return SHPS_DSM_FN_IRQ_BASE_PRESENCE + type;
82 static void shps_dsm_notify_irq(struct platform_device *pdev, enum shps_irq_type type)
84 struct shps_device *sdev = platform_get_drvdata(pdev);
85 acpi_handle handle = ACPI_HANDLE(&pdev->dev);
86 union acpi_object *result;
87 union acpi_object param;
90 mutex_lock(&sdev->lock[type]);
92 value = gpiod_get_value_cansleep(sdev->gpio[type]);
94 mutex_unlock(&sdev->lock[type]);
95 dev_err(&pdev->dev, "failed to get gpio: %d (irq=%d)\n", type, value);
99 dev_dbg(&pdev->dev, "IRQ notification via DSM (irq=%d, value=%d)\n", type, value);
101 param.type = ACPI_TYPE_INTEGER;
102 param.integer.value = value;
104 result = acpi_evaluate_dsm(handle, &shps_dsm_guid, SHPS_DSM_REVISION,
105 shps_dsm_fn_for_irq(type), ¶m);
108 dev_err(&pdev->dev, "IRQ notification via DSM failed (irq=%d, gpio=%d)\n",
111 } else if (result->type != ACPI_TYPE_BUFFER) {
113 "IRQ notification via DSM failed: unexpected result type (irq=%d, gpio=%d)\n",
116 } else if (result->buffer.length != 1 || result->buffer.pointer[0] != 0) {
118 "IRQ notification via DSM failed: unexpected result value (irq=%d, gpio=%d)\n",
122 mutex_unlock(&sdev->lock[type]);
128 static irqreturn_t shps_handle_irq(int irq, void *data)
130 struct platform_device *pdev = data;
131 struct shps_device *sdev = platform_get_drvdata(pdev);
134 /* Figure out which IRQ we're handling. */
135 for (type = 0; type < SHPS_NUM_IRQS; type++)
136 if (irq == sdev->irq[type])
139 /* We should have found our interrupt, if not: this is a bug. */
140 if (WARN(type >= SHPS_NUM_IRQS, "invalid IRQ number: %d\n", irq))
143 /* Forward interrupt to ACPI via DSM. */
144 shps_dsm_notify_irq(pdev, type);
148 static int shps_setup_irq(struct platform_device *pdev, enum shps_irq_type type)
150 unsigned long flags = IRQF_ONESHOT | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
151 struct shps_device *sdev = platform_get_drvdata(pdev);
152 struct gpio_desc *gpiod;
153 acpi_handle handle = ACPI_HANDLE(&pdev->dev);
154 const char *irq_name;
155 const int dsm = shps_dsm_fn_for_irq(type);
159 * Only set up interrupts that we actually need: The Surface Book 3
160 * does not have a DSM for base presence, so don't set up an interrupt
163 if (!acpi_check_dsm(handle, &shps_dsm_guid, SHPS_DSM_REVISION, BIT(dsm))) {
164 dev_dbg(&pdev->dev, "IRQ notification via DSM not present (irq=%d)\n", type);
168 gpiod = devm_gpiod_get(&pdev->dev, shps_gpio_names[type], GPIOD_ASIS);
170 return PTR_ERR(gpiod);
172 irq = gpiod_to_irq(gpiod);
176 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "shps-irq-%d", type);
180 status = devm_request_threaded_irq(&pdev->dev, irq, NULL, shps_handle_irq,
181 flags, irq_name, pdev);
185 dev_dbg(&pdev->dev, "set up irq %d as type %d\n", irq, type);
187 sdev->gpio[type] = gpiod;
188 sdev->irq[type] = irq;
193 static int surface_hotplug_remove(struct platform_device *pdev)
195 struct shps_device *sdev = platform_get_drvdata(pdev);
198 /* Ensure that IRQs have been fully handled and won't trigger any more. */
199 for (i = 0; i < SHPS_NUM_IRQS; i++) {
200 if (sdev->irq[i] != SHPS_IRQ_NOT_PRESENT)
201 disable_irq(sdev->irq[i]);
203 mutex_destroy(&sdev->lock[i]);
209 static int surface_hotplug_probe(struct platform_device *pdev)
211 struct shps_device *sdev;
215 * The MSHW0153 device is also present on the Surface Laptop 3,
216 * however that doesn't have a hot-pluggable PCIe device. It also
217 * doesn't have any GPIO interrupts/pins under the MSHW0153, so filter
220 if (gpiod_count(&pdev->dev, NULL) < 0)
223 status = devm_acpi_dev_add_driver_gpios(&pdev->dev, shps_acpi_gpios);
227 sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
231 platform_set_drvdata(pdev, sdev);
234 * Initialize IRQs so that we can safely call surface_hotplug_remove()
237 for (i = 0; i < SHPS_NUM_IRQS; i++)
238 sdev->irq[i] = SHPS_IRQ_NOT_PRESENT;
241 for (i = 0; i < SHPS_NUM_IRQS; i++) {
242 mutex_init(&sdev->lock[i]);
244 status = shps_setup_irq(pdev, i);
246 dev_err(&pdev->dev, "failed to set up IRQ %d: %d\n", i, status);
251 /* Ensure everything is up-to-date. */
252 for (i = 0; i < SHPS_NUM_IRQS; i++)
253 if (sdev->irq[i] != SHPS_IRQ_NOT_PRESENT)
254 shps_dsm_notify_irq(pdev, i);
259 surface_hotplug_remove(pdev);
263 static const struct acpi_device_id surface_hotplug_acpi_match[] = {
267 MODULE_DEVICE_TABLE(acpi, surface_hotplug_acpi_match);
269 static struct platform_driver surface_hotplug_driver = {
270 .probe = surface_hotplug_probe,
271 .remove = surface_hotplug_remove,
273 .name = "surface_hotplug",
274 .acpi_match_table = surface_hotplug_acpi_match,
275 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
278 module_platform_driver(surface_hotplug_driver);
281 MODULE_DESCRIPTION("Surface Hot-Plug Signaling Driver for Surface Book Devices");
282 MODULE_LICENSE("GPL");