David Cohen | 2f556bd | 2016-12-21 12:20:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Intel INT3496 ACPI device extcon driver |
| 3 | * |
| 4 | * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com> |
| 5 | * |
| 6 | * Based on android x86 kernel code which is: |
| 7 | * |
| 8 | * Copyright (c) 2014, Intel Corporation. |
| 9 | * Author: David Cohen <david.a.cohen@linux.intel.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/acpi.h> |
| 22 | #include <linux/extcon.h> |
| 23 | #include <linux/gpio.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | |
| 28 | #define INT3496_GPIO_USB_ID 0 |
| 29 | #define INT3496_GPIO_VBUS_EN 1 |
| 30 | #define INT3496_GPIO_USB_MUX 2 |
| 31 | #define DEBOUNCE_TIME msecs_to_jiffies(50) |
| 32 | |
| 33 | struct int3496_data { |
| 34 | struct device *dev; |
| 35 | struct extcon_dev *edev; |
| 36 | struct delayed_work work; |
| 37 | struct gpio_desc *gpio_usb_id; |
| 38 | struct gpio_desc *gpio_vbus_en; |
| 39 | struct gpio_desc *gpio_usb_mux; |
| 40 | int usb_id_irq; |
| 41 | }; |
| 42 | |
| 43 | static const unsigned int int3496_cable[] = { |
| 44 | EXTCON_USB_HOST, |
| 45 | EXTCON_NONE, |
| 46 | }; |
| 47 | |
Andy Shevchenko | 8cb2cba | 2017-02-24 14:35:55 +0200 | [diff] [blame^] | 48 | static const struct acpi_gpio_params id_gpios = { INT3496_GPIO_USB_ID, 0, false }; |
| 49 | static const struct acpi_gpio_params vbus_gpios = { INT3496_GPIO_VBUS_EN, 0, false }; |
| 50 | static const struct acpi_gpio_params mux_gpios = { INT3496_GPIO_USB_MUX, 0, false }; |
| 51 | |
| 52 | static const struct acpi_gpio_mapping acpi_int3496_default_gpios[] = { |
| 53 | { "id-gpios", &id_gpios, 1 }, |
| 54 | { "vbus-gpios", &vbus_gpios, 1 }, |
| 55 | { "mux-gpios", &mux_gpios, 1 }, |
| 56 | { }, |
| 57 | }; |
| 58 | |
David Cohen | 2f556bd | 2016-12-21 12:20:25 +0100 | [diff] [blame] | 59 | static void int3496_do_usb_id(struct work_struct *work) |
| 60 | { |
| 61 | struct int3496_data *data = |
| 62 | container_of(work, struct int3496_data, work.work); |
| 63 | int id = gpiod_get_value_cansleep(data->gpio_usb_id); |
| 64 | |
| 65 | /* id == 1: PERIPHERAL, id == 0: HOST */ |
| 66 | dev_dbg(data->dev, "Connected %s cable\n", id ? "PERIPHERAL" : "HOST"); |
| 67 | |
| 68 | /* |
| 69 | * Peripheral: set USB mux to peripheral and disable VBUS |
| 70 | * Host: set USB mux to host and enable VBUS |
| 71 | */ |
| 72 | if (!IS_ERR(data->gpio_usb_mux)) |
| 73 | gpiod_direction_output(data->gpio_usb_mux, id); |
| 74 | |
| 75 | if (!IS_ERR(data->gpio_vbus_en)) |
| 76 | gpiod_direction_output(data->gpio_vbus_en, !id); |
| 77 | |
| 78 | extcon_set_state_sync(data->edev, EXTCON_USB_HOST, !id); |
| 79 | } |
| 80 | |
| 81 | static irqreturn_t int3496_thread_isr(int irq, void *priv) |
| 82 | { |
| 83 | struct int3496_data *data = priv; |
| 84 | |
| 85 | /* Let the pin settle before processing it */ |
| 86 | mod_delayed_work(system_wq, &data->work, DEBOUNCE_TIME); |
| 87 | |
| 88 | return IRQ_HANDLED; |
| 89 | } |
| 90 | |
| 91 | static int int3496_probe(struct platform_device *pdev) |
| 92 | { |
| 93 | struct device *dev = &pdev->dev; |
| 94 | struct int3496_data *data; |
| 95 | int ret; |
| 96 | |
Andy Shevchenko | 8cb2cba | 2017-02-24 14:35:55 +0200 | [diff] [blame^] | 97 | ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), |
| 98 | acpi_int3496_default_gpios); |
| 99 | if (ret) { |
| 100 | dev_err(dev, "can't add GPIO ACPI mapping\n"); |
| 101 | return ret; |
| 102 | } |
| 103 | |
David Cohen | 2f556bd | 2016-12-21 12:20:25 +0100 | [diff] [blame] | 104 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 105 | if (!data) |
| 106 | return -ENOMEM; |
| 107 | |
| 108 | data->dev = dev; |
| 109 | INIT_DELAYED_WORK(&data->work, int3496_do_usb_id); |
| 110 | |
| 111 | data->gpio_usb_id = devm_gpiod_get_index(dev, "id", |
| 112 | INT3496_GPIO_USB_ID, |
| 113 | GPIOD_IN); |
| 114 | if (IS_ERR(data->gpio_usb_id)) { |
| 115 | ret = PTR_ERR(data->gpio_usb_id); |
| 116 | dev_err(dev, "can't request USB ID GPIO: %d\n", ret); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | data->usb_id_irq = gpiod_to_irq(data->gpio_usb_id); |
Andy Shevchenko | bafa687 | 2017-02-23 12:31:54 +0200 | [diff] [blame] | 121 | if (data->usb_id_irq < 0) { |
David Cohen | 2f556bd | 2016-12-21 12:20:25 +0100 | [diff] [blame] | 122 | dev_err(dev, "can't get USB ID IRQ: %d\n", data->usb_id_irq); |
Andy Shevchenko | bafa687 | 2017-02-23 12:31:54 +0200 | [diff] [blame] | 123 | return data->usb_id_irq; |
David Cohen | 2f556bd | 2016-12-21 12:20:25 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Andy Shevchenko | 90400c5 | 2017-02-24 14:35:54 +0200 | [diff] [blame] | 126 | data->gpio_vbus_en = devm_gpiod_get_index(dev, "vbus", |
David Cohen | 2f556bd | 2016-12-21 12:20:25 +0100 | [diff] [blame] | 127 | INT3496_GPIO_VBUS_EN, |
| 128 | GPIOD_ASIS); |
| 129 | if (IS_ERR(data->gpio_vbus_en)) |
| 130 | dev_info(dev, "can't request VBUS EN GPIO\n"); |
| 131 | |
Andy Shevchenko | 90400c5 | 2017-02-24 14:35:54 +0200 | [diff] [blame] | 132 | data->gpio_usb_mux = devm_gpiod_get_index(dev, "mux", |
David Cohen | 2f556bd | 2016-12-21 12:20:25 +0100 | [diff] [blame] | 133 | INT3496_GPIO_USB_MUX, |
| 134 | GPIOD_ASIS); |
| 135 | if (IS_ERR(data->gpio_usb_mux)) |
| 136 | dev_info(dev, "can't request USB MUX GPIO\n"); |
| 137 | |
| 138 | /* register extcon device */ |
| 139 | data->edev = devm_extcon_dev_allocate(dev, int3496_cable); |
| 140 | if (IS_ERR(data->edev)) |
| 141 | return -ENOMEM; |
| 142 | |
| 143 | ret = devm_extcon_dev_register(dev, data->edev); |
| 144 | if (ret < 0) { |
| 145 | dev_err(dev, "can't register extcon device: %d\n", ret); |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | ret = devm_request_threaded_irq(dev, data->usb_id_irq, |
| 150 | NULL, int3496_thread_isr, |
| 151 | IRQF_SHARED | IRQF_ONESHOT | |
| 152 | IRQF_TRIGGER_RISING | |
| 153 | IRQF_TRIGGER_FALLING, |
| 154 | dev_name(dev), data); |
| 155 | if (ret < 0) { |
| 156 | dev_err(dev, "can't request IRQ for USB ID GPIO: %d\n", ret); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | /* queue initial processing of id-pin */ |
| 161 | queue_delayed_work(system_wq, &data->work, 0); |
| 162 | |
| 163 | platform_set_drvdata(pdev, data); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int int3496_remove(struct platform_device *pdev) |
| 169 | { |
| 170 | struct int3496_data *data = platform_get_drvdata(pdev); |
| 171 | |
| 172 | devm_free_irq(&pdev->dev, data->usb_id_irq, data); |
| 173 | cancel_delayed_work_sync(&data->work); |
| 174 | |
Andy Shevchenko | 8cb2cba | 2017-02-24 14:35:55 +0200 | [diff] [blame^] | 175 | acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev)); |
| 176 | |
David Cohen | 2f556bd | 2016-12-21 12:20:25 +0100 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static struct acpi_device_id int3496_acpi_match[] = { |
| 181 | { "INT3496" }, |
| 182 | { } |
| 183 | }; |
| 184 | MODULE_DEVICE_TABLE(acpi, int3496_acpi_match); |
| 185 | |
| 186 | static struct platform_driver int3496_driver = { |
| 187 | .driver = { |
| 188 | .name = "intel-int3496", |
| 189 | .acpi_match_table = int3496_acpi_match, |
| 190 | }, |
| 191 | .probe = int3496_probe, |
| 192 | .remove = int3496_remove, |
| 193 | }; |
| 194 | |
| 195 | module_platform_driver(int3496_driver); |
| 196 | |
| 197 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); |
| 198 | MODULE_DESCRIPTION("Intel INT3496 ACPI device extcon driver"); |
| 199 | MODULE_LICENSE("GPL"); |