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 | * |
Chanwoo Choi | a7da72e | 2016-07-18 16:16:29 +0900 | [diff] [blame] | 6 | * Copyright (C) 2016 Samsung Electronics |
| 7 | * Chanwoo Choi <cw00.choi@samsung.com> |
| 8 | * |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 9 | * Copyright (C) 2012 Samsung Electronics |
| 10 | * MyungJoo Ham <myungjoo.ham@samsung.com> |
| 11 | * |
| 12 | * Modified for calling to IIO to get adc by <anish.singh@samsung.com> |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License version 2 as |
| 16 | * published by the Free Software Foundation. |
| 17 | * |
| 18 | */ |
| 19 | |
Axel Lin | d9310e3 | 2012-10-02 09:16:53 +0900 | [diff] [blame] | 20 | #include <linux/module.h> |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/workqueue.h> |
| 27 | #include <linux/iio/consumer.h> |
| 28 | #include <linux/extcon/extcon-adc-jack.h> |
| 29 | #include <linux/extcon.h> |
| 30 | |
| 31 | /** |
| 32 | * struct adc_jack_data - internal data for adc_jack device driver |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 33 | * @edev: extcon device. |
| 34 | * @cable_names: list of supported cables. |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 35 | * @adc_conditions: list of adc value conditions. |
| 36 | * @num_conditions: size of adc_conditions. |
| 37 | * @irq: irq number of attach/detach event (0 if not exist). |
| 38 | * @handling_delay: interrupt handler will schedule extcon event |
| 39 | * handling at handling_delay jiffies. |
| 40 | * @handler: extcon event handler called by interrupt handler. |
| 41 | * @chan: iio channel being queried. |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 42 | */ |
| 43 | struct adc_jack_data { |
Venkat Reddy Talla | 1b6cf31 | 2016-06-30 17:54:00 +0900 | [diff] [blame] | 44 | struct device *dev; |
Chanwoo Choi | 1876fd9 | 2014-04-21 20:49:30 +0900 | [diff] [blame] | 45 | struct extcon_dev *edev; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 46 | |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 47 | const unsigned int **cable_names; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 48 | struct adc_jack_cond *adc_conditions; |
| 49 | int num_conditions; |
| 50 | |
| 51 | int irq; |
| 52 | unsigned long handling_delay; /* in jiffies */ |
| 53 | struct delayed_work handler; |
| 54 | |
| 55 | struct iio_channel *chan; |
Venkat Reddy Talla | 1b6cf31 | 2016-06-30 17:54:00 +0900 | [diff] [blame] | 56 | bool wakeup_source; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | static void adc_jack_handler(struct work_struct *work) |
| 60 | { |
| 61 | struct adc_jack_data *data = container_of(to_delayed_work(work), |
| 62 | struct adc_jack_data, |
| 63 | handler); |
Chanwoo Choi | a7da72e | 2016-07-18 16:16:29 +0900 | [diff] [blame] | 64 | struct adc_jack_cond *def; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 65 | int ret, adc_val; |
| 66 | int i; |
| 67 | |
| 68 | ret = iio_read_channel_raw(data->chan, &adc_val); |
| 69 | if (ret < 0) { |
Chanwoo Choi | 1876fd9 | 2014-04-21 20:49:30 +0900 | [diff] [blame] | 70 | dev_err(&data->edev->dev, "read channel() error: %d\n", ret); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
| 74 | /* Get state from adc value with adc_conditions */ |
| 75 | for (i = 0; i < data->num_conditions; i++) { |
Chanwoo Choi | a7da72e | 2016-07-18 16:16:29 +0900 | [diff] [blame] | 76 | def = &data->adc_conditions[i]; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 77 | if (def->min_adc <= adc_val && def->max_adc >= adc_val) { |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 78 | extcon_set_state_sync(data->edev, def->id, true); |
Chanwoo Choi | a7da72e | 2016-07-18 16:16:29 +0900 | [diff] [blame] | 79 | return; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 82 | |
Chanwoo Choi | a7da72e | 2016-07-18 16:16:29 +0900 | [diff] [blame] | 83 | /* Set the detached state if adc value is not included in the range */ |
| 84 | for (i = 0; i < data->num_conditions; i++) { |
| 85 | def = &data->adc_conditions[i]; |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 86 | extcon_set_state_sync(data->edev, def->id, false); |
Chanwoo Choi | a7da72e | 2016-07-18 16:16:29 +0900 | [diff] [blame] | 87 | } |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static irqreturn_t adc_jack_irq_thread(int irq, void *_data) |
| 91 | { |
| 92 | struct adc_jack_data *data = _data; |
| 93 | |
Mark Brown | 1a82e81 | 2013-07-19 18:47:35 +0100 | [diff] [blame] | 94 | queue_delayed_work(system_power_efficient_wq, |
| 95 | &data->handler, data->handling_delay); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 96 | return IRQ_HANDLED; |
| 97 | } |
| 98 | |
Bill Pemberton | 44f34fd | 2012-11-19 13:23:21 -0500 | [diff] [blame] | 99 | static int adc_jack_probe(struct platform_device *pdev) |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 100 | { |
| 101 | struct adc_jack_data *data; |
Jingoo Han | 7c0f6558 | 2013-09-11 13:22:18 +0900 | [diff] [blame] | 102 | struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 103 | int i, err = 0; |
| 104 | |
| 105 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 106 | if (!data) |
| 107 | return -ENOMEM; |
| 108 | |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 109 | if (!pdata->cable_names) { |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 110 | dev_err(&pdev->dev, "error: cable_names not defined.\n"); |
Sangjung Woo | 4b5dd73 | 2014-04-21 19:10:09 +0900 | [diff] [blame] | 111 | return -EINVAL; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Venkat Reddy Talla | 1b6cf31 | 2016-06-30 17:54:00 +0900 | [diff] [blame] | 114 | data->dev = &pdev->dev; |
Chanwoo Choi | 1876fd9 | 2014-04-21 20:49:30 +0900 | [diff] [blame] | 115 | data->edev = devm_extcon_dev_allocate(&pdev->dev, pdata->cable_names); |
| 116 | if (IS_ERR(data->edev)) { |
| 117 | dev_err(&pdev->dev, "failed to allocate extcon device\n"); |
| 118 | return -ENOMEM; |
| 119 | } |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 120 | |
Chanwoo Choi | a7da72e | 2016-07-18 16:16:29 +0900 | [diff] [blame] | 121 | if (!pdata->adc_conditions) { |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 122 | dev_err(&pdev->dev, "error: adc_conditions not defined.\n"); |
Sangjung Woo | 4b5dd73 | 2014-04-21 19:10:09 +0900 | [diff] [blame] | 123 | return -EINVAL; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 124 | } |
| 125 | data->adc_conditions = pdata->adc_conditions; |
| 126 | |
| 127 | /* Check the length of array and set num_conditions */ |
Chanwoo Choi | a7da72e | 2016-07-18 16:16:29 +0900 | [diff] [blame] | 128 | for (i = 0; data->adc_conditions[i].id != EXTCON_NONE; i++); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 129 | data->num_conditions = i; |
| 130 | |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 131 | data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel); |
Sangjung Woo | 4b5dd73 | 2014-04-21 19:10:09 +0900 | [diff] [blame] | 132 | if (IS_ERR(data->chan)) |
| 133 | return PTR_ERR(data->chan); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 134 | |
| 135 | data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms); |
Venkat Reddy Talla | 1b6cf31 | 2016-06-30 17:54:00 +0900 | [diff] [blame] | 136 | data->wakeup_source = pdata->wakeup_source; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 137 | |
Linus Torvalds | 033d995 | 2012-10-02 09:54:49 -0700 | [diff] [blame] | 138 | INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 139 | |
| 140 | platform_set_drvdata(pdev, data); |
| 141 | |
Chanwoo Choi | 1876fd9 | 2014-04-21 20:49:30 +0900 | [diff] [blame] | 142 | err = devm_extcon_dev_register(&pdev->dev, data->edev); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 143 | if (err) |
Sangjung Woo | 4b5dd73 | 2014-04-21 19:10:09 +0900 | [diff] [blame] | 144 | return err; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 145 | |
| 146 | data->irq = platform_get_irq(pdev, 0); |
| 147 | if (!data->irq) { |
| 148 | dev_err(&pdev->dev, "platform_get_irq failed\n"); |
Sangjung Woo | 4b5dd73 | 2014-04-21 19:10:09 +0900 | [diff] [blame] | 149 | return -ENODEV; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | err = request_any_context_irq(data->irq, adc_jack_irq_thread, |
| 153 | pdata->irq_flags, pdata->name, data); |
| 154 | |
Axel Lin | 0301975 | 2012-10-02 09:14:59 +0900 | [diff] [blame] | 155 | if (err < 0) { |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 156 | dev_err(&pdev->dev, "error: irq %d\n", data->irq); |
Sangjung Woo | 4b5dd73 | 2014-04-21 19:10:09 +0900 | [diff] [blame] | 157 | return err; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Venkat Reddy Talla | 1b6cf31 | 2016-06-30 17:54:00 +0900 | [diff] [blame] | 160 | if (data->wakeup_source) |
| 161 | device_init_wakeup(&pdev->dev, 1); |
| 162 | |
Venkat Reddy Talla | ba4b271 | 2016-07-05 19:26:21 +0530 | [diff] [blame] | 163 | adc_jack_handler(&data->handler.work); |
Axel Lin | 0301975 | 2012-10-02 09:14:59 +0900 | [diff] [blame] | 164 | return 0; |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Bill Pemberton | 93ed032 | 2012-11-19 13:25:49 -0500 | [diff] [blame] | 167 | static int adc_jack_remove(struct platform_device *pdev) |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 168 | { |
| 169 | struct adc_jack_data *data = platform_get_drvdata(pdev); |
| 170 | |
| 171 | free_irq(data->irq, data); |
| 172 | cancel_work_sync(&data->handler.work); |
Ivan T. Ivanov | 5a696d9 | 2014-12-17 17:59:27 +0200 | [diff] [blame] | 173 | iio_channel_release(data->chan); |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Venkat Reddy Talla | 1b6cf31 | 2016-06-30 17:54:00 +0900 | [diff] [blame] | 178 | #ifdef CONFIG_PM_SLEEP |
| 179 | static int adc_jack_suspend(struct device *dev) |
| 180 | { |
| 181 | struct adc_jack_data *data = dev_get_drvdata(dev); |
| 182 | |
| 183 | cancel_delayed_work_sync(&data->handler); |
| 184 | if (device_may_wakeup(data->dev)) |
| 185 | enable_irq_wake(data->irq); |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static int adc_jack_resume(struct device *dev) |
| 191 | { |
| 192 | struct adc_jack_data *data = dev_get_drvdata(dev); |
| 193 | |
| 194 | if (device_may_wakeup(data->dev)) |
| 195 | disable_irq_wake(data->irq); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | #endif /* CONFIG_PM_SLEEP */ |
| 200 | |
| 201 | static SIMPLE_DEV_PM_OPS(adc_jack_pm_ops, |
| 202 | adc_jack_suspend, adc_jack_resume); |
| 203 | |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 204 | static struct platform_driver adc_jack_driver = { |
| 205 | .probe = adc_jack_probe, |
Bill Pemberton | 5f7e222 | 2012-11-19 13:20:06 -0500 | [diff] [blame] | 206 | .remove = adc_jack_remove, |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 207 | .driver = { |
| 208 | .name = "adc-jack", |
Venkat Reddy Talla | 1b6cf31 | 2016-06-30 17:54:00 +0900 | [diff] [blame] | 209 | .pm = &adc_jack_pm_ops, |
anish kumar | 1993986 | 2012-08-17 09:57:22 -0700 | [diff] [blame] | 210 | }, |
| 211 | }; |
| 212 | |
| 213 | module_platform_driver(adc_jack_driver); |
Axel Lin | d9310e3 | 2012-10-02 09:16:53 +0900 | [diff] [blame] | 214 | |
| 215 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 216 | MODULE_DESCRIPTION("ADC Jack extcon driver"); |
| 217 | MODULE_LICENSE("GPL v2"); |