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
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"
21 //#define DEBUG_SSD0303 1
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)
29 #define DPRINTF(fmt, ...) do {} while(0)
30 #define BADF(fmt, ...) \
31 do { fprintf(stderr, "ssd0303: error: " fmt , ## __VA_ARGS__);} while (0)
34 /* Scaling factor for pixels. */
49 #define TYPE_SSD0303 "ssd0303"
50 OBJECT_DECLARE_SIMPLE_TYPE(ssd0303_state
, SSD0303
)
52 struct ssd0303_state
{
64 enum ssd0303_mode mode
;
65 enum ssd0303_cmd cmd_state
;
66 uint8_t framebuffer
[132*8];
69 static uint8_t ssd0303_recv(I2CSlave
*i2c
)
71 BADF("Reads not implemented\n");
75 static int ssd0303_send(I2CSlave
*i2c
, uint8_t data
)
77 ssd0303_state
*s
= SSD0303(i2c
);
78 enum ssd0303_cmd old_cmd_state
;
82 DPRINTF("byte 0x%02x\n", data
);
84 s
->mode
= SSD0303_CMD
;
85 else if (data
== 0x40)
86 s
->mode
= SSD0303_DATA
;
88 BADF("Unexpected byte 0x%x\n", data
);
91 DPRINTF("data 0x%02x\n", data
);
93 s
->framebuffer
[s
->col
+ s
->row
* 132] = data
;
99 old_cmd_state
= s
->cmd_state
;
100 s
->cmd_state
= SSD0303_CMD_NONE
;
101 switch (old_cmd_state
) {
102 case SSD0303_CMD_NONE
:
103 DPRINTF("cmd 0x%02x\n", data
);
104 s
->mode
= SSD0303_IDLE
;
106 case 0x00 ... 0x0f: /* Set lower column address. */
107 s
->col
= (s
->col
& 0xf0) | (data
& 0xf);
109 case 0x10 ... 0x20: /* Set higher column address. */
110 s
->col
= (s
->col
& 0x0f) | ((data
& 0xf) << 4);
112 case 0x40 ... 0x7f: /* Set start line. */
115 case 0x81: /* Set contrast (Ignored). */
116 s
->cmd_state
= SSD0303_CMD_SKIP1
;
118 case 0xa0: /* Mirror off. */
121 case 0xa1: /* Mirror off. */
124 case 0xa4: /* Entire display off. */
127 case 0xa5: /* Entire display on. */
130 case 0xa6: /* Inverse off. */
133 case 0xa7: /* Inverse on. */
136 case 0xa8: /* Set multiplied ratio (Ignored). */
137 s
->cmd_state
= SSD0303_CMD_SKIP1
;
139 case 0xad: /* DC-DC power control. */
140 s
->cmd_state
= SSD0303_CMD_SKIP1
;
142 case 0xae: /* Display off. */
145 case 0xaf: /* Display on. */
148 case 0xb0 ... 0xbf: /* Set Page address. */
151 case 0xc0 ... 0xc8: /* Set COM output direction (Ignored). */
153 case 0xd3: /* Set display offset (Ignored). */
154 s
->cmd_state
= SSD0303_CMD_SKIP1
;
156 case 0xd5: /* Set display clock (Ignored). */
157 s
->cmd_state
= SSD0303_CMD_SKIP1
;
159 case 0xd8: /* Set color and power mode (Ignored). */
160 s
->cmd_state
= SSD0303_CMD_SKIP1
;
162 case 0xd9: /* Set pre-charge period (Ignored). */
163 s
->cmd_state
= SSD0303_CMD_SKIP1
;
165 case 0xda: /* Set COM pin configuration (Ignored). */
166 s
->cmd_state
= SSD0303_CMD_SKIP1
;
168 case 0xdb: /* Set VCOM dselect level (Ignored). */
169 s
->cmd_state
= SSD0303_CMD_SKIP1
;
171 case 0xe3: /* no-op. */
174 BADF("Unknown command: 0x%x\n", data
);
177 case SSD0303_CMD_SKIP1
:
178 DPRINTF("skip 0x%02x\n", data
);
186 static int ssd0303_event(I2CSlave
*i2c
, enum i2c_event event
)
188 ssd0303_state
*s
= SSD0303(i2c
);
192 s
->mode
= SSD0303_IDLE
;
204 static void ssd0303_update_display(void *opaque
)
206 ssd0303_state
*s
= (ssd0303_state
*)opaque
;
207 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
214 char colortab
[MAGNIFY
* 8];
221 switch (surface_bits_per_pixel(surface
)) {
237 BADF("Bad color depth\n");
240 dest_width
*= MAGNIFY
;
241 memset(colortab
, 0xff, dest_width
);
242 memset(colortab
+ dest_width
, 0, dest_width
);
244 colors
[0] = colortab
;
245 colors
[1] = colortab
;
246 } else if (s
->inverse
) {
247 colors
[0] = colortab
;
248 colors
[1] = colortab
+ dest_width
;
250 colors
[0] = colortab
+ dest_width
;
251 colors
[1] = colortab
;
253 dest
= surface_data(surface
);
254 for (y
= 0; y
< 16; y
++) {
255 line
= (y
+ s
->start_line
) & 63;
256 src
= s
->framebuffer
+ 132 * (line
>> 3) + 36;
257 mask
= 1 << (line
& 7);
258 for (x
= 0; x
< 96; x
++) {
259 memcpy(dest
, colors
[(*src
& mask
) != 0], dest_width
);
263 for (x
= 1; x
< MAGNIFY
; x
++) {
264 memcpy(dest
, dest
- dest_width
* 96, dest_width
* 96);
265 dest
+= dest_width
* 96;
269 dpy_gfx_update(s
->con
, 0, 0, 96 * MAGNIFY
, 16 * MAGNIFY
);
272 static void ssd0303_invalidate_display(void * opaque
)
274 ssd0303_state
*s
= (ssd0303_state
*)opaque
;
278 static const VMStateDescription vmstate_ssd0303
= {
279 .name
= "ssd0303_oled",
281 .minimum_version_id
= 1,
282 .fields
= (VMStateField
[]) {
283 VMSTATE_INT32(row
, ssd0303_state
),
284 VMSTATE_INT32(col
, ssd0303_state
),
285 VMSTATE_INT32(start_line
, ssd0303_state
),
286 VMSTATE_INT32(mirror
, ssd0303_state
),
287 VMSTATE_INT32(flash
, ssd0303_state
),
288 VMSTATE_INT32(enabled
, ssd0303_state
),
289 VMSTATE_INT32(inverse
, ssd0303_state
),
290 VMSTATE_INT32(redraw
, ssd0303_state
),
291 VMSTATE_UINT32(mode
, ssd0303_state
),
292 VMSTATE_UINT32(cmd_state
, ssd0303_state
),
293 VMSTATE_BUFFER(framebuffer
, ssd0303_state
),
294 VMSTATE_I2C_SLAVE(parent_obj
, ssd0303_state
),
295 VMSTATE_END_OF_LIST()
299 static const GraphicHwOps ssd0303_ops
= {
300 .invalidate
= ssd0303_invalidate_display
,
301 .gfx_update
= ssd0303_update_display
,
304 static void ssd0303_realize(DeviceState
*dev
, Error
**errp
)
306 ssd0303_state
*s
= SSD0303(dev
);
308 s
->con
= graphic_console_init(dev
, 0, &ssd0303_ops
, s
);
309 qemu_console_resize(s
->con
, 96 * MAGNIFY
, 16 * MAGNIFY
);
312 static void ssd0303_class_init(ObjectClass
*klass
, void *data
)
314 DeviceClass
*dc
= DEVICE_CLASS(klass
);
315 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
317 dc
->realize
= ssd0303_realize
;
318 k
->event
= ssd0303_event
;
319 k
->recv
= ssd0303_recv
;
320 k
->send
= ssd0303_send
;
321 dc
->vmsd
= &vmstate_ssd0303
;
324 static const TypeInfo ssd0303_info
= {
325 .name
= TYPE_SSD0303
,
326 .parent
= TYPE_I2C_SLAVE
,
327 .instance_size
= sizeof(ssd0303_state
),
328 .class_init
= ssd0303_class_init
,
331 static void ssd0303_register_types(void)
333 type_register_static(&ssd0303_info
);
336 type_init(ssd0303_register_types
)