2 * drivers/extcon/extcon_gpio.c
4 * Single-state GPIO extcon driver based on extcon class
6 * Copyright (C) 2008 Google, Inc.
7 * Author: Mike Lockwood <lockwood@android.com>
9 * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon
10 * (originally switch class is supported)
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.
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.
23 #include <linux/extcon.h>
24 #include <linux/extcon/extcon-gpio.h>
25 #include <linux/gpio.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32 #include <linux/workqueue.h>
34 struct gpio_extcon_data {
35 struct extcon_dev *edev;
37 struct delayed_work work;
38 unsigned long debounce_jiffies;
40 struct gpio_extcon_platform_data *pdata;
43 static void gpio_extcon_work(struct work_struct *work)
46 struct gpio_extcon_data *data =
47 container_of(to_delayed_work(work), struct gpio_extcon_data,
50 state = gpio_get_value(data->pdata->gpio);
51 if (data->pdata->gpio_active_low)
53 extcon_set_state(data->edev, state);
56 static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
58 struct gpio_extcon_data *data = dev_id;
60 queue_delayed_work(system_power_efficient_wq, &data->work,
61 data->debounce_jiffies);
65 static int gpio_extcon_probe(struct platform_device *pdev)
67 struct gpio_extcon_platform_data *pdata = dev_get_platdata(&pdev->dev);
68 struct gpio_extcon_data *data;
73 if (!pdata->irq_flags) {
74 dev_err(&pdev->dev, "IRQ flag is not specified.\n");
78 data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
83 data->edev = devm_extcon_dev_allocate(&pdev->dev, NULL);
84 if (IS_ERR(data->edev)) {
85 dev_err(&pdev->dev, "failed to allocate extcon device\n");
90 ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio, GPIOF_DIR_IN,
95 if (pdata->debounce) {
96 ret = gpio_set_debounce(data->pdata->gpio,
97 pdata->debounce * 1000);
99 data->debounce_jiffies =
100 msecs_to_jiffies(pdata->debounce);
103 ret = devm_extcon_dev_register(&pdev->dev, data->edev);
107 INIT_DELAYED_WORK(&data->work, gpio_extcon_work);
109 data->irq = gpio_to_irq(data->pdata->gpio);
113 ret = devm_request_any_context_irq(&pdev->dev, data->irq,
114 gpio_irq_handler, pdata->irq_flags,
119 platform_set_drvdata(pdev, data);
120 /* Perform initial detection */
121 gpio_extcon_work(&data->work.work);
126 static int gpio_extcon_remove(struct platform_device *pdev)
128 struct gpio_extcon_data *data = platform_get_drvdata(pdev);
130 cancel_delayed_work_sync(&data->work);
135 #ifdef CONFIG_PM_SLEEP
136 static int gpio_extcon_resume(struct device *dev)
138 struct gpio_extcon_data *data;
140 data = dev_get_drvdata(dev);
141 if (data->pdata->check_on_resume)
142 queue_delayed_work(system_power_efficient_wq,
143 &data->work, data->debounce_jiffies);
149 static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
151 static struct platform_driver gpio_extcon_driver = {
152 .probe = gpio_extcon_probe,
153 .remove = gpio_extcon_remove,
155 .name = "extcon-gpio",
156 .pm = &gpio_extcon_pm_ops,
160 module_platform_driver(gpio_extcon_driver);
162 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
163 MODULE_DESCRIPTION("GPIO extcon driver");
164 MODULE_LICENSE("GPL");