driver: update gpt driver to support internal divider by dts

using dts support gpt clock source and divider

Signed-off-by: Hake Huang <hake.huang@oss.nxp.com>
This commit is contained in:
Hake Huang 2020-12-09 16:35:47 +08:00 committed by Maureen Helm
commit 3940b407d9

View file

@ -7,6 +7,7 @@
#define DT_DRV_COMPAT nxp_imx_gpt
#include <drivers/counter.h>
#include <drivers/clock_control.h>
#include <fsl_gpt.h>
#include <logging/log.h>
@ -15,6 +16,8 @@ LOG_MODULE_REGISTER(mcux_gpt, CONFIG_COUNTER_LOG_LEVEL);
struct mcux_gpt_config {
/* info must be first element */
struct counter_config_info info;
char *clock_name;
clock_control_subsys_t clock_subsys;
GPT_Type *base;
clock_name_t clock_source;
};
@ -169,13 +172,24 @@ static uint32_t mcux_gpt_get_max_relative_alarm(const struct device *dev)
static int mcux_gpt_init(const struct device *dev)
{
const struct mcux_gpt_config *config = dev->config;
const struct device *clock_dev;
gpt_config_t gptConfig;
uint32_t clock_freq;
clock_dev = device_get_binding(config->clock_name);
if (clock_dev == NULL) {
return -EINVAL;
}
if (clock_control_get_rate(clock_dev, config->clock_subsys,
&clock_freq)) {
return -EINVAL;
}
/* Adjust divider to match expected freq */
clock_freq = CLOCK_GetFreq(config->clock_source);
if (clock_freq % config->info.freq) {
LOG_ERR("Cannot Adjust GPT freq to %u\n", config->info.freq);
LOG_ERR("clock src is %u\n", clock_freq);
return -EINVAL;
}
@ -205,10 +219,12 @@ static const struct counter_driver_api mcux_gpt_driver_api = {
\
static const struct mcux_gpt_config mcux_gpt_config_ ## n = { \
.base = (void *)DT_INST_REG_ADDR(n), \
.clock_source = kCLOCK_PerClk, \
.clock_name = DT_INST_CLOCKS_LABEL(n), \
.clock_subsys = \
(clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name),\
.info = { \
.max_top_value = UINT32_MAX, \
.freq = 25000000, \
.freq = DT_INST_PROP(n, gptfreq), \
.channels = 1, \
.flags = COUNTER_CONFIG_INFO_COUNT_UP, \
}, \