extcon-adc-jack.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #include <linux/slab.h>
  17. #include <linux/device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/err.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/iio/consumer.h>
  23. #include <linux/extcon/extcon-adc-jack.h>
  24. #include <linux/extcon.h>
  25. /**
  26. * struct adc_jack_data - internal data for adc_jack device driver
  27. * @edev - extcon device.
  28. * @cable_names - list of supported cables.
  29. * @num_cables - size of cable_names.
  30. * @adc_conditions - list of adc value conditions.
  31. * @num_conditions - size of adc_conditions.
  32. * @irq - irq number of attach/detach event (0 if not exist).
  33. * @handling_delay - interrupt handler will schedule extcon event
  34. * handling at handling_delay jiffies.
  35. * @handler - extcon event handler called by interrupt handler.
  36. * @chan - iio channel being queried.
  37. */
  38. struct adc_jack_data {
  39. struct extcon_dev edev;
  40. const char **cable_names;
  41. int num_cables;
  42. struct adc_jack_cond *adc_conditions;
  43. int num_conditions;
  44. int irq;
  45. unsigned long handling_delay; /* in jiffies */
  46. struct delayed_work handler;
  47. struct iio_channel *chan;
  48. };
  49. static void adc_jack_handler(struct work_struct *work)
  50. {
  51. struct adc_jack_data *data = container_of(to_delayed_work(work),
  52. struct adc_jack_data,
  53. handler);
  54. u32 state = 0;
  55. int ret, adc_val;
  56. int i;
  57. ret = iio_read_channel_raw(data->chan, &adc_val);
  58. if (ret < 0) {
  59. dev_err(data->edev.dev, "read channel() error: %d\n", ret);
  60. return;
  61. }
  62. /* Get state from adc value with adc_conditions */
  63. for (i = 0; i < data->num_conditions; i++) {
  64. struct adc_jack_cond *def = &data->adc_conditions[i];
  65. if (!def->state)
  66. break;
  67. if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
  68. state = def->state;
  69. break;
  70. }
  71. }
  72. /* if no def has met, it means state = 0 (no cables attached) */
  73. extcon_set_state(&data->edev, state);
  74. }
  75. static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
  76. {
  77. struct adc_jack_data *data = _data;
  78. schedule_delayed_work(&data->handler, data->handling_delay);
  79. return IRQ_HANDLED;
  80. }
  81. static int __devinit adc_jack_probe(struct platform_device *pdev)
  82. {
  83. struct adc_jack_data *data;
  84. struct adc_jack_pdata *pdata = pdev->dev.platform_data;
  85. int i, err = 0;
  86. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  87. if (!data)
  88. return -ENOMEM;
  89. data->edev.name = pdata->name;
  90. if (!pdata->cable_names) {
  91. err = -EINVAL;
  92. dev_err(&pdev->dev, "error: cable_names not defined.\n");
  93. goto out;
  94. }
  95. data->edev.supported_cable = pdata->cable_names;
  96. /* Check the length of array and set num_cables */
  97. for (i = 0; data->edev.supported_cable[i]; i++)
  98. ;
  99. if (i == 0 || i > SUPPORTED_CABLE_MAX) {
  100. err = -EINVAL;
  101. dev_err(&pdev->dev, "error: pdata->cable_names size = %d\n",
  102. i - 1);
  103. goto out;
  104. }
  105. data->num_cables = i;
  106. if (!pdata->adc_conditions ||
  107. !pdata->adc_conditions[0].state) {
  108. err = -EINVAL;
  109. dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
  110. goto out;
  111. }
  112. data->adc_conditions = pdata->adc_conditions;
  113. /* Check the length of array and set num_conditions */
  114. for (i = 0; data->adc_conditions[i].state; i++)
  115. ;
  116. data->num_conditions = i;
  117. data->chan = iio_channel_get(dev_name(&pdev->dev),
  118. pdata->consumer_channel);
  119. if (IS_ERR(data->chan)) {
  120. err = PTR_ERR(data->chan);
  121. goto out;
  122. }
  123. data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
  124. INIT_DELAYED_WORK_DEFERRABLE(&data->handler, adc_jack_handler);
  125. platform_set_drvdata(pdev, data);
  126. err = extcon_dev_register(&data->edev, &pdev->dev);
  127. if (err)
  128. goto out;
  129. data->irq = platform_get_irq(pdev, 0);
  130. if (!data->irq) {
  131. dev_err(&pdev->dev, "platform_get_irq failed\n");
  132. err = -ENODEV;
  133. goto err_irq;
  134. }
  135. err = request_any_context_irq(data->irq, adc_jack_irq_thread,
  136. pdata->irq_flags, pdata->name, data);
  137. if (err) {
  138. dev_err(&pdev->dev, "error: irq %d\n", data->irq);
  139. err = -EINVAL;
  140. goto err_irq;
  141. }
  142. goto out;
  143. err_irq:
  144. extcon_dev_unregister(&data->edev);
  145. out:
  146. return err;
  147. }
  148. static int __devexit adc_jack_remove(struct platform_device *pdev)
  149. {
  150. struct adc_jack_data *data = platform_get_drvdata(pdev);
  151. free_irq(data->irq, data);
  152. cancel_work_sync(&data->handler.work);
  153. extcon_dev_unregister(&data->edev);
  154. return 0;
  155. }
  156. static struct platform_driver adc_jack_driver = {
  157. .probe = adc_jack_probe,
  158. .remove = __devexit_p(adc_jack_remove),
  159. .driver = {
  160. .name = "adc-jack",
  161. .owner = THIS_MODULE,
  162. },
  163. };
  164. module_platform_driver(adc_jack_driver);