2 * SSD0323 OLED controller with OSRAM Pictiva 128x64 display.
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
10 /* The controller can support a variety of different displays, but we only
11 implement one. Most of the commends relating to brightness and geometry
13 #include "qemu/osdep.h"
14 #include "hw/ssi/ssi.h"
15 #include "ui/console.h"
17 //#define DEBUG_SSD0323 1
20 #define DPRINTF(fmt, ...) \
21 do { printf("ssd0323: " fmt , ## __VA_ARGS__); } while (0)
22 #define BADF(fmt, ...) \
24 fprintf(stderr, "ssd0323: error: " fmt , ## __VA_ARGS__); abort(); \
27 #define DPRINTF(fmt, ...) do {} while(0)
28 #define BADF(fmt, ...) \
29 do { fprintf(stderr, "ssd0323: error: " fmt , ## __VA_ARGS__);} while (0)
32 /* Scaling factor for pixels. */
35 #define REMAP_SWAP_COLUMN 0x01
36 #define REMAP_SWAP_NYBBLE 0x02
37 #define REMAP_VERTICAL 0x04
38 #define REMAP_SWAP_COM 0x10
39 #define REMAP_SPLIT_COM 0x40
62 enum ssd0323_mode mode
;
63 uint8_t framebuffer
[128 * 80 / 2];
66 static uint32_t ssd0323_transfer(SSISlave
*dev
, uint32_t data
)
68 ssd0323_state
*s
= FROM_SSI_SLAVE(ssd0323_state
, dev
);
72 DPRINTF("data 0x%02x\n", data
);
73 s
->framebuffer
[s
->col
+ s
->row
* 64] = data
;
74 if (s
->remap
& REMAP_VERTICAL
) {
76 if (s
->row
> s
->row_end
) {
77 s
->row
= s
->row_start
;
80 if (s
->col
> s
->col_end
) {
81 s
->col
= s
->col_start
;
85 if (s
->col
> s
->col_end
) {
87 s
->col
= s
->col_start
;
89 if (s
->row
> s
->row_end
) {
90 s
->row
= s
->row_start
;
96 DPRINTF("cmd 0x%02x\n", data
);
97 if (s
->cmd_len
== 0) {
100 s
->cmd_data
[s
->cmd_len
- 1] = data
;
104 #define DATA(x) if (s->cmd_len <= (x)) return 0
105 case 0x15: /* Set column. */
107 s
->col
= s
->col_start
= s
->cmd_data
[0] % 64;
108 s
->col_end
= s
->cmd_data
[1] % 64;
110 case 0x75: /* Set row. */
112 s
->row
= s
->row_start
= s
->cmd_data
[0] % 80;
113 s
->row_end
= s
->cmd_data
[1] % 80;
115 case 0x81: /* Set contrast */
118 case 0x84: case 0x85: case 0x86: /* Max current. */
121 case 0xa0: /* Set remapping. */
122 /* FIXME: Implement this. */
124 s
->remap
= s
->cmd_data
[0];
126 case 0xa1: /* Set display start line. */
127 case 0xa2: /* Set display offset. */
128 /* FIXME: Implement these. */
131 case 0xa4: /* Normal mode. */
132 case 0xa5: /* All on. */
133 case 0xa6: /* All off. */
134 case 0xa7: /* Inverse. */
135 /* FIXME: Implement these. */
138 case 0xa8: /* Set multiplex ratio. */
139 case 0xad: /* Set DC-DC converter. */
141 /* Ignored. Don't care. */
143 case 0xae: /* Display off. */
144 case 0xaf: /* Display on. */
146 /* TODO: Implement power control. */
148 case 0xb1: /* Set phase length. */
149 case 0xb2: /* Set row period. */
150 case 0xb3: /* Set clock rate. */
151 case 0xbc: /* Set precharge. */
152 case 0xbe: /* Set VCOMH. */
153 case 0xbf: /* Set segment low. */
155 /* Ignored. Don't care. */
157 case 0xb8: /* Set grey scale table. */
158 /* FIXME: Implement this. */
161 case 0xe3: /* NOP. */
164 case 0xff: /* Nasty hack because we don't handle chip selects
168 BADF("Unknown command: 0x%x\n", data
);
176 static void ssd0323_update_display(void *opaque
)
178 ssd0323_state
*s
= (ssd0323_state
*)opaque
;
179 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
187 char colortab
[MAGNIFY
* 64];
194 switch (surface_bits_per_pixel(surface
)) {
210 BADF("Bad color depth\n");
214 for (i
= 0; i
< 16; i
++) {
217 switch (surface_bits_per_pixel(surface
)) {
219 n
= i
* 2 + (i
>> 3);
221 p
[1] = (n
<< 2) | (n
>> 3);
224 n
= i
* 2 + (i
>> 3);
225 p
[0] = n
| (n
<< 6) | ((n
<< 1) & 0x20);
226 p
[1] = (n
<< 3) | (n
>> 2);
231 p
[0] = p
[1] = p
[2] = n
;
234 BADF("Bad color depth\n");
239 /* TODO: Implement row/column remapping. */
240 dest
= surface_data(surface
);
241 for (y
= 0; y
< 64; y
++) {
243 src
= s
->framebuffer
+ 64 * line
;
244 for (x
= 0; x
< 64; x
++) {
247 for (i
= 0; i
< MAGNIFY
; i
++) {
248 memcpy(dest
, colors
[val
], dest_width
);
252 for (i
= 0; i
< MAGNIFY
; i
++) {
253 memcpy(dest
, colors
[val
], dest_width
);
258 for (i
= 1; i
< MAGNIFY
; i
++) {
259 memcpy(dest
, dest
- dest_width
* MAGNIFY
* 128,
260 dest_width
* 128 * MAGNIFY
);
261 dest
+= dest_width
* 128 * MAGNIFY
;
265 dpy_gfx_update(s
->con
, 0, 0, 128 * MAGNIFY
, 64 * MAGNIFY
);
268 static void ssd0323_invalidate_display(void * opaque
)
270 ssd0323_state
*s
= (ssd0323_state
*)opaque
;
274 /* Command/data input. */
275 static void ssd0323_cd(void *opaque
, int n
, int level
)
277 ssd0323_state
*s
= (ssd0323_state
*)opaque
;
278 DPRINTF("%s mode\n", level
? "Data" : "Command");
279 s
->mode
= level
? SSD0323_DATA
: SSD0323_CMD
;
282 static void ssd0323_save(QEMUFile
*f
, void *opaque
)
284 SSISlave
*ss
= SSI_SLAVE(opaque
);
285 ssd0323_state
*s
= (ssd0323_state
*)opaque
;
288 qemu_put_be32(f
, s
->cmd_len
);
289 qemu_put_be32(f
, s
->cmd
);
290 for (i
= 0; i
< 8; i
++)
291 qemu_put_be32(f
, s
->cmd_data
[i
]);
292 qemu_put_be32(f
, s
->row
);
293 qemu_put_be32(f
, s
->row_start
);
294 qemu_put_be32(f
, s
->row_end
);
295 qemu_put_be32(f
, s
->col
);
296 qemu_put_be32(f
, s
->col_start
);
297 qemu_put_be32(f
, s
->col_end
);
298 qemu_put_be32(f
, s
->redraw
);
299 qemu_put_be32(f
, s
->remap
);
300 qemu_put_be32(f
, s
->mode
);
301 qemu_put_buffer(f
, s
->framebuffer
, sizeof(s
->framebuffer
));
303 qemu_put_be32(f
, ss
->cs
);
306 static int ssd0323_load(QEMUFile
*f
, void *opaque
, int version_id
)
308 SSISlave
*ss
= SSI_SLAVE(opaque
);
309 ssd0323_state
*s
= (ssd0323_state
*)opaque
;
315 s
->cmd_len
= qemu_get_be32(f
);
316 if (s
->cmd_len
< 0 || s
->cmd_len
> ARRAY_SIZE(s
->cmd_data
)) {
319 s
->cmd
= qemu_get_be32(f
);
320 for (i
= 0; i
< 8; i
++)
321 s
->cmd_data
[i
] = qemu_get_be32(f
);
322 s
->row
= qemu_get_be32(f
);
323 if (s
->row
< 0 || s
->row
>= 80) {
326 s
->row_start
= qemu_get_be32(f
);
327 if (s
->row_start
< 0 || s
->row_start
>= 80) {
330 s
->row_end
= qemu_get_be32(f
);
331 if (s
->row_end
< 0 || s
->row_end
>= 80) {
334 s
->col
= qemu_get_be32(f
);
335 if (s
->col
< 0 || s
->col
>= 64) {
338 s
->col_start
= qemu_get_be32(f
);
339 if (s
->col_start
< 0 || s
->col_start
>= 64) {
342 s
->col_end
= qemu_get_be32(f
);
343 if (s
->col_end
< 0 || s
->col_end
>= 64) {
346 s
->redraw
= qemu_get_be32(f
);
347 s
->remap
= qemu_get_be32(f
);
348 s
->mode
= qemu_get_be32(f
);
349 if (s
->mode
!= SSD0323_CMD
&& s
->mode
!= SSD0323_DATA
) {
352 qemu_get_buffer(f
, s
->framebuffer
, sizeof(s
->framebuffer
));
354 ss
->cs
= qemu_get_be32(f
);
359 static const GraphicHwOps ssd0323_ops
= {
360 .invalidate
= ssd0323_invalidate_display
,
361 .gfx_update
= ssd0323_update_display
,
364 static int ssd0323_init(SSISlave
*d
)
366 DeviceState
*dev
= DEVICE(d
);
367 ssd0323_state
*s
= FROM_SSI_SLAVE(ssd0323_state
, d
);
371 s
->con
= graphic_console_init(dev
, 0, &ssd0323_ops
, s
);
372 qemu_console_resize(s
->con
, 128 * MAGNIFY
, 64 * MAGNIFY
);
374 qdev_init_gpio_in(dev
, ssd0323_cd
, 1);
376 register_savevm(dev
, "ssd0323_oled", -1, 1,
377 ssd0323_save
, ssd0323_load
, s
);
381 static void ssd0323_class_init(ObjectClass
*klass
, void *data
)
383 SSISlaveClass
*k
= SSI_SLAVE_CLASS(klass
);
385 k
->init
= ssd0323_init
;
386 k
->transfer
= ssd0323_transfer
;
387 k
->cs_polarity
= SSI_CS_HIGH
;
390 static const TypeInfo ssd0323_info
= {
392 .parent
= TYPE_SSI_SLAVE
,
393 .instance_size
= sizeof(ssd0323_state
),
394 .class_init
= ssd0323_class_init
,
397 static void ssd03232_register_types(void)
399 type_register_static(&ssd0323_info
);
402 type_init(ssd03232_register_types
)