| 1 | |
| 2 | |
| 3 | * Surface Book (2 and later) hot-plug driver.
|
| 4 | *
|
| 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.
|
| 9 | *
|
| 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.
|
| 12 | *
|
| 13 | * Copyright (C) 2019-2022 Maximilian Luz <[email protected]>
|
| 14 |
|
| 15 |
|
| 16 | #include <linux/acpi.h>
|
| 17 | #include <linux/gpio/consumer.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>
|
| 23 |
|
| 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 };
|
| 30 |
|
| 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 },
|
| 38 | { },
|
| 39 | };
|
| 40 |
|
| 41 |
|
| 42 | static const guid_t shps_dsm_guid =
|
| 43 | GUID_INIT(0x5515a847, 0xed55, 0x4b27, 0x83, 0x52, 0xcd, 0x32, 0x0e, 0x10, 0x36, 0x0a);
|
| 44 |
|
| 45 | #define SHPS_DSM_REVISION 1
|
| 46 |
|
| 47 | enum shps_dsm_fn {
|
| 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,
|
| 53 | };
|
| 54 |
|
| 55 | enum shps_irq_type {
|
| 56 |
|
| 57 | SHPS_IRQ_TYPE_BASE_PRESENCE = 0,
|
| 58 | SHPS_IRQ_TYPE_DEVICE_POWER = 1,
|
| 59 | SHPS_IRQ_TYPE_DEVICE_PRESENCE = 2,
|
| 60 | SHPS_NUM_IRQS,
|
| 61 | };
|
| 62 |
|
| 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",
|
| 67 | };
|
| 68 |
|
| 69 | struct shps_device {
|
| 70 | struct mutex lock[SHPS_NUM_IRQS];
|
| 71 | struct gpio_desc *gpio[SHPS_NUM_IRQS];
|
| 72 | unsigned int irq[SHPS_NUM_IRQS];
|
| 73 | };
|
| 74 |
|
| 75 | #define SHPS_IRQ_NOT_PRESENT ((unsigned int)-1)
|
| 76 |
|
| 77 | static enum shps_dsm_fn shps_dsm_fn_for_irq(enum shps_irq_type type)
|
| 78 | {
|
| 79 | return SHPS_DSM_FN_IRQ_BASE_PRESENCE + type;
|
| 80 | }
|
| 81 |
|
| 82 | static void shps_dsm_notify_irq(struct platform_device *pdev, enum shps_irq_type type)
|
| 83 | {
|
| 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;
|
| 88 | int value;
|
| 89 |
|
| 90 | mutex_lock(&sdev->lock[type]);
|
| 91 |
|
| 92 | value = gpiod_get_value_cansleep(sdev->gpio[type]);
|
| 93 | if (value < 0) {
|
| 94 | mutex_unlock(&sdev->lock[type]);
|
| 95 | dev_err(&pdev->dev, "failed to get gpio: %d (irq=%d)\n", type, value);
|
| 96 | return;
|
| 97 | }
|
| 98 |
|
| 99 | dev_dbg(&pdev->dev, "IRQ notification via DSM (irq=%d, value=%d)\n", type, value);
|
| 100 |
|
| 101 | param.type = ACPI_TYPE_INTEGER;
|
| 102 | param.integer.value = value;
|
| 103 |
|
| 104 | result = acpi_evaluate_dsm_typed(handle, &shps_dsm_guid, SHPS_DSM_REVISION,
|
| 105 | shps_dsm_fn_for_irq(type), ¶m, ACPI_TYPE_BUFFER);
|
| 106 | if (!result) {
|
| 107 | dev_err(&pdev->dev, "IRQ notification via DSM failed (irq=%d, gpio=%d)\n",
|
| 108 | type, value);
|
| 109 |
|
| 110 | } else if (result->buffer.length != 1 || result->buffer.pointer[0] != 0) {
|
| 111 | dev_err(&pdev->dev,
|
| 112 | "IRQ notification via DSM failed: unexpected result value (irq=%d, gpio=%d)\n",
|
| 113 | type, value);
|
| 114 | }
|
| 115 |
|
| 116 | mutex_unlock(&sdev->lock[type]);
|
| 117 |
|
| 118 | ACPI_FREE(result);
|
| 119 | }
|
| 120 |
|
| 121 | static irqreturn_t shps_handle_irq(int irq, void *data)
|
| 122 | {
|
| 123 | struct platform_device *pdev = data;
|
| 124 | struct shps_device *sdev = platform_get_drvdata(pdev);
|
| 125 | int type;
|
| 126 |
|
| 127 |
|
| 128 | for (type = 0; type < SHPS_NUM_IRQS; type++)
|
| 129 | if (irq == sdev->irq[type])
|
| 130 | break;
|
| 131 |
|
| 132 |
|
| 133 | if (WARN(type >= SHPS_NUM_IRQS, "invalid IRQ number: %d\n", irq))
|
| 134 | return IRQ_HANDLED;
|
| 135 |
|
| 136 |
|
| 137 | shps_dsm_notify_irq(pdev, type);
|
| 138 | return IRQ_HANDLED;
|
| 139 | }
|
| 140 |
|
| 141 | static int shps_setup_irq(struct platform_device *pdev, enum shps_irq_type type)
|
| 142 | {
|
| 143 | unsigned long flags = IRQF_ONESHOT | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
|
| 144 | struct shps_device *sdev = platform_get_drvdata(pdev);
|
| 145 | struct gpio_desc *gpiod;
|
| 146 | acpi_handle handle = ACPI_HANDLE(&pdev->dev);
|
| 147 | const char *irq_name;
|
| 148 | const int dsm = shps_dsm_fn_for_irq(type);
|
| 149 | int status, irq;
|
| 150 |
|
| 151 | |
| 152 | * Only set up interrupts that we actually need: The Surface Book 3
|
| 153 | * does not have a DSM for base presence, so don't set up an interrupt
|
| 154 | * for that.
|
| 155 |
|
| 156 | if (!acpi_check_dsm(handle, &shps_dsm_guid, SHPS_DSM_REVISION, BIT(dsm))) {
|
| 157 | dev_dbg(&pdev->dev, "IRQ notification via DSM not present (irq=%d)\n", type);
|
| 158 | return 0;
|
| 159 | }
|
| 160 |
|
| 161 | gpiod = devm_gpiod_get(&pdev->dev, shps_gpio_names[type], GPIOD_ASIS);
|
| 162 | if (IS_ERR(gpiod))
|
| 163 | return PTR_ERR(gpiod);
|
| 164 |
|
| 165 | irq = gpiod_to_irq(gpiod);
|
| 166 | if (irq < 0)
|
| 167 | return irq;
|
| 168 |
|
| 169 | irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "shps-irq-%d", type);
|
| 170 | if (!irq_name)
|
| 171 | return -ENOMEM;
|
| 172 |
|
| 173 | status = devm_request_threaded_irq(&pdev->dev, irq, NULL, shps_handle_irq,
|
| 174 | flags, irq_name, pdev);
|
| 175 | if (status)
|
| 176 | return status;
|
| 177 |
|
| 178 | dev_dbg(&pdev->dev, "set up irq %d as type %d\n", irq, type);
|
| 179 |
|
| 180 | sdev->gpio[type] = gpiod;
|
| 181 | sdev->irq[type] = irq;
|
| 182 |
|
| 183 | return 0;
|
| 184 | }
|
| 185 |
|
| 186 | static void surface_hotplug_remove(struct platform_device *pdev)
|
| 187 | {
|
| 188 | struct shps_device *sdev = platform_get_drvdata(pdev);
|
| 189 | int i;
|
| 190 |
|
| 191 |
|
| 192 | for (i = 0; i < SHPS_NUM_IRQS; i++) {
|
| 193 | if (sdev->irq[i] != SHPS_IRQ_NOT_PRESENT)
|
| 194 | disable_irq(sdev->irq[i]);
|
| 195 |
|
| 196 | mutex_destroy(&sdev->lock[i]);
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | static int surface_hotplug_probe(struct platform_device *pdev)
|
| 201 | {
|
| 202 | struct shps_device *sdev;
|
| 203 | int status, i;
|
| 204 |
|
| 205 | |
| 206 | * The MSHW0153 device is also present on the Surface Laptop 3,
|
| 207 | * however that doesn't have a hot-pluggable PCIe device. It also
|
| 208 | * doesn't have any GPIO interrupts/pins under the MSHW0153, so filter
|
| 209 | * it out here.
|
| 210 |
|
| 211 | if (gpiod_count(&pdev->dev, NULL) < 0)
|
| 212 | return -ENODEV;
|
| 213 |
|
| 214 | status = devm_acpi_dev_add_driver_gpios(&pdev->dev, shps_acpi_gpios);
|
| 215 | if (status)
|
| 216 | return status;
|
| 217 |
|
| 218 | sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
|
| 219 | if (!sdev)
|
| 220 | return -ENOMEM;
|
| 221 |
|
| 222 | platform_set_drvdata(pdev, sdev);
|
| 223 |
|
| 224 | |
| 225 | * Initialize IRQs so that we can safely call surface_hotplug_remove()
|
| 226 | * on errors.
|
| 227 |
|
| 228 | for (i = 0; i < SHPS_NUM_IRQS; i++)
|
| 229 | sdev->irq[i] = SHPS_IRQ_NOT_PRESENT;
|
| 230 |
|
| 231 |
|
| 232 | for (i = 0; i < SHPS_NUM_IRQS; i++) {
|
| 233 | mutex_init(&sdev->lock[i]);
|
| 234 |
|
| 235 | status = shps_setup_irq(pdev, i);
|
| 236 | if (status) {
|
| 237 | dev_err(&pdev->dev, "failed to set up IRQ %d: %d\n", i, status);
|
| 238 | goto err;
|
| 239 | }
|
| 240 | }
|
| 241 |
|
| 242 |
|
| 243 | for (i = 0; i < SHPS_NUM_IRQS; i++)
|
| 244 | if (sdev->irq[i] != SHPS_IRQ_NOT_PRESENT)
|
| 245 | shps_dsm_notify_irq(pdev, i);
|
| 246 |
|
| 247 | return 0;
|
| 248 |
|
| 249 | err:
|
| 250 | surface_hotplug_remove(pdev);
|
| 251 | return status;
|
| 252 | }
|
| 253 |
|
| 254 | static const struct acpi_device_id surface_hotplug_acpi_match[] = {
|
| 255 | { "MSHW0153", 0 },
|
| 256 | { },
|
| 257 | };
|
| 258 | MODULE_DEVICE_TABLE(acpi, surface_hotplug_acpi_match);
|
| 259 |
|
| 260 | static struct platform_driver surface_hotplug_driver = {
|
| 261 | .probe = surface_hotplug_probe,
|
| 262 | .remove = surface_hotplug_remove,
|
| 263 | .driver = {
|
| 264 | .name = "surface_hotplug",
|
| 265 | .acpi_match_table = surface_hotplug_acpi_match,
|
| 266 | .probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
| 267 | },
|
| 268 | };
|
| 269 | module_platform_driver(surface_hotplug_driver);
|
| 270 |
|
| 271 | MODULE_AUTHOR("Maximilian Luz <[email protected]>");
|
| 272 | MODULE_DESCRIPTION("Surface Hot-Plug Signaling Driver for Surface Book Devices");
|
| 273 | MODULE_LICENSE("GPL");
|