Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * extcon_gpio.c - Single-state GPIO extcon driver based on extcon class
0004  *
0005  * Copyright (C) 2008 Google, Inc.
0006  * Author: Mike Lockwood <lockwood@android.com>
0007  *
0008  * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon
0009  * (originally switch class is supported)
0010  */
0011 
0012 #include <linux/devm-helpers.h>
0013 #include <linux/extcon-provider.h>
0014 #include <linux/gpio/consumer.h>
0015 #include <linux/init.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/kernel.h>
0018 #include <linux/module.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/slab.h>
0021 #include <linux/workqueue.h>
0022 
0023 /**
0024  * struct gpio_extcon_data - A simple GPIO-controlled extcon device state container.
0025  * @edev:       Extcon device.
0026  * @work:       Work fired by the interrupt.
0027  * @debounce_jiffies:   Number of jiffies to wait for the GPIO to stabilize, from the debounce
0028  *          value.
0029  * @gpiod:      GPIO descriptor for this external connector.
0030  * @extcon_id:      The unique id of specific external connector.
0031  * @debounce:       Debounce time for GPIO IRQ in ms.
0032  * @check_on_resume:    Boolean describing whether to check the state of gpio
0033  *          while resuming from sleep.
0034  */
0035 struct gpio_extcon_data {
0036     struct extcon_dev *edev;
0037     struct delayed_work work;
0038     unsigned long debounce_jiffies;
0039     struct gpio_desc *gpiod;
0040     unsigned int extcon_id;
0041     unsigned long debounce;
0042     bool check_on_resume;
0043 };
0044 
0045 static void gpio_extcon_work(struct work_struct *work)
0046 {
0047     int state;
0048     struct gpio_extcon_data *data =
0049         container_of(to_delayed_work(work), struct gpio_extcon_data,
0050                  work);
0051 
0052     state = gpiod_get_value_cansleep(data->gpiod);
0053     extcon_set_state_sync(data->edev, data->extcon_id, state);
0054 }
0055 
0056 static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
0057 {
0058     struct gpio_extcon_data *data = dev_id;
0059 
0060     queue_delayed_work(system_power_efficient_wq, &data->work,
0061                   data->debounce_jiffies);
0062     return IRQ_HANDLED;
0063 }
0064 
0065 static int gpio_extcon_probe(struct platform_device *pdev)
0066 {
0067     struct gpio_extcon_data *data;
0068     struct device *dev = &pdev->dev;
0069     unsigned long irq_flags;
0070     int irq;
0071     int ret;
0072 
0073     data = devm_kzalloc(dev, sizeof(struct gpio_extcon_data), GFP_KERNEL);
0074     if (!data)
0075         return -ENOMEM;
0076 
0077     /*
0078      * FIXME: extcon_id represents the unique identifier of external
0079      * connectors such as EXTCON_USB, EXTCON_DISP_HDMI and so on. extcon_id
0080      * is necessary to register the extcon device. But, it's not yet
0081      * developed to get the extcon id from device-tree or others.
0082      * On later, it have to be solved.
0083      */
0084     if (data->extcon_id > EXTCON_NONE)
0085         return -EINVAL;
0086 
0087     data->gpiod = devm_gpiod_get(dev, "extcon", GPIOD_IN);
0088     if (IS_ERR(data->gpiod))
0089         return PTR_ERR(data->gpiod);
0090     irq = gpiod_to_irq(data->gpiod);
0091     if (irq <= 0)
0092         return irq;
0093 
0094     /*
0095      * It is unlikely that this is an acknowledged interrupt that goes
0096      * away after handling, what we are looking for are falling edges
0097      * if the signal is active low, and rising edges if the signal is
0098      * active high.
0099      */
0100     if (gpiod_is_active_low(data->gpiod))
0101         irq_flags = IRQF_TRIGGER_FALLING;
0102     else
0103         irq_flags = IRQF_TRIGGER_RISING;
0104 
0105     /* Allocate the memory of extcon devie and register extcon device */
0106     data->edev = devm_extcon_dev_allocate(dev, &data->extcon_id);
0107     if (IS_ERR(data->edev)) {
0108         dev_err(dev, "failed to allocate extcon device\n");
0109         return -ENOMEM;
0110     }
0111 
0112     ret = devm_extcon_dev_register(dev, data->edev);
0113     if (ret < 0)
0114         return ret;
0115 
0116     ret = devm_delayed_work_autocancel(dev, &data->work, gpio_extcon_work);
0117     if (ret)
0118         return ret;
0119 
0120     /*
0121      * Request the interrupt of gpio to detect whether external connector
0122      * is attached or detached.
0123      */
0124     ret = devm_request_any_context_irq(dev, irq,
0125                     gpio_irq_handler, irq_flags,
0126                     pdev->name, data);
0127     if (ret < 0)
0128         return ret;
0129 
0130     platform_set_drvdata(pdev, data);
0131     /* Perform initial detection */
0132     gpio_extcon_work(&data->work.work);
0133 
0134     return 0;
0135 }
0136 
0137 #ifdef CONFIG_PM_SLEEP
0138 static int gpio_extcon_resume(struct device *dev)
0139 {
0140     struct gpio_extcon_data *data;
0141 
0142     data = dev_get_drvdata(dev);
0143     if (data->check_on_resume)
0144         queue_delayed_work(system_power_efficient_wq,
0145             &data->work, data->debounce_jiffies);
0146 
0147     return 0;
0148 }
0149 #endif
0150 
0151 static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
0152 
0153 static struct platform_driver gpio_extcon_driver = {
0154     .probe      = gpio_extcon_probe,
0155     .driver     = {
0156         .name   = "extcon-gpio",
0157         .pm = &gpio_extcon_pm_ops,
0158     },
0159 };
0160 
0161 module_platform_driver(gpio_extcon_driver);
0162 
0163 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
0164 MODULE_DESCRIPTION("GPIO extcon driver");
0165 MODULE_LICENSE("GPL");