summaryrefslogtreecommitdiff
path: root/drivers/video/tegra/dc/cursor.c
blob: a090f2572e73f6d26deede3233ec6e3113a2098f (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
/*
 * drivers/video/tegra/dc/cursor.c
 *
 * Copyright (c) 2011-2014, NVIDIA CORPORATION, All rights reserved.
 *
 * Author:
 *  Robert Morell <rmorell@nvidia.com>
 *  Jon Mayo <jmayo@nvidia.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/mutex.h>

#include "dc_priv.h"
#include "dc_reg.h"

/* modify val with cursor field set for a given size.
 * ignore val if it is NULL.
 * return non-zero on error, and clear val. */
static inline int cursor_size_value(enum tegra_dc_cursor_size size, u32 *val)
{
	u32 scratch = 0;
	if (!val)
		val = &scratch;
	switch (size) {
	case TEGRA_DC_CURSOR_SIZE_32X32:
		*val |= CURSOR_SIZE_32;
		return 0;
	case TEGRA_DC_CURSOR_SIZE_64X64:
		*val |= CURSOR_SIZE_64;
		return 0;
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && !defined(CONFIG_ARCH_TEGRA_3x_SOC)
	case TEGRA_DC_CURSOR_SIZE_128X128:
		*val |= CURSOR_SIZE_128;
		return 0;
	case TEGRA_DC_CURSOR_SIZE_256X256:
		*val |= CURSOR_SIZE_256;
		return 0;
#endif
	}
	*val = 0;
	return -EINVAL;
}

/* modify val with cursor format.
 * ignore val if it is NULL.
 * return non-zero on error, and clear val. */
static inline u32 cursor_format_value(enum tegra_dc_cursor_format format,
	u32 *val)
{
	u32 scratch = 0;
	if (!val)
		val = &scratch;
	switch (format) {
	case TEGRA_DC_CURSOR_FORMAT_2BIT_LEGACY:
		*val |= CURSOR_MODE_SELECT(0);
		return 0;
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC)
	case TEGRA_DC_CURSOR_FORMAT_RGBA_NON_PREMULT_ALPHA:
		*val |= CURSOR_MODE_SELECT(1);
# if !defined(CONFIG_ARCH_TEGRA_11x_SOC)
		*val |= CURSOR_ALPHA(255) | CURSOR_DST_BLEND_FACTOR_SELECT(2);
		*val |= CURSOR_SRC_BLEND_FACTOR_SELECT(1);
# endif
		return 0;
#endif
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_11x_SOC)
	case TEGRA_DC_CURSOR_FORMAT_RGBA_PREMULT_ALPHA:
		*val |= CURSOR_MODE_SELECT(1);
		*val |= CURSOR_ALPHA(255) | CURSOR_DST_BLEND_FACTOR_SELECT(2);
		*val |= CURSOR_SRC_BLEND_FACTOR_SELECT(0);
		return 0;
#endif
	}
	*val = 0;
	return -EINVAL;
}

static unsigned int set_cursor_start_addr(struct tegra_dc *dc,
	enum tegra_dc_cursor_size size, dma_addr_t phys_addr)
{
	u32 val = 0;
	int clip_win;

	BUG_ON(phys_addr & ~CURSOR_START_ADDR_MASK);

	dc->cursor.phys_addr = phys_addr;
	dc->cursor.size = size;
	/* this should not fail, as tegra_dc_cursor_image() checks the size */
	if (WARN(cursor_size_value(size, &val), "invalid cursor size."))
		return 0;

	clip_win = dc->cursor.clip_win;
	val |= CURSOR_CLIP_SHIFT_BITS(clip_win);
#if defined(CONFIG_ARCH_TEGRA_2x_SOC) || defined(CONFIG_ARCH_TEGRA_3x_SOC) || \
	defined(CONFIG_ARCH_TEGRA_11x_SOC) || defined(CONFIG_ARCH_TEGRA_14x_SOC)
	tegra_dc_writel(dc, val | CURSOR_START_ADDR(((unsigned long)phys_addr)),
		DC_DISP_CURSOR_START_ADDR);
#else
	/* TODO: check calculation with HW */
	tegra_dc_writel(dc, (u32)(CURSOR_START_ADDR_HI(phys_addr)),
		DC_DISP_CURSOR_START_ADDR_HI);
	tegra_dc_writel(dc, (u32)(val | CURSOR_START_ADDR_LOW(phys_addr)),
		DC_DISP_CURSOR_START_ADDR);
#endif

#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_11x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_14x_SOC)
	tegra_dc_writel(dc, CURSOR_UPDATE, DC_CMD_STATE_CONTROL);
	tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
	return 0;
#else
	return 1;
#endif
}

static int set_cursor_position(struct tegra_dc *dc, s16 x, s16 y)
{
	tegra_dc_writel(dc, CURSOR_POSITION(x, y), DC_DISP_CURSOR_POSITION);

#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_11x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_14x_SOC)
	tegra_dc_writel(dc, CURSOR_UPDATE, DC_CMD_STATE_CONTROL);
	tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
	return 0;
#else
	return 1;
#endif
}

static int set_cursor_activation_control(struct tegra_dc *dc)
{
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_11x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_14x_SOC)
	u32 reg = tegra_dc_readl(dc, DC_CMD_REG_ACT_CONTROL);

	if ((reg & (1 << CURSOR_ACT_CNTR_SEL)) ==
	    (CURSOR_ACT_CNTR_SEL_V << CURSOR_ACT_CNTR_SEL)) {
		reg &= ~(1 << CURSOR_ACT_CNTR_SEL);
		reg |= (CURSOR_ACT_CNTR_SEL_V << CURSOR_ACT_CNTR_SEL);
		tegra_dc_writel(dc, reg, DC_CMD_REG_ACT_CONTROL);
		return 1;
	}
