0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/acpi.h>
0014 #include <linux/devm-helpers.h>
0015 #include <linux/extcon-provider.h>
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/module.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/regulator/consumer.h>
0021
0022 #define INT3496_GPIO_USB_ID 0
0023 #define INT3496_GPIO_VBUS_EN 1
0024 #define INT3496_GPIO_USB_MUX 2
0025 #define DEBOUNCE_TIME msecs_to_jiffies(50)
0026
0027 struct int3496_data {
0028 struct device *dev;
0029 struct extcon_dev *edev;
0030 struct delayed_work work;
0031 struct gpio_desc *gpio_usb_id;
0032 struct gpio_desc *gpio_vbus_en;
0033 struct gpio_desc *gpio_usb_mux;
0034 struct regulator *vbus_boost;
0035 int usb_id_irq;
0036 bool vbus_boost_enabled;
0037 };
0038
0039 static const unsigned int int3496_cable[] = {
0040 EXTCON_USB_HOST,
0041 EXTCON_NONE,
0042 };
0043
0044 static const struct acpi_gpio_params id_gpios = { INT3496_GPIO_USB_ID, 0, false };
0045 static const struct acpi_gpio_params vbus_gpios = { INT3496_GPIO_VBUS_EN, 0, false };
0046 static const struct acpi_gpio_params mux_gpios = { INT3496_GPIO_USB_MUX, 0, false };
0047
0048 static const struct acpi_gpio_mapping acpi_int3496_default_gpios[] = {
0049
0050
0051
0052
0053 { "id-gpios", &id_gpios, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION },
0054 { "vbus-gpios", &vbus_gpios, 1 },
0055 { "mux-gpios", &mux_gpios, 1 },
0056 { },
0057 };
0058
0059 static void int3496_set_vbus_boost(struct int3496_data *data, bool enable)
0060 {
0061 int ret;
0062
0063 if (IS_ERR_OR_NULL(data->vbus_boost))
0064 return;
0065
0066 if (data->vbus_boost_enabled == enable)
0067 return;
0068
0069 if (enable)
0070 ret = regulator_enable(data->vbus_boost);
0071 else
0072 ret = regulator_disable(data->vbus_boost);
0073
0074 if (ret == 0)
0075 data->vbus_boost_enabled = enable;
0076 else
0077 dev_err(data->dev, "Error updating Vbus boost regulator: %d\n", ret);
0078 }
0079
0080 static void int3496_do_usb_id(struct work_struct *work)
0081 {
0082 struct int3496_data *data =
0083 container_of(work, struct int3496_data, work.work);
0084 int id = gpiod_get_value_cansleep(data->gpio_usb_id);
0085
0086
0087 dev_dbg(data->dev, "Connected %s cable\n", id ? "PERIPHERAL" : "HOST");
0088
0089
0090
0091
0092
0093 if (!IS_ERR(data->gpio_usb_mux))
0094 gpiod_direction_output(data->gpio_usb_mux, id);
0095
0096 if (!IS_ERR(data->gpio_vbus_en))
0097 gpiod_direction_output(data->gpio_vbus_en, !id);
0098 else
0099 int3496_set_vbus_boost(data, !id);
0100
0101 extcon_set_state_sync(data->edev, EXTCON_USB_HOST, !id);
0102 }
0103
0104 static irqreturn_t int3496_thread_isr(int irq, void *priv)
0105 {
0106 struct int3496_data *data = priv;
0107
0108
0109 mod_delayed_work(system_wq, &data->work, DEBOUNCE_TIME);
0110
0111 return IRQ_HANDLED;
0112 }
0113
0114 static int int3496_probe(struct platform_device *pdev)
0115 {
0116 struct device *dev = &pdev->dev;
0117 struct int3496_data *data;
0118 int ret;
0119
0120 if (has_acpi_companion(dev)) {
0121 ret = devm_acpi_dev_add_driver_gpios(dev, acpi_int3496_default_gpios);
0122 if (ret) {
0123 dev_err(dev, "can't add GPIO ACPI mapping\n");
0124 return ret;
0125 }
0126 }
0127
0128 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0129 if (!data)
0130 return -ENOMEM;
0131
0132 data->dev = dev;
0133 ret = devm_delayed_work_autocancel(dev, &data->work, int3496_do_usb_id);
0134 if (ret)
0135 return ret;
0136
0137 data->gpio_usb_id =
0138 devm_gpiod_get(dev, "id", GPIOD_IN | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
0139 if (IS_ERR(data->gpio_usb_id)) {
0140 ret = PTR_ERR(data->gpio_usb_id);
0141 dev_err(dev, "can't request USB ID GPIO: %d\n", ret);
0142 return ret;
0143 }
0144
0145 data->usb_id_irq = gpiod_to_irq(data->gpio_usb_id);
0146 if (data->usb_id_irq < 0) {
0147 dev_err(dev, "can't get USB ID IRQ: %d\n", data->usb_id_irq);
0148 return data->usb_id_irq;
0149 }
0150
0151 data->gpio_vbus_en = devm_gpiod_get(dev, "vbus", GPIOD_ASIS);
0152 if (IS_ERR(data->gpio_vbus_en)) {
0153 dev_dbg(dev, "can't request VBUS EN GPIO\n");
0154 data->vbus_boost = devm_regulator_get_optional(dev, "vbus");
0155 }
0156
0157 data->gpio_usb_mux = devm_gpiod_get(dev, "mux", GPIOD_ASIS);
0158 if (IS_ERR(data->gpio_usb_mux))
0159 dev_dbg(dev, "can't request USB MUX GPIO\n");
0160
0161
0162 data->edev = devm_extcon_dev_allocate(dev, int3496_cable);
0163 if (IS_ERR(data->edev))
0164 return -ENOMEM;
0165
0166 ret = devm_extcon_dev_register(dev, data->edev);
0167 if (ret < 0) {
0168 dev_err(dev, "can't register extcon device: %d\n", ret);
0169 return ret;
0170 }
0171
0172 ret = devm_request_threaded_irq(dev, data->usb_id_irq,
0173 NULL, int3496_thread_isr,
0174 IRQF_SHARED | IRQF_ONESHOT |
0175 IRQF_TRIGGER_RISING |
0176 IRQF_TRIGGER_FALLING,
0177 dev_name(dev), data);
0178 if (ret < 0) {
0179 dev_err(dev, "can't request IRQ for USB ID GPIO: %d\n", ret);
0180 return ret;
0181 }
0182
0183
0184 queue_delayed_work(system_wq, &data->work, 0);
0185 flush_delayed_work(&data->work);
0186
0187 platform_set_drvdata(pdev, data);
0188
0189 return 0;
0190 }
0191
0192 static const struct acpi_device_id int3496_acpi_match[] = {
0193 { "INT3496" },
0194 { }
0195 };
0196 MODULE_DEVICE_TABLE(acpi, int3496_acpi_match);
0197
0198 static const struct platform_device_id int3496_ids[] = {
0199 { .name = "intel-int3496" },
0200 {},
0201 };
0202 MODULE_DEVICE_TABLE(platform, int3496_ids);
0203
0204 static struct platform_driver int3496_driver = {
0205 .driver = {
0206 .name = "intel-int3496",
0207 .acpi_match_table = int3496_acpi_match,
0208 },
0209 .probe = int3496_probe,
0210 .id_table = int3496_ids,
0211 };
0212
0213 module_platform_driver(int3496_driver);
0214
0215 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
0216 MODULE_DESCRIPTION("Intel INT3496 ACPI device extcon driver");
0217 MODULE_LICENSE("GPL v2");