blob: 6f6537ab0a7911e5dbfaaee5e2f2794326588838 [file] [log] [blame]
anish kumar19939862012-08-17 09:57:22 -07001/*
2 * drivers/extcon/extcon-adc-jack.c
3 *
4 * Analog Jack extcon driver with ADC-based detection capability.
5 *
Chanwoo Choia7da72e2016-07-18 16:16:29 +09006 * Copyright (C) 2016 Samsung Electronics
7 * Chanwoo Choi <cw00.choi@samsung.com>
8 *
anish kumar19939862012-08-17 09:57:22 -07009 * 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
Axel Lind9310e32012-10-02 09:16:53 +090020#include <linux/module.h>
anish kumar19939862012-08-17 09:57:22 -070021#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
Chanwoo Choia75e1c72013-08-31 13:16:49 +090033 * @edev: extcon device.
34 * @cable_names: list of supported cables.
Chanwoo Choia75e1c72013-08-31 13:16:49 +090035 * @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.
anish kumar19939862012-08-17 09:57:22 -070042 */
43struct adc_jack_data {
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +090044 struct device *dev;
Chanwoo Choi1876fd92014-04-21 20:49:30 +090045 struct extcon_dev *edev;
anish kumar19939862012-08-17 09:57:22 -070046
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090047 const unsigned int **cable_names;
anish kumar19939862012-08-17 09:57:22 -070048 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;
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +090056 bool wakeup_source;
anish kumar19939862012-08-17 09:57:22 -070057};
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);
Chanwoo Choia7da72e2016-07-18 16:16:29 +090064 struct adc_jack_cond *def;
anish kumar19939862012-08-17 09:57:22 -070065 int ret, adc_val;
66 int i;
67
68 ret = iio_read_channel_raw(data->chan, &adc_val);
69 if (ret < 0) {
Chanwoo Choi6e3a7e82016-12-26 20:47:13 +090070 dev_err(data->dev, "read channel() error: %d\n", ret);
anish kumar19939862012-08-17 09:57:22 -070071 return;
72 }
73
74 /* Get state from adc value with adc_conditions */
75 for (i = 0; i < data->num_conditions; i++) {
Chanwoo Choia7da72e2016-07-18 16:16:29 +090076 def = &data->adc_conditions[i];
anish kumar19939862012-08-17 09:57:22 -070077 if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
Chanwoo Choi8670b452016-08-16 15:55:34 +090078 extcon_set_state_sync(data->edev, def->id, true);
Chanwoo Choia7da72e2016-07-18 16:16:29 +090079 return;
anish kumar19939862012-08-17 09:57:22 -070080 }
81 }
anish kumar19939862012-08-17 09:57:22 -070082
Chanwoo Choia7da72e2016-07-18 16:16:29 +090083 /* 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];
Chanwoo Choi8670b452016-08-16 15:55:34 +090086 extcon_set_state_sync(data->edev, def->id, false);
Chanwoo Choia7da72e2016-07-18 16:16:29 +090087 }
anish kumar19939862012-08-17 09:57:22 -070088}
89
90static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
91{
92 struct adc_jack_data *data = _data;
93
Mark Brown1a82e812013-07-19 18:47:35 +010094 queue_delayed_work(system_power_efficient_wq,
95 &data->handler, data->handling_delay);
anish kumar19939862012-08-17 09:57:22 -070096 return IRQ_HANDLED;
97}
98
Bill Pemberton44f34fd2012-11-19 13:23:21 -050099static int adc_jack_probe(struct platform_device *pdev)
anish kumar19939862012-08-17 09:57:22 -0700100{
101 struct adc_jack_data *data;
Jingoo Han7c0f65582013-09-11 13:22:18 +0900102 struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
anish kumar19939862012-08-17 09:57:22 -0700103 int i, err = 0;
104
105 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
106 if (!data)
107 return -ENOMEM;
108
anish kumar19939862012-08-17 09:57:22 -0700109 if (!pdata->cable_names) {
anish kumar19939862012-08-17 09:57:22 -0700110 dev_err(&pdev->dev, "error: cable_names not defined.\n");
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900111 return -EINVAL;
anish kumar19939862012-08-17 09:57:22 -0700112 }
113
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900114 data->dev = &pdev->dev;
Chanwoo Choi1876fd92014-04-21 20:49:30 +0900115 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 }
anish kumar19939862012-08-17 09:57:22 -0700120
Chanwoo Choia7da72e2016-07-18 16:16:29 +0900121 if (!pdata->adc_conditions) {
anish kumar19939862012-08-17 09:57:22 -0700122 dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900123 return -EINVAL;
anish kumar19939862012-08-17 09:57:22 -0700124 }
125 data->adc_conditions = pdata->adc_conditions;
126
127 /* Check the length of array and set num_conditions */
Chanwoo Choia7da72e2016-07-18 16:16:29 +0900128 for (i = 0; data->adc_conditions[i].id != EXTCON_NONE; i++);
anish kumar19939862012-08-17 09:57:22 -0700129 data->num_conditions = i;
130
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000131 data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel);
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900132 if (IS_ERR(data->chan))
133 return PTR_ERR(data->chan);
anish kumar19939862012-08-17 09:57:22 -0700134
135 data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900136 data->wakeup_source = pdata->wakeup_source;
anish kumar19939862012-08-17 09:57:22 -0700137
Linus Torvalds033d9952012-10-02 09:54:49 -0700138 INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
anish kumar19939862012-08-17 09:57:22 -0700139
140 platform_set_drvdata(pdev, data);
141
Chanwoo Choi1876fd92014-04-21 20:49:30 +0900142 err = devm_extcon_dev_register(&pdev->dev, data->edev);
anish kumar19939862012-08-17 09:57:22 -0700143 if (err)
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900144 return err;
anish kumar19939862012-08-17 09:57:22 -0700145
146 data->irq = platform_get_irq(pdev, 0);
147 if (!data->irq) {
148 dev_err(&pdev->dev, "platform_get_irq failed\n");
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900149 return -ENODEV;
anish kumar19939862012-08-17 09:57:22 -0700150 }
151
152 err = request_any_context_irq(data->irq, adc_jack_irq_thread,
153 pdata->irq_flags, pdata->name, data);
154
Axel Lin03019752012-10-02 09:14:59 +0900155 if (err < 0) {
anish kumar19939862012-08-17 09:57:22 -0700156 dev_err(&pdev->dev, "error: irq %d\n", data->irq);
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900157 return err;
anish kumar19939862012-08-17 09:57:22 -0700158 }
159
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900160 if (data->wakeup_source)
161 device_init_wakeup(&pdev->dev, 1);
162
Venkat Reddy Tallaba4b2712016-07-05 19:26:21 +0530163 adc_jack_handler(&data->handler.work);
Axel Lin03019752012-10-02 09:14:59 +0900164 return 0;
anish kumar19939862012-08-17 09:57:22 -0700165}
166
Bill Pemberton93ed0322012-11-19 13:25:49 -0500167static int adc_jack_remove(struct platform_device *pdev)
anish kumar19939862012-08-17 09:57:22 -0700168{
169 struct adc_jack_data *data = platform_get_drvdata(pdev);
170
171 free_irq(data->irq, data);
172 cancel_work_sync(&data->handler.work);
Ivan T. Ivanov5a696d92014-12-17 17:59:27 +0200173 iio_channel_release(data->chan);
anish kumar19939862012-08-17 09:57:22 -0700174
175 return 0;
176}
177
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900178#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
anish kumar19939862012-08-17 09:57:22 -0700204static struct platform_driver adc_jack_driver = {
205 .probe = adc_jack_probe,
Bill Pemberton5f7e2222012-11-19 13:20:06 -0500206 .remove = adc_jack_remove,
anish kumar19939862012-08-17 09:57:22 -0700207 .driver = {
208 .name = "adc-jack",
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900209 .pm = &adc_jack_pm_ops,
anish kumar19939862012-08-17 09:57:22 -0700210 },
211};
212
213module_platform_driver(adc_jack_driver);
Axel Lind9310e32012-10-02 09:16:53 +0900214
215MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
216MODULE_DESCRIPTION("ADC Jack extcon driver");
217MODULE_LICENSE("GPL v2");