Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | /*
* Copyright (c) 2021 Nuvoton Technology Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nuvoton_npcx_tach
/**
* @file
* @brief Nuvoton NPCX tachometer sensor module driver
*
* This file contains a driver for the tachometer sensor module which contains
* two independent timers (counter 1 and 2). They are used to capture a counter
* value when the signals via external pins match the condition. The following
* is block diagram of this module when it set to mode 5.
*
* | Capture A
* | | +-----------+ TA Pin
* +-----------+ | +-----+-----+ | _ _ | |
* APB_CLK-->| Prescaler |--->|---+--->| Counter 1 |<--| _| |_| |_ |<--+
* +-----------+ | | +-----------+ +-----------+
* | CLK_SEL Edge Detection
* | Capture B
* LFCLK--------------------->| | +-----------+ TB Pin
* | +-----+-----+ | _ _ | |
* |---+--->| Counter 2 |<--| _| |_| |_ |<--+
* | | +-----------+ +-----------+
* | CLK_SEL Edge Detection
* |
* | TACH_CLK
* +----------
* (NPCX Tachometer Mode 5, Dual-Independent Input Capture)
*
* This mode is used to measure either the frequency of two external clocks
* (via TA or TB pins) that are slower than TACH_CLK. A transition event (rising
* or falling edge) received on TAn/TBn pin causes a transfer of timer 1/2
* contents to Capture register and reload counter. Based on this value, we can
* compute the current RPM of external signal from encoders.
*/
#include <errno.h>
#include <device.h>
#include <drivers/clock_control.h>
#include <drivers/sensor.h>
#include <dt-bindings/sensor/npcx_tach.h>
#include <soc.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(tach_npcx, CONFIG_SENSOR_LOG_LEVEL);
/* Device config */
struct tach_npcx_config {
/* tachometer controller base address */
uintptr_t base;
/* clock configuration */
struct npcx_clk_cfg clk_cfg;
/* pinmux configuration */
const uint8_t alts_size;
const struct npcx_alt *alts_list;
/* sampling clock frequency of tachometer */
uint32_t sample_clk;
/* selected port of tachometer */
int port;
/* number of pulses (holes) per round of tachometer's input (encoder) */
int pulses_per_round;
};
/* Driver data */
struct tach_npcx_data {
/* Input clock for tachometers */
uint32_t input_clk;
/* Captured counts of tachometer */
uint32_t capture;
};
/* Driver convenience defines */
#define DRV_CONFIG(dev) ((const struct tach_npcx_config *)(dev)->config)
#define DRV_DATA(dev) ((struct tach_npcx_data *)(dev)->data)
#define HAL_INSTANCE(dev) (struct tach_reg *)(DRV_CONFIG(dev)->base)
/* Maximum count of prescaler */
#define NPCX_TACHO_PRSC_MAX 0xff
/* Maximum count of counter */
#define NPCX_TACHO_CNT_MAX 0xffff
/* Operation mode used for tachometer */
#define NPCX_TACH_MDSEL 4
/* Clock selection for tachometer */
#define NPCX_CLKSEL_APBCLK 1
#define NPCX_CLKSEL_LFCLK 4
/* TACH inline local functions */
static inline void tach_npcx_start_port_a(const struct device *dev)
{
struct tach_npcx_data *const data = DRV_DATA(dev);
struct tach_reg *const inst = HAL_INSTANCE(dev);
/* Set the default value of counter and capture register of timer 1. */
inst->TCNT1 = NPCX_TACHO_CNT_MAX;
inst->TCRA = NPCX_TACHO_CNT_MAX;
/*
* Set the edge detection polarity of port A to falling (high-to-low
* transition) and enable the functionality to capture TCNT1 into TCRA
* and preset TCNT1 when event is triggered.
*/
inst->TMCTRL |= BIT(NPCX_TMCTRL_TAEN);
/* Enable input debounce logic into TA pin. */
inst->TCFG |= BIT(NPCX_TCFG_TADBEN);
/* Select clock source of timer 1 from no clock and start to count. */
SET_FIELD(inst->TCKC, NPCX_TCKC_C1CSEL_FIELD, data->input_clk == LFCLK
? NPCX_CLKSEL_LFCLK : NPCX_CLKSEL_APBCLK);
}
static inline void tach_npcx_start_port_b(const struct device *dev)
{
struct tach_reg *const inst = HAL_INSTANCE(dev);
struct tach_npcx_data *const data = DRV_DATA(dev);
/* Set the default value of counter and capture register of timer 2. */
inst->TCNT2 = NPCX_TACHO_CNT_MAX;
inst->TCRB = NPCX_TACHO_CNT_MAX;
/*
* Set the edge detection polarity of port B to falling (high-to-low
* transition) and enable the functionality to capture TCNT2 into TCRB
* and preset TCNT2 when event is triggered.
*/
inst->TMCTRL |= BIT(NPCX_TMCTRL_TBEN);
/* Enable input debounce logic into TB pin. */
inst->TCFG |= BIT(NPCX_TCFG_TBDBEN);
/* Select clock source of timer 2 from no clock and start to count. */
SET_FIELD(inst->TCKC, NPCX_TCKC_C2CSEL_FIELD, data->input_clk == LFCLK
? NPCX_CLKSEL_LFCLK : NPCX_CLKSEL_APBCLK);
}
static inline bool tach_npcx_is_underflow(const struct device *dev)
{
const struct tach_npcx_config *const config = DRV_CONFIG(dev);
struct tach_reg *const inst = HAL_INSTANCE(dev);
LOG_DBG("port A is underflow %d, port b is underflow %d",
IS_BIT_SET(inst->TECTRL, NPCX_TECTRL_TCPND),
IS_BIT_SET(inst->TECTRL, NPCX_TECTRL_TDPND));
/*
* In mode 5, the flag TCPND or TDPND indicates the TCNT1 or TCNT2
* is under underflow situation. (No edges are detected.)
*/
if (config->port == NPCX_TACH_PORT_A) {
return IS_BIT_SET(inst->TECTRL, NPCX_TECTRL_TCPND);
} else {
return IS_BIT_SET(inst->TECTRL, NPCX_TECTRL_TDPND);
}
}
static inline void tach_npcx_clear_underflow_flag(const struct device *dev)
{
const struct tach_npcx_config *const config = DRV_CONFIG(dev);
struct tach_reg *const inst = HAL_INSTANCE(dev);
if (config->port == NPCX_TACH_PORT_A) {
inst->TECLR = BIT(NPCX_TECLR_TCCLR);
} else {
inst->TECLR = BIT(NPCX_TECLR_TDCLR);
}
}
static inline bool tach_npcx_is_captured(const struct device *dev)
{
const struct tach_npcx_config *const config = DRV_CONFIG(dev);
struct tach_reg *const inst = HAL_INSTANCE(dev);
LOG_DBG("port A is captured %d, port b is captured %d",
IS_BIT_SET(inst->TECTRL, NPCX_TECTRL_TAPND),
IS_BIT_SET(inst->TECTRL, NPCX_TECTRL_TAPND));
/*
* In mode 5, the flag TAPND or TBPND indicates a input captured on
* TAn or TBn transition.
*/
if (config->port == NPCX_TACH_PORT_A) {
return IS_BIT_SET(inst->TECTRL, NPCX_TECTRL_TAPND);
} else {
return IS_BIT_SET(inst->TECTRL, NPCX_TECTRL_TBPND);
}
}
static inline void tach_npcx_clear_captured_flag(const struct device *dev)
{
const struct tach_npcx_config *const config = DRV_CONFIG(dev);
struct tach_reg *const inst = HAL_INSTANCE(dev);
if (config->port == NPCX_TACH_PORT_A) {
inst->TECLR = BIT(NPCX_TECLR_TACLR);
} else {
inst->TECLR = BIT(NPCX_TECLR_TBCLR);
}
}
static inline uint16_t tach_npcx_get_captured_count(const struct device *dev)
{
const struct tach_npcx_config *const config = DRV_CONFIG(dev);
struct tach_reg *const inst = HAL_INSTANCE(dev);
if (config->port == NPCX_TACH_PORT_A) {
return inst->TCRA;
} else {
return inst->TCRB;
}
}
/* TACH local functions */
static int tach_npcx_configure(const struct device *dev)
{
const struct tach_npcx_config *const config = DRV_CONFIG(dev);
struct tach_npcx_data *const data = DRV_DATA(dev);
struct tach_reg *const inst = HAL_INSTANCE(dev);
/* Set mode 5 to tachometer module */
SET_FIELD(inst->TMCTRL, NPCX_TMCTRL_MDSEL_FIELD, NPCX_TACH_MDSEL);
/* Configure clock module and its frequency of tachometer */
if (config->sample_clk == 0) {
return -EINVAL;
} else if (data->input_clk == LFCLK) {
/* Enable low power mode */
inst->TCKC |= BIT(NPCX_TCKC_LOW_PWR);
if (config->sample_clk != data->input_clk) {
LOG_ERR("%s operate freq is %d not fixed to 32kHz",
dev->name, data->input_clk);
return -EINVAL;
}
} else {
/* Configure sampling freq by setting prescaler of APB1 */
uint16_t prescaler = data->input_clk / config->sample_clk;
if (data->input_clk > config->sample_clk) {
LOG_ERR("%s operate freq exceeds APB1 clock",
dev->name);
return -EINVAL;
}
inst->TPRSC = MIN(NPCX_TACHO_PRSC_MAX, MAX(prescaler, 1));
}
return 0;
}
/* TACH api functions */
int tach_npcx_sample_fetch(const struct device *dev, enum sensor_channel chan)
{
ARG_UNUSED(chan);
struct tach_npcx_data *const data = DRV_DATA(dev);
/* Check whether underflow flag of tachometer is occurred */
if (tach_npcx_is_underflow(dev)) {
/* Clear pending flags */
tach_npcx_clear_underflow_flag(dev);
LOG_INF("%s is underflow!", dev->name);
data->capture = 0;
return 0;
}
/* Check whether capture flag of tachometer is set */
if (tach_npcx_is_captured(dev)) {
/* Clear pending flags */
tach_npcx_clear_captured_flag(dev);
/* Save captured count */
data->capture = NPCX_TACHO_CNT_MAX -
tach_npcx_get_captured_count(dev);
}
return 0;
}
static int tach_npcx_channel_get(const struct device *dev,
enum sensor_channel chan,
struct sensor_value *val)
{
const struct tach_npcx_config *const config = DRV_CONFIG(dev);
struct tach_npcx_data *const data = DRV_DATA(dev);
if (chan != SENSOR_CHAN_RPM) {
return -ENOTSUP;
}
if (data->capture > 0) {
/*
* RPM = (f * 60) / (n * TACH)
* n = Pulses per round
* f = Tachometer operation freqency (Hz)
* TACH = Captured counts of tachometer
*/
val->val1 = (config->sample_clk * 60) /
(config->pulses_per_round * data->capture);
} else {
val->val1 = 0U;
}
val->val2 = 0U;
return 0;
}
/* TACH driver registration */
static int tach_npcx_init(const struct device *dev)
{
const struct tach_npcx_config *const config = DRV_CONFIG(dev);
struct tach_npcx_data *const data = DRV_DATA(dev);
const struct device *const clk_dev = DEVICE_DT_GET(NPCX_CLK_CTRL_NODE);
int ret;
/* Turn on device clock first and get source clock freq. */
ret = clock_control_on(clk_dev, (clock_control_subsys_t *)
&config->clk_cfg);
if (ret < 0) {
LOG_ERR("Turn on tachometer clock fail %d", ret);
return ret;
}
ret = clock_control_get_rate(clk_dev, (clock_control_subsys_t *)
&config->clk_cfg, &data->input_clk);
if (ret < 0) {
LOG_ERR("Get tachometer clock rate error %d", ret);
return ret;
}
/* Configure pin-mux for tachometer device */
npcx_pinctrl_mux_configure(config->alts_list, config->alts_size, 1);
/* Configure tachometer and its operate freq. */
ret = tach_npcx_configure(dev);
if (ret < 0) {
LOG_ERR("Config tachometer port %d failed", config->port);
return ret;
}
/* Start tachometer sensor */
if (config->port == NPCX_TACH_PORT_A) {
tach_npcx_start_port_a(dev);
} else if (config->port == NPCX_TACH_PORT_B) {
tach_npcx_start_port_b(dev);
} else {
return -EINVAL;
}
return 0;
}
static const struct sensor_driver_api tach_npcx_driver_api = {
.sample_fetch = tach_npcx_sample_fetch,
.channel_get = tach_npcx_channel_get,
};
#define NPCX_TACH_INIT(inst) \
static const struct npcx_alt tach_alts##inst[] = \
NPCX_DT_ALT_ITEMS_LIST(inst); \
\
static const struct tach_npcx_config tach_cfg_##inst = { \
.base = DT_INST_REG_ADDR(inst), \
.clk_cfg = NPCX_DT_CLK_CFG_ITEM(inst), \
.alts_size = ARRAY_SIZE(tach_alts##inst), \
.alts_list = tach_alts##inst, \
.sample_clk = DT_INST_PROP(inst, sample_clk), \
.port = DT_INST_PROP(inst, port), \
.pulses_per_round = DT_INST_PROP(inst, pulses_per_round), \
}; \
\
static struct tach_npcx_data tach_data_##inst; \
\
DEVICE_DT_INST_DEFINE(inst, \
tach_npcx_init, \
NULL, \
&tach_data_##inst, \
&tach_cfg_##inst, \
POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, \
&tach_npcx_driver_api);
DT_INST_FOREACH_STATUS_OKAY(NPCX_TACH_INIT)
|