2 * SSD0303 OLED controller with OSRAM Pictiva 96x16 display.
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced 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
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. */
55 enum ssd0303_mode mode
;
56 enum ssd0303_cmd cmd_state
;
57 uint8_t framebuffer
[132*8];
60 static int ssd0303_recv(i2c_slave
*i2c
)
62 BADF("Reads not implemented\n");
66 static int ssd0303_send(i2c_slave
*i2c
, uint8_t data
)
68 ssd0303_state
*s
= (ssd0303_state
*)i2c
;
69 enum ssd0303_cmd old_cmd_state
;
72 DPRINTF("byte 0x%02x\n", data
);
74 s
->mode
= SSD0303_CMD
;
75 else if (data
== 0x40)
76 s
->mode
= SSD0303_DATA
;
78 BADF("Unexpected byte 0x%x\n", data
);
81 DPRINTF("data 0x%02x\n", data
);
83 s
->framebuffer
[s
->col
+ s
->row
* 132] = data
;
89 old_cmd_state
= s
->cmd_state
;
90 s
->cmd_state
= SSD0303_CMD_NONE
;
91 switch (old_cmd_state
) {
92 case SSD0303_CMD_NONE
:
93 DPRINTF("cmd 0x%02x\n", data
);
94 s
->mode
= SSD0303_IDLE
;
96 case 0x00 ... 0x0f: /* Set lower colum address. */
97 s
->col
= (s
->col
& 0xf0) | (data
& 0xf);
99 case 0x10 ... 0x20: /* Set higher column address. */
100 s
->col
= (s
->col
& 0x0f) | ((data
& 0xf) << 4);
102 case 0x40 ... 0x7f: /* Set start line. */
105 case 0x81: /* Set contrast (Ignored). */
106 s
->cmd_state
= SSD0303_CMD_SKIP1
;
108 case 0xa0: /* Mirror off. */
111 case 0xa1: /* Mirror off. */
114 case 0xa4: /* Entire display off. */
117 case 0xa5: /* Entire display on. */
120 case 0xa6: /* Inverse off. */
123 case 0xa7: /* Inverse on. */
126 case 0xa8: /* Set multipled ratio (Ignored). */
127 s
->cmd_state
= SSD0303_CMD_SKIP1
;
129 case 0xad: /* DC-DC power control. */
130 s
->cmd_state
= SSD0303_CMD_SKIP1
;
132 case 0xae: /* Display off. */
135 case 0xaf: /* Display on. */
138 case 0xb0 ... 0xbf: /* Set Page address. */
141 case 0xc0 ... 0xc8: /* Set COM output direction (Ignored). */
143 case 0xd3: /* Set display offset (Ignored). */
144 s
->cmd_state
= SSD0303_CMD_SKIP1
;
146 case 0xd5: /* Set display clock (Ignored). */
147 s
->cmd_state
= SSD0303_CMD_SKIP1
;
149 case 0xd8: /* Set color and power mode (Ignored). */
150 s
->cmd_state
= SSD0303_CMD_SKIP1
;
152 case 0xd9: /* Set pre-charge period (Ignored). */
153 s
->cmd_state
= SSD0303_CMD_SKIP1
;
155 case 0xda: /* Set COM pin configuration (Ignored). */
156 s
->cmd_state
= SSD0303_CMD_SKIP1
;
158 case 0xdb: /* Set VCOM dselect level (Ignored). */
159 s
->cmd_state
= SSD0303_CMD_SKIP1
;
161 case 0xe3: /* no-op. */
164 BADF("Unknown command: 0x%x\n", data
);
167 case SSD0303_CMD_SKIP1
:
168 DPRINTF("skip 0x%02x\n", data
);
176 static void ssd0303_event(i2c_slave
*i2c
, enum i2c_event event
)
178 ssd0303_state
*s
= (ssd0303_state
*)i2c
;
181 s
->mode
= SSD0303_IDLE
;
191 static void ssd0303_update_display(void *opaque
)
193 ssd0303_state
*s
= (ssd0303_state
*)opaque
;
200 char colortab
[MAGNIFY
* 8];
207 switch (ds_get_bits_per_pixel(s
->ds
)) {
223 BADF("Bad color depth\n");
226 dest_width
*= MAGNIFY
;
227 memset(colortab
, 0xff, dest_width
);
228 memset(colortab
+ dest_width
, 0, dest_width
);
230 colors
[0] = colortab
;
231 colors
[1] = colortab
;
232 } else if (s
->inverse
) {
233 colors
[0] = colortab
;
234 colors
[1] = colortab
+ dest_width
;
236 colors
[0] = colortab
+ dest_width
;
237 colors
[1] = colortab
;
239 dest
= ds_get_data(s
->ds
);
240 for (y
= 0; y
< 16; y
++) {
241 line
= (y
+ s
->start_line
) & 63;
242 src
= s
->framebuffer
+ 132 * (line
>> 3) + 36;
243 mask
= 1 << (line
& 7);
244 for (x
= 0; x
< 96; x
++) {
245 memcpy(dest
, colors
[(*src
& mask
) != 0], dest_width
);
249 for (x
= 1; x
< MAGNIFY
; x
++) {
250 memcpy(dest
, dest
- dest_width
* 96, dest_width
* 96);
251 dest
+= dest_width
* 96;
255 dpy_update(s
->ds
, 0, 0, 96 * MAGNIFY
, 16 * MAGNIFY
);
258 static void ssd0303_invalidate_display(void * opaque
)
260 ssd0303_state
*s
= (ssd0303_state
*)opaque
;
264 static void ssd0303_save(QEMUFile
*f
, void *opaque
)
266 ssd0303_state
*s
= (ssd0303_state
*)opaque
;
268 qemu_put_be32(f
, s
->row
);
269 qemu_put_be32(f
, s
->col
);
270 qemu_put_be32(f
, s
->start_line
);
271 qemu_put_be32(f
, s
->mirror
);
272 qemu_put_be32(f
, s
->flash
);
273 qemu_put_be32(f
, s
->enabled
);
274 qemu_put_be32(f
, s
->inverse
);
275 qemu_put_be32(f
, s
->redraw
);
276 qemu_put_be32(f
, s
->mode
);
277 qemu_put_be32(f
, s
->cmd_state
);
278 qemu_put_buffer(f
, s
->framebuffer
, sizeof(s
->framebuffer
));
280 i2c_slave_save(f
, &s
->i2c
);
283 static int ssd0303_load(QEMUFile
*f
, void *opaque
, int version_id
)
285 ssd0303_state
*s
= (ssd0303_state
*)opaque
;
290 s
->row
= qemu_get_be32(f
);
291 s
->col
= qemu_get_be32(f
);
292 s
->start_line
= qemu_get_be32(f
);
293 s
->mirror
= qemu_get_be32(f
);
294 s
->flash
= qemu_get_be32(f
);
295 s
->enabled
= qemu_get_be32(f
);
296 s
->inverse
= qemu_get_be32(f
);
297 s
->redraw
= qemu_get_be32(f
);
298 s
->mode
= qemu_get_be32(f
);
299 s
->cmd_state
= qemu_get_be32(f
);
300 qemu_get_buffer(f
, s
->framebuffer
, sizeof(s
->framebuffer
));
302 i2c_slave_load(f
, &s
->i2c
);
307 static void ssd0303_init(i2c_slave
*i2c
)
309 ssd0303_state
*s
= FROM_I2C_SLAVE(ssd0303_state
, i2c
);
311 s
->ds
= graphic_console_init(ssd0303_update_display
,
312 ssd0303_invalidate_display
,
314 qemu_console_resize(s
->ds
, 96 * MAGNIFY
, 16 * MAGNIFY
);
315 register_savevm("ssd0303_oled", -1, 1, ssd0303_save
, ssd0303_load
, s
);
318 static I2CSlaveInfo ssd0303_info
= {
319 .init
= ssd0303_init
,
320 .event
= ssd0303_event
,
321 .recv
= ssd0303_recv
,
325 static void ssd0303_register_devices(void)
327 i2c_register_slave("ssd0303", sizeof(ssd0303_state
), &ssd0303_info
);
330 device_init(ssd0303_register_devices
)