18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2018 NXP
48c2ecf20Sopenharmony_ci *	Dong Aisheng <aisheng.dong@nxp.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/bits.h>
88c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
98c2ecf20Sopenharmony_ci#include <linux/err.h>
108c2ecf20Sopenharmony_ci#include <linux/io.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "clk-scu.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(imx_lpcg_scu_lock);
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define CLK_GATE_SCU_LPCG_MASK		0x3
198c2ecf20Sopenharmony_ci#define CLK_GATE_SCU_LPCG_HW_SEL	BIT(0)
208c2ecf20Sopenharmony_ci#define CLK_GATE_SCU_LPCG_SW_SEL	BIT(1)
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/*
238c2ecf20Sopenharmony_ci * struct clk_lpcg_scu - Description of LPCG clock
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * @hw: clk_hw of this LPCG
268c2ecf20Sopenharmony_ci * @reg: register of this LPCG clock
278c2ecf20Sopenharmony_ci * @bit_idx: bit index of this LPCG clock
288c2ecf20Sopenharmony_ci * @hw_gate: HW auto gate enable
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * This structure describes one LPCG clock
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_cistruct clk_lpcg_scu {
338c2ecf20Sopenharmony_ci	struct clk_hw hw;
348c2ecf20Sopenharmony_ci	void __iomem *reg;
358c2ecf20Sopenharmony_ci	u8 bit_idx;
368c2ecf20Sopenharmony_ci	bool hw_gate;
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define to_clk_lpcg_scu(_hw) container_of(_hw, struct clk_lpcg_scu, hw)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic int clk_lpcg_scu_enable(struct clk_hw *hw)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	struct clk_lpcg_scu *clk = to_clk_lpcg_scu(hw);
448c2ecf20Sopenharmony_ci	unsigned long flags;
458c2ecf20Sopenharmony_ci	u32 reg, val;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	spin_lock_irqsave(&imx_lpcg_scu_lock, flags);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	reg = readl_relaxed(clk->reg);
508c2ecf20Sopenharmony_ci	reg &= ~(CLK_GATE_SCU_LPCG_MASK << clk->bit_idx);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	val = CLK_GATE_SCU_LPCG_SW_SEL;
538c2ecf20Sopenharmony_ci	if (clk->hw_gate)
548c2ecf20Sopenharmony_ci		val |= CLK_GATE_SCU_LPCG_HW_SEL;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	reg |= val << clk->bit_idx;
578c2ecf20Sopenharmony_ci	writel(reg, clk->reg);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&imx_lpcg_scu_lock, flags);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	return 0;
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic void clk_lpcg_scu_disable(struct clk_hw *hw)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	struct clk_lpcg_scu *clk = to_clk_lpcg_scu(hw);
678c2ecf20Sopenharmony_ci	unsigned long flags;
688c2ecf20Sopenharmony_ci	u32 reg;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	spin_lock_irqsave(&imx_lpcg_scu_lock, flags);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	reg = readl_relaxed(clk->reg);
738c2ecf20Sopenharmony_ci	reg &= ~(CLK_GATE_SCU_LPCG_MASK << clk->bit_idx);
748c2ecf20Sopenharmony_ci	writel(reg, clk->reg);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&imx_lpcg_scu_lock, flags);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic const struct clk_ops clk_lpcg_scu_ops = {
808c2ecf20Sopenharmony_ci	.enable = clk_lpcg_scu_enable,
818c2ecf20Sopenharmony_ci	.disable = clk_lpcg_scu_disable,
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistruct clk_hw *imx_clk_lpcg_scu(const char *name, const char *parent_name,
858c2ecf20Sopenharmony_ci				unsigned long flags, void __iomem *reg,
868c2ecf20Sopenharmony_ci				u8 bit_idx, bool hw_gate)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct clk_lpcg_scu *clk;
898c2ecf20Sopenharmony_ci	struct clk_init_data init;
908c2ecf20Sopenharmony_ci	struct clk_hw *hw;
918c2ecf20Sopenharmony_ci	int ret;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	clk = kzalloc(sizeof(*clk), GFP_KERNEL);
948c2ecf20Sopenharmony_ci	if (!clk)
958c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	clk->reg = reg;
988c2ecf20Sopenharmony_ci	clk->bit_idx = bit_idx;
998c2ecf20Sopenharmony_ci	clk->hw_gate = hw_gate;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	init.name = name;
1028c2ecf20Sopenharmony_ci	init.ops = &clk_lpcg_scu_ops;
1038c2ecf20Sopenharmony_ci	init.flags = CLK_SET_RATE_PARENT | flags;
1048c2ecf20Sopenharmony_ci	init.parent_names = parent_name ? &parent_name : NULL;
1058c2ecf20Sopenharmony_ci	init.num_parents = parent_name ? 1 : 0;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	clk->hw.init = &init;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	hw = &clk->hw;
1108c2ecf20Sopenharmony_ci	ret = clk_hw_register(NULL, hw);
1118c2ecf20Sopenharmony_ci	if (ret) {
1128c2ecf20Sopenharmony_ci		kfree(clk);
1138c2ecf20Sopenharmony_ci		hw = ERR_PTR(ret);
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	return hw;
1178c2ecf20Sopenharmony_ci}
118