aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-msm/clock-dsi-8610.c
blob: 10294c75b949d171f003bde0e0bb078796a5902c (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/iopoll.h>
#include <linux/clk.h>
#include <linux/clk/msm-clk-provider.h>

#include <asm/processor.h>
#include <mach/msm_iomap.h>

#include "clock-dsi-8610.h"

#define DSI_PHY_PHYS		0xFDD00000
#define DSI_PHY_SIZE		0x00100000

#define DSI_CTRL		0x0000
#define DSI_DSIPHY_PLL_CTRL_0	0x0200
#define DSI_DSIPHY_PLL_CTRL_1	0x0204
#define DSI_DSIPHY_PLL_CTRL_2	0x0208
#define DSI_DSIPHY_PLL_CTRL_3	0x020C
#define DSI_DSIPHY_PLL_RDY	0x0280
#define DSI_DSIPHY_PLL_CTRL_8	0x0220
#define DSI_DSIPHY_PLL_CTRL_9	0x0224
#define DSI_DSIPHY_PLL_CTRL_10	0x0228

#define DSI_BPP			3
#define DSI_PLL_RDY_BIT		0x01
#define DSI_PLL_RDY_LOOP_COUNT	80000
#define DSI_MAX_DIVIDER		256

static unsigned char *dsi_base;
static struct clk *dsi_ahb_clk;

int __init dsi_clk_ctrl_init(struct clk *ahb_clk)
{
	dsi_base = ioremap(DSI_PHY_PHYS, DSI_PHY_SIZE);
	if (!dsi_base) {
		pr_err("unable to remap dsi base\n");
		return -ENODEV;
	}

	dsi_ahb_clk = ahb_clk;
	return 0;
}

static int dsi_pll_vco_enable(struct clk *c)
{
	u32 status;
	int i = 0, ret = 0;

	ret = clk_enable(dsi_ahb_clk);
	if (ret) {
		pr_err("fail to enable dsi ahb clk\n");
		return ret;
	}

	writel_relaxed(0x01, dsi_base + DSI_DSIPHY_PLL_CTRL_0);

	do {
		status = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_RDY);
	} while (!(status & DSI_PLL_RDY_BIT) && (i++ < DSI_PLL_RDY_LOOP_COUNT));

	if (!(status & DSI_PLL_RDY_BIT)) {
		pr_err("DSI PLL not ready, polling time out!\n");
		ret = -ETIMEDOUT;
	}

	clk_disable(dsi_ahb_clk);

	return ret;
}

static void dsi_pll_vco_disable(struct clk *c)
{
	int ret;

	ret = clk_enable(dsi_ahb_clk);
	if (ret) {
		pr_err("fail to enable dsi ahb clk\n");
		return;
	}

	writel_relaxed(0x00, dsi_base + DSI_DSIPHY_PLL_CTRL_0);
	clk_disable(dsi_ahb_clk);
}

static int dsi_pll_vco_set_rate(struct clk *c, unsigned long rate)
{
	int ret;
	u32 temp, val;
	unsigned long fb_divider;
	struct clk *parent = c->parent;
	struct dsi_pll_vco_clk *vco_clk =
				container_of(c, struct dsi_pll_vco_clk, c);

	if (!rate)
		return 0;

	ret = clk_prepare_enable(dsi_ahb_clk);
	if (ret) {
		pr_err("fail to enable dsi ahb clk\n");
		return ret;
	}

	temp = rate / 10;
	val = parent->rate / 10;
	fb_divider = (temp * vco_clk->pref_div_ratio) / val;
	fb_divider = fb_divider / 2 - 1;

	temp =  readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_1);
	val = (temp & 0xFFFFFF00) | (fb_divider & 0xFF);
	writel_relaxed(val, dsi_base + DSI_DSIPHY_PLL_CTRL_1);

	temp =  readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_2);
	val = (temp & 0xFFFFFFF8) | ((fb_divider >> 8) & 0x07);
	writel_relaxed(val, dsi_base + DSI_DSIPHY_PLL_CTRL_2);

	temp =  readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_3);
	val = (temp & 0xFFFFFFC0) | (vco_clk->pref_div_ratio - 1);
	writel_relaxed(val, dsi_base + DSI_DSIPHY_PLL_CTRL_3);

	clk_disable_unprepare(dsi_ahb_clk);

	return 0;
}

