Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * drivers/extcon/extcon-adc-jack.c
  4 *
  5 * Analog Jack extcon driver with ADC-based detection capability.
  6 *
  7 * Copyright (C) 2016 Samsung Electronics
  8 * Chanwoo Choi <cw00.choi@samsung.com>
  9 *
 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>
 
 
 
 
 
 14 */
 15
 16#include <linux/module.h>
 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>
 25#include <linux/extcon-provider.h>
 26
 27/**
 28 * struct adc_jack_data - internal data for adc_jack device driver
 29 * @edev:		extcon device.
 30 * @cable_names:	list of supported cables.
 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.
 38 */
 39struct adc_jack_data {
 40	struct device *dev;
 41	struct extcon_dev *edev;
 42
 43	const unsigned int **cable_names;
 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;
 52	bool wakeup_source;
 53};
 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);
 60	struct adc_jack_cond *def;
 61	int ret, adc_val;
 62	int i;
 63
 64	ret = iio_read_channel_raw(data->chan, &adc_val);
 65	if (ret < 0) {
 66		dev_err(data->dev, "read channel() error: %d\n", ret);
 67		return;
 68	}
 69
 70	/* Get state from adc value with adc_conditions */
 71	for (i = 0; i < data->num_conditions; i++) {
 72		def = &data->adc_conditions[i];
 
 
 73		if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
 74			extcon_set_state_sync(data->edev, def->id, true);
 75			return;
 76		}
 77	}
 
 78
 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];
 82		extcon_set_state_sync(data->edev, def->id, false);
 83	}
 84}
 85
 86static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
 87{
 88	struct adc_jack_data *data = _data;
 89
 90	queue_delayed_work(system_power_efficient_wq,
 91			   &data->handler, data->handling_delay);
 92	return IRQ_HANDLED;
 93}
 94
 95static int adc_jack_probe(struct platform_device *pdev)
 96{
 97	struct adc_jack_data *data;
 98	struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
 99	int i, err = 0;
100
101	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
102	if (!data)
103		return -ENOMEM;
104
105	if (!pdata->cable_names) {
106		dev_err(&pdev->dev, "error: cable_names not defined.\n");
107		return -EINVAL;
108	}
109
110	data->dev = &pdev->dev;
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	}
116
117	if (!pdata->adc_conditions) {
 
118		dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
119		return -EINVAL;
120	}
121	data->adc_conditions = pdata->adc_conditions;
122
123	/* Check the length of array and set num_conditions */
124	for (i = 0; data->adc_conditions[i].id != EXTCON_NONE; i++);
 
125	data->num_conditions = i;
126
127	data->chan = devm_iio_channel_get(&pdev->dev, pdata->consumer_channel);
128	if (IS_ERR(data->chan))
129		return PTR_ERR(data->chan);
130
131	data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
132	data->wakeup_source = pdata->wakeup_source;
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 < 0)
 
144		return -ENODEV;
 
