2 * Copyright (C) 2008 Maarten Maathuis.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include "nv50_crtc.h"
30 #include "nv50_display.h"
32 static int nv50_lut_alloc(struct nv50_crtc *crtc)
34 struct mem_block *block;
35 struct drm_file *file_priv = kzalloc(sizeof(struct drm_file), GFP_KERNEL);
36 uint32_t flags = NOUVEAU_MEM_INTERNAL | NOUVEAU_MEM_FB | NOUVEAU_MEM_MAPPED;
44 /* Any file_priv should do as it's pointer is used as identification. */
45 block = nouveau_mem_alloc(crtc->dev, 0, 4096, flags, file_priv);
52 crtc->lut->block = block;
63 static int nv50_lut_free(struct nv50_crtc *crtc)
65 struct drm_file *file_priv = crtc->lut->block->file_priv;
69 nouveau_mem_free(crtc->dev, crtc->lut->block);
76 #define NV50_LUT_INDEX(val, w) ((val << (8 - w)) | (val >> ((w << 1) - 8)))
77 static int nv50_lut_set(struct nv50_crtc *crtc, uint16_t *red, uint16_t *green, uint16_t *blue)
79 uint32_t index = 0, i;
80 struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
81 void __iomem *lut = NULL;
85 if (!crtc->lut || !crtc->lut->block) {
86 DRM_ERROR("Something wrong with the LUT\n");
90 /* 16 bits, red, green, blue, unused, total of 64 bits per index */
91 /* maybe switch to ioremap_wc once it becomes available. */
92 lut = ioremap(dev_priv->fb_phys + crtc->lut->block->start, crtc->lut->block->size);
94 DRM_ERROR("ioremap failed on LUT\n");
98 /* 10 bits lut, with 14 bits values. */
99 switch (crtc->fb->depth) {
102 for (i = 0; i < 32; i++) {
103 index = NV50_LUT_INDEX(i, 5);
104 writew(red[i] >> 2, lut + 8*index + 0);
105 writew(green[i] >> 2, lut + 8*index + 2);
106 writew(blue[i] >> 2, lut + 8*index + 4);
111 for (i = 0; i < 32; i++) {
112 index = NV50_LUT_INDEX(i, 5);
113 writew(red[i] >> 2, lut + 8*index + 0);
114 writew(blue[i] >> 2, lut + 8*index + 4);
117 /* Green has an extra bit. */
118 for (i = 0; i < 64; i++) {
119 index = NV50_LUT_INDEX(i, 6);
120 writew(green[i] >> 2, lut + 8*index + 2);
125 for (i = 0; i < 256; i++) {
126 writew(red[i] >> 2, lut + 8*i + 0);
127 writew(green[i] >> 2, lut + 8*i + 2);
128 writew(blue[i] >> 2, lut + 8*i + 4);
133 crtc->lut->depth = crtc->fb->depth;
141 int nv50_lut_create(struct nv50_crtc *crtc)
150 crtc->lut = kzalloc(sizeof(struct nv50_lut), GFP_KERNEL);
155 rval = nv50_lut_alloc(crtc);
160 /* lut will be inited when fb is bound */
161 crtc->lut->depth = 0;
163 /* function pointers */
164 crtc->lut->set = nv50_lut_set;
175 int nv50_lut_destroy(struct nv50_crtc *crtc)
184 rval = nv50_lut_free(crtc);