18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Zynq UltraScale+ MPSoC mux
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2016-2018 Xilinx
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
98c2ecf20Sopenharmony_ci#include <linux/slab.h>
108c2ecf20Sopenharmony_ci#include "clk-zynqmp.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci * DOC: basic adjustable multiplexer clock that cannot gate
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Traits of this clock:
168c2ecf20Sopenharmony_ci * prepare - clk_prepare only ensures that parents are prepared
178c2ecf20Sopenharmony_ci * enable - clk_enable only ensures that parents are enabled
188c2ecf20Sopenharmony_ci * rate - rate is only affected by parent switching.  No clk_set_rate support
198c2ecf20Sopenharmony_ci * parent - parent is adjustable through clk_set_parent
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/**
238c2ecf20Sopenharmony_ci * struct zynqmp_clk_mux - multiplexer clock
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * @hw:		handle between common and hardware-specific interfaces
268c2ecf20Sopenharmony_ci * @flags:	hardware-specific flags
278c2ecf20Sopenharmony_ci * @clk_id:	Id of clock
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_cistruct zynqmp_clk_mux {
308c2ecf20Sopenharmony_ci	struct clk_hw hw;
318c2ecf20Sopenharmony_ci	u8 flags;
328c2ecf20Sopenharmony_ci	u32 clk_id;
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define to_zynqmp_clk_mux(_hw) container_of(_hw, struct zynqmp_clk_mux, hw)
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/**
388c2ecf20Sopenharmony_ci * zynqmp_clk_mux_get_parent() - Get parent of clock
398c2ecf20Sopenharmony_ci * @hw:		handle between common and hardware-specific interfaces
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * Return: Parent index
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_cistatic u8 zynqmp_clk_mux_get_parent(struct clk_hw *hw)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct zynqmp_clk_mux *mux = to_zynqmp_clk_mux(hw);
468c2ecf20Sopenharmony_ci	const char *clk_name = clk_hw_get_name(hw);
478c2ecf20Sopenharmony_ci	u32 clk_id = mux->clk_id;
488c2ecf20Sopenharmony_ci	u32 val;
498c2ecf20Sopenharmony_ci	int ret;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	ret = zynqmp_pm_clock_getparent(clk_id, &val);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	if (ret)
548c2ecf20Sopenharmony_ci		pr_warn_once("%s() getparent failed for clock: %s, ret = %d\n",
558c2ecf20Sopenharmony_ci			     __func__, clk_name, ret);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return val;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/**
618c2ecf20Sopenharmony_ci * zynqmp_clk_mux_set_parent() - Set parent of clock
628c2ecf20Sopenharmony_ci * @hw:		handle between common and hardware-specific interfaces
638c2ecf20Sopenharmony_ci * @index:	Parent index
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci * Return: 0 on success else error+reason
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_cistatic int zynqmp_clk_mux_set_parent(struct clk_hw *hw, u8 index)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	struct zynqmp_clk_mux *mux = to_zynqmp_clk_mux(hw);
708c2ecf20Sopenharmony_ci	const char *clk_name = clk_hw_get_name(hw);
718c2ecf20Sopenharmony_ci	u32 clk_id = mux->clk_id;
728c2ecf20Sopenharmony_ci	int ret;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	ret = zynqmp_pm_clock_setparent(clk_id, index);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	if (ret)
778c2ecf20Sopenharmony_ci		pr_warn_once("%s() set parent failed for clock: %s, ret = %d\n",
788c2ecf20Sopenharmony_ci			     __func__, clk_name, ret);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	return ret;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic const struct clk_ops zynqmp_clk_mux_ops = {
848c2ecf20Sopenharmony_ci	.get_parent = zynqmp_clk_mux_get_parent,
858c2ecf20Sopenharmony_ci	.set_parent = zynqmp_clk_mux_set_parent,
868c2ecf20Sopenharmony_ci	.determine_rate = __clk_mux_determine_rate_closest,
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic const struct clk_ops zynqmp_clk_mux_ro_ops = {
908c2ecf20Sopenharmony_ci	.get_parent = zynqmp_clk_mux_get_parent,
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/**
948c2ecf20Sopenharmony_ci * zynqmp_clk_register_mux() - Register a mux table with the clock
958c2ecf20Sopenharmony_ci *			       framework
968c2ecf20Sopenharmony_ci * @name:		Name of this clock
978c2ecf20Sopenharmony_ci * @clk_id:		Id of this clock
988c2ecf20Sopenharmony_ci * @parents:		Name of this clock's parents
998c2ecf20Sopenharmony_ci * @num_parents:	Number of parents
1008c2ecf20Sopenharmony_ci * @nodes:		Clock topology node
1018c2ecf20Sopenharmony_ci *
1028c2ecf20Sopenharmony_ci * Return: clock hardware of the registered clock mux
1038c2ecf20Sopenharmony_ci */
1048c2ecf20Sopenharmony_cistruct clk_hw *zynqmp_clk_register_mux(const char *name, u32 clk_id,
1058c2ecf20Sopenharmony_ci				       const char * const *parents,
1068c2ecf20Sopenharmony_ci				       u8 num_parents,
1078c2ecf20Sopenharmony_ci				       const struct clock_topology *nodes)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct zynqmp_clk_mux *mux;
1108c2ecf20Sopenharmony_ci	struct clk_hw *hw;
1118c2ecf20Sopenharmony_ci	struct clk_init_data init;
1128c2ecf20Sopenharmony_ci	int ret;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	mux = kzalloc(sizeof(*mux), GFP_KERNEL);
1158c2ecf20Sopenharmony_ci	if (!mux)
1168c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	init.name = name;
1198c2ecf20Sopenharmony_ci	if (nodes->type_flag & CLK_MUX_READ_ONLY)
1208c2ecf20Sopenharmony_ci		init.ops = &zynqmp_clk_mux_ro_ops;
1218c2ecf20Sopenharmony_ci	else
1228c2ecf20Sopenharmony_ci		init.ops = &zynqmp_clk_mux_ops;
1238c2ecf20Sopenharmony_ci	init.flags = nodes->flag;
1248c2ecf20Sopenharmony_ci	init.parent_names = parents;
1258c2ecf20Sopenharmony_ci	init.num_parents = num_parents;
1268c2ecf20Sopenharmony_ci	mux->flags = nodes->type_flag;
1278c2ecf20Sopenharmony_ci	mux->hw.init = &init;
1288c2ecf20Sopenharmony_ci	mux->clk_id = clk_id;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	hw = &mux->hw;
1318c2ecf20Sopenharmony_ci	ret = clk_hw_register(NULL, hw);
1328c2ecf20Sopenharmony_ci	if (ret) {
1338c2ecf20Sopenharmony_ci		kfree(hw);
1348c2ecf20Sopenharmony_ci		hw = ERR_PTR(ret);
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	return hw;
1388c2ecf20Sopenharmony_ci}
139