pwm_acts.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Copyright (c) 2018 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @brief PWM controller driver for Actions SoC
  8. */
  9. #include <errno.h>
  10. #include <sys/__assert.h>
  11. #include <stdbool.h>
  12. #include <kernel.h>
  13. #include <device.h>
  14. #include <init.h>
  15. #include <drivers/pwm.h>
  16. #include <soc.h>
  17. #include <drivers/dma.h>
  18. #include <errno.h>
  19. #include <board.h>
  20. #define LOG_LEVEL CONFIG_LOG_PWM_DEV_LEVEL
  21. #include <logging/log.h>
  22. LOG_MODULE_REGISTER(pwm);
  23. #define CMU_PWMCLK_CLKSEL_SHIFT (9)
  24. #define CMU_PWMCLK_CLKSEL_MASK (0x3 << CMU_PWMCLK_CLKSEL_SHIFT)
  25. #define CMU_PWMCLK_CLK_SEL(x) ((x) << CMU_PWMCLK_CLKSEL_SHIFT)
  26. #define CMU_PWMCLK_CLKSEL_16K CMU_PWMCLK_CLK_SEL(0)
  27. #define CMU_PWMCLK_CLKSEL_HOSC CMU_PWMCLK_CLK_SEL(1)
  28. #define CMU_PWMCLK_CLKSEL_CK64M CMU_PWMCLK_CLK_SEL(2)
  29. #define CMU_PWMCLK_CLKSEL_32K CMU_PWMCLK_CLK_SEL(0)
  30. #define CMU_PWMCLK_CLKDIV_SHIFT (0)
  31. #define CMU_PWMCLK_CLKDIV(x) ((x) << CMU_PWMCLK_CLKDIV_SHIFT)
  32. #define CMU_PWMCLK_CLKDIV_MASK CMU_PWMCLK_CLKDIV(0x1FF)
  33. #define PWM_CTRL_MODE_SEL_SHIFT (0)
  34. #define PWM_CTRL_MODE_SEL_MASK (0x3 << PWM_CTRL_MODE_SEL_SHIFT)
  35. #define PWM_CTRL_MODE_SEL(x) ((x) << PWM_CTRL_MODE_SEL_SHIFT)
  36. #define PWM_CTRL_MODE_SEL_FIXED PWM_CTRL_MODE_SEL(0)
  37. #define PWM_CTRL_MODE_SEL_BREATH PWM_CTRL_MODE_SEL(1)
  38. #define PWM_CTRL_MODE_SEL_PROGRAM PWM_CTRL_MODE_SEL(2)
  39. #define PWM_CTRL_POL_SEL_HIGH BIT(2)
  40. #define PWM_CTRL_CHANNEL_EN BIT(3)
  41. #define PWM_CTRL_CHANNEL_START BIT(4)
  42. #define PWM_BREATH_MODE_C_SHIFT (0)
  43. #define PWM_BREATH_MODE_C_MASK (0xFF << PWM_BREATH_MODE_C_SHIFT)
  44. #define PWM_BREATH_MODE_C(x) ((x) << PWM_BREATH_MODE_C_SHIFT)
  45. #define PWM_BREATH_MODE_QU_SHIFT (0)
  46. #define PWM_BREATH_MODE_QU_MASK (0xFF << PWM_BREATH_MODE_QU_SHIFT)
  47. #define PWM_BREATH_MODE_QU(x) ((x) << PWM_BREATH_MODE_QU_SHIFT)
  48. #define PWM_BREATH_MODE_QD_SHIFT (16)
  49. #define PWM_BREATH_MODE_QD_MASK (0xFF << PWM_BREATH_MODE_QD_SHIFT)
  50. #define PWM_BREATH_MODE_QD(x) ((x) << PWM_BREATH_MODE_QD_SHIFT)
  51. #define PWM_BREATH_MODE_H_SHIFT (0)
  52. #define PWM_BREATH_MODE_H_MASK (0xFFFF << PWM_BREATH_MODE_H_SHIFT)
  53. #define PWM_BREATH_MODE_H(x) ((x) << PWM_BREATH_MODE_H_SHIFT)
  54. #define PWM_BREATH_MODE_L_SHIFT (0)
  55. #define PWM_BREATH_MODE_L_MASK (0xFFFF << PWM_BREATH_MODE_L_SHIFT)
  56. #define PWM_BREATH_MODE_L(x) ((x) << PWM_BREATH_MODE_L_SHIFT)
  57. #define PWM_DUTYMAX_SHIFT (0)
  58. #define PWM_DUTYMAX_MASK (0xFFFF << PWM_DUTYMAX_SHIFT)
  59. #define PWM_DUTYMAX(x) ((x) << PWM_DUTYMAX_SHIFT)
  60. #define PWM_DUTY_SHIFT (0)
  61. #define PWM_DUTY_MASK (0xFFFF << PWM_DUTY_SHIFT)
  62. #define PWM_DUTY(x) ((x) << PWM_DUTY_SHIFT)
  63. #define PWM_DMA_CTL_START BIT(0)
  64. #define PWM_FIFO_CLK_SEL_SHIFT (4)
  65. #define PWM_FIFO_CLK_SEL_MASK (0xF << PWM_FIFO_CLK_SEL_SHIFT)
  66. #define PWM_FIFO_CLK_SEL(x) ((x) << PWM_FIFO_CLK_SEL_SHIFT)
  67. #define PWM_FIFOSTA_ERROR BIT(0)
  68. #define PWM_FIFOSTA_FULL BIT(1)
  69. #define PWM_FIFOSTA_EMPTY BIT(2)
  70. #define PWM_FIFOSTA_LEVEL_SHIFT (3)
  71. #define PWM_FIFOSTA_LEVEL_MASK (0x3 << PWM_FIFOSTA_LEVEL_SHIFT)
  72. #ifdef CHIP_LEOPARD
  73. #define PWM_CLK_CYCLES_PER_SEC (32000000)
  74. #else
  75. #define PWM_CLK_CYCLES_PER_SEC (16000)
  76. #endif
  77. #define PWM_PIN_CYCLES_PER_SEC (8000)
  78. #define PWM_PIN_CLK_PERIOD_USEC (1000000UL / PWM_PIN_CYCLES_PER_SEC)
  79. #define PWM_DUTYMAX_DEFAULT (16000) /* 1s / PWM_PIN_CYCLES_PER_SEC x PWM_DUTYMAX_DEFAULT = 2s */
  80. #define PWM_PROGRAM_PIN_INVALID (0xFF)
  81. #define PWM_BREATH_MODE_DEFAULT_C (32)
  82. #define PWM_DMACTL_REG_OFFSET (0x0F00)
  83. #define PWM_DMACTL_REG(x) ((struct acts_pwm_dmactl *)((x)->base + PWM_DMACTL_REG_OFFSET))
  84. #define PWM_CHAN(base, chan_id) ((struct acts_pwm_chan *)((base) + (chan_id * 0x100)))
  85. #define PWM_CYCLES_MAX (0xFFFF)
  86. #define DMA_IRQ_TC (0) /* DMA completion flag */
  87. #define DMA_IRQ_HF (1) /* DMA half-full flag */
  88. #define PWM_MFP (0x12 | (0x1 << 5) | ((3) << GPIO_CTL_PADDRV_SHIFT))
  89. #define LOW_DIV_FACTOR 1
  90. #define HIG_DIV_FACTOR 2
  91. #define LOW_DIV_MAX_VAL 256
  92. #define BOARD_RC32K_FREQ 32000
  93. #define BOARD_HOSC_FREQ 32000000
  94. #define BOARD_RC64M_FREQ 64000000
  95. enum {
  96. /**start voltage level low */
  97. START_VOLTAGE_LOW = 0,
  98. /**start voltage level high */
  99. START_VOLTAGE_HIGH,
  100. };
  101. /* pwm control registers */
  102. struct acts_pwm_chan {
  103. volatile u32_t ctrl;
  104. volatile u32_t c;
  105. volatile u32_t q;
  106. volatile u32_t h;
  107. volatile u32_t l;
  108. volatile u32_t duty_max;
  109. volatile u32_t duty;
  110. };
  111. /* pwm dma control registers */
  112. struct acts_pwm_dmactl {
  113. volatile u32_t dmactl;
  114. volatile u32_t fifodat;
  115. volatile u32_t fifosta;
  116. };
  117. struct pwm_acts_data {
  118. struct k_mutex mutex;
  119. const struct device *dma_dev;
  120. int dma_chan;
  121. int (*program_callback)(void *cb_data, u8_t reason);
  122. void *cb_data;
  123. u8_t program_pin;
  124. struct acts_pin_config pins_pwm_mfp[9];
  125. };
  126. struct pwm_acts_config {
  127. u32_t base;
  128. u32_t pwmclk_reg;
  129. u32_t cycle;
  130. u32_t chans;
  131. #if CHIP_LEOPARD
  132. u8_t clock0_id;
  133. u8_t clock1_id;
  134. u8_t clock2_id;
  135. u8_t clock3_id;
  136. #else
  137. u8_t clock_id;
  138. #endif
  139. u8_t reset_id;
  140. const char *dma_dev_name;
  141. u8_t txdma_id;
  142. u8_t flag_use_dma;
  143. };
  144. struct pwm_acts_chans{
  145. u8_t chan;
  146. u8_t gpio_num;
  147. };
  148. /*
  149. * Each line represents a set of pwm channels {0, 3}, {0, 4}, {0, 14}, {0, 36}, {0, 49}
  150. * The first numeric value of each set represents the pwm channel, and the second numeric value represents the pin number
  151. * For example: 0 represents pwm channel 0 3 represents GPIO3
  152. * The pwm driver scans the form to confirm if a channel has a gpio available
  153. */
  154. #ifdef CHIP_LEOPARD
  155. #define CONFIG_PWM_PIN_CHAN_MAP {0, 3}, {0, 14}, {0, 21}, {0, 49}, {0, 53}, {0, 64},\
  156. {1, 15}, {1, 18}, {1, 22}, {1, 50}, {1, 54}, {1, 57}, {1, 74},\
  157. {2, 6}, {2, 16}, {2, 19}, {2, 51}, {2, 55}, {2, 75},\
  158. {3, 7}, {3, 17}, {3, 20}, {3, 52}, {3, 56}, {3, 76}
  159. #else
  160. #define CONFIG_PWM_PIN_CHAN_MAP {0, 3}, {0, 4}, {0, 14}, {0, 36}, {0, 49},\
  161. {1, 5}, {1, 15}, {1, 37}, {1, 50},\
  162. {2, 6}, {2, 21}, {2, 38}, {2, 51},\
  163. {3, 7}, {3, 17}, {3, 39}, {3, 52},\
  164. {4, 8}, {4, 18}, {4, 40}, {4, 53},\
  165. {5, 9}, {5, 19}, {5, 41}, {5, 54},\
  166. {6, 10}, {6, 20}, {6, 42}, {6, 55},\
  167. {7, 11}, {7, 21}, {7, 43}, {7, 45}, {7, 56},\
  168. {8, 12}, {8, 22}, {8, 44}, {8, 46}, {8, 57}
  169. #endif
  170. static const struct pwm_acts_chans pwm_acts_chan_gpio_num[] = {CONFIG_PWM_PIN_CHAN_MAP};
  171. /*
  172. * Convert frequency and duty to clkdiv/dutymax/duty of pwm register.
  173. *
  174. * Input Parameters
  175. * clk_src_freq: clock source frequency [Hz]
  176. * pwm_freq: PWM channel to set
  177. * duty: duty
  178. * Output Parameters
  179. * reg_div_index: set value of PWMxCLKDIV in CMU_PWMxCLK register
  180. * reg_dutymax: set value of PWMx_DUTYMAX register
  181. * reg_duty: set value of PWMx_DUTY register
  182. * return 0, or negative errno code
  183. */
  184. static int convert_freq2pwmreg(uint32_t clk_src_freq, uint32_t pwm_freq,
  185. uint16_t duty, uint16_t *reg_div_index,
  186. uint16_t *reg_dutymax, uint16_t *reg_duty)
  187. {
  188. uint32_t i;
  189. uint16_t div = 0;
  190. uint32_t pwm_clk;
  191. uint32_t dutymax;
  192. uint8_t found_presc_flag = 0;
  193. static const uint16_t high_presc_values[6] =
  194. {256, 512, 1024, 2048, 4096, 8192};
  195. if (clk_src_freq < pwm_freq) {
  196. LOG_ERR("Undesirable input pwm setting frequency\n");
  197. return -ESPIPE;
  198. }
  199. /* Calculate optimal values of the pwmclk prescaler and dutymax for
  200. * the pwm register.
  201. * Example:
  202. *
  203. * clk_src_freq = 24 MHz
  204. * pwm_freq = 100 Hz
  205. *
  206. * div = 24,000,000 / 65,535 / 100
  207. * = 3.6 (or 4 -- taking the ceiling always)
  208. * pwmclk = 24,000,000 / 4
  209. * = 6,000,000
  210. * dutymax = 6,000,000 / 100
  211. * = 60,000
  212. */
  213. div = (clk_src_freq / pwm_freq + 65534) / 65535;
  214. for (i = 0; i < LOW_DIV_MAX_VAL; i++) {
  215. if (div <= (i + 1)) {
  216. found_presc_flag = LOW_DIV_FACTOR;
  217. break;
  218. }
  219. }
  220. if (found_presc_flag == 0) {
  221. for (i = 0; i < 6; i++) {
  222. if (div <= high_presc_values[i]) {
  223. found_presc_flag = HIG_DIV_FACTOR;
  224. break;
  225. }
  226. }
  227. }
  228. if (found_presc_flag == LOW_DIV_FACTOR) {
  229. div = i + 1;
  230. *reg_div_index = div - 1;
  231. } else if (found_presc_flag == HIG_DIV_FACTOR) {
  232. div = high_presc_values[i];
  233. *reg_div_index = ((LOW_DIV_MAX_VAL - 1) + i);
  234. } else {
  235. LOG_ERR("cannot find proper prescale\n");
  236. return -EACCES;
  237. }
  238. pwm_clk = clk_src_freq / div;
  239. dutymax = pwm_clk / pwm_freq;
  240. if (dutymax < 1)
  241. dutymax = 1;
  242. else if (dutymax > 65535)
  243. dutymax = 65535;
  244. *reg_dutymax = dutymax;
  245. *reg_duty = dutymax * duty / 100;
  246. LOG_DBG("div=%d, dutymax=%d, duty=%d", *reg_div_index, *reg_dutymax, *reg_duty);
  247. return 0;
  248. }
  249. int pwm_acts_mfp(unsigned int pin, unsigned int mode, bool set)
  250. {
  251. unsigned int key, ctl_reg, val;
  252. if (pin >= GPIO_MAX_PIN_NUM)
  253. return -EINVAL;
  254. ctl_reg = GPIO_REG_CTL(GPIO_REG_BASE, pin);
  255. key = irq_lock();
  256. if(set)
  257. val = (sys_read32(ctl_reg) & ~PINMUX_MODE_MASK) | mode;
  258. else
  259. val = (sys_read32(ctl_reg) & ~PINMUX_MODE_MASK);
  260. sys_write32(val, ctl_reg);
  261. irq_unlock(key);
  262. return 0;
  263. }
  264. static u16_t pwm_acts_check_chan(u32_t pwm, u32_t pin)
  265. {
  266. for(int i = 0; i < sizeof(pwm_acts_chan_gpio_num)/sizeof(struct pwm_acts_chans); i++)
  267. if(pwm == pwm_acts_chan_gpio_num[i].chan && pin == pwm_acts_chan_gpio_num[i].gpio_num)
  268. return 0;
  269. return -EINVAL;
  270. }
  271. static int pwm_acts_set_pinmux(const struct device *dev, u32_t chan)
  272. {
  273. //const struct pwm_acts_config *cfg = dev->config;
  274. struct pwm_acts_data *data = dev->data;
  275. if(data->pins_pwm_mfp[chan].pin_num == 0xffffffff) {
  276. LOG_ERR("this pin is not initialized");
  277. return -1;
  278. }
  279. pwm_acts_mfp(data->pins_pwm_mfp[chan].pin_num, data->pins_pwm_mfp[chan].mode, true);
  280. return 0;
  281. }
  282. static void pwm_acts_dma_reload(const struct device *dev, void *user_data,
  283. uint32_t channel, int status)
  284. {
  285. u32_t _reason;
  286. int ret;
  287. struct pwm_acts_data *data = (struct pwm_acts_data *)user_data;
  288. if (status == DMA_IRQ_HF) {
  289. _reason = PWM_PROGRAM_DMA_IRQ_HF;
  290. } else if (status == DMA_IRQ_TC) {
  291. _reason = PWM_PROGRAM_DMA_IRQ_TC;
  292. } else {
  293. LOG_ERR("Unknown DMA reason %d", status);
  294. dma_stop(dev, data->dma_chan);
  295. return ;
  296. }
  297. ret = data->program_callback(data->cb_data, _reason);
  298. if (ret < 0)
  299. dma_stop(dev, data->dma_chan);
  300. }
  301. static void pwm_acts_dma_direct(const struct device *dev, void *user_data,
  302. uint32_t channel, int status)
  303. {
  304. struct pwm_acts_data *data = (struct pwm_acts_data *)user_data;
  305. ARG_UNUSED(dev);
  306. if ((status == DMA_IRQ_TC) && data->program_callback) {
  307. data->program_callback(data->cb_data, PWM_PROGRAM_DMA_IRQ_TC);
  308. }
  309. }
  310. int pwm_acts_dma_prepare(struct device *dev, u32_t chan, pwm_program_ctrl_t *ctrl)
  311. {
  312. const struct pwm_acts_config *cfg = dev->config;
  313. struct pwm_acts_data *data = dev->data;
  314. struct acts_pwm_dmactl *dmactrl = PWM_DMACTL_REG(cfg);
  315. struct dma_config dma_cfg = {0};
  316. struct dma_block_config dma_block_cfg = {0};
  317. /* request dma channel handle */
  318. if (data->dma_chan == -1) {
  319. data->dma_chan = dma_request(data->dma_dev, 0xFF);
  320. if (!data->dma_chan) {
  321. LOG_ERR("Failed to request dma channel");
  322. return -ENXIO;
  323. }
  324. }
  325. dma_cfg.channel_direction = MEMORY_TO_PERIPHERAL;
  326. dma_cfg.source_burst_length = 8;
  327. dma_cfg.dest_burst_length = 8;
  328. dma_cfg.source_data_size = 2;
  329. dma_cfg.dma_slot = cfg->txdma_id;
  330. dma_block_cfg.source_address = (u32_t)ctrl->ram_buf;
  331. dma_block_cfg.dest_address = (u32_t)&dmactrl->fifodat;
  332. dma_block_cfg.block_size = ctrl->ram_buf_len;
  333. dma_block_cfg.source_reload_en = ctrl->reload_en;
  334. dma_cfg.block_count = 1;
  335. dma_cfg.head_block = &dma_block_cfg;
  336. if (ctrl->program_callback) {
  337. if (ctrl->reload_en)
  338. dma_cfg.dma_callback = pwm_acts_dma_reload;
  339. else
  340. dma_cfg.dma_callback = pwm_acts_dma_direct;
  341. dma_cfg.user_data = data;
  342. dma_cfg.complete_callback_en = 1;
  343. // dma_cfg.half_complete_callback_en = 1;
  344. data->program_callback = ctrl->program_callback;
  345. data->cb_data = ctrl->cb_data;
  346. }
  347. if (dma_config(data->dma_dev, data->dma_chan, &dma_cfg)) {
  348. LOG_ERR("DMA config error");
  349. return -EFAULT;
  350. }
  351. return 0;
  352. }
  353. /*
  354. * Set the period and pulse width for a PWM pin.
  355. *
  356. * Parameters
  357. * dev: Pointer to PWM device structure
  358. * pwm: PWM channel to set
  359. * period_cycles: Period (in timer count)
  360. * pulse_cycles: Pulse width (in timer count).
  361. * @param flags Flags for pin configuration (polarity).
  362. * return 0, or negative errno code
  363. */
  364. static int pwm_acts_pin_set(const struct device *dev, u32_t chan,
  365. u32_t period_cycles, u32_t pulse_cycles, pwm_flags_t flags)
  366. {
  367. const struct pwm_acts_config *cfg = dev->config;
  368. //struct pwm_acts_data *data = dev->data;
  369. struct acts_pwm_chan *pwm_chan;
  370. uint32_t key;
  371. LOG_INF("PWM@%d set period cycles %d, pulse cycles %d",
  372. chan, period_cycles, pulse_cycles);
  373. if (chan >= cfg->chans) {
  374. LOG_ERR("invalid chan %d", chan);
  375. return -EINVAL;
  376. }
  377. if (pulse_cycles > period_cycles) {
  378. LOG_ERR("pulse cycles %d is biger than period's %d",
  379. pulse_cycles, period_cycles);
  380. return -EINVAL;
  381. }
  382. if (period_cycles > PWM_CYCLES_MAX || period_cycles < 1) {
  383. LOG_ERR("period cycles invalid %d (max %d min 1)",
  384. period_cycles, PWM_CYCLES_MAX);
  385. return -EINVAL;
  386. }
  387. if (pwm_acts_set_pinmux(dev, chan)) {
  388. LOG_DBG("set pwm pinmux error");
  389. return -EFAULT;
  390. }
  391. key = irq_lock();
  392. pwm_chan = PWM_CHAN(cfg->base, chan);
  393. LOG_DBG("pwm_chan %p", pwm_chan);
  394. /* disable pwm */
  395. pwm_chan->ctrl = 0;
  396. /* setup pwm parameters */
  397. if (START_VOLTAGE_HIGH == flags)
  398. pwm_chan->ctrl = PWM_CTRL_POL_SEL_HIGH | PWM_CTRL_MODE_SEL_FIXED;
  399. else
  400. pwm_chan->ctrl = PWM_CTRL_MODE_SEL_FIXED;
  401. /* PWM period = PWM_CLK period x PWM_DUTYMAX */
  402. pwm_chan->duty_max = PWM_DUTYMAX(period_cycles);
  403. /* PWM DUTY = DUTY / PWM_DUTYMAX */
  404. pwm_chan->duty = pulse_cycles * (pwm_chan->duty_max & PWM_DUTYMAX_MASK) / period_cycles;
  405. /* enable pwm */
  406. pwm_chan->ctrl |= (PWM_CTRL_CHANNEL_EN | PWM_CTRL_CHANNEL_START);
  407. irq_unlock(key);
  408. return 0;
  409. }
  410. /*
  411. * Set the frequency and duty for a PWM channel.
  412. *
  413. * Parameters
  414. * dev: Pointer to PWM device structure
  415. * pwm: PWM channel to set
  416. * freq: frequency of pwm.
  417. * duty: duty cycle from 0~100.
  418. * @param flags Flags for pin configuration (polarity).
  419. * return 0, or negative errno code
  420. */
  421. static int pwm_acts_freq_duty_set(const struct device *dev, u32_t chan,
  422. u32_t freq, u8_t duty, pwm_flags_t flags)
  423. {
  424. const struct pwm_acts_config *cfg = dev->config;
  425. struct acts_pwm_chan *pwm_chan;
  426. uint16_t reg_div_index;
  427. uint16_t reg_dutymax;
  428. uint16_t reg_duty;
  429. uint32_t reg_pwmclk = 0;
  430. uint32_t key;
  431. int ret;
  432. LOG_DBG("PWM@%d set frequency %d, duty %%%d", chan, freq, duty);
  433. if (chan >= cfg->chans) {
  434. LOG_ERR("invalid chan %d", chan);
  435. return -EINVAL;
  436. }
  437. if (duty > 100) {
  438. LOG_ERR("duty cycle is not valid");
  439. return -EINVAL;
  440. }
  441. if (pwm_acts_set_pinmux(dev, chan)) {
  442. LOG_ERR("set pwm pinmux error");
  443. return -EFAULT;
  444. }
  445. ret = convert_freq2pwmreg(PWM_CLK_CYCLES_PER_SEC, freq, duty,
  446. &reg_div_index, &reg_dutymax, &reg_duty);
  447. if (ret < 0)
  448. return -EINVAL;
  449. key = irq_lock();
  450. switch (PWM_CLK_CYCLES_PER_SEC) {
  451. case BOARD_RC64M_FREQ:
  452. reg_pwmclk = CMU_PWMCLK_CLKSEL_CK64M;
  453. break;
  454. case BOARD_RC32K_FREQ:
  455. reg_pwmclk = CMU_PWMCLK_CLKSEL_32K;
  456. break;
  457. case BOARD_HOSC_FREQ:
  458. reg_pwmclk = CMU_PWMCLK_CLKSEL_HOSC;
  459. break;
  460. default:
  461. reg_pwmclk = CMU_PWMCLK_CLKSEL_HOSC;
  462. break;
  463. }
  464. sys_write32(reg_pwmclk | CMU_PWMCLK_CLKDIV(reg_div_index),
  465. PWM_CLK0_BASE + 4 * chan);
  466. pwm_chan = PWM_CHAN(cfg->base, chan);
  467. /* disable pwm */
  468. pwm_chan->ctrl = 0;
  469. /* setup pwm parameters */
  470. if (START_VOLTAGE_HIGH == flags)
  471. pwm_chan->ctrl = PWM_CTRL_POL_SEL_HIGH | PWM_CTRL_MODE_SEL_FIXED;
  472. else
  473. pwm_chan->ctrl = PWM_CTRL_MODE_SEL_FIXED;
  474. pwm_chan->duty_max = reg_dutymax;
  475. pwm_chan->duty = reg_duty;
  476. /* enable pwm */
  477. pwm_chan->ctrl |= (PWM_CTRL_CHANNEL_EN | PWM_CTRL_CHANNEL_START);
  478. irq_unlock(key);
  479. return 0;
  480. }
  481. /*
  482. * Get the clock rate (cycles per second) for a PWM pin.
  483. *
  484. * Parameters
  485. * dev: Pointer to PWM device structure
  486. * pwm: PWM port number
  487. * cycles: Pointer to the memory to store clock rate (cycles per second)
  488. *
  489. * return 0, or negative errno code
  490. */
  491. static int pwm_acts_get_cycles_per_sec(const struct device *dev, u32_t chan,
  492. u64_t *cycles)
  493. {
  494. const struct pwm_acts_config *cfg = dev->config;
  495. if (chan >= cfg->chans) {
  496. LOG_ERR("invalid pwm chan %d", chan);
  497. return -EINVAL;
  498. }
  499. *cycles = (u64_t)cfg->cycle;
  500. return 0;
  501. }
  502. static int pwm_acts_set_breath_mode(const struct device *dev, u32_t chan, pwm_breath_ctrl_t *ctrl)
  503. {
  504. const struct pwm_acts_config *cfg = dev->config;
  505. struct pwm_acts_data *data = dev->data;
  506. struct acts_pwm_chan *pwm_chan;
  507. u32_t period = PWM_PIN_CLK_PERIOD_USEC, qd, qu, high, low;
  508. pwm_breath_ctrl_t breath_ctrl = {0};
  509. if (!ctrl) {
  510. breath_ctrl.rise_time_ms = PWM_BREATH_RISE_TIME_DEFAULT;
  511. breath_ctrl.down_time_ms = PWM_BREATH_DOWN_TIME_DEFAULT;
  512. breath_ctrl.high_time_ms = PWM_BREATH_HIGH_TIME_DEFAULT;
  513. breath_ctrl.low_time_ms = PWM_BREATH_LOW_TIME_DEFAULT;
  514. } else {
  515. memcpy(&breath_ctrl, ctrl, sizeof(pwm_breath_ctrl_t));
  516. }
  517. LOG_INF("PWM@%d rise %dms, down %dms, high %dms, low %dms",
  518. chan, breath_ctrl.rise_time_ms, breath_ctrl.down_time_ms,
  519. breath_ctrl.high_time_ms, breath_ctrl.low_time_ms);
  520. if (chan >= cfg->chans) {
  521. LOG_ERR("invalid chan %d", chan);
  522. return -EINVAL;
  523. }
  524. if (pwm_acts_set_pinmux(dev, chan)) {
  525. LOG_DBG("set pwm pinmux error");
  526. return -EFAULT;
  527. }
  528. k_mutex_lock(&data->mutex, K_FOREVER);
  529. pwm_chan = PWM_CHAN(cfg->base, chan);
  530. /* disable pwm */
  531. pwm_chan->ctrl = 0;
  532. /* setup pwm parameters */
  533. pwm_chan->ctrl = PWM_CTRL_POL_SEL_HIGH | PWM_CTRL_MODE_SEL_BREATH;
  534. /* rise time T1 = QU x C x C x t; C=32, t=PWM_PIN_CLK_PERIOD_USEC */
  535. qu = breath_ctrl.rise_time_ms * 1000 / PWM_BREATH_MODE_DEFAULT_C / PWM_BREATH_MODE_DEFAULT_C;
  536. qu = (qu + period - 1) / period*3/2;
  537. /* down time T2 = QD x C x C x t; C=32, t=PWM_PIN_CLK_PERIOD_USEC */
  538. qd = breath_ctrl.down_time_ms * 1000 / PWM_BREATH_MODE_DEFAULT_C / PWM_BREATH_MODE_DEFAULT_C;
  539. qd = (qd + period - 1) / period*3/2;
  540. /* high level time T3 = H x C x t; C=32, t = PWM_PIN_CLK_PERIOD_USEC */
  541. high = breath_ctrl.high_time_ms * 1000 / PWM_BREATH_MODE_DEFAULT_C;
  542. high = (high + period - 1) / period*3/2;
  543. /* high level time T3 = L x C x t; C=32, t = PWM_PIN_CLK_PERIOD_USEC */
  544. low = breath_ctrl.low_time_ms * 1000 / PWM_BREATH_MODE_DEFAULT_C;
  545. low = (low + period - 1) / period*3/2;
  546. LOG_INF("QU:%d QD:%d high:%d low:%d", qu, qd, high, low);
  547. pwm_chan->c = PWM_BREATH_MODE_C(PWM_BREATH_MODE_DEFAULT_C);
  548. pwm_chan->q = PWM_BREATH_MODE_QU(qu) | PWM_BREATH_MODE_QD(qd);
  549. pwm_chan->h = PWM_BREATH_MODE_H(high);
  550. pwm_chan->l = PWM_BREATH_MODE_L(low);
  551. /* enable pwm */
  552. pwm_chan->ctrl |= (PWM_CTRL_CHANNEL_EN | PWM_CTRL_CHANNEL_START);
  553. k_mutex_unlock(&data->mutex);
  554. return 0;
  555. }
  556. static int pwm_acts_pin_stop(const struct device *dev, u32_t chan)
  557. {
  558. const struct pwm_acts_config *cfg = dev->config;
  559. //struct pwm_acts_data *data = dev->data;
  560. struct acts_pwm_chan *pwm_chan;
  561. if (chan >= cfg->chans) {
  562. LOG_ERR("invalid pwm chan %d", chan);
  563. return -EINVAL;
  564. }
  565. pwm_chan = PWM_CHAN(cfg->base, chan);
  566. /* disable pwm */
  567. pwm_chan->ctrl = 0;
  568. LOG_INF("PWM@%d pin stop", chan);
  569. return 0;
  570. }
  571. static int pwm_acts_pin_mfp_set(const struct device *dev, u32_t pwm, u32_t pin)
  572. {
  573. //const struct pwm_acts_config *cfg = dev->config;
  574. struct pwm_acts_data *data = dev->data;
  575. if(pwm_acts_check_chan(pwm, pin)) {
  576. LOG_ERR("invalid pwm %d: %d", pwm, pin);
  577. return -1;
  578. }
  579. if(data->pins_pwm_mfp[pwm].pin_num != 0)//this chan has been used
  580. pwm_acts_mfp(data->pins_pwm_mfp[pwm].pin_num, 0, false);
  581. data->pins_pwm_mfp[pwm].pin_num = pin;
  582. data->pins_pwm_mfp[pwm].mode = PWM_MFP;
  583. LOG_INF("PWM@%d pin mfp set", pin);
  584. return 0;
  585. }
  586. int pwm_acts_init(const struct device *dev)
  587. {
  588. const struct pwm_acts_config *cfg = dev->config;
  589. struct pwm_acts_data *data = dev->data;
  590. int i, chan;
  591. #ifdef CHIP_LEOPARD
  592. u16_t reg_div;
  593. #else
  594. u8_t clk_div;
  595. #endif
  596. struct board_pwm_pinmux_info pinmux_info;
  597. board_get_pwm_pinmux_info(&pinmux_info);
  598. memset(data->pins_pwm_mfp, 0xff, sizeof(data->pins_pwm_mfp));
  599. for (i = 0; i < pinmux_info.pins_num; i++) {
  600. chan = pinmux_info.pins_config[i].pin_chan;
  601. data->pins_pwm_mfp[chan].pin_num = pinmux_info.pins_config[i].pin_num;
  602. data->pins_pwm_mfp[chan].mode = pinmux_info.pins_config[i].mode;
  603. }
  604. #ifdef CHIP_LEOPARD
  605. /* enable pwm controller clock */
  606. acts_clock_peripheral_enable(cfg->clock0_id);
  607. acts_clock_peripheral_enable(cfg->clock1_id);
  608. acts_clock_peripheral_enable(cfg->clock2_id);
  609. acts_clock_peripheral_enable(cfg->clock3_id);
  610. #else
  611. /* enable pwm controller clock */
  612. acts_clock_peripheral_enable(cfg->clock_id);
  613. #endif
  614. /* reset pwm controller */
  615. acts_reset_peripheral(cfg->reset_id);
  616. #ifdef CHIP_LEOPARD
  617. /* clock source: 32Mhz, pwm clock fs 8KHz period 125us */
  618. reg_div = PWM_CLK_CYCLES_PER_SEC / PWM_PIN_CYCLES_PER_SEC;
  619. /* Segmental calculate reg val according div factor */
  620. if (reg_div < 256) {
  621. reg_div -= 1;
  622. } else if ((reg_div >= 256) && (reg_div < 384)) {/* div: 256 */
  623. reg_div = 255;
  624. } else if ((reg_div >= 384) && (reg_div < 768)) {/* div: 512 */
  625. reg_div = 256;
  626. } else if ((reg_div >= 768) && (reg_div < 1536)) {/* div: 1024 */
  627. reg_div = 257;
  628. } else if ((reg_div >= 1536) && (reg_div < 3072)) {/* div: 2048 */
  629. reg_div = 258;
  630. } else if ((reg_div >= 3072) && (reg_div < 6144)) {/* div: 4096 */
  631. reg_div = 259;
  632. } else if (reg_div >= 6144) {/* div: 8192 */
  633. reg_div = 260;
  634. }
  635. /* init PWM clock */
  636. for (i = 0; i < cfg->chans; i++)
  637. sys_write32(CMU_PWMCLK_CLKSEL_HOSC | CMU_PWMCLK_CLKDIV(reg_div), \
  638. cfg->pwmclk_reg + 4 * i);
  639. #else
  640. /* clock source: 16K, div= / 2, pwm clock fs 8KHz period 125us */
  641. clk_div = (PWM_CLK_CYCLES_PER_SEC / PWM_PIN_CYCLES_PER_SEC) - 1;
  642. /* init PWM clock */
  643. for (i = 0; i < cfg->chans; i++) {
  644. sys_write32(CMU_PWMCLK_CLKSEL_16K | CMU_PWMCLK_CLKDIV(clk_div), \
  645. cfg->pwmclk_reg + 4 * i);
  646. }
  647. #endif
  648. k_mutex_init(&data->mutex);
  649. if (cfg->flag_use_dma) {
  650. data->dma_dev = device_get_binding(cfg->dma_dev_name);
  651. if (!data->dma_dev) {
  652. LOG_ERR("Bind DMA device %s error", cfg->dma_dev_name);
  653. return -ENOENT;
  654. }
  655. data->program_pin = PWM_PROGRAM_PIN_INVALID;
  656. data->dma_chan = dma_request(data->dma_dev, 0xff);
  657. if(data->dma_chan < 0){
  658. LOG_ERR("dma-dev rxchan config err chan=%d\n", data->dma_chan);
  659. return -ENODEV;
  660. }
  661. }
  662. //sys_write32(sys_read32(0x40068000)|(1<<16), 0x40068000);
  663. return 0;
  664. }
  665. const struct pwm_driver_api pwm_acts_driver_api = {
  666. .pin_set = pwm_acts_pin_set,
  667. .get_cycles_per_sec = pwm_acts_get_cycles_per_sec,
  668. .set_breath = pwm_acts_set_breath_mode,
  669. .pin_mfp = pwm_acts_pin_mfp_set,
  670. // .set_program = pwm_acts_set_program_mode,
  671. .pin_stop = pwm_acts_pin_stop,
  672. .pin_set_freq_duty = pwm_acts_freq_duty_set,
  673. };
  674. static struct pwm_acts_data pwm_acts_data;
  675. static const struct pwm_acts_config pwm_acts_config = {
  676. .base = (uint32_t)PWM_REG_BASE,
  677. .pwmclk_reg = PWM_CLK0_BASE,
  678. .cycle = CONFIG_PWM_CYCLE,
  679. .chans = CONFIG_PWM_CHANS,
  680. #if CHIP_LEOPARD
  681. .clock0_id = CLOCK_ID_PWM0,
  682. .clock1_id = CLOCK_ID_PWM1,
  683. .clock2_id = CLOCK_ID_PWM2,
  684. .clock3_id = CLOCK_ID_PWM3,
  685. #else
  686. .clock_id = CLOCK_ID_PWM,
  687. #endif
  688. .reset_id = RESET_ID_PWM,
  689. .dma_dev_name = CONFIG_DMA_0_NAME,
  690. .txdma_id = CONFIG_PWM_DMA_CHAN,
  691. .flag_use_dma = CONFIG_PWM_USE_DMA,
  692. };
  693. #if IS_ENABLED(CONFIG_PWM)
  694. DEVICE_DEFINE(pwm_acts, CONFIG_PWM_NAME,
  695. pwm_acts_init, NULL,
  696. &pwm_acts_data, &pwm_acts_config,
  697. POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
  698. &pwm_acts_driver_api);
  699. #endif