summaryrefslogtreecommitdiff
path: root/drivers/clk/actions/owl-pll.c
blob: 02437bdedf4dbfe931026e4cd55c779e89e79eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// SPDX-License-Identifier: GPL-2.0+
//
// OWL pll clock driver
//
// Copyright (c) 2014 Actions Semi Inc.
// Author: David Liu <liuwei@actions-semi.com>
//
// Copyright (c) 2018 Linaro Ltd.
// Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>

#include <linux/clk-provider.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/delay.h>

#include "owl-pll.h"

static u32 owl_pll_calculate_mul(struct owl_pll_hw *pll_hw, unsigned long rate)
{
	u32 mul;

	mul = DIV_ROUND_CLOSEST(rate, pll_hw->bfreq);
	if (mul < pll_hw->min_mul)
		mul = pll_hw->min_mul;
	else if (mul > pll_hw->max_mul)
		mul = pll_hw->max_mul;

	return mul &= mul_mask(pll_hw);
}

static unsigned long _get_table_rate(const struct clk_pll_table *table,
		unsigned int val)
{
	const struct clk_pll_table *clkt;

	for (clkt = table; clkt->rate; clkt++)
		if (clkt->val == val)
			return clkt->rate;

	return 0;
}

static const struct clk_pll_table *_get_pll_table(
		const struct clk_pll_table *table, unsigned long rate)
{
	const struct clk_pll_table *clkt;

	for (clkt = table; clkt->rate; clkt++) {
		if (clkt->rate == rate) {
			table = clkt;
			break;
		} else if (clkt->rate < rate)
			table = clkt;
	}

	return table;
}

static long owl_pll_round_rate(struct clk_hw *hw, unsigned long rate,
		unsigned long *parent_rate)
{
	struct owl_pll *pll = hw_to_owl_pll(hw);
	struct owl_pll_hw *pll_hw = &pll->pll_hw;
	const struct clk_pll_table *clkt;
	u32 mul;

	if (pll_hw->table) {
		clkt = _get_pll_table(pll_hw->table, rate);
		return clkt->rate;
	}

	/* fixed frequency */
	if (pll_hw->width == 0)
		return pll_hw->bfreq;

	mul = owl_pll_calculate_mul(pll_hw, rate);

	return pll_hw->bfreq * mul;
}

static unsigned long owl_pll_recalc_rate(struct clk_hw *hw,
		unsigned long parent_rate)
{
	struct owl_pll *pll = hw_to_owl_pll(hw);
	struct owl_pll_hw *pll_hw = &pll->pll_hw;
	const struct owl_clk_common *common = &pll->common;
	u32 val;

	if (pll_hw->table) {
		regmap_read(common->regmap, pll_hw->reg, &val);

		val = val >> pll_hw->shift;
		val &= mul_mask(pll_hw);

		return _get_table_rate(pll_hw->table, val);
	}

	/* fixed frequency */
	if (pll_hw->width == 0)
		return pll_hw->bfreq;

	regmap_read(common->regmap, pll_hw->reg, &val);

	val = val >> pll_hw->shift;
	val &= mul_mask(pll_hw);

	return pll_hw->bfreq * val;
}

static int owl_pll_is_enabled(struct clk_hw *hw)
{
	struct owl_pll *pll = hw_to_owl_pll(hw);
	struct owl_pll_hw *pll_hw = &pll->pll_hw;
	const struct owl_clk_common *common = &pll->common;
	u32 reg;

	regmap_read(common->regmap, pll_hw->reg, &reg);

	return !!(reg & BIT(pll_hw->bit_idx));
}

static void owl_pll_set(const struct owl_clk_common *common,
		       const struct owl_pll_hw *pll_hw, bool enable)
{
	u32 reg;

	regmap_read(common->regmap, pll_hw->reg, &reg);

	if (enable)
		reg |= BIT(pll_hw->bit_idx);
	else
		reg &= ~BIT(pll_hw->bit_idx);

	regmap_write(common->regmap, pll_hw->reg, reg);
}

static int owl_pll_enable(struct clk_hw *hw)
{
	struct owl_pll *pll = hw_to_owl_pll(hw);
	const struct owl_clk_common *common = &pll->common;

	owl_pll_set(common, &pll->pll_hw, true);

	return 0;
}

static void owl_pll_disable(struct clk_hw *hw)
{
	struct owl_pll *pll = hw_to_owl_pll(hw);
	const struct owl_clk_common *common = &pll->common;

	owl_pll_set(common, &pll->pll_hw, false);
}

static int owl_pll_set_rate(struct clk_hw *hw, unsigned long rate,
		unsigned long parent_rate)
{
	struct owl_pll *pll = hw_to_owl_pll(hw);
	struct owl_pll_hw *pll_hw = &pll->pll_hw;
	const struct owl_clk_common *common = &pll->common;
	const struct clk_pll_table *clkt;
	u32 val, reg;

	/* fixed frequency */
	if (pll_hw->width == 0)
		return 0;

	if (pll_hw->table) {
		clkt = _get_pll_table(pll_hw->table, rate);
		val = clkt->val;
	} else {
		val = owl_pll_calculate_mul(pll_hw, rate);
	}

	regmap_read(common->regmap, pll_hw->reg, &reg);

	reg &= ~mul_mask(pll_hw);
	reg |= val << pll_hw->shift;

	regmap_write(common->regmap, pll_hw->reg, reg);

	udelay(pll_hw->delay);

	return 0;
}

const struct clk_ops owl_pll_ops = {
	.enable = owl_pll_enable,
	.disable = owl_pll_disable,
	.is_enabled = owl_pll_is_enabled,
	.round_rate = owl_pll_round_rate,
	.recalc_rate = owl_pll_recalc_rate,
	.set_rate = owl_pll_set_rate,
};