MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 1 | /* |
Chanwoo Choi | 6ba1299 | 2015-09-29 22:59:55 +0900 | [diff] [blame] | 2 | * extcon_gpio.c - Single-state GPIO extcon driver based on extcon class |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 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. |
Chanwoo Choi | 6ba1299 | 2015-09-29 22:59:55 +0900 | [diff] [blame] | 18 | */ |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 19 | |
George Cherian | 6236435 | 2014-09-09 09:44:34 +0530 | [diff] [blame] | 20 | #include <linux/extcon.h> |
| 21 | #include <linux/extcon/extcon-gpio.h> |
| 22 | #include <linux/gpio.h> |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 23 | #include <linux/gpio/consumer.h> |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 24 | #include <linux/init.h> |
| 25 | #include <linux/interrupt.h> |
George Cherian | 6236435 | 2014-09-09 09:44:34 +0530 | [diff] [blame] | 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/slab.h> |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 30 | #include <linux/workqueue.h> |
Subhash Jadavani | e5371bd | 2017-05-08 18:06:42 -0700 | [diff] [blame] | 31 | #include <linux/of_gpio.h> |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 32 | |
| 33 | struct gpio_extcon_data { |
Chanwoo Choi | 60cd62d | 2014-04-21 20:51:08 +0900 | [diff] [blame] | 34 | struct extcon_dev *edev; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 35 | int irq; |
| 36 | struct delayed_work work; |
| 37 | unsigned long debounce_jiffies; |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 38 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 39 | struct gpio_desc *id_gpiod; |
Chanwoo Choi | 6ba1299 | 2015-09-29 22:59:55 +0900 | [diff] [blame] | 40 | struct gpio_extcon_pdata *pdata; |
Subhash Jadavani | e5371bd | 2017-05-08 18:06:42 -0700 | [diff] [blame] | 41 | unsigned int *supported_cable; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | static void gpio_extcon_work(struct work_struct *work) |
| 45 | { |
| 46 | int state; |
| 47 | struct gpio_extcon_data *data = |
| 48 | container_of(to_delayed_work(work), struct gpio_extcon_data, |
| 49 | work); |
| 50 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 51 | state = gpiod_get_value_cansleep(data->id_gpiod); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 52 | if (data->pdata->gpio_active_low) |
Guenter Roeck | 5bfbdc9 | 2013-09-12 08:49:54 +0900 | [diff] [blame] | 53 | state = !state; |
Kishon Vijay Abraham I | cb9850d | 2016-09-15 15:46:11 +0530 | [diff] [blame] | 54 | |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 55 | extcon_set_state_sync(data->edev, data->pdata->extcon_id, state); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static irqreturn_t gpio_irq_handler(int irq, void *dev_id) |
| 59 | { |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 60 | struct gpio_extcon_data *data = dev_id; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 61 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 62 | queue_delayed_work(system_power_efficient_wq, &data->work, |
| 63 | data->debounce_jiffies); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 64 | return IRQ_HANDLED; |
| 65 | } |
| 66 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 67 | static int gpio_extcon_init(struct device *dev, struct gpio_extcon_data *data) |
| 68 | { |
| 69 | struct gpio_extcon_pdata *pdata = data->pdata; |
| 70 | int ret; |
| 71 | |
| 72 | ret = devm_gpio_request_one(dev, pdata->gpio, GPIOF_DIR_IN, |
| 73 | dev_name(dev)); |
| 74 | if (ret < 0) |
| 75 | return ret; |
| 76 | |
| 77 | data->id_gpiod = gpio_to_desc(pdata->gpio); |
| 78 | if (!data->id_gpiod) |
| 79 | return -EINVAL; |
| 80 | |
| 81 | if (pdata->debounce) { |
| 82 | ret = gpiod_set_debounce(data->id_gpiod, |
| 83 | pdata->debounce * 1000); |
| 84 | if (ret < 0) |
| 85 | data->debounce_jiffies = |
| 86 | msecs_to_jiffies(pdata->debounce); |
| 87 | } |
| 88 | |
| 89 | data->irq = gpiod_to_irq(data->id_gpiod); |
| 90 | if (data->irq < 0) |
| 91 | return data->irq; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
Subhash Jadavani | e5371bd | 2017-05-08 18:06:42 -0700 | [diff] [blame] | 96 | static int extcon_parse_pinctrl_data(struct device *dev, |
| 97 | struct gpio_extcon_pdata *pdata) |
| 98 | { |
| 99 | struct pinctrl *pctrl; |
| 100 | int ret = 0; |
| 101 | |
| 102 | /* Try to obtain pinctrl handle */ |
| 103 | pctrl = devm_pinctrl_get(dev); |
| 104 | if (IS_ERR(pctrl)) { |
| 105 | ret = PTR_ERR(pctrl); |
| 106 | goto out; |
| 107 | } |
| 108 | pdata->pctrl = pctrl; |
| 109 | |
| 110 | /* Look-up and keep the state handy to be used later */ |
| 111 | pdata->pins_default = pinctrl_lookup_state(pdata->pctrl, |
| 112 | "default"); |
| 113 | if (IS_ERR(pdata->pins_default)) { |
| 114 | ret = PTR_ERR(pdata->pins_default); |
| 115 | dev_err(dev, "Can't get default pinctrl state, ret %d\n", ret); |
| 116 | } |
| 117 | out: |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | /* Parse platform data */ |
| 122 | static |
| 123 | struct gpio_extcon_pdata *extcon_populate_pdata(struct device *dev) |
| 124 | { |
| 125 | struct gpio_extcon_pdata *pdata = NULL; |
| 126 | struct device_node *np = dev->of_node; |
| 127 | enum of_gpio_flags flags; |
| 128 | u32 val; |
| 129 | |
| 130 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 131 | if (!pdata) |
| 132 | goto out; |
| 133 | |
| 134 | if (of_property_read_u32(np, "extcon-id", &pdata->extcon_id)) { |
| 135 | dev_err(dev, "extcon-id property not found\n"); |
| 136 | goto out; |
| 137 | } |
| 138 | |
| 139 | pdata->gpio = of_get_named_gpio_flags(np, "gpio", 0, &flags); |
| 140 | if (gpio_is_valid(pdata->gpio)) { |
| 141 | if (flags & OF_GPIO_ACTIVE_LOW) |
| 142 | pdata->gpio_active_low = true; |
| 143 | } else { |
| 144 | dev_err(dev, "gpio property not found or invalid\n"); |
| 145 | goto out; |
| 146 | } |
| 147 | |
| 148 | if (of_property_read_u32(np, "irq-flags", &val)) { |
| 149 | dev_err(dev, "irq-flags property not found\n"); |
| 150 | goto out; |
| 151 | } |
| 152 | pdata->irq_flags = val; |
| 153 | |
| 154 | if (of_property_read_u32(np, "debounce-ms", &val)) { |
| 155 | dev_err(dev, "debounce-ms property not found\n"); |
| 156 | goto out; |
| 157 | } |
| 158 | pdata->debounce = val; |
| 159 | |
| 160 | if (extcon_parse_pinctrl_data(dev, pdata)) { |
| 161 | dev_err(dev, "failed to parse pinctrl data\n"); |
| 162 | goto out; |
| 163 | } |
| 164 | |
| 165 | return pdata; |
| 166 | out: |
| 167 | return NULL; |
| 168 | } |
| 169 | |
Bill Pemberton | 44f34fd | 2012-11-19 13:23:21 -0500 | [diff] [blame] | 170 | static int gpio_extcon_probe(struct platform_device *pdev) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 171 | { |
Chanwoo Choi | 6ba1299 | 2015-09-29 22:59:55 +0900 | [diff] [blame] | 172 | struct gpio_extcon_pdata *pdata = dev_get_platdata(&pdev->dev); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 173 | struct gpio_extcon_data *data; |
Guenter Roeck | 1073514 | 2013-08-29 21:29:33 -0700 | [diff] [blame] | 174 | int ret; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 175 | |
Subhash Jadavani | e5371bd | 2017-05-08 18:06:42 -0700 | [diff] [blame] | 176 | if (!pdata) { |
| 177 | /* try populating pdata from device tree */ |
| 178 | pdata = extcon_populate_pdata(&pdev->dev); |
| 179 | if (!pdata) |
| 180 | return -EBUSY; |
| 181 | } |
| 182 | if (!pdata->irq_flags || pdata->extcon_id >= EXTCON_NUM) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 183 | return -EINVAL; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 184 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 185 | data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data), |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 186 | GFP_KERNEL); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 187 | if (!data) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 188 | return -ENOMEM; |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 189 | data->pdata = pdata; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 190 | |
Subhash Jadavani | e5371bd | 2017-05-08 18:06:42 -0700 | [diff] [blame] | 191 | ret = pinctrl_select_state(pdata->pctrl, pdata->pins_default); |
| 192 | if (ret < 0) |
| 193 | dev_err(&pdev->dev, "pinctrl state select failed, ret %d\n", |
| 194 | ret); |
| 195 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 196 | /* Initialize the gpio */ |
| 197 | ret = gpio_extcon_init(&pdev->dev, data); |
| 198 | if (ret < 0) |
| 199 | return ret; |
| 200 | |
Subhash Jadavani | e5371bd | 2017-05-08 18:06:42 -0700 | [diff] [blame] | 201 | data->supported_cable = devm_kzalloc(&pdev->dev, |
| 202 | sizeof(*data->supported_cable) * 2, |
| 203 | GFP_KERNEL); |
| 204 | if (!data->supported_cable) |
| 205 | return -ENOMEM; |
| 206 | |
| 207 | data->supported_cable[0] = pdata->extcon_id; |
| 208 | data->supported_cable[1] = EXTCON_NONE; |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 209 | /* Allocate the memory of extcon devie and register extcon device */ |
Subhash Jadavani | e5371bd | 2017-05-08 18:06:42 -0700 | [diff] [blame] | 210 | data->edev = devm_extcon_dev_allocate(&pdev->dev, |
| 211 | data->supported_cable); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 212 | if (IS_ERR(data->edev)) { |
Chanwoo Choi | 60cd62d | 2014-04-21 20:51:08 +0900 | [diff] [blame] | 213 | dev_err(&pdev->dev, "failed to allocate extcon device\n"); |
| 214 | return -ENOMEM; |
| 215 | } |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 216 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 217 | ret = devm_extcon_dev_register(&pdev->dev, data->edev); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 218 | if (ret < 0) |
Axel Lin | 01eaf24 | 2012-06-16 11:56:24 +0800 | [diff] [blame] | 219 | return ret; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 220 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 221 | INIT_DELAYED_WORK(&data->work, gpio_extcon_work); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 222 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 223 | /* |
Moritz Fischer | b51b387 | 2015-12-23 21:34:07 -0800 | [diff] [blame] | 224 | * Request the interrupt of gpio to detect whether external connector |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 225 | * is attached or detached. |
| 226 | */ |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 227 | ret = devm_request_any_context_irq(&pdev->dev, data->irq, |
Chanwoo Choi | ae59f3a | 2015-09-25 22:40:51 +0900 | [diff] [blame] | 228 | gpio_irq_handler, pdata->irq_flags, |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 229 | pdev->name, data); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 230 | if (ret < 0) |
Sangjung Woo | d92c2f1 | 2014-04-21 19:10:10 +0900 | [diff] [blame] | 231 | return ret; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 232 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 233 | platform_set_drvdata(pdev, data); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 234 | /* Perform initial detection */ |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 235 | gpio_extcon_work(&data->work.work); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 236 | |
| 237 | return 0; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 238 | } |
| 239 | |
Bill Pemberton | 93ed032 | 2012-11-19 13:25:49 -0500 | [diff] [blame] | 240 | static int gpio_extcon_remove(struct platform_device *pdev) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 241 | { |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 242 | struct gpio_extcon_data *data = platform_get_drvdata(pdev); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 243 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 244 | cancel_delayed_work_sync(&data->work); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 249 | #ifdef CONFIG_PM_SLEEP |
| 250 | static int gpio_extcon_resume(struct device *dev) |
| 251 | { |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 252 | struct gpio_extcon_data *data; |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 253 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 254 | data = dev_get_drvdata(dev); |
| 255 | if (data->pdata->check_on_resume) |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 256 | queue_delayed_work(system_power_efficient_wq, |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 257 | &data->work, data->debounce_jiffies); |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | #endif |
| 262 | |
Jingoo Han | 3cc731d | 2014-02-27 20:37:15 +0900 | [diff] [blame] | 263 | static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume); |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 264 | |
Subhash Jadavani | e5371bd | 2017-05-08 18:06:42 -0700 | [diff] [blame] | 265 | static const struct of_device_id extcon_gpio_of_match[] = { |
| 266 | { .compatible = "extcon-gpio"}, |
| 267 | {}, |
| 268 | }; |
| 269 | |
H Hartley Sweeten | 2878bda | 2012-05-02 15:38:44 -0700 | [diff] [blame] | 270 | static struct platform_driver gpio_extcon_driver = { |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 271 | .probe = gpio_extcon_probe, |
Bill Pemberton | 5f7e222 | 2012-11-19 13:20:06 -0500 | [diff] [blame] | 272 | .remove = gpio_extcon_remove, |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 273 | .driver = { |
| 274 | .name = "extcon-gpio", |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 275 | .pm = &gpio_extcon_pm_ops, |
Subhash Jadavani | e5371bd | 2017-05-08 18:06:42 -0700 | [diff] [blame] | 276 | .of_match_table = of_match_ptr(extcon_gpio_of_match), |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 277 | }, |
| 278 | }; |
| 279 | |
H Hartley Sweeten | 2878bda | 2012-05-02 15:38:44 -0700 | [diff] [blame] | 280 | module_platform_driver(gpio_extcon_driver); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 281 | |
| 282 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); |
| 283 | MODULE_DESCRIPTION("GPIO extcon driver"); |
| 284 | MODULE_LICENSE("GPL"); |