2012-08-18 00:57:22 +08:00
|
|
|
/*
|
|
|
|
* drivers/extcon/extcon-adc-jack.c
|
|
|
|
*
|
|
|
|
* Analog Jack extcon driver with ADC-based detection capability.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Samsung Electronics
|
|
|
|
* MyungJoo Ham <myungjoo.ham@samsung.com>
|
|
|
|
*
|
|
|
|
* Modified for calling to IIO to get adc by <anish.singh@samsung.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-10-02 08:16:53 +08:00
|
|
|
#include <linux/module.h>
|
2012-08-18 00:57:22 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/workqueue.h>
|
|
|
|
#include <linux/iio/consumer.h>
|
|
|
|
#include <linux/extcon/extcon-adc-jack.h>
|
|
|
|
#include <linux/extcon.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct adc_jack_data - internal data for adc_jack device driver
|
2013-08-31 12:16:49 +08:00
|
|
|
* @edev: extcon device.
|
|
|
|
* @cable_names: list of supported cables.
|
|
|
|
* @num_cables: size of cable_names.
|
|
|
|
* @adc_conditions: list of adc value conditions.
|
|
|
|
* @num_conditions: size of adc_conditions.
|
|
|
|
* @irq: irq number of attach/detach event (0 if not exist).
|
|
|
|
* @handling_delay: interrupt handler will schedule extcon event
|
|
|
|
* handling at handling_delay jiffies.
|
|
|
|
* @handler: extcon event handler called by interrupt handler.
|
|
|
|
* @chan: iio channel being queried.
|
2012-08-18 00:57:22 +08:00
|
|
|
*/
|
|
|
|
struct adc_jack_data {
|
|
|
|
struct extcon_dev edev;
|
|
|
|
|
|
|
|
const char **cable_names;
|
|
|
|
int num_cables;
|
|
|
|
struct adc_jack_cond *adc_conditions;
|
|
|
|
int num_conditions;
|
|
|
|
|
|
|
|
int irq;
|
|
|
|
unsigned long handling_delay; /* in jiffies */
|
|
|
|
struct delayed_work handler;
|
|
|
|
|
|
|
|
struct iio_channel *chan;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void adc_jack_handler(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct adc_jack_data *data = container_of(to_delayed_work(work),
|
|
|
|
struct adc_jack_data,
|
|
|
|
handler);
|
|
|
|
u32 state = 0;
|
|
|
|
int ret, adc_val;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ret = iio_read_channel_raw(data->chan, &adc_val);
|
|
|
|
if (ret < 0) {
|
2013-09-27 08:19:40 +08:00
|
|
|
dev_err(&data->edev.dev, "read channel() error: %d\n", ret);
|
2012-08-18 00:57:22 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get state from adc value with adc_conditions */
|
|
|
|
for (i = 0; i < data->num_conditions; i++) {
|
|
|
|
struct adc_jack_cond *def = &data->adc_conditions[i];
|
|
|
|
if (!def->state)
|
|
|
|
break;
|
|
|
|
if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
|
|
|
|
state = def->state;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if no def has met, it means state = 0 (no cables attached) */
|
|
|
|
|
|
|
|
extcon_set_state(&data->edev, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
|
|
|
|
{
|
|
|
|
struct adc_jack_data *data = _data;
|
|
|
|
|
2013-07-20 01:47:35 +08:00
|
|
|
queue_delayed_work(system_power_efficient_wq,
|
|
|
|
&data->handler, data->handling_delay);
|
2012-08-18 00:57:22 +08:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2012-11-20 02:23:21 +08:00
|
|
|
static int adc_jack_probe(struct platform_device *pdev)
|
2012-08-18 00:57:22 +08:00
|
|
|
{
|
|
|
|
struct adc_jack_data *data;
|
2013-09-11 12:22:18 +08:00
|
|
|
struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
|
2012-08-18 00:57:22 +08:00
|
|
|
int i, err = 0;
|
|
|
|
|
|
|
|
data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
data->edev.name = pdata->name;
|
|
|
|
|
|
|
|
if (!pdata->cable_names) {
|
|
|
|
err = -EINVAL;
|
|
|
|
dev_err(&pdev->dev, "error: cable_names not defined.\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-09-27 08:20:26 +08:00
|
|
|
data->edev.dev.parent = &pdev->dev;
|
2012-08-18 00:57:22 +08:00
|
|
|
data->edev.supported_cable = pdata->cable_names;
|
|
|
|
|
|
|
|
/* Check the length of array and set num_cables */
|
|
|
|
for (i = 0; data->edev.supported_cable[i]; i++)
|
|
|
|
;
|
|
|
|
if (i == 0 || i > SUPPORTED_CABLE_MAX) {
|
|
|
|
err = -EINVAL;
|
|
|
|
dev_err(&pdev->dev, "error: pdata->cable_names size = %d\n",
|
|
|
|
i - 1);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
data->num_cables = i;
|
|
|
|
|
|
|
|
if (!pdata->adc_conditions ||
|
|
|
|
!pdata->adc_conditions[0].state) {
|
|
|
|
err = -EINVAL;
|
|
|
|
dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
data->adc_conditions = pdata->adc_conditions;
|
|
|
|
|
|
|
|
/* Check the length of array and set num_conditions */
|
|
|
|
for (i = 0; data->adc_conditions[i].state; i++)
|
|
|
|
;
|
|
|
|
data->num_conditions = i;
|
|
|
|
|
2013-02-05 04:26:00 +08:00
|
|
|
data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel);
|
2012-08-18 00:57:22 +08:00
|
|
|
if (IS_ERR(data->chan)) {
|
|
|
|
err = PTR_ERR(data->chan);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
|
|
|
|
|
2012-10-03 00:54:49 +08:00
|
|
|
INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
|
2012-08-18 00:57:22 +08:00
|
|
|
|
|
|
|
platform_set_drvdata(pdev, data);
|
|
|
|
|
2013-09-27 08:20:26 +08:00
|
|
|
err = extcon_dev_register(&data->edev);
|
2012-08-18 00:57:22 +08:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
data->irq = platform_get_irq(pdev, 0);
|
|
|
|
if (!data->irq) {
|
|
|
|
dev_err(&pdev->dev, "platform_get_irq failed\n");
|
|
|
|
err = -ENODEV;
|
|
|
|
goto err_irq;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = request_any_context_irq(data->irq, adc_jack_irq_thread,
|
|
|
|
pdata->irq_flags, pdata->name, data);
|
|
|
|
|
2012-10-02 08:14:59 +08:00
|
|
|
if (err < 0) {
|
2012-08-18 00:57:22 +08:00
|
|
|
dev_err(&pdev->dev, "error: irq %d\n", data->irq);
|
|
|
|
goto err_irq;
|
|
|
|
}
|
|
|
|
|
2012-10-02 08:14:59 +08:00
|
|
|
return 0;
|
2012-08-18 00:57:22 +08:00
|
|
|
|
|
|
|
err_irq:
|
|
|
|
extcon_dev_unregister(&data->edev);
|
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-11-20 02:25:49 +08:00
|
|
|
static int adc_jack_remove(struct platform_device *pdev)
|
2012-08-18 00:57:22 +08:00
|
|
|
{
|
|
|
|
struct adc_jack_data *data = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
free_irq(data->irq, data);
|
|
|
|
cancel_work_sync(&data->handler.work);
|
|
|
|
extcon_dev_unregister(&data->edev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver adc_jack_driver = {
|
|
|
|
.probe = adc_jack_probe,
|
2012-11-20 02:20:06 +08:00
|
|
|
.remove = adc_jack_remove,
|
2012-08-18 00:57:22 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "adc-jack",
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(adc_jack_driver);
|
2012-10-02 08:16:53 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
|
|
|
|
MODULE_DESCRIPTION("ADC Jack extcon driver");
|
|
|
|
MODULE_LICENSE("GPL v2");
|