extcon-adc-jack.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/extcon/extcon-adc-jack.c
  4. *
  5. * Analog Jack extcon driver with ADC-based detection capability.
  6. *
  7. * Copyright (C) 2016 Samsung Electronics
  8. * Chanwoo Choi <cw00.choi@samsung.com>
  9. *
  10. * Copyright (C) 2012 Samsung Electronics
  11. * MyungJoo Ham <myungjoo.ham@samsung.com>
  12. *
  13. * Modified for calling to IIO to get adc by <anish.singh@samsung.com>
  14. */
  15. #include <linux/module.h>
  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-provider.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. * @adc_conditions: list of adc value conditions.
  30. * @num_conditions: size of adc_conditions.
  31. * @irq: irq number of attach/detach event (0 if not exist).
  32. * @handling_delay: interrupt handler will schedule extcon event
  33. * handling at handling_delay jiffies.
  34. * @handler: extcon event handler called by interrupt handler.
  35. * @chan: iio channel being queried.
  36. */
  37. struct adc_jack_data {
  38. struct device *dev;
  39. struct extcon_dev *edev;
  40. const unsigned int **cable_names;
  41. struct adc_jack_cond *adc_conditions;
  42. int num_conditions;
  43. int irq;
  44. unsigned long handling_delay; /* in jiffies */
  45. struct delayed_work handler;
  46. struct iio_channel *chan;
  47. bool wakeup_source;
  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. struct adc_jack_cond *def;
  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->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. def = &data->adc_conditions[i];
  65. if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
  66. extcon_set_state_sync(data->edev, def->id, true);
  67. return;
  68. }
  69. }
  70. /* Set the detached state if adc value is not included in the range */
  71. for (i = 0; i < data->num_conditions; i++) {
  72. def = &data->adc_conditions[i];
  73. extcon_set_state_sync(data->edev, def->id, false);
  74. }
  75. }
  76. static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
  77. {
  78. struct adc_jack_data *data = _data;
  79. queue_delayed_work(system_power_efficient_wq,
  80. &data->handler, data->handling_delay);
  81. return IRQ_HANDLED;
  82. }
  83. static int adc_jack_probe(struct platform_device *pdev)
  84. {
  85. struct adc_jack_data *data;
  86. struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
  87. int i, err = 0;
  88. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  89. if (!data)
  90. return -ENOMEM;
  91. if (!pdata->cable_names) {
  92. dev_err(&pdev->dev, "error: cable_names not defined.\n");
  93. return -EINVAL;
  94. }
  95. data->dev = &pdev->dev;
  96. data->edev = devm_extcon_dev_allocate(&pdev->dev, pdata->cable_names);
  97. if (IS_ERR(data->edev)) {
  98. dev_err(&pdev->dev, "failed to allocate extcon device\n");
  99. return -ENOMEM;
  100. }
  101. if (!pdata->adc_conditions) {
  102. dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
  103. return -EINVAL;
  104. }
  105. data->adc_conditions = pdata->adc_conditions;
  106. /* Check the length of array and set num_conditions */
  107. for (i = 0; data->adc_conditions[i].id != EXTCON_NONE; i++);
  108. data->num_conditions = i;
  109. data->chan = devm_iio_channel_get(&pdev->dev, pdata->consumer_channel);
  110. if (IS_ERR(data->chan))
  111. return PTR_ERR(data->chan);
  112. data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
  113. data->wakeup_source = pdata->wakeup_source;
  114. INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
  115. platform_set_drvdata(pdev, data);
  116. err = devm_extcon_dev_register(&pdev->dev, data->edev);
  117. if (err)
  118. return err;
  119. data->irq = platform_get_irq(pdev, 0);
  120. if (data->irq < 0)
  121. return -ENODEV;
  122. err = request_any_context_irq(data->irq, adc_jack_irq_thread,
  123. pdata->irq_flags, pdata->name, data);
  124. if (err < 0) {
  125. dev_err(&pdev->dev, "error: irq %d\n", data->irq);
  126. return err;
  127. }
  128. if (data->wakeup_source)
  129. device_init_wakeup(&pdev->dev, 1);
  130. adc_jack_handler(&data->handler.work);
  131. return 0;
  132. }
  133. static int adc_jack_remove(struct platform_device *pdev)
  134. {
  135. struct adc_jack_data *data = platform_get_drvdata(pdev);
  136. free_irq(data->irq, data);
  137. cancel_work_sync(&data->handler.work);
  138. return 0;
  139. }
  140. #ifdef CONFIG_PM_SLEEP
  141. static int adc_jack_suspend(struct device *dev)
  142. {
  143. struct adc_jack_data *data = dev_get_drvdata(dev);
  144. cancel_delayed_work_sync(&data->handler);
  145. if (device_may_wakeup(data->dev))
  146. enable_irq_wake(data->irq);
  147. return 0;
  148. }
  149. static int adc_jack_resume(struct device *dev)
  150. {
  151. struct adc_jack_data *data = dev_get_drvdata(dev);
  152. if (device_may_wakeup(data->dev))
  153. disable_irq_wake(data->irq);
  154. return 0;
  155. }
  156. #endif /* CONFIG_PM_SLEEP */
  157. static SIMPLE_DEV_PM_OPS(adc_jack_pm_ops,
  158. adc_jack_suspend, adc_jack_resume);
  159. static struct platform_driver adc_jack_driver = {
  160. .probe = adc_jack_probe,
  161. .remove = adc_jack_remove,
  162. .driver = {
  163. .name = "adc-jack",
  164. .pm = &adc_jack_pm_ops,
  165. },
  166. };
  167. module_platform_driver(adc_jack_driver);
  168. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  169. MODULE_DESCRIPTION("ADC Jack extcon driver");
  170. MODULE_LICENSE("GPL v2");