MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 1 | /* |
| 2 | * drivers/extcon/extcon_gpio.c |
| 3 | * |
| 4 | * Single-state GPIO extcon driver based on extcon class |
| 5 | * |
| 6 | * Copyright (C) 2008 Google, Inc. |
| 7 | * Author: Mike Lockwood <lockwood@android.com> |
| 8 | * |
| 9 | * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon |
| 10 | * (originally switch class is supported) |
| 11 | * |
| 12 | * This software is licensed under the terms of the GNU General Public |
| 13 | * License version 2, as published by the Free Software Foundation, and |
| 14 | * may be copied, distributed, and modified under those terms. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | */ |
| 22 | |
George Cherian | 6236435 | 2014-09-09 09:44:34 +0530 | [diff] [blame] | 23 | #include <linux/extcon.h> |
| 24 | #include <linux/extcon/extcon-gpio.h> |
| 25 | #include <linux/gpio.h> |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 26 | #include <linux/init.h> |
| 27 | #include <linux/interrupt.h> |
George Cherian | 6236435 | 2014-09-09 09:44:34 +0530 | [diff] [blame] | 28 | #include <linux/kernel.h> |
| 29 | #include <linux/module.h> |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/slab.h> |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 32 | #include <linux/workqueue.h> |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 33 | |
| 34 | struct gpio_extcon_data { |
Chanwoo Choi | 60cd62d | 2014-04-21 20:51:08 +0900 | [diff] [blame] | 35 | struct extcon_dev *edev; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 36 | int irq; |
| 37 | struct delayed_work work; |
| 38 | unsigned long debounce_jiffies; |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 39 | |
| 40 | struct gpio_extcon_platform_data *pdata; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static void gpio_extcon_work(struct work_struct *work) |
| 44 | { |
| 45 | int state; |
| 46 | struct gpio_extcon_data *data = |
| 47 | container_of(to_delayed_work(work), struct gpio_extcon_data, |
| 48 | work); |
| 49 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 50 | state = gpio_get_value(data->pdata->gpio); |
| 51 | if (data->pdata->gpio_active_low) |
Guenter Roeck | 5bfbdc9 | 2013-09-12 08:49:54 +0900 | [diff] [blame] | 52 | state = !state; |
Chanwoo Choi | 60cd62d | 2014-04-21 20:51:08 +0900 | [diff] [blame] | 53 | extcon_set_state(data->edev, 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 | |
Bill Pemberton | 44f34fd | 2012-11-19 13:23:21 -0500 | [diff] [blame] | 65 | static int gpio_extcon_probe(struct platform_device *pdev) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 66 | { |
Jingoo Han | 7c0f6558 | 2013-09-11 13:22:18 +0900 | [diff] [blame] | 67 | struct gpio_extcon_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 68 | struct gpio_extcon_data *data; |
Guenter Roeck | 1073514 | 2013-08-29 21:29:33 -0700 | [diff] [blame] | 69 | int ret; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 70 | |
| 71 | if (!pdata) |
| 72 | return -EBUSY; |
Chanwoo Choi | ce6f749 | 2015-09-29 22:57:24 +0900 | [diff] [blame^] | 73 | if (!pdata->irq_flags || pdata->extcon_id > EXTCON_NONE) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 74 | return -EINVAL; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 75 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 76 | data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data), |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 77 | GFP_KERNEL); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 78 | if (!data) |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 79 | return -ENOMEM; |
| 80 | |
Chanwoo Choi | ce6f749 | 2015-09-29 22:57:24 +0900 | [diff] [blame^] | 81 | data->edev = devm_extcon_dev_allocate(&pdev->dev, &pdata->extcon_id); |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 82 | if (IS_ERR(data->edev)) { |
Chanwoo Choi | 60cd62d | 2014-04-21 20:51:08 +0900 | [diff] [blame] | 83 | dev_err(&pdev->dev, "failed to allocate extcon device\n"); |
| 84 | return -ENOMEM; |
| 85 | } |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 86 | data->pdata = pdata; |
Chanwoo Choi | 60cd62d | 2014-04-21 20:51:08 +0900 | [diff] [blame] | 87 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 88 | ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio, GPIOF_DIR_IN, |
Guenter Roeck | 4288d9b | 2013-11-22 09:26:01 -0800 | [diff] [blame] | 89 | pdev->name); |
| 90 | if (ret < 0) |
| 91 | return ret; |
| 92 | |
Guenter Roeck | 338de0c | 2013-09-10 19:16:18 -0700 | [diff] [blame] | 93 | if (pdata->debounce) { |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 94 | ret = gpio_set_debounce(data->pdata->gpio, |
Guenter Roeck | 338de0c | 2013-09-10 19:16:18 -0700 | [diff] [blame] | 95 | pdata->debounce * 1000); |
| 96 | if (ret < 0) |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 97 | data->debounce_jiffies = |
Guenter Roeck | 338de0c | 2013-09-10 19:16:18 -0700 | [diff] [blame] | 98 | msecs_to_jiffies(pdata->debounce); |
| 99 | } |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 100 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 101 | ret = devm_extcon_dev_register(&pdev->dev, data->edev); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 102 | if (ret < 0) |
Axel Lin | 01eaf24 | 2012-06-16 11:56:24 +0800 | [diff] [blame] | 103 | return ret; |
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 | INIT_DELAYED_WORK(&data->work, gpio_extcon_work); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 106 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 107 | data->irq = gpio_to_irq(data->pdata->gpio); |
| 108 | if (data->irq < 0) |
| 109 | return data->irq; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 110 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 111 | ret = devm_request_any_context_irq(&pdev->dev, data->irq, |
Chanwoo Choi | ae59f3a | 2015-09-25 22:40:51 +0900 | [diff] [blame] | 112 | gpio_irq_handler, pdata->irq_flags, |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 113 | pdev->name, data); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 114 | if (ret < 0) |
Sangjung Woo | d92c2f1 | 2014-04-21 19:10:10 +0900 | [diff] [blame] | 115 | return ret; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 116 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 117 | platform_set_drvdata(pdev, data); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 118 | /* Perform initial detection */ |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 119 | gpio_extcon_work(&data->work.work); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 120 | |
| 121 | return 0; |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 122 | } |
| 123 | |
Bill Pemberton | 93ed032 | 2012-11-19 13:25:49 -0500 | [diff] [blame] | 124 | static int gpio_extcon_remove(struct platform_device *pdev) |
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 | struct gpio_extcon_data *data = platform_get_drvdata(pdev); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 127 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 128 | cancel_delayed_work_sync(&data->work); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 133 | #ifdef CONFIG_PM_SLEEP |
| 134 | static int gpio_extcon_resume(struct device *dev) |
| 135 | { |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 136 | struct gpio_extcon_data *data; |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 137 | |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 138 | data = dev_get_drvdata(dev); |
| 139 | if (data->pdata->check_on_resume) |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 140 | queue_delayed_work(system_power_efficient_wq, |
Chanwoo Choi | 60f9b9e | 2015-09-29 20:53:12 +0900 | [diff] [blame] | 141 | &data->work, data->debounce_jiffies); |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | #endif |
| 146 | |
Jingoo Han | 3cc731d | 2014-02-27 20:37:15 +0900 | [diff] [blame] | 147 | 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] | 148 | |
H Hartley Sweeten | 2878bda | 2012-05-02 15:38:44 -0700 | [diff] [blame] | 149 | static struct platform_driver gpio_extcon_driver = { |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 150 | .probe = gpio_extcon_probe, |
Bill Pemberton | 5f7e222 | 2012-11-19 13:20:06 -0500 | [diff] [blame] | 151 | .remove = gpio_extcon_remove, |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 152 | .driver = { |
| 153 | .name = "extcon-gpio", |
Rongjun Ying | 6544dfa | 2014-01-09 09:50:13 +0900 | [diff] [blame] | 154 | .pm = &gpio_extcon_pm_ops, |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 155 | }, |
| 156 | }; |
| 157 | |
H Hartley Sweeten | 2878bda | 2012-05-02 15:38:44 -0700 | [diff] [blame] | 158 | module_platform_driver(gpio_extcon_driver); |
MyungJoo Ham | be48308 | 2012-04-20 14:16:23 +0900 | [diff] [blame] | 159 | |
| 160 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); |
| 161 | MODULE_DESCRIPTION("GPIO extcon driver"); |
| 162 | MODULE_LICENSE("GPL"); |