blob: 4105df74f2b06f74dee219d86d9f390af58a3144 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
MyungJoo Hambe483082012-04-20 14:16:23 +09002/*
Chanwoo Choi6ba12992015-09-29 22:59:55 +09003 * extcon_gpio.c - Single-state GPIO extcon driver based on extcon class
MyungJoo Hambe483082012-04-20 14:16:23 +09004 *
5 * Copyright (C) 2008 Google, Inc.
6 * Author: Mike Lockwood <lockwood@android.com>
7 *
8 * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon
9 * (originally switch class is supported)
Chanwoo Choi6ba12992015-09-29 22:59:55 +090010 */
MyungJoo Hambe483082012-04-20 14:16:23 +090011
Matti Vaittinenf94a5be2021-03-23 15:57:08 +020012#include <linux/devm-helpers.h>
Chanwoo Choi176aa362017-09-21 12:11:24 +090013#include <linux/extcon-provider.h>
Chanwoo Choide992ac2015-09-30 14:57:57 +090014#include <linux/gpio/consumer.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090015#include <linux/init.h>
16#include <linux/interrupt.h>
George Cherian62364352014-09-09 09:44:34 +053017#include <linux/kernel.h>
18#include <linux/module.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090019#include <linux/platform_device.h>
20#include <linux/slab.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090021#include <linux/workqueue.h>
MyungJoo Hambe483082012-04-20 14:16:23 +090022
Linus Walleij66afded2018-02-12 09:53:12 +010023/**
Linus Walleija62300d2018-02-12 09:53:13 +010024 * struct gpio_extcon_data - A simple GPIO-controlled extcon device state container.
25 * @edev: Extcon device.
Linus Walleija62300d2018-02-12 09:53:13 +010026 * @work: Work fired by the interrupt.
27 * @debounce_jiffies: Number of jiffies to wait for the GPIO to stabilize, from the debounce
28 * value.
Linus Walleijd368e7d2018-02-12 09:53:14 +010029 * @gpiod: GPIO descriptor for this external connector.
Linus Walleij66afded2018-02-12 09:53:12 +010030 * @extcon_id: The unique id of specific external connector.
Linus Walleij66afded2018-02-12 09:53:12 +010031 * @debounce: Debounce time for GPIO IRQ in ms.
Linus Walleij66afded2018-02-12 09:53:12 +010032 * @check_on_resume: Boolean describing whether to check the state of gpio
33 * while resuming from sleep.
34 */
Linus Walleija62300d2018-02-12 09:53:13 +010035struct gpio_extcon_data {
36 struct extcon_dev *edev;
Linus Walleija62300d2018-02-12 09:53:13 +010037 struct delayed_work work;
38 unsigned long debounce_jiffies;
Linus Walleijd368e7d2018-02-12 09:53:14 +010039 struct gpio_desc *gpiod;
Linus Walleij66afded2018-02-12 09:53:12 +010040 unsigned int extcon_id;
Linus Walleij66afded2018-02-12 09:53:12 +010041 unsigned long debounce;
Linus Walleij66afded2018-02-12 09:53:12 +010042 bool check_on_resume;
43};
44
MyungJoo Hambe483082012-04-20 14:16:23 +090045static void gpio_extcon_work(struct work_struct *work)
46{
47 int state;
48 struct gpio_extcon_data *data =
49 container_of(to_delayed_work(work), struct gpio_extcon_data,
50 work);
51
Linus Walleijd368e7d2018-02-12 09:53:14 +010052 state = gpiod_get_value_cansleep(data->gpiod);
Linus Walleija62300d2018-02-12 09:53:13 +010053 extcon_set_state_sync(data->edev, data->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
Bill Pemberton44f34fd2012-11-19 13:23:21 -050065static int gpio_extcon_probe(struct platform_device *pdev)
MyungJoo Hambe483082012-04-20 14:16:23 +090066{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090067 struct gpio_extcon_data *data;
Linus Walleijd368e7d2018-02-12 09:53:14 +010068 struct device *dev = &pdev->dev;
Linus Walleij8bc48102019-05-30 20:39:32 +020069 unsigned long irq_flags;
70 int irq;
Guenter Roeck10735142013-08-29 21:29:33 -070071 int ret;
MyungJoo Hambe483082012-04-20 14:16:23 +090072
Linus Walleijd368e7d2018-02-12 09:53:14 +010073 data = devm_kzalloc(dev, sizeof(struct gpio_extcon_data), GFP_KERNEL);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +090074 if (!data)
MyungJoo Hambe483082012-04-20 14:16:23 +090075 return -ENOMEM;
Linus Walleija62300d2018-02-12 09:53:13 +010076
77 /*
78 * FIXME: extcon_id represents the unique identifier of external
79 * connectors such as EXTCON_USB, EXTCON_DISP_HDMI and so on. extcon_id
80 * is necessary to register the extcon device. But, it's not yet
81 * developed to get the extcon id from device-tree or others.
82 * On later, it have to be solved.
83 */
Linus Walleij8bc48102019-05-30 20:39:32 +020084 if (data->extcon_id > EXTCON_NONE)
Linus Walleija62300d2018-02-12 09:53:13 +010085 return -EINVAL;
MyungJoo Hambe483082012-04-20 14:16:23 +090086
Linus Walleijd368e7d2018-02-12 09:53:14 +010087 data->gpiod = devm_gpiod_get(dev, "extcon", GPIOD_IN);
88 if (IS_ERR(data->gpiod))
89 return PTR_ERR(data->gpiod);
Linus Walleij8bc48102019-05-30 20:39:32 +020090 irq = gpiod_to_irq(data->gpiod);
91 if (irq <= 0)
92 return irq;
93
94 /*
95 * It is unlikely that this is an acknowledged interrupt that goes
96 * away after handling, what we are looking for are falling edges
97 * if the signal is active low, and rising edges if the signal is
98 * active high.
99 */
100 if (gpiod_is_active_low(data->gpiod))
101 irq_flags = IRQF_TRIGGER_FALLING;
102 else
103 irq_flags = IRQF_TRIGGER_RISING;
Chanwoo Choide992ac2015-09-30 14:57:57 +0900104
105 /* Allocate the memory of extcon devie and register extcon device */
Linus Walleijd368e7d2018-02-12 09:53:14 +0100106 data->edev = devm_extcon_dev_allocate(dev, &data->extcon_id);
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900107 if (IS_ERR(data->edev)) {
Linus Walleijd368e7d2018-02-12 09:53:14 +0100108 dev_err(dev, "failed to allocate extcon device\n");
Chanwoo Choi60cd62d2014-04-21 20:51:08 +0900109 return -ENOMEM;
110 }
MyungJoo Hambe483082012-04-20 14:16:23 +0900111
Linus Walleijd368e7d2018-02-12 09:53:14 +0100112 ret = devm_extcon_dev_register(dev, data->edev);
MyungJoo Hambe483082012-04-20 14:16:23 +0900113 if (ret < 0)
Axel Lin01eaf242012-06-16 11:56:24 +0800114 return ret;
MyungJoo Hambe483082012-04-20 14:16:23 +0900115
Matti Vaittinenf94a5be2021-03-23 15:57:08 +0200116 ret = devm_delayed_work_autocancel(dev, &data->work, gpio_extcon_work);
117 if (ret)
118 return ret;
MyungJoo Hambe483082012-04-20 14:16:23 +0900119
Chanwoo Choide992ac2015-09-30 14:57:57 +0900120 /*
Moritz Fischerb51b3872015-12-23 21:34:07 -0800121 * Request the interrupt of gpio to detect whether external connector
Chanwoo Choide992ac2015-09-30 14:57:57 +0900122 * is attached or detached.
123 */
Linus Walleij8bc48102019-05-30 20:39:32 +0200124 ret = devm_request_any_context_irq(dev, irq,
125 gpio_irq_handler, irq_flags,
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900126 pdev->name, data);
MyungJoo Hambe483082012-04-20 14:16:23 +0900127 if (ret < 0)
Sangjung Wood92c2f12014-04-21 19:10:10 +0900128 return ret;
MyungJoo Hambe483082012-04-20 14:16:23 +0900129
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900130 platform_set_drvdata(pdev, data);
MyungJoo Hambe483082012-04-20 14:16:23 +0900131 /* Perform initial detection */
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900132 gpio_extcon_work(&data->work.work);
MyungJoo Hambe483082012-04-20 14:16:23 +0900133
134 return 0;
MyungJoo Hambe483082012-04-20 14:16:23 +0900135}
136
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900137#ifdef CONFIG_PM_SLEEP
138static int gpio_extcon_resume(struct device *dev)
139{
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900140 struct gpio_extcon_data *data;
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900141
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900142 data = dev_get_drvdata(dev);
Linus Walleija62300d2018-02-12 09:53:13 +0100143 if (data->check_on_resume)
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900144 queue_delayed_work(system_power_efficient_wq,
Chanwoo Choi60f9b9e2015-09-29 20:53:12 +0900145 &data->work, data->debounce_jiffies);
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900146
147 return 0;
148}
149#endif
150
Jingoo Han3cc731d2014-02-27 20:37:15 +0900151static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900152
H Hartley Sweeten2878bda2012-05-02 15:38:44 -0700153static struct platform_driver gpio_extcon_driver = {
MyungJoo Hambe483082012-04-20 14:16:23 +0900154 .probe = gpio_extcon_probe,
MyungJoo Hambe483082012-04-20 14:16:23 +0900155 .driver = {
156 .name = "extcon-gpio",
Rongjun Ying6544dfa2014-01-09 09:50:13 +0900157 .pm = &gpio_extcon_pm_ops,
MyungJoo Hambe483082012-04-20 14:16:23 +0900158 },
159};
160
H Hartley Sweeten2878bda2012-05-02 15:38:44 -0700161module_platform_driver(gpio_extcon_driver);
MyungJoo Hambe483082012-04-20 14:16:23 +0900162
163MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
164MODULE_DESCRIPTION("GPIO extcon driver");
165MODULE_LICENSE("GPL");