blob: ee9b5f70bfa40daa4f2dba92a6bbdfbae9469adc [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
anish kumar19939862012-08-17 09:57:22 -07002/*
3 * drivers/extcon/extcon-adc-jack.c
4 *
5 * Analog Jack extcon driver with ADC-based detection capability.
6 *
Chanwoo Choia7da72e2016-07-18 16:16:29 +09007 * Copyright (C) 2016 Samsung Electronics
8 * Chanwoo Choi <cw00.choi@samsung.com>
9 *
anish kumar19939862012-08-17 09:57:22 -070010 * 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>
anish kumar19939862012-08-17 09:57:22 -070014 */
15
Axel Lind9310e32012-10-02 09:16:53 +090016#include <linux/module.h>
anish kumar19939862012-08-17 09:57:22 -070017#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>
Chanwoo Choi176aa362017-09-21 12:11:24 +090025#include <linux/extcon-provider.h>
anish kumar19939862012-08-17 09:57:22 -070026
27/**
28 * struct adc_jack_data - internal data for adc_jack device driver
Chanwoo Choia75e1c72013-08-31 13:16:49 +090029 * @edev: extcon device.
30 * @cable_names: list of supported cables.
Chanwoo Choia75e1c72013-08-31 13:16:49 +090031 * @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.
anish kumar19939862012-08-17 09:57:22 -070038 */
39struct adc_jack_data {
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +090040 struct device *dev;
Chanwoo Choi1876fd92014-04-21 20:49:30 +090041 struct extcon_dev *edev;
anish kumar19939862012-08-17 09:57:22 -070042
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090043 const unsigned int **cable_names;
anish kumar19939862012-08-17 09:57:22 -070044 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;
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +090052 bool wakeup_source;
anish kumar19939862012-08-17 09:57:22 -070053};
54
55static 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);
Chanwoo Choia7da72e2016-07-18 16:16:29 +090060 struct adc_jack_cond *def;
anish kumar19939862012-08-17 09:57:22 -070061 int ret, adc_val;
62 int i;
63
64 ret = iio_read_channel_raw(data->chan, &adc_val);
65 if (ret < 0) {
Chanwoo Choi6e3a7e82016-12-26 20:47:13 +090066 dev_err(data->dev, "read channel() error: %d\n", ret);
anish kumar19939862012-08-17 09:57:22 -070067 return;
68 }
69
70 /* Get state from adc value with adc_conditions */
71 for (i = 0; i < data->num_conditions; i++) {
Chanwoo Choia7da72e2016-07-18 16:16:29 +090072 def = &data->adc_conditions[i];
anish kumar19939862012-08-17 09:57:22 -070073 if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
Chanwoo Choi8670b452016-08-16 15:55:34 +090074 extcon_set_state_sync(data->edev, def->id, true);
Chanwoo Choia7da72e2016-07-18 16:16:29 +090075 return;
anish kumar19939862012-08-17 09:57:22 -070076 }
77 }
anish kumar19939862012-08-17 09:57:22 -070078
Chanwoo Choia7da72e2016-07-18 16:16:29 +090079 /* 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];
Chanwoo Choi8670b452016-08-16 15:55:34 +090082 extcon_set_state_sync(data->edev, def->id, false);
Chanwoo Choia7da72e2016-07-18 16:16:29 +090083 }
anish kumar19939862012-08-17 09:57:22 -070084}
85
86static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
87{
88 struct adc_jack_data *data = _data;
89
Mark Brown1a82e812013-07-19 18:47:35 +010090 queue_delayed_work(system_power_efficient_wq,
91 &data->handler, data->handling_delay);
anish kumar19939862012-08-17 09:57:22 -070092 return IRQ_HANDLED;
93}
94
Bill Pemberton44f34fd2012-11-19 13:23:21 -050095static int adc_jack_probe(struct platform_device *pdev)
anish kumar19939862012-08-17 09:57:22 -070096{
97 struct adc_jack_data *data;
Jingoo Han7c0f65582013-09-11 13:22:18 +090098 struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
anish kumar19939862012-08-17 09:57:22 -070099 int i, err = 0;
100
101 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
102 if (!data)
103 return -ENOMEM;
104
anish kumar19939862012-08-17 09:57:22 -0700105 if (!pdata->cable_names) {
anish kumar19939862012-08-17 09:57:22 -0700106 dev_err(&pdev->dev, "error: cable_names not defined.\n");
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900107 return -EINVAL;
anish kumar19939862012-08-17 09:57:22 -0700108 }
109
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900110 data->dev = &pdev->dev;
Chanwoo Choi1876fd92014-04-21 20:49:30 +0900111 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 }
anish kumar19939862012-08-17 09:57:22 -0700116
Chanwoo Choia7da72e2016-07-18 16:16:29 +0900117 if (!pdata->adc_conditions) {
anish kumar19939862012-08-17 09:57:22 -0700118 dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900119 return -EINVAL;
anish kumar19939862012-08-17 09:57:22 -0700120 }
121 data->adc_conditions = pdata->adc_conditions;
122
123 /* Check the length of array and set num_conditions */
Chanwoo Choia7da72e2016-07-18 16:16:29 +0900124 for (i = 0; data->adc_conditions[i].id != EXTCON_NONE; i++);
anish kumar19939862012-08-17 09:57:22 -0700125 data->num_conditions = i;
126
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000127 data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel);
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900128 if (IS_ERR(data->chan))
129 return PTR_ERR(data->chan);
anish kumar19939862012-08-17 09:57:22 -0700130
131 data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900132 data->wakeup_source = pdata->wakeup_source;
anish kumar19939862012-08-17 09:57:22 -0700133
Linus Torvalds033d9952012-10-02 09:54:49 -0700134 INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
anish kumar19939862012-08-17 09:57:22 -0700135
136 platform_set_drvdata(pdev, data);
137
Chanwoo Choi1876fd92014-04-21 20:49:30 +0900138 err = devm_extcon_dev_register(&pdev->dev, data->edev);
anish kumar19939862012-08-17 09:57:22 -0700139 if (err)
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900140 return err;
anish kumar19939862012-08-17 09:57:22 -0700141
142 data->irq = platform_get_irq(pdev, 0);
Arvind Yadav447641e2017-11-23 21:25:29 +0530143 if (data->irq < 0) {
anish kumar19939862012-08-17 09:57:22 -0700144 dev_err(&pdev->dev, "platform_get_irq failed\n");
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900145 return -ENODEV;
anish kumar19939862012-08-17 09:57:22 -0700146 }
147
148 err = request_any_context_irq(data->irq, adc_jack_irq_thread,
149 pdata->irq_flags, pdata->name, data);
150
Axel Lin03019752012-10-02 09:14:59 +0900151 if (err < 0) {
anish kumar19939862012-08-17 09:57:22 -0700152 dev_err(&pdev->dev, "error: irq %d\n", data->irq);
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900153 return err;
anish kumar19939862012-08-17 09:57:22 -0700154 }
155
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900156 if (data->wakeup_source)
157 device_init_wakeup(&pdev->dev, 1);
158
Venkat Reddy Tallaba4b2712016-07-05 19:26:21 +0530159 adc_jack_handler(&data->handler.work);
Axel Lin03019752012-10-02 09:14:59 +0900160 return 0;
anish kumar19939862012-08-17 09:57:22 -0700161}
162
Bill Pemberton93ed0322012-11-19 13:25:49 -0500163static int adc_jack_remove(struct platform_device *pdev)
anish kumar19939862012-08-17 09:57:22 -0700164{
165 struct adc_jack_data *data = platform_get_drvdata(pdev);
166
167 free_irq(data->irq, data);
168 cancel_work_sync(&data->handler.work);
Ivan T. Ivanov5a696d92014-12-17 17:59:27 +0200169 iio_channel_release(data->chan);
anish kumar19939862012-08-17 09:57:22 -0700170
171 return 0;
172}
173
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900174#ifdef CONFIG_PM_SLEEP
175static 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
186static 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
197static SIMPLE_DEV_PM_OPS(adc_jack_pm_ops,
198 adc_jack_suspend, adc_jack_resume);
199
anish kumar19939862012-08-17 09:57:22 -0700200static struct platform_driver adc_jack_driver = {
201 .probe = adc_jack_probe,
Bill Pemberton5f7e2222012-11-19 13:20:06 -0500202 .remove = adc_jack_remove,
anish kumar19939862012-08-17 09:57:22 -0700203 .driver = {
204 .name = "adc-jack",
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900205 .pm = &adc_jack_pm_ops,
anish kumar19939862012-08-17 09:57:22 -0700206 },
207};
208
209module_platform_driver(adc_jack_driver);
Axel Lind9310e32012-10-02 09:16:53 +0900210
211MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
212MODULE_DESCRIPTION("ADC Jack extcon driver");
213MODULE_LICENSE("GPL v2");