ir-spi.c 4.1 KB

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