^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * extcon_gpio.c - Single-state GPIO extcon driver based on extcon class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2008 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Mike Lockwood <lockwood@android.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * (originally switch class is supported)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/extcon-provider.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * struct gpio_extcon_data - A simple GPIO-controlled extcon device state container.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @edev: Extcon device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @work: Work fired by the interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * @debounce_jiffies: Number of jiffies to wait for the GPIO to stabilize, from the debounce
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * @gpiod: GPIO descriptor for this external connector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @extcon_id: The unique id of specific external connector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * @debounce: Debounce time for GPIO IRQ in ms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * @check_on_resume: Boolean describing whether to check the state of gpio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * while resuming from sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct gpio_extcon_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct extcon_dev *edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct delayed_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned long debounce_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct gpio_desc *gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned int extcon_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned long debounce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) bool check_on_resume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static void gpio_extcon_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct gpio_extcon_data *data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) container_of(to_delayed_work(work), struct gpio_extcon_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) state = gpiod_get_value_cansleep(data->gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) extcon_set_state_sync(data->edev, data->extcon_id, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct gpio_extcon_data *data = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) queue_delayed_work(system_power_efficient_wq, &data->work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) data->debounce_jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int gpio_extcon_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct gpio_extcon_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) unsigned long irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) data = devm_kzalloc(dev, sizeof(struct gpio_extcon_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * FIXME: extcon_id represents the unique identifier of external
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * connectors such as EXTCON_USB, EXTCON_DISP_HDMI and so on. extcon_id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * is necessary to register the extcon device. But, it's not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * developed to get the extcon id from device-tree or others.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * On later, it have to be solved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (data->extcon_id > EXTCON_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) data->gpiod = devm_gpiod_get(dev, "extcon", GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (IS_ERR(data->gpiod))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return PTR_ERR(data->gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) irq = gpiod_to_irq(data->gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (irq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * It is unlikely that this is an acknowledged interrupt that goes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * away after handling, what we are looking for are falling edges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * if the signal is active low, and rising edges if the signal is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * active high.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (gpiod_is_active_low(data->gpiod))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) irq_flags = IRQF_TRIGGER_FALLING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) irq_flags = IRQF_TRIGGER_RISING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* Allocate the memory of extcon devie and register extcon device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) data->edev = devm_extcon_dev_allocate(dev, &data->extcon_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (IS_ERR(data->edev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) dev_err(dev, "failed to allocate extcon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ret = devm_extcon_dev_register(dev, data->edev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) INIT_DELAYED_WORK(&data->work, gpio_extcon_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * Request the interrupt of gpio to detect whether external connector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * is attached or detached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ret = devm_request_any_context_irq(dev, irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) gpio_irq_handler, irq_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) pdev->name, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) platform_set_drvdata(pdev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* Perform initial detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) gpio_extcon_work(&data->work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static int gpio_extcon_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct gpio_extcon_data *data = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) cancel_delayed_work_sync(&data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int gpio_extcon_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct gpio_extcon_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (data->check_on_resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) queue_delayed_work(system_power_efficient_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) &data->work, data->debounce_jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static struct platform_driver gpio_extcon_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .probe = gpio_extcon_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .remove = gpio_extcon_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .name = "extcon-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .pm = &gpio_extcon_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) module_platform_driver(gpio_extcon_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) MODULE_DESCRIPTION("GPIO extcon driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) MODULE_LICENSE("GPL");