2 * SSD0303 OLED controller with OSRAM Pictiva 96x16 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/i2c/i2c.h"
15 #include "ui/console.h"
17 //#define DEBUG_SSD0303 1
20 #define DPRINTF(fmt, ...) \
21 do { printf("ssd0303: " fmt , ## __VA_ARGS__); } while (0)
22 #define BADF(fmt, ...) \
23 do { fprintf(stderr, "ssd0303: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
25 #define DPRINTF(fmt, ...) do {} while(0)
26 #define BADF(fmt, ...) \
27 do { fprintf(stderr, "ssd0303: error: " fmt , ## __VA_ARGS__);} while (0)
30 /* Scaling factor for pixels. */
45 #define TYPE_SSD0303 "ssd0303"
46 #define SSD0303(obj) OBJECT_CHECK(ssd0303_state, (obj), TYPE_SSD0303)
60 enum ssd0303_mode mode;
61 enum ssd0303_cmd cmd_state;
62 uint8_t framebuffer[132*8];
65 static int ssd0303_recv(I2CSlave *i2c)
67 BADF("Reads not implemented\n");
71 static int ssd0303_send(I2CSlave *i2c, uint8_t data)
73 ssd0303_state *s = SSD0303(i2c);
74 enum ssd0303_cmd old_cmd_state;
78 DPRINTF("byte 0x%02x\n", data);
80 s->mode = SSD0303_CMD;
81 else if (data == 0x40)
82 s->mode = SSD0303_DATA;
84 BADF("Unexpected byte 0x%x\n", data);
87 DPRINTF("data 0x%02x\n", data);
89 s->framebuffer[s->col + s->row * 132] = data;
95 old_cmd_state = s->cmd_state;
96 s->cmd_state = SSD0303_CMD_NONE;
97 switch (old_cmd_state) {
98 case SSD0303_CMD_NONE:
99 DPRINTF("cmd 0x%02x\n", data);
100 s->mode = SSD0303_IDLE;
102 case 0x00 ... 0x0f: /* Set lower column address. */
103 s->col = (s->col & 0xf0) | (data & 0xf);
105 case 0x10 ... 0x20: /* Set higher column address. */
106 s->col = (s->col & 0x0f) | ((data & 0xf) << 4);
108 case 0x40 ... 0x7f: /* Set start line. */
111 case 0x81: /* Set contrast (Ignored). */
112 s->cmd_state = SSD0303_CMD_SKIP1;
114 case 0xa0: /* Mirror off. */
117 case 0xa1: /* Mirror off. */
120 case 0xa4: /* Entire display off. */
123 case 0xa5: /* Entire display on. */
126 case 0xa6: /* Inverse off. */
129 case 0xa7: /* Inverse on. */
132 case 0xa8: /* Set multiplied ratio (Ignored). */
133 s->cmd_state = SSD0303_CMD_SKIP1;
135 case 0xad: /* DC-DC power control. */
136 s->cmd_state = SSD0303_CMD_SKIP1;
138 case 0xae: /* Display off. */
141 case 0xaf: /* Display on. */
144 case 0xb0 ... 0xbf: /* Set Page address. */
147 case 0xc0 ... 0xc8: /* Set COM output direction (Ignored). */
149 case 0xd3: /* Set display offset (Ignored). */
150 s->cmd_state = SSD0303_CMD_SKIP1;
152 case 0xd5: /* Set display clock (Ignored). */
153 s->cmd_state = SSD0303_CMD_SKIP1;
155 case 0xd8: /* Set color and power mode (Ignored). */
156 s->cmd_state = SSD0303_CMD_SKIP1;
158 case 0xd9: /* Set pre-charge period (Ignored). */
159 s->cmd_state = SSD0303_CMD_SKIP1;
161 case 0xda: /* Set COM pin configuration (Ignored). */
162 s->cmd_state = SSD0303_CMD_SKIP1;
164 case 0xdb: /* Set VCOM dselect level (Ignored). */
165 s->cmd_state = SSD0303_CMD_SKIP1;
167 case 0xe3: /* no-op. */
170 BADF("Unknown command: 0x%x\n", data);
173 case SSD0303_CMD_SKIP1:
174 DPRINTF("skip 0x%02x\n", data);
182 static int ssd0303_event(I2CSlave *i2c, enum i2c_event event)
184 ssd0303_state *s = SSD0303(i2c);
188 s->mode = SSD0303_IDLE;
200 static void ssd0303_update_display(void *opaque)
202 ssd0303_state *s = (ssd0303_state *)opaque;
203 DisplaySurface *surface = qemu_console_surface(s->con);
210 char colortab[MAGNIFY * 8];
217 switch (surface_bits_per_pixel(surface)) {
233 BADF("Bad color depth\n");
236 dest_width *= MAGNIFY;
237 memset(colortab, 0xff, dest_width);
238 memset(colortab + dest_width, 0, dest_width);
240 colors[0] = colortab;
241 colors[1] = colortab;
242 } else if (s->inverse) {
243 colors[0] = colortab;
244 colors[1] = colortab + dest_width;
246 colors[0] = colortab + dest_width;
247 colors[1] = colortab;
249 dest = surface_data(surface);
250 for (y = 0; y < 16; y++) {
251 line = (y + s->start_line) & 63;
252 src = s->framebuffer + 132 * (line >> 3) + 36;
253 mask = 1 << (line & 7);
254 for (x = 0; x < 96; x++) {
255 memcpy(dest, colors[(*src & mask) != 0], dest_width);
259 for (x = 1; x < MAGNIFY; x++) {
260 memcpy(dest, dest - dest_width * 96, dest_width * 96);
261 dest += dest_width * 96;
265 dpy_gfx_update(s->con, 0, 0, 96 * MAGNIFY, 16 * MAGNIFY);
268 static void ssd0303_invalidate_display(void * opaque)
270 ssd0303_state *s = (ssd0303_state *)opaque;
274 static const VMStateDescription vmstate_ssd0303 = {
275 .name = "ssd0303_oled",
277 .minimum_version_id = 1,
278 .fields = (VMStateField[]) {
279 VMSTATE_INT32(row, ssd0303_state),
280 VMSTATE_INT32(col, ssd0303_state),
281 VMSTATE_INT32(start_line, ssd0303_state),
282 VMSTATE_INT32(mirror, ssd0303_state),
283 VMSTATE_INT32(flash, ssd0303_state),
284 VMSTATE_INT32(enabled, ssd0303_state),
285 VMSTATE_INT32(inverse, ssd0303_state),
286 VMSTATE_INT32(redraw, ssd0303_state),
287 VMSTATE_UINT32(mode, ssd0303_state),
288 VMSTATE_UINT32(cmd_state, ssd0303_state),
289 VMSTATE_BUFFER(framebuffer, ssd0303_state),
290 VMSTATE_I2C_SLAVE(parent_obj, ssd0303_state),
291 VMSTATE_END_OF_LIST()
295 static const GraphicHwOps ssd0303_ops = {
296 .invalidate = ssd0303_invalidate_display,
297 .gfx_update = ssd0303_update_display,
300 static int ssd0303_init(I2CSlave *i2c)
302 ssd0303_state *s = SSD0303(i2c);
304 s->con = graphic_console_init(DEVICE(i2c), 0, &ssd0303_ops, s);
305 qemu_console_resize(s->con, 96 * MAGNIFY, 16 * MAGNIFY);
309 static void ssd0303_class_init(ObjectClass *klass, void *data)
311 DeviceClass *dc = DEVICE_CLASS(klass);
312 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
314 k->init = ssd0303_init;
315 k->event = ssd0303_event;
316 k->recv = ssd0303_recv;
317 k->send = ssd0303_send;
318 dc->vmsd = &vmstate_ssd0303;
321 static const TypeInfo ssd0303_info = {
322 .name = TYPE_SSD0303,
323 .parent = TYPE_I2C_SLAVE,
324 .instance_size = sizeof(ssd0303_state),
325 .class_init = ssd0303_class_init,
328 static void ssd0303_register_types(void)
330 type_register_static(&ssd0303_info);
333 type_init(ssd0303_register_types)