blob: 1dae4f3ed00d97131d6263f118d6dae142070e43 [file] [log] [blame]
Maxime Ripard6174a1e2016-06-29 21:05:31 +02001/*
2 * Copyright (C) 2016 Maxime Ripard
3 * Maxime Ripard <maxime.ripard@free-electrons.com>
4 *
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 */
10
11#include <linux/clk-provider.h>
Maxime Ripard6174a1e2016-06-29 21:05:31 +020012
13#include "ccu_frac.h"
14#include "ccu_gate.h"
15#include "ccu_nm.h"
16
Maxime Ripardee286482016-09-29 22:53:12 +020017struct _ccu_nm {
Maxime Ripard6e0d50d2016-09-29 22:57:26 +020018 unsigned long n, min_n, max_n;
19 unsigned long m, min_m, max_m;
Maxime Ripardee286482016-09-29 22:53:12 +020020};
21
22static void ccu_nm_find_best(unsigned long parent, unsigned long rate,
23 struct _ccu_nm *nm)
24{
25 unsigned long best_rate = 0;
26 unsigned long best_n = 0, best_m = 0;
27 unsigned long _n, _m;
28
Maxime Ripard6e0d50d2016-09-29 22:57:26 +020029 for (_n = nm->min_n; _n <= nm->max_n; _n++) {
30 for (_m = nm->min_m; _m <= nm->max_m; _m++) {
Maxime Ripardee286482016-09-29 22:53:12 +020031 unsigned long tmp_rate = parent * _n / _m;
32
33 if (tmp_rate > rate)
34 continue;
35
36 if ((rate - tmp_rate) < (rate - best_rate)) {
37 best_rate = tmp_rate;
38 best_n = _n;
39 best_m = _m;
40 }
41 }
42 }
43
44 nm->n = best_n;
45 nm->m = best_m;
46}
47
Maxime Ripard6174a1e2016-06-29 21:05:31 +020048static void ccu_nm_disable(struct clk_hw *hw)
49{
50 struct ccu_nm *nm = hw_to_ccu_nm(hw);
51
52 return ccu_gate_helper_disable(&nm->common, nm->enable);
53}
54
55static int ccu_nm_enable(struct clk_hw *hw)
56{
57 struct ccu_nm *nm = hw_to_ccu_nm(hw);
58
59 return ccu_gate_helper_enable(&nm->common, nm->enable);
60}
61
62static int ccu_nm_is_enabled(struct clk_hw *hw)
63{
64 struct ccu_nm *nm = hw_to_ccu_nm(hw);
65
66 return ccu_gate_helper_is_enabled(&nm->common, nm->enable);
67}
68
69static unsigned long ccu_nm_recalc_rate(struct clk_hw *hw,
70 unsigned long parent_rate)
71{
72 struct ccu_nm *nm = hw_to_ccu_nm(hw);
73 unsigned long n, m;
74 u32 reg;
75
76 if (ccu_frac_helper_is_enabled(&nm->common, &nm->frac))
77 return ccu_frac_helper_read_rate(&nm->common, &nm->frac);
78
79 reg = readl(nm->common.base + nm->common.reg);
80
81 n = reg >> nm->n.shift;
82 n &= (1 << nm->n.width) - 1;
83
84 m = reg >> nm->m.shift;
85 m &= (1 << nm->m.width) - 1;
86
87 return parent_rate * (n + 1) / (m + 1);
88}
89
90static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate,
91 unsigned long *parent_rate)
92{
93 struct ccu_nm *nm = hw_to_ccu_nm(hw);
Maxime Ripardee286482016-09-29 22:53:12 +020094 struct _ccu_nm _nm;
Maxime Ripard6174a1e2016-06-29 21:05:31 +020095
Maxime Ripard6e0d50d2016-09-29 22:57:26 +020096 _nm.min_n = 1;
Maxime Ripardee286482016-09-29 22:53:12 +020097 _nm.max_n = 1 << nm->n.width;
Maxime Ripard6e0d50d2016-09-29 22:57:26 +020098 _nm.min_m = 1;
Maxime Ripardee286482016-09-29 22:53:12 +020099 _nm.max_m = nm->m.max ?: 1 << nm->m.width;
Maxime Ripard87ba9e52016-09-06 12:29:04 +0200100
Maxime Ripardee286482016-09-29 22:53:12 +0200101 ccu_nm_find_best(*parent_rate, rate, &_nm);
Maxime Ripard6174a1e2016-06-29 21:05:31 +0200102
Maxime Ripardee286482016-09-29 22:53:12 +0200103 return *parent_rate * _nm.n / _nm.m;
Maxime Ripard6174a1e2016-06-29 21:05:31 +0200104}
105
106static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
107 unsigned long parent_rate)
108{
109 struct ccu_nm *nm = hw_to_ccu_nm(hw);
Maxime Ripardee286482016-09-29 22:53:12 +0200110 struct _ccu_nm _nm;
Maxime Ripard6174a1e2016-06-29 21:05:31 +0200111 unsigned long flags;
Maxime Ripard6174a1e2016-06-29 21:05:31 +0200112 u32 reg;
113
114 if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate))
115 return ccu_frac_helper_set_rate(&nm->common, &nm->frac, rate);
116 else
117 ccu_frac_helper_disable(&nm->common, &nm->frac);
118
Maxime Ripard6e0d50d2016-09-29 22:57:26 +0200119 _nm.min_n = 1;
Maxime Ripardee286482016-09-29 22:53:12 +0200120 _nm.max_n = 1 << nm->n.width;
Maxime Ripard6e0d50d2016-09-29 22:57:26 +0200121 _nm.min_m = 1;
Maxime Ripardee286482016-09-29 22:53:12 +0200122 _nm.max_m = nm->m.max ?: 1 << nm->m.width;
Maxime Ripard87ba9e52016-09-06 12:29:04 +0200123
Maxime Ripardee286482016-09-29 22:53:12 +0200124 ccu_nm_find_best(parent_rate, rate, &_nm);
Maxime Ripard6174a1e2016-06-29 21:05:31 +0200125
126 spin_lock_irqsave(nm->common.lock, flags);
127
128 reg = readl(nm->common.base + nm->common.reg);
129 reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
130 reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
131
Maxime Ripardee286482016-09-29 22:53:12 +0200132 writel(reg | ((_nm.m - 1) << nm->m.shift) | ((_nm.n - 1) << nm->n.shift),
Maxime Ripard6174a1e2016-06-29 21:05:31 +0200133 nm->common.base + nm->common.reg);
134
135 spin_unlock_irqrestore(nm->common.lock, flags);
136
137 ccu_helper_wait_for_lock(&nm->common, nm->lock);
138
139 return 0;
140}
141
142const struct clk_ops ccu_nm_ops = {
143 .disable = ccu_nm_disable,
144 .enable = ccu_nm_enable,
145 .is_enabled = ccu_nm_is_enabled,
146
147 .recalc_rate = ccu_nm_recalc_rate,
148 .round_rate = ccu_nm_round_rate,
149 .set_rate = ccu_nm_set_rate,
150};