1 /* 2 * SSD0303 OLED controller with OSRAM Pictiva 96x16 display. 3 * 4 * Copyright (c) 2006-2007 CodeSourcery. 5 * Written by Paul Brook 6 * 7 * This code is licensed under the GPL. 8 */ 9 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 14 #include "qemu/osdep.h" 15 #include "hw/i2c/i2c.h" 16 #include "migration/vmstate.h" 17 #include "qemu/module.h" 18 #include "ui/console.h" 19 #include "qom/object.h" 20 21 //#define DEBUG_SSD0303 1 22 23 #ifdef DEBUG_SSD0303 24 #define DPRINTF(fmt, ...) \ 25 do { printf("ssd0303: " fmt , ## __VA_ARGS__); } while (0) 26 #define BADF(fmt, ...) \ 27 do { fprintf(stderr, "ssd0303: error: " fmt , ## __VA_ARGS__); exit(1);} while (0) 28 #else 29 #define DPRINTF(fmt, ...) do {} while(0) 30 #define BADF(fmt, ...) \ 31 do { fprintf(stderr, "ssd0303: error: " fmt , ## __VA_ARGS__);} while (0) 32 #endif 33 34 /* Scaling factor for pixels. */ 35 #define MAGNIFY 4 36 37 enum ssd0303_mode 38 { 39 SSD0303_IDLE, 40 SSD0303_DATA, 41 SSD0303_CMD 42 }; 43 44 enum ssd0303_cmd { 45 SSD0303_CMD_NONE, 46 SSD0303_CMD_SKIP1 47 }; 48 49 #define TYPE_SSD0303 "ssd0303" 50 typedef struct ssd0303_state ssd0303_state; 51 DECLARE_INSTANCE_CHECKER(ssd0303_state, SSD0303, 52 TYPE_SSD0303) 53 54 struct ssd0303_state { 55 I2CSlave parent_obj; 56 57 QemuConsole *con; 58 int row; 59 int col; 60 int start_line; 61 int mirror; 62 int flash; 63 int enabled; 64 int inverse; 65 int redraw; 66 enum ssd0303_mode mode; 67 enum ssd0303_cmd cmd_state; 68 uint8_t framebuffer[132*8]; 69 }; 70 71 static uint8_t ssd0303_recv(I2CSlave *i2c) 72 { 73 BADF("Reads not implemented\n"); 74 return 0xff; 75 } 76 77 static int ssd0303_send(I2CSlave *i2c, uint8_t data) 78 { 79 ssd0303_state *s = SSD0303(i2c); 80 enum ssd0303_cmd old_cmd_state; 81 82 switch (s->mode) { 83 case SSD0303_IDLE: 84 DPRINTF("byte 0x%02x\n", data); 85 if (data == 0x80) 86 s->mode = SSD0303_CMD; 87 else if (data == 0x40) 88 s->mode = SSD0303_DATA; 89 else 90 BADF("Unexpected byte 0x%x\n", data); 91 break; 92 case SSD0303_DATA: 93 DPRINTF("data 0x%02x\n", data); 94 if (s->col < 132) { 95 s->framebuffer[s->col + s->row * 132] = data; 96 s->col++; 97 s->redraw = 1; 98 } 99 break; 100 case SSD0303_CMD: 101 old_cmd_state = s->cmd_state; 102 s->cmd_state = SSD0303_CMD_NONE; 103 switch (old_cmd_state) { 104 case SSD0303_CMD_NONE: 105 DPRINTF("cmd 0x%02x\n", data); 106 s->mode = SSD0303_IDLE; 107 switch (data) { 108 case 0x00 ... 0x0f: /* Set lower column address. */ 109 s->col = (s->col & 0xf0) | (data & 0xf); 110 break; 111 case 0x10 ... 0x20: /* Set higher column address. */ 112 s->col = (s->col & 0x0f) | ((data & 0xf) << 4); 113 break; 114 case 0x40 ... 0x7f: /* Set start line. */ 115 s->start_line = 0; 116 break; 117 case 0x81: /* Set contrast (Ignored). */ 118 s->cmd_state = SSD0303_CMD_SKIP1; 119 break; 120 case 0xa0: /* Mirror off. */ 121 s->mirror = 0; 122 break; 123 case 0xa1: /* Mirror off. */ 124 s->mirror = 1; 125 break; 126 case 0xa4: /* Entire display off. */ 127 s->flash = 0; 128 break; 129 case 0xa5: /* Entire display on. */ 130 s->flash = 1; 131 break; 132 case 0xa6: /* Inverse off. */ 133 s->inverse = 0; 134 break; 135 case 0xa7: /* Inverse on. */ 136 s->inverse = 1; 137 break; 138 case 0xa8: /* Set multiplied ratio (Ignored). */ 139 s->cmd_state = SSD0303_CMD_SKIP1; 140 break; 141 case 0xad: /* DC-DC power control. */ 142 s->cmd_state = SSD0303_CMD_SKIP1; 143 break; 144 case 0xae: /* Display off. */ 145 s->enabled = 0; 146 break; 147 case 0xaf: /* Display on. */ 148 s->enabled = 1; 149 break; 150 case 0xb0 ... 0xbf: /* Set Page address. */ 151 s->row = data & 7; 152 break; 153 case 0xc0 ... 0xc8: /* Set COM output direction (Ignored). */ 154 break; 155 case 0xd3: /* Set display offset (Ignored). */ 156 s->cmd_state = SSD0303_CMD_SKIP1; 157 break; 158 case 0xd5: /* Set display clock (Ignored). */ 159 s->cmd_state = SSD0303_CMD_SKIP1; 160 break; 161 case 0xd8: /* Set color and power mode (Ignored). */ 162 s->cmd_state = SSD0303_CMD_SKIP1; 163 break; 164 case 0xd9: /* Set pre-charge period (Ignored). */ 165 s->cmd_state = SSD0303_CMD_SKIP1; 166 break; 167 case 0xda: /* Set COM pin configuration (Ignored). */ 168 s->cmd_state = SSD0303_CMD_SKIP1; 169 break; 170 case 0xdb: /* Set VCOM dselect level (Ignored). */ 171 s->cmd_state = SSD0303_CMD_SKIP1; 172 break; 173 case 0xe3: /* no-op. */ 174 break; 175 default: 176 BADF("Unknown command: 0x%x\n", data); 177 } 178 break; 179 case SSD0303_CMD_SKIP1: 180 DPRINTF("skip 0x%02x\n", data); 181 break; 182 } 183 break; 184 } 185 return 0; 186 } 187 188 static int ssd0303_event(I2CSlave *i2c, enum i2c_event event) 189 { 190 ssd0303_state *s = SSD0303(i2c); 191 192 switch (event) { 193 case I2C_FINISH: 194 s->mode = SSD0303_IDLE; 195 break; 196 case I2C_START_RECV: 197 case I2C_START_SEND: 198 case I2C_NACK: 199 /* Nothing to do. */ 200 break; 201 } 202 203 return 0; 204 } 205 206 static void ssd0303_update_display(void *opaque) 207 { 208 ssd0303_state *s = (ssd0303_state *)opaque; 209 DisplaySurface *surface = qemu_console_surface(s->con); 210 uint8_t *dest; 211 uint8_t *src; 212 int x; 213 int y; 214 int line; 215 char *colors[2]; 216 char colortab[MAGNIFY * 8]; 217 int dest_width; 218 uint8_t mask; 219 220 if (!s->redraw) 221 return; 222 223 switch (surface_bits_per_pixel(surface)) { 224 case 0: 225 return; 226 case 15: 227 dest_width = 2; 228 break; 229 case 16: 230 dest_width = 2; 231 break; 232 case 24: 233 dest_width = 3; 234 break; 235 case 32: 236 dest_width = 4; 237 break; 238 default: 239 BADF("Bad color depth\n"); 240 return; 241 } 242 dest_width *= MAGNIFY; 243 memset(colortab, 0xff, dest_width); 244 memset(colortab + dest_width, 0, dest_width); 245 if (s->flash) { 246 colors[0] = colortab; 247 colors[1] = colortab; 248 } else if (s->inverse) { 249 colors[0] = colortab; 250 colors[1] = colortab + dest_width; 251 } else { 252 colors[0] = colortab + dest_width; 253 colors[1] = colortab; 254 } 255 dest = surface_data(surface); 256 for (y = 0; y < 16; y++) { 257 line = (y + s->start_line) & 63; 258 src = s->framebuffer + 132 * (line >> 3) + 36; 259 mask = 1 << (line & 7); 260 for (x = 0; x < 96; x++) { 261 memcpy(dest, colors[(*src & mask) != 0], dest_width); 262 dest += dest_width; 263 src++; 264 } 265 for (x = 1; x < MAGNIFY; x++) { 266 memcpy(dest, dest - dest_width * 96, dest_width * 96); 267 dest += dest_width * 96; 268 } 269 } 270 s->redraw = 0; 271 dpy_gfx_update(s->con, 0, 0, 96 * MAGNIFY, 16 * MAGNIFY); 272 } 273 274 static void ssd0303_invalidate_display(void * opaque) 275 { 276 ssd0303_state *s = (ssd0303_state *)opaque; 277 s->redraw = 1; 278 } 279 280 static const VMStateDescription vmstate_ssd0303 = { 281 .name = "ssd0303_oled", 282 .version_id = 1, 283 .minimum_version_id = 1, 284 .fields = (VMStateField[]) { 285 VMSTATE_INT32(row, ssd0303_state), 286 VMSTATE_INT32(col, ssd0303_state), 287 VMSTATE_INT32(start_line, ssd0303_state), 288 VMSTATE_INT32(mirror, ssd0303_state), 289 VMSTATE_INT32(flash, ssd0303_state), 290 VMSTATE_INT32(enabled, ssd0303_state), 291 VMSTATE_INT32(inverse, ssd0303_state), 292 VMSTATE_INT32(redraw, ssd0303_state), 293 VMSTATE_UINT32(mode, ssd0303_state), 294 VMSTATE_UINT32(cmd_state, ssd0303_state), 295 VMSTATE_BUFFER(framebuffer, ssd0303_state), 296 VMSTATE_I2C_SLAVE(parent_obj, ssd0303_state), 297 VMSTATE_END_OF_LIST() 298 } 299 }; 300 301 static const GraphicHwOps ssd0303_ops = { 302 .invalidate = ssd0303_invalidate_display, 303 .gfx_update = ssd0303_update_display, 304 }; 305 306 static void ssd0303_realize(DeviceState *dev, Error **errp) 307 { 308 ssd0303_state *s = SSD0303(dev); 309 310 s->con = graphic_console_init(dev, 0, &ssd0303_ops, s); 311 qemu_console_resize(s->con, 96 * MAGNIFY, 16 * MAGNIFY); 312 } 313 314 static void ssd0303_class_init(ObjectClass *klass, void *data) 315 { 316 DeviceClass *dc = DEVICE_CLASS(klass); 317 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); 318 319 dc->realize = ssd0303_realize; 320 k->event = ssd0303_event; 321 k->recv = ssd0303_recv; 322 k->send = ssd0303_send; 323 dc->vmsd = &vmstate_ssd0303; 324 } 325 326 static const TypeInfo ssd0303_info = { 327 .name = TYPE_SSD0303, 328 .parent = TYPE_I2C_SLAVE, 329 .instance_size = sizeof(ssd0303_state), 330 .class_init = ssd0303_class_init, 331 }; 332 333 static void ssd0303_register_types(void) 334 { 335 type_register_static(&ssd0303_info); 336 } 337 338 type_init(ssd0303_register_types) 339