/* rate is the bit clk rate */
static long dsi_pll_vco_round_rate(struct clk *c, unsigned long rate)
{
	long vco_rate;
	struct dsi_pll_vco_clk *vco_clk =
		container_of(c, struct dsi_pll_vco_clk, c);


	vco_rate = rate;
	if (rate < vco_clk->vco_clk_min)
		vco_rate = vco_clk->vco_clk_min;
	else if (rate > vco_clk->vco_clk_max)
		vco_rate = vco_clk->vco_clk_max;

	return vco_rate;
}

static unsigned long dsi_pll_vco_get_rate(struct clk *c)
{
	u32 fb_divider, ref_divider, vco_rate;
	u32 temp, status;
	struct clk *parent = c->parent;

	status = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_RDY);
	if (status & DSI_PLL_RDY_BIT) {
		fb_divider = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_1);
		fb_divider &= 0xFF;
		temp = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_2) & 0x07;
		fb_divider = (temp << 8) | fb_divider;
		fb_divider += 1;

		ref_divider = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_3);
		ref_divider &= 0x3F;
		ref_divider += 1;

		vco_rate = (parent->rate / ref_divider) * fb_divider;
	} else {
		vco_rate = 0;
	}
	return vco_rate;
}

static enum handoff dsi_pll_vco_handoff(struct clk *c)
{
	u32 status;

	if (clk_prepare_enable(dsi_ahb_clk)) {
		pr_err("fail to enable dsi ahb clk\n");
		return HANDOFF_DISABLED_CLK;
	}

	status = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_0);
	if (!status & DSI_PLL_RDY_BIT) {
		pr_err("DSI PLL not ready\n");
		clk_disable_unprepare(dsi_ahb_clk);
		return HANDOFF_DISABLED_CLK;
	}

	c->rate = dsi_pll_vco_get_rate(c);

	clk_disable_unprepare(dsi_ahb_clk);

	return HANDOFF_ENABLED_CLK;
}

static int dsi_byteclk_set_rate(struct clk *c, unsigned long rate)
{
	int div, ret;
	long vco_rate;
	unsigned long bitclk_rate;
	u32 temp, val;
	struct clk *parent = clk_get_parent(c);

	if (rate == 0) {
		ret = clk_set_rate(parent, 0);
		return ret;
	}

	bitclk_rate = rate * 8;
	for (div = 1; div < DSI_MAX_DIVIDER; div++) {
		vco_rate = clk_round_rate(parent, bitclk_rate * div);

		if (vco_rate == bitclk_rate * div)
			break;

		if (vco_rate < bitclk_rate * div)
			return -EINVAL;
	}

	if (vco_rate != bitclk_rate * div)
		return -EINVAL;

	ret = clk_set_rate(parent, vco_rate);
	if (ret) {
		pr_err("fail to set vco rate\n");
		return ret;
	}

	ret = clk_prepare_enable(dsi_ahb_clk);
	if (ret) {
		pr_err("fail to enable dsi ahb clk\n");
		return ret;
	}

	/* set the bit clk divider */
	temp =  readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_8);
	val = (temp & 0xFFFFFFF0) | (div - 1);
	writel_relaxed(val, dsi_base + DSI_DSIPHY_PLL_CTRL_8);

	/* set the byte clk divider */
	temp = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_9);
	val = (temp & 0xFFFFFF00) | (vco_rate / rate - 1);
	writel_relaxed(val, dsi_base + DSI_DSIPHY_PLL_CTRL_9);
	clk_disable_unprepare(dsi_ahb_clk);

	return 0;
}

