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/module.h> 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 30 * @edev: extcon device. 31 * @cable_names: list of supported cables. 32 * @num_cables: size of cable_names. 33 * @adc_conditions: list of adc value conditions. 34 * @num_conditions: size of adc_conditions. 35 * @irq: irq number of attach/detach event (0 if not exist). 36 * @handling_delay: interrupt handler will schedule extcon event 37 * handling at handling_delay jiffies. 38 * @handler: extcon event handler called by interrupt handler. 39 * @chan: iio channel being queried. 40 */ 41 struct adc_jack_data { 42 struct extcon_dev *edev; 43 44 const char **cable_names; 45 int num_cables; 46 struct adc_jack_cond *adc_conditions; 47 int num_conditions; 48 49 int irq; 50 unsigned long handling_delay; /* in jiffies */ 51 struct delayed_work handler; 52 53 struct iio_channel *chan; 54 }; 55 56 static 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) { 67 dev_err(&data->edev->dev, "read channel() error: %d\n", ret); 68 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 83 extcon_set_state(data->edev, state); 84 } 85 86 static irqreturn_t adc_jack_irq_thread(int irq, void *_data) 87 { 88 struct adc_jack_data *data = _data; 89 90 queue_delayed_work(system_power_efficient_wq, 91 &data->handler, data->handling_delay); 92 return IRQ_HANDLED; 93 } 94 95 static int adc_jack_probe(struct platform_device *pdev) 96 { 97 struct adc_jack_data *data; 98 struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev); 99 int i, err = 0; 100 101 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 102 if (!data) 103 return -ENOMEM; 104 105 if (!pdata->cable_names) { 106 dev_err(&pdev->dev, "error: cable_names not defined.\n"); 107 return -EINVAL; 108 } 109 110 data->edev = devm_extcon_dev_allocate(&pdev->dev, pdata->cable_names); 111 if (IS_ERR(data->edev)) { 112 dev_err(&pdev->dev, "failed to allocate extcon device\n"); 113 return -ENOMEM; 114 } 115 data->edev->dev.parent = &pdev->dev; 116 data->edev->name = pdata->name; 117 118 /* Check the length of array and set num_cables */ 119 for (i = 0; data->edev->supported_cable[i]; i++) 120 ; 121 if (i == 0 || i > SUPPORTED_CABLE_MAX) { 122 dev_err(&pdev->dev, "error: pdata->cable_names size = %d\n", 123 i - 1); 124 return -EINVAL; 125 } 126 data->num_cables = i; 127 128 if (!pdata->adc_conditions || 129 !pdata->adc_conditions[0].state) { 130 dev_err(&pdev->dev, "error: adc_conditions not defined.\n"); 131 return -EINVAL; 132 } 133 data->adc_conditions = pdata->adc_conditions; 134 135 /* Check the length of array and set num_conditions */ 136 for (i = 0; data->adc_conditions[i].state; i++) 137 ; 138 data->num_conditions = i; 139 140 data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel); 141 if (IS_ERR(data->chan)) 142 return PTR_ERR(data->chan); 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 = devm_extcon_dev_register(&pdev->dev, data->edev); 151 if (err) 152 return err; 153 154 data->irq = platform_get_irq(pdev, 0); 155 if (!data->irq) { 156 dev_err(&pdev->dev, "platform_get_irq failed\n"); 157 return -ENODEV; 158 } 159 160 err = request_any_context_irq(data->irq, adc_jack_irq_thread, 161 pdata->irq_flags, pdata->name, data); 162 163 if (err < 0) { 164 dev_err(&pdev->dev, "error: irq %d\n", data->irq); 165 return err; 166 } 167 168 return 0; 169 } 170 171 static int adc_jack_remove(struct platform_device *pdev) 172 { 173 struct adc_jack_data *data = platform_get_drvdata(pdev); 174 175 free_irq(data->irq, data); 176 cancel_work_sync(&data->handler.work); 177 178 return 0; 179 } 180 181 static struct platform_driver adc_jack_driver = { 182 .probe = adc_jack_probe, 183 .remove = adc_jack_remove, 184 .driver = { 185 .name = "adc-jack", 186 .owner = THIS_MODULE, 187 }, 188 }; 189 190 module_platform_driver(adc_jack_driver); 191 192 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); 193 MODULE_DESCRIPTION("ADC Jack extcon driver"); 194 MODULE_LICENSE("GPL v2"); 195