|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 ITE Technology Corporation. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT ite_it51xxx_tach |
| 8 | + |
| 9 | +/** |
| 10 | + * @file |
| 11 | + * @brief ITE it51xxx tachometer sensor module driver |
| 12 | + * |
| 13 | + * This file contains a driver for the tachometer sensor module which contains |
| 14 | + * three independent counters (T0L/MR, T1L/MR and T2L/MR). The content of the |
| 15 | + * Tachometer Reading Register is still update based on the sampling counter |
| 16 | + * that samples the tachometer input (T0A, T0B, T1A, T1B, T2A or T2B pins). |
| 17 | + * The following is block diagram of this module: |
| 18 | + * |
| 19 | + * Sample Rate = TACH_FREQ / 128 |
| 20 | + * | |
| 21 | + * | Tachometer 0 | T0A (GPD6) |
| 22 | + * | | | +-----------+ | |
| 23 | + * | +-----+-----+ | | _ _ |<--+ |
| 24 | + * |------>| T0L/MR |<-+-| | |_| |_ |<--+ |
| 25 | + * | +-----------+ +-----------+ | |
| 26 | + * | capture pulses T0B (GPC6) |
| 27 | + * | in sample rate |
| 28 | + * | period |
| 29 | + * | |
| 30 | + * | Sample Rate = TACH_FREQ / 128 |
| 31 | + * +-----------+ | | |
| 32 | + * Crystal-->| Prescaler |--->| Tachometer 1 | T1A (GPD7) |
| 33 | + * 32.768k +-----------+ | | | +-----------+ | |
| 34 | + * | +-----+-----+ | | _ _ |<--+ |
| 35 | + * |------>| T1L/MR |<-+-| | |_| |_ |<--+ |
| 36 | + * | +-----------+ +-----------+ | |
| 37 | + * | capture pulses T1B (GPJ6) |
| 38 | + * | in sample rate |
| 39 | + * | period |
| 40 | + * | |
| 41 | + * | Sample Rate = TACH_FREQ / 128 |
| 42 | + * | | |
| 43 | + * | Tachometer 2 | T2A (GPJ0) |
| 44 | + * | | | +-----------+ | |
| 45 | + * | +-----+-----+ | | _ _ |<--+ |
| 46 | + * |------>| T2L/MR |<-+-| | |_| |_ |<--+ |
| 47 | + * | +-----------+ +-----------+ | |
| 48 | + * | capture pulses T2B (GPJ1) |
| 49 | + * | in sample rate |
| 50 | + * | period |
| 51 | + * | |
| 52 | + * |
| 53 | + * |
| 54 | + * Based on the counter value, we can compute the current RPM of external signal |
| 55 | + * from encoders. |
| 56 | + */ |
| 57 | + |
| 58 | +#include <zephyr/device.h> |
| 59 | +#include <zephyr/drivers/pinctrl.h> |
| 60 | +#include <zephyr/drivers/sensor.h> |
| 61 | +#include <zephyr/dt-bindings/sensor/it51xxx_tach.h> |
| 62 | +#include <errno.h> |
| 63 | +#include <soc.h> |
| 64 | +#include <soc_dt.h> |
| 65 | + |
| 66 | +#include <zephyr/logging/log.h> |
| 67 | +LOG_MODULE_REGISTER(tach_ite_it51xxx, CONFIG_SENSOR_LOG_LEVEL); |
| 68 | + |
| 69 | +#define TACH_FREQ IT51XXX_EC_FREQ |
| 70 | + |
| 71 | +/* 0xC0/0xD0/0xE0: Tach channel 0~2 tachometer speed (2 bytes value) */ |
| 72 | +#define REG_TACH_CH 0x00 |
| 73 | +/* 0xC6/0xD6/0xE6: Tach channel 0~2 control 1 */ |
| 74 | +#define REG_TACH_CH_CTRL1 0x06 |
| 75 | +#define TACH_CH_DVS BIT(1) |
| 76 | +#define TACH_CH_SEL BIT(0) |
| 77 | + |
| 78 | +struct tach_it51xxx_config { |
| 79 | + /* Tach channel register base address */ |
| 80 | + uintptr_t base; |
| 81 | + /* Tachometer alternate configuration */ |
| 82 | + const struct pinctrl_dev_config *pcfg; |
| 83 | + /* Select input pin to tachometer */ |
| 84 | + int input_pin; |
| 85 | + /* Number of pulses per round of tachometer's input */ |
| 86 | + int pulses_per_round; |
| 87 | +}; |
| 88 | + |
| 89 | +struct tach_it51xxx_data { |
| 90 | + /* Captured counts of tachometer */ |
| 91 | + uint16_t capture; |
| 92 | +}; |
| 93 | + |
| 94 | +static bool tach_ch_is_valid(const struct device *dev, int input_pin) |
| 95 | +{ |
| 96 | + const struct tach_it51xxx_config *const config = dev->config; |
| 97 | + const uintptr_t base = config->base; |
| 98 | + uint8_t reg_val; |
| 99 | + |
| 100 | + if (input_pin > IT51XXX_TACH_INPUT_PIN_B) { |
| 101 | + LOG_ERR("Tach input pin %d invalid, only support 0(A) or 1(B)", input_pin); |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + reg_val = sys_read8(base + REG_TACH_CH_CTRL1); |
| 106 | + if (((reg_val & TACH_CH_SEL) == input_pin) && (reg_val & TACH_CH_DVS)) { |
| 107 | + /* Input pin match register setting and tachometer data valid */ |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + return false; |
| 112 | +} |
| 113 | + |
| 114 | +static int tach_it51xxx_sample_fetch(const struct device *dev, enum sensor_channel chan) |
| 115 | +{ |
| 116 | + const struct tach_it51xxx_config *const config = dev->config; |
| 117 | + const uintptr_t base = config->base; |
| 118 | + int input_pin = config->input_pin; |
| 119 | + struct tach_it51xxx_data *const data = dev->data; |
| 120 | + uint8_t reg_val; |
| 121 | + |
| 122 | + if ((chan != SENSOR_CHAN_RPM) && (chan != SENSOR_CHAN_ALL)) { |
| 123 | + return -ENOTSUP; |
| 124 | + } |
| 125 | + |
| 126 | + if (tach_ch_is_valid(dev, input_pin)) { |
| 127 | + /* If tachometer data is valid, then save it */ |
| 128 | + data->capture = sys_read16(base + REG_TACH_CH); |
| 129 | + /* Clear tachometer data valid status */ |
| 130 | + reg_val = sys_read8(base + REG_TACH_CH_CTRL1); |
| 131 | + sys_write8(reg_val | TACH_CH_DVS, base + REG_TACH_CH_CTRL1); |
| 132 | + } else { |
| 133 | + /* If tachometer data isn't valid, then clear it */ |
| 134 | + data->capture = 0; |
| 135 | + } |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
| 139 | + |
| 140 | +static int tach_it51xxx_channel_get(const struct device *dev, enum sensor_channel chan, |
| 141 | + struct sensor_value *val) |
| 142 | +{ |
| 143 | + const struct tach_it51xxx_config *const config = dev->config; |
| 144 | + int p = config->pulses_per_round; |
| 145 | + struct tach_it51xxx_data *const data = dev->data; |
| 146 | + |
| 147 | + __ASSERT(p > 0, "pulses_per_round must be bigger than 0"); |
| 148 | + |
| 149 | + if (chan != SENSOR_CHAN_RPM) { |
| 150 | + LOG_ERR("Sensor chan %d, only support SENSOR_CHAN_RPM", chan); |
| 151 | + return -ENOTSUP; |
| 152 | + } |
| 153 | + |
| 154 | + /* Transform count unit to RPM */ |
| 155 | + if (data->capture > 0) { |
| 156 | + /* |
| 157 | + * Fan Speed (RPM) = 60 / (1/fs * {TACH_CH_(H & L)} * P) |
| 158 | + * - P denotes the numbers of pulses per round |
| 159 | + * - {TACH_CH_(H & L)} = 0000h denotes Fan Speed is zero |
| 160 | + * - The sampling rate (fs) is TACH_FREQ / 128 |
| 161 | + */ |
| 162 | + val->val1 = (60 * TACH_FREQ / 128 / p / (data->capture)); |
| 163 | + } else { |
| 164 | + val->val1 = 0U; |
| 165 | + } |
| 166 | + |
| 167 | + val->val2 = 0U; |
| 168 | + |
| 169 | + return 0; |
| 170 | +} |
| 171 | + |
| 172 | +static int tach_it51xxx_init(const struct device *dev) |
| 173 | +{ |
| 174 | + const struct tach_it51xxx_config *const config = dev->config; |
| 175 | + const uintptr_t base = config->base; |
| 176 | + int input_pin = config->input_pin; |
| 177 | + int status; |
| 178 | + uint8_t reg_val; |
| 179 | + |
| 180 | + if (input_pin > IT51XXX_TACH_INPUT_PIN_B) { |
| 181 | + LOG_ERR("Tach input pin %d invalid, only support 0(A) or 1(B)", input_pin); |
| 182 | + return -EINVAL; |
| 183 | + } |
| 184 | + |
| 185 | + /* Select input pin to tachometer alternate mode */ |
| 186 | + status = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); |
| 187 | + if (status < 0) { |
| 188 | + LOG_ERR("Failed to configure TACH pins"); |
| 189 | + return status; |
| 190 | + } |
| 191 | + |
| 192 | + reg_val = sys_read8(base + REG_TACH_CH_CTRL1); |
| 193 | + if (input_pin == IT51XXX_TACH_INPUT_PIN_A) { |
| 194 | + /* Select TACH_INPUT_PIN_A to tachometer */ |
| 195 | + reg_val &= ~TACH_CH_SEL; |
| 196 | + } else { |
| 197 | + /* Select TACH_INPUT_PIN_B to tachometer */ |
| 198 | + reg_val |= TACH_CH_SEL; |
| 199 | + } |
| 200 | + /* Clear tachometer data valid status */ |
| 201 | + sys_write8(reg_val | TACH_CH_DVS, base + REG_TACH_CH_CTRL1); |
| 202 | + |
| 203 | + /* Tachometer sensor already start */ |
| 204 | + return 0; |
| 205 | +} |
| 206 | + |
| 207 | +static DEVICE_API(sensor, tach_it51xxx_driver_api) = { |
| 208 | + .sample_fetch = tach_it51xxx_sample_fetch, |
| 209 | + .channel_get = tach_it51xxx_channel_get, |
| 210 | +}; |
| 211 | + |
| 212 | +#define TACH_IT51XXX_INIT(inst) \ |
| 213 | + PINCTRL_DT_INST_DEFINE(inst); \ |
| 214 | + \ |
| 215 | + static const struct tach_it51xxx_config tach_it51xxx_cfg_##inst = { \ |
| 216 | + .base = DT_INST_REG_ADDR(inst), \ |
| 217 | + .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \ |
| 218 | + .input_pin = DT_INST_PROP(inst, input_pin), \ |
| 219 | + .pulses_per_round = DT_INST_PROP(inst, pulses_per_round), \ |
| 220 | + }; \ |
| 221 | + \ |
| 222 | + static struct tach_it51xxx_data tach_it51xxx_data_##inst; \ |
| 223 | + \ |
| 224 | + SENSOR_DEVICE_DT_INST_DEFINE(inst, tach_it51xxx_init, NULL, &tach_it51xxx_data_##inst, \ |
| 225 | + &tach_it51xxx_cfg_##inst, POST_KERNEL, \ |
| 226 | + CONFIG_SENSOR_INIT_PRIORITY, &tach_it51xxx_driver_api); |
| 227 | + |
| 228 | +DT_INST_FOREACH_STATUS_OKAY(TACH_IT51XXX_INIT) |
0 commit comments