anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 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 | |
Axel Lin | d9310e3 | 2012-10-02 09:16:53 +0900 | [diff] [blame] | 17 | #include <linux/module.h> |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 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 |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 30 | * @edev: extcon device. |
| 31 | * @cable_names: list of supported cables. |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 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. |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 39 | */ |
| 40 | struct adc_jack_data { |
Chanwoo Choi | 1876fd9 | 2014-04-21 20:49:30 +0900 | [diff] [blame] | 41 | struct extcon_dev *edev; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 42 | |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 43 | const unsigned int **cable_names; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 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 | |
| 54 | static 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) { |
Chanwoo Choi | 1876fd9 | 2014-04-21 20:49:30 +0900 | [diff] [blame] | 65 | dev_err(&data->edev->dev, "read channel() error: %d\n", ret); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 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 | |
Chanwoo Choi | 1876fd9 | 2014-04-21 20:49:30 +0900 | [diff] [blame] | 81 | extcon_set_state(data->edev, state); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static irqreturn_t adc_jack_irq_thread(int irq, void *_data) |
| 85 | { |
| 86 | struct adc_jack_data *data = _data; |
| 87 | |
Mark Brown | 1a82e81 | 2013-07-19 18:47:35 +0100 | [diff] [blame] | 88 | queue_delayed_work(system_power_efficient_wq, |
| 89 | &data->handler, data->handling_delay); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 90 | return IRQ_HANDLED; |
| 91 | } |
| 92 | |
Bill Pemberton | 44f34fd | 2012-11-19 13:23:21 -0500 | [diff] [blame] | 93 | static int adc_jack_probe(struct platform_device *pdev) |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 94 | { |
| 95 | struct adc_jack_data *data; |
Jingoo Han | 7c0f6558 | 2013-09-11 13:22:18 +0900 | [diff] [blame] | 96 | struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 97 | int i, err = 0; |
| 98 | |
| 99 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 100 | if (!data) |
| 101 | return -ENOMEM; |
| 102 | |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 103 | if (!pdata->cable_names) { |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 104 | dev_err(&pdev->dev, "error: cable_names not defined.\n"); |
Sangjung Woo | 4b5dd73 | 2014-04-21 19:10:09 +0900 | [diff] [blame] | 105 | return -EINVAL; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Chanwoo Choi | 1876fd9 | 2014-04-21 20:49:30 +0900 | [diff] [blame] | 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 | } |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 113 | |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 114 | if (!pdata->adc_conditions || |
| 115 | !pdata->adc_conditions[0].state) { |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 116 | dev_err(&pdev->dev, "error: adc_conditions not defined.\n"); |
Sangjung Woo | 4b5dd73 | 2014-04-21 19:10:09 +0900 | [diff] [blame] | 117 | return -EINVAL; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 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 | |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 126 | data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel); |
Sangjung Woo | 4b5dd73 | 2014-04-21 19:10:09 +0900 | [diff] [blame] | 127 | if (IS_ERR(data->chan)) |
| 128 | return PTR_ERR(data->chan); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 129 | |
| 130 | data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms); |
| 131 | |
Linus Torvalds | 033d995 | 2012-10-02 09:54:49 -0700 | [diff] [blame] | 132 | INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 133 | |
| 134 | platform_set_drvdata(pdev, data); |
| 135 | |
Chanwoo Choi | 1876fd9 | 2014-04-21 20:49:30 +0900 | [diff] [blame] | 136 | err = devm_extcon_dev_register(&pdev->dev, data->edev); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 137 | if (err) |
Sangjung Woo | 4b5dd73 | 2014-04-21 19:10:09 +0900 | [diff] [blame] | 138 | return err; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 139 | |
| 140 | data->irq = platform_get_irq(pdev, 0); |
| 141 | if (!data->irq) { |
| 142 | dev_err(&pdev->dev, "platform_get_irq failed\n"); |
Sangjung Woo | 4b5dd73 | 2014-04-21 19:10:09 +0900 | [diff] [blame] | 143 | return -ENODEV; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | err = request_any_context_irq(data->irq, adc_jack_irq_thread, |
| 147 | pdata->irq_flags, pdata->name, data); |
| 148 | |
Axel Lin | 0301975 | 2012-10-02 09:14:59 +0900 | [diff] [blame] | 149 | if (err < 0) { |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 150 | dev_err(&pdev->dev, "error: irq %d\n", data->irq); |
Sangjung Woo | 4b5dd73 | 2014-04-21 19:10:09 +0900 | [diff] [blame] | 151 | return err; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Axel Lin | 0301975 | 2012-10-02 09:14:59 +0900 | [diff] [blame] | 154 | return 0; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Bill Pemberton | 93ed032 | 2012-11-19 13:25:49 -0500 | [diff] [blame] | 157 | static int adc_jack_remove(struct platform_device *pdev) |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 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); |
Ivan T. Ivanov | 5a696d9 | 2014-12-17 17:59:27 +0200 | [diff] [blame] | 163 | iio_channel_release(data->chan); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static struct platform_driver adc_jack_driver = { |
| 169 | .probe = adc_jack_probe, |
Bill Pemberton | 5f7e222 | 2012-11-19 13:20:06 -0500 | [diff] [blame] | 170 | .remove = adc_jack_remove, |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 171 | .driver = { |
| 172 | .name = "adc-jack", |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 173 | }, |
| 174 | }; |
| 175 | |
| 176 | module_platform_driver(adc_jack_driver); |
Axel Lin | d9310e3 | 2012-10-02 09:16:53 +0900 | [diff] [blame] | 177 | |
| 178 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 179 | MODULE_DESCRIPTION("ADC Jack extcon driver"); |
| 180 | MODULE_LICENSE("GPL v2"); |