1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Maxim Integrated MAX3355 USB OTG chip extcon driver 4 * 5 * Copyright (C) 2014-2015 Cogent Embedded, Inc. 6 * Author: Sergei Shtylyov <sergei.shtylyov@cogentembedded.com> 7 */ 8 9 #include <linux/extcon-provider.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/interrupt.h> 12 #include <linux/module.h> 13 #include <linux/platform_device.h> 14 15 struct max3355_data { 16 struct extcon_dev *edev; 17 struct gpio_desc *id_gpiod; 18 struct gpio_desc *shdn_gpiod; 19 }; 20 21 static const unsigned int max3355_cable[] = { 22 EXTCON_USB, 23 EXTCON_USB_HOST, 24 EXTCON_NONE, 25 }; 26 27 static irqreturn_t max3355_id_irq(int irq, void *dev_id) 28 { 29 struct max3355_data *data = dev_id; 30 int id = gpiod_get_value_cansleep(data->id_gpiod); 31 32 if (id) { 33 /* 34 * ID = 1 means USB HOST cable detached. 35 * As we don't have event for USB peripheral cable attached, 36 * we simulate USB peripheral attach here. 37 */ 38 extcon_set_state_sync(data->edev, EXTCON_USB_HOST, false); 39 extcon_set_state_sync(data->edev, EXTCON_USB, true); 40 } else { 41 /* 42 * ID = 0 means USB HOST cable attached. 43 * As we don't have event for USB peripheral cable detached, 44 * we simulate USB peripheral detach here. 45 */ 46 extcon_set_state_sync(data->edev, EXTCON_USB, false); 47 extcon_set_state_sync(data->edev, EXTCON_USB_HOST, true); 48 } 49 50 return IRQ_HANDLED; 51 } 52 53 static int max3355_probe(struct platform_device *pdev) 54 { 55 struct max3355_data *data; 56 struct gpio_desc *gpiod; 57 int irq, err; 58 59 data = devm_kzalloc(&pdev->dev, sizeof(struct max3355_data), 60 GFP_KERNEL); 61 if (!data) 62 return -ENOMEM; 63 64 gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN); 65 if (IS_ERR(gpiod)) { 66 dev_err(&pdev->dev, "failed to get ID_OUT GPIO\n"); 67 return PTR_ERR(gpiod); 68 } 69 data->id_gpiod = gpiod; 70 71 gpiod = devm_gpiod_get(&pdev->dev, "maxim,shdn", GPIOD_OUT_HIGH); 72 if (IS_ERR(gpiod)) { 73 dev_err(&pdev->dev, "failed to get SHDN# GPIO\n"); 74 return PTR_ERR(gpiod); 75 } 76 data->shdn_gpiod = gpiod; 77 78 data->edev = devm_extcon_dev_allocate(&pdev->dev, max3355_cable); 79 if (IS_ERR(data->edev)) { 80 dev_err(&pdev->dev, "failed to allocate extcon device\n"); 81 return PTR_ERR(data->edev); 82 } 83 84 err = devm_extcon_dev_register(&pdev->dev, data->edev); 85 if (err < 0) { 86 dev_err(&pdev->dev, "failed to register extcon device\n"); 87 return err; 88 } 89 90 irq = gpiod_to_irq(data->id_gpiod); 91 if (irq < 0) { 92 dev_err(&pdev->dev, "failed to translate ID_OUT GPIO to IRQ\n"); 93 return irq; 94 } 95 96 err = devm_request_threaded_irq(&pdev->dev, irq, NULL, max3355_id_irq, 97 IRQF_ONESHOT | IRQF_NO_SUSPEND | 98 IRQF_TRIGGER_RISING | 99 IRQF_TRIGGER_FALLING, 100 pdev->name, data); 101 if (err < 0) { 102 dev_err(&pdev->dev, "failed to request ID_OUT IRQ\n"); 103 return err; 104 } 105 106 platform_set_drvdata(pdev, data); 107 108 /* Perform initial detection */ 109 max3355_id_irq(irq, data); 110 111 return 0; 112 } 113 114 static void max3355_remove(struct platform_device *pdev) 115 { 116 struct max3355_data *data = platform_get_drvdata(pdev); 117 118 gpiod_set_value_cansleep(data->shdn_gpiod, 0); 119 } 120 121 static const struct of_device_id max3355_match_table[] = { 122 { .compatible = "maxim,max3355", }, 123 { } 124 }; 125 MODULE_DEVICE_TABLE(of, max3355_match_table); 126 127 static struct platform_driver max3355_driver = { 128 .probe = max3355_probe, 129 .remove = max3355_remove, 130 .driver = { 131 .name = "extcon-max3355", 132 .of_match_table = max3355_match_table, 133 }, 134 }; 135 136 module_platform_driver(max3355_driver); 137 138 MODULE_AUTHOR("Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>"); 139 MODULE_DESCRIPTION("Maxim MAX3355 extcon driver"); 140 MODULE_LICENSE("GPL v2"); 141