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