]>
Commit | Line | Data |
---|---|---|
9c92ab61 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
be48308a | 2 | /* |
6ba12997 | 3 | * extcon_gpio.c - Single-state GPIO extcon driver based on extcon class |
be48308a MH |
4 | * |
5 | * Copyright (C) 2008 Google, Inc. | |
6 | * Author: Mike Lockwood <lockwood@android.com> | |
7 | * | |
8 | * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon | |
9 | * (originally switch class is supported) | |
6ba12997 | 10 | */ |
be48308a | 11 | |
f94a5bec | 12 | #include <linux/devm-helpers.h> |
176aa360 | 13 | #include <linux/extcon-provider.h> |
de992acb | 14 | #include <linux/gpio/consumer.h> |
be48308a MH |
15 | #include <linux/init.h> |
16 | #include <linux/interrupt.h> | |
62364357 GC |
17 | #include <linux/kernel.h> |
18 | #include <linux/module.h> | |
be48308a MH |
19 | #include <linux/platform_device.h> |
20 | #include <linux/slab.h> | |
be48308a | 21 | #include <linux/workqueue.h> |
be48308a | 22 | |
66afdedf | 23 | /** |
a62300d9 LW |
24 | * struct gpio_extcon_data - A simple GPIO-controlled extcon device state container. |
25 | * @edev: Extcon device. | |
a62300d9 LW |
26 | * @work: Work fired by the interrupt. |
27 | * @debounce_jiffies: Number of jiffies to wait for the GPIO to stabilize, from the debounce | |
28 | * value. | |
d368e7de | 29 | * @gpiod: GPIO descriptor for this external connector. |
66afdedf | 30 | * @extcon_id: The unique id of specific external connector. |
66afdedf | 31 | * @debounce: Debounce time for GPIO IRQ in ms. |
66afdedf LW |
32 | * @check_on_resume: Boolean describing whether to check the state of gpio |
33 | * while resuming from sleep. | |
34 | */ | |
be48308a | 35 | struct gpio_extcon_data { |
60cd62d4 | 36 | struct extcon_dev *edev; |
be48308a MH |
37 | struct delayed_work work; |
38 | unsigned long debounce_jiffies; | |
d368e7de | 39 | struct gpio_desc *gpiod; |
a62300d9 | 40 | unsigned int extcon_id; |
a62300d9 | 41 | unsigned long debounce; |
a62300d9 | 42 | bool check_on_resume; |
be48308a MH |
43 | }; |
44 | ||
45 | static void gpio_extcon_work(struct work_struct *work) | |
46 | { | |
47 | int state; | |
48 | struct gpio_extcon_data *data = | |
49 | container_of(to_delayed_work(work), struct gpio_extcon_data, | |
50 | work); | |
51 | ||
d368e7de | 52 | state = gpiod_get_value_cansleep(data->gpiod); |
a62300d9 | 53 | extcon_set_state_sync(data->edev, data->extcon_id, state); |
be48308a MH |
54 | } |
55 | ||
56 | static irqreturn_t gpio_irq_handler(int irq, void *dev_id) | |
57 | { | |
60f9b9e6 | 58 | struct gpio_extcon_data *data = dev_id; |
be48308a | 59 | |
60f9b9e6 CC |
60 | queue_delayed_work(system_power_efficient_wq, &data->work, |
61 | data->debounce_jiffies); | |
be48308a MH |
62 | return IRQ_HANDLED; |
63 | } | |
64 | ||
44f34fd4 | 65 | static int gpio_extcon_probe(struct platform_device *pdev) |
be48308a | 66 | { |
60f9b9e6 | 67 | struct gpio_extcon_data *data; |
d368e7de | 68 | struct device *dev = &pdev->dev; |
8bc4810b LW |
69 | unsigned long irq_flags; |
70 | int irq; | |
1073514b | 71 | int ret; |
be48308a | 72 | |
d368e7de | 73 | data = devm_kzalloc(dev, sizeof(struct gpio_extcon_data), GFP_KERNEL); |
60f9b9e6 | 74 | if (!data) |
be48308a | 75 | return -ENOMEM; |
a62300d9 LW |
76 | |
77 | /* | |
78 | * FIXME: extcon_id represents the unique identifier of external | |
79 | * connectors such as EXTCON_USB, EXTCON_DISP_HDMI and so on. extcon_id | |
80 | * is necessary to register the extcon device. But, it's not yet | |
81 | * developed to get the extcon id from device-tree or others. | |
82 | * On later, it have to be solved. | |
83 | */ | |
8bc4810b | 84 | if (data->extcon_id > EXTCON_NONE) |
a62300d9 | 85 | return -EINVAL; |
60cd62d4 | 86 | |
d368e7de LW |
87 | data->gpiod = devm_gpiod_get(dev, "extcon", GPIOD_IN); |
88 | if (IS_ERR(data->gpiod)) | |
89 | return PTR_ERR(data->gpiod); | |
8bc4810b LW |
90 | irq = gpiod_to_irq(data->gpiod); |
91 | if (irq <= 0) | |
92 | return irq; | |
93 | ||
94 | /* | |
95 | * It is unlikely that this is an acknowledged interrupt that goes | |
96 | * away after handling, what we are looking for are falling edges | |
97 | * if the signal is active low, and rising edges if the signal is | |
98 | * active high. | |
99 | */ | |
100 | if (gpiod_is_active_low(data->gpiod)) | |
101 | irq_flags = IRQF_TRIGGER_FALLING; | |
102 | else | |
103 | irq_flags = IRQF_TRIGGER_RISING; | |
4288d9b8 | 104 | |
de992acb | 105 | /* Allocate the memory of extcon devie and register extcon device */ |
d368e7de | 106 | data->edev = devm_extcon_dev_allocate(dev, &data->extcon_id); |
de992acb | 107 | if (IS_ERR(data->edev)) { |
d368e7de | 108 | dev_err(dev, "failed to allocate extcon device\n"); |
de992acb | 109 | return -ENOMEM; |
338de0ca | 110 | } |
be48308a | 111 | |
d368e7de | 112 | ret = devm_extcon_dev_register(dev, data->edev); |
be48308a | 113 | if (ret < 0) |
01eaf245 | 114 | return ret; |
be48308a | 115 | |
f94a5bec MV |
116 | ret = devm_delayed_work_autocancel(dev, &data->work, gpio_extcon_work); |
117 | if (ret) | |
118 | return ret; | |
be48308a | 119 | |
de992acb | 120 | /* |
b51b3870 | 121 | * Request the interrupt of gpio to detect whether external connector |
de992acb CC |
122 | * is attached or detached. |
123 | */ | |
8bc4810b LW |
124 | ret = devm_request_any_context_irq(dev, irq, |
125 | gpio_irq_handler, irq_flags, | |
60f9b9e6 | 126 | pdev->name, data); |
be48308a | 127 | if (ret < 0) |
d92c2f12 | 128 | return ret; |
be48308a | 129 | |
60f9b9e6 | 130 | platform_set_drvdata(pdev, data); |
be48308a | 131 | /* Perform initial detection */ |
60f9b9e6 | 132 | gpio_extcon_work(&data->work.work); |
be48308a MH |
133 | |
134 | return 0; | |
be48308a MH |
135 | } |
136 | ||
6544dfa5 RY |
137 | #ifdef CONFIG_PM_SLEEP |
138 | static int gpio_extcon_resume(struct device *dev) | |
139 | { | |
60f9b9e6 | 140 | struct gpio_extcon_data *data; |
6544dfa5 | 141 | |
60f9b9e6 | 142 | data = dev_get_drvdata(dev); |
a62300d9 | 143 | if (data->check_on_resume) |
6544dfa5 | 144 | queue_delayed_work(system_power_efficient_wq, |
60f9b9e6 | 145 | &data->work, data->debounce_jiffies); |
6544dfa5 RY |
146 | |
147 | return 0; | |
148 | } | |
149 | #endif | |
150 | ||
3cc731d9 | 151 | static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume); |
6544dfa5 | 152 | |
2878bda8 | 153 | static struct platform_driver gpio_extcon_driver = { |
be48308a | 154 | .probe = gpio_extcon_probe, |
be48308a MH |
155 | .driver = { |
156 | .name = "extcon-gpio", | |
6544dfa5 | 157 | .pm = &gpio_extcon_pm_ops, |
be48308a MH |
158 | }, |
159 | }; | |
160 | ||
2878bda8 | 161 | module_platform_driver(gpio_extcon_driver); |
be48308a MH |
162 | |
163 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); | |
164 | MODULE_DESCRIPTION("GPIO extcon driver"); | |
165 | MODULE_LICENSE("GPL"); |