Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / clk / sunxi-ng / ccu_nm.c
blobe35ddd8eec8b15ea7dec3e15910e554b1f4e4cd3
1 /*
2 * Copyright (C) 2016 Maxime Ripard
3 * Maxime Ripard <maxime.ripard@free-electrons.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 */
11 #include <linux/clk-provider.h>
12 #include <linux/rational.h>
14 #include "ccu_frac.h"
15 #include "ccu_gate.h"
16 #include "ccu_nm.h"
18 static void ccu_nm_disable(struct clk_hw *hw)
20 struct ccu_nm *nm = hw_to_ccu_nm(hw);
22 return ccu_gate_helper_disable(&nm->common, nm->enable);
25 static int ccu_nm_enable(struct clk_hw *hw)
27 struct ccu_nm *nm = hw_to_ccu_nm(hw);
29 return ccu_gate_helper_enable(&nm->common, nm->enable);
32 static int ccu_nm_is_enabled(struct clk_hw *hw)
34 struct ccu_nm *nm = hw_to_ccu_nm(hw);
36 return ccu_gate_helper_is_enabled(&nm->common, nm->enable);
39 static unsigned long ccu_nm_recalc_rate(struct clk_hw *hw,
40 unsigned long parent_rate)
42 struct ccu_nm *nm = hw_to_ccu_nm(hw);
43 unsigned long n, m;
44 u32 reg;
46 if (ccu_frac_helper_is_enabled(&nm->common, &nm->frac))
47 return ccu_frac_helper_read_rate(&nm->common, &nm->frac);
49 reg = readl(nm->common.base + nm->common.reg);
51 n = reg >> nm->n.shift;
52 n &= (1 << nm->n.width) - 1;
54 m = reg >> nm->m.shift;
55 m &= (1 << nm->m.width) - 1;
57 return parent_rate * (n + 1) / (m + 1);
60 static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate,
61 unsigned long *parent_rate)
63 struct ccu_nm *nm = hw_to_ccu_nm(hw);
64 unsigned long n, m;
66 rational_best_approximation(rate, *parent_rate,
67 1 << nm->n.width, 1 << nm->m.width,
68 &n, &m);
70 return *parent_rate * n / m;
73 static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
74 unsigned long parent_rate)
76 struct ccu_nm *nm = hw_to_ccu_nm(hw);
77 unsigned long flags;
78 unsigned long n, m;
79 u32 reg;
81 if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate))
82 return ccu_frac_helper_set_rate(&nm->common, &nm->frac, rate);
83 else
84 ccu_frac_helper_disable(&nm->common, &nm->frac);
86 rational_best_approximation(rate, parent_rate,
87 1 << nm->n.width, 1 << nm->m.width,
88 &n, &m);
90 spin_lock_irqsave(nm->common.lock, flags);
92 reg = readl(nm->common.base + nm->common.reg);
93 reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
94 reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
96 writel(reg | ((m - 1) << nm->m.shift) | ((n - 1) << nm->n.shift),
97 nm->common.base + nm->common.reg);
99 spin_unlock_irqrestore(nm->common.lock, flags);
101 ccu_helper_wait_for_lock(&nm->common, nm->lock);
103 return 0;
106 const struct clk_ops ccu_nm_ops = {
107 .disable = ccu_nm_disable,
108 .enable = ccu_nm_enable,
109 .is_enabled = ccu_nm_is_enabled,
111 .recalc_rate = ccu_nm_recalc_rate,
112 .round_rate = ccu_nm_round_rate,
113 .set_rate = ccu_nm_set_rate,