162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2018 Red Hat Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci#include "head.h"
2362306a36Sopenharmony_ci#include "atom.h"
2462306a36Sopenharmony_ci#include "core.h"
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#include <nvif/pushc37b.h>
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include <nvhw/class/clc57d.h>
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_cistatic int
3162306a36Sopenharmony_ciheadc57d_or(struct nv50_head *head, struct nv50_head_atom *asyh)
3262306a36Sopenharmony_ci{
3362306a36Sopenharmony_ci	struct nvif_push *push = nv50_disp(head->base.base.dev)->core->chan.push;
3462306a36Sopenharmony_ci	const int i = head->base.index;
3562306a36Sopenharmony_ci	u8 depth;
3662306a36Sopenharmony_ci	int ret;
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	/*XXX: This is a dirty hack until OR depth handling is
3962306a36Sopenharmony_ci	 *     improved later for deep colour etc.
4062306a36Sopenharmony_ci	 */
4162306a36Sopenharmony_ci	switch (asyh->or.depth) {
4262306a36Sopenharmony_ci	case 6: depth = 5; break;
4362306a36Sopenharmony_ci	case 5: depth = 4; break;
4462306a36Sopenharmony_ci	case 2: depth = 1; break;
4562306a36Sopenharmony_ci	case 0:	depth = 4; break;
4662306a36Sopenharmony_ci	default:
4762306a36Sopenharmony_ci		depth = asyh->or.depth;
4862306a36Sopenharmony_ci		WARN_ON(1);
4962306a36Sopenharmony_ci		break;
5062306a36Sopenharmony_ci	}
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 2)))
5362306a36Sopenharmony_ci		return ret;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE(i),
5662306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE, CRC_MODE, asyh->or.crc_raster) |
5762306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE, HSYNC_POLARITY, asyh->or.nhsync) |
5862306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE, VSYNC_POLARITY, asyh->or.nvsync) |
5962306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE, PIXEL_DEPTH, depth) |
6062306a36Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE, COLOR_SPACE_OVERRIDE, DISABLE) |
6162306a36Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE, EXT_PACKET_WIN, NONE));
6262306a36Sopenharmony_ci	return 0;
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_cistatic int
6662306a36Sopenharmony_ciheadc57d_procamp(struct nv50_head *head, struct nv50_head_atom *asyh)
6762306a36Sopenharmony_ci{
6862306a36Sopenharmony_ci	struct nvif_push *push = nv50_disp(head->base.base.dev)->core->chan.push;
6962306a36Sopenharmony_ci	const int i = head->base.index;
7062306a36Sopenharmony_ci	int ret;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 2)))
7362306a36Sopenharmony_ci		return ret;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	//TODO:
7662306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_PROCAMP(i),
7762306a36Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_PROCAMP, COLOR_SPACE, RGB) |
7862306a36Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_PROCAMP, CHROMA_LPF, DISABLE) |
7962306a36Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_PROCAMP, DYNAMIC_RANGE, VESA));
8062306a36Sopenharmony_ci	return 0;
8162306a36Sopenharmony_ci}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistatic int
8462306a36Sopenharmony_ciheadc57d_olut_clr(struct nv50_head *head)
8562306a36Sopenharmony_ci{
8662306a36Sopenharmony_ci	struct nvif_push *push = nv50_disp(head->base.base.dev)->core->chan.push;
8762306a36Sopenharmony_ci	const int i = head->base.index;
8862306a36Sopenharmony_ci	int ret;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 2)))
9162306a36Sopenharmony_ci		return ret;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_CONTEXT_DMA_OLUT(i), 0x00000000);
9462306a36Sopenharmony_ci	return 0;
9562306a36Sopenharmony_ci}
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cistatic int
9862306a36Sopenharmony_ciheadc57d_olut_set(struct nv50_head *head, struct nv50_head_atom *asyh)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	struct nvif_push *push = nv50_disp(head->base.base.dev)->core->chan.push;
10162306a36Sopenharmony_ci	const int i = head->base.index;
10262306a36Sopenharmony_ci	int ret;
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 5)))
10562306a36Sopenharmony_ci		return ret;
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_OLUT_CONTROL(i),
10862306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_OLUT_CONTROL, INTERPOLATE, asyh->olut.output_mode) |
10962306a36Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_OLUT_CONTROL, MIRROR, DISABLE) |
11062306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_OLUT_CONTROL, MODE, asyh->olut.mode) |
11162306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_OLUT_CONTROL, SIZE, asyh->olut.size),
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci				HEAD_SET_OLUT_FP_NORM_SCALE(i), 0xffffffff,
11462306a36Sopenharmony_ci				HEAD_SET_CONTEXT_DMA_OLUT(i), asyh->olut.handle,
11562306a36Sopenharmony_ci				HEAD_SET_OFFSET_OLUT(i), asyh->olut.offset >> 8);
11662306a36Sopenharmony_ci	return 0;
11762306a36Sopenharmony_ci}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_cistatic void
12062306a36Sopenharmony_ciheadc57d_olut_load_8(struct drm_color_lut *in, int size, void __iomem *mem)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	memset_io(mem, 0x00, 0x20); /* VSS header. */
12362306a36Sopenharmony_ci	mem += 0x20;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	while (size--) {
12662306a36Sopenharmony_ci		u16 r = drm_color_lut_extract(in->  red + 0, 16);
12762306a36Sopenharmony_ci		u16 g = drm_color_lut_extract(in->green + 0, 16);
12862306a36Sopenharmony_ci		u16 b = drm_color_lut_extract(in-> blue + 0, 16);
12962306a36Sopenharmony_ci		u16 ri = 0, gi = 0, bi = 0, i;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci		if (in++, size) {
13262306a36Sopenharmony_ci			ri = (drm_color_lut_extract(in->  red, 16) - r) / 4;
13362306a36Sopenharmony_ci			gi = (drm_color_lut_extract(in->green, 16) - g) / 4;
13462306a36Sopenharmony_ci			bi = (drm_color_lut_extract(in-> blue, 16) - b) / 4;
13562306a36Sopenharmony_ci		}
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci		for (i = 0; i < 4; i++, mem += 8) {
13862306a36Sopenharmony_ci			writew(r + ri * i, mem + 0);
13962306a36Sopenharmony_ci			writew(g + gi * i, mem + 2);
14062306a36Sopenharmony_ci			writew(b + bi * i, mem + 4);
14162306a36Sopenharmony_ci		}
14262306a36Sopenharmony_ci	}
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	/* INTERPOLATE modes require a "next" entry to interpolate with,
14562306a36Sopenharmony_ci	 * so we replicate the last entry to deal with this for now.
14662306a36Sopenharmony_ci	 */
14762306a36Sopenharmony_ci	writew(readw(mem - 8), mem + 0);
14862306a36Sopenharmony_ci	writew(readw(mem - 6), mem + 2);
14962306a36Sopenharmony_ci	writew(readw(mem - 4), mem + 4);
15062306a36Sopenharmony_ci}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_cistatic void
15362306a36Sopenharmony_ciheadc57d_olut_load(struct drm_color_lut *in, int size, void __iomem *mem)
15462306a36Sopenharmony_ci{
15562306a36Sopenharmony_ci	memset_io(mem, 0x00, 0x20); /* VSS header. */
15662306a36Sopenharmony_ci	mem += 0x20;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	for (; size--; in++, mem += 0x08) {
15962306a36Sopenharmony_ci		writew(drm_color_lut_extract(in->  red, 16), mem + 0);
16062306a36Sopenharmony_ci		writew(drm_color_lut_extract(in->green, 16), mem + 2);
16162306a36Sopenharmony_ci		writew(drm_color_lut_extract(in-> blue, 16), mem + 4);
16262306a36Sopenharmony_ci	}
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	/* INTERPOLATE modes require a "next" entry to interpolate with,
16562306a36Sopenharmony_ci	 * so we replicate the last entry to deal with this for now.
16662306a36Sopenharmony_ci	 */
16762306a36Sopenharmony_ci	writew(readw(mem - 8), mem + 0);
16862306a36Sopenharmony_ci	writew(readw(mem - 6), mem + 2);
16962306a36Sopenharmony_ci	writew(readw(mem - 4), mem + 4);
17062306a36Sopenharmony_ci}
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_cistatic bool
17362306a36Sopenharmony_ciheadc57d_olut(struct nv50_head *head, struct nv50_head_atom *asyh, int size)
17462306a36Sopenharmony_ci{
17562306a36Sopenharmony_ci	if (size != 0 && size != 256 && size != 1024)
17662306a36Sopenharmony_ci		return false;
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	asyh->olut.mode = NVC57D_HEAD_SET_OLUT_CONTROL_MODE_DIRECT10;
17962306a36Sopenharmony_ci	asyh->olut.size = 4 /* VSS header. */ + 1024 + 1 /* Entries. */;
18062306a36Sopenharmony_ci	asyh->olut.output_mode = NVC57D_HEAD_SET_OLUT_CONTROL_INTERPOLATE_ENABLE;
18162306a36Sopenharmony_ci	if (size == 256)
18262306a36Sopenharmony_ci		asyh->olut.load = headc57d_olut_load_8;
18362306a36Sopenharmony_ci	else
18462306a36Sopenharmony_ci		asyh->olut.load = headc57d_olut_load;
18562306a36Sopenharmony_ci	return true;
18662306a36Sopenharmony_ci}
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_cistatic int
18962306a36Sopenharmony_ciheadc57d_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
19062306a36Sopenharmony_ci{
19162306a36Sopenharmony_ci	struct nvif_push *push = nv50_disp(head->base.base.dev)->core->chan.push;
19262306a36Sopenharmony_ci	struct nv50_head_mode *m = &asyh->mode;
19362306a36Sopenharmony_ci	const int i = head->base.index;
19462306a36Sopenharmony_ci	int ret;
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 15)))
19762306a36Sopenharmony_ci		return ret;
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_RASTER_SIZE(i),
20062306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_SIZE, WIDTH, m->h.active) |
20162306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_SIZE, HEIGHT, m->v.active),
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci				HEAD_SET_RASTER_SYNC_END(i),
20462306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_SYNC_END, X, m->h.synce) |
20562306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_SYNC_END, Y, m->v.synce),
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci				HEAD_SET_RASTER_BLANK_END(i),
20862306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_BLANK_END, X, m->h.blanke) |
20962306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_BLANK_END, Y, m->v.blanke),
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci				HEAD_SET_RASTER_BLANK_START(i),
21262306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_BLANK_START, X, m->h.blanks) |
21362306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_BLANK_START, Y, m->v.blanks));
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	//XXX:
21662306a36Sopenharmony_ci	PUSH_NVSQ(push, NVC57D, 0x2074 + (i * 0x400), m->v.blank2e << 16 | m->v.blank2s);
21762306a36Sopenharmony_ci	PUSH_NVSQ(push, NVC57D, 0x2008 + (i * 0x400), m->interlace);
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_PIXEL_CLOCK_FREQUENCY(i),
22062306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_PIXEL_CLOCK_FREQUENCY, HERTZ, m->clock * 1000));
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_PIXEL_CLOCK_FREQUENCY_MAX(i),
22362306a36Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_PIXEL_CLOCK_FREQUENCY_MAX, HERTZ, m->clock * 1000));
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	/*XXX: HEAD_USAGE_BOUNDS, doesn't belong here. */
22662306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_HEAD_USAGE_BOUNDS(i),
22762306a36Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_HEAD_USAGE_BOUNDS, CURSOR, USAGE_W256_H256) |
22862306a36Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_HEAD_USAGE_BOUNDS, OLUT_ALLOWED, TRUE) |
22962306a36Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_HEAD_USAGE_BOUNDS, OUTPUT_SCALER_TAPS, TAPS_2) |
23062306a36Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_HEAD_USAGE_BOUNDS, UPSCALING_ALLOWED, TRUE));
23162306a36Sopenharmony_ci	return 0;
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ciconst struct nv50_head_func
23562306a36Sopenharmony_ciheadc57d = {
23662306a36Sopenharmony_ci	.view = headc37d_view,
23762306a36Sopenharmony_ci	.mode = headc57d_mode,
23862306a36Sopenharmony_ci	.olut = headc57d_olut,
23962306a36Sopenharmony_ci	.ilut_check = head907d_ilut_check,
24062306a36Sopenharmony_ci	.olut_identity = true,
24162306a36Sopenharmony_ci	.olut_size = 1024,
24262306a36Sopenharmony_ci	.olut_set = headc57d_olut_set,
24362306a36Sopenharmony_ci	.olut_clr = headc57d_olut_clr,
24462306a36Sopenharmony_ci	.curs_layout = head917d_curs_layout,
24562306a36Sopenharmony_ci	.curs_format = headc37d_curs_format,
24662306a36Sopenharmony_ci	.curs_set = headc37d_curs_set,
24762306a36Sopenharmony_ci	.curs_clr = headc37d_curs_clr,
24862306a36Sopenharmony_ci	.dither = headc37d_dither,
24962306a36Sopenharmony_ci	.procamp = headc57d_procamp,
25062306a36Sopenharmony_ci	.or = headc57d_or,
25162306a36Sopenharmony_ci	/* TODO: flexible window mappings */
25262306a36Sopenharmony_ci	.static_wndw_map = headc37d_static_wndw_map,
25362306a36Sopenharmony_ci};
254