xref: /openbmc/linux/drivers/clk/sunxi-ng/ccu_frac.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
289a3dfb7SMaxime Ripard /*
389a3dfb7SMaxime Ripard  * Copyright (C) 2016 Maxime Ripard
489a3dfb7SMaxime Ripard  * Maxime Ripard <maxime.ripard@free-electrons.com>
589a3dfb7SMaxime Ripard  */
689a3dfb7SMaxime Ripard 
789a3dfb7SMaxime Ripard #include <linux/clk-provider.h>
862e59c4eSStephen Boyd #include <linux/io.h>
989a3dfb7SMaxime Ripard #include <linux/spinlock.h>
1089a3dfb7SMaxime Ripard 
1189a3dfb7SMaxime Ripard #include "ccu_frac.h"
1289a3dfb7SMaxime Ripard 
ccu_frac_helper_is_enabled(struct ccu_common * common,struct ccu_frac_internal * cf)1389a3dfb7SMaxime Ripard bool ccu_frac_helper_is_enabled(struct ccu_common *common,
14a501a14eSMaxime Ripard 				struct ccu_frac_internal *cf)
1589a3dfb7SMaxime Ripard {
1689a3dfb7SMaxime Ripard 	if (!(common->features & CCU_FEATURE_FRACTIONAL))
1789a3dfb7SMaxime Ripard 		return false;
1889a3dfb7SMaxime Ripard 
1989a3dfb7SMaxime Ripard 	return !(readl(common->base + common->reg) & cf->enable);
2089a3dfb7SMaxime Ripard }
21*551b62b1SSamuel Holland EXPORT_SYMBOL_NS_GPL(ccu_frac_helper_is_enabled, SUNXI_CCU);
2289a3dfb7SMaxime Ripard 
ccu_frac_helper_enable(struct ccu_common * common,struct ccu_frac_internal * cf)2389a3dfb7SMaxime Ripard void ccu_frac_helper_enable(struct ccu_common *common,
24a501a14eSMaxime Ripard 			    struct ccu_frac_internal *cf)
2589a3dfb7SMaxime Ripard {
2689a3dfb7SMaxime Ripard 	unsigned long flags;
2789a3dfb7SMaxime Ripard 	u32 reg;
2889a3dfb7SMaxime Ripard 
2989a3dfb7SMaxime Ripard 	if (!(common->features & CCU_FEATURE_FRACTIONAL))
3089a3dfb7SMaxime Ripard 		return;
3189a3dfb7SMaxime Ripard 
3289a3dfb7SMaxime Ripard 	spin_lock_irqsave(common->lock, flags);
3389a3dfb7SMaxime Ripard 	reg = readl(common->base + common->reg);
3489a3dfb7SMaxime Ripard 	writel(reg & ~cf->enable, common->base + common->reg);
3589a3dfb7SMaxime Ripard 	spin_unlock_irqrestore(common->lock, flags);
3689a3dfb7SMaxime Ripard }
37*551b62b1SSamuel Holland EXPORT_SYMBOL_NS_GPL(ccu_frac_helper_enable, SUNXI_CCU);
3889a3dfb7SMaxime Ripard 
ccu_frac_helper_disable(struct ccu_common * common,struct ccu_frac_internal * cf)3989a3dfb7SMaxime Ripard void ccu_frac_helper_disable(struct ccu_common *common,
40a501a14eSMaxime Ripard 			     struct ccu_frac_internal *cf)
4189a3dfb7SMaxime Ripard {
4289a3dfb7SMaxime Ripard 	unsigned long flags;
4389a3dfb7SMaxime Ripard 	u32 reg;
4489a3dfb7SMaxime Ripard 
4589a3dfb7SMaxime Ripard 	if (!(common->features & CCU_FEATURE_FRACTIONAL))
4689a3dfb7SMaxime Ripard 		return;
4789a3dfb7SMaxime Ripard 
4889a3dfb7SMaxime Ripard 	spin_lock_irqsave(common->lock, flags);
4989a3dfb7SMaxime Ripard 	reg = readl(common->base + common->reg);
5089a3dfb7SMaxime Ripard 	writel(reg | cf->enable, common->base + common->reg);
5189a3dfb7SMaxime Ripard 	spin_unlock_irqrestore(common->lock, flags);
5289a3dfb7SMaxime Ripard }
53*551b62b1SSamuel Holland EXPORT_SYMBOL_NS_GPL(ccu_frac_helper_disable, SUNXI_CCU);
5489a3dfb7SMaxime Ripard 
ccu_frac_helper_has_rate(struct ccu_common * common,struct ccu_frac_internal * cf,unsigned long rate)5589a3dfb7SMaxime Ripard bool ccu_frac_helper_has_rate(struct ccu_common *common,
56a501a14eSMaxime Ripard 			      struct ccu_frac_internal *cf,
5789a3dfb7SMaxime Ripard 			      unsigned long rate)
5889a3dfb7SMaxime Ripard {
5989a3dfb7SMaxime Ripard 	if (!(common->features & CCU_FEATURE_FRACTIONAL))
6089a3dfb7SMaxime Ripard 		return false;
6189a3dfb7SMaxime Ripard 
6289a3dfb7SMaxime Ripard 	return (cf->rates[0] == rate) || (cf->rates[1] == rate);
6389a3dfb7SMaxime Ripard }
64*551b62b1SSamuel Holland EXPORT_SYMBOL_NS_GPL(ccu_frac_helper_has_rate, SUNXI_CCU);
6589a3dfb7SMaxime Ripard 
ccu_frac_helper_read_rate(struct ccu_common * common,struct ccu_frac_internal * cf)6689a3dfb7SMaxime Ripard unsigned long ccu_frac_helper_read_rate(struct ccu_common *common,
67a501a14eSMaxime Ripard 					struct ccu_frac_internal *cf)
6889a3dfb7SMaxime Ripard {
6989a3dfb7SMaxime Ripard 	u32 reg;
7089a3dfb7SMaxime Ripard 
71b655f36eSJernej Škrabec 	pr_debug("%s: Read fractional\n", clk_hw_get_name(&common->hw));
7289a3dfb7SMaxime Ripard 
7389a3dfb7SMaxime Ripard 	if (!(common->features & CCU_FEATURE_FRACTIONAL))
7489a3dfb7SMaxime Ripard 		return 0;
7589a3dfb7SMaxime Ripard 
76b655f36eSJernej Škrabec 	pr_debug("%s: clock is fractional (rates %lu and %lu)\n",
7789a3dfb7SMaxime Ripard 		 clk_hw_get_name(&common->hw), cf->rates[0], cf->rates[1]);
7889a3dfb7SMaxime Ripard 
7989a3dfb7SMaxime Ripard 	reg = readl(common->base + common->reg);
8089a3dfb7SMaxime Ripard 
81b655f36eSJernej Škrabec 	pr_debug("%s: clock reg is 0x%x (select is 0x%x)\n",
8289a3dfb7SMaxime Ripard 		 clk_hw_get_name(&common->hw), reg, cf->select);
8389a3dfb7SMaxime Ripard 
8489a3dfb7SMaxime Ripard 	return (reg & cf->select) ? cf->rates[1] : cf->rates[0];
8589a3dfb7SMaxime Ripard }
86*551b62b1SSamuel Holland EXPORT_SYMBOL_NS_GPL(ccu_frac_helper_read_rate, SUNXI_CCU);
8789a3dfb7SMaxime Ripard 
ccu_frac_helper_set_rate(struct ccu_common * common,struct ccu_frac_internal * cf,unsigned long rate,u32 lock)8889a3dfb7SMaxime Ripard int ccu_frac_helper_set_rate(struct ccu_common *common,
89a501a14eSMaxime Ripard 			     struct ccu_frac_internal *cf,
901d42460aSJernej Škrabec 			     unsigned long rate, u32 lock)
9189a3dfb7SMaxime Ripard {
9289a3dfb7SMaxime Ripard 	unsigned long flags;
9389a3dfb7SMaxime Ripard 	u32 reg, sel;
9489a3dfb7SMaxime Ripard 
9589a3dfb7SMaxime Ripard 	if (!(common->features & CCU_FEATURE_FRACTIONAL))
9689a3dfb7SMaxime Ripard 		return -EINVAL;
9789a3dfb7SMaxime Ripard 
9889a3dfb7SMaxime Ripard 	if (cf->rates[0] == rate)
9989a3dfb7SMaxime Ripard 		sel = 0;
10089a3dfb7SMaxime Ripard 	else if (cf->rates[1] == rate)
10189a3dfb7SMaxime Ripard 		sel = cf->select;
10289a3dfb7SMaxime Ripard 	else
10389a3dfb7SMaxime Ripard 		return -EINVAL;
10489a3dfb7SMaxime Ripard 
10589a3dfb7SMaxime Ripard 	spin_lock_irqsave(common->lock, flags);
10689a3dfb7SMaxime Ripard 	reg = readl(common->base + common->reg);
10789a3dfb7SMaxime Ripard 	reg &= ~cf->select;
10889a3dfb7SMaxime Ripard 	writel(reg | sel, common->base + common->reg);
10989a3dfb7SMaxime Ripard 	spin_unlock_irqrestore(common->lock, flags);
11089a3dfb7SMaxime Ripard 
1111d42460aSJernej Škrabec 	ccu_helper_wait_for_lock(common, lock);
1121d42460aSJernej Škrabec 
11389a3dfb7SMaxime Ripard 	return 0;
11489a3dfb7SMaxime Ripard }
115*551b62b1SSamuel Holland EXPORT_SYMBOL_NS_GPL(ccu_frac_helper_set_rate, SUNXI_CCU);
116