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 commands 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
;
206 static void ssd0303_update_display(void *opaque
)
208 ssd0303_state
*s
= (ssd0303_state
*)opaque
;
209 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
216 char colortab
[MAGNIFY
* 8];
223 switch (surface_bits_per_pixel(surface
)) {
239 BADF("Bad color depth\n");
242 dest_width
*= MAGNIFY
;
243 memset(colortab
, 0xff, dest_width
);
244 memset(colortab
+ dest_width
, 0, dest_width
);
246 colors
[0] = colortab
;
247 colors
[1] = colortab
;
248 } else if (s
->inverse
) {
249 colors
[0] = colortab
;
250 colors
[1] = colortab
+ dest_width
;
252 colors
[0] = colortab
+ dest_width
;
253 colors
[1] = colortab
;
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
);
265 for (x
= 1; x
< MAGNIFY
; x
++) {
266 memcpy(dest
, dest
- dest_width
* 96, dest_width
* 96);
267 dest
+= dest_width
* 96;
271 dpy_gfx_update(s
->con
, 0, 0, 96 * MAGNIFY
, 16 * MAGNIFY
);
274 static void ssd0303_invalidate_display(void * opaque
)
276 ssd0303_state
*s
= (ssd0303_state
*)opaque
;
280 static const VMStateDescription vmstate_ssd0303
= {
281 .name
= "ssd0303_oled",
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()
301 static const GraphicHwOps ssd0303_ops
= {
302 .invalidate
= ssd0303_invalidate_display
,
303 .gfx_update
= ssd0303_update_display
,
306 static void ssd0303_realize(DeviceState
*dev
, Error
**errp
)
308 ssd0303_state
*s
= SSD0303(dev
);
310 s
->con
= graphic_console_init(dev
, 0, &ssd0303_ops
, s
);
311 qemu_console_resize(s
->con
, 96 * MAGNIFY
, 16 * MAGNIFY
);
314 static void ssd0303_class_init(ObjectClass
*klass
, void *data
)
316 DeviceClass
*dc
= DEVICE_CLASS(klass
);
317 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
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
;
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
,
333 static void ssd0303_register_types(void)
335 type_register_static(&ssd0303_info
);
338 type_init(ssd0303_register_types
)