xref: /linux/drivers/clk/tegra/clk-periph-fixed.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*9952f691SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21ec7032aSThierry Reding /*
31ec7032aSThierry Reding  * Copyright (c) 2015, NVIDIA CORPORATION.  All rights reserved.
41ec7032aSThierry Reding  */
51ec7032aSThierry Reding 
61ec7032aSThierry Reding #include <linux/clk-provider.h>
762e59c4eSStephen Boyd #include <linux/io.h>
81ec7032aSThierry Reding 
91ec7032aSThierry Reding #include "clk.h"
101ec7032aSThierry Reding 
111ec7032aSThierry Reding static inline struct tegra_clk_periph_fixed *
to_tegra_clk_periph_fixed(struct clk_hw * hw)121ec7032aSThierry Reding to_tegra_clk_periph_fixed(struct clk_hw *hw)
131ec7032aSThierry Reding {
141ec7032aSThierry Reding 	return container_of(hw, struct tegra_clk_periph_fixed, hw);
151ec7032aSThierry Reding }
161ec7032aSThierry Reding 
tegra_clk_periph_fixed_is_enabled(struct clk_hw * hw)171ec7032aSThierry Reding static int tegra_clk_periph_fixed_is_enabled(struct clk_hw *hw)
181ec7032aSThierry Reding {
191ec7032aSThierry Reding 	struct tegra_clk_periph_fixed *fixed = to_tegra_clk_periph_fixed(hw);
201ec7032aSThierry Reding 	u32 mask = 1 << (fixed->num % 32), value;
211ec7032aSThierry Reding 
221ec7032aSThierry Reding 	value = readl(fixed->base + fixed->regs->enb_reg);
231ec7032aSThierry Reding 	if (value & mask) {
241ec7032aSThierry Reding 		value = readl(fixed->base + fixed->regs->rst_reg);
251ec7032aSThierry Reding 		if ((value & mask) == 0)
261ec7032aSThierry Reding 			return 1;
271ec7032aSThierry Reding 	}
281ec7032aSThierry Reding 
291ec7032aSThierry Reding 	return 0;
301ec7032aSThierry Reding }
311ec7032aSThierry Reding 
tegra_clk_periph_fixed_enable(struct clk_hw * hw)321ec7032aSThierry Reding static int tegra_clk_periph_fixed_enable(struct clk_hw *hw)
331ec7032aSThierry Reding {
341ec7032aSThierry Reding 	struct tegra_clk_periph_fixed *fixed = to_tegra_clk_periph_fixed(hw);
351ec7032aSThierry Reding 	u32 mask = 1 << (fixed->num % 32);
361ec7032aSThierry Reding 
371ec7032aSThierry Reding 	writel(mask, fixed->base + fixed->regs->enb_set_reg);
381ec7032aSThierry Reding 
391ec7032aSThierry Reding 	return 0;
401ec7032aSThierry Reding }
411ec7032aSThierry Reding 
tegra_clk_periph_fixed_disable(struct clk_hw * hw)421ec7032aSThierry Reding static void tegra_clk_periph_fixed_disable(struct clk_hw *hw)
431ec7032aSThierry Reding {
441ec7032aSThierry Reding 	struct tegra_clk_periph_fixed *fixed = to_tegra_clk_periph_fixed(hw);
451ec7032aSThierry Reding 	u32 mask = 1 << (fixed->num % 32);
461ec7032aSThierry Reding 
471ec7032aSThierry Reding 	writel(mask, fixed->base + fixed->regs->enb_clr_reg);
481ec7032aSThierry Reding }
491ec7032aSThierry Reding 
501ec7032aSThierry Reding static unsigned long
tegra_clk_periph_fixed_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)511ec7032aSThierry Reding tegra_clk_periph_fixed_recalc_rate(struct clk_hw *hw,
521ec7032aSThierry Reding 				   unsigned long parent_rate)
531ec7032aSThierry Reding {
541ec7032aSThierry Reding 	struct tegra_clk_periph_fixed *fixed = to_tegra_clk_periph_fixed(hw);
551ec7032aSThierry Reding 	unsigned long long rate;
561ec7032aSThierry Reding 
571ec7032aSThierry Reding 	rate = (unsigned long long)parent_rate * fixed->mul;
581ec7032aSThierry Reding 	do_div(rate, fixed->div);
591ec7032aSThierry Reding 
601ec7032aSThierry Reding 	return (unsigned long)rate;
611ec7032aSThierry Reding }
621ec7032aSThierry Reding 
631ec7032aSThierry Reding static const struct clk_ops tegra_clk_periph_fixed_ops = {
641ec7032aSThierry Reding 	.is_enabled = tegra_clk_periph_fixed_is_enabled,
651ec7032aSThierry Reding 	.enable = tegra_clk_periph_fixed_enable,
661ec7032aSThierry Reding 	.disable = tegra_clk_periph_fixed_disable,
671ec7032aSThierry Reding 	.recalc_rate = tegra_clk_periph_fixed_recalc_rate,
681ec7032aSThierry Reding };
691ec7032aSThierry Reding 
tegra_clk_register_periph_fixed(const char * name,const char * parent,unsigned long flags,void __iomem * base,unsigned int mul,unsigned int div,unsigned int num)701ec7032aSThierry Reding struct clk *tegra_clk_register_periph_fixed(const char *name,
711ec7032aSThierry Reding 					    const char *parent,
721ec7032aSThierry Reding 					    unsigned long flags,
731ec7032aSThierry Reding 					    void __iomem *base,
741ec7032aSThierry Reding 					    unsigned int mul,
751ec7032aSThierry Reding 					    unsigned int div,
761ec7032aSThierry Reding 					    unsigned int num)
771ec7032aSThierry Reding {
781ec7032aSThierry Reding 	const struct tegra_clk_periph_regs *regs;
791ec7032aSThierry Reding 	struct tegra_clk_periph_fixed *fixed;
801ec7032aSThierry Reding 	struct clk_init_data init;
811ec7032aSThierry Reding 	struct clk *clk;
821ec7032aSThierry Reding 
831ec7032aSThierry Reding 	regs = get_reg_bank(num);
841ec7032aSThierry Reding 	if (!regs)
851ec7032aSThierry Reding 		return ERR_PTR(-EINVAL);
861ec7032aSThierry Reding 
871ec7032aSThierry Reding 	fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
881ec7032aSThierry Reding 	if (!fixed)
891ec7032aSThierry Reding 		return ERR_PTR(-ENOMEM);
901ec7032aSThierry Reding 
911ec7032aSThierry Reding 	init.name = name;
921ec7032aSThierry Reding 	init.flags = flags;
931ec7032aSThierry Reding 	init.parent_names = parent ? &parent : NULL;
941ec7032aSThierry Reding 	init.num_parents = parent ? 1 : 0;
951ec7032aSThierry Reding 	init.ops = &tegra_clk_periph_fixed_ops;
961ec7032aSThierry Reding 
971ec7032aSThierry Reding 	fixed->base = base;
981ec7032aSThierry Reding 	fixed->regs = regs;
991ec7032aSThierry Reding 	fixed->mul = mul;
1001ec7032aSThierry Reding 	fixed->div = div;
1011ec7032aSThierry Reding 	fixed->num = num;
1021ec7032aSThierry Reding 
1031ec7032aSThierry Reding 	fixed->hw.init = &init;
1041ec7032aSThierry Reding 
1051ec7032aSThierry Reding 	clk = clk_register(NULL, &fixed->hw);
1061ec7032aSThierry Reding 	if (IS_ERR(clk))
1071ec7032aSThierry Reding 		kfree(fixed);
1081ec7032aSThierry Reding 
1091ec7032aSThierry Reding 	return clk;
1101ec7032aSThierry Reding }
111