extcon-adc-jack.c 5.5 KB

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