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 "hw/i2c/i2c.h"
14 #include "ui/console.h"
16 //#define DEBUG_SSD0303 1
19 #define DPRINTF(fmt, ...) \
20 do { printf("ssd0303: " fmt , ## __VA_ARGS__); } while (0)
21 #define BADF(fmt, ...) \
22 do { fprintf(stderr, "ssd0303: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
24 #define DPRINTF(fmt, ...) do {} while(0)
25 #define BADF(fmt, ...) \
26 do { fprintf(stderr, "ssd0303: error: " fmt , ## __VA_ARGS__);} while (0)
29 /* Scaling factor for pixels. */
44 #define TYPE_SSD0303 "ssd0303"
45 #define SSD0303(obj) OBJECT_CHECK(ssd0303_state, (obj), TYPE_SSD0303)
59 enum ssd0303_mode mode;
60 enum ssd0303_cmd cmd_state;
61 uint8_t framebuffer[132*8];
64 static int ssd0303_recv(I2CSlave *i2c)
66 BADF("Reads not implemented\n");
70 static int ssd0303_send(I2CSlave *i2c, uint8_t data)
72 ssd0303_state *s = SSD0303(i2c);
73 enum ssd0303_cmd old_cmd_state;
77 DPRINTF("byte 0x%02x\n", data);
79 s->mode = SSD0303_CMD;
80 else if (data == 0x40)
81 s->mode = SSD0303_DATA;
83 BADF("Unexpected byte 0x%x\n", data);
86 DPRINTF("data 0x%02x\n", data);
88 s->framebuffer[s->col + s->row * 132] = data;
94 old_cmd_state = s->cmd_state;
95 s->cmd_state = SSD0303_CMD_NONE;
96 switch (old_cmd_state) {
97 case SSD0303_CMD_NONE:
98 DPRINTF("cmd 0x%02x\n", data);
99 s->mode = SSD0303_IDLE;
101 case 0x00 ... 0x0f: /* Set lower column address. */
102 s->col = (s->col & 0xf0) | (data & 0xf);
104 case 0x10 ... 0x20: /* Set higher column address. */
105 s->col = (s->col & 0x0f) | ((data & 0xf) << 4);
107 case 0x40 ... 0x7f: /* Set start line. */
110 case 0x81: /* Set contrast (Ignored). */
111 s->cmd_state = SSD0303_CMD_SKIP1;
113 case 0xa0: /* Mirror off. */
116 case 0xa1: /* Mirror off. */
119 case 0xa4: /* Entire display off. */
122 case 0xa5: /* Entire display on. */
125 case 0xa6: /* Inverse off. */
128 case 0xa7: /* Inverse on. */
131 case 0xa8: /* Set multiplied ratio (Ignored). */
132 s->cmd_state = SSD0303_CMD_SKIP1;
134 case 0xad: /* DC-DC power control. */
135 s->cmd_state = SSD0303_CMD_SKIP1;
137 case 0xae: /* Display off. */
140 case 0xaf: /* Display on. */
143 case 0xb0 ... 0xbf: /* Set Page address. */
146 case 0xc0 ... 0xc8: /* Set COM output direction (Ignored). */
148 case 0xd3: /* Set display offset (Ignored). */
149 s->cmd_state = SSD0303_CMD_SKIP1;
151 case 0xd5: /* Set display clock (Ignored). */
152 s->cmd_state = SSD0303_CMD_SKIP1;
154 case 0xd8: /* Set color and power mode (Ignored). */
155 s->cmd_state = SSD0303_CMD_SKIP1;
157 case 0xd9: /* Set pre-charge period (Ignored). */
158 s->cmd_state = SSD0303_CMD_SKIP1;
160 case 0xda: /* Set COM pin configuration (Ignored). */
161 s->cmd_state = SSD0303_CMD_SKIP1;
163 case 0xdb: /* Set VCOM dselect level (Ignored). */
164 s->cmd_state = SSD0303_CMD_SKIP1;
166 case 0xe3: /* no-op. */
169 BADF("Unknown command: 0x%x\n", data);
172 case SSD0303_CMD_SKIP1:
173 DPRINTF("skip 0x%02x\n", data);
181 static void ssd0303_event(I2CSlave *i2c, enum i2c_event event)
183 ssd0303_state *s = SSD0303(i2c);
187 s->mode = SSD0303_IDLE;
197 static void ssd0303_update_display(void *opaque)
199 ssd0303_state *s = (ssd0303_state *)opaque;
200 DisplaySurface *surface = qemu_console_surface(s->con);
207 char colortab[MAGNIFY * 8];
214 switch (surface_bits_per_pixel(surface)) {
230 BADF("Bad color depth\n");
233 dest_width *= MAGNIFY;
234 memset(colortab, 0xff, dest_width);
235 memset(colortab + dest_width, 0, dest_width);
237 colors[0] = colortab;
238 colors[1] = colortab;
239 } else if (s->inverse) {
240 colors[0] = colortab;
241 colors[1] = colortab + dest_width;
243 colors[0] = colortab + dest_width;
244 colors[1] = colortab;
246 dest = surface_data(surface);
247 for (y = 0; y < 16; y++) {
248 line = (y + s->start_line) & 63;
249 src = s->framebuffer + 132 * (line >> 3) + 36;
250 mask = 1 << (line & 7);
251 for (x = 0; x < 96; x++) {
252 memcpy(dest, colors[(*src & mask) != 0], dest_width);
256 for (x = 1; x < MAGNIFY; x++) {
257 memcpy(dest, dest - dest_width * 96, dest_width * 96);
258 dest += dest_width * 96;
262 dpy_gfx_update(s->con, 0, 0, 96 * MAGNIFY, 16 * MAGNIFY);
265 static void ssd0303_invalidate_display(void * opaque)
267 ssd0303_state *s = (ssd0303_state *)opaque;
271 static const VMStateDescription vmstate_ssd0303 = {
272 .name = "ssd0303_oled",
274 .minimum_version_id = 1,
275 .fields = (VMStateField[]) {
276 VMSTATE_INT32(row, ssd0303_state),
277 VMSTATE_INT32(col, ssd0303_state),
278 VMSTATE_INT32(start_line, ssd0303_state),
279 VMSTATE_INT32(mirror, ssd0303_state),
280 VMSTATE_INT32(flash, ssd0303_state),
281 VMSTATE_INT32(enabled, ssd0303_state),
282 VMSTATE_INT32(inverse, ssd0303_state),
283 VMSTATE_INT32(redraw, ssd0303_state),
284 VMSTATE_UINT32(mode, ssd0303_state),
285 VMSTATE_UINT32(cmd_state, ssd0303_state),
286 VMSTATE_BUFFER(framebuffer, ssd0303_state),
287 VMSTATE_I2C_SLAVE(parent_obj, ssd0303_state),
288 VMSTATE_END_OF_LIST()
292 static const GraphicHwOps ssd0303_ops = {
293 .invalidate = ssd0303_invalidate_display,
294 .gfx_update = ssd0303_update_display,
297 static int ssd0303_init(I2CSlave *i2c)
299 ssd0303_state *s = SSD0303(i2c);
301 s->con = graphic_console_init(DEVICE(i2c), 0, &ssd0303_ops, s);
302 qemu_console_resize(s->con, 96 * MAGNIFY, 16 * MAGNIFY);
306 static void ssd0303_class_init(ObjectClass *klass, void *data)
308 DeviceClass *dc = DEVICE_CLASS(klass);
309 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
311 k->init = ssd0303_init;
312 k->event = ssd0303_event;
313 k->recv = ssd0303_recv;
314 k->send = ssd0303_send;
315 dc->vmsd = &vmstate_ssd0303;
318 static const TypeInfo ssd0303_info = {
319 .name = TYPE_SSD0303,
320 .parent = TYPE_I2C_SLAVE,
321 .instance_size = sizeof(ssd0303_state),
322 .class_init = ssd0303_class_init,
325 static void ssd0303_register_types(void)
327 type_register_static(&ssd0303_info);
330 type_init(ssd0303_register_types)