2 * Maxim Integrated MAX3355 USB OTG chip extcon driver
4 * Copyright (C) 2014-2015 Cogent Embedded, Inc.
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
12 #include <linux/extcon-provider.h>
13 #include <linux/gpio.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/platform_device.h>
21 struct extcon_dev *edev;
22 struct gpio_desc *id_gpiod;
23 struct gpio_desc *shdn_gpiod;
26 static const unsigned int max3355_cable[] = {
32 static irqreturn_t max3355_id_irq(int irq, void *dev_id)
34 struct max3355_data *data = dev_id;
35 int id = gpiod_get_value_cansleep(data->id_gpiod);
39 * ID = 1 means USB HOST cable detached.
40 * As we don't have event for USB peripheral cable attached,
41 * we simulate USB peripheral attach here.
43 extcon_set_state_sync(data->edev, EXTCON_USB_HOST, false);
44 extcon_set_state_sync(data->edev, EXTCON_USB, true);
47 * ID = 0 means USB HOST cable attached.
48 * As we don't have event for USB peripheral cable detached,
49 * we simulate USB peripheral detach here.
51 extcon_set_state_sync(data->edev, EXTCON_USB, false);
52 extcon_set_state_sync(data->edev, EXTCON_USB_HOST, true);
58 static int max3355_probe(struct platform_device *pdev)
60 struct max3355_data *data;
61 struct gpio_desc *gpiod;
64 data = devm_kzalloc(&pdev->dev, sizeof(struct max3355_data),
69 gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
71 dev_err(&pdev->dev, "failed to get ID_OUT GPIO\n");
72 return PTR_ERR(gpiod);
74 data->id_gpiod = gpiod;
76 gpiod = devm_gpiod_get(&pdev->dev, "maxim,shdn", GPIOD_OUT_HIGH);
78 dev_err(&pdev->dev, "failed to get SHDN# GPIO\n");
79 return PTR_ERR(gpiod);
81 data->shdn_gpiod = gpiod;
83 data->edev = devm_extcon_dev_allocate(&pdev->dev, max3355_cable);
84 if (IS_ERR(data->edev)) {
85 dev_err(&pdev->dev, "failed to allocate extcon device\n");
86 return PTR_ERR(data->edev);
89 err = devm_extcon_dev_register(&pdev->dev, data->edev);
91 dev_err(&pdev->dev, "failed to register extcon device\n");
95 irq = gpiod_to_irq(data->id_gpiod);
97 dev_err(&pdev->dev, "failed to translate ID_OUT GPIO to IRQ\n");
101 err = devm_request_threaded_irq(&pdev->dev, irq, NULL, max3355_id_irq,
102 IRQF_ONESHOT | IRQF_NO_SUSPEND |
103 IRQF_TRIGGER_RISING |
104 IRQF_TRIGGER_FALLING,
107 dev_err(&pdev->dev, "failed to request ID_OUT IRQ\n");
111 platform_set_drvdata(pdev, data);
113 /* Perform initial detection */
114 max3355_id_irq(irq, data);
119 static int max3355_remove(struct platform_device *pdev)
121 struct max3355_data *data = platform_get_drvdata(pdev);
123 gpiod_set_value_cansleep(data->shdn_gpiod, 0);
128 static const struct of_device_id max3355_match_table[] = {
129 { .compatible = "maxim,max3355", },
132 MODULE_DEVICE_TABLE(of, max3355_match_table);
134 static struct platform_driver max3355_driver = {
135 .probe = max3355_probe,
136 .remove = max3355_remove,
138 .name = "extcon-max3355",
139 .of_match_table = max3355_match_table,
143 module_platform_driver(max3355_driver);
146 MODULE_DESCRIPTION("Maxim MAX3355 extcon driver");
147 MODULE_LICENSE("GPL v2");