drv_pwm.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. {
  28. rt_uint32_t __PAD0;
  29. rt_uint32_t low_buffer;
  30. rt_uint32_t full_buffer;
  31. rt_uint32_t ctrl;
  32. };
  33. rt_err_t loongson_pwm_enable(struct rt_device_pwm *device, int channel)
  34. {
  35. int **priv;
  36. struct loongson_pwm *chip;
  37. volatile rt_uint64_t *config0;
  38. rt_uint64_t m;
  39. channel %= 4;
  40. config0 = (void *)GEN_CONFIG0_REG;
  41. m = 1ULL << 12 << channel;
  42. *config0 |= m;
  43. priv = device->parent.user_data;
  44. chip = (void *)priv[channel];
  45. chip->ctrl = CTRL_EN;
  46. return RT_EOK;
  47. }
  48. rt_err_t loongson_pwm_disable(struct rt_device_pwm *device, int channel)
  49. {
  50. struct loongson_pwm **chip;
  51. rt_uint64_t m;
  52. chip = device->parent.user_data;
  53. channel %= 4;
  54. chip[channel]->ctrl &= ~CTRL_EN;
  55. return RT_EOK;
  56. }
  57. rt_err_t loongson_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
  58. {
  59. struct loongson_pwm *chip;
  60. rt_uint32_t **priv;
  61. priv = device->parent.user_data;
  62. channel %= 4;
  63. chip = (void *)priv[channel];
  64. chip->ctrl &= ~CTRL_EN;
  65. chip->full_buffer = period;
  66. chip->low_buffer = pulse;
  67. chip->ctrl |= CTRL_EN;
  68. return RT_EOK;
  69. }
  70. static rt_err_t loongson_pwm_ioctl(struct rt_device_pwm *device, int cmd, void *arg)
  71. {
  72. rt_err_t rc;
  73. struct rt_pwm_configuration *cfg;
  74. cfg = (void *)arg;
  75. switch (cmd)
  76. {
  77. case PWM_CMD_ENABLE:
  78. rc = loongson_pwm_enable(device, cfg->channel);
  79. break;
  80. case PWM_CMD_DISABLE:
  81. rc = loongson_pwm_disable(device, cfg->channel);
  82. break;
  83. case PWM_CMD_SET:
  84. rc = loongson_pwm_set(device, cfg->channel, cfg->period, cfg->pulse);
  85. break;
  86. case PWM_CMD_GET:
  87. rc = -RT_ENOSYS;
  88. break;
  89. default:
  90. rc = -RT_EINVAL;
  91. break;
  92. }
  93. return rc;
  94. }
  95. struct rt_pwm_ops loongson_pwm_ops =
  96. {
  97. .control = loongson_pwm_ioctl,
  98. };
  99. struct rt_device_pwm loongson_pwm =
  100. {
  101. .ops = &loongson_pwm_ops,
  102. };
  103. int loongson_pwm_init(void)
  104. {
  105. int rc = RT_EOK;
  106. static rt_uint32_t *priv[] =
  107. {
  108. (void *)PWM0_BASE,
  109. (void *)PWM1_BASE,
  110. (void *)PWM2_BASE,
  111. (void *)PWM3_BASE
  112. };
  113. rc = rt_device_pwm_register(&loongson_pwm, "pwm0", &loongson_pwm_ops, &priv);
  114. return rc;
  115. }
  116. INIT_DEVICE_EXPORT(loongson_pwm_init);
  117. #endif /*RT_USING_PWM*/