ir-spi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: GPL-2.0
  2. // SPI driven IR LED device driver
  3. //
  4. // Copyright (c) 2016 Samsung Electronics Co., Ltd.
  5. // Copyright (c) Andi Shyti <andi@etezian.org>
  6. #include <linux/delay.h>
  7. #include <linux/fs.h>
  8. #include <linux/module.h>
  9. #include <linux/mutex.h>
  10. #include <linux/of_gpio.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <linux/spi/spi.h>
  13. #include <media/rc-core.h>
  14. #define IR_SPI_DRIVER_NAME "ir-spi"
  15. #define IR_SPI_DEFAULT_FREQUENCY 38000
  16. #define IR_SPI_MAX_BUFSIZE 4096
  17. struct ir_spi_data {
  18. u32 freq;
  19. bool negated;
  20. u16 tx_buf[IR_SPI_MAX_BUFSIZE];
  21. u16 pulse;
  22. u16 space;
  23. struct rc_dev *rc;
  24. struct spi_device *spi;
  25. struct regulator *regulator;
  26. };
  27. static int ir_spi_tx(struct rc_dev *dev,
  28. unsigned int *buffer, unsigned int count)
  29. {
  30. int i;
  31. int ret;
  32. unsigned int len = 0;
  33. struct ir_spi_data *idata = dev->priv;
  34. struct spi_transfer xfer;
  35. /* convert the pulse/space signal to raw binary signal */
  36. for (i = 0; i < count; i++) {
  37. unsigned int periods;
  38. int j;
  39. u16 val;
  40. periods = DIV_ROUND_CLOSEST(buffer[i] * idata->freq, 1000000);
  41. if (len + periods >= IR_SPI_MAX_BUFSIZE)
  42. return -EINVAL;
  43. /*
  44. * the first value in buffer is a pulse, so that 0, 2, 4, ...
  45. * contain a pulse duration. On the contrary, 1, 3, 5, ...
  46. * contain a space duration.
  47. */
  48. val = (i % 2) ? idata->space : idata->pulse;
  49. for (j = 0; j < periods; j++)
  50. idata->tx_buf[len++] = val;
  51. }
  52. memset(&xfer, 0, sizeof(xfer));
  53. xfer.speed_hz = idata->freq * 16;
  54. xfer.len = len * sizeof(*idata->tx_buf);
  55. xfer.tx_buf = idata->tx_buf;
  56. ret = regulator_enable(idata->regulator);
  57. if (ret)
  58. return ret;
  59. ret = spi_sync_transfer(idata->spi, &xfer, 1);
  60. if (ret)
  61. dev_err(&idata->spi->dev, "unable to deliver the signal\n");
  62. regulator_disable(idata->regulator);
  63. return ret ? ret : count;
  64. }
  65. static int ir_spi_set_tx_carrier(struct rc_dev *dev, u32 carrier)
  66. {
  67. struct ir_spi_data *idata = dev->priv;
  68. if (!carrier)
  69. return -EINVAL;
  70. idata->freq = carrier;
  71. return 0;
  72. }
  73. static int ir_spi_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
  74. {
  75. struct ir_spi_data *idata = dev->priv;
  76. int bits = (duty_cycle * 15) / 100;
  77. idata->pulse = GENMASK(bits, 0);
  78. if (idata->negated) {
  79. idata->pulse = ~idata->pulse;
  80. idata->space = 0xffff;
  81. } else {
  82. idata->space = 0;
  83. }
  84. return 0;
  85. }
  86. static int ir_spi_probe(struct spi_device *spi)
  87. {
  88. int ret;
  89. u8 dc;
  90. struct ir_spi_data *idata;
  91. idata = devm_kzalloc(&spi->dev, sizeof(*idata), GFP_KERNEL);
  92. if (!idata)
  93. return -ENOMEM;
  94. idata->regulator = devm_regulator_get(&spi->dev, "irda_regulator");
  95. if (IS_ERR(idata->regulator))
  96. return PTR_ERR(idata->regulator);
  97. idata->rc = devm_rc_allocate_device(&spi->dev, RC_DRIVER_IR_RAW_TX);
  98. if (!idata->rc)
  99. return -ENOMEM;
  100. idata->rc->tx_ir = ir_spi_tx;
  101. idata->rc->s_tx_carrier = ir_spi_set_tx_carrier;
  102. idata->rc->s_tx_duty_cycle = ir_spi_set_duty_cycle;
  103. idata->rc->device_name = "IR SPI";
  104. idata->rc->driver_name = IR_SPI_DRIVER_NAME;
  105. idata->rc->priv = idata;
  106. idata->spi = spi;
  107. idata->negated = of_property_read_bool(spi->dev.of_node,
  108. "led-active-low");
  109. ret = of_property_read_u8(spi->dev.of_node, "duty-cycle", &dc);
  110. if (ret)
  111. dc = 50;
  112. /* ir_spi_set_duty_cycle cannot fail,
  113. * it returns int to be compatible with the
  114. * rc->s_tx_duty_cycle function
  115. */
  116. ir_spi_set_duty_cycle(idata->rc, dc);
  117. idata->freq = IR_SPI_DEFAULT_FREQUENCY;
  118. return devm_rc_register_device(&spi->dev, idata->rc);
  119. }
  120. static int ir_spi_remove(struct spi_device *spi)
  121. {
  122. return 0;
  123. }
  124. static const struct of_device_id ir_spi_of_match[] = {
  125. { .compatible = "ir-spi-led" },
  126. {},
  127. };
  128. MODULE_DEVICE_TABLE(of, ir_spi_of_match);
  129. static struct spi_driver ir_spi_driver = {
  130. .probe = ir_spi_probe,
  131. .remove = ir_spi_remove,
  132. .driver = {
  133. .name = IR_SPI_DRIVER_NAME,
  134. .of_match_table = ir_spi_of_match,
  135. },
  136. };
  137. module_spi_driver(ir_spi_driver);
  138. MODULE_AUTHOR("Andi Shyti <andi@etezian.org>");
  139. MODULE_DESCRIPTION("SPI IR LED");
  140. MODULE_LICENSE("GPL v2");