]> git.proxmox.com Git - qemu.git/blame - hw/max7310.c
SSD0303 qdev conversion
[qemu.git] / hw / max7310.c
CommitLineData
adb86c37
AZ
1/*
2 * MAX7310 8-port GPIO expansion chip.
3 *
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
6 *
7 * This file is licensed under GNU GPL.
8 */
9
87ecb68b
PB
10#include "hw.h"
11#include "i2c.h"
adb86c37 12
bc24a225 13typedef struct {
adb86c37
AZ
14 i2c_slave i2c;
15 int i2c_command_byte;
16 int len;
17
18 uint8_t level;
19 uint8_t direction;
20 uint8_t polarity;
21 uint8_t status;
22 uint8_t command;
23 qemu_irq handler[8];
24 qemu_irq *gpio_in;
bc24a225 25} MAX7310State;
adb86c37
AZ
26
27void max7310_reset(i2c_slave *i2c)
28{
bc24a225 29 MAX7310State *s = (MAX7310State *) i2c;
adb86c37
AZ
30 s->level &= s->direction;
31 s->direction = 0xff;
32 s->polarity = 0xf0;
33 s->status = 0x01;
34 s->command = 0x00;
35}
36
37static int max7310_rx(i2c_slave *i2c)
38{
bc24a225 39 MAX7310State *s = (MAX7310State *) i2c;
adb86c37
AZ
40
41 switch (s->command) {
42 case 0x00: /* Input port */
43 return s->level ^ s->polarity;
44 break;
45
46 case 0x01: /* Output port */
47 return s->level & ~s->direction;
48 break;
49
50 case 0x02: /* Polarity inversion */
51 return s->polarity;
52
53 case 0x03: /* Configuration */
54 return s->direction;
55
56 case 0x04: /* Timeout */
57 return s->status;
58 break;
59
60 case 0xff: /* Reserved */
61 return 0xff;
62
63 default:
64#ifdef VERBOSE
65 printf("%s: unknown register %02x\n", __FUNCTION__, s->command);
66#endif
67 break;
68 }
69 return 0xff;
70}
71
72static int max7310_tx(i2c_slave *i2c, uint8_t data)
73{
bc24a225 74 MAX7310State *s = (MAX7310State *) i2c;
adb86c37
AZ
75 uint8_t diff;
76 int line;
77
78 if (s->len ++ > 1) {
79#ifdef VERBOSE
80 printf("%s: message too long (%i bytes)\n", __FUNCTION__, s->len);
81#endif
82 return 1;
83 }
84
85 if (s->i2c_command_byte) {
86 s->command = data;
87 s->i2c_command_byte = 0;
88 return 0;
89 }
90
91 switch (s->command) {
92 case 0x01: /* Output port */
93 for (diff = (data ^ s->level) & ~s->direction; diff;
94 diff &= ~(1 << line)) {
95 line = ffs(diff) - 1;
96 if (s->handler[line])
97 qemu_set_irq(s->handler[line], (data >> line) & 1);
98 }
99 s->level = (s->level & s->direction) | (data & ~s->direction);
100 break;
101
102 case 0x02: /* Polarity inversion */
103 s->polarity = data;
104 break;
105
106 case 0x03: /* Configuration */
107 s->level &= ~(s->direction ^ data);
108 s->direction = data;
109 break;
110
111 case 0x04: /* Timeout */
112 s->status = data;
113 break;
114
115 case 0x00: /* Input port - ignore writes */
116 break;
117 default:
118#ifdef VERBOSE
119 printf("%s: unknown register %02x\n", __FUNCTION__, s->command);
120#endif
121 return 1;
122 }
123
124 return 0;
125}
126
127static void max7310_event(i2c_slave *i2c, enum i2c_event event)
128{
bc24a225 129 MAX7310State *s = (MAX7310State *) i2c;
adb86c37
AZ
130 s->len = 0;
131
132 switch (event) {
133 case I2C_START_SEND:
134 s->i2c_command_byte = 1;
135 break;
136 case I2C_FINISH:
adb86c37 137#ifdef VERBOSE
e62ab7a1 138 if (s->len == 1)
adb86c37
AZ
139 printf("%s: message too short (%i bytes)\n", __FUNCTION__, s->len);
140#endif
141 break;
142 default:
143 break;
144 }
145}
146
aa941b94
AZ
147static void max7310_save(QEMUFile *f, void *opaque)
148{
bc24a225 149 MAX7310State *s = (MAX7310State *) opaque;
aa941b94
AZ
150
151 qemu_put_be32(f, s->i2c_command_byte);
152 qemu_put_be32(f, s->len);
153
154 qemu_put_8s(f, &s->level);
155 qemu_put_8s(f, &s->direction);
156 qemu_put_8s(f, &s->polarity);
157 qemu_put_8s(f, &s->status);
158 qemu_put_8s(f, &s->command);
159
160 i2c_slave_save(f, &s->i2c);
161}
162
163static int max7310_load(QEMUFile *f, void *opaque, int version_id)
164{
bc24a225 165 MAX7310State *s = (MAX7310State *) opaque;
aa941b94
AZ
166
167 s->i2c_command_byte = qemu_get_be32(f);
168 s->len = qemu_get_be32(f);
169
170 qemu_get_8s(f, &s->level);
171 qemu_get_8s(f, &s->direction);
172 qemu_get_8s(f, &s->polarity);
173 qemu_get_8s(f, &s->status);
174 qemu_get_8s(f, &s->command);
175
176 i2c_slave_load(f, &s->i2c);
177 return 0;
178}
179
adb86c37
AZ
180static void max7310_gpio_set(void *opaque, int line, int level)
181{
bc24a225 182 MAX7310State *s = (MAX7310State *) opaque;
b1503cda 183 if (line >= ARRAY_SIZE(s->handler) || line < 0)
87ecb68b 184 hw_error("bad GPIO line");
adb86c37
AZ
185
186 if (level)
187 s->level |= s->direction & (1 << line);
188 else
189 s->level &= ~(s->direction & (1 << line));
190}
191
192/* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
193 * but also accepts sequences that are not SMBus so return an I2C device. */
bc24a225 194i2c_slave *max7310_init(i2c_bus *bus)
adb86c37 195{
bc24a225
PB
196 MAX7310State *s = (MAX7310State *)
197 i2c_slave_init(bus, 0, sizeof(MAX7310State));
adb86c37
AZ
198 s->i2c.event = max7310_event;
199 s->i2c.recv = max7310_rx;
200 s->i2c.send = max7310_tx;
201 s->gpio_in = qemu_allocate_irqs(max7310_gpio_set, s,
b1503cda 202 ARRAY_SIZE(s->handler));
adb86c37
AZ
203
204 max7310_reset(&s->i2c);
205
18be5187 206 register_savevm("max7310", -1, 0, max7310_save, max7310_load, s);
aa941b94 207
adb86c37
AZ
208 return &s->i2c;
209}
210
211qemu_irq *max7310_gpio_in_get(i2c_slave *i2c)
212{
bc24a225 213 MAX7310State *s = (MAX7310State *) i2c;
adb86c37
AZ
214 return s->gpio_in;
215}
216
217void max7310_gpio_out_set(i2c_slave *i2c, int line, qemu_irq handler)
218{
bc24a225 219 MAX7310State *s = (MAX7310State *) i2c;
b1503cda 220 if (line >= ARRAY_SIZE(s->handler) || line < 0)
87ecb68b 221 hw_error("bad GPIO line");
adb86c37
AZ
222
223 s->handler[line] = handler;
224}