blob: e23f1c2e505366dd24cb2ff6037f31074f7331ff [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 *
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
Axel Lind9310e32012-10-02 09:16:53 +090017#include <linux/module.h>
anish kumar19939862012-08-17 09:57:22 -070018#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
Chanwoo Choia75e1c72013-08-31 13:16:49 +090030 * @edev: extcon device.
31 * @cable_names: list of supported cables.
32 * @num_cables: size of cable_names.
33 * @adc_conditions: list of adc value conditions.
34 * @num_conditions: size of adc_conditions.
35 * @irq: irq number of attach/detach event (0 if not exist).
36 * @handling_delay: interrupt handler will schedule extcon event
37 * handling at handling_delay jiffies.
38 * @handler: extcon event handler called by interrupt handler.
39 * @chan: iio channel being queried.
anish kumar19939862012-08-17 09:57:22 -070040 */
41struct adc_jack_data {
42 struct extcon_dev edev;
43
44 const char **cable_names;
45 int num_cables;
46 struct adc_jack_cond *adc_conditions;
47 int num_conditions;
48
49 int irq;
50 unsigned long handling_delay; /* in jiffies */
51 struct delayed_work handler;
52
53 struct iio_channel *chan;
54};
55
56static void adc_jack_handler(struct work_struct *work)
57{
58 struct adc_jack_data *data = container_of(to_delayed_work(work),
59 struct adc_jack_data,
60 handler);
61 u32 state = 0;
62 int ret, adc_val;
63 int i;
64
65 ret = iio_read_channel_raw(data->chan, &adc_val);
66 if (ret < 0) {
Chanwoo Choidae61652013-09-27 09:19:40 +090067 dev_err(&data->edev.dev, "read channel() error: %d\n", ret);
anish kumar19939862012-08-17 09:57:22 -070068 return;
69 }
70
71 /* Get state from adc value with adc_conditions */
72 for (i = 0; i < data->num_conditions; i++) {
73 struct adc_jack_cond *def = &data->adc_conditions[i];
74 if (!def->state)
75 break;
76 if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
77 state = def->state;
78 break;
79 }
80 }
81 /* if no def has met, it means state = 0 (no cables attached) */
82
83 extcon_set_state(&data->edev, state);
84}
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
105 data->edev.name = pdata->name;
106
107 if (!pdata->cable_names) {
108 err = -EINVAL;
109 dev_err(&pdev->dev, "error: cable_names not defined.\n");
110 goto out;
111 }
112
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900113 data->edev.dev.parent = &pdev->dev;
anish kumar19939862012-08-17 09:57:22 -0700114 data->edev.supported_cable = pdata->cable_names;
115
116 /* Check the length of array and set num_cables */
117 for (i = 0; data->edev.supported_cable[i]; i++)
118 ;
119 if (i == 0 || i > SUPPORTED_CABLE_MAX) {
120 err = -EINVAL;
121 dev_err(&pdev->dev, "error: pdata->cable_names size = %d\n",
122 i - 1);
123 goto out;
124 }
125 data->num_cables = i;
126
127 if (!pdata->adc_conditions ||
128 !pdata->adc_conditions[0].state) {
129 err = -EINVAL;
130 dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
131 goto out;
132 }
133 data->adc_conditions = pdata->adc_conditions;
134
135 /* Check the length of array and set num_conditions */
136 for (i = 0; data->adc_conditions[i].state; i++)
137 ;
138 data->num_conditions = i;
139
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000140 data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel);
anish kumar19939862012-08-17 09:57:22 -0700141 if (IS_ERR(data->chan)) {
142 err = PTR_ERR(data->chan);
143 goto out;
144 }
145
146 data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
147
Linus Torvalds033d9952012-10-02 09:54:49 -0700148 INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
anish kumar19939862012-08-17 09:57:22 -0700149
150 platform_set_drvdata(pdev, data);
151
Chanwoo Choi42d7d752013-09-27 09:20:26 +0900152 err = extcon_dev_register(&data->edev);
anish kumar19939862012-08-17 09:57:22 -0700153 if (err)
154 goto out;
155
156 data->irq = platform_get_irq(pdev, 0);
157 if (!data->irq) {
158 dev_err(&pdev->dev, "platform_get_irq failed\n");
159 err = -ENODEV;
160 goto err_irq;
161 }
162
163 err = request_any_context_irq(data->irq, adc_jack_irq_thread,
164 pdata->irq_flags, pdata->name, data);
165
Axel Lin03019752012-10-02 09:14:59 +0900166 if (err < 0) {
anish kumar19939862012-08-17 09:57:22 -0700167 dev_err(&pdev->dev, "error: irq %d\n", data->irq);
anish kumar19939862012-08-17 09:57:22 -0700168 goto err_irq;
169 }
170
Axel Lin03019752012-10-02 09:14:59 +0900171 return 0;
anish kumar19939862012-08-17 09:57:22 -0700172
173err_irq:
174 extcon_dev_unregister(&data->edev);
175out:
176 return err;
177}
178
Bill Pemberton93ed0322012-11-19 13:25:49 -0500179static int adc_jack_remove(struct platform_device *pdev)
anish kumar19939862012-08-17 09:57:22 -0700180{
181 struct adc_jack_data *data = platform_get_drvdata(pdev);
182
183 free_irq(data->irq, data);
184 cancel_work_sync(&data->handler.work);
185 extcon_dev_unregister(&data->edev);
186
187 return 0;
188}
189
190static struct platform_driver adc_jack_driver = {
191 .probe = adc_jack_probe,
Bill Pemberton5f7e2222012-11-19 13:20:06 -0500192 .remove = adc_jack_remove,
anish kumar19939862012-08-17 09:57:22 -0700193 .driver = {
194 .name = "adc-jack",
195 .owner = THIS_MODULE,
196 },
197};
198
199module_platform_driver(adc_jack_driver);
Axel Lind9310e32012-10-02 09:16:53 +0900200
201MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
202MODULE_DESCRIPTION("ADC Jack extcon driver");
203MODULE_LICENSE("GPL v2");