pwm-ir-tx.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/pwm.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 "pwm-ir-tx"
  22. #define DEVICE_NAME "PWM IR Transmitter"
  23. struct pwm_ir {
  24. struct pwm_device *pwm;
  25. unsigned int carrier;
  26. unsigned int duty_cycle;
  27. };
  28. static const struct of_device_id pwm_ir_of_match[] = {
  29. { .compatible = "pwm-ir-tx", },
  30. { },
  31. };
  32. MODULE_DEVICE_TABLE(of, pwm_ir_of_match);
  33. static int pwm_ir_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
  34. {
  35. struct pwm_ir *pwm_ir = dev->priv;
  36. pwm_ir->duty_cycle = duty_cycle;
  37. return 0;
  38. }
  39. static int pwm_ir_set_carrier(struct rc_dev *dev, u32 carrier)
  40. {
  41. struct pwm_ir *pwm_ir = dev->priv;
  42. if (!carrier)
  43. return -EINVAL;
  44. pwm_ir->carrier = carrier;
  45. return 0;
  46. }
  47. static int pwm_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
  48. unsigned int count)
  49. {
  50. struct pwm_ir *pwm_ir = dev->priv;
  51. struct pwm_device *pwm = pwm_ir->pwm;
  52. int i, duty, period;
  53. ktime_t edge;
  54. long delta;
  55. period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, pwm_ir->carrier);
  56. duty = DIV_ROUND_CLOSEST(pwm_ir->duty_cycle * period, 100);
  57. pwm_config(pwm, duty, period);
  58. edge = ktime_get();
  59. for (i = 0; i < count; i++) {
  60. if (i % 2) // space
  61. pwm_disable(pwm);
  62. else
  63. pwm_enable(pwm);
  64. edge = ktime_add_us(edge, txbuf[i]);
  65. delta = ktime_us_delta(edge, ktime_get());
  66. if (delta > 0)
  67. usleep_range(delta, delta + 10);
  68. }
  69. pwm_disable(pwm);
  70. return count;
  71. }
  72. static int pwm_ir_probe(struct platform_device *pdev)
  73. {
  74. struct pwm_ir *pwm_ir;
  75. struct rc_dev *rcdev;
  76. int rc;
  77. pwm_ir = devm_kmalloc(&pdev->dev, sizeof(*pwm_ir), GFP_KERNEL);
  78. if (!pwm_ir)
  79. return -ENOMEM;
  80. pwm_ir->pwm = devm_pwm_get(&pdev->dev, NULL);
  81. if (IS_ERR(pwm_ir->pwm))
  82. return PTR_ERR(pwm_ir->pwm);
  83. pwm_ir->carrier = 38000;
  84. pwm_ir->duty_cycle = 50;
  85. rcdev = devm_rc_allocate_device(&pdev->dev, RC_DRIVER_IR_RAW_TX);
  86. if (!rcdev)
  87. return -ENOMEM;
  88. rcdev->priv = pwm_ir;
  89. rcdev->driver_name = DRIVER_NAME;
  90. rcdev->device_name = DEVICE_NAME;
  91. rcdev->tx_ir = pwm_ir_tx;
  92. rcdev->s_tx_duty_cycle = pwm_ir_set_duty_cycle;
  93. rcdev->s_tx_carrier = pwm_ir_set_carrier;
  94. rc = devm_rc_register_device(&pdev->dev, rcdev);
  95. if (rc < 0)
  96. dev_err(&pdev->dev, "failed to register rc device\n");
  97. return rc;
  98. }
  99. static struct platform_driver pwm_ir_driver = {
  100. .probe = pwm_ir_probe,
  101. .driver = {
  102. .name = DRIVER_NAME,
  103. .of_match_table = of_match_ptr(pwm_ir_of_match),
  104. },
  105. };
  106. module_platform_driver(pwm_ir_driver);
  107. MODULE_DESCRIPTION("PWM IR Transmitter");
  108. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  109. MODULE_LICENSE("GPL");