extcon-adc-jack.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * @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 device *dev;
  40. struct extcon_dev *edev;
  41. const unsigned int **cable_names;
  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. bool wakeup_source;
  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. 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. !pdata->adc_conditions[0].state) {
  103. dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
  104. return -EINVAL;
  105. }
  106. data->adc_conditions = pdata->adc_conditions;
  107. /* Check the length of array and set num_conditions */
  108. for (i = 0; data->adc_conditions[i].state; i++)
  109. ;
  110. data->num_conditions = i;
  111. data->chan = 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) {
  123. dev_err(&pdev->dev, "platform_get_irq failed\n");
  124. return -ENODEV;
  125. }
  126. err = request_any_context_irq(data->irq, adc_jack_irq_thread,
  127. pdata->irq_flags, pdata->name, data);
  128. if (err < 0) {
  129. dev_err(&pdev->dev, "error: irq %d\n", data->irq);
  130. return err;
  131. }
  132. if (data->wakeup_source)
  133. device_init_wakeup(&pdev->dev, 1);
  134. return 0;
  135. }
  136. static int adc_jack_remove(struct platform_device *pdev)
  137. {
  138. struct adc_jack_data *data = platform_get_drvdata(pdev);
  139. free_irq(data->irq, data);
  140. cancel_work_sync(&data->handler.work);
  141. iio_channel_release(data->chan);
  142. return 0;
  143. }
  144. #ifdef CONFIG_PM_SLEEP
  145. static int adc_jack_suspend(struct device *dev)
  146. {
  147. struct adc_jack_data *data = dev_get_drvdata(dev);
  148. cancel_delayed_work_sync(&data->handler);
  149. if (device_may_wakeup(data->dev))
  150. enable_irq_wake(data->irq);
  151. return 0;
  152. }
  153. static int adc_jack_resume(struct device *dev)
  154. {
  155. struct adc_jack_data *data = dev_get_drvdata(dev);
  156. if (device_may_wakeup(data->dev))
  157. disable_irq_wake(data->irq);
  158. return 0;
  159. }
  160. #endif /* CONFIG_PM_SLEEP */
  161. static SIMPLE_DEV_PM_OPS(adc_jack_pm_ops,
  162. adc_jack_suspend, adc_jack_resume);
  163. static struct platform_driver adc_jack_driver = {
  164. .probe = adc_jack_probe,
  165. .remove = adc_jack_remove,
  166. .driver = {
  167. .name = "adc-jack",
  168. .pm = &adc_jack_pm_ops,
  169. },
  170. };
  171. module_platform_driver(adc_jack_driver);
  172. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  173. MODULE_DESCRIPTION("ADC Jack extcon driver");
  174. MODULE_LICENSE("GPL v2");