Merge branch 'linux-linaro-lsk-v4.4-android' of git://git.linaro.org/kernel/linux...
[firefly-linux-kernel-4.4.55.git] / drivers / extcon / extcon-adc-jack.c
index 5985807e52c9439fc1f7587aa2c0645f5e344c57..8df3eea9e6cb5b389340d4f6badcb1d836ec0012 100644 (file)
@@ -3,6 +3,9 @@
  *
  * Analog Jack extcon driver with ADC-based detection capability.
  *
+ * Copyright (C) 2016 Samsung Electronics
+ * Chanwoo Choi <cw00.choi@samsung.com>
+ *
  * Copyright (C) 2012 Samsung Electronics
  * MyungJoo Ham <myungjoo.ham@samsung.com>
  *
 
 /**
  * struct adc_jack_data - internal data for adc_jack device driver
- * @edev        - extcon device.
- * @cable_names - list of supported cables.
- * @num_cables  - size of cable_names.
- * @adc_conditions       - list of adc value conditions.
- * @num_conditions       - size of adc_conditions.
- * @irq         - irq number of attach/detach event (0 if not exist).
- * @handling_delay      - interrupt handler will schedule extcon event
- *                      handling at handling_delay jiffies.
- * @handler     - extcon event handler called by interrupt handler.
- * @chan       - iio channel being queried.
+ * @edev:              extcon device.
+ * @cable_names:       list of supported cables.
+ * @adc_conditions:    list of adc value conditions.
+ * @num_conditions:    size of adc_conditions.
+ * @irq:               irq number of attach/detach event (0 if not exist).
+ * @handling_delay:    interrupt handler will schedule extcon event
+ *                     handling at handling_delay jiffies.
+ * @handler:           extcon event handler called by interrupt handler.
+ * @chan:              iio channel being queried.
  */
 struct adc_jack_data {
-       struct extcon_dev edev;
+       struct extcon_dev *edev;
 
-       const char **cable_names;
-       int num_cables;
+       const unsigned int **cable_names;
        struct adc_jack_cond *adc_conditions;
        int num_conditions;
 
@@ -58,29 +59,30 @@ static void adc_jack_handler(struct work_struct *work)
        struct adc_jack_data *data = container_of(to_delayed_work(work),
                        struct adc_jack_data,
                        handler);
-       u32 state = 0;
+       struct adc_jack_cond *def;
        int ret, adc_val;
        int i;
 
        ret = iio_read_channel_raw(data->chan, &adc_val);
        if (ret < 0) {
-               dev_err(data->edev.dev, "read channel() error: %d\n", ret);
+               dev_err(&data->edev->dev, "read channel() error: %d\n", ret);
                return;
        }
 
        /* Get state from adc value with adc_conditions */
        for (i = 0; i < data->num_conditions; i++) {
-               struct adc_jack_cond *def = &data->adc_conditions[i];
-               if (!def->state)
-                       break;
+               def = &data->adc_conditions[i];
                if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
-                       state = def->state;
-                       break;
+                       extcon_set_cable_state_(data->edev, def->id, true);
+                       return;
                }
        }
-       /* if no def has met, it means state = 0 (no cables attached) */
 
-       extcon_set_state(&data->edev, state);
+       /* Set the detached state if adc value is not included in the range */
+       for (i = 0; i < data->num_conditions; i++) {
+               def = &data->adc_conditions[i];
+               extcon_set_cable_state_(data->edev, def->id, false);
+       }
 }
 
 static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
@@ -95,52 +97,37 @@ static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
 static int adc_jack_probe(struct platform_device *pdev)
 {
        struct adc_jack_data *data;
-       struct adc_jack_pdata *pdata = pdev->dev.platform_data;
+       struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
        int i, err = 0;
 
        data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
        if (!data)
                return -ENOMEM;
 
-       data->edev.name = pdata->name;
-
        if (!pdata->cable_names) {
-               err = -EINVAL;
                dev_err(&pdev->dev, "error: cable_names not defined.\n");
-               goto out;
+               return -EINVAL;
        }
 
-       data->edev.supported_cable = pdata->cable_names;
-
-       /* Check the length of array and set num_cables */
-       for (i = 0; data->edev.supported_cable[i]; i++)
-               ;
-       if (i == 0 || i > SUPPORTED_CABLE_MAX) {
-               err = -EINVAL;
-               dev_err(&pdev->dev, "error: pdata->cable_names size = %d\n",
-                               i - 1);
-               goto out;
+       data->edev = devm_extcon_dev_allocate(&pdev->dev, pdata->cable_names);
+       if (IS_ERR(data->edev)) {
+               dev_err(&pdev->dev, "failed to allocate extcon device\n");
+               return -ENOMEM;
        }
-       data->num_cables = i;
 
-       if (!pdata->adc_conditions ||
-                       !pdata->adc_conditions[0].state) {
-               err = -EINVAL;
+       if (!pdata->adc_conditions) {
                dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
-               goto out;
+               return -EINVAL;
        }
        data->adc_conditions = pdata->adc_conditions;
 
        /* Check the length of array and set num_conditions */
-       for (i = 0; data->adc_conditions[i].state; i++)
-               ;
+       for (i = 0; data->adc_conditions[i].id != EXTCON_NONE; i++);
        data->num_conditions = i;
 
        data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel);
-       if (IS_ERR(data->chan)) {
-               err = PTR_ERR(data->chan);
-               goto out;
-       }
+       if (IS_ERR(data->chan))
+               return PTR_ERR(data->chan);
 
        data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
 
@@ -148,15 +135,14 @@ static int adc_jack_probe(struct platform_device *pdev)
 
        platform_set_drvdata(pdev, data);
 
-       err = extcon_dev_register(&data->edev, &pdev->dev);
+       err = devm_extcon_dev_register(&pdev->dev, data->edev);
        if (err)
-               goto out;
+               return err;
 
        data->irq = platform_get_irq(pdev, 0);
        if (!data->irq) {
                dev_err(&pdev->dev, "platform_get_irq failed\n");
-               err = -ENODEV;
-               goto err_irq;
+               return -ENODEV;
        }
 
        err = request_any_context_irq(data->irq, adc_jack_irq_thread,
@@ -164,15 +150,10 @@ static int adc_jack_probe(struct platform_device *pdev)
 
        if (err < 0) {
                dev_err(&pdev->dev, "error: irq %d\n", data->irq);
-               goto err_irq;
+               return err;
        }
 
        return 0;
-
-err_irq:
-       extcon_dev_unregister(&data->edev);
-out:
-       return err;
 }
 
 static int adc_jack_remove(struct platform_device *pdev)
@@ -181,7 +162,7 @@ static int adc_jack_remove(struct platform_device *pdev)
 
        free_irq(data->irq, data);
        cancel_work_sync(&data->handler.work);
-       extcon_dev_unregister(&data->edev);
+       iio_channel_release(data->chan);
 
        return 0;
 }
@@ -191,7 +172,6 @@ static struct platform_driver adc_jack_driver = {
        .remove         = adc_jack_remove,
        .driver         = {
                .name   = "adc-jack",
-               .owner  = THIS_MODULE,
        },
 };