ir-spi.c 4.6 KB

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