Statistics
| Branch: | Revision:

root / hw / display / ssd0303.c @ 2c62f08d

History | View | Annotate | Download (8.9 kB)

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