2 * drivers/extcon/extcon-adc-jack.c
4 * Analog Jack extcon driver with ADC-based detection capability.
6 * Copyright (C) 2016 Samsung Electronics
7 * Chanwoo Choi <cw00.choi@samsung.com>
9 * Copyright (C) 2012 Samsung Electronics
10 * MyungJoo Ham <myungjoo.ham@samsung.com>
12 * Modified for calling to IIO to get adc by <anish.singh@samsung.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/device.h>
23 #include <linux/platform_device.h>
24 #include <linux/err.h>
25 #include <linux/interrupt.h>
26 #include <linux/workqueue.h>
27 #include <linux/iio/consumer.h>
28 #include <linux/extcon/extcon-adc-jack.h>
29 #include <linux/extcon.h>
32 * struct adc_jack_data - internal data for adc_jack device driver
33 * @edev: extcon device.
34 * @cable_names: list of supported cables.
35 * @adc_conditions: list of adc value conditions.
36 * @num_conditions: size of adc_conditions.
37 * @irq: irq number of attach/detach event (0 if not exist).
38 * @handling_delay: interrupt handler will schedule extcon event
39 * handling at handling_delay jiffies.
40 * @handler: extcon event handler called by interrupt handler.
41 * @chan: iio channel being queried.
43 struct adc_jack_data {
44 struct extcon_dev *edev;
46 const unsigned int **cable_names;
47 struct adc_jack_cond *adc_conditions;
51 unsigned long handling_delay; /* in jiffies */
52 struct delayed_work handler;
54 struct iio_channel *chan;
57 static void adc_jack_handler(struct work_struct *work)
59 struct adc_jack_data *data = container_of(to_delayed_work(work),
62 struct adc_jack_cond *def;
66 ret = iio_read_channel_raw(data->chan, &adc_val);
68 dev_err(&data->edev->dev, "read channel() error: %d\n", ret);
72 /* Get state from adc value with adc_conditions */
73 for (i = 0; i < data->num_conditions; i++) {
74 def = &data->adc_conditions[i];
75 if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
76 extcon_set_cable_state_(data->edev, def->id, true);
81 /* Set the detached state if adc value is not included in the range */
82 for (i = 0; i < data->num_conditions; i++) {
83 def = &data->adc_conditions[i];
84 extcon_set_cable_state_(data->edev, def->id, false);
88 static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
90 struct adc_jack_data *data = _data;
92 queue_delayed_work(system_power_efficient_wq,
93 &data->handler, data->handling_delay);
97 static int adc_jack_probe(struct platform_device *pdev)
99 struct adc_jack_data *data;
100 struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
103 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
107 if (!pdata->cable_names) {
108 dev_err(&pdev->dev, "error: cable_names not defined.\n");
112 data->edev = devm_extcon_dev_allocate(&pdev->dev, pdata->cable_names);
113 if (IS_ERR(data->edev)) {
114 dev_err(&pdev->dev, "failed to allocate extcon device\n");
118 if (!pdata->adc_conditions) {
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].id != EXTCON_NONE; i++);
126 data->num_conditions = i;
128 data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel);
129 if (IS_ERR(data->chan))
130 return PTR_ERR(data->chan);
132 data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
134 INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
136 platform_set_drvdata(pdev, data);
138 err = devm_extcon_dev_register(&pdev->dev, data->edev);
142 data->irq = platform_get_irq(pdev, 0);
144 dev_err(&pdev->dev, "platform_get_irq failed\n");
148 err = request_any_context_irq(data->irq, adc_jack_irq_thread,
149 pdata->irq_flags, pdata->name, data);
152 dev_err(&pdev->dev, "error: irq %d\n", data->irq);
159 static int adc_jack_remove(struct platform_device *pdev)
161 struct adc_jack_data *data = platform_get_drvdata(pdev);
163 free_irq(data->irq, data);
164 cancel_work_sync(&data->handler.work);
165 iio_channel_release(data->chan);
170 static struct platform_driver adc_jack_driver = {
171 .probe = adc_jack_probe,
172 .remove = adc_jack_remove,
178 module_platform_driver(adc_jack_driver);
180 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
181 MODULE_DESCRIPTION("ADC Jack extcon driver");
182 MODULE_LICENSE("GPL v2");