blob: 2a89c89dfaf538bb07f7a631811e30c97fc9559e [file] [log] [blame]
MyungJoo Hambe483082012-04-20 14:16:23 +09001/*
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 Cherian62364352014-09-09 09:44:34 +053023#include <linux/extcon.h>
24#include <linux/extcon/extcon-gpio.h>
25#include <linux/gpio.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090026#include <linux/init.h>
27#include <linux/interrupt.h>
George Cherian62364352014-09-09 09:44:34 +053028#include <linux/kernel.h>
29#include <linux/module.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090030#include <linux/platform_device.h>
31#include <linux/slab.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090032#include <linux/workqueue.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090033
34struct gpio_extcon_data {
Chanwoo Choi60cd62d2014-04-21 20:51:08 +090035 struct extcon_dev *edev;
MyungJoo Hambe483082012-04-20 14:16:23 +090036 int irq;
37 struct delayed_work work;
38 unsigned long debounce_jiffies;
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090039
40 struct gpio_extcon_platform_data *pdata;
MyungJoo Hambe483082012-04-20 14:16:23 +090041};
42
43static 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 Choi60f9b9e2015-09-29 20:53:12 +090050 state = gpio_get_value(data->pdata->gpio);
51 if (data->pdata->gpio_active_low)
Guenter Roeck5bfbdc92013-09-12 08:49:54 +090052 state = !state;
Chanwoo Choi60cd62d2014-04-21 20:51:08 +090053 extcon_set_state(data->edev, state);
MyungJoo Hambe483082012-04-20 14:16:23 +090054}
55
56static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
57{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090058 struct gpio_extcon_data *data = dev_id;
MyungJoo Hambe483082012-04-20 14:16:23 +090059
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090060 queue_delayed_work(system_power_efficient_wq, &data->work,
61 data->debounce_jiffies);
MyungJoo Hambe483082012-04-20 14:16:23 +090062 return IRQ_HANDLED;
63}
64
Bill Pemberton44f34fd2012-11-19 13:23:21 -050065static int gpio_extcon_probe(struct platform_device *pdev)
MyungJoo Hambe483082012-04-20 14:16:23 +090066{
Jingoo Han7c0f65582013-09-11 13:22:18 +090067 struct gpio_extcon_platform_data *pdata = dev_get_platdata(&pdev->dev);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090068 struct gpio_extcon_data *data;
Guenter Roeck10735142013-08-29 21:29:33 -070069 int ret;
MyungJoo Hambe483082012-04-20 14:16:23 +090070
71 if (!pdata)
72 return -EBUSY;
73 if (!pdata->irq_flags) {
74 dev_err(&pdev->dev, "IRQ flag is not specified.\n");
75 return -EINVAL;
76 }
77
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090078 data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
MyungJoo Hambe483082012-04-20 14:16:23 +090079 GFP_KERNEL);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090080 if (!data)
MyungJoo Hambe483082012-04-20 14:16:23 +090081 return -ENOMEM;
82
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090083 data->edev = devm_extcon_dev_allocate(&pdev->dev, NULL);
84 if (IS_ERR(data->edev)) {
Chanwoo Choi60cd62d2014-04-21 20:51:08 +090085 dev_err(&pdev->dev, "failed to allocate extcon device\n");
86 return -ENOMEM;
87 }
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090088 data->pdata = pdata;
Chanwoo Choi60cd62d2014-04-21 20:51:08 +090089
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090090 ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio, GPIOF_DIR_IN,
Guenter Roeck4288d9b2013-11-22 09:26:01 -080091 pdev->name);
92 if (ret < 0)
93 return ret;
94
Guenter Roeck338de0c2013-09-10 19:16:18 -070095 if (pdata->debounce) {
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090096 ret = gpio_set_debounce(data->pdata->gpio,
Guenter Roeck338de0c2013-09-10 19:16:18 -070097 pdata->debounce * 1000);
98 if (ret < 0)
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090099 data->debounce_jiffies =
Guenter Roeck338de0c2013-09-10 19:16:18 -0700100 msecs_to_jiffies(pdata->debounce);
101 }
MyungJoo Hambe483082012-04-20 14:16:23 +0900102
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900103 ret = devm_extcon_dev_register(&pdev->dev, data->edev);
MyungJoo Hambe483082012-04-20 14:16:23 +0900104 if (ret < 0)
Axel Lin01eaf242012-06-16 11:56:24 +0800105 return ret;
MyungJoo Hambe483082012-04-20 14:16:23 +0900106
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900107 INIT_DELAYED_WORK(&data->work, gpio_extcon_work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900108
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900109 data->irq = gpio_to_irq(data->pdata->gpio);
110 if (data->irq < 0)
111 return data->irq;
MyungJoo Hambe483082012-04-20 14:16:23 +0900112
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900113 ret = devm_request_any_context_irq(&pdev->dev, data->irq,
Chanwoo Choiae59f3a2015-09-25 22:40:51 +0900114 gpio_irq_handler, pdata->irq_flags,
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900115 pdev->name, data);
MyungJoo Hambe483082012-04-20 14:16:23 +0900116 if (ret < 0)
Sangjung Wood92c2f12014-04-21 19:10:10 +0900117 return ret;
MyungJoo Hambe483082012-04-20 14:16:23 +0900118
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900119 platform_set_drvdata(pdev, data);
MyungJoo Hambe483082012-04-20 14:16:23 +0900120 /* Perform initial detection */
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900121 gpio_extcon_work(&data->work.work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900122
123 return 0;
MyungJoo Hambe483082012-04-20 14:16:23 +0900124}
125
Bill Pemberton93ed0322012-11-19 13:25:49 -0500126static int gpio_extcon_remove(struct platform_device *pdev)
MyungJoo Hambe483082012-04-20 14:16:23 +0900127{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900128 struct gpio_extcon_data *data = platform_get_drvdata(pdev);
MyungJoo Hambe483082012-04-20 14:16:23 +0900129
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900130 cancel_delayed_work_sync(&data->work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900131
132 return 0;
133}
134
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900135#ifdef CONFIG_PM_SLEEP
136static int gpio_extcon_resume(struct device *dev)
137{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900138 struct gpio_extcon_data *data;
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900139
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900140 data = dev_get_drvdata(dev);
141 if (data->pdata->check_on_resume)
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900142 queue_delayed_work(system_power_efficient_wq,
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900143 &data->work, data->debounce_jiffies);
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900144
145 return 0;
146}
147#endif
148
Jingoo Han3cc731d2014-02-27 20:37:15 +0900149static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900150
H Hartley Sweeten2878bda2012-05-02 15:38:44 -0700151static struct platform_driver gpio_extcon_driver = {
MyungJoo Hambe483082012-04-20 14:16:23 +0900152 .probe = gpio_extcon_probe,
Bill Pemberton5f7e2222012-11-19 13:20:06 -0500153 .remove = gpio_extcon_remove,
MyungJoo Hambe483082012-04-20 14:16:23 +0900154 .driver = {
155 .name = "extcon-gpio",
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900156 .pm = &gpio_extcon_pm_ops,
MyungJoo Hambe483082012-04-20 14:16:23 +0900157 },
158};
159
H Hartley Sweeten2878bda2012-05-02 15:38:44 -0700160module_platform_driver(gpio_extcon_driver);
MyungJoo Hambe483082012-04-20 14:16:23 +0900161
162MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
163MODULE_DESCRIPTION("GPIO extcon driver");
164MODULE_LICENSE("GPL");