Blame view

drivers/extcon/extcon-intel-int3496.c 4.9 KB
2e464ff0a   Andy Shevchenko   extcon: int3496: ...
1
  // SPDX-License-Identifier: GPL-2.0
2f556bdb9   David Cohen   extcon: int3496: ...
2
3
4
5
6
7
8
9
10
  /*
   * Intel INT3496 ACPI device extcon driver
   *
   * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com>
   *
   * Based on android x86 kernel code which is:
   *
   * Copyright (c) 2014, Intel Corporation.
   * Author: David Cohen <david.a.cohen@linux.intel.com>
2f556bdb9   David Cohen   extcon: int3496: ...
11
12
13
   */
  
  #include <linux/acpi.h>
176aa3601   Chanwoo Choi   extcon: Split out...
14
  #include <linux/extcon-provider.h>
c355bb1af   Wolfram Sang   extcon: int3496: ...
15
  #include <linux/gpio/consumer.h>
2f556bdb9   David Cohen   extcon: int3496: ...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  #include <linux/interrupt.h>
  #include <linux/module.h>
  #include <linux/platform_device.h>
  
  #define INT3496_GPIO_USB_ID	0
  #define INT3496_GPIO_VBUS_EN	1
  #define INT3496_GPIO_USB_MUX	2
  #define DEBOUNCE_TIME		msecs_to_jiffies(50)
  
  struct int3496_data {
  	struct device *dev;
  	struct extcon_dev *edev;
  	struct delayed_work work;
  	struct gpio_desc *gpio_usb_id;
  	struct gpio_desc *gpio_vbus_en;
  	struct gpio_desc *gpio_usb_mux;
  	int usb_id_irq;
  };
  
  static const unsigned int int3496_cable[] = {
  	EXTCON_USB_HOST,
  	EXTCON_NONE,
  };
8cb2cbae4   Andy Shevchenko   extcon: int3496: ...
39
40
41
42
43
  static const struct acpi_gpio_params id_gpios = { INT3496_GPIO_USB_ID, 0, false };
  static const struct acpi_gpio_params vbus_gpios = { INT3496_GPIO_VBUS_EN, 0, false };
  static const struct acpi_gpio_params mux_gpios = { INT3496_GPIO_USB_MUX, 0, false };
  
  static const struct acpi_gpio_mapping acpi_int3496_default_gpios[] = {
eca0f13c8   Andy Shevchenko   extcon: int3496: ...
44
45
46
47
48
  	/*
  	 * Some platforms have a bug in ACPI GPIO description making IRQ
  	 * GPIO to be output only. Ask the GPIO core to ignore this limit.
  	 */
  	{ "id-gpios", &id_gpios, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION },
8cb2cbae4   Andy Shevchenko   extcon: int3496: ...
49
50
51
52
  	{ "vbus-gpios", &vbus_gpios, 1 },
  	{ "mux-gpios", &mux_gpios, 1 },
  	{ },
  };
