Skip to content

Commit 1993986

Browse files
anish198519851985gregkh
authored andcommitted
extcon: adc_jack: adc-jack driver to support 3.5 pi or simliar devices
External connector devices that decides connection information based on ADC values may use adc-jack device driver. The user simply needs to provide a table of adc range and connection states. Then, extcon framework will automatically notify others. Changes in V1: added Lars-Peter Clausen suggested changes: Using macros to get rid of boiler plate code such as devm_kzalloc and module_platform_driver.Other changes suggested are related to coding guidelines. Changes in V2: Removed some unnecessary checks and changed the way we are un-regitering extcon and freeing the irq while removing. Changes in V3: Renamed the files to comply with extcon naming. Changes in V4: Added the cancel_work_sync during removing of driver. Changes in V5: Added the dependency of IIO in Kconfig. Changes in V6: Some nitpicks related to naming. Changes in this version: V6 patch version patch broke the build: ERROR: "extcon_cable_name" [drivers/extcon/extcon-adc-jack.ko] undefined! Fixed it in this version. Acked-by: Jonathan Cameron <jic23@kernel.org> Reviewed-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: anish kumar <anish.singh@samsung.com> Signed-off-by: MyungJoo Ham <myungjoo.ham@samsung.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c338bb0 commit 1993986

4 files changed

Lines changed: 276 additions & 0 deletions

File tree

‎drivers/extcon/Kconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ config EXTCON_GPIO
2121
Say Y here to enable GPIO based extcon support. Note that GPIO
2222
extcon supports single state per extcon instance.
2323

24+
config 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+
2430
config EXTCON_MAX77693
2531
tristate "MAX77693 EXTCON Support"
2632
depends on MFD_MAX77693

‎drivers/extcon/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
obj-$(CONFIG_EXTCON) += extcon-class.o
66
obj-$(CONFIG_EXTCON_GPIO) += extcon-gpio.o
7+
obj-$(CONFIG_EXTCON_ADC_JACK) += extcon-adc-jack.o
78
obj-$(CONFIG_EXTCON_MAX77693) += extcon-max77693.o
89
obj-$(CONFIG_EXTCON_MAX8997) += extcon-max8997.o
910
obj-$(CONFIG_EXTCON_ARIZONA) += extcon-arizona.o

