18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2018 BayLibre, SAS.
48c2ecf20Sopenharmony_ci * Author: Jerome Brunet <jbrunet@baylibre.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include "clk-regmap.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic int clk_regmap_gate_endisable(struct clk_hw *hw, int enable)
118c2ecf20Sopenharmony_ci{
128c2ecf20Sopenharmony_ci	struct clk_regmap *clk = to_clk_regmap(hw);
138c2ecf20Sopenharmony_ci	struct clk_regmap_gate_data *gate = clk_get_regmap_gate_data(clk);
148c2ecf20Sopenharmony_ci	int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	set ^= enable;
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci	return regmap_update_bits(clk->map, gate->offset, BIT(gate->bit_idx),
198c2ecf20Sopenharmony_ci				  set ? BIT(gate->bit_idx) : 0);
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic int clk_regmap_gate_enable(struct clk_hw *hw)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	return clk_regmap_gate_endisable(hw, 1);
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic void clk_regmap_gate_disable(struct clk_hw *hw)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	clk_regmap_gate_endisable(hw, 0);
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int clk_regmap_gate_is_enabled(struct clk_hw *hw)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	struct clk_regmap *clk = to_clk_regmap(hw);
358c2ecf20Sopenharmony_ci	struct clk_regmap_gate_data *gate = clk_get_regmap_gate_data(clk);
368c2ecf20Sopenharmony_ci	unsigned int val;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	regmap_read(clk->map, gate->offset, &val);
398c2ecf20Sopenharmony_ci	if (gate->flags & CLK_GATE_SET_TO_DISABLE)
408c2ecf20Sopenharmony_ci		val ^= BIT(gate->bit_idx);
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	val &= BIT(gate->bit_idx);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	return val ? 1 : 0;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciconst struct clk_ops clk_regmap_gate_ops = {
488c2ecf20Sopenharmony_ci	.enable = clk_regmap_gate_enable,
498c2ecf20Sopenharmony_ci	.disable = clk_regmap_gate_disable,
508c2ecf20Sopenharmony_ci	.is_enabled = clk_regmap_gate_is_enabled,
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_regmap_gate_ops);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ciconst struct clk_ops clk_regmap_gate_ro_ops = {
558c2ecf20Sopenharmony_ci	.is_enabled = clk_regmap_gate_is_enabled,
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_regmap_gate_ro_ops);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic unsigned long clk_regmap_div_recalc_rate(struct clk_hw *hw,
608c2ecf20Sopenharmony_ci						unsigned long prate)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct clk_regmap *clk = to_clk_regmap(hw);
638c2ecf20Sopenharmony_ci	struct clk_regmap_div_data *div = clk_get_regmap_div_data(clk);
648c2ecf20Sopenharmony_ci	unsigned int val;
658c2ecf20Sopenharmony_ci	int ret;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	ret = regmap_read(clk->map, div->offset, &val);
688c2ecf20Sopenharmony_ci	if (ret)
698c2ecf20Sopenharmony_ci		/* Gives a hint that something is wrong */
708c2ecf20Sopenharmony_ci		return 0;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	val >>= div->shift;
738c2ecf20Sopenharmony_ci	val &= clk_div_mask(div->width);
748c2ecf20Sopenharmony_ci	return divider_recalc_rate(hw, prate, val, div->table, div->flags,
758c2ecf20Sopenharmony_ci				   div->width);
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic long clk_regmap_div_round_rate(struct clk_hw *hw, unsigned long rate,
798c2ecf20Sopenharmony_ci				      unsigned long *prate)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct clk_regmap *clk = to_clk_regmap(hw);
828c2ecf20Sopenharmony_ci	struct clk_regmap_div_data *div = clk_get_regmap_div_data(clk);
838c2ecf20Sopenharmony_ci	unsigned int val;
848c2ecf20Sopenharmony_ci	int ret;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	/* if read only, just return current value */
878c2ecf20Sopenharmony_ci	if (div->flags & CLK_DIVIDER_READ_ONLY) {
888c2ecf20Sopenharmony_ci		ret = regmap_read(clk->map, div->offset, &val);
898c2ecf20Sopenharmony_ci		if (ret)
908c2ecf20Sopenharmony_ci			/* Gives a hint that something is wrong */
918c2ecf20Sopenharmony_ci			return 0;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci		val >>= div->shift;
948c2ecf20Sopenharmony_ci		val &= clk_div_mask(div->width);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci		return divider_ro_round_rate(hw, rate, prate, div->table,
978c2ecf20Sopenharmony_ci					     div->width, div->flags, val);
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return divider_round_rate(hw, rate, prate, div->table, div->width,
1018c2ecf20Sopenharmony_ci				  div->flags);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic int clk_regmap_div_set_rate(struct clk_hw *hw, unsigned long rate,
1058c2ecf20Sopenharmony_ci				   unsigned long parent_rate)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct clk_regmap *clk = to_clk_regmap(hw);
1088c2ecf20Sopenharmony_ci	struct clk_regmap_div_data *div = clk_get_regmap_div_data(clk);
1098c2ecf20Sopenharmony_ci	unsigned int val;
1108c2ecf20Sopenharmony_ci	int ret;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	ret = divider_get_val(rate, parent_rate, div->table, div->width,
1138c2ecf20Sopenharmony_ci			      div->flags);
1148c2ecf20Sopenharmony_ci	if (ret < 0)
1158c2ecf20Sopenharmony_ci		return ret;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	val = (unsigned int)ret << div->shift;
1188c2ecf20Sopenharmony_ci	return regmap_update_bits(clk->map, div->offset,
1198c2ecf20Sopenharmony_ci				  clk_div_mask(div->width) << div->shift, val);
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/* Would prefer clk_regmap_div_ro_ops but clashes with qcom */
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ciconst struct clk_ops clk_regmap_divider_ops = {
1258c2ecf20Sopenharmony_ci	.recalc_rate = clk_regmap_div_recalc_rate,
1268c2ecf20Sopenharmony_ci	.round_rate = clk_regmap_div_round_rate,
1278c2ecf20Sopenharmony_ci	.set_rate = clk_regmap_div_set_rate,
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_regmap_divider_ops);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ciconst struct clk_ops clk_regmap_divider_ro_ops = {
1328c2ecf20Sopenharmony_ci	.recalc_rate = clk_regmap_div_recalc_rate,
1338c2ecf20Sopenharmony_ci	.round_rate = clk_regmap_div_round_rate,
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_regmap_divider_ro_ops);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic u8 clk_regmap_mux_get_parent(struct clk_hw *hw)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	struct clk_regmap *clk = to_clk_regmap(hw);
1408c2ecf20Sopenharmony_ci	struct clk_regmap_mux_data *mux = clk_get_regmap_mux_data(clk);
1418c2ecf20Sopenharmony_ci	unsigned int val;
1428c2ecf20Sopenharmony_ci	int ret;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	ret = regmap_read(clk->map, mux->offset, &val);
1458c2ecf20Sopenharmony_ci	if (ret)
1468c2ecf20Sopenharmony_ci		return ret;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	val >>= mux->shift;
1498c2ecf20Sopenharmony_ci	val &= mux->mask;
1508c2ecf20Sopenharmony_ci	return clk_mux_val_to_index(hw, mux->table, mux->flags, val);
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic int clk_regmap_mux_set_parent(struct clk_hw *hw, u8 index)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	struct clk_regmap *clk = to_clk_regmap(hw);
1568c2ecf20Sopenharmony_ci	struct clk_regmap_mux_data *mux = clk_get_regmap_mux_data(clk);
1578c2ecf20Sopenharmony_ci	unsigned int val = clk_mux_index_to_val(mux->table, mux->flags, index);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	return regmap_update_bits(clk->map, mux->offset,
1608c2ecf20Sopenharmony_ci				  mux->mask << mux->shift,
1618c2ecf20Sopenharmony_ci				  val << mux->shift);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic int clk_regmap_mux_determine_rate(struct clk_hw *hw,
1658c2ecf20Sopenharmony_ci					 struct clk_rate_request *req)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	struct clk_regmap *clk = to_clk_regmap(hw);
1688c2ecf20Sopenharmony_ci	struct clk_regmap_mux_data *mux = clk_get_regmap_mux_data(clk);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	return clk_mux_determine_rate_flags(hw, req, mux->flags);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ciconst struct clk_ops clk_regmap_mux_ops = {
1748c2ecf20Sopenharmony_ci	.get_parent = clk_regmap_mux_get_parent,
1758c2ecf20Sopenharmony_ci	.set_parent = clk_regmap_mux_set_parent,
1768c2ecf20Sopenharmony_ci	.determine_rate = clk_regmap_mux_determine_rate,
1778c2ecf20Sopenharmony_ci};
1788c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_regmap_mux_ops);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ciconst struct clk_ops clk_regmap_mux_ro_ops = {
1818c2ecf20Sopenharmony_ci	.get_parent = clk_regmap_mux_get_parent,
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_regmap_mux_ro_ops);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Amlogic regmap backed clock driver");
1868c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
1878c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
188