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.
11 #include <linux/clk-provider.h>
12 #include <linux/rational.h>
18 unsigned long n, max_n;
19 unsigned long k, max_k;
20 unsigned long m, max_m;
23 static void ccu_nkm_find_best(unsigned long parent, unsigned long rate,
26 unsigned long best_rate = 0;
27 unsigned long best_n = 0, best_k = 0, best_m = 0;
28 unsigned long _n, _k, _m;
30 for (_k = 1; _k <= nkm->max_k; _k++) {
31 unsigned long tmp_rate;
33 rational_best_approximation(rate / _k, parent,
34 nkm->max_n, nkm->max_m, &_n, &_m);
36 tmp_rate = parent * _n * _k / _m;
41 if ((rate - tmp_rate) < (rate - best_rate)) {
54 static void ccu_nkm_disable(struct clk_hw *hw)
56 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
58 return ccu_gate_helper_disable(&nkm->common, nkm->enable);
61 static int ccu_nkm_enable(struct clk_hw *hw)
63 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
65 return ccu_gate_helper_enable(&nkm->common, nkm->enable);
68 static int ccu_nkm_is_enabled(struct clk_hw *hw)
70 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
72 return ccu_gate_helper_is_enabled(&nkm->common, nkm->enable);
75 static unsigned long ccu_nkm_recalc_rate(struct clk_hw *hw,
76 unsigned long parent_rate)
78 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
79 unsigned long n, m, k;
82 reg = readl(nkm->common.base + nkm->common.reg);
84 n = reg >> nkm->n.shift;
85 n &= (1 << nkm->n.width) - 1;
87 k = reg >> nkm->k.shift;
88 k &= (1 << nkm->k.width) - 1;
90 m = reg >> nkm->m.shift;
91 m &= (1 << nkm->m.width) - 1;
93 return parent_rate * (n + 1) * (k + 1) / (m + 1);
96 static long ccu_nkm_round_rate(struct clk_hw *hw, unsigned long rate,
97 unsigned long *parent_rate)
99 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
100 struct _ccu_nkm _nkm;
102 _nkm.max_n = 1 << nkm->n.width;
103 _nkm.max_k = 1 << nkm->k.width;
104 _nkm.max_m = 1 << nkm->m.width;
106 ccu_nkm_find_best(*parent_rate, rate, &_nkm);
108 return *parent_rate * _nkm.n * _nkm.k / _nkm.m;
111 static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate,
112 unsigned long parent_rate)
114 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
115 struct _ccu_nkm _nkm;
119 _nkm.max_n = 1 << nkm->n.width;
120 _nkm.max_k = 1 << nkm->k.width;
121 _nkm.max_m = 1 << nkm->m.width;
123 ccu_nkm_find_best(parent_rate, rate, &_nkm);
125 spin_lock_irqsave(nkm->common.lock, flags);
127 reg = readl(nkm->common.base + nkm->common.reg);
128 reg &= ~GENMASK(nkm->n.width + nkm->n.shift - 1, nkm->n.shift);
129 reg &= ~GENMASK(nkm->k.width + nkm->k.shift - 1, nkm->k.shift);
130 reg &= ~GENMASK(nkm->m.width + nkm->m.shift - 1, nkm->m.shift);
132 reg |= (_nkm.n - 1) << nkm->n.shift;
133 reg |= (_nkm.k - 1) << nkm->k.shift;
134 reg |= (_nkm.m - 1) << nkm->m.shift;
136 writel(reg, nkm->common.base + nkm->common.reg);
138 spin_unlock_irqrestore(nkm->common.lock, flags);
140 ccu_helper_wait_for_lock(&nkm->common, nkm->lock);
145 const struct clk_ops ccu_nkm_ops = {
146 .disable = ccu_nkm_disable,
147 .enable = ccu_nkm_enable,
148 .is_enabled = ccu_nkm_is_enabled,
150 .recalc_rate = ccu_nkm_recalc_rate,
151 .round_rate = ccu_nkm_round_rate,
152 .set_rate = ccu_nkm_set_rate,