aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/extcon
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/extcon')
-rw-r--r--drivers/extcon/Kconfig6
-rw-r--r--drivers/extcon/Makefile1
-rw-r--r--drivers/extcon/extcon-adc-jack.c195
3 files changed, 202 insertions, 0 deletions
diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig
index e175c8ed4ec4..dd5b01b957aa 100644
--- a/drivers/extcon/Kconfig
+++ b/drivers/extcon/Kconfig
@@ -21,6 +21,12 @@ config EXTCON_GPIO
21 Say Y here to enable GPIO based extcon support. Note that GPIO 21 Say Y here to enable GPIO based extcon support. Note that GPIO
22 extcon supports single state per extcon instance. 22 extcon supports single state per extcon instance.
23 23
24config EXTCON_ADC_JACK
25 tristate "ADC Jack extcon support"
26 depends on IIO
27 help
28 Say Y here to enable extcon device driver based on ADC values.
29
24config EXTCON_MAX77693 30config EXTCON_MAX77693
25 tristate "MAX77693 EXTCON Support" 31 tristate "MAX77693 EXTCON Support"
26 depends on MFD_MAX77693 32 depends on MFD_MAX77693
diff --git a/drivers/extcon/Makefile b/drivers/extcon/Makefile
index 88961b332348..53d88c1cce36 100644
--- a/drivers/extcon/Makefile
+++ b/drivers/extcon/Makefile
@@ -4,6 +4,7 @@
4 4
5obj-$(CONFIG_EXTCON) += extcon_class.o 5obj-$(CONFIG_EXTCON) += extcon_class.o
6obj-$(CONFIG_EXTCON_GPIO) += extcon_gpio.o 6obj-$(CONFIG_EXTCON_GPIO) += extcon_gpio.o
7obj-$(CONFIG_EXTCON_ADC_JACK) += extcon-adc-jack.o
7obj-$(CONFIG_EXTCON_MAX77693) += extcon-max77693.o 8obj-$(CONFIG_EXTCON_MAX77693) += extcon-max77693.o
8obj-$(CONFIG_EXTCON_MAX8997) += extcon-max8997.o 9obj-$(CONFIG_EXTCON_MAX8997) += extcon-max8997.o
9obj-$(CONFIG_EXTCON_ARIZONA) += extcon-arizona.o 10obj-$(CONFIG_EXTCON_ARIZONA) += extcon-arizona.o
diff --git a/drivers/extcon/extcon-adc-jack.c b/drivers/extcon/extcon-adc-jack.c
new file mode 100644
index 000000000000..da058f7033ef
--- /dev/null
+++ b/drivers/extcon/extcon-adc-jack.c
@@ -0,0 +1,195 @@
1/*
2 * drivers/extcon/extcon-adc-jack.c
3 *
4 * Analog Jack extcon driver with ADC-based detection capability.
5 *
6 * Copyright (C) 2012 Samsung Electronics
7 * MyungJoo Ham <myungjoo.ham@samsung.com>
8 *
9 * Modified for calling to IIO to get adc by <anish.singh@samsung.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16
17#include <linux/slab.h>
18#include <linux/device.h>
19#include <linux/platform_device.h>
20#include <linux/err.h>
21#include <linux/interrupt.h>
22#include <linux/workqueue.h>
23#include <linux/iio/consumer.h>
24#include <linux/extcon/extcon-adc-jack.h>
25#include <linux/extcon.h>
26
27/**
28 * struct adc_jack_data - internal data for adc_jack device driver
29 * @edev - extcon device.
30 * @cable_names - list of supported cables.
31 * @num_cables - size of cable_names.
32 * @adc_conditions - list of adc value conditions.
33 * @num_conditions - size of adc_conditions.
34 * @irq - irq number of attach/detach event (0 if not exist).
35 * @handling_delay - interrupt handler will schedule extcon event
36 * handling at handling_delay jiffies.
37 * @handler - extcon event handler called by interrupt handler.
38 * @chan - iio channel being queried.
39 */
40struct adc_jack_data {
41 struct extcon_dev edev;
42
43 const char **cable_names;
44 int num_cables;
45 struct adc_jack_cond *adc_conditions;
46 int num_conditions;
47
48 int irq;
49 unsigned long handling_delay; /* in jiffies */
50 struct delayed_work handler;
51
52 struct iio_channel *chan;
53};
54
55static void adc_jack_handler(struct work_struct *work)
56{
57 struct adc_jack_data *data = container_of(to_delayed_work(work),
58 struct adc_jack_data,
59 handler);
60 u32 state = 0;
61 int ret, adc_val;
62 int i;
63
64 ret = iio_read_channel_raw(data->chan, &adc_val);
65 if (ret < 0) {
66 dev_err(data->edev.dev, "read channel() error: %d\n", ret);
67 return;
68 }
69
70 /* Get state from adc value with adc_conditions */
71 for (i = 0; i < data->num_conditions; i++) {
72 struct adc_jack_cond *def = &data->adc_conditions[i];
73 if (!def->state)
74 break;
75 if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
76 state = def->state;
77 break;
78 }
79 }
80 /* if no def has met, it means state = 0 (no cables attached) */
81
82 extcon_set_state(&data->edev, state);
83}
84
85static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
86{
87 struct adc_jack_data *data = _data;
88
89 schedule_delayed_work(&data->handler, data->handling_delay);
90 return IRQ_HANDLED;
91}
92
93static int __devinit adc_jack_probe(struct platform_device *pdev)
94{
95 struct adc_jack_data *data;
96 struct adc_jack_pdata *pdata = pdev->dev.platform_data;
97 int i, err = 0;
98
99 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
100 if (!data)
101 return -ENOMEM;
102
103 data->edev.name = pdata->name;
104
105 if (pdata->cable_names)
106 data->edev.supported_cable = pdata->cable_names;
107 else
108 data->edev.supported_cable = extcon_cable_name;
109
110 /* Check the length of array and set num_cables */
111 for (i = 0; data->edev.supported_cable[i]; i++)
112 ;
113 if (i == 0 || i > SUPPORTED_CABLE_MAX) {
114 err = -EINVAL;
115 dev_err(&pdev->dev, "error: pdata->cable_names size = %d\n",
116 i - 1);
117 goto out;
118 }
119 data->num_cables = i;
120
121 if (!pdata->adc_conditions ||
122 !pdata->adc_conditions[0].state) {
123 err = -EINVAL;
124 dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
125 goto out;
126 }
127 data->adc_conditions = pdata->adc_conditions;
128
129 /* Check the length of array and set num_conditions */
130 for (i = 0; data->adc_conditions[i].state; i++)
131 ;
132 data->num_conditions = i;
133
134 data->chan = iio_channel_get(dev_name(&pdev->dev),
135 pdata->consumer_channel);
136 if (IS_ERR(data->chan)) {
137 err = PTR_ERR(data->chan);
138 goto out;
139 }
140
141 data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
142
143 INIT_DELAYED_WORK_DEFERRABLE(&data->handler, adc_jack_handler);
144
145 platform_set_drvdata(pdev, data);
146
147 err = extcon_dev_register(&data->edev, &pdev->dev);
148 if (err)
149 goto out;
150
151 data->irq = platform_get_irq(pdev, 0);
152 if (!data->irq) {
153 dev_err(&pdev->dev, "platform_get_irq failed\n");
154 err = -ENODEV;
155 goto err_irq;
156 }
157
158 err = request_any_context_irq(data->irq, adc_jack_irq_thread,
159 pdata->irq_flags, pdata->name, data);
160
161 if (err) {
162 dev_err(&pdev->dev, "error: irq %d\n", data->irq);
163 err = -EINVAL;
164 goto err_irq;
165 }
166
167 goto out;
168
169err_irq:
170 extcon_dev_unregister(&data->edev);
171out:
172 return err;
173}
174
175static int __devexit adc_jack_remove(struct platform_device *pdev)
176{
177 struct adc_jack_data *data = platform_get_drvdata(pdev);
178
179 free_irq(data->irq, data);
180 cancel_work_sync(&data->handler.work);
181 extcon_dev_unregister(&data->edev);
182
183 return 0;
184}
185
186static struct platform_driver adc_jack_driver = {
187 .probe = adc_jack_probe,
188 .remove = __devexit_p(adc_jack_remove),
189 .driver = {
190 .name = "adc-jack",
191 .owner = THIS_MODULE,
192 },
193};
194
195module_platform_driver(adc_jack_driver);