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 * @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
{
42 struct extcon_dev
*edev
;
44 const unsigned int **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
;
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 queue_delayed_work(system_power_efficient_wq
,
91 &data
->handler
, data
->handling_delay
);
95 static int adc_jack_probe(struct platform_device
*pdev
)
97 struct adc_jack_data
*data
;
98 struct adc_jack_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
101 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
105 if (!pdata
->cable_names
) {
106 dev_err(&pdev
->dev
, "error: cable_names not defined.\n");
110 data
->dev
= &pdev
->dev
;
111 data
->edev
= devm_extcon_dev_allocate(&pdev
->dev
, pdata
->cable_names
);
112 if (IS_ERR(data
->edev
)) {
113 dev_err(&pdev
->dev
, "failed to allocate extcon device\n");
117 if (!pdata
->adc_conditions
||
118 !pdata
->adc_conditions
[0].state
) {
119 dev_err(&pdev
->dev
, "error: adc_conditions not defined.\n");
122 data
->adc_conditions
= pdata
->adc_conditions
;
124 /* Check the length of array and set num_conditions */
125 for (i
= 0; data
->adc_conditions
[i
].state
; i
++)
127 data
->num_conditions
= i
;
129 data
->chan
= iio_channel_get(&pdev
->dev
, pdata
->consumer_channel
);
130 if (IS_ERR(data
->chan
))
131 return PTR_ERR(data
->chan
);
133 data
->handling_delay
= msecs_to_jiffies(pdata
->handling_delay_ms
);
134 data
->wakeup_source
= pdata
->wakeup_source
;
136 INIT_DEFERRABLE_WORK(&data
->handler
, adc_jack_handler
);
138 platform_set_drvdata(pdev
, data
);
140 err
= devm_extcon_dev_register(&pdev
->dev
, data
->edev
);
144 data
->irq
= platform_get_irq(pdev
, 0);
146 dev_err(&pdev
->dev
, "platform_get_irq failed\n");
150 err
= request_any_context_irq(data
->irq
, adc_jack_irq_thread
,
151 pdata
->irq_flags
, pdata
->name
, data
);
154 dev_err(&pdev
->dev
, "error: irq %d\n", data
->irq
);
158 if (data
->wakeup_source
)
159 device_init_wakeup(&pdev
->dev
, 1);
164 static int adc_jack_remove(struct platform_device
*pdev
)
166 struct adc_jack_data
*data
= platform_get_drvdata(pdev
);
168 free_irq(data
->irq
, data
);
169 cancel_work_sync(&data
->handler
.work
);
170 iio_channel_release(data
->chan
);
175 #ifdef CONFIG_PM_SLEEP
176 static int adc_jack_suspend(struct device
*dev
)
178 struct adc_jack_data
*data
= dev_get_drvdata(dev
);
180 cancel_delayed_work_sync(&data
->handler
);
181 if (device_may_wakeup(data
->dev
))
182 enable_irq_wake(data
->irq
);
187 static int adc_jack_resume(struct device
*dev
)
189 struct adc_jack_data
*data
= dev_get_drvdata(dev
);
191 if (device_may_wakeup(data
->dev
))
192 disable_irq_wake(data
->irq
);
196 #endif /* CONFIG_PM_SLEEP */
198 static SIMPLE_DEV_PM_OPS(adc_jack_pm_ops
,
199 adc_jack_suspend
, adc_jack_resume
);
201 static struct platform_driver adc_jack_driver
= {
202 .probe
= adc_jack_probe
,
203 .remove
= adc_jack_remove
,
206 .pm
= &adc_jack_pm_ops
,
210 module_platform_driver(adc_jack_driver
);
212 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
213 MODULE_DESCRIPTION("ADC Jack extcon driver");
214 MODULE_LICENSE("GPL v2");