static long dsi_byteclk_round_rate(struct clk *c, unsigned long rate)
{
	int div;
	long vco_rate;
	unsigned long bitclk_rate;
	struct clk *parent = clk_get_parent(c);

	if (rate == 0)
		return -EINVAL;

	bitclk_rate = rate * 8;
	for (div = 1; div < DSI_MAX_DIVIDER; div++) {
		vco_rate = clk_round_rate(parent, bitclk_rate * div);
		if (vco_rate == bitclk_rate * div)
			break;
		if (vco_rate < bitclk_rate * div)
			return -EINVAL;
	}

	if (vco_rate != bitclk_rate * div)
		return -EINVAL;

	return rate;
}

static enum handoff dsi_byteclk_handoff(struct clk *c)
{
	struct clk *parent = clk_get_parent(c);
	unsigned long vco_rate = clk_get_rate(parent);
	u32 out_div2;

	if (vco_rate == 0)
		return HANDOFF_DISABLED_CLK;

	if (clk_prepare_enable(dsi_ahb_clk)) {
		pr_err("fail to enable dsi ahb clk\n");
		return HANDOFF_DISABLED_CLK;
	}

	out_div2 = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_9);
	out_div2 &= 0xFF;
	c->rate = vco_rate / (out_div2 + 1);
	clk_disable_unprepare(dsi_ahb_clk);

	return HANDOFF_ENABLED_CLK;
}

static int dsi_dsiclk_set_rate(struct clk *c, unsigned long rate)
{
	u32 temp, val;
	int ret;
	struct clk *parent = clk_get_parent(c);
	unsigned long vco_rate = clk_get_rate(parent);

	if (rate == 0)
		return 0;

	if (vco_rate % rate != 0) {
		pr_err("dsiclk_set_rate invalid rate\n");
		return -EINVAL;
	}

	ret = clk_prepare_enable(dsi_ahb_clk);
	if (ret) {
		pr_err("fail to enable dsi ahb clk\n");
		return ret;
	}
	temp =	readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_10);
	val = (temp & 0xFFFFFF00) | (vco_rate / rate - 1);
	writel_relaxed(val, dsi_base + DSI_DSIPHY_PLL_CTRL_10);
	clk_disable_unprepare(dsi_ahb_clk);

	return 0;
}

static long dsi_dsiclk_round_rate(struct clk *c, unsigned long rate)
{
	/* rate is the pixel clk rate, translate into dsi clk rate*/
	struct clk *parent = clk_get_parent(c);
	unsigned long vco_rate = clk_get_rate(parent);

	rate *= DSI_BPP;

	if (vco_rate < rate)
		return -EINVAL;

	if (vco_rate % rate != 0)
		return -EINVAL;

	return rate;
}

static enum handoff dsi_dsiclk_handoff(struct clk *c)
{
	struct clk *parent = clk_get_parent(c);
	unsigned long vco_rate = clk_get_rate(parent);
	u32 out_div3;

	if (vco_rate == 0)
		return HANDOFF_DISABLED_CLK;

	if (clk_prepare_enable(dsi_ahb_clk)) {
		pr_err("fail to enable dsi ahb clk\n");
		return HANDOFF_DISABLED_CLK;
	}

	out_div3 = readl_relaxed(dsi_base + DSI_DSIPHY_PLL_CTRL_10);
	out_div3 &= 0xFF;
	c->rate = vco_rate / (out_div3 + 1);
	clk_disable_unprepare(dsi_ahb_clk);

	return HANDOFF_ENABLED_CLK;
}

struct clk_ops clk_ops_dsi_dsiclk = {
	.set_rate = dsi_dsiclk_set_rate,
	.round_rate = dsi_dsiclk_round_rate,
	.handoff = dsi_dsiclk_handoff,
};

struct clk_ops clk_ops_dsi_byteclk = {
	.set_rate = dsi_byteclk_set_rate,
	.round_rate = dsi_byteclk_round_rate,
	.handoff = dsi_byteclk_handoff,
};

struct clk_ops clk_ops_dsi_vco = {
	.enable = dsi_pll_vco_enable,
	.disable = dsi_pll_vco_disable,
	.set_rate = dsi_pll_vco_set_rate,
	.round_rate = dsi_pll_vco_round_rate,
	.handoff = dsi_pll_vco_handoff,
};