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