Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Maxim Integrated MAX3355 USB OTG chip extcon driver
0004  *
0005  * Copyright (C)  2014-2015 Cogent Embedded, Inc.
0006  * Author: Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>
0007  */
0008 
0009 #include <linux/extcon-provider.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/module.h>
0013 #include <linux/mod_devicetable.h>
0014 #include <linux/platform_device.h>
0015 
0016 struct max3355_data {
0017     struct extcon_dev *edev;
0018     struct gpio_desc *id_gpiod;
0019     struct gpio_desc *shdn_gpiod;
0020 };
0021 
0022 static const unsigned int max3355_cable[] = {
0023     EXTCON_USB,
0024     EXTCON_USB_HOST,
0025     EXTCON_NONE,
0026 };
0027 
0028 static irqreturn_t max3355_id_irq(int irq, void *dev_id)
0029 {
0030     struct max3355_data *data = dev_id;
0031     int id = gpiod_get_value_cansleep(data->id_gpiod);
0032 
0033     if (id) {
0034         /*
0035          * ID = 1 means USB HOST cable detached.
0036          * As we don't have event for USB peripheral cable attached,
0037          * we simulate USB peripheral attach here.
0038          */
0039         extcon_set_state_sync(data->edev, EXTCON_USB_HOST, false);
0040         extcon_set_state_sync(data->edev, EXTCON_USB, true);
0041     } else {
0042         /*
0043          * ID = 0 means USB HOST cable attached.
0044          * As we don't have event for USB peripheral cable detached,
0045          * we simulate USB peripheral detach here.
0046          */
0047         extcon_set_state_sync(data->edev, EXTCON_USB, false);
0048         extcon_set_state_sync(data->edev, EXTCON_USB_HOST, true);
0049     }
0050 
0051     return IRQ_HANDLED;
0052 }
0053 
0054 static int max3355_probe(struct platform_device *pdev)
0055 {
0056     struct max3355_data *data;
0057     struct gpio_desc *gpiod;
0058     int irq, err;
0059 
0060     data = devm_kzalloc(&pdev->dev, sizeof(struct max3355_data),
0061                 GFP_KERNEL);
0062     if (!data)
0063         return -ENOMEM;
0064 
0065     gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
0066     if (IS_ERR(gpiod)) {
0067         dev_err(&pdev->dev, "failed to get ID_OUT GPIO\n");
0068         return PTR_ERR(gpiod);
0069     }
0070     data->id_gpiod = gpiod;
0071 
0072     gpiod = devm_gpiod_get(&pdev->dev, "maxim,shdn", GPIOD_OUT_HIGH);
0073     if (IS_ERR(gpiod)) {
0074         dev_err(&pdev->dev, "failed to get SHDN# GPIO\n");
0075         return PTR_ERR(gpiod);
0076     }
0077     data->shdn_gpiod = gpiod;
0078 
0079     data->edev = devm_extcon_dev_allocate(&pdev->dev, max3355_cable);
0080     if (IS_ERR(data->edev)) {
0081         dev_err(&pdev->dev, "failed to allocate extcon device\n");
0082         return PTR_ERR(data->edev);
0083     }
0084 
0085     err = devm_extcon_dev_register(&pdev->dev, data->edev);
0086     if (err < 0) {
0087         dev_err(&pdev->dev, "failed to register extcon device\n");
0088         return err;
0089     }
0090 
0091     irq = gpiod_to_irq(data->id_gpiod);
0092     if (irq < 0) {
0093         dev_err(&pdev->dev, "failed to translate ID_OUT GPIO to IRQ\n");
0094         return irq;
0095     }
0096 
0097     err = devm_request_threaded_irq(&pdev->dev, irq, NULL, max3355_id_irq,
0098                     IRQF_ONESHOT | IRQF_NO_SUSPEND |
0099                     IRQF_TRIGGER_RISING |
0100                     IRQF_TRIGGER_FALLING,
0101                     pdev->name, data);
0102     if (err < 0) {
0103         dev_err(&pdev->dev, "failed to request ID_OUT IRQ\n");
0104         return err;
0105     }
0106 
0107     platform_set_drvdata(pdev, data);
0108 
0109     /* Perform initial detection */
0110     max3355_id_irq(irq, data);
0111 
0112     return 0;
0113 }
0114 
0115 static int max3355_remove(struct platform_device *pdev)
0116 {
0117     struct max3355_data *data = platform_get_drvdata(pdev);
0118 
0119     gpiod_set_value_cansleep(data->shdn_gpiod, 0);
0120 
0121     return 0;
0122 }
0123 
0124 static const struct of_device_id max3355_match_table[] = {
0125     { .compatible = "maxim,max3355", },
0126     { }
0127 };
0128 MODULE_DEVICE_TABLE(of, max3355_match_table);
0129 
0130 static struct platform_driver max3355_driver = {
0131     .probe      = max3355_probe,
0132     .remove     = max3355_remove,
0133     .driver     = {
0134         .name   = "extcon-max3355",
0135         .of_match_table = max3355_match_table,
0136     },
0137 };
0138 
0139 module_platform_driver(max3355_driver);
0140 
0141 MODULE_AUTHOR("Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>");
0142 MODULE_DESCRIPTION("Maxim MAX3355 extcon driver");
0143 MODULE_LICENSE("GPL v2");