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; |
Chanwoo Choi | 60cd62d | 2014-04-21 20:51:08 +0900 | [diff] [blame] | 52 | extcon_set_state(data->edev, state); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static irqreturn_t gpio_irq_handler(int irq, void *dev_id) |
| 56 | { |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 57 | struct gpio_extcon_data *data = dev_id; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 58 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 59 | queue_delayed_work(system_power_efficient_wq, &data->work, |
| 60 | data->debounce_jiffies); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 61 | return IRQ_HANDLED; |
| 62 | } |
| 63 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 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 | |
Bill Pemberton | 44f34fd | 2012-11-19 13:23:21 -0500 | [diff] [blame] | 93 | static int gpio_extcon_probe(struct platform_device *pdev) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 94 | { |
Chanwoo Choi | 6ba1299 | 2015-09-29 22:59:55 +0900 | [diff] [blame] | 95 | struct gpio_extcon_pdata *pdata = dev_get_platdata(&pdev->dev); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 96 | struct gpio_extcon_data *data; |
Guenter Roeck | 1073514 | 2013-08-29 21:29:33 -0700 | [diff] [blame] | 97 | int ret; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 98 | |
| 99 | if (!pdata) |
| 100 | return -EBUSY; |
Chanwoo Choi | ce6f749 | 2015-09-29 22:57:24 +0900 | [diff] [blame] | 101 | if (!pdata->irq_flags || pdata->extcon_id > EXTCON_NONE) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 102 | return -EINVAL; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 103 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 104 | data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data), |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 105 | GFP_KERNEL); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 106 | if (!data) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 107 | return -ENOMEM; |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 108 | data->pdata = pdata; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 109 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 110 | /* Initialize the gpio */ |
| 111 | ret = gpio_extcon_init(&pdev->dev, data); |
| 112 | if (ret < 0) |
| 113 | return ret; |
| 114 | |
| 115 | /* Allocate the memory of extcon devie and register extcon device */ |
Chanwoo Choi | ce6f749 | 2015-09-29 22:57:24 +0900 | [diff] [blame] | 116 | data->edev = devm_extcon_dev_allocate(&pdev->dev, &pdata->extcon_id); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 117 | if (IS_ERR(data->edev)) { |
Chanwoo Choi | 60cd62d | 2014-04-21 20:51:08 +0900 | [diff] [blame] | 118 | dev_err(&pdev->dev, "failed to allocate extcon device\n"); |
| 119 | return -ENOMEM; |
| 120 | } |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 121 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 122 | ret = devm_extcon_dev_register(&pdev->dev, data->edev); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 123 | if (ret < 0) |
Axel Lin | 01eaf24 | 2012-06-16 11:56:24 +0800 | [diff] [blame] | 124 | return ret; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 125 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 126 | INIT_DELAYED_WORK(&data->work, gpio_extcon_work); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 127 | |
Chanwoo Choi | de992ac | 2015-09-30 14:57:57 +0900 | [diff] [blame] | 128 | /* |
| 129 | * Request the interrput of gpio to detect whether external connector |
| 130 | * is attached or detached. |
| 131 | */ |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 132 | ret = devm_request_any_context_irq(&pdev->dev, data->irq, |
Chanwoo Choi | ae59f3a | 2015-09-25 22:40:51 +0900 | [diff] [blame] | 133 | gpio_irq_handler, pdata->irq_flags, |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 134 | pdev->name, data); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 135 | if (ret < 0) |
Sangjung Woo | d92c2f1 | 2014-04-21 19:10:10 +0900 | [diff] [blame] | 136 | return ret; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 137 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 138 | platform_set_drvdata(pdev, data); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 139 | /* Perform initial detection */ |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 140 | gpio_extcon_work(&data->work.work); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 141 | |
| 142 | return 0; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Bill Pemberton | 93ed032 | 2012-11-19 13:25:49 -0500 | [diff] [blame] | 145 | static int gpio_extcon_remove(struct platform_device *pdev) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 146 | { |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 147 | struct gpio_extcon_data *data = platform_get_drvdata(pdev); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 148 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 149 | cancel_delayed_work_sync(&data->work); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 154 | #ifdef CONFIG_PM_SLEEP |
| 155 | static int gpio_extcon_resume(struct device *dev) |
| 156 | { |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 157 | struct gpio_extcon_data *data; |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 158 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 159 | data = dev_get_drvdata(dev); |
| 160 | if (data->pdata->check_on_resume) |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 161 | queue_delayed_work(system_power_efficient_wq, |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 162 | &data->work, data->debounce_jiffies); |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | #endif |
| 167 | |
Jingoo Han | 3cc731d | 2014-02-27 20:37:15 +0900 | [diff] [blame] | 168 | 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] | 169 | |
H Hartley Sweeten | 2878bda | 2012-05-02 15:38:44 -0700 | [diff] [blame] | 170 | static struct platform_driver gpio_extcon_driver = { |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 171 | .probe = gpio_extcon_probe, |
Bill Pemberton | 5f7e222 | 2012-11-19 13:20:06 -0500 | [diff] [blame] | 172 | .remove = gpio_extcon_remove, |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 173 | .driver = { |
| 174 | .name = "extcon-gpio", |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 175 | .pm = &gpio_extcon_pm_ops, |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 176 | }, |
| 177 | }; |
| 178 | |
H Hartley Sweeten | 2878bda | 2012-05-02 15:38:44 -0700 | [diff] [blame] | 179 | module_platform_driver(gpio_extcon_driver); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 180 | |
| 181 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); |
| 182 | MODULE_DESCRIPTION("GPIO extcon driver"); |
| 183 | MODULE_LICENSE("GPL"); |