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 "lut.h"
23 #include "disp.h"
24 
25 #include <drm/drm_color_mgmt.h>
26 #include <drm/drm_mode.h>
27 #include <drm/drm_property.h>
28 
29 #include <nvif/class.h>
30 
31 u32
32 nv50_lut_load(struct nv50_lut *lut, bool legacy, int buffer,
33 	      struct drm_property_blob *blob)
34 {
35 	struct drm_color_lut *in = (struct drm_color_lut *)blob->data;
36 	void __iomem *mem = lut->mem[buffer].object.map.ptr;
37 	const int size = blob->length / sizeof(*in);
38 	int bits, shift, i;
39 	u16 zero, r, g, b;
40 	u32 addr = lut->mem[buffer].addr;
41 
42 	/* This can't happen.. But it shuts the compiler up. */
43 	if (WARN_ON(size != 256))
44 		return 0;
45 
46 	if (legacy) {
47 		bits = 11;
48 		shift = 3;
49 		zero = 0x0000;
50 	} else {
51 		bits = 14;
52 		shift = 0;
53 		zero = 0x6000;
54 	}
55 
56 	for (i = 0; i < size; i++) {
57 		r = (drm_color_lut_extract(in[i].  red, bits) + zero) << shift;
58 		g = (drm_color_lut_extract(in[i].green, bits) + zero) << shift;
59 		b = (drm_color_lut_extract(in[i]. blue, bits) + zero) << shift;
60 		writew(r, mem + (i * 0x08) + 0);
61 		writew(g, mem + (i * 0x08) + 2);
62 		writew(b, mem + (i * 0x08) + 4);
63 	}
64 
65 	/* INTERPOLATE modes require a "next" entry to interpolate with,
66 	 * so we replicate the last entry to deal with this for now.
67 	 */
68 	writew(r, mem + (i * 0x08) + 0);
69 	writew(g, mem + (i * 0x08) + 2);
70 	writew(b, mem + (i * 0x08) + 4);
71 	return addr;
72 }
73 
74 void
75 nv50_lut_fini(struct nv50_lut *lut)
76 {
77 	int i;
78 	for (i = 0; i < ARRAY_SIZE(lut->mem); i++)
79 		nvif_mem_fini(&lut->mem[i]);
80 }
81 
82 int
83 nv50_lut_init(struct nv50_disp *disp, struct nvif_mmu *mmu,
84 	      struct nv50_lut *lut)
85 {
86 	const u32 size = disp->disp->object.oclass < GF110_DISP ? 257 : 1025;
87 	int i;
88 	for (i = 0; i < ARRAY_SIZE(lut->mem); i++) {
89 		int ret = nvif_mem_init_map(mmu, NVIF_MEM_VRAM, size * 8,
90 					    &lut->mem[i]);
91 		if (ret)
92 			return ret;
93 	}
94 	return 0;
95 }
96