extcon: gpio: Fix minor coding style and remove the unused fields.
[cascardo/linux.git] / drivers / extcon / extcon-gpio.c
1 /*
2  * extcon_gpio.c - Single-state GPIO extcon driver based on extcon class
3  *
4  * Copyright (C) 2008 Google, Inc.
5  * Author: Mike Lockwood <lockwood@android.com>
6  *
7  * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon
8  * (originally switch class is supported)
9  *
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.
13  *
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.
18  */
19
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>
30
31 struct gpio_extcon_data {
32         struct extcon_dev *edev;
33         int irq;
34         struct delayed_work work;
35         unsigned long debounce_jiffies;
36
37         struct gpio_extcon_pdata *pdata;
38 };
39
40 static void gpio_extcon_work(struct work_struct *work)
41 {
42         int state;
43         struct gpio_extcon_data *data =
44                 container_of(to_delayed_work(work), struct gpio_extcon_data,
45                              work);
46
47         state = gpio_get_value(data->pdata->gpio);
48         if (data->pdata->gpio_active_low)
49                 state = !state;
50         extcon_set_state(data->edev, state);
51 }
52
53 static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
54 {
55         struct gpio_extcon_data *data = dev_id;
56
57         queue_delayed_work(system_power_efficient_wq, &data->work,
58                               data->debounce_jiffies);
59         return IRQ_HANDLED;
60 }
61
62 static int gpio_extcon_probe(struct platform_device *pdev)
63 {
64         struct gpio_extcon_pdata *pdata = dev_get_platdata(&pdev->dev);
65         struct gpio_extcon_data *data;
66         int ret;
67
68         if (!pdata)
69                 return -EBUSY;
70         if (!pdata->irq_flags || pdata->extcon_id > EXTCON_NONE)
71                 return -EINVAL;
72
73         data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
74                                    GFP_KERNEL);
75         if (!data)
76                 return -ENOMEM;
77
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");
81                 return -ENOMEM;
82         }
83         data->pdata = pdata;
84
85         ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio, GPIOF_DIR_IN,
86                                     pdev->name);
87         if (ret < 0)
88                 return ret;
89
90         if (pdata->debounce) {
91                 ret = gpio_set_debounce(data->pdata->gpio,
92                                         pdata->debounce * 1000);
93                 if (ret < 0)
94                         data->debounce_jiffies =
95                                 msecs_to_jiffies(pdata->debounce);
96         }
97
98         ret = devm_extcon_dev_register(&pdev->dev, data->edev);
99         if (ret < 0)
100                 return ret;
101
102         INIT_DELAYED_WORK(&data->work, gpio_extcon_work);
103
104         data->irq = gpio_to_irq(data->pdata->gpio);
105         if (data->irq < 0)
106                 return data->irq;
107
108         ret = devm_request_any_context_irq(&pdev->dev, data->irq,
109                                         gpio_irq_handler, pdata->irq_flags,
110                                         pdev->name, data);
111         if (ret < 0)
112                 return ret;
113
114         platform_set_drvdata(pdev, data);
115         /* Perform initial detection */
116         gpio_extcon_work(&data->work.work);
117
118         return 0;
119 }
120
121 static int gpio_extcon_remove(struct platform_device *pdev)
122 {
123         struct gpio_extcon_data *data = platform_get_drvdata(pdev);
124
125         cancel_delayed_work_sync(&data->work);
126
127         return 0;
128 }
129
130 #ifdef CONFIG_PM_SLEEP
131 static int gpio_extcon_resume(struct device *dev)
132 {
133         struct gpio_extcon_data *data;
134
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);
139
140         return 0;
141 }
142 #endif
143
144 static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
145
146 static struct platform_driver gpio_extcon_driver = {
147         .probe          = gpio_extcon_probe,
148         .remove         = gpio_extcon_remove,
149         .driver         = {
150                 .name   = "extcon-gpio",
151                 .pm     = &gpio_extcon_pm_ops,
152         },
153 };
154
155 module_platform_driver(gpio_extcon_driver);
156
157 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
158 MODULE_DESCRIPTION("GPIO extcon driver");
159 MODULE_LICENSE("GPL");