drivers/counter/stm32-timer-cnt.c
Source file repositories/reference/linux-study-clean/drivers/counter/stm32-timer-cnt.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/counter/stm32-timer-cnt.c- Extension
.c- Size
- 23668 bytes
- Lines
- 871
- Domain
- Driver Families
- Bucket
- drivers/counter
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/counter.hlinux/interrupt.hlinux/mfd/stm32-timers.hlinux/mod_devicetable.hlinux/module.hlinux/of.hlinux/pinctrl/consumer.hlinux/platform_device.hlinux/types.h
Detected Declarations
struct stm32_timer_regsstruct stm32_timer_cntstruct stm32_count_cc_regsfunction stm32_count_readfunction stm32_count_writefunction stm32_count_function_readfunction stm32_count_function_writefunction stm32_count_direction_readfunction stm32_count_ceiling_readfunction stm32_count_ceiling_writefunction stm32_count_enable_readfunction stm32_count_enable_writefunction stm32_count_prescaler_readfunction stm32_count_prescaler_writefunction stm32_count_cap_readfunction stm32_count_nb_ovf_readfunction stm32_count_nb_ovf_writefunction stm32_action_readfunction stm32_count_capture_configurefunction stm32_count_events_configurefunction list_for_each_entryfunction stm32_count_watch_validatefunction stm32_count_clk_get_freqfunction stm32_timer_cnt_isrfunction stm32_timer_cnt_detect_channelsfunction stm32_timer_cnt_probe_encoderfunction stm32_timer_cnt_probefunction stm32_timer_cnt_suspendfunction stm32_timer_cnt_resume
Annotated Snippet
struct stm32_timer_regs {
u32 cr1;
u32 cnt;
u32 smcr;
u32 arr;
};
struct stm32_timer_cnt {
struct regmap *regmap;
struct clk *clk;
u32 max_arr;
bool enabled;
struct stm32_timer_regs bak;
bool has_encoder;
unsigned int nchannels;
unsigned int nr_irqs;
spinlock_t lock; /* protects nb_ovf */
u64 nb_ovf;
};
static const enum counter_function stm32_count_functions[] = {
COUNTER_FUNCTION_INCREASE,
COUNTER_FUNCTION_QUADRATURE_X2_A,
COUNTER_FUNCTION_QUADRATURE_X2_B,
COUNTER_FUNCTION_QUADRATURE_X4,
};
static int stm32_count_read(struct counter_device *counter,
struct counter_count *count, u64 *val)
{
struct stm32_timer_cnt *const priv = counter_priv(counter);
u32 cnt;
regmap_read(priv->regmap, TIM_CNT, &cnt);
*val = cnt;
return 0;
}
static int stm32_count_write(struct counter_device *counter,
struct counter_count *count, const u64 val)
{
struct stm32_timer_cnt *const priv = counter_priv(counter);
u32 ceiling;
regmap_read(priv->regmap, TIM_ARR, &ceiling);
if (val > ceiling)
return -EINVAL;
return regmap_write(priv->regmap, TIM_CNT, val);
}
static int stm32_count_function_read(struct counter_device *counter,
struct counter_count *count,
enum counter_function *function)
{
struct stm32_timer_cnt *const priv = counter_priv(counter);
u32 smcr;
regmap_read(priv->regmap, TIM_SMCR, &smcr);
switch (smcr & TIM_SMCR_SMS) {
case TIM_SMCR_SMS_SLAVE_MODE_DISABLED:
*function = COUNTER_FUNCTION_INCREASE;
return 0;
case TIM_SMCR_SMS_ENCODER_MODE_1:
*function = COUNTER_FUNCTION_QUADRATURE_X2_A;
return 0;
case TIM_SMCR_SMS_ENCODER_MODE_2:
*function = COUNTER_FUNCTION_QUADRATURE_X2_B;
return 0;
case TIM_SMCR_SMS_ENCODER_MODE_3:
*function = COUNTER_FUNCTION_QUADRATURE_X4;
return 0;
default:
return -EINVAL;
}
}
static int stm32_count_function_write(struct counter_device *counter,
struct counter_count *count,
enum counter_function function)
{
struct stm32_timer_cnt *const priv = counter_priv(counter);
u32 cr1, sms;
switch (function) {
case COUNTER_FUNCTION_INCREASE:
sms = TIM_SMCR_SMS_SLAVE_MODE_DISABLED;
break;
Annotation
- Immediate include surface: `linux/counter.h`, `linux/interrupt.h`, `linux/mfd/stm32-timers.h`, `linux/mod_devicetable.h`, `linux/module.h`, `linux/of.h`, `linux/pinctrl/consumer.h`, `linux/platform_device.h`.
- Detected declarations: `struct stm32_timer_regs`, `struct stm32_timer_cnt`, `struct stm32_count_cc_regs`, `function stm32_count_read`, `function stm32_count_write`, `function stm32_count_function_read`, `function stm32_count_function_write`, `function stm32_count_direction_read`, `function stm32_count_ceiling_read`, `function stm32_count_ceiling_write`.
- Atlas domain: Driver Families / drivers/counter.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.