xref: /linux/drivers/extcon/extcon-gpio.c (revision c4ee0af3fa0dc65f690fc908f02b8355f9576ea0)
1 /*
2  *  drivers/extcon/extcon_gpio.c
3  *
4  *  Single-state GPIO extcon driver based on extcon class
5  *
6  * Copyright (C) 2008 Google, Inc.
7  * Author: Mike Lockwood <lockwood@android.com>
8  *
9  * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon
10  * (originally switch class is supported)
11  *
12  * This software is licensed under the terms of the GNU General Public
13  * License version 2, as published by the Free Software Foundation, and
14  * may be copied, distributed, and modified under those terms.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21 */
22 
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/workqueue.h>
30 #include <linux/gpio.h>
31 #include <linux/extcon.h>
32 #include <linux/extcon/extcon-gpio.h>
33 
34 struct gpio_extcon_data {
35 	struct extcon_dev edev;
36 	unsigned gpio;
37 	bool gpio_active_low;
38 	const char *state_on;
39 	const char *state_off;
40 	int irq;
41 	struct delayed_work work;
42 	unsigned long debounce_jiffies;
43 };
44 
45 static void gpio_extcon_work(struct work_struct *work)
46 {
47 	int state;
48 	struct gpio_extcon_data	*data =
49 		container_of(to_delayed_work(work), struct gpio_extcon_data,
50 			     work);
51 
52 	state = gpio_get_value(data->gpio);
53 	if (data->gpio_active_low)
54 		state = !state;
55 	extcon_set_state(&data->edev, state);
56 }
57 
58 static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
59 {
60 	struct gpio_extcon_data *extcon_data = dev_id;
61 
62 	queue_delayed_work(system_power_efficient_wq, &extcon_data->work,
63 			      extcon_data->debounce_jiffies);
64 	return IRQ_HANDLED;
65 }
66 
67 static ssize_t extcon_gpio_print_state(struct extcon_dev *edev, char *buf)
68 {
69 	struct gpio_extcon_data	*extcon_data =
70 		container_of(edev, struct gpio_extcon_data, edev);
71 	const char *state;
72 	if (extcon_get_state(edev))
73 		state = extcon_data->state_on;
74 	else
75 		state = extcon_data->state_off;
76 
77 	if (state)
78 		return sprintf(buf, "%s\n", state);
79 	return -EINVAL;
80 }
81 
82 static int gpio_extcon_probe(struct platform_device *pdev)
83 {
84 	struct gpio_extcon_platform_data *pdata = dev_get_platdata(&pdev->dev);
85 	struct gpio_extcon_data *extcon_data;
86 	int ret;
87 
88 	if (!pdata)
89 		return -EBUSY;
90 	if (!pdata->irq_flags) {
91 		dev_err(&pdev->dev, "IRQ flag is not specified.\n");
92 		return -EINVAL;
93 	}
94 
95 	extcon_data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
96 				   GFP_KERNEL);
97 	if (!extcon_data)
98 		return -ENOMEM;
99 
100 	extcon_data->edev.name = pdata->name;
101 	extcon_data->edev.dev.parent = &pdev->dev;
102 	extcon_data->gpio = pdata->gpio;
103 	extcon_data->gpio_active_low = pdata->gpio_active_low;
104 	extcon_data->state_on = pdata->state_on;
105 	extcon_data->state_off = pdata->state_off;
106 	if (pdata->state_on && pdata->state_off)
107 		extcon_data->edev.print_state = extcon_gpio_print_state;
108 	if (pdata->debounce) {
109 		ret = gpio_set_debounce(extcon_data->gpio,
110 					pdata->debounce * 1000);
111 		if (ret < 0)
112 			extcon_data->debounce_jiffies =
113 				msecs_to_jiffies(pdata->debounce);
114 	}
115 
116 	ret = extcon_dev_register(&extcon_data->edev);
117 	if (ret < 0)
118 		return ret;
119 
120 	ret = devm_gpio_request_one(&pdev->dev, extcon_data->gpio, GPIOF_DIR_IN,
121 				    pdev->name);
122 	if (ret < 0)
123 		goto err;
124 
125 	INIT_DELAYED_WORK(&extcon_data->work, gpio_extcon_work);
126 
127 	extcon_data->irq = gpio_to_irq(extcon_data->gpio);
128 	if (extcon_data->irq < 0) {
129 		ret = extcon_data->irq;
130 		goto err;
131 	}
132 
133 	ret = request_any_context_irq(extcon_data->irq, gpio_irq_handler,
134 				      pdata->irq_flags, pdev->name,
135 				      extcon_data);
136 	if (ret < 0)
137 		goto err;
138 
139 	platform_set_drvdata(pdev, extcon_data);
140 	/* Perform initial detection */
141 	gpio_extcon_work(&extcon_data->work.work);
142 
143 	return 0;
144 
145 err:
146 	extcon_dev_unregister(&extcon_data->edev);
147 
148 	return ret;
149 }
150 
151 static int gpio_extcon_remove(struct platform_device *pdev)
152 {
153 	struct gpio_extcon_data *extcon_data = platform_get_drvdata(pdev);
154 
155 	cancel_delayed_work_sync(&extcon_data->work);
156 	free_irq(extcon_data->irq, extcon_data);
157 	extcon_dev_unregister(&extcon_data->edev);
158 
159 	return 0;
160 }
161 
162 static struct platform_driver gpio_extcon_driver = {
163 	.probe		= gpio_extcon_probe,
164 	.remove		= gpio_extcon_remove,
165 	.driver		= {
166 		.name	= "extcon-gpio",
167 		.owner	= THIS_MODULE,
168 	},
169 };
170 
171 module_platform_driver(gpio_extcon_driver);
172 
173 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
174 MODULE_DESCRIPTION("GPIO extcon driver");
175 MODULE_LICENSE("GPL");
176