drv_pwm.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Copyright(c) 2020, Du Huanpeng<548708880@qq.com>
  7. *
  8. */
  9. #include <rtthread.h>
  10. #include <rtdevice.h>
  11. #include <ls2k1000.h>
  12. #ifdef RT_USING_PWM
  13. #define PWM0_BASE (0xFFFFFFFFBFe02000)
  14. #define PWM1_BASE (0xFFFFFFFFBFe02010)
  15. #define PWM2_BASE (0xFFFFFFFFBFe02020)
  16. #define PWM3_BASE (0xFFFFFFFFBFe02030)
  17. #define CTRL_EN (1UL<<0)
  18. #define CTRL_OE (1UL<<3)
  19. #define CTRL_SINGL (1UL<<4)
  20. #define CTRL_INTE (1UL<<5)
  21. #define CTRL_INT (1UL<<6)
  22. #define CTRL_RST (1UL<<7)
  23. #define CTRL_CAPTE (1UL<<8)
  24. #define CTRL_INVERT (1UL<<9)
  25. #define CTRL_DZONE (1UL<<10)
  26. struct loongson_pwm {
  27. rt_uint32_t __PAD0;
  28. rt_uint32_t low_buffer;
  29. rt_uint32_t full_buffer;
  30. rt_uint32_t ctrl;
  31. };
  32. rt_err_t loongson_pwm_enable(struct rt_device_pwm *device, int channel)
  33. {
  34. int **priv;
  35. struct loongson_pwm *chip;
  36. volatile rt_uint64_t *config0;
  37. rt_uint64_t m;
  38. channel %= 4;
  39. config0 = (void *)GEN_CONFIG0_REG;
  40. m = 1ULL << 12 << channel;
  41. *config0 |= m;
  42. priv = device->parent.user_data;
  43. chip = (void *)priv[channel];
  44. chip->ctrl = CTRL_EN;
  45. return RT_EOK;
  46. }
  47. rt_err_t loongson_pwm_disable(struct rt_device_pwm *device, int channel)
  48. {
  49. struct loongson_pwm **chip;
  50. rt_uint64_t m;
  51. chip = device->parent.user_data;
  52. channel %= 4;
  53. chip[channel]->ctrl &= ~CTRL_EN;
  54. return RT_EOK;
  55. }
  56. rt_err_t loongson_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
  57. {
  58. struct loongson_pwm *chip;
  59. rt_uint32_t **priv;
  60. priv = device->parent.user_data;
  61. channel %= 4;
  62. chip = (void *)priv[channel];
  63. chip->ctrl &= ~CTRL_EN;
  64. chip->full_buffer = period;
  65. chip->low_buffer = pulse;
  66. chip->ctrl |= CTRL_EN;
  67. return RT_EOK;
  68. }
  69. static rt_err_t loongson_pwm_ioctl(struct rt_device_pwm *device, int cmd, void *arg)
  70. {
  71. rt_err_t rc;
  72. struct rt_pwm_configuration *cfg;
  73. cfg = (void *)arg;
  74. switch (cmd) {
  75. case PWM_CMD_ENABLE:
  76. rc = loongson_pwm_enable(device, cfg->channel);
  77. break;
  78. case PWM_CMD_DISABLE:
  79. rc = loongson_pwm_disable(device, cfg->channel);
  80. break;
  81. case PWM_CMD_SET:
  82. rc = loongson_pwm_set(device, cfg->channel, cfg->period, cfg->pulse);
  83. break;
  84. case PWM_CMD_GET:
  85. rc = RT_ENOSYS;
  86. break;
  87. default:
  88. rc = RT_EINVAL;
  89. break;
  90. }
  91. return rc;
  92. }
  93. struct rt_pwm_ops loongson_pwm_ops = {
  94. .control = loongson_pwm_ioctl,
  95. };
  96. struct rt_device_pwm loongson_pwm = {
  97. .ops = &loongson_pwm_ops,
  98. };
  99. int loongson_pwm_init(void)
  100. {
  101. int rc = RT_EOK;
  102. static rt_uint32_t *priv[] = {
  103. (void *)PWM0_BASE,
  104. (void *)PWM1_BASE,
  105. (void *)PWM2_BASE,
  106. (void *)PWM3_BASE
  107. };
  108. rc = rt_device_pwm_register(&loongson_pwm, "pwm0", &loongson_pwm_ops, &priv);
  109. return rc;
  110. }
  111. INIT_DEVICE_EXPORT(loongson_pwm_init);
  112. #endif /*RT_USING_PWM*/