2 * extcon_gpio.c - Single-state GPIO extcon driver based on extcon class
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
7 * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon
8 * (originally switch class is supported)
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/extcon.h>
21 #include <linux/extcon/extcon-gpio.h>
22 #include <linux/gpio.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/workqueue.h>
31 struct gpio_extcon_data {
32 struct extcon_dev *edev;
34 struct delayed_work work;
35 unsigned long debounce_jiffies;
37 struct gpio_extcon_pdata *pdata;
40 static void gpio_extcon_work(struct work_struct *work)
43 struct gpio_extcon_data *data =
44 container_of(to_delayed_work(work), struct gpio_extcon_data,
47 state = gpio_get_value(data->pdata->gpio);
48 if (data->pdata->gpio_active_low)
50 extcon_set_state(data->edev, state);
53 static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
55 struct gpio_extcon_data *data = dev_id;
57 queue_delayed_work(system_power_efficient_wq, &data->work,
58 data->debounce_jiffies);
62 static int gpio_extcon_probe(struct platform_device *pdev)
64 struct gpio_extcon_pdata *pdata = dev_get_platdata(&pdev->dev);
65 struct gpio_extcon_data *data;
70 if (!pdata->irq_flags || pdata->extcon_id > EXTCON_NONE)
73 data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
78 data->edev = devm_extcon_dev_allocate(&pdev->dev, &pdata->extcon_id);
79 if (IS_ERR(data->edev)) {
80 dev_err(&pdev->dev, "failed to allocate extcon device\n");
85 ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio, GPIOF_DIR_IN,
90 if (pdata->debounce) {
91 ret = gpio_set_debounce(data->pdata->gpio,
92 pdata->debounce * 1000);
94 data->debounce_jiffies =
95 msecs_to_jiffies(pdata->debounce);
98 ret = devm_extcon_dev_register(&pdev->dev, data->edev);
102 INIT_DELAYED_WORK(&data->work, gpio_extcon_work);
104 data->irq = gpio_to_irq(data->pdata->gpio);
108 ret = devm_request_any_context_irq(&pdev->dev, data->irq,
109 gpio_irq_handler, pdata->irq_flags,
114 platform_set_drvdata(pdev, data);
115 /* Perform initial detection */
116 gpio_extcon_work(&data->work.work);
121 static int gpio_extcon_remove(struct platform_device *pdev)
123 struct gpio_extcon_data *data = platform_get_drvdata(pdev);
125 cancel_delayed_work_sync(&data->work);
130 #ifdef CONFIG_PM_SLEEP
131 static int gpio_extcon_resume(struct device *dev)
133 struct gpio_extcon_data *data;
135 data = dev_get_drvdata(dev);
136 if (data->pdata->check_on_resume)
137 queue_delayed_work(system_power_efficient_wq,
138 &data->work, data->debounce_jiffies);
144 static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
146 static struct platform_driver gpio_extcon_driver = {
147 .probe = gpio_extcon_probe,
148 .remove = gpio_extcon_remove,
150 .name = "extcon-gpio",
151 .pm = &gpio_extcon_pm_ops,
155 module_platform_driver(gpio_extcon_driver);
157 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
158 MODULE_DESCRIPTION("GPIO extcon driver");
159 MODULE_LICENSE("GPL");