]>
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 | |
176aa360 | 12 | #include <linux/extcon-provider.h> |
de992acb | 13 | #include <linux/gpio/consumer.h> |
be48308a MH |
14 | #include <linux/init.h> |
15 | #include <linux/interrupt.h> | |
62364357 GC |
16 | #include <linux/kernel.h> |
17 | #include <linux/module.h> | |
be48308a MH |
18 | #include <linux/platform_device.h> |
19 | #include <linux/slab.h> | |
be48308a | 20 | #include <linux/workqueue.h> |
be48308a | 21 | |
66afdedf | 22 | /** |
a62300d9 LW |
23 | * struct gpio_extcon_data - A simple GPIO-controlled extcon device state container. |
24 | * @edev: Extcon device. | |
25 | * @irq: Interrupt line for the external connector. | |
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 LW |
31 | * @debounce: Debounce time for GPIO IRQ in ms. |
32 | * @irq_flags: IRQ Flags (e.g., IRQF_TRIGGER_LOW). | |
33 | * @check_on_resume: Boolean describing whether to check the state of gpio | |
34 | * while resuming from sleep. | |
35 | */ | |
be48308a | 36 | struct gpio_extcon_data { |
60cd62d4 | 37 | struct extcon_dev *edev; |
be48308a MH |
38 | int irq; |
39 | struct delayed_work work; | |
40 | unsigned long debounce_jiffies; | |
d368e7de | 41 | struct gpio_desc *gpiod; |
a62300d9 | 42 | unsigned int extcon_id; |
a62300d9 LW |
43 | unsigned long debounce; |
44 | unsigned long irq_flags; | |
45 | bool check_on_resume; | |
be48308a MH |
46 | }; |
47 | ||
48 | static void gpio_extcon_work(struct work_struct *work) | |
49 | { | |
50 | int state; | |
51 | struct gpio_extcon_data *data = | |
52 | container_of(to_delayed_work(work), struct gpio_extcon_data, | |
53 | work); | |
54 | ||
d368e7de | 55 | state = gpiod_get_value_cansleep(data->gpiod); |
a62300d9 | 56 | extcon_set_state_sync(data->edev, data->extcon_id, state); |
be48308a MH |
57 | } |
58 | ||
59 | static irqreturn_t gpio_irq_handler(int irq, void *dev_id) | |
60 | { | |
60f9b9e6 | 61 | struct gpio_extcon_data *data = dev_id; |
be48308a | 62 | |
60f9b9e6 CC |
63 | queue_delayed_work(system_power_efficient_wq, &data->work, |
64 | data->debounce_jiffies); | |
be48308a MH |
65 | return IRQ_HANDLED; |
66 | } | |
67 | ||
44f34fd4 | 68 | static int gpio_extcon_probe(struct platform_device *pdev) |
be48308a | 69 | { |
60f9b9e6 | 70 | struct gpio_extcon_data *data; |
d368e7de | 71 | struct device *dev = &pdev->dev; |
1073514b | 72 | int ret; |
be48308a | 73 | |
d368e7de | 74 | data = devm_kzalloc(dev, sizeof(struct gpio_extcon_data), GFP_KERNEL); |
60f9b9e6 | 75 | if (!data) |
be48308a | 76 | return -ENOMEM; |
a62300d9 LW |
77 | |
78 | /* | |
79 | * FIXME: extcon_id represents the unique identifier of external | |
80 | * connectors such as EXTCON_USB, EXTCON_DISP_HDMI and so on. extcon_id | |
81 | * is necessary to register the extcon device. But, it's not yet | |
82 | * developed to get the extcon id from device-tree or others. | |
83 | * On later, it have to be solved. | |
84 | */ | |
85 | if (!data->irq_flags || data->extcon_id > EXTCON_NONE) | |
86 | return -EINVAL; | |
60cd62d4 | 87 | |
d368e7de LW |
88 | data->gpiod = devm_gpiod_get(dev, "extcon", GPIOD_IN); |
89 | if (IS_ERR(data->gpiod)) | |
90 | return PTR_ERR(data->gpiod); | |
91 | data->irq = gpiod_to_irq(data->gpiod); | |
92 | if (data->irq <= 0) | |
93 | return data->irq; | |
4288d9b8 | 94 | |
de992acb | 95 | /* Allocate the memory of extcon devie and register extcon device */ |
d368e7de | 96 | data->edev = devm_extcon_dev_allocate(dev, &data->extcon_id); |
de992acb | 97 | if (IS_ERR(data->edev)) { |
d368e7de | 98 | dev_err(dev, "failed to allocate extcon device\n"); |
de992acb | 99 | return -ENOMEM; |
338de0ca | 100 | } |
be48308a | 101 | |
d368e7de | 102 | ret = devm_extcon_dev_register(dev, data->edev); |
be48308a | 103 | if (ret < 0) |
01eaf245 | 104 | return ret; |
be48308a | 105 | |
60f9b9e6 | 106 | INIT_DELAYED_WORK(&data->work, gpio_extcon_work); |
be48308a | 107 | |
de992acb | 108 | /* |
b51b3870 | 109 | * Request the interrupt of gpio to detect whether external connector |
de992acb CC |
110 | * is attached or detached. |
111 | */ | |
d368e7de | 112 | ret = devm_request_any_context_irq(dev, data->irq, |
a62300d9 | 113 | gpio_irq_handler, data->irq_flags, |
60f9b9e6 | 114 | pdev->name, data); |
be48308a | 115 | if (ret < 0) |
d92c2f12 | 116 | return ret; |
be48308a | 117 | |
60f9b9e6 | 118 | platform_set_drvdata(pdev, data); |
be48308a | 119 | /* Perform initial detection */ |
60f9b9e6 | 120 | gpio_extcon_work(&data->work.work); |
be48308a MH |
121 | |
122 | return 0; | |
be48308a MH |
123 | } |
124 | ||
93ed0327 | 125 | static int gpio_extcon_remove(struct platform_device *pdev) |
be48308a | 126 | { |
60f9b9e6 | 127 | struct gpio_extcon_data *data = platform_get_drvdata(pdev); |
be48308a | 128 | |
60f9b9e6 | 129 | cancel_delayed_work_sync(&data->work); |
be48308a MH |
130 | |
131 | return 0; | |
132 | } | |
133 | ||
6544dfa5 RY |
134 | #ifdef CONFIG_PM_SLEEP |
135 | static int gpio_extcon_resume(struct device *dev) | |
136 | { | |
60f9b9e6 | 137 | struct gpio_extcon_data *data; |
6544dfa5 | 138 | |
60f9b9e6 | 139 | data = dev_get_drvdata(dev); |
a62300d9 | 140 | if (data->check_on_resume) |
6544dfa5 | 141 | queue_delayed_work(system_power_efficient_wq, |
60f9b9e6 | 142 | &data->work, data->debounce_jiffies); |
6544dfa5 RY |
143 | |
144 | return 0; | |
145 | } | |
146 | #endif | |
147 | ||
3cc731d9 | 148 | static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume); |
6544dfa5 | 149 | |
2878bda8 | 150 | static struct platform_driver gpio_extcon_driver = { |
be48308a | 151 | .probe = gpio_extcon_probe, |
5f7e2228 | 152 | .remove = gpio_extcon_remove, |
be48308a MH |
153 | .driver = { |
154 | .name = "extcon-gpio", | |
6544dfa5 | 155 | .pm = &gpio_extcon_pm_ops, |
be48308a MH |
156 | }, |
157 | }; | |
158 | ||
2878bda8 | 159 | module_platform_driver(gpio_extcon_driver); |
be48308a MH |
160 | |
161 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); | |
162 | MODULE_DESCRIPTION("GPIO extcon driver"); | |
163 | MODULE_LICENSE("GPL"); |