145
146	err = request_any_context_irq(data->irq, adc_jack_irq_thread,
147			pdata->irq_flags, pdata->name, data);
148
149	if (err < 0) {
150		dev_err(&pdev->dev, "error: irq %d\n", data->irq);
151		return err;
152	}
153
154	if (data->wakeup_source)
155		device_init_wakeup(&pdev->dev, 1);
156
157	adc_jack_handler(&data->handler.work);
158	return 0;
159}
160
161static int adc_jack_remove(struct platform_device *pdev)
162{
163	struct adc_jack_data *data = platform_get_drvdata(pdev);
164
165	free_irq(data->irq, data);
166	cancel_work_sync(&data->handler.work);
 
167
168	return 0;
169}
170
171#ifdef CONFIG_PM_SLEEP
172static int adc_jack_suspend(struct device *dev)
173{
174	struct adc_jack_data *data = dev_get_drvdata(dev);
175
176	cancel_delayed_work_sync(&data->handler);
177	if (device_may_wakeup(data->dev))
178		enable_irq_wake(data->irq);
179
180	return 0;
181}
182
183static int adc_jack_resume(struct device *dev)
184{
185	struct adc_jack_data *data = dev_get_drvdata(dev);
186
187	if (device_may_wakeup(data->dev))
188		disable_irq_wake(data->irq);
189
190	return 0;
191}
192#endif /* CONFIG_PM_SLEEP */
193
194static SIMPLE_DEV_PM_OPS(adc_jack_pm_ops,
195		adc_jack_suspend, adc_jack_resume);
196
197static struct platform_driver adc_jack_driver = {
198	.probe          = adc_jack_probe,
199	.remove         = adc_jack_remove,
200	.driver         = {
201		.name   = "adc-jack",
202		.pm = &adc_jack_pm_ops,
203	},
204};
205
206module_platform_driver(adc_jack_driver);
207
208MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
209MODULE_DESCRIPTION("ADC Jack extcon driver");
210MODULE_LICENSE("GPL v2");
v4.6
 
  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
 17#include <linux/module.h>
 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
 30 * @edev:		extcon device.
 31 * @cable_names:	list of supported cables.
 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.
 39 */
 40struct adc_jack_data {
 
 41	struct extcon_dev *edev;
 42
 43	const unsigned int **cable_names;
 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;
 
 52};
 53
 54static void adc_jack_handler(struct work_struct *work)
 55{
 56	struct adc_jack_data *data = container_of(to_delayed_work(work),
 57			struct adc_jack_data,
 58			handler);
 59	u32 state = 0;
 60	int ret, adc_val;
 61	int i;
 62
 63	ret = iio_read_channel_raw(data->chan, &adc_val);
 64	if (ret < 0) {
 65		dev_err(&data->edev->dev, "read channel() error: %d\n", ret);
 66		return;
 67	}
 68
 69	/* Get state from adc value with adc_conditions */
 70	for (i = 0; i < data->num_conditions; i++) {
 71		struct adc_jack_cond *def = &data->adc_conditions[i];
 72		if (!def->state)
 73			break;
 74		if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
 75			state = def->state;
 76			break;
 77		}
 78	}
 79	/* if no def has met, it means state = 0 (no cables attached) */
 80
 81	extcon_set_state(data->edev, state);
 
 
 
 
 82}
 83
 84static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
 85{
 86	struct adc_jack_data *data = _data;
 87
 88	queue_delayed_work(system_power_efficient_wq,
 89			   &data->handler, data->handling_delay);
 90	return IRQ_HANDLED;
 91}
 92
 93static int adc_jack_probe(struct platform_device *pdev)
 94{
 95	struct adc_jack_data *data;
 96	struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
 97	int i, err = 0;
 98
 99	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
100	if (!data)
101		return -ENOMEM;
102
103	if (!pdata->cable_names) {
104		dev_err(&pdev->dev, "error: cable_names not defined.\n");
105		return -EINVAL;
106	}
107
 
108	data->edev = devm_extcon_dev_allocate(&pdev->dev, pdata->cable_names);
109	if (IS_ERR(data->edev)) {
110		dev_err(&pdev->dev, "failed to allocate extcon device\n");
111		return -ENOMEM;
112	}
113
114	if (!pdata->adc_conditions ||
115			!pdata->adc_conditions[0].state) {
116		dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
117		return -EINVAL;
118	}
119	data->adc_conditions = pdata->adc_conditions;
120
121	/* Check the length of array and set num_conditions */
122	for (i = 0; data->adc_conditions[i].state; i++)
123		;
124	data->num_conditions = i;
125
126	data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel);
127	if (IS_ERR(data->chan))
128		return PTR_ERR(data->chan);
129
130	data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
 
131
132	INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
133
134	platform_set_drvdata(pdev, data);
135
136	err = devm_extcon_dev_register(&pdev->dev, data->edev);
137	if (err)
138		return err;
139
140	data->irq = platform_get_irq(pdev, 0);
141	if (!data->irq) {
142		dev_err(&pdev->dev, "platform_get_irq failed\n");
143		return -ENODEV;
144	}
145
146	err = request_any_context_irq(data->irq, adc_jack_irq_thread,
147			pdata->irq_flags, pdata->name, data);
148
149	if (err < 0) {
150		dev_err(&pdev->dev, "error: irq %d\n", data->irq);
151		return err;
152	}
153
 
 
 
 
154	return 0;
155}
156
157static int adc_jack_remove(struct platform_device *pdev)
158{
159	struct adc_jack_data *data = platform_get_drvdata(pdev);
160
161	free_irq(data->irq, data);
162	cancel_work_sync(&data->handler.work);
163	iio_channel_release(data->chan);
164
165	return 0;
166}
167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168static struct platform_driver adc_jack_driver = {
169	.probe          = adc_jack_probe,
170	.remove         = adc_jack_remove,
171	.driver         = {
172		.name   = "adc-jack",
 
173	},
174};
175
176module_platform_driver(adc_jack_driver);
177
178MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
179MODULE_DESCRIPTION("ADC Jack extcon driver");
180MODULE_LICENSE("GPL v2");