2 * drivers/extcon/extcon-adc-jack.c
4 * Analog Jack extcon driver with ADC-based detection capability.
6 * Copyright (C) 2012 Samsung Electronics
7 * MyungJoo Ham <myungjoo.ham@samsung.com>
9 * Modified for calling to IIO to get adc by <anish.singh@samsung.com>
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.
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>
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.
40 struct adc_jack_data
{
41 struct extcon_dev edev
;
43 const char **cable_names
;
45 struct adc_jack_cond
*adc_conditions
;
49 unsigned long handling_delay
; /* in jiffies */
50 struct delayed_work handler
;
52 struct iio_channel
*chan
;
55 static void adc_jack_handler(struct work_struct
*work
)
57 struct adc_jack_data
*data
= container_of(to_delayed_work(work
),
64 ret
= iio_read_channel_raw(data
->chan
, &adc_val
);
66 dev_err(data
->edev
.dev
, "read channel() error: %d\n", ret
);
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
];
75 if (def
->min_adc
<= adc_val
&& def
->max_adc
>= adc_val
) {
80 /* if no def has met, it means state = 0 (no cables attached) */
82 extcon_set_state(&data
->edev
, state
);
85 static irqreturn_t
adc_jack_irq_thread(int irq
, void *_data
)
87 struct adc_jack_data
*data
= _data
;
89 schedule_delayed_work(&data
->handler
, data
->handling_delay
);
93 static int __devinit
adc_jack_probe(struct platform_device
*pdev
)
95 struct adc_jack_data
*data
;
96 struct adc_jack_pdata
*pdata
= pdev
->dev
.platform_data
;
99 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
103 data
->edev
.name
= pdata
->name
;
105 if (!pdata
->cable_names
) {
107 dev_err(&pdev
->dev
, "error: cable_names not defined.\n");
111 data
->edev
.supported_cable
= pdata
->cable_names
;
113 /* Check the length of array and set num_cables */
114 for (i
= 0; data
->edev
.supported_cable
[i
]; i
++)
116 if (i
== 0 || i
> SUPPORTED_CABLE_MAX
) {
118 dev_err(&pdev
->dev
, "error: pdata->cable_names size = %d\n",
122 data
->num_cables
= i
;
124 if (!pdata
->adc_conditions
||
125 !pdata
->adc_conditions
[0].state
) {
127 dev_err(&pdev
->dev
, "error: adc_conditions not defined.\n");
130 data
->adc_conditions
= pdata
->adc_conditions
;
132 /* Check the length of array and set num_conditions */
133 for (i
= 0; data
->adc_conditions
[i
].state
; i
++)
135 data
->num_conditions
= i
;
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
);
144 data
->handling_delay
= msecs_to_jiffies(pdata
->handling_delay_ms
);
146 INIT_DEFERRABLE_WORK(&data
->handler
, adc_jack_handler
);
148 platform_set_drvdata(pdev
, data
);
150 err
= extcon_dev_register(&data
->edev
, &pdev
->dev
);
154 data
->irq
= platform_get_irq(pdev
, 0);
156 dev_err(&pdev
->dev
, "platform_get_irq failed\n");
161 err
= request_any_context_irq(data
->irq
, adc_jack_irq_thread
,
162 pdata
->irq_flags
, pdata
->name
, data
);
165 dev_err(&pdev
->dev
, "error: irq %d\n", data
->irq
);
173 extcon_dev_unregister(&data
->edev
);
178 static int __devexit
adc_jack_remove(struct platform_device
*pdev
)
180 struct adc_jack_data
*data
= platform_get_drvdata(pdev
);
182 free_irq(data
->irq
, data
);
183 cancel_work_sync(&data
->handler
.work
);
184 extcon_dev_unregister(&data
->edev
);
189 static struct platform_driver adc_jack_driver
= {
190 .probe
= adc_jack_probe
,
191 .remove
= __devexit_p(adc_jack_remove
),
194 .owner
= THIS_MODULE
,
198 module_platform_driver(adc_jack_driver
);