drv_pwm.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * 2. Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. *
  11. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  12. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  13. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  16. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. * Copyright (c) 2006-2025 RT-Thread Development Team
  27. *
  28. * SPDX-License-Identifier: Apache-2.0
  29. */
  30. #include <rtthread.h>
  31. #include <rtdevice.h>
  32. #include "riscv_io.h"
  33. #include "board.h"
  34. #include "ioremap.h"
  35. #include <rtdbg.h>
  36. #include <stdbool.h>
  37. #include "sysctl_clk.h"
  38. #include "drv_pwm.h"
  39. #include <sys/ioctl.h>
  40. /**
  41. *
  42. * pwm0
  43. * ├── channel 0
  44. * ├── channel 1
  45. * └── channel 2
  46. * pwm1
  47. * ├── channel 0
  48. * ├── channel 1
  49. * └── channel 2
  50. *
  51. * Note:
  52. * The K230 PWM controller has 4 hardware channels:
  53. * - Channel 0 (pwmcmp0) is used to set the period and does not generate output.
  54. * - Channels 1 to 3 (pwmcmp1~3) are used to control the duty cycle and produce output signals.
  55. * Therefore, the driver maps these output channels (1~3) as logical channels 0~2.
  56. */
  57. #define PWM_REG_OFFSET 0x40
  58. #define PWM_CFG_BIT_INVERT (1 << 12)
  59. #define PWM_CFG_DEGLITCH (1 << 9)
  60. #define PWM_MAX_SCALE 0xF
  61. #define PWM_CMP_WIDTH 16
  62. #define PWM_PERIOD_BITS 16
  63. #define PWM_SCALE_MAX_BITS 15
  64. #define PWM_DEV_NUM 2
  65. #define PWM_MAX_CHANNELS 3
  66. #define PWM0_BASE_ADDR PWM_BASE_ADDR
  67. #define PWM1_BASE_ADDR PWM_BASE_ADDR + PWM_REG_OFFSET
  68. struct k230_pwm_dev
  69. {
  70. struct rt_device_pwm device;
  71. const char *name;
  72. rt_ubase_t base;
  73. };
  74. static struct k230_pwm_dev pwm_devs[] = {
  75. #ifdef BSP_USING_PWM0
  76. {
  77. .name = "pwm0",
  78. .base = PWM0_BASE_ADDR,
  79. },
  80. #endif
  81. #ifdef BSP_USING_PWM1
  82. {
  83. .name = "pwm1",
  84. .base = PWM1_BASE_ADDR,
  85. },
  86. #endif
  87. #if !defined(BSP_USING_PWM0) && !defined(BSP_USING_PWM1)
  88. #error "No pwm device defined!"
  89. #endif
  90. };
  91. static int check_channel(int channel)
  92. {
  93. if (channel < 0 || channel >= PWM_MAX_CHANNELS)
  94. {
  95. LOG_E("channel %d is not valid\n", channel);
  96. return -RT_ERROR;
  97. }
  98. return channel;
  99. }
  100. static rt_err_t pwm_start(kd_pwm_t *reg, int channel)
  101. {
  102. rt_err_t ret;
  103. ret = (rt_err_t)check_channel(channel);
  104. if (ret < 0)
  105. return ret;
  106. reg->pwmcfg |= PWM_CFG_BIT_INVERT; /* default always mode */
  107. return RT_EOK;
  108. }
  109. static rt_err_t pwm_stop(kd_pwm_t *reg, int channel)
  110. {
  111. rt_err_t ret;
  112. ret = (rt_err_t)check_channel(channel);
  113. if (ret < 0)
  114. return ret;
  115. reg->pwmcfg &= ~PWM_CFG_BIT_INVERT;
  116. return RT_EOK;
  117. }
  118. static rt_err_t kd_pwm_get(kd_pwm_t *reg, int channel, struct rt_pwm_configuration *configuration)
  119. {
  120. int ret;
  121. uint64_t pulse, period;
  122. uint32_t pwm_pclock, pwmscale;
  123. ret = check_channel(channel);
  124. if (ret < 0)
  125. return ret;
  126. pwm_pclock = sysctl_clk_get_leaf_freq(SYSCTL_CLK_PWM_APB_GATE);
  127. pwmscale = reg->pwmcfg & 0xf;
  128. pwm_pclock >>= pwmscale;
  129. period = reg->pwmcmp0;
  130. period = period * NSEC_PER_SEC / pwm_pclock;
  131. pulse = *((&reg->pwmcmp1) + channel);
  132. pulse = pulse * NSEC_PER_SEC / pwm_pclock;
  133. configuration->period = period;
  134. configuration->pulse = pulse;
  135. return RT_EOK;
  136. }
  137. static rt_err_t kd_pwm_set(kd_pwm_t *reg, int channel, struct rt_pwm_configuration *configuration)
  138. {
  139. int ret;
  140. uint64_t pulse, period, pwmcmpx_max;
  141. uint32_t pwm_pclock, pwmscale = 0;
  142. ret = check_channel(channel);
  143. if (ret < 0)
  144. return ret;
  145. pwm_pclock = sysctl_clk_get_leaf_freq(SYSCTL_CLK_PWM_APB_GATE);
  146. pulse = (uint64_t)configuration->pulse * pwm_pclock / NSEC_PER_SEC;
  147. period = (uint64_t)configuration->period * pwm_pclock / NSEC_PER_SEC;
  148. if (pulse > period)
  149. return -RT_EINVAL;
  150. /* Calculate duty cycle */
  151. pwmcmpx_max = (1 << PWM_CMP_WIDTH) - 1;
  152. if (period > ((1 << (PWM_SCALE_MAX_BITS + PWM_PERIOD_BITS)) - 1LL))
  153. return -RT_EINVAL;
  154. while ((period >> pwmscale) > pwmcmpx_max)
  155. pwmscale++;
  156. if (pwmscale > PWM_MAX_SCALE)
  157. return -RT_EINVAL;
  158. reg->pwmcfg |= PWM_CFG_DEGLITCH; /* default always mode */
  159. reg->pwmcfg &= (~PWM_MAX_SCALE);
  160. reg->pwmcfg |= pwmscale; /* scale */
  161. reg->pwmcmp0 = (period >> pwmscale);
  162. *((&reg->pwmcmp1) + channel) = reg->pwmcmp0 - (pulse >> pwmscale);
  163. return RT_EOK;
  164. }
  165. static rt_err_t kd_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
  166. {
  167. struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg;
  168. int channel = 0;
  169. int ret;
  170. struct k230_pwm_dev *pwm_dev = rt_container_of(device, struct k230_pwm_dev, device);
  171. kd_pwm_t *reg = (kd_pwm_t *)pwm_dev->base;
  172. channel = configuration->channel;
  173. switch (cmd)
  174. {
  175. case PWM_CMD_ENABLE:
  176. ret = pwm_start(reg, channel);
  177. break;
  178. case PWM_CMD_DISABLE:
  179. ret = pwm_stop(reg, channel);
  180. break;
  181. case PWM_CMD_SET:
  182. ret = kd_pwm_set(reg, channel, configuration);
  183. break;
  184. case PWM_CMD_GET:
  185. ret = kd_pwm_get(reg, channel, configuration);
  186. break;
  187. default:
  188. ret = -RT_EINVAL;
  189. }
  190. return ret;
  191. }
  192. static struct rt_pwm_ops drv_ops =
  193. {
  194. .control = kd_pwm_control
  195. };
  196. int rt_hw_pwm_init(void)
  197. {
  198. rt_err_t ret;
  199. for (int i = 0; i < sizeof(pwm_devs)/sizeof(struct k230_pwm_dev); i++)
  200. {
  201. struct k230_pwm_dev *dev = &pwm_devs[i];
  202. dev->base = (rt_ubase_t)rt_ioremap((void *)(dev->base), sizeof(kd_pwm_t));
  203. ret = rt_device_pwm_register(&dev->device, dev->name, &drv_ops, RT_NULL);
  204. if (ret != RT_EOK)
  205. {
  206. LOG_E("Failed to register PWM device %s, error code: %d\n", dev->name, ret);
  207. return ret;
  208. }
  209. }
  210. return RT_EOK;
  211. }
  212. INIT_DEVICE_EXPORT(rt_hw_pwm_init);