ite8181.c revision 1.19 1 /* $NetBSD: ite8181.c,v 1.19 2003/07/15 02:29:29 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 2000,2001 SATO Kazumi
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: ite8181.c,v 1.19 2003/07/15 02:29:29 lukem Exp $");
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/systm.h>
37 #include <sys/boot_flag.h>
38 #include <sys/buf.h>
39
40 #include <uvm/uvm_extern.h>
41
42 #include <dev/wscons/wsconsio.h>
43
44 #include <machine/bootinfo.h>
45 #include <machine/bus.h>
46 #include <machine/autoconf.h>
47 #include <machine/config_hook.h>
48 #include <machine/platid.h>
49 #include <machine/platid_mask.h>
50
51 #include <hpcmips/dev/ite8181reg.h>
52 #include <hpcmips/dev/ite8181var.h>
53 #include "bivideo.h"
54 #if NBIVIDEO > 0
55 #include <dev/hpc/bivideovar.h>
56 #endif
57 #include <dev/hpc/hpccmapvar.h>
58
59 #define ITE8181DEBUG
60 #ifdef ITE8181DEBUG
61 #ifndef ITE8181DEBUG_CONF
62 #define ITE8181DEBUG_CONF 0
63 #endif
64 int ite8181_debug = ITE8181DEBUG_CONF;
65 #define DPRINTF(arg) if (ite8181_debug) printf arg
66 #define DPRINTFN(n, arg) if (ite8181_debug > (n)) printf arg
67 #define VPRINTF(arg) if (bootverbose || ite8181_debug) printf arg
68 #define VPRINTFN(n, arg) if (bootverbose || ite8181_debug > (n)) printf arg
69 #else
70 #define DPRINTF(arg)
71 #define DPRINTFN(n, arg)
72 #define VPRINTF(arg) if (bootverbose) printf arg
73 #define VPRINTFN(n, arg) if (bootverbose) printf arg
74 #endif
75
76 #ifndef ITE8181_LCD_CONTROL_ENABLE
77 int ite8181_lcd_control_disable = 1;
78 #else /* ITE8181_LCD_CONTROL_ENABLE */
79 int ite8181_lcd_control_disable = 0;
80 #endif /* ITE8181_LCD_CONTROL_ENABLE */
81
82 #define ITE8181_WINCE_CMAP
83
84 /*
85 * XXX:
86 * IBM WorkPad z50 power unit has too weak power.
87 * So we must wait too many times to access some device
88 * after LCD panel and BackLight on.
89 * Currently delay is not enough ??? FIXME
90 */
91 #ifndef ITE8181_LCD_ON_SELF_DELAY
92 #define ITE8181_LCD_ON_SELF_DELAY 1000
93 #endif /* ITE8181_LCD_ON__SELF_DELAY */
94 #ifndef ITE8181_LCD_ON_DELAY
95 #define ITE8181_LCD_ON_DELAY 2000
96 #endif /* ITE8181_LCD_ON_DELAY */
97 int ite8181_lcd_on_self_delay = ITE8181_LCD_ON_SELF_DELAY; /* msec */
98 int ite8181_lcd_on_delay = ITE8181_LCD_ON_DELAY; /* msec */
99
100 #define MSEC 1000
101 /*
102 * function prototypes
103 */
104 static void ite8181_config_write_4(bus_space_tag_t, bus_space_handle_t,
105 int, int);
106 static int ite8181_config_read_4(bus_space_tag_t, bus_space_handle_t,
107 int);
108 static void ite8181_gui_write_4(struct ite8181_softc *, int, int);
109 static int ite8181_gui_read_4(struct ite8181_softc *, int);
110
111 static void ite8181_gui_write_1(struct ite8181_softc *, int, int);
112 static int ite8181_gui_read_1(struct ite8181_softc *, int);
113
114 static void ite8181_graphics_write_1(struct ite8181_softc *, int, int);
115 static int ite8181_graphics_read_1(struct ite8181_softc *, int);
116
117 static void ite8181_ema_write_1(struct ite8181_softc *, int, int);
118 static int ite8181_ema_read_1(struct ite8181_softc *, int);
119
120 static void ite8181_power(int, void *);
121 static int ite8181_hardpower(void *, int, long, void *);
122 static int ite8181_fbinit(struct hpcfb_fbconf *);
123 static int ite8181_ioctl(void *, u_long, caddr_t, int, struct proc *);
124 static paddr_t ite8181_mmap(void *, off_t offset, int);
125 static void ite8181_erase_cursor(struct ite8181_softc *);
126 static int ite8181_lcd_power(struct ite8181_softc *, int);
127
128 static void ite8181_update_powerstate(struct ite8181_softc *, int);
129 void ite8181_init_backlight(struct ite8181_softc *, int);
130 void ite8181_init_brightness(struct ite8181_softc *, int);
131 void ite8181_init_contrast(struct ite8181_softc *, int);
132 void ite8181_set_brightness(struct ite8181_softc *, int);
133 void ite8181_set_contrast(struct ite8181_softc *, int);
134
135 /*
136 * static variables
137 */
138 struct hpcfb_accessops ite8181_ha = {
139 ite8181_ioctl, ite8181_mmap
140 };
141
142 inline int
143 ite8181_config_read_4(bus_space_tag_t iot, bus_space_handle_t ioh,
144 int byteoffset)
145 {
146
147 return (bus_space_read_4(iot, ioh, ITE8181_CONF_OFFSET + byteoffset));
148 }
149
150 inline void
151 ite8181_config_write_4(bus_space_tag_t iot, bus_space_handle_t ioh,
152 int byteoffset, int data)
153 {
154
155 bus_space_write_4(iot, ioh, ITE8181_CONF_OFFSET + byteoffset, data);
156 }
157
158 inline int
159 ite8181_gui_read_4(struct ite8181_softc *sc, int byteoffset)
160 {
161
162 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
163 sc->sc_gba + byteoffset));
164 }
165
166 inline void
167 ite8181_gui_write_4(struct ite8181_softc *sc, int byteoffset, int data)
168 {
169
170 bus_space_write_4(sc->sc_iot, sc->sc_ioh, sc->sc_gba + byteoffset,
171 data);
172 }
173
174 inline int
175 ite8181_gui_read_1(struct ite8181_softc *sc, int byteoffset)
176 {
177
178 return (bus_space_read_1(sc->sc_iot, sc->sc_ioh,
179 sc->sc_gba + byteoffset));
180 }
181
182 inline void
183 ite8181_gui_write_1(struct ite8181_softc *sc, int byteoffset, int data)
184 {
185
186 bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_gba + byteoffset,
187 data);
188 }
189
190 inline int
191 ite8181_graphics_read_1(struct ite8181_softc *sc, int byteoffset)
192 {
193
194 return (bus_space_read_1(sc->sc_iot, sc->sc_ioh,
195 sc->sc_sba + byteoffset));
196 }
197
198 inline void
199 ite8181_graphics_write_1(struct ite8181_softc *sc, int byteoffset, int data)
200 {
201
202 bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_sba + byteoffset,
203 data);
204 }
205
206 inline int
207 ite8181_ema_read_1(struct ite8181_softc *sc, int byteoffset)
208 {
209
210 ite8181_graphics_write_1(sc, ITE8181_EMA_EXAX, byteoffset);
211 return (ite8181_graphics_read_1(sc, ITE8181_EMA_EXADATA));
212 }
213
214 inline void
215 ite8181_ema_write_1(struct ite8181_softc *sc, int byteoffset, int data)
216 {
217
218 ite8181_graphics_write_1(sc, ITE8181_EMA_EXAX, byteoffset);
219 ite8181_graphics_write_1(sc, ITE8181_EMA_EXADATA, data);
220 }
221
222 int
223 ite8181_probe(bus_space_tag_t iot, bus_space_handle_t ioh)
224 {
225 unsigned long regval;
226
227 #if NBIVIDEO > 0
228 if (bivideo_dont_attach) /* some video driver already attached */
229 return (0);
230 #endif /* NBIVIDEO > 0 */
231
232 regval = ite8181_config_read_4(iot, ioh, ITE8181_ID);
233 VPRINTF(("ite8181_probe: vendor id=%04lx product id=%04lx\n",
234 regval & 0xffff, (regval >> 16) & 0xffff));
235 if (regval != ((ITE8181_PRODUCT_ID << 16) | ITE8181_VENDER_ID))
236 return (0);
237
238 return (1);
239 }
240
241 void
242 ite8181_attach(struct ite8181_softc *sc)
243 {
244 unsigned long regval;
245 struct hpcfb_attach_args ha;
246 int console = (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) ? 0 : 1;
247
248 printf(": ");
249 if (ite8181_fbinit(&sc->sc_fbconf) != 0) {
250 /* just return so that hpcfb will not be attached */
251 return;
252 }
253
254 regval = ite8181_config_read_4(sc->sc_iot, sc->sc_ioh, ITE8181_CLASS);
255 printf("ITE8181 Rev.%02lx", regval & ITE8181_REV_MASK);
256 if (console) {
257 printf(", console");
258 }
259 printf("\n");
260 printf("%s: framebuffer address: 0x%08lx\n",
261 sc->sc_dev.dv_xname, (u_long)bootinfo->fb_addr);
262 if (ite8181_lcd_control_disable)
263 printf("%s: ite8181 lcd control is DISABLED.\n",
264 sc->sc_dev.dv_xname);
265
266 /* set base offsets */
267 sc->sc_mba = ite8181_config_read_4(sc->sc_iot, sc->sc_ioh, ITE8181_MBA);
268 DPRINTFN(1, ("ite8181: Memory base offset %08x\n", sc->sc_mba));
269 sc->sc_gba = ite8181_config_read_4(sc->sc_iot, sc->sc_ioh, ITE8181_GBA);
270 DPRINTFN(1, ("ite8181: GUI base offset %08x\n", sc->sc_gba));
271 sc->sc_sba = ite8181_config_read_4(sc->sc_iot, sc->sc_ioh, ITE8181_SBA);
272 DPRINTFN(1, ("ite8181: Graphics base offset %08x\n", sc->sc_sba));
273
274 /* assume lcd is on */
275 sc->sc_lcd = 1;
276 /* erase wince cursor */
277 ite8181_erase_cursor(sc);
278
279 /* Add a power hook to power saving */
280 sc->sc_powerhook = powerhook_establish(ite8181_power, sc);
281 if (sc->sc_powerhook == NULL)
282 printf("%s: WARNING: unable to establish power hook\n",
283 sc->sc_dev.dv_xname);
284
285 /* Add a hard power hook to power saving */
286 sc->sc_hardpowerhook = config_hook(CONFIG_HOOK_PMEVENT,
287 CONFIG_HOOK_PMEVENT_HARDPOWER,
288 CONFIG_HOOK_SHARE,
289 ite8181_hardpower, sc);
290 if (sc->sc_hardpowerhook == NULL)
291 printf("%s: WARNING: unable to establish hard power hook\n",
292 sc->sc_dev.dv_xname);
293
294 /* initialize backlight brightness and lcd contrast */
295 sc->sc_lcd_inited = 0;
296 ite8181_init_brightness(sc, 1);
297 ite8181_init_contrast(sc, 1);
298 ite8181_init_backlight(sc, 1);
299
300 if (console && hpcfb_cnattach(&sc->sc_fbconf) != 0) {
301 panic("ite8181_attach: can't init fb console");
302 }
303
304 ha.ha_console = console;
305 ha.ha_accessops = &ite8181_ha;
306 ha.ha_accessctx = sc;
307 ha.ha_curfbconf = 0;
308 ha.ha_nfbconf = 1;
309 ha.ha_fbconflist = &sc->sc_fbconf;
310 ha.ha_curdspconf = 0;
311 ha.ha_ndspconf = 1;
312 ha.ha_dspconflist = &sc->sc_dspconf;
313
314 config_found(&sc->sc_dev, &ha, hpcfbprint);
315
316 #if NBIVIDEO > 0
317 /*
318 * bivideo is no longer need
319 */
320 bivideo_dont_attach = 1;
321 #endif /* NBIVIDEO > 0 */
322 }
323
324 int
325 ite8181_lcd_power(struct ite8181_softc *sc, int on)
326 {
327 int lcd_p;
328 int lcd_s;
329 int lcd_seq;
330 int loop = 10;
331
332 if (ite8181_lcd_control_disable) {
333 VPRINTF(("ite8171_lcd_control_disable!: %s\n", on?"on":"off"));
334 return 0;
335 }
336
337 if (sc->sc_lcd != on) {
338 ite8181_ema_write_1(sc, ITE8181_EMA_ENABLEEMA,
339 ITE8181_EMA_ENABLEPASS);
340 lcd_p = ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWER);
341 lcd_s = ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWERSTAT);
342 lcd_seq = ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWERSEQ);
343 DPRINTFN(1,("ite8181_lcd_power(%d)< p=%x, s=%x, seq=%x\n",
344 on,
345 lcd_p, lcd_s, lcd_seq));
346 if (on) {
347 sc->sc_lcd = 1;
348 lcd_seq |= (ITE8181_PUP0|ITE8181_PUP1|ITE8181_PUP2);
349 ite8181_ema_write_1(sc, ITE8181_EMA_LCDPOWERSEQ,
350 lcd_seq);
351 lcd_p &= ~ITE8181_LCDSTANDBY;
352 ite8181_ema_write_1(sc, ITE8181_EMA_LCDPOWER, lcd_p);
353 /*
354 * XXX:
355 * IBM WorkPad z50 power unit has too weak power.
356 * So we must wait too many times to access self device
357 * after LCD panel and BackLight on.
358 * Currently delay is not enough ??? FIXME
359 */
360 delay(ite8181_lcd_on_self_delay*MSEC);
361 while (loop--) {
362 lcd_p = ite8181_ema_read_1(sc,
363 ITE8181_EMA_LCDPOWER);
364 lcd_s = ite8181_ema_read_1(sc,
365 ITE8181_EMA_LCDPOWERSTAT);
366 lcd_seq = ite8181_ema_read_1(sc,
367 ITE8181_EMA_LCDPOWERSEQ);
368 DPRINTFN(1,("ite8181_lcd_power(%d)%d| p=%x,"
369 " s=%x, seq=%x\n", on, loop, lcd_p, lcd_s,
370 lcd_seq));
371 /*
372 * XXX the states which are not described
373 * XXX in manual.
374 */
375 if (!(lcd_s&ITE8181_LCDPSTANDBY) &&
376 !(lcd_s&ITE8181_LCDPUP) &&
377 (lcd_s&ITE8181_LCDPON))
378 break;
379 delay(100);
380 }
381 lcd_s |= ITE8181_PPTOBEON;
382 ite8181_ema_write_1(sc, ITE8181_EMA_LCDPOWERSTAT,
383 lcd_s);
384 } else {
385 sc->sc_lcd = 0;
386 lcd_p |= ITE8181_LCDSTANDBY;
387 ite8181_ema_write_1(sc, ITE8181_EMA_LCDPOWER, lcd_p);
388 while (loop--) {
389 lcd_p = ite8181_ema_read_1(sc,
390 ITE8181_EMA_LCDPOWER);
391 lcd_s = ite8181_ema_read_1(sc,
392 ITE8181_EMA_LCDPOWERSTAT);
393 lcd_seq = ite8181_ema_read_1(sc,
394 ITE8181_EMA_LCDPOWERSEQ);
395 DPRINTFN(1,("ite8181_lcd_power(%d)%d| p=%x,"
396 " s=%x, seq=%x\n", on, loop, lcd_p, lcd_s,
397 lcd_seq));
398 /*
399 * XXX the states which are not described
400 * XXX in manual.
401 */
402 if ((lcd_s&ITE8181_LCDPSTANDBY) &&
403 !(lcd_s&ITE8181_LCDPDOWN) &&
404 !(lcd_s&ITE8181_LCDPON))
405 break;
406 delay(100);
407 }
408 lcd_s &= ~ITE8181_PPTOBEON;
409 ite8181_ema_write_1(sc, ITE8181_EMA_LCDPOWERSTAT,
410 lcd_s);
411 }
412 DPRINTFN(1,("ite8181_lcd_power(%d)> p=%x, s=%x, seq=%x\n",
413 on,
414 ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWER),
415 ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWERSTAT),
416 ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWERSEQ)));
417 ite8181_ema_write_1(sc, ITE8181_EMA_ENABLEEMA,
418 ITE8181_EMA_DISABLEPASS);
419 }
420 return 0;
421 }
422
423 static void
424 ite8181_erase_cursor(struct ite8181_softc *sc)
425 {
426 ite8181_gui_write_1(sc, ITE8181_GUI_C1C, 0); /* Cursor 1 Control Reg. */
427 /* other ? */
428 }
429
430 static void
431 ite8181_update_powerstate(struct ite8181_softc *sc, int updates)
432 {
433 if (updates & PWRSTAT_LCD)
434 config_hook_call(CONFIG_HOOK_POWERCONTROL,
435 CONFIG_HOOK_POWERCONTROL_LCD,
436 (void*)!(sc->sc_powerstate &
437 (PWRSTAT_VIDEOOFF|PWRSTAT_SUSPEND)));
438
439 if (updates & PWRSTAT_BACKLIGHT)
440 config_hook_call(CONFIG_HOOK_POWERCONTROL,
441 CONFIG_HOOK_POWERCONTROL_LCDLIGHT,
442 (void*)(!(sc->sc_powerstate &
443 (PWRSTAT_VIDEOOFF|PWRSTAT_SUSPEND)) &&
444 (sc->sc_powerstate & PWRSTAT_BACKLIGHT)));
445 }
446
447 static void
448 ite8181_power(int why, void *arg)
449 {
450 struct ite8181_softc *sc = arg;
451
452 switch (why) {
453 case PWR_STANDBY:
454 sc->sc_powerstate |= PWRSTAT_SUSPEND;
455 ite8181_update_powerstate(sc, PWRSTAT_ALL);
456 break;
457 case PWR_SUSPEND:
458 sc->sc_powerstate |= PWRSTAT_SUSPEND;
459 ite8181_update_powerstate(sc, PWRSTAT_ALL);
460 break;
461 case PWR_RESUME:
462 sc->sc_powerstate &= ~PWRSTAT_SUSPEND;
463 ite8181_update_powerstate(sc, PWRSTAT_ALL);
464 break;
465 }
466 }
467
468 static int
469 ite8181_hardpower(void *ctx, int type, long id, void *msg)
470 {
471 struct ite8181_softc *sc = ctx;
472 int why = (int)msg;
473
474 switch (why) {
475 case PWR_STANDBY:
476 /* ite8181_lcd_power(sc, 0); */
477 delay(MSEC);
478 break;
479 case PWR_SUSPEND:
480 ite8181_lcd_power(sc, 0);
481 delay(MSEC);
482 break;
483 case PWR_RESUME:
484 delay(MSEC);
485 ite8181_lcd_power(sc, 1);
486 /*
487 * XXX:
488 * IBM WorkPad z50 power unit has too weak power.
489 * So we must wait too many times to access other devices
490 * after LCD panel and BackLight on.
491 */
492 delay(ite8181_lcd_on_delay*MSEC);
493 break;
494 }
495
496 /*
497 * you should wait until the
498 * power state transit sequence will end.
499 */
500
501 return (0);
502 }
503
504 static int
505 ite8181_fbinit(struct hpcfb_fbconf *fb)
506 {
507
508 /*
509 * get fb settings from bootinfo
510 */
511 if (bootinfo == NULL ||
512 bootinfo->fb_addr == 0 ||
513 bootinfo->fb_line_bytes == 0 ||
514 bootinfo->fb_width == 0 ||
515 bootinfo->fb_height == 0) {
516 printf("no frame buffer information.\n");
517 return (-1);
518 }
519
520 /* zero fill */
521 bzero(fb, sizeof(*fb));
522
523 fb->hf_conf_index = 0; /* configuration index */
524 fb->hf_nconfs = 1; /* how many configurations */
525 strcpy(fb->hf_name, "built-in video");
526 /* frame buffer name */
527 strcpy(fb->hf_conf_name, "default");
528 /* configuration name */
529 fb->hf_height = bootinfo->fb_height;
530 fb->hf_width = bootinfo->fb_width;
531 fb->hf_baseaddr = (u_long)bootinfo->fb_addr;
532 fb->hf_offset = (u_long)bootinfo->fb_addr -
533 mips_ptob(mips_btop(bootinfo->fb_addr));
534 /* frame buffer start offset */
535 fb->hf_bytes_per_line = bootinfo->fb_line_bytes;
536 fb->hf_nplanes = 1;
537 fb->hf_bytes_per_plane = bootinfo->fb_height *
538 bootinfo->fb_line_bytes;
539
540 fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
541 fb->hf_access_flags |= HPCFB_ACCESS_WORD;
542 fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
543
544 switch (bootinfo->fb_type) {
545 /*
546 * gray scale
547 */
548 case BIFB_D2_M2L_3:
549 case BIFB_D2_M2L_3x2:
550 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
551 /* fall through */
552 case BIFB_D2_M2L_0:
553 case BIFB_D2_M2L_0x2:
554 fb->hf_class = HPCFB_CLASS_GRAYSCALE;
555 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
556 fb->hf_pack_width = 8;
557 fb->hf_pixels_per_pack = 4;
558 fb->hf_pixel_width = 2;
559 fb->hf_class_data_length = sizeof(struct hf_gray_tag);
560 fb->hf_u.hf_gray.hf_flags = 0; /* reserved for future use */
561 break;
562
563 case BIFB_D4_M2L_F:
564 case BIFB_D4_M2L_Fx2:
565 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
566 /* fall through */
567 case BIFB_D4_M2L_0:
568 case BIFB_D4_M2L_0x2:
569 fb->hf_class = HPCFB_CLASS_GRAYSCALE;
570 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
571 fb->hf_pack_width = 8;
572 fb->hf_pixels_per_pack = 2;
573 fb->hf_pixel_width = 4;
574 fb->hf_class_data_length = sizeof(struct hf_gray_tag);
575 fb->hf_u.hf_gray.hf_flags = 0; /* reserved for future use */
576 break;
577
578 /*
579 * indexed color
580 */
581 case BIFB_D8_FF:
582 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
583 /* fall through */
584 case BIFB_D8_00:
585 fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
586 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
587 fb->hf_pack_width = 8;
588 fb->hf_pixels_per_pack = 1;
589 fb->hf_pixel_width = 8;
590 fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
591 fb->hf_u.hf_indexed.hf_flags = 0; /* reserved for future use */
592 break;
593
594 /*
595 * RGB color
596 */
597 case BIFB_D16_FFFF:
598 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
599 /* fall through */
600 case BIFB_D16_0000:
601 fb->hf_class = HPCFB_CLASS_RGBCOLOR;
602 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
603 fb->hf_order_flags = HPCFB_REVORDER_BYTE;
604 fb->hf_pack_width = 16;
605 fb->hf_pixels_per_pack = 1;
606 fb->hf_pixel_width = 16;
607
608 fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
609 fb->hf_u.hf_rgb.hf_flags = 0; /* reserved for future use */
610
611 fb->hf_u.hf_rgb.hf_red_width = 5;
612 fb->hf_u.hf_rgb.hf_red_shift = 11;
613 fb->hf_u.hf_rgb.hf_green_width = 6;
614 fb->hf_u.hf_rgb.hf_green_shift = 5;
615 fb->hf_u.hf_rgb.hf_blue_width = 5;
616 fb->hf_u.hf_rgb.hf_blue_shift = 0;
617 fb->hf_u.hf_rgb.hf_alpha_width = 0;
618 fb->hf_u.hf_rgb.hf_alpha_shift = 0;
619 break;
620
621 default:
622 printf("unknown type (=%d).\n", bootinfo->fb_type);
623 return (-1);
624 break;
625 }
626
627 return (0); /* no error */
628 }
629
630 int
631 ite8181_ioctl(v, cmd, data, flag, p)
632 void *v;
633 u_long cmd;
634 caddr_t data;
635 int flag;
636 struct proc *p;
637 {
638 struct ite8181_softc *sc = (struct ite8181_softc *)v;
639 struct hpcfb_fbconf *fbconf;
640 struct hpcfb_dspconf *dspconf;
641 struct wsdisplay_cmap *cmap;
642 struct wsdisplay_param *dispparam;
643
644 switch (cmd) {
645 case WSDISPLAYIO_GETCMAP:
646 cmap = (struct wsdisplay_cmap*)data;
647
648 if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
649 sc->sc_fbconf.hf_pack_width != 8 ||
650 256 <= cmap->index ||
651 256 - cmap->index < cmap->count)
652 return (EINVAL);
653
654 if (!uvm_useracc(cmap->red, cmap->count, B_WRITE) ||
655 !uvm_useracc(cmap->green, cmap->count, B_WRITE) ||
656 !uvm_useracc(cmap->blue, cmap->count, B_WRITE))
657 return (EFAULT);
658
659 #ifdef ITE8181_WINCE_CMAP
660 copyout(&bivideo_cmap_r[cmap->index], cmap->red, cmap->count);
661 copyout(&bivideo_cmap_g[cmap->index], cmap->green,cmap->count);
662 copyout(&bivideo_cmap_b[cmap->index], cmap->blue, cmap->count);
663 return (0);
664 #else /* ITE8181_WINCE_CMAP */
665 return EINVAL;
666 #endif /* ITE8181_WINCE_CMAP */
667
668 case WSDISPLAYIO_PUTCMAP:
669 /*
670 * This driver can't set color map.
671 */
672 return (EINVAL);
673
674 case WSDISPLAYIO_SVIDEO:
675 if (*(int *)data == WSDISPLAYIO_VIDEO_OFF)
676 sc->sc_powerstate |= PWRSTAT_VIDEOOFF;
677 else
678 sc->sc_powerstate &= ~PWRSTAT_VIDEOOFF;
679 ite8181_update_powerstate(sc, PWRSTAT_ALL);
680 return 0;
681
682 case WSDISPLAYIO_GVIDEO:
683 *(int *)data = (sc->sc_powerstate&PWRSTAT_VIDEOOFF) ?
684 WSDISPLAYIO_VIDEO_OFF:WSDISPLAYIO_VIDEO_ON;
685 return 0;
686
687
688 case WSDISPLAYIO_GETPARAM:
689 dispparam = (struct wsdisplay_param*)data;
690 switch (dispparam->param) {
691 case WSDISPLAYIO_PARAM_BACKLIGHT:
692 VPRINTF(("ite8181_ioctl: GET:BACKLIGHT\n"));
693 ite8181_init_brightness(sc, 0);
694 ite8181_init_backlight(sc, 0);
695 VPRINTF(("ite8181_ioctl: GET:(real)BACKLIGHT %d\n",
696 (sc->sc_powerstate&PWRSTAT_BACKLIGHT)? 1: 0));
697 dispparam->min = 0;
698 dispparam->max = 1;
699 if (sc->sc_max_brightness > 0)
700 dispparam->curval =
701 sc->sc_brightness > 0 ? 1: 0;
702 else
703 dispparam->curval =
704 (sc->sc_powerstate & PWRSTAT_BACKLIGHT)
705 ? 1: 0;
706 VPRINTF(("ite8181_ioctl: GET:BACKLIGHT:%d(%s)\n",
707 dispparam->curval,
708 sc->sc_max_brightness > 0? "brightness": "light"));
709 return 0;
710 break;
711 case WSDISPLAYIO_PARAM_CONTRAST:
712 VPRINTF(("ite8181_ioctl: GET:CONTRAST\n"));
713 ite8181_init_contrast(sc, 0);
714 if (sc->sc_max_contrast > 0) {
715 dispparam->min = 0;
716 dispparam->max = sc->sc_max_contrast;
717 dispparam->curval = sc->sc_contrast;
718 VPRINTF(("ite8181_ioctl: GET:CONTRAST max=%d,"
719 " current=%d\n", sc->sc_max_contrast,
720 sc->sc_contrast));
721 return 0;
722 } else {
723 VPRINTF(("ite8181_ioctl: "
724 "GET:CONTRAST EINVAL\n"));
725 return (EINVAL);
726 }
727 break;
728 case WSDISPLAYIO_PARAM_BRIGHTNESS:
729 VPRINTF(("ite8181_ioctl: GET:BRIGHTNESS\n"));
730 ite8181_init_brightness(sc, 0);
731 if (sc->sc_max_brightness > 0) {
732 dispparam->min = 0;
733 dispparam->max = sc->sc_max_brightness;
734 dispparam->curval = sc->sc_brightness;
735 VPRINTF(("ite8181_ioctl: GET:BRIGHTNESS"
736 " max=%d, current=%d\n",
737 sc->sc_max_brightness, sc->sc_brightness));
738 return 0;
739 } else {
740 VPRINTF(("ite8181_ioctl: GET:BRIGHTNESS"
741 " EINVAL\n"));
742 return (EINVAL);
743 }
744 return (EINVAL);
745 default:
746 return (EINVAL);
747 }
748 return (0);
749
750 case WSDISPLAYIO_SETPARAM:
751 dispparam = (struct wsdisplay_param*)data;
752 switch (dispparam->param) {
753 case WSDISPLAYIO_PARAM_BACKLIGHT:
754 VPRINTF(("ite8181_ioctl: SET:BACKLIGHT\n"));
755 if (dispparam->curval < 0 ||
756 1 < dispparam->curval)
757 return (EINVAL);
758 ite8181_init_brightness(sc, 0);
759 VPRINTF(("ite8181_ioctl: SET:max brightness=%d\n",
760 sc->sc_max_brightness));
761 if (sc->sc_max_brightness > 0) { /* dimmer */
762 if (dispparam->curval == 0){
763 sc->sc_brightness_save =
764 sc->sc_brightness;
765 ite8181_set_brightness(sc, 0);/* min */
766 } else {
767 if (sc->sc_brightness_save == 0)
768 sc->sc_brightness_save =
769 sc->sc_max_brightness;
770 ite8181_set_brightness(sc,
771 sc->sc_brightness_save);
772 }
773 VPRINTF(("ite8181_ioctl: SET:BACKLIGHT:"
774 "brightness=%d\n", sc->sc_brightness));
775 } else { /* off */
776 if (dispparam->curval == 0)
777 sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
778 else
779 sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
780 VPRINTF(("ite8181_ioctl: SET:BACKLIGHT:"
781 "powerstate %d\n",
782 (sc->sc_powerstate & PWRSTAT_BACKLIGHT)
783 ? 1 : 0));
784 ite8181_update_powerstate(sc,
785 PWRSTAT_BACKLIGHT);
786 VPRINTF(("ite8181_ioctl: SET:BACKLIGHT:%d\n",
787 (sc->sc_powerstate & PWRSTAT_BACKLIGHT)
788 ? 1 : 0));
789 }
790 return 0;
791 break;
792 case WSDISPLAYIO_PARAM_CONTRAST:
793 VPRINTF(("ite8181_ioctl: SET:CONTRAST\n"));
794 ite8181_init_contrast(sc, 0);
795 if (dispparam->curval < 0 ||
796 sc->sc_max_contrast < dispparam->curval)
797 return (EINVAL);
798 if (sc->sc_max_contrast > 0) {
799 int org = sc->sc_contrast;
800 ite8181_set_contrast(sc, dispparam->curval);
801 VPRINTF(("ite8181_ioctl: SET:CONTRAST"
802 " org=%d, current=%d\n", org,
803 sc->sc_contrast));
804 return 0;
805 } else {
806 VPRINTF(("ite8181_ioctl: SET:CONTRAST"
807 " EINVAL\n"));
808 return (EINVAL);
809 }
810 break;
811 case WSDISPLAYIO_PARAM_BRIGHTNESS:
812 VPRINTF(("ite8181_ioctl: SET:BRIGHTNESS\n"));
813 ite8181_init_brightness(sc, 0);
814 if (dispparam->curval < 0 ||
815 sc->sc_max_brightness < dispparam->curval)
816 return (EINVAL);
817 if (sc->sc_max_brightness > 0) {
818 int org = sc->sc_brightness;
819 ite8181_set_brightness(sc, dispparam->curval);
820 VPRINTF(("ite8181_ioctl: SET:BRIGHTNESS"
821 " org=%d, current=%d\n", org,
822 sc->sc_brightness));
823 return 0;
824 } else {
825 VPRINTF(("ite8181_ioctl: SET:BRIGHTNESS"
826 " EINVAL\n"));
827 return (EINVAL);
828 }
829 break;
830 default:
831 return (EINVAL);
832 }
833 return (0);
834
835 case HPCFBIO_GCONF:
836 fbconf = (struct hpcfb_fbconf *)data;
837 if (fbconf->hf_conf_index != 0 &&
838 fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
839 return (EINVAL);
840 }
841 *fbconf = sc->sc_fbconf; /* structure assignment */
842 return (0);
843 case HPCFBIO_SCONF:
844 fbconf = (struct hpcfb_fbconf *)data;
845 if (fbconf->hf_conf_index != 0 &&
846 fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
847 return (EINVAL);
848 }
849 /*
850 * nothing to do because we have only one configration
851 */
852 return (0);
853 case HPCFBIO_GDSPCONF:
854 dspconf = (struct hpcfb_dspconf *)data;
855 if ((dspconf->hd_unit_index != 0 &&
856 dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
857 (dspconf->hd_conf_index != 0 &&
858 dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
859 return (EINVAL);
860 }
861 *dspconf = sc->sc_dspconf; /* structure assignment */
862 return (0);
863 case HPCFBIO_SDSPCONF:
864 dspconf = (struct hpcfb_dspconf *)data;
865 if ((dspconf->hd_unit_index != 0 &&
866 dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
867 (dspconf->hd_conf_index != 0 &&
868 dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
869 return (EINVAL);
870 }
871 /*
872 * nothing to do
873 * because we have only one unit and one configration
874 */
875 return (0);
876 case HPCFBIO_GOP:
877 case HPCFBIO_SOP:
878 /*
879 * curently not implemented...
880 */
881 return (EINVAL);
882 }
883
884 return (EPASSTHROUGH);
885 }
886
887 paddr_t
888 ite8181_mmap(void *ctx, off_t offset, int prot)
889 {
890 struct ite8181_softc *sc = (struct ite8181_softc *)ctx;
891
892 if (offset < 0 ||
893 (sc->sc_fbconf.hf_bytes_per_plane +
894 sc->sc_fbconf.hf_offset) < offset)
895 return -1;
896
897 return mips_btop((u_long)bootinfo->fb_addr + offset);
898 }
899
900
901 void
902 ite8181_init_backlight(struct ite8181_softc *sc, int inattach)
903 {
904 int val = -1;
905
906 if (sc->sc_lcd_inited&BACKLIGHT_INITED)
907 return;
908
909 if (config_hook_call(CONFIG_HOOK_GET,
910 CONFIG_HOOK_POWER_LCDLIGHT, &val) != -1) {
911 /* we can get real light state */
912 VPRINTF(("ite8181_init_backlight: real backlight=%d\n", val));
913 if (val == 0)
914 sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
915 else
916 sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
917 sc->sc_lcd_inited |= BACKLIGHT_INITED;
918 } else if (inattach) {
919 /*
920 we cannot get real light state in attach time
921 because light device not yet attached.
922 we will retry in !inattach.
923 temporary assume light is on.
924 */
925 sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
926 } else {
927 /* we cannot get real light state, so work by myself state */
928 sc->sc_lcd_inited |= BACKLIGHT_INITED;
929 }
930 }
931
932 void
933 ite8181_init_brightness(struct ite8181_softc *sc, int inattach)
934 {
935 int val = -1;
936
937 if (sc->sc_lcd_inited&BRIGHTNESS_INITED)
938 return;
939
940 VPRINTF(("ite8181_init_brightness\n"));
941 if (config_hook_call(CONFIG_HOOK_GET,
942 CONFIG_HOOK_BRIGHTNESS_MAX, &val) != -1) {
943 /* we can get real brightness max */
944 VPRINTF(("ite8181_init_brightness: real brightness max=%d\n",
945 val));
946 sc->sc_max_brightness = val;
947 val = -1;
948 if (config_hook_call(CONFIG_HOOK_GET,
949 CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
950 /* we can get real brightness */
951 VPRINTF(("ite8181_init_brightness:"
952 " real brightness=%d\n", val));
953 sc->sc_brightness_save = sc->sc_brightness = val;
954 } else {
955 sc->sc_brightness_save =
956 sc->sc_brightness = sc->sc_max_brightness;
957 }
958 sc->sc_lcd_inited |= BRIGHTNESS_INITED;
959 } else if (inattach) {
960 /*
961 we cannot get real brightness in attach time
962 because brightness device not yet attached.
963 we will retry in !inattach.
964 */
965 sc->sc_max_brightness = -1;
966 sc->sc_brightness = -1;
967 sc->sc_brightness_save = -1;
968 } else {
969 /* we cannot get real brightness */
970 sc->sc_lcd_inited |= BRIGHTNESS_INITED;
971 }
972
973 return;
974 }
975
976 void
977 ite8181_init_contrast(struct ite8181_softc *sc, int inattach)
978 {
979 int val = -1;
980
981 if (sc->sc_lcd_inited&CONTRAST_INITED)
982 return;
983
984 VPRINTF(("ite8181_init_contrast\n"));
985 if (config_hook_call(CONFIG_HOOK_GET,
986 CONFIG_HOOK_CONTRAST_MAX, &val) != -1) {
987 /* we can get real contrast max */
988 VPRINTF(("ite8181_init_contrast: real contrast max=%d\n", val));
989 sc->sc_max_contrast = val;
990 val = -1;
991 if (config_hook_call(CONFIG_HOOK_GET,
992 CONFIG_HOOK_CONTRAST, &val) != -1) {
993 /* we can get real contrast */
994 VPRINTF(("ite8181_init_contrast: real contrast=%d\n",
995 val));
996 sc->sc_contrast = val;
997 } else {
998 sc->sc_contrast = sc->sc_max_contrast;
999 }
1000 sc->sc_lcd_inited |= CONTRAST_INITED;
1001 } else if (inattach) {
1002 /*
1003 we cannot get real contrast in attach time
1004 because contrast device not yet attached.
1005 we will retry in !inattach.
1006 */
1007 sc->sc_max_contrast = -1;
1008 sc->sc_contrast = -1;
1009 } else {
1010 /* we cannot get real contrast */
1011 sc->sc_lcd_inited |= CONTRAST_INITED;
1012 }
1013
1014 return;
1015 }
1016
1017
1018 void
1019 ite8181_set_brightness(struct ite8181_softc *sc, int val)
1020 {
1021
1022 sc->sc_brightness = val;
1023
1024 config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
1025 if (config_hook_call(CONFIG_HOOK_GET,
1026 CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
1027 sc->sc_brightness = val;
1028 }
1029 }
1030
1031 void
1032 ite8181_set_contrast(struct ite8181_softc *sc, int val)
1033 {
1034
1035 sc->sc_contrast = val;
1036
1037 config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST, &val);
1038 if (config_hook_call(CONFIG_HOOK_GET,
1039 CONFIG_HOOK_CONTRAST, &val) != -1) {
1040 sc->sc_contrast = val;
1041 }
1042 }
1043