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 
48 static void int3496_do_usb_id(struct work_struct *work)
49 {
50 	struct int3496_data *data =
51 		container_of(work, struct int3496_data, work.work);
52 	int id = gpiod_get_value_cansleep(data->gpio_usb_id);
53 
54 	/* id == 1: PERIPHERAL, id == 0: HOST */
55 	dev_dbg(data->dev, "Connected %s cable\n", id ? "PERIPHERAL" : "HOST");
56 
57 	/*
58 	 * Peripheral: set USB mux to peripheral and disable VBUS
59 	 * Host: set USB mux to host and enable VBUS
60 	 */
61 	if (!IS_ERR(data->gpio_usb_mux))
62 		gpiod_direction_output(data->gpio_usb_mux, id);
63 
64 	if (!IS_ERR(data->gpio_vbus_en))
65 		gpiod_direction_output(data->gpio_vbus_en, !id);
66 
67 	extcon_set_state_sync(data->edev, EXTCON_USB_HOST, !id);
68 }
69 
70 static irqreturn_t int3496_thread_isr(int irq, void *priv)
71 {
72 	struct int3496_data *data = priv;
73 
74 	/* Let the pin settle before processing it */
75 	mod_delayed_work(system_wq, &data->work, DEBOUNCE_TIME);
76 
77 	return IRQ_HANDLED;
78 }
79 
80 static int int3496_probe(struct platform_device *pdev)
81 {
82 	struct device *dev = &pdev->dev;
83 	struct int3496_data *data;
84 	int ret;
85 
86 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
87 	if (!data)
88 		return -ENOMEM;
89 
90 	data->dev = dev;
91 	INIT_DELAYED_WORK(&data->work, int3496_do_usb_id);
92 
93 	data->gpio_usb_id = devm_gpiod_get_index(dev, "id",
94 						INT3496_GPIO_USB_ID,
95 						GPIOD_IN);
96 	if (IS_ERR(data->gpio_usb_id)) {
97 		ret = PTR_ERR(data->gpio_usb_id);
98 		dev_err(dev, "can't request USB ID GPIO: %d\n", ret);
99 		return ret;
100 	}
101 
102 	data->usb_id_irq = gpiod_to_irq(data->gpio_usb_id);
103 	if (data->usb_id_irq <= 0) {
104 		dev_err(dev, "can't get USB ID IRQ: %d\n", data->usb_id_irq);
105 		return -EINVAL;
106 	}
107 
108 	data->gpio_vbus_en = devm_gpiod_get_index(dev, "vbus en",
109 						 INT3496_GPIO_VBUS_EN,
110 						 GPIOD_ASIS);
111 	if (IS_ERR(data->gpio_vbus_en))
112 		dev_info(dev, "can't request VBUS EN GPIO\n");
113 
114 	data->gpio_usb_mux = devm_gpiod_get_index(dev, "usb mux",
115 						 INT3496_GPIO_USB_MUX,
116 						 GPIOD_ASIS);
117 	if (IS_ERR(data->gpio_usb_mux))
118 		dev_info(dev, "can't request USB MUX GPIO\n");
119 
120 	/* register extcon device */
121 	data->edev = devm_extcon_dev_allocate(dev, int3496_cable);
122 	if (IS_ERR(data->edev))
123 		return -ENOMEM;
124 
125 	ret = devm_extcon_dev_register(dev, data->edev);
126 	if (ret < 0) {
127 		dev_err(dev, "can't register extcon device: %d\n", ret);
128 		return ret;
129 	}
130 
131 	ret = devm_request_threaded_irq(dev, data->usb_id_irq,
132 					NULL, int3496_thread_isr,
133 					IRQF_SHARED | IRQF_ONESHOT |
134 					IRQF_TRIGGER_RISING |
135 					IRQF_TRIGGER_FALLING,
136 					dev_name(dev), data);
137 	if (ret < 0) {
138 		dev_err(dev, "can't request IRQ for USB ID GPIO: %d\n", ret);
139 		return ret;
140 	}
141 
142 	/* queue initial processing of id-pin */
143 	queue_delayed_work(system_wq, &data->work, 0);
144 
145 	platform_set_drvdata(pdev, data);
146 
147 	return 0;
148 }
149 
150 static int int3496_remove(struct platform_device *pdev)
151 {
152 	struct int3496_data *data = platform_get_drvdata(pdev);
153 
154 	devm_free_irq(&pdev->dev, data->usb_id_irq, data);
155 	cancel_delayed_work_sync(&data->work);
156 
157 	return 0;
158 }
159 
160 static struct acpi_device_id int3496_acpi_match[] = {
161 	{ "INT3496" },
162 	{ }
163 };
164 MODULE_DEVICE_TABLE(acpi, int3496_acpi_match);
165 
166 static struct platform_driver int3496_driver = {
167 	.driver = {
168 		.name = "intel-int3496",
169 		.acpi_match_table = int3496_acpi_match,
170 	},
171 	.probe = int3496_probe,
172 	.remove = int3496_remove,
173 };
174 
175 module_platform_driver(int3496_driver);
176 
177 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
178 MODULE_DESCRIPTION("Intel INT3496 ACPI device extcon driver");
179 MODULE_LICENSE("GPL");
180