extcon-adc-jack.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. * @dev: The device structure associated with the adc_jack.
  28. * @edev: extcon device.
  29. * @cable_names: list of supported cables.
  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. * @wakeup_source: Indicates if the device can wake up the system.
  38. */
  39. struct adc_jack_data {
  40. struct device *dev;
  41. struct extcon_dev *edev;
  42. const unsigned int **cable_names;
  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. bool wakeup_source;
  50. };
  51. static void adc_jack_handler(struct work_struct *work)
  52. {
  53. struct adc_jack_data *data = container_of(to_delayed_work(work),
  54. struct adc_jack_data,
  55. handler);
  56. struct adc_jack_cond *def;
  57. int ret, adc_val;
  58. int i;
  59. ret = iio_read_channel_raw(data->chan, &adc_val);
  60. if (ret < 0) {
  61. dev_err(data->dev, "read channel() error: %d\n", ret);
  62. return;
  63. }
  64. /* Get state from adc value with adc_conditions */
  65. for (i = 0; i < data->num_conditions; i++) {
  66. def = &data->adc_conditions[i];
  67. if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
  68. extcon_set_state_sync(data->edev, def->id, true);
  69. return;
  70. }
  71. }
  72. /* Set the detached state if adc value is not included in the range */
  73. for (i = 0; i < data->num_conditions; i++) {
  74. def = &data->adc_conditions[i];
  75. extcon_set_state_sync(data->edev, def->id, false);
  76. }
  77. }
  78. static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
  79. {
  80. struct adc_jack_data *data = _data;
  81. queue_delayed_work(system_power_efficient_wq,
  82. &data->handler, data->handling_delay);
  83. return IRQ_HANDLED;
  84. }
  85. static int adc_jack_probe(struct platform_device *pdev)
  86. {
  87. struct adc_jack_data *data;
  88. struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
  89. int i, err = 0;
  90. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  91. if (!data)
  92. return -ENOMEM;
  93. if (!pdata->cable_names) {
  94. dev_err(&pdev->dev, "error: cable_names not defined.\n");
  95. return -EINVAL;
  96. }
  97. data->dev = &pdev->dev;
  98. data->edev = devm_extcon_dev_allocate(&pdev->dev, pdata->cable_names);
  99. if (IS_ERR(data->edev)) {
  100. dev_err(&pdev->dev, "failed to allocate extcon device\n");
  101. return -ENOMEM;
  102. }
  103. if (!pdata->adc_conditions) {
  104. dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
  105. return -EINVAL;
  106. }
  107. data->adc_conditions = pdata->adc_conditions;
  108. /* Check the length of array and set num_conditions */
  109. for (i = 0; data->adc_conditions[i].id != EXTCON_NONE; i++);
  110. data->num_conditions = i;
  111. data->chan = devm_iio_channel_get(&pdev->dev, pdata->consumer_channel);
  112. if (IS_ERR(data->chan))
  113. return PTR_ERR(data->chan);
  114. data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
  115. data->wakeup_source = pdata->wakeup_source;
  116. INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
  117. platform_set_drvdata(pdev, data);
  118. err = devm_extcon_dev_register(&pdev->dev, data->edev);
  119. if (err)
  120. return err;
  121. data->irq = platform_get_irq(pdev, 0);
  122. if (data->irq < 0)
  123. return -ENODEV;
  124. err = request_any_context_irq(data->irq, adc_jack_irq_thread,
  125. pdata->irq_flags, pdata->name, data);
  126. if (err < 0) {
  127. dev_err(&pdev->dev, "error: irq %d\n", data->irq);
  128. return err;
  129. }
  130. if (data->wakeup_source)
  131. device_init_wakeup(&pdev->dev, 1);
  132. adc_jack_handler(&data->handler.work);
  133. return 0;
  134. }
  135. static void adc_jack_remove(struct platform_device *pdev)
  136. {
  137. struct adc_jack_data *data = platform_get_drvdata(pdev);
  138. if (data->wakeup_source)
  139. device_init_wakeup(&pdev->dev, false);
  140. free_irq(data->irq, data);
  141. cancel_work_sync(&data->handler.work);
  142. }
  143. #ifdef CONFIG_PM_SLEEP
  144. static int adc_jack_suspend(struct device *dev)
  145. {
  146. struct adc_jack_data *data = dev_get_drvdata(dev);
  147. cancel_delayed_work_sync(&data->handler);
  148. if (device_may_wakeup(data->dev))
  149. enable_irq_wake(data->irq);
  150. return 0;
  151. }
  152. static int adc_jack_resume(struct device *dev)
  153. {
  154. struct adc_jack_data *data = dev_get_drvdata(dev);
  155. if (device_may_wakeup(data->dev))
  156. disable_irq_wake(data->irq);
  157. return 0;
  158. }
  159. #endif /* CONFIG_PM_SLEEP */
  160. static SIMPLE_DEV_PM_OPS(adc_jack_pm_ops,
  161. adc_jack_suspend, adc_jack_resume);
  162. static struct platform_driver adc_jack_driver = {
  163. .probe = adc_jack_probe,
  164. .remove = adc_jack_remove,
  165. .driver = {
  166. .name = "adc-jack",
  167. .pm = &adc_jack_pm_ops,
  168. },
  169. };
  170. module_platform_driver(adc_jack_driver);
  171. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  172. MODULE_DESCRIPTION("ADC Jack extcon driver");
  173. MODULE_LICENSE("GPL v2");