UPSTREAM: PCI: rockchip: remove the pointer to L1 substate cap
[firefly-linux-kernel-4.4.55.git] / drivers / extcon / extcon-adc-jack.c
1 /*
2  * drivers/extcon/extcon-adc-jack.c
3  *
4  * Analog Jack extcon driver with ADC-based detection capability.
5  *
6  * Copyright (C) 2016 Samsung Electronics
7  * Chanwoo Choi <cw00.choi@samsung.com>
8  *
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
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>
30
31 /**
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.
42  */
43 struct adc_jack_data {
44         struct extcon_dev *edev;
45
46         const unsigned int **cable_names;
47         struct adc_jack_cond *adc_conditions;
48         int num_conditions;
49
50         int irq;
51         unsigned long handling_delay; /* in jiffies */
52         struct delayed_work handler;
53
54         struct iio_channel *chan;
55 };
56
57 static void adc_jack_handler(struct work_struct *work)
58 {
59         struct adc_jack_data *data = container_of(to_delayed_work(work),
60                         struct adc_jack_data,
61                         handler);
62         struct adc_jack_cond *def;
63         int ret, adc_val;
64         int i;
65
66         ret = iio_read_channel_raw(data->chan, &adc_val);
67         if (ret < 0) {
68                 dev_err(&data->edev->dev, "read channel() error: %d\n", ret);
69                 return;
70         }
71
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);
77                         return;
78                 }
79         }
80
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);
85         }
86 }
87
88 static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
89 {
90         struct adc_jack_data *data = _data;
91
92         queue_delayed_work(system_power_efficient_wq,
93                            &data->handler, data->handling_delay);
94         return IRQ_HANDLED;
95 }
96
97 static int adc_jack_probe(struct platform_device *pdev)
98 {
99         struct adc_jack_data *data;
100         struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
101         int i, err = 0;
102
103         data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
104         if (!data)
105                 return -ENOMEM;
106
107         if (!pdata->cable_names) {
108                 dev_err(&pdev->dev, "error: cable_names not defined.\n");
109                 return -EINVAL;
110         }
111
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");
115                 return -ENOMEM;
116         }
117
118         if (!pdata->adc_conditions) {
119                 dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
120                 return -EINVAL;
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].id != EXTCON_NONE; i++);
126         data->num_conditions = i;
127
128         data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel);
129         if (IS_ERR(data->chan))
130                 return PTR_ERR(data->chan);
131
132         data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
133
134         INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
135
136         platform_set_drvdata(pdev, data);
137
138         err = devm_extcon_dev_register(&pdev->dev, data->edev);
139         if (err)
140                 return err;
141
142         data->irq = platform_get_irq(pdev, 0);
143         if (!data->irq) {
144                 dev_err(&pdev->dev, "platform_get_irq failed\n");
145                 return -ENODEV;
146         }
147
148         err = request_any_context_irq(data->irq, adc_jack_irq_thread,
149                         pdata->irq_flags, pdata->name, data);
150
151         if (err < 0) {
152                 dev_err(&pdev->dev, "error: irq %d\n", data->irq);
153                 return err;
154         }
155
156         return 0;
157 }
158
159 static int adc_jack_remove(struct platform_device *pdev)
160 {
161         struct adc_jack_data *data = platform_get_drvdata(pdev);
162
163         free_irq(data->irq, data);
164         cancel_work_sync(&data->handler.work);
165         iio_channel_release(data->chan);
166
167         return 0;
168 }
169
170 static struct platform_driver adc_jack_driver = {
171         .probe          = adc_jack_probe,
172         .remove         = adc_jack_remove,
173         .driver         = {
174                 .name   = "adc-jack",
175         },
176 };
177
178 module_platform_driver(adc_jack_driver);
179
180 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
181 MODULE_DESCRIPTION("ADC Jack extcon driver");
182 MODULE_LICENSE("GPL v2");