2 * op_gammatool - pandora's LCD gamma adjustment tool
4 * Copyright (c) 2010, GraÅžvydas Ignotas
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the organization nor the
14 * names of its contributors may be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
39 #include <sys/ioctl.h>
41 #include <linux/input.h>
42 #include <linux/matroxfb.h>
47 #define DO_RGB(type,d,after,r,g,b) { \
48 int i, u, c, l, div = (w << 8) / SPLIT; \
49 char lines[8][dotsz * w]; \
51 for (dest = (void *)lines[0], i = 0; i < w; i++) { \
52 d = (i * (1 << r) / w) << (g+b); \
55 for (dest = (void *)lines[1], i = 0; i < w; i++) { \
56 d = (i * (1 << g) / w) << b; \
59 for (dest = (void *)lines[2], i = 0; i < w; i++) { \
60 d = (i * (1 << b) / w); \
63 for (dest = (void *)lines[3], i = 0; i < w; i++) { \
64 d = ((i * (1 << r) / w) << (g+b)) | \
65 ((i * (1 << g) / w) << b) | \
70 for (dest = (void *)lines[4], i = 0; i < w; i++) { \
71 c = (((i << 8) / div + 1) * div) >> 8; \
72 d = ((c * (1 << r) / w) << (g+b)) | \
73 ((c * (1 << g) / w) << b) | \
77 for (dest = (void *)lines[5], i = 0; i < w; i++) { \
78 c = (((i << 8) / div + 1) * div) >> 8; \
79 d = (c * (1 << b) / w); \
82 for (dest = (void *)lines[6], i = 0; i < w; i++) { \
83 c = (((i << 8) / div + 1) * div) >> 8; \
84 d = (c * (1 << g) / w) << b; \
87 for (dest = (void *)lines[7], i = 0; i < w; i++) { \
88 c = (((i << 8) / div + 1) * div) >> 8; \
89 d = (c * (1 << r) / w) << (g+b); \
93 for (u = 0; u < h; u++) { \
95 memcpy((char *)ptr + u * dotsz * w, lines[l], dotsz * w); \
101 static void text_out(void *fbi, int x, int y, int dotsz, int stride, const char *text)
106 fb = (char *)fbi + x * dotsz + y * stride;
108 for (i = 0; i < strlen(text); i++)
112 #define pix(fdmask,add) \
113 if (fontdata8x8[((text[i])*8)+l]&fdmask) \
114 memcpy(fb + l*stride + add*dotsz, &v, dotsz)
129 static void get_config(char *buff, size_t size, int just_dir)
133 home = getenv("HOME");
137 snprintf(buff, size, "%s/.config", home);
139 snprintf(buff, size, "%s/.config/op_gammatool", home);
142 static int do_load(int *gamma)
148 get_config(buff, sizeof(buff), 0);
149 f = fopen(buff, "r");
152 ret = fscanf(f, "%d %d %d %d %d %d %d %d %d %d %d %d\n",
153 &g[0], &g[1], &g[2], &g[3], &g[4], &g[5],
154 &g[6], &g[7], &g[8], &g[9], &g[10], &g[11]);
157 return (ret == 12) ? 0 : -1;
160 static int do_save(const int *gamma)
162 const int *g = gamma;
167 get_config(buff, sizeof(buff), 1);
169 get_config(buff, sizeof(buff), 0);
170 f = fopen(buff, "w");
173 ret = fprintf(f, "%d %d %d %d %d %d %d %d %d %d %d %d\n",
174 g[0], g[1], g[2], g[3], g[4], g[5],
175 g[6], g[7], g[8], g[9], g[10], g[11]);
178 return (ret > 0) ? 0 : -1;
181 int main(int argc, char *argv[])
183 const char *msg_once = "Start - exit, Y - change bpp, A/B - save/load, "
184 "hold R for faster tuning";
185 int pressed_up = 0, pressed_down = 0, pressed_r = 0;
186 struct fb_var_screeninfo fbvar;
187 int fbdev, ifd, ret, sel = 0;
188 int i, w, h, dotsz = 0, stride;
189 int gamma[12] = { 0, };
194 fbdev = open("/dev/fb0", O_RDWR);
196 perror("open(\"/dev/fb0\") failed");
200 ret = ioctl(fbdev, FBIOGET_VSCREENINFO, &fbvar);
202 perror("ioctl(FBIOGET_FSCREENINFO) failed");
206 if (argv[1] != NULL) {
207 fbvar.bits_per_pixel = atoi(argv[1]);
208 ret = ioctl(fbdev, FBIOPUT_VSCREENINFO, &fbvar);
211 perror("ioctl FBIOPUT_VSCREENINFO");
215 printf("visible resolution: %i %i\n", fbvar.xres, fbvar.yres);
216 printf("virtual resolution: %i %i\n", fbvar.xres_virtual, fbvar.yres_virtual);
217 printf("offset from virtual to visible: %i %i\n", fbvar.xoffset, fbvar.yoffset);
218 printf("bits_per_pixel: %i\n", fbvar.bits_per_pixel);
220 ptr = mmap(0, fbvar.xres * fbvar.yres * 4,
221 PROT_WRITE|PROT_READ, MAP_SHARED, fbdev, 0);
222 if (ptr == MAP_FAILED)
223 ptr = mmap(0, fbvar.xres * fbvar.yres * fbvar.bits_per_pixel / 8,
224 PROT_WRITE|PROT_READ, MAP_SHARED, fbdev, 0);
225 if (ptr == MAP_FAILED)
227 perror("mmap(fbptr) failed");
234 // search for gpio keys
235 for (ifd = -1, i = 0; ; i++) {
236 snprintf(buff, sizeof(buff), "/dev/input/event%i", i);
237 ifd = open(buff, O_RDONLY | O_NONBLOCK);
241 ioctl(ifd, EVIOCGNAME(sizeof(buff)), buff);
243 if (strcasestr(buff, "gpio") != NULL)
249 f = fopen("/sys/devices/platform/omapdss/display0/gamma", "r+");
251 f = fopen("/sys/devices/platform/omap2_mcspi.1/spi1.1/gamma", "r+");
255 ret = fscanf(f, "%d %d %d %d %d %d %d %d %d %d %d %d\n",
256 &g[0], &g[1], &g[2], &g[3], &g[4], &g[5],
257 &g[6], &g[7], &g[8], &g[9], &g[10], &g[11]);
261 fprintf(stderr, "gamma file missing or bad\n");
270 fscanf(f, "%d %d %d %d %d %d %d %d %d %d %d %d\n",
271 &g[0], &g[1], &g[2], &g[3], &g[4], &g[5],
272 &g[6], &g[7], &g[8], &g[9], &g[10], &g[11]);
275 //ioctl(fbdev, FBIO_WAITFORVSYNC, &ret);
276 if (fbvar.bits_per_pixel == 16)
279 DO_RGB(unsigned short, dest[i],, 5, 6, 5);
281 else if (fbvar.bits_per_pixel == 24)
287 DO_RGB(unsigned char, d, p = &dest[i*3]; p[0] = d; p[1] = d >> 8; p[2] = d >> 16, 8, 8, 8);
289 else if (fbvar.bits_per_pixel == 32)
292 DO_RGB(unsigned int, dest[i],, 8, 8, 8);
296 fprintf(stderr, "unhandled bpp: %d\n", fbvar.bits_per_pixel);
301 snprintf(buff, sizeof(buff), "%dbpp", fbvar.bits_per_pixel);
302 text_out(ptr, 8, 10, dotsz, stride, buff);
303 if (msg_once != NULL) {
304 text_out(ptr, 80, 10, dotsz, stride, msg_once);
307 for (i = 0; i < 12; i++) {
308 int x = i * w / 12 + 8;
310 text_out(ptr, x, h - 18, dotsz, stride, "____");
311 snprintf(buff, sizeof(buff), "%d", gamma[i]);
312 text_out(ptr, x, h - 20, dotsz, stride, buff);
321 struct input_event ev;
322 ret = read(ifd, &ev, sizeof(ev));
323 if (ret < (int) sizeof(ev)) {
324 if (errno == EAGAIN || errno == EWOULDBLOCK)
325 goto input_no_change;
327 perror("evtest: read error");
331 if (ev.type != EV_KEY)
348 pressed_up = ev.value;
351 pressed_down = ev.value;
355 pressed_r = ev.value;
360 fbvar.bits_per_pixel += 8;
361 if (fbvar.bits_per_pixel > 32)
362 fbvar.bits_per_pixel = 16;
363 ret = ioctl(fbdev, FBIOPUT_VSCREENINFO, &fbvar);
365 perror("ioctl FBIOPUT_VSCREENINFO");
372 if (do_load(gamma) == 0)
375 msg_once = "load failed";
376 goto input_done_gamma;
381 if (do_save(gamma) == 0)
384 msg_once = "save failed";
397 if (gamma[sel] >= 1023)
401 goto input_done_gamma;
409 goto input_done_gamma;
419 fprintf(f, "%d %d %d %d %d %d %d %d %d %d %d %d\n",
420 g[0], g[1], g[2], g[3], g[4], g[5],
421 g[6], g[7], g[8], g[9], g[10], g[11]);
427 munmap(ptr, fbvar.xres * fbvar.yres * fbvar.bits_per_pixel / 8);