2021-07-27 17:39:02 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Telink Semiconductor
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT telink_b91_pwm
|
|
|
|
|
2022-06-17 18:50:31 +08:00
|
|
|
#include <pwm.h>
|
|
|
|
#include <clock.h>
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/pwm.h>
|
|
|
|
#include <zephyr/drivers/pinctrl.h>
|
2021-07-27 17:39:02 +08:00
|
|
|
|
2022-06-17 18:38:47 +08:00
|
|
|
struct pwm_b91_config {
|
|
|
|
const struct pinctrl_dev_config *pcfg;
|
|
|
|
uint32_t clock_frequency;
|
|
|
|
uint8_t channels;
|
|
|
|
uint8_t clk32k_ch_enable;
|
|
|
|
};
|
2021-07-27 17:39:02 +08:00
|
|
|
|
|
|
|
/* API implementation: init */
|
|
|
|
static int pwm_b91_init(const struct device *dev)
|
|
|
|
{
|
2022-06-17 18:38:47 +08:00
|
|
|
const struct pwm_b91_config *config = dev->config;
|
2021-07-27 17:39:02 +08:00
|
|
|
|
2022-02-11 20:17:22 +08:00
|
|
|
uint32_t status = 0;
|
2021-07-27 17:39:02 +08:00
|
|
|
uint8_t clk_32k_en = 0;
|
|
|
|
uint32_t pwm_clk_div = 0;
|
|
|
|
|
|
|
|
/* Calculate and check PWM clock divider */
|
2022-06-17 18:38:47 +08:00
|
|
|
pwm_clk_div = sys_clk.pclk * 1000 * 1000 / config->clock_frequency - 1;
|
2021-07-27 17:39:02 +08:00
|
|
|
if (pwm_clk_div > 255) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set PWM Peripheral clock */
|
|
|
|
pwm_set_clk((unsigned char) (pwm_clk_div & 0xFF));
|
|
|
|
|
|
|
|
/* Set PWM 32k Channel clock if enabled */
|
2022-06-17 18:38:47 +08:00
|
|
|
clk_32k_en |= (config->clk32k_ch_enable & BIT(0)) ? PWM_CLOCK_32K_CHN_PWM0 : 0;
|
|
|
|
clk_32k_en |= (config->clk32k_ch_enable & BIT(1)) ? PWM_CLOCK_32K_CHN_PWM1 : 0;
|
|
|
|
clk_32k_en |= (config->clk32k_ch_enable & BIT(2)) ? PWM_CLOCK_32K_CHN_PWM2 : 0;
|
|
|
|
clk_32k_en |= (config->clk32k_ch_enable & BIT(3)) ? PWM_CLOCK_32K_CHN_PWM3 : 0;
|
|
|
|
clk_32k_en |= (config->clk32k_ch_enable & BIT(4)) ? PWM_CLOCK_32K_CHN_PWM4 : 0;
|
|
|
|
clk_32k_en |= (config->clk32k_ch_enable & BIT(5)) ? PWM_CLOCK_32K_CHN_PWM5 : 0;
|
2021-07-27 17:39:02 +08:00
|
|
|
pwm_32k_chn_en(clk_32k_en);
|
|
|
|
|
|
|
|
/* Config PWM pins */
|
2022-06-17 18:38:47 +08:00
|
|
|
status = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
|
2022-02-11 20:17:22 +08:00
|
|
|
if (status < 0) {
|
|
|
|
return status;
|
2021-07-27 17:39:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:06:43 +08:00
|
|
|
/* API implementation: set_cycles */
|
|
|
|
static int pwm_b91_set_cycles(const struct device *dev, uint32_t channel,
|
|
|
|
uint32_t period_cycles, uint32_t pulse_cycles,
|
|
|
|
pwm_flags_t flags)
|
2021-07-27 17:39:02 +08:00
|
|
|
{
|
2022-06-17 18:38:47 +08:00
|
|
|
const struct pwm_b91_config *config = dev->config;
|
2021-07-27 17:39:02 +08:00
|
|
|
|
|
|
|
/* check pwm channel */
|
2022-06-17 18:38:47 +08:00
|
|
|
if (channel >= config->channels) {
|
2021-07-27 17:39:02 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check size of pulse and period (2 bytes) */
|
|
|
|
if ((period_cycles > 0xFFFFu) ||
|
|
|
|
(pulse_cycles > 0xFFFFu)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set polarity */
|
|
|
|
if (flags & PWM_POLARITY_INVERTED) {
|
2022-04-04 22:35:22 +08:00
|
|
|
pwm_invert_en(channel);
|
2021-07-27 17:39:02 +08:00
|
|
|
} else {
|
2022-04-04 22:35:22 +08:00
|
|
|
pwm_invert_dis(channel);
|
2021-07-27 17:39:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set pulse and period */
|
2022-04-04 22:35:22 +08:00
|
|
|
pwm_set_tcmp(channel, pulse_cycles);
|
|
|
|
pwm_set_tmax(channel, period_cycles);
|
2021-07-27 17:39:02 +08:00
|
|
|
|
|
|
|
/* start pwm */
|
2022-04-04 22:35:22 +08:00
|
|
|
pwm_start(channel);
|
2021-07-27 17:39:02 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* API implementation: get_cycles_per_sec */
|
2022-04-04 22:35:22 +08:00
|
|
|
static int pwm_b91_get_cycles_per_sec(const struct device *dev,
|
|
|
|
uint32_t channel, uint64_t *cycles)
|
2021-07-27 17:39:02 +08:00
|
|
|
{
|
2022-06-17 18:38:47 +08:00
|
|
|
const struct pwm_b91_config *config = dev->config;
|
2021-07-27 17:39:02 +08:00
|
|
|
|
|
|
|
/* check pwm channel */
|
2022-06-17 18:38:47 +08:00
|
|
|
if (channel >= config->channels) {
|
2021-07-27 17:39:02 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-06-17 18:38:47 +08:00
|
|
|
if ((config->clk32k_ch_enable & BIT(channel)) != 0U) {
|
2021-07-27 17:39:02 +08:00
|
|
|
*cycles = 32000u;
|
|
|
|
} else {
|
|
|
|
*cycles = sys_clk.pclk * 1000 * 1000 / (reg_pwm_clkdiv + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PWM driver APIs structure */
|
|
|
|
static const struct pwm_driver_api pwm_b91_driver_api = {
|
2022-04-01 17:06:43 +08:00
|
|
|
.set_cycles = pwm_b91_set_cycles,
|
2021-07-27 17:39:02 +08:00
|
|
|
.get_cycles_per_sec = pwm_b91_get_cycles_per_sec,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* PWM driver registration */
|
|
|
|
#define PWM_B91_INIT(n) \
|
2022-06-17 18:38:47 +08:00
|
|
|
PINCTRL_DT_INST_DEFINE(n); \
|
|
|
|
\
|
|
|
|
static const struct pwm_b91_config config##n = { \
|
|
|
|
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
|
|
|
|
.clock_frequency = DT_INST_PROP(n, clock_frequency), \
|
|
|
|
.channels = DT_INST_PROP(n, channels), \
|
|
|
|
.clk32k_ch_enable = \
|
|
|
|
((DT_INST_PROP(n, clk32k_ch0_enable) << 0U) | \
|
|
|
|
(DT_INST_PROP(n, clk32k_ch1_enable) << 1U) | \
|
|
|
|
(DT_INST_PROP(n, clk32k_ch2_enable) << 2U) | \
|
|
|
|
(DT_INST_PROP(n, clk32k_ch3_enable) << 3U) | \
|
|
|
|
(DT_INST_PROP(n, clk32k_ch4_enable) << 4U) | \
|
|
|
|
(DT_INST_PROP(n, clk32k_ch5_enable) << 5U)), \
|
|
|
|
}; \
|
2021-07-27 17:39:02 +08:00
|
|
|
\
|
|
|
|
DEVICE_DT_INST_DEFINE(n, pwm_b91_init, \
|
2022-06-17 18:38:47 +08:00
|
|
|
NULL, NULL, &config##n, \
|
2022-09-21 17:22:06 +08:00
|
|
|
POST_KERNEL, CONFIG_PWM_INIT_PRIORITY, \
|
2021-07-27 17:39:02 +08:00
|
|
|
&pwm_b91_driver_api);
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(PWM_B91_INIT)
|