blob: 48dec94b487b500d939e1555946f7e3dcd57f4d0 [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.
Chanwoo Choia75e1c72013-08-31 13:16:49 +090032 * @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.
anish kumar19939862012-08-17 09:57:22 -070039 */
40struct adc_jack_data {
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +090041 struct device *dev;
Chanwoo Choi1876fd92014-04-21 20:49:30 +090042 struct extcon_dev *edev;
anish kumar19939862012-08-17 09:57:22 -070043
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090044 const unsigned int **cable_names;
anish kumar19939862012-08-17 09:57:22 -070045 struct adc_jack_cond *adc_conditions;
46 int num_conditions;
47
48 int irq;
49 unsigned long handling_delay; /* in jiffies */
50 struct delayed_work handler;
51
52 struct iio_channel *chan;
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +090053 bool wakeup_source;
anish kumar19939862012-08-17 09:57:22 -070054};
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 Choi1876fd92014-04-21 20:49:30 +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
Chanwoo Choi1876fd92014-04-21 20:49:30 +090083 extcon_set_state(data->edev, state);
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
anish kumar19939862012-08-17 09:57:22 -0700117 if (!pdata->adc_conditions ||
118 !pdata->adc_conditions[0].state) {
anish kumar19939862012-08-17 09:57:22 -0700119 dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900120 return -EINVAL;
anish kumar19939862012-08-17 09:57:22 -0700121 }
122 data->adc_conditions = pdata->adc_conditions;
123
124 /* Check the length of array and set num_conditions */
125 for (i = 0; data->adc_conditions[i].state; i++)
126 ;
127 data->num_conditions = i;
128
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000129 data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel);
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900130 if (IS_ERR(data->chan))
131 return PTR_ERR(data->chan);
anish kumar19939862012-08-17 09:57:22 -0700132
133 data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900134 data->wakeup_source = pdata->wakeup_source;
anish kumar19939862012-08-17 09:57:22 -0700135
Linus Torvalds033d9952012-10-02 09:54:49 -0700136 INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
anish kumar19939862012-08-17 09:57:22 -0700137
138 platform_set_drvdata(pdev, data);
139
Chanwoo Choi1876fd92014-04-21 20:49:30 +0900140 err = devm_extcon_dev_register(&pdev->dev, data->edev);
anish kumar19939862012-08-17 09:57:22 -0700141 if (err)
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900142 return err;
anish kumar19939862012-08-17 09:57:22 -0700143
144 data->irq = platform_get_irq(pdev, 0);
145 if (!data->irq) {
146 dev_err(&pdev->dev, "platform_get_irq failed\n");
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900147 return -ENODEV;
anish kumar19939862012-08-17 09:57:22 -0700148 }
149
150 err = request_any_context_irq(data->irq, adc_jack_irq_thread,
151 pdata->irq_flags, pdata->name, data);
152
Axel Lin03019752012-10-02 09:14:59 +0900153 if (err < 0) {
anish kumar19939862012-08-17 09:57:22 -0700154 dev_err(&pdev->dev, "error: irq %d\n", data->irq);
Sangjung Woo4b5dd732014-04-21 19:10:09 +0900155 return err;
anish kumar19939862012-08-17 09:57:22 -0700156 }
157
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900158 if (data->wakeup_source)
159 device_init_wakeup(&pdev->dev, 1);
160
Venkat Reddy Tallaba4b2712016-07-05 19:26:21 +0530161 adc_jack_handler(&data->handler.work);
Axel Lin03019752012-10-02 09:14:59 +0900162 return 0;
anish kumar19939862012-08-17 09:57:22 -0700163}
164
Bill Pemberton93ed0322012-11-19 13:25:49 -0500165static int adc_jack_remove(struct platform_device *pdev)
anish kumar19939862012-08-17 09:57:22 -0700166{
167 struct adc_jack_data *data = platform_get_drvdata(pdev);
168
169 free_irq(data->irq, data);
170 cancel_work_sync(&data->handler.work);
Ivan T. Ivanov5a696d92014-12-17 17:59:27 +0200171 iio_channel_release(data->chan);
anish kumar19939862012-08-17 09:57:22 -0700172
173 return 0;
174}
175
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900176#ifdef CONFIG_PM_SLEEP
177static int adc_jack_suspend(struct device *dev)
178{
179 struct adc_jack_data *data = dev_get_drvdata(dev);
180
181 cancel_delayed_work_sync(&data->handler);
182 if (device_may_wakeup(data->dev))
183 enable_irq_wake(data->irq);
184
185 return 0;
186}
187
188static int adc_jack_resume(struct device *dev)
189{
190 struct adc_jack_data *data = dev_get_drvdata(dev);
191
192 if (device_may_wakeup(data->dev))
193 disable_irq_wake(data->irq);
194
195 return 0;
196}
197#endif /* CONFIG_PM_SLEEP */
198
199static SIMPLE_DEV_PM_OPS(adc_jack_pm_ops,
200 adc_jack_suspend, adc_jack_resume);
201
anish kumar19939862012-08-17 09:57:22 -0700202static struct platform_driver adc_jack_driver = {
203 .probe = adc_jack_probe,
Bill Pemberton5f7e2222012-11-19 13:20:06 -0500204 .remove = adc_jack_remove,
anish kumar19939862012-08-17 09:57:22 -0700205 .driver = {
206 .name = "adc-jack",
Venkat Reddy Talla1b6cf312016-06-30 17:54:00 +0900207 .pm = &adc_jack_pm_ops,
anish kumar19939862012-08-17 09:57:22 -0700208 },
209};
210
211module_platform_driver(adc_jack_driver);
Axel Lind9310e32012-10-02 09:16:53 +0900212
213MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
214MODULE_DESCRIPTION("ADC Jack extcon driver");
215MODULE_LICENSE("GPL v2");