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
14 #include "ui/console.h"
16 //#define DEBUG_SSD0323 1
19 #define DPRINTF(fmt, ...) \
20 do { printf("ssd0323: " fmt , ## __VA_ARGS__); } while (0)
21 #define BADF(fmt, ...) \
23 fprintf(stderr, "ssd0323: error: " fmt , ## __VA_ARGS__); abort(); \
26 #define DPRINTF(fmt, ...) do {} while(0)
27 #define BADF(fmt, ...) \
28 do { fprintf(stderr, "ssd0323: error: " fmt , ## __VA_ARGS__);} while (0)
31 /* Scaling factor for pixels. */
34 #define REMAP_SWAP_COLUMN 0x01
35 #define REMAP_SWAP_NYBBLE 0x02
36 #define REMAP_VERTICAL 0x04
37 #define REMAP_SWAP_COM 0x10
38 #define REMAP_SPLIT_COM 0x40
61 enum ssd0323_mode mode
;
62 uint8_t framebuffer
[128 * 80 / 2];
65 static uint32_t ssd0323_transfer(SSISlave
*dev
, uint32_t data
)
67 ssd0323_state
*s
= FROM_SSI_SLAVE(ssd0323_state
, dev
);
71 DPRINTF("data 0x%02x\n", data
);
72 s
->framebuffer
[s
->col
+ s
->row
* 64] = data
;
73 if (s
->remap
& REMAP_VERTICAL
) {
75 if (s
->row
> s
->row_end
) {
76 s
->row
= s
->row_start
;
79 if (s
->col
> s
->col_end
) {
80 s
->col
= s
->col_start
;
84 if (s
->col
> s
->col_end
) {
86 s
->col
= s
->col_start
;
88 if (s
->row
> s
->row_end
) {
89 s
->row
= s
->row_start
;
95 DPRINTF("cmd 0x%02x\n", data
);
96 if (s
->cmd_len
== 0) {
99 s
->cmd_data
[s
->cmd_len
- 1] = data
;
103 #define DATA(x) if (s->cmd_len <= (x)) return 0
104 case 0x15: /* Set column. */
106 s
->col
= s
->col_start
= s
->cmd_data
[0] % 64;
107 s
->col_end
= s
->cmd_data
[1] % 64;
109 case 0x75: /* Set row. */
111 s
->row
= s
->row_start
= s
->cmd_data
[0] % 80;
112 s
->row_end
= s
->cmd_data
[1] % 80;
114 case 0x81: /* Set contrast */
117 case 0x84: case 0x85: case 0x86: /* Max current. */
120 case 0xa0: /* Set remapping. */
121 /* FIXME: Implement this. */
123 s
->remap
= s
->cmd_data
[0];
125 case 0xa1: /* Set display start line. */
126 case 0xa2: /* Set display offset. */
127 /* FIXME: Implement these. */
130 case 0xa4: /* Normal mode. */
131 case 0xa5: /* All on. */
132 case 0xa6: /* All off. */
133 case 0xa7: /* Inverse. */
134 /* FIXME: Implement these. */
137 case 0xa8: /* Set multiplex ratio. */
138 case 0xad: /* Set DC-DC converter. */
140 /* Ignored. Don't care. */
142 case 0xae: /* Display off. */
143 case 0xaf: /* Display on. */
145 /* TODO: Implement power control. */
147 case 0xb1: /* Set phase length. */
148 case 0xb2: /* Set row period. */
149 case 0xb3: /* Set clock rate. */
150 case 0xbc: /* Set precharge. */
151 case 0xbe: /* Set VCOMH. */
152 case 0xbf: /* Set segment low. */
154 /* Ignored. Don't care. */
156 case 0xb8: /* Set grey scale table. */
157 /* FIXME: Implement this. */
160 case 0xe3: /* NOP. */
163 case 0xff: /* Nasty hack because we don't handle chip selects
167 BADF("Unknown command: 0x%x\n", data
);
175 static void ssd0323_update_display(void *opaque
)
177 ssd0323_state
*s
= (ssd0323_state
*)opaque
;
185 char colortab
[MAGNIFY
* 64];
192 switch (ds_get_bits_per_pixel(s
->ds
)) {
208 BADF("Bad color depth\n");
212 for (i
= 0; i
< 16; i
++) {
215 switch (ds_get_bits_per_pixel(s
->ds
)) {
217 n
= i
* 2 + (i
>> 3);
219 p
[1] = (n
<< 2) | (n
>> 3);
222 n
= i
* 2 + (i
>> 3);
223 p
[0] = n
| (n
<< 6) | ((n
<< 1) & 0x20);
224 p
[1] = (n
<< 3) | (n
>> 2);
229 p
[0] = p
[1] = p
[2] = n
;
232 BADF("Bad color depth\n");
237 /* TODO: Implement row/column remapping. */
238 dest
= ds_get_data(s
->ds
);
239 for (y
= 0; y
< 64; y
++) {
241 src
= s
->framebuffer
+ 64 * line
;
242 for (x
= 0; x
< 64; x
++) {
245 for (i
= 0; i
< MAGNIFY
; i
++) {
246 memcpy(dest
, colors
[val
], dest_width
);
250 for (i
= 0; i
< MAGNIFY
; i
++) {
251 memcpy(dest
, colors
[val
], dest_width
);
256 for (i
= 1; i
< MAGNIFY
; i
++) {
257 memcpy(dest
, dest
- dest_width
* MAGNIFY
* 128,
258 dest_width
* 128 * MAGNIFY
);
259 dest
+= dest_width
* 128 * MAGNIFY
;
263 dpy_gfx_update(s
->ds
, 0, 0, 128 * MAGNIFY
, 64 * MAGNIFY
);
266 static void ssd0323_invalidate_display(void * opaque
)
268 ssd0323_state
*s
= (ssd0323_state
*)opaque
;
272 /* Command/data input. */
273 static void ssd0323_cd(void *opaque
, int n
, int level
)
275 ssd0323_state
*s
= (ssd0323_state
*)opaque
;
276 DPRINTF("%s mode\n", level
? "Data" : "Command");
277 s
->mode
= level
? SSD0323_DATA
: SSD0323_CMD
;
280 static void ssd0323_save(QEMUFile
*f
, void *opaque
)
282 SSISlave
*ss
= SSI_SLAVE(opaque
);
283 ssd0323_state
*s
= (ssd0323_state
*)opaque
;
286 qemu_put_be32(f
, s
->cmd_len
);
287 qemu_put_be32(f
, s
->cmd
);
288 for (i
= 0; i
< 8; i
++)
289 qemu_put_be32(f
, s
->cmd_data
[i
]);
290 qemu_put_be32(f
, s
->row
);
291 qemu_put_be32(f
, s
->row_start
);
292 qemu_put_be32(f
, s
->row_end
);
293 qemu_put_be32(f
, s
->col
);
294 qemu_put_be32(f
, s
->col_start
);
295 qemu_put_be32(f
, s
->col_end
);
296 qemu_put_be32(f
, s
->redraw
);
297 qemu_put_be32(f
, s
->remap
);
298 qemu_put_be32(f
, s
->mode
);
299 qemu_put_buffer(f
, s
->framebuffer
, sizeof(s
->framebuffer
));
301 qemu_put_be32(f
, ss
->cs
);
304 static int ssd0323_load(QEMUFile
*f
, void *opaque
, int version_id
)
306 SSISlave
*ss
= SSI_SLAVE(opaque
);
307 ssd0323_state
*s
= (ssd0323_state
*)opaque
;
313 s
->cmd_len
= qemu_get_be32(f
);
314 s
->cmd
= qemu_get_be32(f
);
315 for (i
= 0; i
< 8; i
++)
316 s
->cmd_data
[i
] = qemu_get_be32(f
);
317 s
->row
= qemu_get_be32(f
);
318 s
->row_start
= qemu_get_be32(f
);
319 s
->row_end
= qemu_get_be32(f
);
320 s
->col
= qemu_get_be32(f
);
321 s
->col_start
= qemu_get_be32(f
);
322 s
->col_end
= qemu_get_be32(f
);
323 s
->redraw
= qemu_get_be32(f
);
324 s
->remap
= qemu_get_be32(f
);
325 s
->mode
= qemu_get_be32(f
);
326 qemu_get_buffer(f
, s
->framebuffer
, sizeof(s
->framebuffer
));
328 ss
->cs
= qemu_get_be32(f
);
333 static int ssd0323_init(SSISlave
*dev
)
335 ssd0323_state
*s
= FROM_SSI_SLAVE(ssd0323_state
, dev
);
339 s
->ds
= graphic_console_init(ssd0323_update_display
,
340 ssd0323_invalidate_display
,
342 qemu_console_resize(s
->ds
, 128 * MAGNIFY
, 64 * MAGNIFY
);
344 qdev_init_gpio_in(&dev
->qdev
, ssd0323_cd
, 1);
346 register_savevm(&dev
->qdev
, "ssd0323_oled", -1, 1,
347 ssd0323_save
, ssd0323_load
, s
);
351 static void ssd0323_class_init(ObjectClass
*klass
, void *data
)
353 SSISlaveClass
*k
= SSI_SLAVE_CLASS(klass
);
355 k
->init
= ssd0323_init
;
356 k
->transfer
= ssd0323_transfer
;
357 k
->cs_polarity
= SSI_CS_HIGH
;
360 static TypeInfo ssd0323_info
= {
362 .parent
= TYPE_SSI_SLAVE
,
363 .instance_size
= sizeof(ssd0323_state
),
364 .class_init
= ssd0323_class_init
,
367 static void ssd03232_register_types(void)
369 type_register_static(&ssd0323_info
);
372 type_init(ssd03232_register_types
)