2f556bdb9   David Cohen   extcon: int3496: ...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  static void int3496_do_usb_id(struct work_struct *work)
  {
  	struct int3496_data *data =
  		container_of(work, struct int3496_data, work.work);
  	int id = gpiod_get_value_cansleep(data->gpio_usb_id);
  
  	/* id == 1: PERIPHERAL, id == 0: HOST */
  	dev_dbg(data->dev, "Connected %s cable
  ", id ? "PERIPHERAL" : "HOST");
  
  	/*
  	 * Peripheral: set USB mux to peripheral and disable VBUS
  	 * Host: set USB mux to host and enable VBUS
  	 */
  	if (!IS_ERR(data->gpio_usb_mux))
  		gpiod_direction_output(data->gpio_usb_mux, id);
  
  	if (!IS_ERR(data->gpio_vbus_en))
  		gpiod_direction_output(data->gpio_vbus_en, !id);
  
  	extcon_set_state_sync(data->edev, EXTCON_USB_HOST, !id);
  }
  
  static irqreturn_t int3496_thread_isr(int irq, void *priv)
  {
  	struct int3496_data *data = priv;
  
  	/* Let the pin settle before processing it */
  	mod_delayed_work(system_wq, &data->work, DEBOUNCE_TIME);
  
  	return IRQ_HANDLED;
  }
  
  static int int3496_probe(struct platform_device *pdev)
  {
  	struct device *dev = &pdev->dev;
  	struct int3496_data *data;
  	int ret;
1f4be2478   Andy Shevchenko   extcon: int3496: ...
91
  	ret = devm_acpi_dev_add_driver_gpios(dev, acpi_int3496_default_gpios);
8cb2cbae4   Andy Shevchenko   extcon: int3496: ...
92
93
94
95
96
  	if (ret) {
  		dev_err(dev, "can't add GPIO ACPI mapping
  ");
  		return ret;
  	}
2f556bdb9   David Cohen   extcon: int3496: ...
97
98
99
100
101
102
  	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  	if (!data)
  		return -ENOMEM;
  
  	data->dev = dev;
  	INIT_DELAYED_WORK(&data->work, int3496_do_usb_id);
408c5b41d   Hans de Goede   extcon: int3496: ...
103
  	data->gpio_usb_id = devm_gpiod_get(dev, "id", GPIOD_IN);
2f556bdb9   David Cohen   extcon: int3496: ...
104
105
106
107
108
109
110
111
  	if (IS_ERR(data->gpio_usb_id)) {
  		ret = PTR_ERR(data->gpio_usb_id);
  		dev_err(dev, "can't request USB ID GPIO: %d
  ", ret);
  		return ret;
  	}
  
  	data->usb_id_irq = gpiod_to_irq(data->gpio_usb_id);
bafa687dc   Andy Shevchenko   extcon: int3496: ...
112
  	if (data->usb_id_irq < 0) {
2f556bdb9   David Cohen   extcon: int3496: ...
113
114
  		dev_err(dev, "can't get USB ID IRQ: %d
  ", data->usb_id_irq);
bafa687dc   Andy Shevchenko   extcon: int3496: ...
115
  		return data->usb_id_irq;
2f556bdb9   David Cohen   extcon: int3496: ...
116
  	}
408c5b41d   Hans de Goede   extcon: int3496: ...
117
  	data->gpio_vbus_en = devm_gpiod_get(dev, "vbus", GPIOD_ASIS);
2f556bdb9   David Cohen   extcon: int3496: ...
118
119
120
  	if (IS_ERR(data->gpio_vbus_en))
  		dev_info(dev, "can't request VBUS EN GPIO
  ");
408c5b41d   Hans de Goede   extcon: int3496: ...
121
  	data->gpio_usb_mux = devm_gpiod_get(dev, "mux", GPIOD_ASIS);
2f556bdb9   David Cohen   extcon: int3496: ...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  	if (IS_ERR(data->gpio_usb_mux))
  		dev_info(dev, "can't request USB MUX GPIO
  ");
  
  	/* register extcon device */
  	data->edev = devm_extcon_dev_allocate(dev, int3496_cable);
  	if (IS_ERR(data->edev))
  		return -ENOMEM;
  
  	ret = devm_extcon_dev_register(dev, data->edev);
  	if (ret < 0) {
  		dev_err(dev, "can't register extcon device: %d
  ", ret);
  		return ret;
  	}
  
  	ret = devm_request_threaded_irq(dev, data->usb_id_irq,
  					NULL, int3496_thread_isr,
  					IRQF_SHARED | IRQF_ONESHOT |
  					IRQF_TRIGGER_RISING |
  					IRQF_TRIGGER_FALLING,
  					dev_name(dev), data);
  	if (ret < 0) {
  		dev_err(dev, "can't request IRQ for USB ID GPIO: %d
  ", ret);
  		return ret;
  	}
0434352d3   Hans de Goede   extcon: int3496: ...
149
  	/* process id-pin so that we start with the right status */
2f556bdb9   David Cohen   extcon: int3496: ...
150
  	queue_delayed_work(system_wq, &data->work, 0);
0434352d3   Hans de Goede   extcon: int3496: ...
151
  	flush_delayed_work(&data->work);
2f556bdb9   David Cohen   extcon: int3496: ...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  
  	platform_set_drvdata(pdev, data);
  
  	return 0;
  }
  
  static int int3496_remove(struct platform_device *pdev)
  {
  	struct int3496_data *data = platform_get_drvdata(pdev);
  
  	devm_free_irq(&pdev->dev, data->usb_id_irq, data);
  	cancel_delayed_work_sync(&data->work);
  
  	return 0;
  }
ff890bc00   Arvind Yadav   extcon: int3496: ...
167
  static const struct acpi_device_id int3496_acpi_match[] = {
2f556bdb9   David Cohen   extcon: int3496: ...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  	{ "INT3496" },
  	{ }
  };
  MODULE_DEVICE_TABLE(acpi, int3496_acpi_match);
  
  static struct platform_driver int3496_driver = {
  	.driver = {
  		.name = "intel-int3496",
  		.acpi_match_table = int3496_acpi_match,
  	},
  	.probe = int3496_probe,
  	.remove = int3496_remove,
  };
  
  module_platform_driver(int3496_driver);
  
  MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  MODULE_DESCRIPTION("Intel INT3496 ACPI device extcon driver");
2e464ff0a   Andy Shevchenko   extcon: int3496: ...
186
  MODULE_LICENSE("GPL v2");