1 /* 2 * Maxim Integrated MAX3355 USB OTG chip extcon driver 3 * 4 * Copyright (C) 2014-2015 Cogent Embedded, Inc. 5 * Author: Sergei Shtylyov <sergei.shtylyov@cogentembedded.com> 6 * 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. 10 */ 11 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> 19 20 struct max3355_data { 21 struct extcon_dev *edev; 22 struct gpio_desc *id_gpiod; 23 struct gpio_desc *shdn_gpiod; 24 }; 25 26 static const unsigned int max3355_cable[] = { 27 EXTCON_USB, 28 EXTCON_USB_HOST, 29 EXTCON_NONE, 30 }; 31 32 static irqreturn_t max3355_id_irq(int irq, void *dev_id) 33 { 34 struct max3355_data *data = dev_id; 35 int id = gpiod_get_value_cansleep(data->id_gpiod); 36 37 if (id) { 38 /* 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. 42 */ 43 extcon_set_state_sync(data->edev, EXTCON_USB_HOST, false); 44 extcon_set_state_sync(data->edev, EXTCON_USB, true); 45 } else { 46 /* 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. 50 */ 51 extcon_set_state_sync(data->edev, EXTCON_USB, false); 52 extcon_set_state_sync(data->edev, EXTCON_USB_HOST, true); 53 } 54 55 return IRQ_HANDLED; 56 } 57 58 static int max3355_probe(struct platform_device *pdev) 59 { 60 struct max3355_data *data; 61 struct gpio_desc *gpiod; 62 int irq, err; 63 64 data = devm_kzalloc(&pdev->dev, sizeof(struct max3355_data), 65 GFP_KERNEL); 66 if (!data) 67 return -ENOMEM; 68 69 gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN); 70 if (IS_ERR(gpiod)) { 71 dev_err(&pdev->dev, "failed to get ID_OUT GPIO\n"); 72 return PTR_ERR(gpiod); 73 } 74 data->id_gpiod = gpiod; 75 76 gpiod = devm_gpiod_get(&pdev->dev, "maxim,shdn", GPIOD_OUT_HIGH); 77 if (IS_ERR(gpiod)) { 78 dev_err(&pdev->dev, "failed to get SHDN# GPIO\n"); 79 return PTR_ERR(gpiod); 80 } 81 data->shdn_gpiod = gpiod; 82 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); 87 } 88 89 err = devm_extcon_dev_register(&pdev->dev, data->edev); 90 if (err < 0) { 91 dev_err(&pdev->dev, "failed to register extcon device\n"); 92 return err; 93 } 94 95 irq = gpiod_to_irq(data->id_gpiod); 96 if (irq < 0) { 97 dev_err(&pdev->dev, "failed to translate ID_OUT GPIO to IRQ\n"); 98 return irq; 99 } 100 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, 105 pdev->name, data); 106 if (err < 0) { 107 dev_err(&pdev->dev, "failed to request ID_OUT IRQ\n"); 108 return err; 109 } 110 111 platform_set_drvdata(pdev, data); 112 113 /* Perform initial detection */ 114 max3355_id_irq(irq, data); 115 116 return 0; 117 } 118 119 static int max3355_remove(struct platform_device *pdev) 120 { 121 struct max3355_data *data = platform_get_drvdata(pdev); 122 123 gpiod_set_value_cansleep(data->shdn_gpiod, 0); 124 125 return 0; 126 } 127 128 static const struct of_device_id max3355_match_table[] = { 129 { .compatible = "maxim,max3355", }, 130 { } 131 }; 132 MODULE_DEVICE_TABLE(of, max3355_match_table); 133 134 static struct platform_driver max3355_driver = { 135 .probe = max3355_probe, 136 .remove = max3355_remove, 137 .driver = { 138 .name = "extcon-max3355", 139 .of_match_table = max3355_match_table, 140 }, 141 }; 142 143 module_platform_driver(max3355_driver); 144 145 MODULE_AUTHOR("Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>"); 146 MODULE_DESCRIPTION("Maxim MAX3355 extcon driver"); 147 MODULE_LICENSE("GPL v2"); 148