0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/devm-helpers.h>
0013 #include <linux/extcon-provider.h>
0014 #include <linux/gpio/consumer.h>
0015 #include <linux/init.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/kernel.h>
0018 #include <linux/module.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/slab.h>
0021 #include <linux/workqueue.h>
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 struct gpio_extcon_data {
0036 struct extcon_dev *edev;
0037 struct delayed_work work;
0038 unsigned long debounce_jiffies;
0039 struct gpio_desc *gpiod;
0040 unsigned int extcon_id;
0041 unsigned long debounce;
0042 bool check_on_resume;
0043 };
0044
0045 static void gpio_extcon_work(struct work_struct *work)
0046 {
0047 int state;
0048 struct gpio_extcon_data *data =
0049 container_of(to_delayed_work(work), struct gpio_extcon_data,
0050 work);
0051
0052 state = gpiod_get_value_cansleep(data->gpiod);
0053 extcon_set_state_sync(data->edev, data->extcon_id, state);
0054 }
0055
0056 static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
0057 {
0058 struct gpio_extcon_data *data = dev_id;
0059
0060 queue_delayed_work(system_power_efficient_wq, &data->work,
0061 data->debounce_jiffies);
0062 return IRQ_HANDLED;
0063 }
0064
0065 static int gpio_extcon_probe(struct platform_device *pdev)
0066 {
0067 struct gpio_extcon_data *data;
0068 struct device *dev = &pdev->dev;
0069 unsigned long irq_flags;
0070 int irq;
0071 int ret;
0072
0073 data = devm_kzalloc(dev, sizeof(struct gpio_extcon_data), GFP_KERNEL);
0074 if (!data)
0075 return -ENOMEM;
0076
0077
0078
0079
0080
0081
0082
0083
0084 if (data->extcon_id > EXTCON_NONE)
0085 return -EINVAL;
0086
0087 data->gpiod = devm_gpiod_get(dev, "extcon", GPIOD_IN);
0088 if (IS_ERR(data->gpiod))
0089 return PTR_ERR(data->gpiod);
0090 irq = gpiod_to_irq(data->gpiod);
0091 if (irq <= 0)
0092 return irq;
0093
0094
0095
0096
0097
0098
0099
0100 if (gpiod_is_active_low(data->gpiod))
0101 irq_flags = IRQF_TRIGGER_FALLING;
0102 else
0103 irq_flags = IRQF_TRIGGER_RISING;
0104
0105
0106 data->edev = devm_extcon_dev_allocate(dev, &data->extcon_id);
0107 if (IS_ERR(data->edev)) {
0108 dev_err(dev, "failed to allocate extcon device\n");
0109 return -ENOMEM;
0110 }
0111
0112 ret = devm_extcon_dev_register(dev, data->edev);
0113 if (ret < 0)
0114 return ret;
0115
0116 ret = devm_delayed_work_autocancel(dev, &data->work, gpio_extcon_work);
0117 if (ret)
0118 return ret;
0119
0120
0121
0122
0123
0124 ret = devm_request_any_context_irq(dev, irq,
0125 gpio_irq_handler, irq_flags,
0126 pdev->name, data);
0127 if (ret < 0)
0128 return ret;
0129
0130 platform_set_drvdata(pdev, data);
0131
0132 gpio_extcon_work(&data->work.work);
0133
0134 return 0;
0135 }
0136
0137 #ifdef CONFIG_PM_SLEEP
0138 static int gpio_extcon_resume(struct device *dev)
0139 {
0140 struct gpio_extcon_data *data;
0141
0142 data = dev_get_drvdata(dev);
0143 if (data->check_on_resume)
0144 queue_delayed_work(system_power_efficient_wq,
0145 &data->work, data->debounce_jiffies);
0146
0147 return 0;
0148 }
0149 #endif
0150
0151 static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
0152
0153 static struct platform_driver gpio_extcon_driver = {
0154 .probe = gpio_extcon_probe,
0155 .driver = {
0156 .name = "extcon-gpio",
0157 .pm = &gpio_extcon_pm_ops,
0158 },
0159 };
0160
0161 module_platform_driver(gpio_extcon_driver);
0162
0163 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
0164 MODULE_DESCRIPTION("GPIO extcon driver");
0165 MODULE_LICENSE("GPL");