aboutsummaryrefslogtreecommitdiff
path: root/linux-core/nv50_lut.c
blob: 5cdf6b5d09358ec98d9e85002a33f4bde0beae51 (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
/*
 * Copyright (C) 2008 Maarten Maathuis.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "nv50_lut.h"
#include "nv50_fb.h"
#include "nv50_crtc.h"
#include "nv50_display.h"

static int nv50_lut_alloc(struct nv50_crtc *crtc)
{
	struct mem_block *block;
	struct drm_file *file_priv = kzalloc(sizeof(struct drm_file), GFP_KERNEL);
	uint32_t flags = NOUVEAU_MEM_INTERNAL | NOUVEAU_MEM_FB | NOUVEAU_MEM_MAPPED;
	int rval = 0;

	NV50_DEBUG("\n");

	if (!file_priv)
		return -ENOMEM;

	/* Any file_priv should do as it's pointer is used as identification. */
	block = nouveau_mem_alloc(crtc->dev, 0, 4096, flags, file_priv);

	if (!block) {
		rval = -ENOMEM;
		goto out;
	}

	crtc->lut->block = block;

	return 0;

out:
	if (file_priv)
		kfree(file_priv);

	return rval;
}

static int nv50_lut_free(struct nv50_crtc *crtc)
{
	struct drm_file *file_priv = crtc->lut->block->file_priv;

	NV50_DEBUG("\n");

	nouveau_mem_free(crtc->dev, crtc->lut->block);

	kfree(file_priv);

	return 0;
}

#define NV50_LUT_INDEX(val, w) ((val << (8 - w)) | (val >> ((w << 1) - 8)))
static int nv50_lut_set(struct nv50_crtc *crtc, uint16_t *red, uint16_t *green, uint16_t *blue)
{
	uint32_t index = 0, i;
	struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
	void __iomem *lut = NULL;

	NV50_DEBUG("\n");

	if (!crtc->lut || !crtc->lut->block) {
		DRM_ERROR("Something wrong with the LUT\n");
		return -EINVAL;
	}

	/* 16 bits, red, green, blue, unused, total of 64 bits per index */
	/* maybe switch to ioremap_wc once it becomes available. */
	lut = ioremap(dev_priv->fb_phys + crtc->lut->block->start, crtc->lut->block->size);
	if (!lut) {
		DRM_ERROR("ioremap failed on LUT\n");
		return -EINVAL;
	}

	/* 10 bits lut, with 14 bits values. */
	switch (crtc->fb->depth) {
		case 15:
			/* R5G5B5 */
			for (i = 0; i < 32; i++) {
				index = NV50_LUT_INDEX(i, 5);
				writew(red[i] >> 2, lut + 8*index + 0);
				writew(green[i] >> 2, lut + 8*index + 2);
				writew(blue[i] >> 2, lut + 8*index + 4);
			}
			break;
		case 16:
			/* R5G6B5 */
			for (i = 0; i < 32; i++) {
				index = NV50_LUT_INDEX(i, 5);
				writew(red[i] >> 2, lut + 8*index + 0);
				writew(blue[i] >> 2, lut + 8*index + 4);
			}

			/* Green has an extra bit. */
			for (i = 0; i < 64; i++) {
				index = NV50_LUT_INDEX(i, 6);
				writew(green[i] >> 2, lut + 8*index + 2);
			}
			break;
		default:
			/* R8G8B8 */
			for (i = 0; i < 256; i++) {
				writew(red[i] >> 2, lut + 8*i + 0);
				writew(green[i] >> 2, lut + 8*i + 2);
				writew(blue[i] >> 2, lut + 8*i + 4);
			}
			break;
	}

	crtc->lut->depth = crtc->fb->depth;

	/* Cleaning time. */
	iounmap(lut);

	return 0;
}

int nv50_lut_create(struct nv50_crtc *crtc)
{
	int rval = 0;

	NV50_DEBUG("\n");

	if (!crtc)
		return -EINVAL;

	crtc->lut = kzalloc(sizeof(struct nv50_lut), GFP_KERNEL);

	if (!crtc->lut)
		return -ENOMEM;

	rval = nv50_lut_alloc(crtc);
	if (rval != 0) {
		goto out;
	}

	/* lut will be inited when fb is bound */
	crtc->lut->depth = 0;

	/* function pointers */
	crtc->lut->set = nv50_lut_set;

	return 0;

out:
	if (crtc->lut)
		kfree(crtc->lut);

	return rval;
}

int nv50_lut_destroy(struct nv50_crtc *crtc)
{
	int rval = 0;

	NV50_DEBUG("\n");

	if (!crtc)
		return -EINVAL;

	rval = nv50_lut_free(crtc);

	kfree(crtc->lut);
	crtc->lut = NULL;

	if (rval != 0)
		return rval;

	return 0;
}