blob: 279ff8f6637da8beb62cd92bbbe63b8759c896a6 [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;
Chanwoo Choi60cd62d2014-04-21 20:51:08 +090052 extcon_set_state(data->edev, state);
MyungJoo Hambe483082012-04-20 14:16:23 +090053}
54
55static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
56{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090057 struct gpio_extcon_data *data = dev_id;
MyungJoo Hambe483082012-04-20 14:16:23 +090058
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090059 queue_delayed_work(system_power_efficient_wq, &data->work,
60 data->debounce_jiffies);
MyungJoo Hambe483082012-04-20 14:16:23 +090061 return IRQ_HANDLED;
62}
63
Chanwoo Choide992ac2015-09-30 14:57:57 +090064static 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 Pemberton44f34fd2012-11-19 13:23:21 -050093static int gpio_extcon_probe(struct platform_device *pdev)
MyungJoo Hambe483082012-04-20 14:16:23 +090094{
Chanwoo Choi6ba12992015-09-29 22:59:55 +090095 struct gpio_extcon_pdata *pdata = dev_get_platdata(&pdev->dev);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090096 struct gpio_extcon_data *data;
Guenter Roeck10735142013-08-29 21:29:33 -070097 int ret;
MyungJoo Hambe483082012-04-20 14:16:23 +090098
99 if (!pdata)
100 return -EBUSY;
Chanwoo Choice6f7492015-09-29 22:57:24 +0900101 if (!pdata->irq_flags || pdata->extcon_id > EXTCON_NONE)
MyungJoo Hambe483082012-04-20 14:16:23 +0900102 return -EINVAL;
MyungJoo Hambe483082012-04-20 14:16:23 +0900103
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900104 data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
MyungJoo Hambe483082012-04-20 14:16:23 +0900105 GFP_KERNEL);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900106 if (!data)
MyungJoo Hambe483082012-04-20 14:16:23 +0900107 return -ENOMEM;
Chanwoo Choide992ac2015-09-30 14:57:57 +0900108 data->pdata = pdata;
MyungJoo Hambe483082012-04-20 14:16:23 +0900109
Chanwoo Choide992ac2015-09-30 14:57:57 +0900110 /* 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 Choice6f7492015-09-29 22:57:24 +0900116 data->edev = devm_extcon_dev_allocate(&pdev->dev, &pdata->extcon_id);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900117 if (IS_ERR(data->edev)) {
Chanwoo Choi60cd62d2014-04-21 20:51:08 +0900118 dev_err(&pdev->dev, "failed to allocate extcon device\n");
119 return -ENOMEM;
120 }
MyungJoo Hambe483082012-04-20 14:16:23 +0900121
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900122 ret = devm_extcon_dev_register(&pdev->dev, data->edev);
MyungJoo Hambe483082012-04-20 14:16:23 +0900123 if (ret < 0)
Axel Lin01eaf242012-06-16 11:56:24 +0800124 return ret;
MyungJoo Hambe483082012-04-20 14:16:23 +0900125
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900126 INIT_DELAYED_WORK(&data->work, gpio_extcon_work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900127
Chanwoo Choide992ac2015-09-30 14:57:57 +0900128 /*
129 * Request the interrput of gpio to detect whether external connector
130 * is attached or detached.
131 */
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900132 ret = devm_request_any_context_irq(&pdev->dev, data->irq,
Chanwoo Choiae59f3a2015-09-25 22:40:51 +0900133 gpio_irq_handler, pdata->irq_flags,
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900134 pdev->name, data);
MyungJoo Hambe483082012-04-20 14:16:23 +0900135 if (ret < 0)
Sangjung Wood92c2f12014-04-21 19:10:10 +0900136 return ret;
MyungJoo Hambe483082012-04-20 14:16:23 +0900137
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900138 platform_set_drvdata(pdev, data);
MyungJoo Hambe483082012-04-20 14:16:23 +0900139 /* Perform initial detection */
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900140 gpio_extcon_work(&data->work.work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900141
142 return 0;
MyungJoo Hambe483082012-04-20 14:16:23 +0900143}
144
Bill Pemberton93ed0322012-11-19 13:25:49 -0500145static int gpio_extcon_remove(struct platform_device *pdev)
MyungJoo Hambe483082012-04-20 14:16:23 +0900146{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900147 struct gpio_extcon_data *data = platform_get_drvdata(pdev);
MyungJoo Hambe483082012-04-20 14:16:23 +0900148
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900149 cancel_delayed_work_sync(&data->work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900150
151 return 0;
152}
153
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900154#ifdef CONFIG_PM_SLEEP
155static int gpio_extcon_resume(struct device *dev)
156{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900157 struct gpio_extcon_data *data;
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900158
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900159 data = dev_get_drvdata(dev);
160 if (data->pdata->check_on_resume)
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900161 queue_delayed_work(system_power_efficient_wq,
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900162 &data->work, data->debounce_jiffies);
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900163
164 return 0;
165}
166#endif
167
Jingoo Han3cc731d2014-02-27 20:37:15 +0900168static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900169
H Hartley Sweeten2878bda2012-05-02 15:38:44 -0700170static struct platform_driver gpio_extcon_driver = {
MyungJoo Hambe483082012-04-20 14:16:23 +0900171 .probe = gpio_extcon_probe,
Bill Pemberton5f7e2222012-11-19 13:20:06 -0500172 .remove = gpio_extcon_remove,
MyungJoo Hambe483082012-04-20 14:16:23 +0900173 .driver = {
174 .name = "extcon-gpio",
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900175 .pm = &gpio_extcon_pm_ops,
MyungJoo Hambe483082012-04-20 14:16:23 +0900176 },
177};
178
H Hartley Sweeten2878bda2012-05-02 15:38:44 -0700179module_platform_driver(gpio_extcon_driver);
MyungJoo Hambe483082012-04-20 14:16:23 +0900180
181MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
182MODULE_DESCRIPTION("GPIO extcon driver");
183MODULE_LICENSE("GPL");