drivers/hwspinlock/stm32_hwspinlock.c

Source file repositories/reference/linux-study-clean/drivers/hwspinlock/stm32_hwspinlock.c

File Facts

System
Linux kernel
Corpus path
drivers/hwspinlock/stm32_hwspinlock.c
Extension
.c
Size
4240 bytes
Lines
176
Domain
Driver Families
Bucket
drivers/hwspinlock
Inferred role
Driver Families: exported/initcall integration point
Status
integration 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

struct stm32_hwspinlock {
	struct clk *clk;
	struct hwspinlock_device bank;
};

static int stm32_hwspinlock_trylock(struct hwspinlock *lock)
{
	void __iomem *lock_addr = lock->priv;
	u32 status;

	writel(STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID, lock_addr);
	status = readl(lock_addr);

	return status == (STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID);
}

static void stm32_hwspinlock_unlock(struct hwspinlock *lock)
{
	void __iomem *lock_addr = lock->priv;

	writel(STM32_MUTEX_COREID, lock_addr);
}

static void stm32_hwspinlock_relax(struct hwspinlock *lock)
{
	ndelay(50);
}

static const struct hwspinlock_ops stm32_hwspinlock_ops = {
	.trylock	= stm32_hwspinlock_trylock,
	.unlock		= stm32_hwspinlock_unlock,
	.relax		= stm32_hwspinlock_relax,
};

static void stm32_hwspinlock_disable_clk(void *data)
{
	struct platform_device *pdev = data;
	struct stm32_hwspinlock *hw = platform_get_drvdata(pdev);
	struct device *dev = &pdev->dev;

	pm_runtime_get_sync(dev);
	pm_runtime_disable(dev);
	pm_runtime_set_suspended(dev);
	pm_runtime_put_noidle(dev);

	clk_disable_unprepare(hw->clk);
}

static int stm32_hwspinlock_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct stm32_hwspinlock *hw;
	void __iomem *io_base;
	int i, ret;

	io_base = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(io_base))
		return PTR_ERR(io_base);

	hw = devm_kzalloc(dev, struct_size(hw, bank.lock, STM32_MUTEX_NUM_LOCKS), GFP_KERNEL);
	if (!hw)
		return -ENOMEM;

	hw->clk = devm_clk_get(dev, "hsem");
	if (IS_ERR(hw->clk))
		return PTR_ERR(hw->clk);

	ret = clk_prepare_enable(hw->clk);
	if (ret) {
		dev_err(dev, "Failed to prepare_enable clock\n");
		return ret;
	}

	platform_set_drvdata(pdev, hw);

	pm_runtime_get_noresume(dev);
	pm_runtime_set_active(dev);
	pm_runtime_enable(dev);
	pm_runtime_put(dev);

	ret = devm_add_action_or_reset(dev, stm32_hwspinlock_disable_clk, pdev);
	if (ret) {
		dev_err(dev, "Failed to register action\n");
		return ret;
	}

	for (i = 0; i < STM32_MUTEX_NUM_LOCKS; i++)
		hw->bank.lock[i].priv = io_base + i * sizeof(u32);

	ret = devm_hwspin_lock_register(dev, &hw->bank, &stm32_hwspinlock_ops,

Annotation

Implementation Notes