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/module.h>
18 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/err.h>
22 #include <linux/interrupt.h>
23 #include <linux/workqueue.h>
24 #include <linux/iio/consumer.h>
25 #include <linux/extcon/extcon-adc-jack.h>
26 #include <linux/extcon.h>
29 * struct adc_jack_data - internal data for adc_jack device driver
30 * @edev - extcon device.
31 * @cable_names - list of supported cables.
32 * @num_cables - size of cable_names.
33 * @adc_conditions - list of adc value conditions.
34 * @num_conditions - size of adc_conditions.
35 * @irq - irq number of attach/detach event (0 if not exist).
36 * @handling_delay - interrupt handler will schedule extcon event
37 * handling at handling_delay jiffies.
38 * @handler - extcon event handler called by interrupt handler.
39 * @chan - iio channel being queried.
41 struct adc_jack_data
{
42 struct extcon_dev edev
;
44 const char **cable_names
;
46 struct adc_jack_cond
*adc_conditions
;
50 unsigned long handling_delay
; /* in jiffies */
51 struct delayed_work handler
;
53 struct iio_channel
*chan
;
56 static void adc_jack_handler(struct work_struct
*work
)
58 struct adc_jack_data
*data
= container_of(to_delayed_work(work
),
65 ret
= iio_read_channel_raw(data
->chan
, &adc_val
);
67 dev_err(data
->edev
.dev
, "read channel() error: %d\n", ret
);
71 /* Get state from adc value with adc_conditions */
72 for (i
= 0; i
< data
->num_conditions
; i
++) {
73 struct adc_jack_cond
*def
= &data
->adc_conditions
[i
];
76 if (def
->min_adc
<= adc_val
&& def
->max_adc
>= adc_val
) {
81 /* if no def has met, it means state = 0 (no cables attached) */
83 extcon_set_state(&data
->edev
, state
);
86 static irqreturn_t
adc_jack_irq_thread(int irq
, void *_data
)
88 struct adc_jack_data
*data
= _data
;
90 schedule_delayed_work(&data
->handler
, data
->handling_delay
);
94 static int adc_jack_probe(struct platform_device
*pdev
)
96 struct adc_jack_data
*data
;
97 struct adc_jack_pdata
*pdata
= pdev
->dev
.platform_data
;
100 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
104 data
->edev
.name
= pdata
->name
;
106 if (!pdata
->cable_names
) {
108 dev_err(&pdev
->dev
, "error: cable_names not defined.\n");
112 data
->edev
.supported_cable
= pdata
->cable_names
;
114 /* Check the length of array and set num_cables */
115 for (i
= 0; data
->edev
.supported_cable
[i
]; i
++)
117 if (i
== 0 || i
> SUPPORTED_CABLE_MAX
) {
119 dev_err(&pdev
->dev
, "error: pdata->cable_names size = %d\n",
123 data
->num_cables
= i
;
125 if (!pdata
->adc_conditions
||
126 !pdata
->adc_conditions
[0].state
) {
128 dev_err(&pdev
->dev
, "error: adc_conditions not defined.\n");
131 data
->adc_conditions
= pdata
->adc_conditions
;
133 /* Check the length of array and set num_conditions */
134 for (i
= 0; data
->adc_conditions
[i
].state
; i
++)
136 data
->num_conditions
= i
;
138 data
->chan
= iio_channel_get(&pdev
->dev
, 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
);
172 extcon_dev_unregister(&data
->edev
);
177 static int adc_jack_remove(struct platform_device
*pdev
)
179 struct adc_jack_data
*data
= platform_get_drvdata(pdev
);
181 free_irq(data
->irq
, data
);
182 cancel_work_sync(&data
->handler
.work
);
183 extcon_dev_unregister(&data
->edev
);
188 static struct platform_driver adc_jack_driver
= {
189 .probe
= adc_jack_probe
,
190 .remove
= adc_jack_remove
,
193 .owner
= THIS_MODULE
,
197 module_platform_driver(adc_jack_driver
);
199 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
200 MODULE_DESCRIPTION("ADC Jack extcon driver");
201 MODULE_LICENSE("GPL v2");