extcon-adc-jack.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. 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. data->edev.name = pdata->name;
  92. if (!pdata->cable_names) {
  93. err = -EINVAL;
  94. dev_err(&pdev->dev, "error: cable_names not defined.\n");
  95. goto out;
  96. }
  97. data->edev.dev.parent = &pdev->dev;
  98. data->edev.supported_cable = pdata->cable_names;
  99. /* Check the length of array and set num_cables */
  100. for (i = 0; data->edev.supported_cable[i]; i++)
  101. ;
  102. if (i == 0 || i > SUPPORTED_CABLE_MAX) {
  103. err = -EINVAL;
  104. dev_err(&pdev->dev, "error: pdata->cable_names size = %d\n",
  105. i - 1);
  106. goto out;
  107. }
  108. data->num_cables = i;
  109. if (!pdata->adc_conditions ||
  110. !pdata->adc_conditions[0].state) {
  111. err = -EINVAL;
  112. dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
  113. goto out;
  114. }
  115. data->adc_conditions = pdata->adc_conditions;
  116. /* Check the length of array and set num_conditions */
  117. for (i = 0; data->adc_conditions[i].state; i++)
  118. ;
  119. data->num_conditions = i;
  120. data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel);
  121. if (IS_ERR(data->chan)) {
  122. err = PTR_ERR(data->chan);
  123. goto out;
  124. }
  125. data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
  126. INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
  127. platform_set_drvdata(pdev, data);
  128. err = extcon_dev_register(&data->edev);
  129. if (err)
  130. goto out;
  131. data->irq = platform_get_irq(pdev, 0);
  132. if (!data->irq) {
  133. dev_err(&pdev->dev, "platform_get_irq failed\n");
  134. err = -ENODEV;
  135. goto err_irq;
  136. }
  137. err = request_any_context_irq(data->irq, adc_jack_irq_thread,
  138. pdata->irq_flags, pdata->name, data);
  139. if (err < 0) {
  140. dev_err(&pdev->dev, "error: irq %d\n", data->irq);
  141. goto err_irq;
  142. }
  143. return 0;
  144. err_irq:
  145. extcon_dev_unregister(&data->edev);
  146. out:
  147. return err;
  148. }
  149. static int adc_jack_remove(struct platform_device *pdev)
  150. {
  151. struct adc_jack_data *data = platform_get_drvdata(pdev);
  152. free_irq(data->irq, data);
  153. cancel_work_sync(&data->handler.work);
  154. extcon_dev_unregister(&data->edev);
  155. return 0;
  156. }
  157. static struct platform_driver adc_jack_driver = {
  158. .probe = adc_jack_probe,
  159. .remove = adc_jack_remove,
  160. .driver = {
  161. .name = "adc-jack",
  162. .owner = THIS_MODULE,
  163. },
  164. };
  165. module_platform_driver(adc_jack_driver);
  166. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  167. MODULE_DESCRIPTION("ADC Jack extcon driver");
  168. MODULE_LICENSE("GPL v2");