extcon-adc-jack.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 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. };
  48. static void adc_jack_handler(struct work_struct *work)
  49. {
  50. struct adc_jack_data *data = container_of(to_delayed_work(work),
  51. struct adc_jack_data,
  52. handler);
  53. u32 state = 0;
  54. int ret, adc_val;
  55. int i;
  56. ret = iio_read_channel_raw(data->chan, &adc_val);
  57. if (ret < 0) {
  58. dev_err(&data->edev->dev, "read channel() error: %d\n", ret);
  59. return;
  60. }
  61. /* Get state from adc value with adc_conditions */
  62. for (i = 0; i < data->num_conditions; i++) {
  63. struct adc_jack_cond *def = &data->adc_conditions[i];
  64. if (!def->state)
  65. break;
  66. if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
  67. state = def->state;
  68. break;
  69. }
  70. }
  71. /* if no def has met, it means state = 0 (no cables attached) */
  72. extcon_set_state(data->edev, state);
  73. }
  74. static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
  75. {
  76. struct adc_jack_data *data = _data;
  77. queue_delayed_work(system_power_efficient_wq,
  78. &data->handler, data->handling_delay);
  79. return IRQ_HANDLED;
  80. }
  81. static int adc_jack_probe(struct platform_device *pdev)
  82. {
  83. struct adc_jack_data *data;
  84. struct adc_jack_pdata *pdata = dev_get_platdata(&pdev->dev);
  85. int i, err = 0;
  86. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  87. if (!data)
  88. return -ENOMEM;
  89. if (!pdata->cable_names) {
  90. dev_err(&pdev->dev, "error: cable_names not defined.\n");
  91. return -EINVAL;
  92. }
  93. data->edev = devm_extcon_dev_allocate(&pdev->dev, pdata->cable_names);
  94. if (IS_ERR(data->edev)) {
  95. dev_err(&pdev->dev, "failed to allocate extcon device\n");
  96. return -ENOMEM;
  97. }
  98. if (!pdata->adc_conditions ||
  99. !pdata->adc_conditions[0].state) {
  100. dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
  101. return -EINVAL;
  102. }
  103. data->adc_conditions = pdata->adc_conditions;
  104. /* Check the length of array and set num_conditions */
  105. for (i = 0; data->adc_conditions[i].state; i++)
  106. ;
  107. data->num_conditions = i;
  108. data->chan = iio_channel_get(&pdev->dev, pdata->consumer_channel);
  109. if (IS_ERR(data->chan))
  110. return PTR_ERR(data->chan);
  111. data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
  112. INIT_DEFERRABLE_WORK(&data->handler, adc_jack_handler);
  113. platform_set_drvdata(pdev, data);
  114. err = devm_extcon_dev_register(&pdev->dev, data->edev);
  115. if (err)
  116. return err;
  117. data->irq = platform_get_irq(pdev, 0);
  118. if (!data->irq) {
  119. dev_err(&pdev->dev, "platform_get_irq failed\n");
  120. return -ENODEV;
  121. }
  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. return 0;
  129. }
  130. static int adc_jack_remove(struct platform_device *pdev)
  131. {
  132. struct adc_jack_data *data = platform_get_drvdata(pdev);
  133. free_irq(data->irq, data);
  134. cancel_work_sync(&data->handler.work);
  135. iio_channel_release(data->chan);
  136. return 0;
  137. }
  138. static struct platform_driver adc_jack_driver = {
  139. .probe = adc_jack_probe,
  140. .remove = adc_jack_remove,
  141. .driver = {
  142. .name = "adc-jack",
  143. },
  144. };
  145. module_platform_driver(adc_jack_driver);
  146. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  147. MODULE_DESCRIPTION("ADC Jack extcon driver");
  148. MODULE_LICENSE("GPL v2");