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 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> 25 #include <linux/extcon.h> 26 27 /** 28 * struct adc_jack_data - internal data for adc_jack device driver 29 * @edev - extcon device. 30 * @cable_names - list of supported cables. 31 * @num_cables - size of cable_names. 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. 39 */ 40 struct adc_jack_data { 41 struct extcon_dev edev; 42 43 const char **cable_names; 44 int num_cables; 45 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; 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); 60 u32 state = 0; 61 int ret, adc_val; 62 int i; 63 64 ret = iio_read_channel_raw(data->chan, &adc_val); 65 if (ret < 0) { 66 dev_err(data->edev.dev, "read channel() error: %d\n", ret); 67 return; 68 } 69 70 /* Get state from adc value with adc_conditions */ 71 for (i = 0; i < data->num_conditions; i++) { 72 struct adc_jack_cond *def = &data->adc_conditions[i]; 73 if (!def->state) 74 break; 75 if (def->min_adc <= adc_val && def->max_adc >= adc_val) { 76 state = def->state; 77 break; 78 } 79 } 80 /* if no def has met, it means state = 0 (no cables attached) */ 81 82 extcon_set_state(&data->edev, state); 83 } 84 85 static irqreturn_t adc_jack_irq_thread(int irq, void *_data) 86 { 87 struct adc_jack_data *data = _data; 88 89 schedule_delayed_work(&data->handler, data->handling_delay); 90 return IRQ_HANDLED; 91 } 92 93 static int __devinit adc_jack_probe(struct platform_device *pdev) 94 { 95 struct adc_jack_data *data; 96 struct adc_jack_pdata *pdata = pdev->dev.platform_data; 97 int i, err = 0; 98 99 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 100 if (!data) 101 return -ENOMEM; 102 103 data->edev.name = pdata->name; 104 105 if (!pdata->cable_names) { 106 err = -EINVAL; 107 dev_err(&pdev->dev, "error: cable_names not defined.\n"); 108 goto out; 109 } 110 111 data->edev.supported_cable = pdata->cable_names; 112 113 /* Check the length of array and set num_cables */ 114 for (i = 0; data->edev.supported_cable[i]; i++) 115 ; 116 if (i == 0 || i > SUPPORTED_CABLE_MAX) { 117 err = -EINVAL; 118 dev_err(&pdev->dev, "error: pdata->cable_names size = %d\n", 119 i - 1); 120 goto out; 121 } 122 data->num_cables = i; 123 124 if (!pdata->adc_conditions || 125 !pdata->adc_conditions[0].state) { 126 err = -EINVAL; 127 dev_err(&pdev->dev, "error: adc_conditions not defined.\n"); 128 goto out; 129 } 130 data->adc_conditions = pdata->adc_conditions; 131 132 /* Check the length of array and set num_conditions */ 133 for (i = 0; data->adc_conditions[i].state; i++) 134 ; 135 data->num_conditions = i; 136 137 data->chan = iio_channel_get(dev_name(&pdev->dev), 138 pdata->consumer_channel); 139 if (IS_ERR(data->chan)) { 140 err = PTR_ERR(data->chan); 141 goto out; 142 } 143 144 data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms); 145 146 INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler); 147 148 platform_set_drvdata(pdev, data); 149 150 err = extcon_dev_register(&data->edev, &pdev->dev); 151 if (err) 152 goto out; 153 154 data->irq = platform_get_irq(pdev, 0); 155 if (!data->irq) { 156 dev_err(&pdev->dev, "platform_get_irq failed\n"); 157 err = -ENODEV; 158 goto err_irq; 159 } 160 161 err = request_any_context_irq(data->irq, adc_jack_irq_thread, 162 pdata->irq_flags, pdata->name, data); 163 164 if (err) { 165 dev_err(&pdev->dev, "error: irq %d\n", data->irq); 166 err = -EINVAL; 167 goto err_irq; 168 } 169 170 goto out; 171 172 err_irq: 173 extcon_dev_unregister(&data->edev); 174 out: 175 return err; 176 } 177 178 static int __devexit adc_jack_remove(struct platform_device *pdev) 179 { 180 struct adc_jack_data *data = platform_get_drvdata(pdev); 181 182 free_irq(data->irq, data); 183 cancel_work_sync(&data->handler.work); 184 extcon_dev_unregister(&data->edev); 185 186 return 0; 187 } 188 189 static struct platform_driver adc_jack_driver = { 190 .probe = adc_jack_probe, 191 .remove = __devexit_p(adc_jack_remove), 192 .driver = { 193 .name = "adc-jack", 194 .owner = THIS_MODULE, 195 }, 196 }; 197 198 module_platform_driver(adc_jack_driver); 199