#endif
	return 0;
}

static int set_cursor_enable(struct tegra_dc *dc, bool enable)
{
	u32 val = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
	if (!!(val & CURSOR_ENABLE) != enable) {
		val &= ~CURSOR_ENABLE;
		if (enable)
			val |= CURSOR_ENABLE;
		tegra_dc_writel(dc, val, DC_DISP_DISP_WIN_OPTIONS);
		return 1;
	}
	dc->cursor.enabled = enable;
	return 0;
}

static int set_cursor_blend(struct tegra_dc *dc, u32 format)
{
	u32 val = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);

	u32 newval = WINH_CURS_SELECT(0);

	/* this should not fail, as tegra_dc_cursor_image() checks the format */
	if (WARN(cursor_format_value(format, &newval), "invalid cursor format"))
		return 0;

	if (val != newval) {
		tegra_dc_writel(dc, newval, DC_DISP_BLEND_CURSOR_CONTROL);
		return 1;
	}
	dc->cursor.format = format;

	return 0;
}

static int set_cursor_fg_bg(struct tegra_dc *dc, u32 fg, u32 bg)
{
	int general_update_needed = 0;
	/* TODO: check fg/bg against data structure, don't read the HW */
	if (fg != tegra_dc_readl(dc, DC_DISP_CURSOR_FOREGROUND)) {
		tegra_dc_writel(dc, fg, DC_DISP_CURSOR_FOREGROUND);
		general_update_needed |= 1;
	}

	if (bg != tegra_dc_readl(dc, DC_DISP_CURSOR_BACKGROUND)) {
		tegra_dc_writel(dc, bg, DC_DISP_CURSOR_BACKGROUND);
		general_update_needed |= 1;
	}
	dc->cursor.fg = fg;
	dc->cursor.bg = bg;

	return general_update_needed;
}

static void tegra_dc_cursor_do_update(struct tegra_dc *dc,
	bool need_general_update)
{
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_11x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_14x_SOC)
	tegra_dc_writel(dc, CURSOR_UPDATE, DC_CMD_STATE_CONTROL);
	tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
#else
	need_general_update = true;
#endif
	if (need_general_update) {
		tegra_dc_writel(dc, GENERAL_UPDATE, DC_CMD_STATE_CONTROL);
		tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
	}
}

static int tegra_dc_cursor_program(struct tegra_dc *dc)
{
	bool need_general_update = false;

	if (!dc->enabled)
		return 0;
	/* these checks are redundant */
	if (cursor_size_value(dc->cursor.size, NULL))
		return -EINVAL;
	if (cursor_format_value(dc->cursor.format, NULL))
		return -EINVAL;

	mutex_lock(&dc->lock);
	if (!dc->cursor.dirty) {
		mutex_unlock(&dc->lock);
		return 0;
	}
	tegra_dc_get(dc);

	need_general_update |= set_cursor_start_addr(dc, dc->cursor.size,
		dc->cursor.phys_addr);

	need_general_update |= set_cursor_fg_bg(dc,
		dc->cursor.fg, dc->cursor.bg);

	need_general_update |= set_cursor_blend(dc, dc->cursor.format);

	need_general_update |= set_cursor_enable(dc, dc->cursor.enabled);

	need_general_update |= set_cursor_position(dc,
		dc->cursor.x, dc->cursor.y);

	need_general_update |= set_cursor_activation_control(dc);

	tegra_dc_cursor_do_update(dc, need_general_update);

	tegra_dc_put(dc);
	dc->cursor.dirty = false;
	mutex_unlock(&dc->lock);

	return 0;
}

int tegra_dc_cursor_image(struct tegra_dc *dc,
	enum tegra_dc_cursor_format format, enum tegra_dc_cursor_size size,
	u32 fg, u32 bg, dma_addr_t phys_addr)
{
	if (cursor_size_value(size, NULL))
		return -EINVAL;

	if (cursor_format_value(format, NULL))
		return -EINVAL;

	mutex_lock(&dc->lock);
	dc->cursor.fg = fg;
	dc->cursor.bg = bg;
	dc->cursor.size = size;
	dc->cursor.format = format;
	dc->cursor.phys_addr = phys_addr;
	dc->cursor.dirty = true;
	mutex_unlock(&dc->lock);

	return tegra_dc_cursor_program(dc);
}

int tegra_dc_cursor_set(struct tegra_dc *dc, bool enable, int x, int y)
{
	mutex_lock(&dc->lock);
	dc->cursor.x = x;
	dc->cursor.y = y;
	dc->cursor.enabled = enable;
	dc->cursor.dirty = true;
	mutex_unlock(&dc->lock);

	return tegra_dc_cursor_program(dc);
}

/* clip:
 * 0 - display
 * 1 - window A
 * 2 - window B
 * 3 - window C
 */
int tegra_dc_cursor_clip(struct tegra_dc *dc, unsigned clip)
{
	mutex_lock(&dc->lock);
	dc->cursor.clip_win = clip;
	dc->cursor.dirty = true;
	mutex_unlock(&dc->lock);

	return tegra_dc_cursor_program(dc);
}

/* disable the cursor on suspend. but leave the state unmodified */
int tegra_dc_cursor_suspend(struct tegra_dc *dc)
{
	u32 val;

	if (!dc->enabled)
		return 0;
	mutex_lock(&dc->lock);
	tegra_dc_get(dc);
	val = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
	val &= ~CURSOR_ENABLE;
	tegra_dc_cursor_do_update(dc, true);
	dc->cursor.dirty = true;
	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);
	return 0;
}

/* restore the state */
int tegra_dc_cursor_resume(struct tegra_dc *dc)
{
	return tegra_dc_cursor_program(dc);
}