extcon: Use the extcon_set_state_sync() instead of deprecated functions
[deliverable/linux.git] / drivers / extcon / extcon-adc-jack.c
CommitLineData
19939860 1/*
2 * drivers/extcon/extcon-adc-jack.c
3 *
4 * Analog Jack extcon driver with ADC-based detection capability.
5 *
a7da72ee
CC
6 * Copyright (C) 2016 Samsung Electronics
7 * Chanwoo Choi <cw00.choi@samsung.com>
8 *
19939860 9 * Copyright (C) 2012 Samsung Electronics
10 * MyungJoo Ham <myungjoo.ham@samsung.com>
11 *
12 * Modified for calling to IIO to get adc by <anish.singh@samsung.com>
13 *
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.
17 *
18 */
19
d9310e35 20#include <linux/module.h>
19939860 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>
30
31/**
32 * struct adc_jack_data - internal data for adc_jack device driver
a75e1c73
CC
33 * @edev: extcon device.
34 * @cable_names: list of supported cables.
a75e1c73
CC
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.
19939860 42 */
43struct adc_jack_data {
1b6cf310 44 struct device *dev;
1876fd9a 45 struct extcon_dev *edev;
19939860 46
73b6ecdb 47 const unsigned int **cable_names;
19939860 48 struct adc_jack_cond *adc_conditions;
49 int num_conditions;
50
51 int irq;
52 unsigned long handling_delay; /* in jiffies */
53 struct delayed_work handler;
54
55 struct iio_channel *chan;
1b6cf310 56 bool wakeup_source;
19939860 57};
58
59static void adc_jack_handler(struct work_struct *work)
60{
61 struct adc_jack_data *data = container_of(to_delayed_work(work),
62 struct adc_jack_data,
63 handler);
a7da72ee 64 struct adc_jack_cond *def;
19939860 65 int ret, adc_val;
66 int i;
67
68 ret = iio_read_channel_raw(data->chan, &adc_val);
69 if (ret < 0) {
1876fd9a 70 dev_err(&data->edev->dev, "read channel() error: %d\n", ret);
19939860 71 return;
72 }
73
74 /* Get state from adc value with adc_conditions */
75 for (i = 0; i < data->num_conditions; i++) {
a7da72ee 76 def = &data->adc_conditions[i];
19939860 77 if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
8670b459 78 extcon_set_state_sync(data->edev, def->id, true);
a7da72ee 79 return;
19939860 80 }
81 }
19939860 82
a7da72ee
CC
83 /* Set the detached state if adc value is not included in the range */
84 for (i = 0; i < data->num_conditions; i++) {
85 def = &data->adc_conditions[i];
8670b459 86 extcon_set_state_sync(data->edev, def->id, false);
a7da72ee 87 }
19939860 88}
89
90static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
91{
92 struct adc_jack_data *data = _data;
93
1a82e81e
MB
94 queue_delayed_work(system_power_efficient_wq,
95 &data->handler, data->handling_delay);
19939860 96 return IRQ_HANDLED;
97}
98
44f34fd4 99static int adc_jack_probe(struct platform_device *pdev)
19939860 100{
101 struct adc_jack_data *data;
7c0f6558 102 struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
19939860 103 int i, err = 0;
104
105 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
106 if (!data)
107 return -ENOMEM;
108
19939860 109 if (!pdata->cable_names) {
19939860 110 dev_err(&pdev->dev, "error: cable_names not defined.\n");
4b5dd738 111 return -EINVAL;
19939860 112 }
113
1b6cf310 114 data->dev = &pdev->dev;
1876fd9a
CC
115 data->edev = devm_extcon_dev_allocate(&pdev->dev, pdata->cable_names);
116 if (IS_ERR(data->edev)) {
117 dev_err(&pdev->dev, "failed to allocate extcon device\n");
118 return -ENOMEM;
119 }
19939860 120
a7da72ee 121 if (!pdata->adc_conditions) {
19939860 122 dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
4b5dd738 123 return -EINVAL;
19939860 124 }
125 data->adc_conditions = pdata->adc_conditions;
126
127 /* Check the length of array and set num_conditions */
a7da72ee 128 for (i = 0; data->adc_conditions[i].id != EXTCON_NONE; i++);
19939860 129 data->num_conditions = i;
130
5aa57f0a 131 data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel);
4b5dd738
SW
132 if (IS_ERR(data->chan))
133 return PTR_ERR(data->chan);
19939860 134
135 data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
1b6cf310 136 data->wakeup_source = pdata->wakeup_source;
19939860 137
033d9959 138 INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
19939860 139
140 platform_set_drvdata(pdev, data);
141
1876fd9a 142 err = devm_extcon_dev_register(&pdev->dev, data->edev);
19939860 143 if (err)
4b5dd738 144 return err;
19939860 145
146 data->irq = platform_get_irq(pdev, 0);
147 if (!data->irq) {
148 dev_err(&pdev->dev, "platform_get_irq failed\n");
4b5dd738 149 return -ENODEV;
19939860 150 }
151
152 err = request_any_context_irq(data->irq, adc_jack_irq_thread,
153 pdata->irq_flags, pdata->name, data);
154
03019759 155 if (err < 0) {
19939860 156 dev_err(&pdev->dev, "error: irq %d\n", data->irq);
4b5dd738 157 return err;
19939860 158 }
159
1b6cf310
VRT
160 if (data->wakeup_source)
161 device_init_wakeup(&pdev->dev, 1);
162
ba4b2715 163 adc_jack_handler(&data->handler.work);
03019759 164 return 0;
19939860 165}
166
93ed0327 167static int adc_jack_remove(struct platform_device *pdev)
19939860 168{
169 struct adc_jack_data *data = platform_get_drvdata(pdev);
170
171 free_irq(data->irq, data);
172 cancel_work_sync(&data->handler.work);
5a696d97 173 iio_channel_release(data->chan);
19939860 174
175 return 0;
176}
177
1b6cf310
VRT
178#ifdef CONFIG_PM_SLEEP
179static int adc_jack_suspend(struct device *dev)
180{
181 struct adc_jack_data *data = dev_get_drvdata(dev);
182
183 cancel_delayed_work_sync(&data->handler);
184 if (device_may_wakeup(data->dev))
185 enable_irq_wake(data->irq);
186
187 return 0;
188}
189
190static int adc_jack_resume(struct device *dev)
191{
192 struct adc_jack_data *data = dev_get_drvdata(dev);
193
194 if (device_may_wakeup(data->dev))
195 disable_irq_wake(data->irq);
196
197 return 0;
198}
199#endif /* CONFIG_PM_SLEEP */
200
201static SIMPLE_DEV_PM_OPS(adc_jack_pm_ops,
202 adc_jack_suspend, adc_jack_resume);
203
19939860 204static struct platform_driver adc_jack_driver = {
205 .probe = adc_jack_probe,
5f7e2228 206 .remove = adc_jack_remove,
19939860 207 .driver = {
208 .name = "adc-jack",
1b6cf310 209 .pm = &adc_jack_pm_ops,
19939860 210 },
211};
212
213module_platform_driver(adc_jack_driver);
d9310e35
AL
214
215MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
216MODULE_DESCRIPTION("ADC Jack extcon driver");
217MODULE_LICENSE("GPL v2");
This page took 0.253901 seconds and 5 git commands to generate.