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