gpio-ir-tx.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2017 Sean Young <sean@mess.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/delay.h>
  17. #include <linux/slab.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <media/rc-core.h>
  21. #define DRIVER_NAME "gpio-ir-tx"
  22. #define DEVICE_NAME "GPIO IR Bit Banging Transmitter"
  23. struct gpio_ir {
  24. struct gpio_desc *gpio;
  25. unsigned int carrier;
  26. unsigned int duty_cycle;
  27. /* we need a spinlock to hold the cpu while transmitting */
  28. spinlock_t lock;
  29. };
  30. static const struct of_device_id gpio_ir_tx_of_match[] = {
  31. { .compatible = "gpio-ir-tx", },
  32. { },
  33. };
  34. MODULE_DEVICE_TABLE(of, gpio_ir_tx_of_match);
  35. static int gpio_ir_tx_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
  36. {
  37. struct gpio_ir *gpio_ir = dev->priv;
  38. gpio_ir->duty_cycle = duty_cycle;
  39. return 0;
  40. }
  41. static int gpio_ir_tx_set_carrier(struct rc_dev *dev, u32 carrier)
  42. {
  43. struct gpio_ir *gpio_ir = dev->priv;
  44. if (!carrier)
  45. return -EINVAL;
  46. gpio_ir->carrier = carrier;
  47. return 0;
  48. }
  49. static int gpio_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
  50. unsigned int count)
  51. {
  52. struct gpio_ir *gpio_ir = dev->priv;
  53. unsigned long flags;
  54. ktime_t edge;
  55. /*
  56. * delta should never exceed 0.5 seconds (IR_MAX_DURATION) and on
  57. * m68k ndelay(s64) does not compile; so use s32 rather than s64.
  58. */
  59. s32 delta;
  60. int i;
  61. unsigned int pulse, space;
  62. /* Ensure the dividend fits into 32 bit */
  63. pulse = DIV_ROUND_CLOSEST(gpio_ir->duty_cycle * (NSEC_PER_SEC / 100),
  64. gpio_ir->carrier);
  65. space = DIV_ROUND_CLOSEST((100 - gpio_ir->duty_cycle) *
  66. (NSEC_PER_SEC / 100), gpio_ir->carrier);
  67. spin_lock_irqsave(&gpio_ir->lock, flags);
  68. edge = ktime_get();
  69. for (i = 0; i < count; i++) {
  70. if (i % 2) {
  71. // space
  72. edge = ktime_add_us(edge, txbuf[i]);
  73. delta = ktime_us_delta(edge, ktime_get());
  74. if (delta > 10) {
  75. spin_unlock_irqrestore(&gpio_ir->lock, flags);
  76. usleep_range(delta, delta + 10);
  77. spin_lock_irqsave(&gpio_ir->lock, flags);
  78. } else if (delta > 0) {
  79. udelay(delta);
  80. }
  81. } else {
  82. // pulse
  83. ktime_t last = ktime_add_us(edge, txbuf[i]);
  84. while (ktime_before(ktime_get(), last)) {
  85. gpiod_set_value(gpio_ir->gpio, 1);
  86. edge = ktime_add_ns(edge, pulse);
  87. delta = ktime_to_ns(ktime_sub(edge,
  88. ktime_get()));
  89. if (delta > 0)
  90. ndelay(delta);
  91. gpiod_set_value(gpio_ir->gpio, 0);
  92. edge = ktime_add_ns(edge, space);
  93. delta = ktime_to_ns(ktime_sub(edge,
  94. ktime_get()));
  95. if (delta > 0)
  96. ndelay(delta);
  97. }
  98. edge = last;
  99. }
  100. }
  101. spin_unlock_irqrestore(&gpio_ir->lock, flags);
  102. return count;
  103. }
  104. static int gpio_ir_tx_probe(struct platform_device *pdev)
  105. {
  106. struct gpio_ir *gpio_ir;
  107. struct rc_dev *rcdev;
  108. int rc;
  109. gpio_ir = devm_kmalloc(&pdev->dev, sizeof(*gpio_ir), GFP_KERNEL);
  110. if (!gpio_ir)
  111. return -ENOMEM;
  112. rcdev = devm_rc_allocate_device(&pdev->dev, RC_DRIVER_IR_RAW_TX);
  113. if (!rcdev)
  114. return -ENOMEM;
  115. gpio_ir->gpio = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);
  116. if (IS_ERR(gpio_ir->gpio)) {
  117. if (PTR_ERR(gpio_ir->gpio) != -EPROBE_DEFER)
  118. dev_err(&pdev->dev, "Failed to get gpio (%ld)\n",
  119. PTR_ERR(gpio_ir->gpio));
  120. return PTR_ERR(gpio_ir->gpio);
  121. }
  122. rcdev->priv = gpio_ir;
  123. rcdev->driver_name = DRIVER_NAME;
  124. rcdev->device_name = DEVICE_NAME;
  125. rcdev->tx_ir = gpio_ir_tx;
  126. rcdev->s_tx_duty_cycle = gpio_ir_tx_set_duty_cycle;
  127. rcdev->s_tx_carrier = gpio_ir_tx_set_carrier;
  128. gpio_ir->carrier = 38000;
  129. gpio_ir->duty_cycle = 50;
  130. spin_lock_init(&gpio_ir->lock);
  131. rc = devm_rc_register_device(&pdev->dev, rcdev);
  132. if (rc < 0)
  133. dev_err(&pdev->dev, "failed to register rc device\n");
  134. return rc;
  135. }
  136. static struct platform_driver gpio_ir_tx_driver = {
  137. .probe = gpio_ir_tx_probe,
  138. .driver = {
  139. .name = DRIVER_NAME,
  140. .of_match_table = of_match_ptr(gpio_ir_tx_of_match),
  141. },
  142. };
  143. module_platform_driver(gpio_ir_tx_driver);
  144. MODULE_DESCRIPTION("GPIO IR Bit Banging Transmitter");
  145. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  146. MODULE_LICENSE("GPL");