xref: /linux/drivers/clk/zynqmp/divider.c (revision 60d74e011c8e7450846563739a8530f823e5d77a)
13fde0e16SJolly Shah // SPDX-License-Identifier: GPL-2.0
23fde0e16SJolly Shah /*
33fde0e16SJolly Shah  * Zynq UltraScale+ MPSoC Divider support
43fde0e16SJolly Shah  *
53fde0e16SJolly Shah  *  Copyright (C) 2016-2018 Xilinx
63fde0e16SJolly Shah  *
73fde0e16SJolly Shah  * Adjustable divider clock implementation
83fde0e16SJolly Shah  */
93fde0e16SJolly Shah 
103fde0e16SJolly Shah #include <linux/clk.h>
113fde0e16SJolly Shah #include <linux/clk-provider.h>
123fde0e16SJolly Shah #include <linux/slab.h>
133fde0e16SJolly Shah #include "clk-zynqmp.h"
143fde0e16SJolly Shah 
153fde0e16SJolly Shah /*
163fde0e16SJolly Shah  * DOC: basic adjustable divider clock that cannot gate
173fde0e16SJolly Shah  *
183fde0e16SJolly Shah  * Traits of this clock:
193fde0e16SJolly Shah  * prepare - clk_prepare only ensures that parents are prepared
203fde0e16SJolly Shah  * enable - clk_enable only ensures that parents are enabled
213fde0e16SJolly Shah  * rate - rate is adjustable.  clk->rate = ceiling(parent->rate / divisor)
223fde0e16SJolly Shah  * parent - fixed parent.  No clk_set_parent support
233fde0e16SJolly Shah  */
243fde0e16SJolly Shah 
253fde0e16SJolly Shah #define to_zynqmp_clk_divider(_hw)		\
263fde0e16SJolly Shah 	container_of(_hw, struct zynqmp_clk_divider, hw)
273fde0e16SJolly Shah 
283fde0e16SJolly Shah #define CLK_FRAC	BIT(13) /* has a fractional parent */
293fde0e16SJolly Shah 
303fde0e16SJolly Shah /**
313fde0e16SJolly Shah  * struct zynqmp_clk_divider - adjustable divider clock
323fde0e16SJolly Shah  * @hw:		handle between common and hardware-specific interfaces
333fde0e16SJolly Shah  * @flags:	Hardware specific flags
343fde0e16SJolly Shah  * @clk_id:	Id of clock
353fde0e16SJolly Shah  * @div_type:	divisor type (TYPE_DIV1 or TYPE_DIV2)
363fde0e16SJolly Shah  */
373fde0e16SJolly Shah struct zynqmp_clk_divider {
383fde0e16SJolly Shah 	struct clk_hw hw;
393fde0e16SJolly Shah 	u8 flags;
403fde0e16SJolly Shah 	u32 clk_id;
413fde0e16SJolly Shah 	u32 div_type;
423fde0e16SJolly Shah };
433fde0e16SJolly Shah 
443fde0e16SJolly Shah static inline int zynqmp_divider_get_val(unsigned long parent_rate,
453fde0e16SJolly Shah 					 unsigned long rate)
463fde0e16SJolly Shah {
473fde0e16SJolly Shah 	return DIV_ROUND_CLOSEST(parent_rate, rate);
483fde0e16SJolly Shah }
493fde0e16SJolly Shah 
503fde0e16SJolly Shah /**
513fde0e16SJolly Shah  * zynqmp_clk_divider_recalc_rate() - Recalc rate of divider clock
523fde0e16SJolly Shah  * @hw:			handle between common and hardware-specific interfaces
533fde0e16SJolly Shah  * @parent_rate:	rate of parent clock
543fde0e16SJolly Shah  *
553fde0e16SJolly Shah  * Return: 0 on success else error+reason
563fde0e16SJolly Shah  */
573fde0e16SJolly Shah static unsigned long zynqmp_clk_divider_recalc_rate(struct clk_hw *hw,
583fde0e16SJolly Shah 						    unsigned long parent_rate)
593fde0e16SJolly Shah {
603fde0e16SJolly Shah 	struct zynqmp_clk_divider *divider = to_zynqmp_clk_divider(hw);
613fde0e16SJolly Shah 	const char *clk_name = clk_hw_get_name(hw);
623fde0e16SJolly Shah 	u32 clk_id = divider->clk_id;
633fde0e16SJolly Shah 	u32 div_type = divider->div_type;
643fde0e16SJolly Shah 	u32 div, value;
653fde0e16SJolly Shah 	int ret;
663fde0e16SJolly Shah 	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
673fde0e16SJolly Shah 
683fde0e16SJolly Shah 	ret = eemi_ops->clock_getdivider(clk_id, &div);
693fde0e16SJolly Shah 
703fde0e16SJolly Shah 	if (ret)
713fde0e16SJolly Shah 		pr_warn_once("%s() get divider failed for %s, ret = %d\n",
723fde0e16SJolly Shah 			     __func__, clk_name, ret);
733fde0e16SJolly Shah 
743fde0e16SJolly Shah 	if (div_type == TYPE_DIV1)
753fde0e16SJolly Shah 		value = div & 0xFFFF;
763fde0e16SJolly Shah 	else
773fde0e16SJolly Shah 		value = div >> 16;
783fde0e16SJolly Shah 
79*60d74e01SRajan Vaja 	if (!value) {
80*60d74e01SRajan Vaja 		WARN(!(divider->flags & CLK_DIVIDER_ALLOW_ZERO),
81*60d74e01SRajan Vaja 		     "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
82*60d74e01SRajan Vaja 		     clk_name);
83*60d74e01SRajan Vaja 		return parent_rate;
84*60d74e01SRajan Vaja 	}
85*60d74e01SRajan Vaja 
863fde0e16SJolly Shah 	return DIV_ROUND_UP_ULL(parent_rate, value);
873fde0e16SJolly Shah }
883fde0e16SJolly Shah 
893fde0e16SJolly Shah /**
903fde0e16SJolly Shah  * zynqmp_clk_divider_round_rate() - Round rate of divider clock
913fde0e16SJolly Shah  * @hw:			handle between common and hardware-specific interfaces
923fde0e16SJolly Shah  * @rate:		rate of clock to be set
933fde0e16SJolly Shah  * @prate:		rate of parent clock
943fde0e16SJolly Shah  *
953fde0e16SJolly Shah  * Return: 0 on success else error+reason
963fde0e16SJolly Shah  */
973fde0e16SJolly Shah static long zynqmp_clk_divider_round_rate(struct clk_hw *hw,
983fde0e16SJolly Shah 					  unsigned long rate,
993fde0e16SJolly Shah 					  unsigned long *prate)
1003fde0e16SJolly Shah {
1013fde0e16SJolly Shah 	struct zynqmp_clk_divider *divider = to_zynqmp_clk_divider(hw);
1023fde0e16SJolly Shah 	const char *clk_name = clk_hw_get_name(hw);
1033fde0e16SJolly Shah 	u32 clk_id = divider->clk_id;
1043fde0e16SJolly Shah 	u32 div_type = divider->div_type;
1053fde0e16SJolly Shah 	u32 bestdiv;
1063fde0e16SJolly Shah 	int ret;
1073fde0e16SJolly Shah 	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
1083fde0e16SJolly Shah 
1093fde0e16SJolly Shah 	/* if read only, just return current value */
1103fde0e16SJolly Shah 	if (divider->flags & CLK_DIVIDER_READ_ONLY) {
1113fde0e16SJolly Shah 		ret = eemi_ops->clock_getdivider(clk_id, &bestdiv);
1123fde0e16SJolly Shah 
1133fde0e16SJolly Shah 		if (ret)
1143fde0e16SJolly Shah 			pr_warn_once("%s() get divider failed for %s, ret = %d\n",
1153fde0e16SJolly Shah 				     __func__, clk_name, ret);
1163fde0e16SJolly Shah 		if (div_type == TYPE_DIV1)
1173fde0e16SJolly Shah 			bestdiv = bestdiv & 0xFFFF;
1183fde0e16SJolly Shah 		else
1193fde0e16SJolly Shah 			bestdiv  = bestdiv >> 16;
1203fde0e16SJolly Shah 
1213fde0e16SJolly Shah 		return DIV_ROUND_UP_ULL((u64)*prate, bestdiv);
1223fde0e16SJolly Shah 	}
1233fde0e16SJolly Shah 
1243fde0e16SJolly Shah 	bestdiv = zynqmp_divider_get_val(*prate, rate);
1253fde0e16SJolly Shah 
1263fde0e16SJolly Shah 	if ((clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) &&
1273fde0e16SJolly Shah 	    (divider->flags & CLK_FRAC))
1283fde0e16SJolly Shah 		bestdiv = rate % *prate ? 1 : bestdiv;
1293fde0e16SJolly Shah 	*prate = rate * bestdiv;
1303fde0e16SJolly Shah 
1313fde0e16SJolly Shah 	return rate;
1323fde0e16SJolly Shah }
1333fde0e16SJolly Shah 
1343fde0e16SJolly Shah /**
1353fde0e16SJolly Shah  * zynqmp_clk_divider_set_rate() - Set rate of divider clock
1363fde0e16SJolly Shah  * @hw:			handle between common and hardware-specific interfaces
1373fde0e16SJolly Shah  * @rate:		rate of clock to be set
1383fde0e16SJolly Shah  * @parent_rate:	rate of parent clock
1393fde0e16SJolly Shah  *
1403fde0e16SJolly Shah  * Return: 0 on success else error+reason
1413fde0e16SJolly Shah  */
1423fde0e16SJolly Shah static int zynqmp_clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
1433fde0e16SJolly Shah 				       unsigned long parent_rate)
1443fde0e16SJolly Shah {
1453fde0e16SJolly Shah 	struct zynqmp_clk_divider *divider = to_zynqmp_clk_divider(hw);
1463fde0e16SJolly Shah 	const char *clk_name = clk_hw_get_name(hw);
1473fde0e16SJolly Shah 	u32 clk_id = divider->clk_id;
1483fde0e16SJolly Shah 	u32 div_type = divider->div_type;
1493fde0e16SJolly Shah 	u32 value, div;
1503fde0e16SJolly Shah 	int ret;
1513fde0e16SJolly Shah 	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
1523fde0e16SJolly Shah 
1533fde0e16SJolly Shah 	value = zynqmp_divider_get_val(parent_rate, rate);
1543fde0e16SJolly Shah 	if (div_type == TYPE_DIV1) {
1553fde0e16SJolly Shah 		div = value & 0xFFFF;
1563fde0e16SJolly Shah 		div |= 0xffff << 16;
1573fde0e16SJolly Shah 	} else {
1583fde0e16SJolly Shah 		div = 0xffff;
1593fde0e16SJolly Shah 		div |= value << 16;
1603fde0e16SJolly Shah 	}
1613fde0e16SJolly Shah 
1623fde0e16SJolly Shah 	ret = eemi_ops->clock_setdivider(clk_id, div);
1633fde0e16SJolly Shah 
1643fde0e16SJolly Shah 	if (ret)
1653fde0e16SJolly Shah 		pr_warn_once("%s() set divider failed for %s, ret = %d\n",
1663fde0e16SJolly Shah 			     __func__, clk_name, ret);
1673fde0e16SJolly Shah 
1683fde0e16SJolly Shah 	return ret;
1693fde0e16SJolly Shah }
1703fde0e16SJolly Shah 
1713fde0e16SJolly Shah static const struct clk_ops zynqmp_clk_divider_ops = {
1723fde0e16SJolly Shah 	.recalc_rate = zynqmp_clk_divider_recalc_rate,
1733fde0e16SJolly Shah 	.round_rate = zynqmp_clk_divider_round_rate,
1743fde0e16SJolly Shah 	.set_rate = zynqmp_clk_divider_set_rate,
1753fde0e16SJolly Shah };
1763fde0e16SJolly Shah 
1773fde0e16SJolly Shah /**
1783fde0e16SJolly Shah  * zynqmp_clk_register_divider() - Register a divider clock
1793fde0e16SJolly Shah  * @name:		Name of this clock
1803fde0e16SJolly Shah  * @clk_id:		Id of clock
1813fde0e16SJolly Shah  * @parents:		Name of this clock's parents
1823fde0e16SJolly Shah  * @num_parents:	Number of parents
1833fde0e16SJolly Shah  * @nodes:		Clock topology node
1843fde0e16SJolly Shah  *
1853fde0e16SJolly Shah  * Return: clock hardware to registered clock divider
1863fde0e16SJolly Shah  */
1873fde0e16SJolly Shah struct clk_hw *zynqmp_clk_register_divider(const char *name,
1883fde0e16SJolly Shah 					   u32 clk_id,
1893fde0e16SJolly Shah 					   const char * const *parents,
1903fde0e16SJolly Shah 					   u8 num_parents,
1913fde0e16SJolly Shah 					   const struct clock_topology *nodes)
1923fde0e16SJolly Shah {
1933fde0e16SJolly Shah 	struct zynqmp_clk_divider *div;
1943fde0e16SJolly Shah 	struct clk_hw *hw;
1953fde0e16SJolly Shah 	struct clk_init_data init;
1963fde0e16SJolly Shah 	int ret;
1973fde0e16SJolly Shah 
1983fde0e16SJolly Shah 	/* allocate the divider */
1993fde0e16SJolly Shah 	div = kzalloc(sizeof(*div), GFP_KERNEL);
2003fde0e16SJolly Shah 	if (!div)
2013fde0e16SJolly Shah 		return ERR_PTR(-ENOMEM);
2023fde0e16SJolly Shah 
2033fde0e16SJolly Shah 	init.name = name;
2043fde0e16SJolly Shah 	init.ops = &zynqmp_clk_divider_ops;
2053fde0e16SJolly Shah 	init.flags = nodes->flag;
2063fde0e16SJolly Shah 	init.parent_names = parents;
2073fde0e16SJolly Shah 	init.num_parents = 1;
2083fde0e16SJolly Shah 
2093fde0e16SJolly Shah 	/* struct clk_divider assignments */
2103fde0e16SJolly Shah 	div->flags = nodes->type_flag;
2113fde0e16SJolly Shah 	div->hw.init = &init;
2123fde0e16SJolly Shah 	div->clk_id = clk_id;
2133fde0e16SJolly Shah 	div->div_type = nodes->type;
2143fde0e16SJolly Shah 
2153fde0e16SJolly Shah 	hw = &div->hw;
2163fde0e16SJolly Shah 	ret = clk_hw_register(NULL, hw);
2173fde0e16SJolly Shah 	if (ret) {
2183fde0e16SJolly Shah 		kfree(div);
2193fde0e16SJolly Shah 		hw = ERR_PTR(ret);
2203fde0e16SJolly Shah 	}
2213fde0e16SJolly Shah 
2223fde0e16SJolly Shah 	return hw;
2233fde0e16SJolly Shah }
2243fde0e16SJolly Shah EXPORT_SYMBOL_GPL(zynqmp_clk_register_divider);
225