ui: move files to ui/ and include/ui/
[qemu/opensuse.git] / hw / ssd0323.c
blobfe6f801ae7cdfb7795453dc19afb825d317606a9
1 /*
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.
8 */
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
12 setup are ignored. */
13 #include "ssi.h"
14 #include "ui/console.h"
16 //#define DEBUG_SSD0323 1
18 #ifdef DEBUG_SSD0323
19 #define DPRINTF(fmt, ...) \
20 do { printf("ssd0323: " fmt , ## __VA_ARGS__); } while (0)
21 #define BADF(fmt, ...) \
22 do { \
23 fprintf(stderr, "ssd0323: error: " fmt , ## __VA_ARGS__); abort(); \
24 } while (0)
25 #else
26 #define DPRINTF(fmt, ...) do {} while(0)
27 #define BADF(fmt, ...) \
28 do { fprintf(stderr, "ssd0323: error: " fmt , ## __VA_ARGS__);} while (0)
29 #endif
31 /* Scaling factor for pixels. */
32 #define MAGNIFY 4
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
40 enum ssd0323_mode
42 SSD0323_CMD,
43 SSD0323_DATA
46 typedef struct {
47 SSISlave ssidev;
48 DisplayState *ds;
50 int cmd_len;
51 int cmd;
52 int cmd_data[8];
53 int row;
54 int row_start;
55 int row_end;
56 int col;
57 int col_start;
58 int col_end;
59 int redraw;
60 int remap;
61 enum ssd0323_mode mode;
62 uint8_t framebuffer[128 * 80 / 2];
63 } ssd0323_state;
65 static uint32_t ssd0323_transfer(SSISlave *dev, uint32_t data)
67 ssd0323_state *s = FROM_SSI_SLAVE(ssd0323_state, dev);
69 switch (s->mode) {
70 case SSD0323_DATA:
71 DPRINTF("data 0x%02x\n", data);
72 s->framebuffer[s->col + s->row * 64] = data;
73 if (s->remap & REMAP_VERTICAL) {
74 s->row++;
75 if (s->row > s->row_end) {
76 s->row = s->row_start;
77 s->col++;
79 if (s->col > s->col_end) {
80 s->col = s->col_start;
82 } else {
83 s->col++;
84 if (s->col > s->col_end) {
85 s->row++;
86 s->col = s->col_start;
88 if (s->row > s->row_end) {
89 s->row = s->row_start;
92 s->redraw = 1;
93 break;
94 case SSD0323_CMD:
95 DPRINTF("cmd 0x%02x\n", data);
96 if (s->cmd_len == 0) {
97 s->cmd = data;
98 } else {
99 s->cmd_data[s->cmd_len - 1] = data;
101 s->cmd_len++;
102 switch (s->cmd) {
103 #define DATA(x) if (s->cmd_len <= (x)) return 0
104 case 0x15: /* Set column. */
105 DATA(2);
106 s->col = s->col_start = s->cmd_data[0] % 64;
107 s->col_end = s->cmd_data[1] % 64;
108 break;
109 case 0x75: /* Set row. */
110 DATA(2);
111 s->row = s->row_start = s->cmd_data[0] % 80;
112 s->row_end = s->cmd_data[1] % 80;
113 break;
114 case 0x81: /* Set contrast */
115 DATA(1);
116 break;
117 case 0x84: case 0x85: case 0x86: /* Max current. */
118 DATA(0);
119 break;
120 case 0xa0: /* Set remapping. */
121 /* FIXME: Implement this. */
122 DATA(1);
123 s->remap = s->cmd_data[0];
124 break;
125 case 0xa1: /* Set display start line. */
126 case 0xa2: /* Set display offset. */
127 /* FIXME: Implement these. */
128 DATA(1);
129 break;
130 case 0xa4: /* Normal mode. */
131 case 0xa5: /* All on. */
132 case 0xa6: /* All off. */
133 case 0xa7: /* Inverse. */
134 /* FIXME: Implement these. */
135 DATA(0);
136 break;
137 case 0xa8: /* Set multiplex ratio. */
138 case 0xad: /* Set DC-DC converter. */
139 DATA(1);
140 /* Ignored. Don't care. */
141 break;
142 case 0xae: /* Display off. */
143 case 0xaf: /* Display on. */
144 DATA(0);
145 /* TODO: Implement power control. */
146 break;
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. */
153 DATA(1);
154 /* Ignored. Don't care. */
155 break;
156 case 0xb8: /* Set grey scale table. */
157 /* FIXME: Implement this. */
158 DATA(8);
159 break;
160 case 0xe3: /* NOP. */
161 DATA(0);
162 break;
163 case 0xff: /* Nasty hack because we don't handle chip selects
164 properly. */
165 break;
166 default:
167 BADF("Unknown command: 0x%x\n", data);
169 s->cmd_len = 0;
170 return 0;
172 return 0;
175 static void ssd0323_update_display(void *opaque)
177 ssd0323_state *s = (ssd0323_state *)opaque;
178 uint8_t *dest;
179 uint8_t *src;
180 int x;
181 int y;
182 int i;
183 int line;
184 char *colors[16];
185 char colortab[MAGNIFY * 64];
186 char *p;
187 int dest_width;
189 if (!s->redraw)
190 return;
192 switch (ds_get_bits_per_pixel(s->ds)) {
193 case 0:
194 return;
195 case 15:
196 dest_width = 2;
197 break;
198 case 16:
199 dest_width = 2;
200 break;
201 case 24:
202 dest_width = 3;
203 break;
204 case 32:
205 dest_width = 4;
206 break;
207 default:
208 BADF("Bad color depth\n");
209 return;
211 p = colortab;
212 for (i = 0; i < 16; i++) {
213 int n;
214 colors[i] = p;
215 switch (ds_get_bits_per_pixel(s->ds)) {
216 case 15:
217 n = i * 2 + (i >> 3);
218 p[0] = n | (n << 5);
219 p[1] = (n << 2) | (n >> 3);
220 break;
221 case 16:
222 n = i * 2 + (i >> 3);
223 p[0] = n | (n << 6) | ((n << 1) & 0x20);
224 p[1] = (n << 3) | (n >> 2);
225 break;
226 case 24:
227 case 32:
228 n = (i << 4) | i;
229 p[0] = p[1] = p[2] = n;
230 break;
231 default:
232 BADF("Bad color depth\n");
233 return;
235 p += dest_width;
237 /* TODO: Implement row/column remapping. */
238 dest = ds_get_data(s->ds);
239 for (y = 0; y < 64; y++) {
240 line = y;
241 src = s->framebuffer + 64 * line;
242 for (x = 0; x < 64; x++) {
243 int val;
244 val = *src >> 4;
245 for (i = 0; i < MAGNIFY; i++) {
246 memcpy(dest, colors[val], dest_width);
247 dest += dest_width;
249 val = *src & 0xf;
250 for (i = 0; i < MAGNIFY; i++) {
251 memcpy(dest, colors[val], dest_width);
252 dest += dest_width;
254 src++;
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;
262 s->redraw = 0;
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;
269 s->redraw = 1;
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;
284 int i;
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;
308 int i;
310 if (version_id != 1)
311 return -EINVAL;
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);
330 return 0;
333 static int ssd0323_init(SSISlave *dev)
335 ssd0323_state *s = FROM_SSI_SLAVE(ssd0323_state, dev);
337 s->col_end = 63;
338 s->row_end = 79;
339 s->ds = graphic_console_init(ssd0323_update_display,
340 ssd0323_invalidate_display,
341 NULL, NULL, s);
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);
348 return 0;
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 = {
361 .name = "ssd0323",
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)