blob: ebed22f22d75701e020716c0bd2c4c1da3722fb1 [file] [log] [blame]
MyungJoo Hambe483082012-04-20 14:16:23 +09001/*
Chanwoo Choi6ba12992015-09-29 22:59:55 +09002 * extcon_gpio.c - Single-state GPIO extcon driver based on extcon class
MyungJoo Hambe483082012-04-20 14:16:23 +09003 *
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 Choi6ba12992015-09-29 22:59:55 +090018 */
MyungJoo Hambe483082012-04-20 14:16:23 +090019
George Cherian62364352014-09-09 09:44:34 +053020#include <linux/extcon.h>
21#include <linux/extcon/extcon-gpio.h>
22#include <linux/gpio.h>
Chanwoo Choide992ac2015-09-30 14:57:57 +090023#include <linux/gpio/consumer.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090024#include <linux/init.h>
25#include <linux/interrupt.h>
George Cherian62364352014-09-09 09:44:34 +053026#include <linux/kernel.h>
27#include <linux/module.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090028#include <linux/platform_device.h>
29#include <linux/slab.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090030#include <linux/workqueue.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090031
32struct gpio_extcon_data {
Chanwoo Choi60cd62d2014-04-21 20:51:08 +090033 struct extcon_dev *edev;
MyungJoo Hambe483082012-04-20 14:16:23 +090034 int irq;
35 struct delayed_work work;
36 unsigned long debounce_jiffies;
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090037
Chanwoo Choide992ac2015-09-30 14:57:57 +090038 struct gpio_desc *id_gpiod;
Chanwoo Choi6ba12992015-09-29 22:59:55 +090039 struct gpio_extcon_pdata *pdata;
MyungJoo Hambe483082012-04-20 14:16:23 +090040};
41
42static 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 Choide992ac2015-09-30 14:57:57 +090049 state = gpiod_get_value_cansleep(data->id_gpiod);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090050 if (data->pdata->gpio_active_low)
Guenter Roeck5bfbdc92013-09-12 08:49:54 +090051 state = !state;
Kishon Vijay Abraham Icb9850d2016-09-15 15:46:11 +053052
Chanwoo Choi8670b452016-08-16 15:55:34 +090053 extcon_set_state_sync(data->edev, data->pdata->extcon_id, 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
Chanwoo Choide992ac2015-09-30 14:57:57 +090065static 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 Pemberton44f34fd2012-11-19 13:23:21 -050094static int gpio_extcon_probe(struct platform_device *pdev)
MyungJoo Hambe483082012-04-20 14:16:23 +090095{
Chanwoo Choi6ba12992015-09-29 22:59:55 +090096 struct gpio_extcon_pdata *pdata = dev_get_platdata(&pdev->dev);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090097 struct gpio_extcon_data *data;
Guenter Roeck10735142013-08-29 21:29:33 -070098 int ret;
MyungJoo Hambe483082012-04-20 14:16:23 +090099
100 if (!pdata)
101 return -EBUSY;
Chanwoo Choice6f7492015-09-29 22:57:24 +0900102 if (!pdata->irq_flags || pdata->extcon_id > EXTCON_NONE)
MyungJoo Hambe483082012-04-20 14:16:23 +0900103 return -EINVAL;
MyungJoo Hambe483082012-04-20 14:16:23 +0900104
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900105 data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
MyungJoo Hambe483082012-04-20 14:16:23 +0900106 GFP_KERNEL);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900107 if (!data)
MyungJoo Hambe483082012-04-20 14:16:23 +0900108 return -ENOMEM;
Chanwoo Choide992ac2015-09-30 14:57:57 +0900109 data->pdata = pdata;
MyungJoo Hambe483082012-04-20 14:16:23 +0900110
Chanwoo Choide992ac2015-09-30 14:57:57 +0900111 /* 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 Choice6f7492015-09-29 22:57:24 +0900117 data->edev = devm_extcon_dev_allocate(&pdev->dev, &pdata->extcon_id);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900118 if (IS_ERR(data->edev)) {
Chanwoo Choi60cd62d2014-04-21 20:51:08 +0900119 dev_err(&pdev->dev, "failed to allocate extcon device\n");
120 return -ENOMEM;
121 }
MyungJoo Hambe483082012-04-20 14:16:23 +0900122
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900123 ret = devm_extcon_dev_register(&pdev->dev, data->edev);
MyungJoo Hambe483082012-04-20 14:16:23 +0900124 if (ret < 0)
Axel Lin01eaf242012-06-16 11:56:24 +0800125 return ret;
MyungJoo Hambe483082012-04-20 14:16:23 +0900126
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900127 INIT_DELAYED_WORK(&data->work, gpio_extcon_work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900128
Chanwoo Choide992ac2015-09-30 14:57:57 +0900129 /*
Moritz Fischerb51b3872015-12-23 21:34:07 -0800130 * Request the interrupt of gpio to detect whether external connector
Chanwoo Choide992ac2015-09-30 14:57:57 +0900131 * is attached or detached.
132 */
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900133 ret = devm_request_any_context_irq(&pdev->dev, data->irq,
Chanwoo Choiae59f3a2015-09-25 22:40:51 +0900134 gpio_irq_handler, pdata->irq_flags,
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900135 pdev->name, data);
MyungJoo Hambe483082012-04-20 14:16:23 +0900136 if (ret < 0)
Sangjung Wood92c2f12014-04-21 19:10:10 +0900137 return ret;
MyungJoo Hambe483082012-04-20 14:16:23 +0900138
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900139 platform_set_drvdata(pdev, data);
MyungJoo Hambe483082012-04-20 14:16:23 +0900140 /* Perform initial detection */
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900141 gpio_extcon_work(&data->work.work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900142
143 return 0;
MyungJoo Hambe483082012-04-20 14:16:23 +0900144}
145
Bill Pemberton93ed0322012-11-19 13:25:49 -0500146static int gpio_extcon_remove(struct platform_device *pdev)
MyungJoo Hambe483082012-04-20 14:16:23 +0900147{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900148 struct gpio_extcon_data *data = platform_get_drvdata(pdev);
MyungJoo Hambe483082012-04-20 14:16:23 +0900149
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900150 cancel_delayed_work_sync(&data->work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900151
152 return 0;
153}
154
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900155#ifdef CONFIG_PM_SLEEP
156static int gpio_extcon_resume(struct device *dev)
157{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900158 struct gpio_extcon_data *data;
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900159
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900160 data = dev_get_drvdata(dev);
161 if (data->pdata->check_on_resume)
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900162 queue_delayed_work(system_power_efficient_wq,
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900163 &data->work, data->debounce_jiffies);
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900164
165 return 0;
166}
167#endif
168
Jingoo Han3cc731d2014-02-27 20:37:15 +0900169static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900170
H Hartley Sweeten2878bda2012-05-02 15:38:44 -0700171static struct platform_driver gpio_extcon_driver = {
MyungJoo Hambe483082012-04-20 14:16:23 +0900172 .probe = gpio_extcon_probe,
Bill Pemberton5f7e2222012-11-19 13:20:06 -0500173 .remove = gpio_extcon_remove,
MyungJoo Hambe483082012-04-20 14:16:23 +0900174 .driver = {
175 .name = "extcon-gpio",
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900176 .pm = &gpio_extcon_pm_ops,
MyungJoo Hambe483082012-04-20 14:16:23 +0900177 },
178};
179
H Hartley Sweeten2878bda2012-05-02 15:38:44 -0700180module_platform_driver(gpio_extcon_driver);
MyungJoo Hambe483082012-04-20 14:16:23 +0900181
182MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
183MODULE_DESCRIPTION("GPIO extcon driver");
184MODULE_LICENSE("GPL");