extcon-adc-jack.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/module.h>
  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. * struct adc_jack_data - internal data for adc_jack device driver
  28. * @edev - extcon device.
  29. * @cable_names - list of supported cables.
  30. * @num_cables - size of cable_names.
  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.
  38. */
  39. struct adc_jack_data {
  40. struct extcon_dev edev;
  41. const char **cable_names;
  42. int num_cables;
  43. struct adc_jack_cond *adc_conditions;
  44. int num_conditions;
  45. int irq;
  46. unsigned long handling_delay; /* in jiffies */
  47. struct delayed_work handler;
  48. struct iio_channel *chan;
  49. };
  50. static void adc_jack_handler(struct work_struct *work)
  51. {
  52. struct adc_jack_data *data = container_of(to_delayed_work(work),
  53. struct adc_jack_data,
  54. handler);
  55. u32 state = 0;
  56. int ret, adc_val;
  57. int i;
  58. ret = iio_read_channel_raw(data->chan, &adc_val);
  59. if (ret < 0) {
  60. dev_err(data->edev.dev, "read channel() error: %d\n", ret);
  61. return;
  62. }
  63. /* Get state from adc value with adc_conditions */
  64. for (i = 0; i < data->num_conditions; i++) {
  65. struct adc_jack_cond *def = &data->adc_conditions[i];
  66. if (!def->state)
  67. break;
  68. if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
  69. state = def->state;
  70. break;
  71. }
  72. }
  73. /* if no def has met, it means state = 0 (no cables attached) */
  74. extcon_set_state(&data->edev, state);
  75. }
  76. static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
  77. {
  78. struct adc_jack_data *data = _data;
  79. schedule_delayed_work(&data->handler, data->handling_delay);
  80. return IRQ_HANDLED;
  81. }
  82. static int adc_jack_probe(struct platform_device *pdev)
  83. {
  84. struct adc_jack_data *data;
  85. struct adc_jack_pdata *pdata = pdev->dev.platform_data;
  86. int i, err = 0;
  87. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  88. if (!data)
  89. return -ENOMEM;
  90. data->edev.name = pdata->name;
  91. if (!pdata->cable_names) {
  92. err = -EINVAL;
  93. dev_err(&pdev->dev, "error: cable_names not defined.\n");
  94. goto out;
  95. }
  96. data->edev.supported_cable = pdata->cable_names;
  97. /* Check the length of array and set num_cables */
  98. for (i = 0; data->edev.supported_cable[i]; i++)
  99. ;
  100. if (i == 0 || i > SUPPORTED_CABLE_MAX) {
  101. err = -EINVAL;
  102. dev_err(&pdev->dev, "error: pdata->cable_names size = %d\n",
  103. i - 1);
  104. goto out;
  105. }
  106. data->num_cables = i;
  107. if (!pdata->adc_conditions ||
  108. !pdata->adc_conditions[0].state) {
  109. err = -EINVAL;
  110. dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
  111. goto out;
  112. }
  113. data->adc_conditions = pdata->adc_conditions;
  114. /* Check the length of array and set num_conditions */
  115. for (i = 0; data->adc_conditions[i].state; i++)
  116. ;
  117. data->num_conditions = i;
  118. data->chan = iio_channel_get(&pdev->dev, 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_DEFERRABLE_WORK(&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 < 0) {
  138. dev_err(&pdev->dev, "error: irq %d\n", data->irq);
  139. goto err_irq;
  140. }
  141. return 0;
  142. err_irq:
  143. extcon_dev_unregister(&data->edev);
  144. out:
  145. return err;
  146. }
  147. static int adc_jack_remove(struct platform_device *pdev)
  148. {
  149. struct adc_jack_data *data = platform_get_drvdata(pdev);
  150. free_irq(data->irq, data);
  151. cancel_work_sync(&data->handler.work);
  152. extcon_dev_unregister(&data->edev);
  153. return 0;
  154. }
  155. static struct platform_driver adc_jack_driver = {
  156. .probe = adc_jack_probe,
  157. .remove = adc_jack_remove,
  158. .driver = {
  159. .name = "adc-jack",
  160. .owner = THIS_MODULE,
  161. },
  162. };
  163. module_platform_driver(adc_jack_driver);
  164. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  165. MODULE_DESCRIPTION("ADC Jack extcon driver");
  166. MODULE_LICENSE("GPL v2");