drivers/mfd/stm32-lptimer.c

Source file repositories/reference/linux-study-clean/drivers/mfd/stm32-lptimer.c

File Facts

System
Linux kernel
Corpus path
drivers/mfd/stm32-lptimer.c
Extension
.c
Size
3266 bytes
Lines
134
Domain
Driver Families
Bucket
drivers/mfd
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.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/*
 * STM32 Low-Power Timer parent driver.
 * Copyright (C) STMicroelectronics 2017
 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
 * Inspired by Benjamin Gaignard's stm32-timers driver
 */

#include <linux/bitfield.h>
#include <linux/mfd/stm32-lptimer.h>
#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>

#define STM32_LPTIM_MAX_REGISTER	0x3fc

static const struct regmap_config stm32_lptimer_regmap_cfg = {
	.reg_bits = 32,
	.val_bits = 32,
	.reg_stride = sizeof(u32),
	.max_register = STM32_LPTIM_MAX_REGISTER,
};

static int stm32_lptimer_detect_encoder(struct stm32_lptimer *ddata)
{
	u32 val;
	int ret;

	/*
	 * Quadrature encoder mode bit can only be written and read back when
	 * Low-Power Timer supports it.
	 */
	ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
				 STM32_LPTIM_ENC, STM32_LPTIM_ENC);
	if (ret)
		return ret;

	ret = regmap_read(ddata->regmap, STM32_LPTIM_CFGR, &val);
	if (ret)
		return ret;

	ret = regmap_update_bits(ddata->regmap, STM32_LPTIM_CFGR,
				 STM32_LPTIM_ENC, 0);
	if (ret)
		return ret;

	ddata->has_encoder = !!(val & STM32_LPTIM_ENC);

	return 0;
}

static int stm32_lptimer_detect_hwcfgr(struct stm32_lptimer *ddata)
{
	u32 val;
	int ret;

	ret = regmap_read(ddata->regmap, STM32_LPTIM_VERR, &ddata->version);
	if (ret)
		return ret;

	/* Try to guess parameters from HWCFGR: e.g. encoder mode (STM32MP15) */
	ret = regmap_read(ddata->regmap, STM32_LPTIM_HWCFGR1, &val);
	if (ret)
		return ret;

	/* Fallback to legacy init if HWCFGR isn't present */
	if (!val)
		return stm32_lptimer_detect_encoder(ddata);

	ddata->has_encoder = FIELD_GET(STM32_LPTIM_HWCFGR1_ENCODER, val);

	ret = regmap_read(ddata->regmap, STM32_LPTIM_HWCFGR2, &val);
	if (ret)
		return ret;

	/* Number of capture/compare channels */
	ddata->num_cc_chans = FIELD_GET(STM32_LPTIM_HWCFGR2_CHAN_NUM, val);

	return 0;
}

static int stm32_lptimer_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct stm32_lptimer *ddata;
	void __iomem *mmio;
	int ret;

	ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
	if (!ddata)

Annotation

Implementation Notes