Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux/fpc-iii.git] / drivers / extcon / extcon-max3355.c
blobfa01926c09f1683e76edb0504f4baa412f549cd2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Maxim Integrated MAX3355 USB OTG chip extcon driver
5 * Copyright (C) 2014-2015 Cogent Embedded, Inc.
6 * Author: Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>
7 */
9 #include <linux/extcon-provider.h>
10 #include <linux/gpio.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/platform_device.h>
17 struct max3355_data {
18 struct extcon_dev *edev;
19 struct gpio_desc *id_gpiod;
20 struct gpio_desc *shdn_gpiod;
23 static const unsigned int max3355_cable[] = {
24 EXTCON_USB,
25 EXTCON_USB_HOST,
26 EXTCON_NONE,
29 static irqreturn_t max3355_id_irq(int irq, void *dev_id)
31 struct max3355_data *data = dev_id;
32 int id = gpiod_get_value_cansleep(data->id_gpiod);
34 if (id) {
36 * ID = 1 means USB HOST cable detached.
37 * As we don't have event for USB peripheral cable attached,
38 * we simulate USB peripheral attach here.
40 extcon_set_state_sync(data->edev, EXTCON_USB_HOST, false);
41 extcon_set_state_sync(data->edev, EXTCON_USB, true);
42 } else {
44 * ID = 0 means USB HOST cable attached.
45 * As we don't have event for USB peripheral cable detached,
46 * we simulate USB peripheral detach here.
48 extcon_set_state_sync(data->edev, EXTCON_USB, false);
49 extcon_set_state_sync(data->edev, EXTCON_USB_HOST, true);
52 return IRQ_HANDLED;
55 static int max3355_probe(struct platform_device *pdev)
57 struct max3355_data *data;
58 struct gpio_desc *gpiod;
59 int irq, err;
61 data = devm_kzalloc(&pdev->dev, sizeof(struct max3355_data),
62 GFP_KERNEL);
63 if (!data)
64 return -ENOMEM;
66 gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
67 if (IS_ERR(gpiod)) {
68 dev_err(&pdev->dev, "failed to get ID_OUT GPIO\n");
69 return PTR_ERR(gpiod);
71 data->id_gpiod = gpiod;
73 gpiod = devm_gpiod_get(&pdev->dev, "maxim,shdn", GPIOD_OUT_HIGH);
74 if (IS_ERR(gpiod)) {
75 dev_err(&pdev->dev, "failed to get SHDN# GPIO\n");
76 return PTR_ERR(gpiod);
78 data->shdn_gpiod = gpiod;
80 data->edev = devm_extcon_dev_allocate(&pdev->dev, max3355_cable);
81 if (IS_ERR(data->edev)) {
82 dev_err(&pdev->dev, "failed to allocate extcon device\n");
83 return PTR_ERR(data->edev);
86 err = devm_extcon_dev_register(&pdev->dev, data->edev);
87 if (err < 0) {
88 dev_err(&pdev->dev, "failed to register extcon device\n");
89 return err;
92 irq = gpiod_to_irq(data->id_gpiod);
93 if (irq < 0) {
94 dev_err(&pdev->dev, "failed to translate ID_OUT GPIO to IRQ\n");
95 return irq;
98 err = devm_request_threaded_irq(&pdev->dev, irq, NULL, max3355_id_irq,
99 IRQF_ONESHOT | IRQF_NO_SUSPEND |
100 IRQF_TRIGGER_RISING |
101 IRQF_TRIGGER_FALLING,
102 pdev->name, data);
103 if (err < 0) {
104 dev_err(&pdev->dev, "failed to request ID_OUT IRQ\n");
105 return err;
108 platform_set_drvdata(pdev, data);
110 /* Perform initial detection */
111 max3355_id_irq(irq, data);
113 return 0;
116 static int max3355_remove(struct platform_device *pdev)
118 struct max3355_data *data = platform_get_drvdata(pdev);
120 gpiod_set_value_cansleep(data->shdn_gpiod, 0);
122 return 0;
125 static const struct of_device_id max3355_match_table[] = {
126 { .compatible = "maxim,max3355", },
129 MODULE_DEVICE_TABLE(of, max3355_match_table);
131 static struct platform_driver max3355_driver = {
132 .probe = max3355_probe,
133 .remove = max3355_remove,
134 .driver = {
135 .name = "extcon-max3355",
136 .of_match_table = max3355_match_table,
140 module_platform_driver(max3355_driver);
142 MODULE_AUTHOR("Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>");
143 MODULE_DESCRIPTION("Maxim MAX3355 extcon driver");
144 MODULE_LICENSE("GPL v2");