]>
Commit | Line | Data |
---|---|---|
19939860 | 1 | /* |
2 | * drivers/extcon/extcon-adc-jack.c | |
3 | * | |
4 | * Analog Jack extcon driver with ADC-based detection capability. | |
5 | * | |
6 | * Copyright (C) 2012 Samsung Electronics | |
7 | * MyungJoo Ham <myungjoo.ham@samsung.com> | |
8 | * | |
9 | * Modified for calling to IIO to get adc by <anish.singh@samsung.com> | |
10 | * | |
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. | |
14 | * | |
15 | */ | |
16 | ||
d9310e35 | 17 | #include <linux/module.h> |
19939860 | 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> | |
27 | ||
28 | /** | |
29 | * struct adc_jack_data - internal data for adc_jack device driver | |
a75e1c73 CC |
30 | * @edev: extcon device. |
31 | * @cable_names: list of supported cables. | |
a75e1c73 CC |
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. | |
19939860 | 39 | */ |
40 | struct adc_jack_data { | |
1b6cf310 | 41 | struct device *dev; |
1876fd9a | 42 | struct extcon_dev *edev; |
19939860 | 43 | |
73b6ecdb | 44 | const unsigned int **cable_names; |
19939860 | 45 | struct adc_jack_cond *adc_conditions; |
46 | int num_conditions; | |
47 | ||
48 | int irq; | |
49 | unsigned long handling_delay; /* in jiffies */ | |
50 | struct delayed_work handler; | |
51 | ||
52 | struct iio_channel *chan; | |
1b6cf310 | 53 | bool wakeup_source; |
19939860 | 54 | }; |
55 | ||
56 | static void adc_jack_handler(struct work_struct *work) | |
57 | { | |
58 | struct adc_jack_data *data = container_of(to_delayed_work(work), | |
59 | struct adc_jack_data, | |
60 | handler); | |
61 | u32 state = 0; | |
62 | int ret, adc_val; | |
63 | int i; | |
64 | ||
65 | ret = iio_read_channel_raw(data->chan, &adc_val); | |
66 | if (ret < 0) { | |
1876fd9a | 67 | dev_err(&data->edev->dev, "read channel() error: %d\n", ret); |
19939860 | 68 | return; |
69 | } | |
70 | ||
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]; | |
74 | if (!def->state) | |
75 | break; | |
76 | if (def->min_adc <= adc_val && def->max_adc >= adc_val) { | |
77 | state = def->state; | |
78 | break; | |
79 | } | |
80 | } | |
81 | /* if no def has met, it means state = 0 (no cables attached) */ | |
82 | ||
1876fd9a | 83 | extcon_set_state(data->edev, state); |
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 | |
19939860 | 117 | if (!pdata->adc_conditions || |
118 | !pdata->adc_conditions[0].state) { | |
19939860 | 119 | dev_err(&pdev->dev, "error: adc_conditions not defined.\n"); |
4b5dd738 | 120 | return -EINVAL; |
19939860 | 121 | } |
122 | data->adc_conditions = pdata->adc_conditions; | |
123 | ||
124 | /* Check the length of array and set num_conditions */ | |
125 | for (i = 0; data->adc_conditions[i].state; i++) | |
126 | ; | |
127 | data->num_conditions = i; | |
128 | ||
5aa57f0a | 129 | data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel); |
4b5dd738 SW |
130 | if (IS_ERR(data->chan)) |
131 | return PTR_ERR(data->chan); | |
19939860 | 132 | |
133 | data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms); | |
1b6cf310 | 134 | data->wakeup_source = pdata->wakeup_source; |
19939860 | 135 | |
033d9959 | 136 | INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler); |
19939860 | 137 | |
138 | platform_set_drvdata(pdev, data); | |
139 | ||
1876fd9a | 140 | err = devm_extcon_dev_register(&pdev->dev, data->edev); |
19939860 | 141 | if (err) |
4b5dd738 | 142 | return err; |
19939860 | 143 | |
144 | data->irq = platform_get_irq(pdev, 0); | |
145 | if (!data->irq) { | |
146 | dev_err(&pdev->dev, "platform_get_irq failed\n"); | |
4b5dd738 | 147 | return -ENODEV; |
19939860 | 148 | } |
149 | ||
150 | err = request_any_context_irq(data->irq, adc_jack_irq_thread, | |
151 | pdata->irq_flags, pdata->name, data); | |
152 | ||
03019759 | 153 | if (err < 0) { |
19939860 | 154 | dev_err(&pdev->dev, "error: irq %d\n", data->irq); |
4b5dd738 | 155 | return err; |
19939860 | 156 | } |
157 | ||
1b6cf310 VRT |
158 | if (data->wakeup_source) |
159 | device_init_wakeup(&pdev->dev, 1); | |
160 | ||
ba4b2715 | 161 | adc_jack_handler(&data->handler.work); |
03019759 | 162 | return 0; |
19939860 | 163 | } |
164 | ||
93ed0327 | 165 | static int adc_jack_remove(struct platform_device *pdev) |
19939860 | 166 | { |
167 | struct adc_jack_data *data = platform_get_drvdata(pdev); | |
168 | ||
169 | free_irq(data->irq, data); | |
170 | cancel_work_sync(&data->handler.work); | |
5a696d97 | 171 | iio_channel_release(data->chan); |
19939860 | 172 | |
173 | return 0; | |
174 | } | |
175 | ||
1b6cf310 VRT |
176 | #ifdef CONFIG_PM_SLEEP |
177 | static int adc_jack_suspend(struct device *dev) | |
178 | { | |
179 | struct adc_jack_data *data = dev_get_drvdata(dev); | |
180 | ||
181 | cancel_delayed_work_sync(&data->handler); | |
182 | if (device_may_wakeup(data->dev)) | |
183 | enable_irq_wake(data->irq); | |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
188 | static int adc_jack_resume(struct device *dev) | |
189 | { | |
190 | struct adc_jack_data *data = dev_get_drvdata(dev); | |
191 | ||
192 | if (device_may_wakeup(data->dev)) | |
193 | disable_irq_wake(data->irq); | |
194 | ||
195 | return 0; | |
196 | } | |
197 | #endif /* CONFIG_PM_SLEEP */ | |
198 | ||
199 | static SIMPLE_DEV_PM_OPS(adc_jack_pm_ops, | |
200 | adc_jack_suspend, adc_jack_resume); | |
201 | ||
19939860 | 202 | static struct platform_driver adc_jack_driver = { |
203 | .probe = adc_jack_probe, | |
5f7e2228 | 204 | .remove = adc_jack_remove, |
19939860 | 205 | .driver = { |
206 | .name = "adc-jack", | |
1b6cf310 | 207 | .pm = &adc_jack_pm_ops, |
19939860 | 208 | }, |
209 | }; | |
210 | ||
211 | module_platform_driver(adc_jack_driver); | |
d9310e35 AL |
212 | |
213 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); | |
214 | MODULE_DESCRIPTION("ADC Jack extcon driver"); | |
215 | MODULE_LICENSE("GPL v2"); |