Skip to content

Commit de992ac

Browse files
committed
extcon: gpio: Use descriptor-based GPIO interface instead of legacy gpio_* API
This patch uses the descriptor-based GPIO interface (gpiod_* API) instead of legacy gpio_* API and add the internal gpio_extcon_init() to handle all gpio-related tasks. Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
1 parent 6ba1299 commit de992ac

1 file changed

Lines changed: 43 additions & 19 deletions

File tree

drivers/extcon/extcon-gpio.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/extcon.h>
2121
#include <linux/extcon/extcon-gpio.h>
2222
#include <linux/gpio.h>
23+
#include <linux/gpio/consumer.h>
2324
#include <linux/init.h>
2425
#include <linux/interrupt.h>
2526
#include <linux/kernel.h>
@@ -34,6 +35,7 @@ struct gpio_extcon_data {
3435
struct delayed_work work;
3536
unsigned long debounce_jiffies;
3637

38+
struct gpio_desc *id_gpiod;
3739
struct gpio_extcon_pdata *pdata;
3840
};
3941

@@ -44,7 +46,7 @@ static void gpio_extcon_work(struct work_struct *work)
4446
container_of(to_delayed_work(work), struct gpio_extcon_data,
4547
work);
4648

47-
state = gpio_get_value(data->pdata->gpio);
49+
state = gpiod_get_value_cansleep(data->id_gpiod);
4850
if (data->pdata->gpio_active_low)
4951
state = !state;
5052
extcon_set_state(data->edev, state);
@@ -59,6 +61,35 @@ static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
5961
return IRQ_HANDLED;
6062
}
6163

64+
static int gpio_extcon_init(struct device *dev, struct gpio_extcon_data *data)
65+
{
66+
struct gpio_extcon_pdata *pdata = data->pdata;
67+
int ret;
68+
69+
ret = devm_gpio_request_one(dev, pdata->gpio, GPIOF_DIR_IN,
70+
dev_name(dev));
71+
if (ret < 0)
72+
return ret;
73+
74+
data->id_gpiod = gpio_to_desc(pdata->gpio);
75+
if (!data->id_gpiod)
76+
return -EINVAL;
77+
78+
if (pdata->debounce) {
79+
ret = gpiod_set_debounce(data->id_gpiod,
80+
pdata->debounce * 1000);
81+
if (ret < 0)
82+
data->debounce_jiffies =
83+
msecs_to_jiffies(pdata->debounce);
84+
}
85+
86+
data->irq = gpiod_to_irq(data->id_gpiod);
87+
if (data->irq < 0)
88+
return data->irq;
89+
90+
return 0;
91+
}
92+
6293
static int gpio_extcon_probe(struct platform_device *pdev)
6394
{
6495
struct gpio_extcon_pdata *pdata = dev_get_platdata(&pdev->dev);
@@ -74,25 +105,18 @@ static int gpio_extcon_probe(struct platform_device *pdev)
74105
GFP_KERNEL);
75106
if (!data)
76107
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-
}
83108
data->pdata = pdata;
84109

85-
ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio, GPIOF_DIR_IN,
86-
pdev->name);
110+
/* Initialize the gpio */
111+
ret = gpio_extcon_init(&pdev->dev, data);
87112
if (ret < 0)
88113
return ret;
89114

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);
115+
/* Allocate the memory of extcon devie and register extcon device */
116+
data->edev = devm_extcon_dev_allocate(&pdev->dev, &pdata->extcon_id);
117+
if (IS_ERR(data->edev)) {
118+
dev_err(&pdev->dev, "failed to allocate extcon device\n");
119+
return -ENOMEM;
96120
}
97121

98122
ret = devm_extcon_dev_register(&pdev->dev, data->edev);
@@ -101,10 +125,10 @@ static int gpio_extcon_probe(struct platform_device *pdev)
101125

102126
INIT_DELAYED_WORK(&data->work, gpio_extcon_work);
103127

104-
data->irq = gpio_to_irq(data->pdata->gpio);
105-
if (data->irq < 0)
106-
return data->irq;
107-
128+
/*
129+
* Request the interrput of gpio to detect whether external connector
130+
* is attached or detached.
131+
*/
108132
ret = devm_request_any_context_irq(&pdev->dev, data->irq,
109133
gpio_irq_handler, pdata->irq_flags,
110134
pdev->name, data);

0 commit comments

Comments
 (0)