‎drivers/extcon/extcon-adc-jack.c‎

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
*/
40+
struct 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+
55+
static 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+
85+
static 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+
93+
static 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+
err = -EINVAL;
107+
dev_err(&pdev->dev, "error: cable_names not defined.\n");
108+
goto out;
109+
}
110+
111+
data->edev.supported_cable = pdata->cable_names;
112+
113+
/* Check the length of array and set num_cables */
114+
for (i = 0; data->edev.supported_cable[i]; i++)
115+
;
116+
if (i == 0 || i > SUPPORTED_CABLE_MAX) {
117+
err = -EINVAL;
118+
dev_err(&pdev->dev, "error: pdata->cable_names size = %d\n",
119+
i - 1);
120+
goto out;
121+
}
122+
data->num_cables = i;
123+
124+
if (!pdata->adc_conditions ||
125+
!pdata->adc_conditions[0].state) {
126+
err = -EINVAL;
127+
dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
128+
goto out;
129+
}
130+
data->adc_conditions = pdata->adc_conditions;
131+
132+
/* Check the length of array and set num_conditions */
133+
for (i = 0; data->adc_conditions[i].state; i++)
134+
;
135+
data->num_conditions = i;
136+
137+
data->chan = iio_channel_get(dev_name(&pdev->dev),
138+
pdata->consumer_channel);
139+
if (IS_ERR(data->chan)) {
140+
err = PTR_ERR(data->chan);
141+
goto out;
142+
}
143+
144+
data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
145+
146+
INIT_DELAYED_WORK_DEFERRABLE(&data->handler, adc_jack_handler);
147+
148+
platform_set_drvdata(pdev, data);
149+
150+
err = extcon_dev_register(&data->edev, &pdev->dev);
151+
if (err)
152+
goto out;
153+
154+
data->irq = platform_get_irq(pdev, 0);
155+
if (!data->irq) {
156+
dev_err(&pdev->dev, "platform_get_irq failed\n");
157+
err = -ENODEV;
158+
goto err_irq;
159+
}
160+
161+
err = request_any_context_irq(data->irq, adc_jack_irq_thread,
162+
pdata->irq_flags, pdata->name, data);
163+
164+
if (err) {
165+
dev_err(&pdev->dev, "error: irq %d\n", data->irq);
166+
err = -EINVAL;
167+
goto err_irq;
168+
}
169+
170+
goto out;
171+
172+
err_irq:
173+
extcon_dev_unregister(&data->edev);
174+
out:
175+
return err;
176+
}
177+
178+
static int __devexit adc_jack_remove(struct platform_device *pdev)
179+
{
180+
struct adc_jack_data *data = platform_get_drvdata(pdev);
181+
182+
free_irq(data->irq, data);
183+
cancel_work_sync(&data->handler.work);
184+
extcon_dev_unregister(&data->edev);
185+
186+
return 0;
187+
}
188+
189+
static struct platform_driver adc_jack_driver = {
190+
.probe = adc_jack_probe,
191+
.remove = __devexit_p(adc_jack_remove),
192+
.driver = {
193+
.name = "adc-jack",
194+
.owner = THIS_MODULE,
195+
},
196+
};
197+
198+
module_platform_driver(adc_jack_driver);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* include/linux/extcon/extcon-adc-jack.h
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+
* This program is free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License version 2 as
11+
* published by the Free Software Foundation.
12+
*
13+
*/
14+
15+
#ifndef _EXTCON_ADC_JACK_H_
16+
#define _EXTCON_ADC_JACK_H_ __FILE__
17+
18+
#include <linux/module.h>
19+
#include <linux/extcon.h>
20+
21+
/**
22+
* struct adc_jack_cond - condition to use an extcon state
23+
* @state - the corresponding extcon state (if 0, this struct denotes
24+
* the last adc_jack_cond element among the array)
25+
* @min_adc - min adc value for this condition
26+
* @max_adc - max adc value for this condition
27+
*
28+
* For example, if { .state = 0x3, .min_adc = 100, .max_adc = 200}, it means
29+
* that if ADC value is between (inclusive) 100 and 200, than the cable 0 and
30+
* 1 are attached (1<<0 | 1<<1 == 0x3)
31+
*
32+
* Note that you don't need to describe condition for "no cable attached"
33+
* because when no adc_jack_cond is met, state = 0 is automatically chosen.
34+
*/
35+
struct adc_jack_cond {
36+
u32 state; /* extcon state value. 0 if invalid */
37+
u32 min_adc;
38+
u32 max_adc;
39+
};
40+
41+
/**
42+
* struct adc_jack_pdata - platform data for adc jack device.
43+
* @name - name of the extcon device. If null, "adc-jack" is used.
44+
* @consumer_channel - Unique name to identify the channel on the consumer
45+
* side. This typically describes the channels used within
46+
* the consumer. E.g. 'battery_voltage'
47+
* @cable_names - array of cable names ending with null.
48+
* @adc_contitions - array of struct adc_jack_cond conditions ending
49+
* with .state = 0 entry. This describes how to decode
50+
* adc values into extcon state.
51+
* @irq_flags - irq flags used for the @irq
52+
* @handling_delay_ms - in some devices, we need to read ADC value some
53+
* milli-seconds after the interrupt occurs. You may
54+
* describe such delays with @handling_delay_ms, which
55+
* is rounded-off by jiffies.
56+
*/
57+
struct adc_jack_pdata {
58+
const char *name;
59+
const char *consumer_channel;
60+
/*
61+
* The last entry should be NULL
62+
*/
63+
const char **cable_names;
64+
/* The last entry's state should be 0 */
65+
struct adc_jack_cond *adc_conditions;
66+
67+
unsigned long irq_flags;
68+
unsigned long handling_delay_ms; /* in ms */
69+
};
70+
71+
#endif /* _EXTCON_ADC_JACK_H */

0 commit comments

Comments
 (0)