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> |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 31 | |
| 32 | struct gpio_extcon_data { |
Chanwoo Choi | 60cd62d | 2014-04-21 20:51:08 +0900 | [diff] [blame] | 33 | struct extcon_dev *edev; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 34 | int irq; |
| 35 | struct delayed_work work; |
| 36 | unsigned long debounce_jiffies; |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 37 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 38 | struct gpio_desc *id_gpiod; |
Chanwoo Choi | 6ba1299 | 2015-09-29 22:59:55 +0900 | [diff] [blame] | 39 | struct gpio_extcon_pdata *pdata; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static void gpio_extcon_work(struct work_struct *work) |
| 43 | { |
| 44 | int state; |
| 45 | struct gpio_extcon_data *data = |
| 46 | container_of(to_delayed_work(work), struct gpio_extcon_data, |
| 47 | work); |
| 48 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 49 | state = gpiod_get_value_cansleep(data->id_gpiod); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 50 | if (data->pdata->gpio_active_low) |
Guenter Roeck | 5bfbdc9 | 2013-09-12 08:49:54 +0900 | [diff] [blame] | 51 | state = !state; |
Kishon Vijay Abraham I | cb9850d | 2016-09-15 15:46:11 +0530 | [diff] [blame] | 52 | |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 53 | extcon_set_state_sync(data->edev, data->pdata->extcon_id, state); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static irqreturn_t gpio_irq_handler(int irq, void *dev_id) |
| 57 | { |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 58 | struct gpio_extcon_data *data = dev_id; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 59 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 60 | queue_delayed_work(system_power_efficient_wq, &data->work, |
| 61 | data->debounce_jiffies); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 62 | return IRQ_HANDLED; |
| 63 | } |
| 64 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 65 | static int gpio_extcon_init(struct device *dev, struct gpio_extcon_data *data) |
| 66 | { |
| 67 | struct gpio_extcon_pdata *pdata = data->pdata; |
| 68 | int ret; |
| 69 | |
| 70 | ret = devm_gpio_request_one(dev, pdata->gpio, GPIOF_DIR_IN, |
| 71 | dev_name(dev)); |
| 72 | if (ret < 0) |
| 73 | return ret; |
| 74 | |
| 75 | data->id_gpiod = gpio_to_desc(pdata->gpio); |
| 76 | if (!data->id_gpiod) |
| 77 | return -EINVAL; |
| 78 | |
| 79 | if (pdata->debounce) { |
| 80 | ret = gpiod_set_debounce(data->id_gpiod, |
| 81 | pdata->debounce * 1000); |
| 82 | if (ret < 0) |
| 83 | data->debounce_jiffies = |
| 84 | msecs_to_jiffies(pdata->debounce); |
| 85 | } |
| 86 | |
| 87 | data->irq = gpiod_to_irq(data->id_gpiod); |
| 88 | if (data->irq < 0) |
| 89 | return data->irq; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Bill Pemberton | 44f34fd | 2012-11-19 13:23:21 -0500 | [diff] [blame] | 94 | static int gpio_extcon_probe(struct platform_device *pdev) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 95 | { |
Chanwoo Choi | 6ba1299 | 2015-09-29 22:59:55 +0900 | [diff] [blame] | 96 | struct gpio_extcon_pdata *pdata = dev_get_platdata(&pdev->dev); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 97 | struct gpio_extcon_data *data; |
Guenter Roeck | 1073514 | 2013-08-29 21:29:33 -0700 | [diff] [blame] | 98 | int ret; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 99 | |
| 100 | if (!pdata) |
| 101 | return -EBUSY; |
Chanwoo Choi | ce6f749 | 2015-09-29 22:57:24 +0900 | [diff] [blame] | 102 | if (!pdata->irq_flags || pdata->extcon_id > EXTCON_NONE) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 103 | return -EINVAL; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 104 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 105 | data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data), |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 106 | GFP_KERNEL); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 107 | if (!data) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 108 | return -ENOMEM; |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 109 | data->pdata = pdata; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 110 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 111 | /* Initialize the gpio */ |
| 112 | ret = gpio_extcon_init(&pdev->dev, data); |
| 113 | if (ret < 0) |
| 114 | return ret; |
| 115 | |
| 116 | /* Allocate the memory of extcon devie and register extcon device */ |
Chanwoo Choi | ce6f749 | 2015-09-29 22:57:24 +0900 | [diff] [blame] | 117 | data->edev = devm_extcon_dev_allocate(&pdev->dev, &pdata->extcon_id); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 118 | if (IS_ERR(data->edev)) { |
Chanwoo Choi | 60cd62d | 2014-04-21 20:51:08 +0900 | [diff] [blame] | 119 | dev_err(&pdev->dev, "failed to allocate extcon device\n"); |
| 120 | return -ENOMEM; |
| 121 | } |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 122 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 123 | ret = devm_extcon_dev_register(&pdev->dev, data->edev); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 124 | if (ret < 0) |
Axel Lin | 01eaf24 | 2012-06-16 11:56:24 +0800 | [diff] [blame] | 125 | return ret; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 126 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 127 | INIT_DELAYED_WORK(&data->work, gpio_extcon_work); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 128 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 129 | /* |
Moritz Fischer | b51b387 | 2015-12-23 21:34:07 -0800 | [diff] [blame] | 130 | * Request the interrupt of gpio to detect whether external connector |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 131 | * is attached or detached. |
| 132 | */ |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 133 | ret = devm_request_any_context_irq(&pdev->dev, data->irq, |
Chanwoo Choi | ae59f3a | 2015-09-25 22:40:51 +0900 | [diff] [blame] | 134 | gpio_irq_handler, pdata->irq_flags, |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 135 | pdev->name, data); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 136 | if (ret < 0) |
Sangjung Woo | d92c2f1 | 2014-04-21 19:10:10 +0900 | [diff] [blame] | 137 | return ret; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 138 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 139 | platform_set_drvdata(pdev, data); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 140 | /* Perform initial detection */ |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 141 | gpio_extcon_work(&data->work.work); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 142 | |
| 143 | return 0; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 144 | } |
| 145 | |
Bill Pemberton | 93ed032 | 2012-11-19 13:25:49 -0500 | [diff] [blame] | 146 | static int gpio_extcon_remove(struct platform_device *pdev) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 147 | { |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 148 | struct gpio_extcon_data *data = platform_get_drvdata(pdev); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 149 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 150 | cancel_delayed_work_sync(&data->work); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 155 | #ifdef CONFIG_PM_SLEEP |
| 156 | static int gpio_extcon_resume(struct device *dev) |
| 157 | { |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 158 | struct gpio_extcon_data *data; |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 159 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 160 | data = dev_get_drvdata(dev); |
| 161 | if (data->pdata->check_on_resume) |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 162 | queue_delayed_work(system_power_efficient_wq, |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 163 | &data->work, data->debounce_jiffies); |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | #endif |
| 168 | |
Jingoo Han | 3cc731d | 2014-02-27 20:37:15 +0900 | [diff] [blame] | 169 | 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] | 170 | |
H Hartley Sweeten | 2878bda | 2012-05-02 15:38:44 -0700 | [diff] [blame] | 171 | static struct platform_driver gpio_extcon_driver = { |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 172 | .probe = gpio_extcon_probe, |
Bill Pemberton | 5f7e222 | 2012-11-19 13:20:06 -0500 | [diff] [blame] | 173 | .remove = gpio_extcon_remove, |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 174 | .driver = { |
| 175 | .name = "extcon-gpio", |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 176 | .pm = &gpio_extcon_pm_ops, |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 177 | }, |
| 178 | }; |
| 179 | |
H Hartley Sweeten | 2878bda | 2012-05-02 15:38:44 -0700 | [diff] [blame] | 180 | module_platform_driver(gpio_extcon_driver); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 181 | |
| 182 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); |
| 183 | MODULE_DESCRIPTION("GPIO extcon driver"); |
| 184 | MODULE_LICENSE("GPL"); |