1 /*
2  * Copyright 2018 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #include "head.h"
23 #include "atom.h"
24 #include "core.h"
25 
26 static void
27 headc57d_or(struct nv50_head *head, struct nv50_head_atom *asyh)
28 {
29 	struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->core->chan;
30 	u8 depth;
31 	u32 *push;
32 
33 	if ((push = evo_wait(core, 2))) {
34 		/*XXX: This is a dirty hack until OR depth handling is
35 		 *     improved later for deep colour etc.
36 		 */
37 		switch (asyh->or.depth) {
38 		case 6: depth = 5; break;
39 		case 5: depth = 4; break;
40 		case 2: depth = 1; break;
41 		case 0:	depth = 4; break;
42 		default:
43 			depth = asyh->or.depth;
44 			WARN_ON(1);
45 			break;
46 		}
47 
48 		evo_mthd(push, 0x2004 + (head->base.index * 0x400), 1);
49 		evo_data(push, 0xfc000000 |
50 			       depth << 4 |
51 			       asyh->or.nvsync << 3 |
52 			       asyh->or.nhsync << 2 |
53 			       asyh->or.crc_raster);
54 		evo_kick(push, core);
55 	}
56 }
57 
58 static void
59 headc57d_procamp(struct nv50_head *head, struct nv50_head_atom *asyh)
60 {
61 	struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->core->chan;
62 	u32 *push;
63 	if ((push = evo_wait(core, 2))) {
64 		evo_mthd(push, 0x2000 + (head->base.index * 0x400), 1);
65 #if 0
66 		evo_data(push, 0x80000000 |
67 			       asyh->procamp.sat.sin << 16 |
68 			       asyh->procamp.sat.cos << 4);
69 #else
70 		evo_data(push, 0);
71 #endif
72 		evo_kick(push, core);
73 	}
74 }
75 
76 void
77 headc57d_olut_clr(struct nv50_head *head)
78 {
79 	struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->core->chan;
80 	u32 *push;
81 	if ((push = evo_wait(core, 2))) {
82 		evo_mthd(push, 0x2288 + (head->base.index * 0x400), 1);
83 		evo_data(push, 0x00000000);
84 		evo_kick(push, core);
85 	}
86 }
87 
88 void
89 headc57d_olut_set(struct nv50_head *head, struct nv50_head_atom *asyh)
90 {
91 	struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->core->chan;
92 	u32 *push;
93 	if ((push = evo_wait(core, 4))) {
94 		evo_mthd(push, 0x2280 + (head->base.index * 0x400), 4);
95 		evo_data(push, asyh->olut.size << 8 |
96 			       asyh->olut.mode << 2 |
97 			       asyh->olut.output_mode);
98 		evo_data(push, 0xffffffff); /* FP_NORM_SCALE. */
99 		evo_data(push, asyh->olut.handle);
100 		evo_data(push, asyh->olut.offset >> 8);
101 		evo_kick(push, core);
102 	}
103 }
104 
105 static void
106 headc57d_olut_load_8(struct drm_color_lut *in, int size, void __iomem *mem)
107 {
108 	memset_io(mem, 0x00, 0x20); /* VSS header. */
109 	mem += 0x20;
110 
111 	while (size--) {
112 		u16 r = drm_color_lut_extract(in->  red + 0, 16);
113 		u16 g = drm_color_lut_extract(in->green + 0, 16);
114 		u16 b = drm_color_lut_extract(in-> blue + 0, 16);
115 		u16 ri = 0, gi = 0, bi = 0, i;
116 
117 		if (in++, size) {
118 			ri = (drm_color_lut_extract(in->  red, 16) - r) / 4;
119 			gi = (drm_color_lut_extract(in->green, 16) - g) / 4;
120 			bi = (drm_color_lut_extract(in-> blue, 16) - b) / 4;
121 		}
122 
123 		for (i = 0; i < 4; i++, mem += 8) {
124 			writew(r + ri * i, mem + 0);
125 			writew(g + gi * i, mem + 2);
126 			writew(b + bi * i, mem + 4);
127 		}
128 	}
129 
130 	/* INTERPOLATE modes require a "next" entry to interpolate with,
131 	 * so we replicate the last entry to deal with this for now.
132 	 */
133 	writew(readw(mem - 8), mem + 0);
134 	writew(readw(mem - 6), mem + 2);
135 	writew(readw(mem - 4), mem + 4);
136 }
137 
138 static void
139 headc57d_olut_load(struct drm_color_lut *in, int size, void __iomem *mem)
140 {
141 	memset_io(mem, 0x00, 0x20); /* VSS header. */
142 	mem += 0x20;
143 
144 	for (; size--; in++, mem += 0x08) {
145 		writew(drm_color_lut_extract(in->  red, 16), mem + 0);
146 		writew(drm_color_lut_extract(in->green, 16), mem + 2);
147 		writew(drm_color_lut_extract(in-> blue, 16), mem + 4);
148 	}
149 
150 	/* INTERPOLATE modes require a "next" entry to interpolate with,
151 	 * so we replicate the last entry to deal with this for now.
152 	 */
153 	writew(readw(mem - 8), mem + 0);
154 	writew(readw(mem - 6), mem + 2);
155 	writew(readw(mem - 4), mem + 4);
156 }
157 
158 bool
159 headc57d_olut(struct nv50_head *head, struct nv50_head_atom *asyh, int size)
160 {
161 	if (size != 0 && size != 256 && size != 1024)
162 		return false;
163 
164 	asyh->olut.mode = 2; /* DIRECT10 */
165 	asyh->olut.size = 4 /* VSS header. */ + 1024 + 1 /* Entries. */;
166 	asyh->olut.output_mode = 1; /* INTERPOLATE_ENABLE. */
167 	if (size == 256)
168 		asyh->olut.load = headc57d_olut_load_8;
169 	else
170 		asyh->olut.load = headc57d_olut_load;
171 	return true;
172 }
173 
174 static void
175 headc57d_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
176 {
177 	struct nv50_dmac *core = &nv50_disp(head->base.base.dev)->core->chan;
178 	struct nv50_head_mode *m = &asyh->mode;
179 	u32 *push;
180 	if ((push = evo_wait(core, 13))) {
181 		evo_mthd(push, 0x2064 + (head->base.index * 0x400), 5);
182 		evo_data(push, (m->v.active  << 16) | m->h.active );
183 		evo_data(push, (m->v.synce   << 16) | m->h.synce  );
184 		evo_data(push, (m->v.blanke  << 16) | m->h.blanke );
185 		evo_data(push, (m->v.blanks  << 16) | m->h.blanks );
186 		evo_data(push, (m->v.blank2e << 16) | m->v.blank2s);
187 		evo_mthd(push, 0x2008 + (head->base.index * 0x400), 2);
188 		evo_data(push, m->interlace);
189 		evo_data(push, m->clock * 1000);
190 		evo_mthd(push, 0x2028 + (head->base.index * 0x400), 1);
191 		evo_data(push, m->clock * 1000);
192 		/*XXX: HEAD_USAGE_BOUNDS, doesn't belong here. */
193 		evo_mthd(push, 0x2030 + (head->base.index * 0x400), 1);
194 		evo_data(push, 0x00001014);
195 		evo_kick(push, core);
196 	}
197 }
198 
199 const struct nv50_head_func
200 headc57d = {
201 	.view = headc37d_view,
202 	.mode = headc57d_mode,
203 	.olut = headc57d_olut,
204 	.olut_identity = true,
205 	.olut_size = 1024,
206 	.olut_set = headc57d_olut_set,
207 	.olut_clr = headc57d_olut_clr,
208 	.curs_layout = head917d_curs_layout,
209 	.curs_format = headc37d_curs_format,
210 	.curs_set = headc37d_curs_set,
211 	.curs_clr = headc37d_curs_clr,
212 	.dither = headc37d_dither,
213 	.procamp = headc57d_procamp,
214 	.or = headc57d_or,
215 	/* TODO: flexible window mappings */
216 	.static_wndw_map = headc37d_static_wndw_map,
217 };
218