extcon: gpio: Remove duplicate data from struct gpio_extcon_data
[firefly-linux-kernel-4.4.55.git] / drivers / extcon / extcon-gpio.c
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/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>
33
34 struct gpio_extcon_data {
35         struct extcon_dev *edev;
36         int irq;
37         struct delayed_work work;
38         unsigned long debounce_jiffies;
39
40         struct gpio_extcon_platform_data *pdata;
41 };
42
43 static void gpio_extcon_work(struct work_struct *work)
44 {
45         int state;
46         struct gpio_extcon_data *data =
47                 container_of(to_delayed_work(work), struct gpio_extcon_data,
48                              work);
49
50         state = gpio_get_value(data->pdata->gpio);
51         if (data->pdata->gpio_active_low)
52                 state = !state;
53         extcon_set_state(data->edev, state);
54 }
55
56 static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
57 {
58         struct gpio_extcon_data *data = dev_id;
59
60         queue_delayed_work(system_power_efficient_wq, &data->work,
61                               data->debounce_jiffies);
62         return IRQ_HANDLED;
63 }
64
65 static int gpio_extcon_probe(struct platform_device *pdev)
66 {
67         struct gpio_extcon_platform_data *pdata = dev_get_platdata(&pdev->dev);
68         struct gpio_extcon_data *data;
69         int ret;
70
71         if (!pdata)
72                 return -EBUSY;
73         if (!pdata->irq_flags) {
74                 dev_err(&pdev->dev, "IRQ flag is not specified.\n");
75                 return -EINVAL;
76         }
77
78         data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
79                                    GFP_KERNEL);
80         if (!data)
81                 return -ENOMEM;
82
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");
86                 return -ENOMEM;
87         }
88         data->pdata = pdata;
89
90         ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio, GPIOF_DIR_IN,
91                                     pdev->name);
92         if (ret < 0)
93                 return ret;
94
95         if (pdata->debounce) {
96                 ret = gpio_set_debounce(data->pdata->gpio,
97                                         pdata->debounce * 1000);
98                 if (ret < 0)
99                         data->debounce_jiffies =
100                                 msecs_to_jiffies(pdata->debounce);
101         }
102
103         ret = devm_extcon_dev_register(&pdev->dev, data->edev);
104         if (ret < 0)
105                 return ret;
106
107         INIT_DELAYED_WORK(&data->work, gpio_extcon_work);
108
109         data->irq = gpio_to_irq(data->pdata->gpio);
110         if (data->irq < 0)
111                 return data->irq;
112
113         ret = devm_request_any_context_irq(&pdev->dev, data->irq,
114                                         gpio_irq_handler, pdata->irq_flags,
115                                         pdev->name, data);
116         if (ret < 0)
117                 return ret;
118
119         platform_set_drvdata(pdev, data);
120         /* Perform initial detection */
121         gpio_extcon_work(&data->work.work);
122
123         return 0;
124 }
125
126 static int gpio_extcon_remove(struct platform_device *pdev)
127 {
128         struct gpio_extcon_data *data = platform_get_drvdata(pdev);
129
130         cancel_delayed_work_sync(&data->work);
131
132         return 0;
133 }
134
135 #ifdef CONFIG_PM_SLEEP
136 static int gpio_extcon_resume(struct device *dev)
137 {
138         struct gpio_extcon_data *data;
139
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);
144
145         return 0;
146 }
147 #endif
148
149 static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
150
151 static struct platform_driver gpio_extcon_driver = {
152         .probe          = gpio_extcon_probe,
153         .remove         = gpio_extcon_remove,
154         .driver         = {
155                 .name   = "extcon-gpio",
156                 .pm     = &gpio_extcon_pm_ops,
157         },
158 };
159
160 module_platform_driver(gpio_extcon_driver);
161
162 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
163 MODULE_DESCRIPTION("GPIO extcon driver");
164 MODULE_LICENSE("GPL");