drivers: counter: sam0_tc32: rework devicetree support

Rework the devicetree to utilize new DT_INST macros and extract per
instance data for clocks from devicetree.  Move the prescaler setting
from Kconfig to devicetree as well.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
This commit is contained in:
Kumar Gala 2020-04-21 10:29:59 -05:00 committed by Kumar Gala
parent fe573b6e23
commit 8eafcc32db
4 changed files with 66 additions and 108 deletions

View file

@ -1,26 +1,10 @@
# Copyright (c) 2019 Derek Hageman <hageman@inthat.cloud>
# SPDX-License-Identifier: Apache-2.0
menuconfig COUNTER_SAM0_TC32
config COUNTER_SAM0_TC32
bool "SAM0 series 32-bit basic timer driver"
default y
depends on SOC_FAMILY_SAM0
help
Enable the SAM0 series timer counter (TC) driver in 32-bit wide
mode.
if COUNTER_SAM0_TC32
index = 0
source "drivers/counter/Kconfig.template.sam0_tc32"
index = 2
source "drivers/counter/Kconfig.template.sam0_tc32"
index = 4
source "drivers/counter/Kconfig.template.sam0_tc32"
index = 6
source "drivers/counter/Kconfig.template.sam0_tc32"
endif # COUNTER_SAM0_TC32

View file

@ -1,35 +0,0 @@
# Copyright (c) 2019 Derek Hageman <hageman@inthat.cloud>
# SPDX-License-Identifier: Apache-2.0
choice
bool "Timer $(index) prescaler"
config COUNTER_SAM0_TC32_$(index)_PRESCALER_1
bool "clock / 1"
config COUNTER_SAM0_TC32_$(index)_PRESCALER_2
bool "clock / 2"
config COUNTER_SAM0_TC32_$(index)_PRESCALER_4
bool "clock / 4"
config COUNTER_SAM0_TC32_$(index)_PRESCALER_8
bool "clock / 8"
config COUNTER_SAM0_TC32_$(index)_PRESCALER_16
bool "clock / 16"
config COUNTER_SAM0_TC32_$(index)_PRESCALER_64
bool "clock / 64"
config COUNTER_SAM0_TC32_$(index)_PRESCALER_256
bool "clock / 256"
config COUNTER_SAM0_TC32_$(index)_PRESCALER_1024
bool "clock / 1024"
endchoice
config COUNTER_SAM0_TC32_$(index)_DIVISOR
int
default 1 if COUNTER_SAM0_TC32_$(index)_PRESCALER_1
default 2 if COUNTER_SAM0_TC32_$(index)_PRESCALER_2
default 4 if COUNTER_SAM0_TC32_$(index)_PRESCALER_4
default 8 if COUNTER_SAM0_TC32_$(index)_PRESCALER_8
default 16 if COUNTER_SAM0_TC32_$(index)_PRESCALER_16
default 64 if COUNTER_SAM0_TC32_$(index)_PRESCALER_64
default 256 if COUNTER_SAM0_TC32_$(index)_PRESCALER_256
default 1024 if COUNTER_SAM0_TC32_$(index)_PRESCALER_1024

View file

