18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2014 MediaTek Inc.
48c2ecf20Sopenharmony_ci * Author: James Liao <jamesjj.liao@mediatek.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/of.h>
88c2ecf20Sopenharmony_ci#include <linux/of_address.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/io.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/delay.h>
138c2ecf20Sopenharmony_ci#include <linux/clkdev.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "clk-mtk.h"
168c2ecf20Sopenharmony_ci#include "clk-gate.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistatic int mtk_cg_bit_is_cleared(struct clk_hw *hw)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
218c2ecf20Sopenharmony_ci	u32 val;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	regmap_read(cg->regmap, cg->sta_ofs, &val);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	val &= BIT(cg->bit);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	return val == 0;
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic int mtk_cg_bit_is_set(struct clk_hw *hw)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
338c2ecf20Sopenharmony_ci	u32 val;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	regmap_read(cg->regmap, cg->sta_ofs, &val);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	val &= BIT(cg->bit);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	return val != 0;
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic void mtk_cg_set_bit(struct clk_hw *hw)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	regmap_write(cg->regmap, cg->set_ofs, BIT(cg->bit));
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic void mtk_cg_clr_bit(struct clk_hw *hw)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	regmap_write(cg->regmap, cg->clr_ofs, BIT(cg->bit));
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic void mtk_cg_set_bit_no_setclr(struct clk_hw *hw)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
598c2ecf20Sopenharmony_ci	u32 cgbit = BIT(cg->bit);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	regmap_update_bits(cg->regmap, cg->sta_ofs, cgbit, cgbit);
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic void mtk_cg_clr_bit_no_setclr(struct clk_hw *hw)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
678c2ecf20Sopenharmony_ci	u32 cgbit = BIT(cg->bit);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	regmap_update_bits(cg->regmap, cg->sta_ofs, cgbit, 0);
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic int mtk_cg_enable(struct clk_hw *hw)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	mtk_cg_clr_bit(hw);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	return 0;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic void mtk_cg_disable(struct clk_hw *hw)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	mtk_cg_set_bit(hw);
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic int mtk_cg_enable_inv(struct clk_hw *hw)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	mtk_cg_set_bit(hw);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	return 0;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic void mtk_cg_disable_inv(struct clk_hw *hw)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	mtk_cg_clr_bit(hw);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic int mtk_cg_enable_no_setclr(struct clk_hw *hw)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	mtk_cg_clr_bit_no_setclr(hw);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return 0;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic void mtk_cg_disable_no_setclr(struct clk_hw *hw)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	mtk_cg_set_bit_no_setclr(hw);
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int mtk_cg_enable_inv_no_setclr(struct clk_hw *hw)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	mtk_cg_set_bit_no_setclr(hw);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	return 0;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic void mtk_cg_disable_inv_no_setclr(struct clk_hw *hw)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	mtk_cg_clr_bit_no_setclr(hw);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ciconst struct clk_ops mtk_clk_gate_ops_setclr = {
1218c2ecf20Sopenharmony_ci	.is_enabled	= mtk_cg_bit_is_cleared,
1228c2ecf20Sopenharmony_ci	.enable		= mtk_cg_enable,
1238c2ecf20Sopenharmony_ci	.disable	= mtk_cg_disable,
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciconst struct clk_ops mtk_clk_gate_ops_setclr_inv = {
1278c2ecf20Sopenharmony_ci	.is_enabled	= mtk_cg_bit_is_set,
1288c2ecf20Sopenharmony_ci	.enable		= mtk_cg_enable_inv,
1298c2ecf20Sopenharmony_ci	.disable	= mtk_cg_disable_inv,
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ciconst struct clk_ops mtk_clk_gate_ops_no_setclr = {
1338c2ecf20Sopenharmony_ci	.is_enabled	= mtk_cg_bit_is_cleared,
1348c2ecf20Sopenharmony_ci	.enable		= mtk_cg_enable_no_setclr,
1358c2ecf20Sopenharmony_ci	.disable	= mtk_cg_disable_no_setclr,
1368c2ecf20Sopenharmony_ci};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ciconst struct clk_ops mtk_clk_gate_ops_no_setclr_inv = {
1398c2ecf20Sopenharmony_ci	.is_enabled	= mtk_cg_bit_is_set,
1408c2ecf20Sopenharmony_ci	.enable		= mtk_cg_enable_inv_no_setclr,
1418c2ecf20Sopenharmony_ci	.disable	= mtk_cg_disable_inv_no_setclr,
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistruct clk *mtk_clk_register_gate(
1458c2ecf20Sopenharmony_ci		const char *name,
1468c2ecf20Sopenharmony_ci		const char *parent_name,
1478c2ecf20Sopenharmony_ci		struct regmap *regmap,
1488c2ecf20Sopenharmony_ci		int set_ofs,
1498c2ecf20Sopenharmony_ci		int clr_ofs,
1508c2ecf20Sopenharmony_ci		int sta_ofs,
1518c2ecf20Sopenharmony_ci		u8 bit,
1528c2ecf20Sopenharmony_ci		const struct clk_ops *ops,
1538c2ecf20Sopenharmony_ci		unsigned long flags,
1548c2ecf20Sopenharmony_ci		struct device *dev)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	struct mtk_clk_gate *cg;
1578c2ecf20Sopenharmony_ci	struct clk *clk;
1588c2ecf20Sopenharmony_ci	struct clk_init_data init = {};
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	cg = kzalloc(sizeof(*cg), GFP_KERNEL);
1618c2ecf20Sopenharmony_ci	if (!cg)
1628c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	init.name = name;
1658c2ecf20Sopenharmony_ci	init.flags = flags | CLK_SET_RATE_PARENT;
1668c2ecf20Sopenharmony_ci	init.parent_names = parent_name ? &parent_name : NULL;
1678c2ecf20Sopenharmony_ci	init.num_parents = parent_name ? 1 : 0;
1688c2ecf20Sopenharmony_ci	init.ops = ops;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	cg->regmap = regmap;
1718c2ecf20Sopenharmony_ci	cg->set_ofs = set_ofs;
1728c2ecf20Sopenharmony_ci	cg->clr_ofs = clr_ofs;
1738c2ecf20Sopenharmony_ci	cg->sta_ofs = sta_ofs;
1748c2ecf20Sopenharmony_ci	cg->bit = bit;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	cg->hw.init = &init;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	clk = clk_register(dev, &cg->hw);
1798c2ecf20Sopenharmony_ci	if (IS_ERR(clk))
1808c2ecf20Sopenharmony_ci		kfree(cg);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	return clk;
1838c2ecf20Sopenharmony_ci}
184