blob: 27de6aee890f6dd7dde39a4d03f6b3b17ade5efd [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>
MyungJoo Hambe483082012-04-20 14:16:23 +090023#include <linux/init.h>
24#include <linux/interrupt.h>
George Cherian62364352014-09-09 09:44:34 +053025#include <linux/kernel.h>
26#include <linux/module.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090027#include <linux/platform_device.h>
28#include <linux/slab.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090029#include <linux/workqueue.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090030
31struct gpio_extcon_data {
Chanwoo Choi60cd62d2014-04-21 20:51:08 +090032 struct extcon_dev *edev;
MyungJoo Hambe483082012-04-20 14:16:23 +090033 int irq;
34 struct delayed_work work;
35 unsigned long debounce_jiffies;
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090036
Chanwoo Choi6ba12992015-09-29 22:59:55 +090037 struct gpio_extcon_pdata *pdata;
MyungJoo Hambe483082012-04-20 14:16:23 +090038};
39
40static void gpio_extcon_work(struct work_struct *work)
41{
42 int state;
43 struct gpio_extcon_data *data =
44 container_of(to_delayed_work(work), struct gpio_extcon_data,
45 work);
46
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090047 state = gpio_get_value(data->pdata->gpio);
48 if (data->pdata->gpio_active_low)
Guenter Roeck5bfbdc92013-09-12 08:49:54 +090049 state = !state;
Chanwoo Choi60cd62d2014-04-21 20:51:08 +090050 extcon_set_state(data->edev, state);
MyungJoo Hambe483082012-04-20 14:16:23 +090051}
52
53static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
54{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090055 struct gpio_extcon_data *data = dev_id;
MyungJoo Hambe483082012-04-20 14:16:23 +090056
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090057 queue_delayed_work(system_power_efficient_wq, &data->work,
58 data->debounce_jiffies);
MyungJoo Hambe483082012-04-20 14:16:23 +090059 return IRQ_HANDLED;
60}
61
Bill Pemberton44f34fd2012-11-19 13:23:21 -050062static int gpio_extcon_probe(struct platform_device *pdev)
MyungJoo Hambe483082012-04-20 14:16:23 +090063{
Chanwoo Choi6ba12992015-09-29 22:59:55 +090064 struct gpio_extcon_pdata *pdata = dev_get_platdata(&pdev->dev);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090065 struct gpio_extcon_data *data;
Guenter Roeck10735142013-08-29 21:29:33 -070066 int ret;
MyungJoo Hambe483082012-04-20 14:16:23 +090067
68 if (!pdata)
69 return -EBUSY;
Chanwoo Choice6f7492015-09-29 22:57:24 +090070 if (!pdata->irq_flags || pdata->extcon_id > EXTCON_NONE)
MyungJoo Hambe483082012-04-20 14:16:23 +090071 return -EINVAL;
MyungJoo Hambe483082012-04-20 14:16:23 +090072
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090073 data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
MyungJoo Hambe483082012-04-20 14:16:23 +090074 GFP_KERNEL);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090075 if (!data)
MyungJoo Hambe483082012-04-20 14:16:23 +090076 return -ENOMEM;
77
Chanwoo Choice6f7492015-09-29 22:57:24 +090078 data->edev = devm_extcon_dev_allocate(&pdev->dev, &pdata->extcon_id);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090079 if (IS_ERR(data->edev)) {
Chanwoo Choi60cd62d2014-04-21 20:51:08 +090080 dev_err(&pdev->dev, "failed to allocate extcon device\n");
81 return -ENOMEM;
82 }
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090083 data->pdata = pdata;
Chanwoo Choi60cd62d2014-04-21 20:51:08 +090084
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090085 ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio, GPIOF_DIR_IN,
Guenter Roeck4288d9b2013-11-22 09:26:01 -080086 pdev->name);
87 if (ret < 0)
88 return ret;
89
Guenter Roeck338de0c2013-09-10 19:16:18 -070090 if (pdata->debounce) {
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090091 ret = gpio_set_debounce(data->pdata->gpio,
Guenter Roeck338de0c2013-09-10 19:16:18 -070092 pdata->debounce * 1000);
93 if (ret < 0)
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090094 data->debounce_jiffies =
Guenter Roeck338de0c2013-09-10 19:16:18 -070095 msecs_to_jiffies(pdata->debounce);
96 }
MyungJoo Hambe483082012-04-20 14:16:23 +090097
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090098 ret = devm_extcon_dev_register(&pdev->dev, data->edev);
MyungJoo Hambe483082012-04-20 14:16:23 +090099 if (ret < 0)
Axel Lin01eaf242012-06-16 11:56:24 +0800100 return ret;
MyungJoo Hambe483082012-04-20 14:16:23 +0900101
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900102 INIT_DELAYED_WORK(&data->work, gpio_extcon_work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900103
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900104 data->irq = gpio_to_irq(data->pdata->gpio);
105 if (data->irq < 0)
106 return data->irq;
MyungJoo Hambe483082012-04-20 14:16:23 +0900107
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900108 ret = devm_request_any_context_irq(&pdev->dev, data->irq,
Chanwoo Choiae59f3a2015-09-25 22:40:51 +0900109 gpio_irq_handler, pdata->irq_flags,
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900110 pdev->name, data);
MyungJoo Hambe483082012-04-20 14:16:23 +0900111 if (ret < 0)
Sangjung Wood92c2f12014-04-21 19:10:10 +0900112 return ret;
MyungJoo Hambe483082012-04-20 14:16:23 +0900113
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900114 platform_set_drvdata(pdev, data);
MyungJoo Hambe483082012-04-20 14:16:23 +0900115 /* Perform initial detection */
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900116 gpio_extcon_work(&data->work.work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900117
118 return 0;
MyungJoo Hambe483082012-04-20 14:16:23 +0900119}
120
Bill Pemberton93ed0322012-11-19 13:25:49 -0500121static int gpio_extcon_remove(struct platform_device *pdev)
MyungJoo Hambe483082012-04-20 14:16:23 +0900122{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900123 struct gpio_extcon_data *data = platform_get_drvdata(pdev);
MyungJoo Hambe483082012-04-20 14:16:23 +0900124
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900125 cancel_delayed_work_sync(&data->work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900126
127 return 0;
128}
129
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900130#ifdef CONFIG_PM_SLEEP
131static int gpio_extcon_resume(struct device *dev)
132{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900133 struct gpio_extcon_data *data;
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900134
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900135 data = dev_get_drvdata(dev);
136 if (data->pdata->check_on_resume)
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900137 queue_delayed_work(system_power_efficient_wq,
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900138 &data->work, data->debounce_jiffies);
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900139
140 return 0;
141}
142#endif
143
Jingoo Han3cc731d2014-02-27 20:37:15 +0900144static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900145
H Hartley Sweeten2878bda2012-05-02 15:38:44 -0700146static struct platform_driver gpio_extcon_driver = {
MyungJoo Hambe483082012-04-20 14:16:23 +0900147 .probe = gpio_extcon_probe,
Bill Pemberton5f7e2222012-11-19 13:20:06 -0500148 .remove = gpio_extcon_remove,
MyungJoo Hambe483082012-04-20 14:16:23 +0900149 .driver = {
150 .name = "extcon-gpio",
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900151 .pm = &gpio_extcon_pm_ops,
MyungJoo Hambe483082012-04-20 14:16:23 +0900152 },
153};
154
H Hartley Sweeten2878bda2012-05-02 15:38:44 -0700155module_platform_driver(gpio_extcon_driver);
MyungJoo Hambe483082012-04-20 14:16:23 +0900156
157MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
158MODULE_DESCRIPTION("GPIO extcon driver");
159MODULE_LICENSE("GPL");