@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT atmel_sam0_tc32
#include <drivers/counter.h>
#include <device.h>
#include <soc.h>
@ -402,64 +404,57 @@ static const struct counter_driver_api counter_sam0_tc32_driver_api = {
#ifdef MCLK
#define COUNTER_SAM0_TC32_CLOCK_CONTROL(n) \
.mclk = MCLK_TC##n, \
.mclk_mask = MCLK_TC##n##_MASK, \
.gclk_id = TC##n##_GCLK_ID,
#define COUNTER_SAM0_TC32_CLOCK_CONTROL(n) \
.mclk = (volatile uint32_t *)MCLK_MASK_DT_INT_REG_ADDR(n), \
.mclk_mask = BIT(DT_INST_CLOCKS_CELL_BY_NAME(n, mclk, bit)), \
.gclk_id = DT_INST_CLOCKS_CELL_BY_NAME(n, gclk, periph_ch),
#else
#define COUNTER_SAM0_TC32_CLOCK_CONTROL(n) \
.pm_apbcmask = PM_APBCMASK_TC##n, \
.gclk_clkctrl_id = UTIL_CAT(GCLK_CLKCTRL_ID_TC ## n ## _TC, \
UTIL_INC(n)),
#define COUNTER_SAM0_TC32_CLOCK_CONTROL(n) \
.pm_apbcmask = BIT(DT_INST_CLOCKS_CELL_BY_NAME(n, pm, bit)), \
.gclk_clkctrl_id = DT_INST_CLOCKS_CELL_BY_NAME(n, gclk, clkctrl_id),
#endif
#define COUNTER_SAM0_TC32_DEVICE(n) \
static void counter_sam0_tc32_config_##n(struct device *dev); \
static const struct counter_sam0_tc32_config \
counter_sam0_tc32_dev_config_##n = { \
.info = { \
.max_top_value = UINT32_MAX, \
.freq = SOC_ATMEL_SAM0_GCLK0_FREQ_HZ / \
CONFIG_COUNTER_SAM0_TC32_##n##_DIVISOR, \
.flags = COUNTER_CONFIG_INFO_COUNT_UP, \
.channels = 1 \
}, \
.regs = (TcCount32 *)DT_ATMEL_SAM0_TC32_TC_##n##_BASE_ADDRESS,\
COUNTER_SAM0_TC32_CLOCK_CONTROL(n) \
.prescaler = UTIL_CAT(TC_CTRLA_PRESCALER_DIV, \
CONFIG_COUNTER_SAM0_TC32_##n##_DIVISOR), \
.irq_config_func = &counter_sam0_tc32_config_##n, \
}; \
static struct counter_sam0_tc32_data counter_sam0_tc32_dev_data_##n; \
DEVICE_AND_API_INIT(counter_sam0_tc32_##n, \
DT_ATMEL_SAM0_TC32_TC_##n##_LABEL, \
&counter_sam0_tc32_initialize, \
&counter_sam0_tc32_dev_data_##n, \
&counter_sam0_tc32_dev_config_##n, PRE_KERNEL_1, \
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
&counter_sam0_tc32_driver_api); \
static void counter_sam0_tc32_config_##n(struct device *dev) \
{ \
IRQ_CONNECT(DT_ATMEL_SAM0_TC32_TC_##n##_IRQ_0, \
DT_ATMEL_SAM0_TC32_TC_##n##_IRQ_0_PRIORITY, \
counter_sam0_tc32_isr, \
DEVICE_GET(counter_sam0_tc32_##n), \
0); \
irq_enable(DT_ATMEL_SAM0_TC32_TC_##n##_IRQ_0); \
#define SAM0_TC32_PRESCALER(n) \
COND_CODE_1(DT_INST_NODE_HAS_PROP(n, prescaler), \
(DT_INST_PROP(n, prescaler)), (1))
#define COUNTER_SAM0_TC32_DEVICE(n) \
static void counter_sam0_tc32_config_##n(struct device *dev); \
static const struct counter_sam0_tc32_config \
\
counter_sam0_tc32_dev_config_##n = { \
.info = { \
.max_top_value = UINT32_MAX, \
.freq = SOC_ATMEL_SAM0_GCLK0_FREQ_HZ / \
SAM0_TC32_PRESCALER(n), \
.flags = COUNTER_CONFIG_INFO_COUNT_UP, \
.channels = 1 \
}, \
.regs = (TcCount32 *)DT_INST_REG_ADDR(n), \
COUNTER_SAM0_TC32_CLOCK_CONTROL(n) \
.prescaler = UTIL_CAT(TC_CTRLA_PRESCALER_DIV, \
SAM0_TC32_PRESCALER(n)), \
.irq_config_func = &counter_sam0_tc32_config_##n, \
}; \
\
static struct counter_sam0_tc32_data counter_sam0_tc32_dev_data_##n;\
\
DEVICE_AND_API_INIT(counter_sam0_tc32_##n, \
DT_INST_LABEL(n), \
&counter_sam0_tc32_initialize, \
&counter_sam0_tc32_dev_data_##n, \
&counter_sam0_tc32_dev_config_##n, \
PRE_KERNEL_1, \
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
&counter_sam0_tc32_driver_api); \
\
static void counter_sam0_tc32_config_##n(struct device *dev) \
{ \
IRQ_CONNECT(DT_INST_IRQN(n), \
DT_INST_IRQ(n, priority), \
counter_sam0_tc32_isr, \
DEVICE_GET(counter_sam0_tc32_##n), 0); \
irq_enable(DT_INST_IRQN(n)); \
}
#if DT_ATMEL_SAM0_TC32_TC_0_BASE_ADDRESS
COUNTER_SAM0_TC32_DEVICE(0);
#endif
#if DT_ATMEL_SAM0_TC32_TC_2_BASE_ADDRESS
COUNTER_SAM0_TC32_DEVICE(2);
#endif
#if DT_ATMEL_SAM0_TC32_TC_4_BASE_ADDRESS
COUNTER_SAM0_TC32_DEVICE(4);
#endif
#if DT_ATMEL_SAM0_TC32_TC_6_BASE_ADDRESS
COUNTER_SAM0_TC32_DEVICE(6);
#endif
DT_INST_FOREACH(COUNTER_SAM0_TC32_DEVICE)

View file

@ -22,3 +22,17 @@ properties:
label:
required: true
prescaler:
type: int
required: false
description: Timer prescaler
enum:
- 1
- 2
- 4
- 8
- 16
- 64
- 256
- 1024