162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2016 Socionext Inc.
462306a36Sopenharmony_ci *   Author: Masahiro Yamada <yamada.masahiro@socionext.com>
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <linux/clk-provider.h>
862306a36Sopenharmony_ci#include <linux/device.h>
962306a36Sopenharmony_ci#include <linux/regmap.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include "clk-uniphier.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_cistruct uniphier_clk_gate {
1462306a36Sopenharmony_ci	struct clk_hw hw;
1562306a36Sopenharmony_ci	struct regmap *regmap;
1662306a36Sopenharmony_ci	unsigned int reg;
1762306a36Sopenharmony_ci	unsigned int bit;
1862306a36Sopenharmony_ci};
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define to_uniphier_clk_gate(_hw) \
2162306a36Sopenharmony_ci				container_of(_hw, struct uniphier_clk_gate, hw)
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_cistatic int uniphier_clk_gate_endisable(struct clk_hw *hw, int enable)
2462306a36Sopenharmony_ci{
2562306a36Sopenharmony_ci	struct uniphier_clk_gate *gate = to_uniphier_clk_gate(hw);
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci	return regmap_write_bits(gate->regmap, gate->reg, BIT(gate->bit),
2862306a36Sopenharmony_ci				 enable ? BIT(gate->bit) : 0);
2962306a36Sopenharmony_ci}
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistatic int uniphier_clk_gate_enable(struct clk_hw *hw)
3262306a36Sopenharmony_ci{
3362306a36Sopenharmony_ci	return uniphier_clk_gate_endisable(hw, 1);
3462306a36Sopenharmony_ci}
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cistatic void uniphier_clk_gate_disable(struct clk_hw *hw)
3762306a36Sopenharmony_ci{
3862306a36Sopenharmony_ci	if (uniphier_clk_gate_endisable(hw, 0) < 0)
3962306a36Sopenharmony_ci		pr_warn("failed to disable clk\n");
4062306a36Sopenharmony_ci}
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_cistatic int uniphier_clk_gate_is_enabled(struct clk_hw *hw)
4362306a36Sopenharmony_ci{
4462306a36Sopenharmony_ci	struct uniphier_clk_gate *gate = to_uniphier_clk_gate(hw);
4562306a36Sopenharmony_ci	unsigned int val;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	if (regmap_read(gate->regmap, gate->reg, &val) < 0)
4862306a36Sopenharmony_ci		pr_warn("is_enabled() may return wrong result\n");
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	return !!(val & BIT(gate->bit));
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic const struct clk_ops uniphier_clk_gate_ops = {
5462306a36Sopenharmony_ci	.enable = uniphier_clk_gate_enable,
5562306a36Sopenharmony_ci	.disable = uniphier_clk_gate_disable,
5662306a36Sopenharmony_ci	.is_enabled = uniphier_clk_gate_is_enabled,
5762306a36Sopenharmony_ci};
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_cistruct clk_hw *uniphier_clk_register_gate(struct device *dev,
6062306a36Sopenharmony_ci					  struct regmap *regmap,
6162306a36Sopenharmony_ci					  const char *name,
6262306a36Sopenharmony_ci				const struct uniphier_clk_gate_data *data)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	struct uniphier_clk_gate *gate;
6562306a36Sopenharmony_ci	struct clk_init_data init;
6662306a36Sopenharmony_ci	int ret;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	gate = devm_kzalloc(dev, sizeof(*gate), GFP_KERNEL);
6962306a36Sopenharmony_ci	if (!gate)
7062306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	init.name = name;
7362306a36Sopenharmony_ci	init.ops = &uniphier_clk_gate_ops;
7462306a36Sopenharmony_ci	init.flags = data->parent_name ? CLK_SET_RATE_PARENT : 0;
7562306a36Sopenharmony_ci	init.parent_names = data->parent_name ? &data->parent_name : NULL;
7662306a36Sopenharmony_ci	init.num_parents = data->parent_name ? 1 : 0;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	gate->regmap = regmap;
7962306a36Sopenharmony_ci	gate->reg = data->reg;
8062306a36Sopenharmony_ci	gate->bit = data->bit;
8162306a36Sopenharmony_ci	gate->hw.init = &init;
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	ret = devm_clk_hw_register(dev, &gate->hw);
8462306a36Sopenharmony_ci	if (ret)
8562306a36Sopenharmony_ci		return ERR_PTR(ret);
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	return &gate->hw;
8862306a36Sopenharmony_ci}
89