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