]>
Commit | Line | Data |
---|---|---|
adb86c37 AZ |
1 | /* |
2 | * MAX7310 8-port GPIO expansion chip. | |
3 | * | |
4 | * Copyright (c) 2006 Openedhand Ltd. | |
5 | * Written by Andrzej Zaborowski <[email protected]> | |
6 | * | |
7 | * This file is licensed under GNU GPL. | |
8 | */ | |
9 | ||
0430891c | 10 | #include "qemu/osdep.h" |
0d09e41a | 11 | #include "hw/i2c/i2c.h" |
adb86c37 | 12 | |
b8bcf811 AF |
13 | #define TYPE_MAX7310 "max7310" |
14 | #define MAX7310(obj) OBJECT_CHECK(MAX7310State, (obj), TYPE_MAX7310) | |
15 | ||
16 | typedef struct MAX7310State { | |
17 | I2CSlave parent_obj; | |
18 | ||
adb86c37 AZ |
19 | int i2c_command_byte; |
20 | int len; | |
21 | ||
22 | uint8_t level; | |
23 | uint8_t direction; | |
24 | uint8_t polarity; | |
25 | uint8_t status; | |
26 | uint8_t command; | |
27 | qemu_irq handler[8]; | |
28 | qemu_irq *gpio_in; | |
bc24a225 | 29 | } MAX7310State; |
adb86c37 | 30 | |
987e8b3b | 31 | static void max7310_reset(DeviceState *dev) |
adb86c37 | 32 | { |
b8bcf811 AF |
33 | MAX7310State *s = MAX7310(dev); |
34 | ||
adb86c37 AZ |
35 | s->level &= s->direction; |
36 | s->direction = 0xff; | |
37 | s->polarity = 0xf0; | |
38 | s->status = 0x01; | |
39 | s->command = 0x00; | |
40 | } | |
41 | ||
9e07bdf8 | 42 | static int max7310_rx(I2CSlave *i2c) |
adb86c37 | 43 | { |
b8bcf811 | 44 | MAX7310State *s = MAX7310(i2c); |
adb86c37 AZ |
45 | |
46 | switch (s->command) { | |
47 | case 0x00: /* Input port */ | |
48 | return s->level ^ s->polarity; | |
49 | break; | |
50 | ||
51 | case 0x01: /* Output port */ | |
52 | return s->level & ~s->direction; | |
53 | break; | |
54 | ||
55 | case 0x02: /* Polarity inversion */ | |
56 | return s->polarity; | |
57 | ||
58 | case 0x03: /* Configuration */ | |
59 | return s->direction; | |
60 | ||
61 | case 0x04: /* Timeout */ | |
62 | return s->status; | |
63 | break; | |
64 | ||
65 | case 0xff: /* Reserved */ | |
66 | return 0xff; | |
67 | ||
68 | default: | |
69 | #ifdef VERBOSE | |
70 | printf("%s: unknown register %02x\n", __FUNCTION__, s->command); | |
71 | #endif | |
72 | break; | |
73 | } | |
74 | return 0xff; | |
75 | } | |
76 | ||
9e07bdf8 | 77 | static int max7310_tx(I2CSlave *i2c, uint8_t data) |
adb86c37 | 78 | { |
b8bcf811 | 79 | MAX7310State *s = MAX7310(i2c); |
adb86c37 AZ |
80 | uint8_t diff; |
81 | int line; | |
82 | ||
83 | if (s->len ++ > 1) { | |
84 | #ifdef VERBOSE | |
85 | printf("%s: message too long (%i bytes)\n", __FUNCTION__, s->len); | |
86 | #endif | |
87 | return 1; | |
88 | } | |
89 | ||
90 | if (s->i2c_command_byte) { | |
91 | s->command = data; | |
92 | s->i2c_command_byte = 0; | |
93 | return 0; | |
94 | } | |
95 | ||
96 | switch (s->command) { | |
97 | case 0x01: /* Output port */ | |
98 | for (diff = (data ^ s->level) & ~s->direction; diff; | |
99 | diff &= ~(1 << line)) { | |
786a4ea8 | 100 | line = ctz32(diff); |
adb86c37 AZ |
101 | if (s->handler[line]) |
102 | qemu_set_irq(s->handler[line], (data >> line) & 1); | |
103 | } | |
104 | s->level = (s->level & s->direction) | (data & ~s->direction); | |
105 | break; | |
106 | ||
107 | case 0x02: /* Polarity inversion */ | |
108 | s->polarity = data; | |
109 | break; | |
110 | ||
111 | case 0x03: /* Configuration */ | |
112 | s->level &= ~(s->direction ^ data); | |
113 | s->direction = data; | |
114 | break; | |
115 | ||
116 | case 0x04: /* Timeout */ | |
117 | s->status = data; | |
118 | break; | |
119 | ||
120 | case 0x00: /* Input port - ignore writes */ | |
121 | break; | |
122 | default: | |
123 | #ifdef VERBOSE | |
124 | printf("%s: unknown register %02x\n", __FUNCTION__, s->command); | |
125 | #endif | |
126 | return 1; | |
127 | } | |
128 | ||
129 | return 0; | |
130 | } | |
131 | ||
9e07bdf8 | 132 | static void max7310_event(I2CSlave *i2c, enum i2c_event event) |
adb86c37 | 133 | { |
b8bcf811 | 134 | MAX7310State *s = MAX7310(i2c); |
adb86c37 AZ |
135 | s->len = 0; |
136 | ||
137 | switch (event) { | |
138 | case I2C_START_SEND: | |
139 | s->i2c_command_byte = 1; | |
140 | break; | |
141 | case I2C_FINISH: | |
adb86c37 | 142 | #ifdef VERBOSE |
e62ab7a1 | 143 | if (s->len == 1) |
adb86c37 AZ |
144 | printf("%s: message too short (%i bytes)\n", __FUNCTION__, s->len); |
145 | #endif | |
146 | break; | |
147 | default: | |
148 | break; | |
149 | } | |
150 | } | |
151 | ||
7cb45faa JQ |
152 | static const VMStateDescription vmstate_max7310 = { |
153 | .name = "max7310", | |
154 | .version_id = 0, | |
155 | .minimum_version_id = 0, | |
8f1e884b | 156 | .fields = (VMStateField[]) { |
7cb45faa JQ |
157 | VMSTATE_INT32(i2c_command_byte, MAX7310State), |
158 | VMSTATE_INT32(len, MAX7310State), | |
159 | VMSTATE_UINT8(level, MAX7310State), | |
160 | VMSTATE_UINT8(direction, MAX7310State), | |
161 | VMSTATE_UINT8(polarity, MAX7310State), | |
162 | VMSTATE_UINT8(status, MAX7310State), | |
163 | VMSTATE_UINT8(command, MAX7310State), | |
b8bcf811 | 164 | VMSTATE_I2C_SLAVE(parent_obj, MAX7310State), |
7cb45faa JQ |
165 | VMSTATE_END_OF_LIST() |
166 | } | |
167 | }; | |
aa941b94 | 168 | |
adb86c37 AZ |
169 | static void max7310_gpio_set(void *opaque, int line, int level) |
170 | { | |
bc24a225 | 171 | MAX7310State *s = (MAX7310State *) opaque; |
b1503cda | 172 | if (line >= ARRAY_SIZE(s->handler) || line < 0) |
87ecb68b | 173 | hw_error("bad GPIO line"); |
adb86c37 AZ |
174 | |
175 | if (level) | |
176 | s->level |= s->direction & (1 << line); | |
177 | else | |
178 | s->level &= ~(s->direction & (1 << line)); | |
179 | } | |
180 | ||
181 | /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols), | |
182 | * but also accepts sequences that are not SMBus so return an I2C device. */ | |
9e07bdf8 | 183 | static int max7310_init(I2CSlave *i2c) |
adb86c37 | 184 | { |
b8bcf811 | 185 | MAX7310State *s = MAX7310(i2c); |
6c0bd6bd | 186 | |
987e8b3b DES |
187 | qdev_init_gpio_in(&i2c->qdev, max7310_gpio_set, 8); |
188 | qdev_init_gpio_out(&i2c->qdev, s->handler, 8); | |
adb86c37 | 189 | |
81a322d4 | 190 | return 0; |
adb86c37 AZ |
191 | } |
192 | ||
b5ea9327 AL |
193 | static void max7310_class_init(ObjectClass *klass, void *data) |
194 | { | |
39bffca2 | 195 | DeviceClass *dc = DEVICE_CLASS(klass); |
b5ea9327 AL |
196 | I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); |
197 | ||
198 | k->init = max7310_init; | |
199 | k->event = max7310_event; | |
200 | k->recv = max7310_rx; | |
201 | k->send = max7310_tx; | |
39bffca2 AL |
202 | dc->reset = max7310_reset; |
203 | dc->vmsd = &vmstate_max7310; | |
b5ea9327 AL |
204 | } |
205 | ||
8c43a6f0 | 206 | static const TypeInfo max7310_info = { |
b8bcf811 | 207 | .name = TYPE_MAX7310, |
39bffca2 AL |
208 | .parent = TYPE_I2C_SLAVE, |
209 | .instance_size = sizeof(MAX7310State), | |
210 | .class_init = max7310_class_init, | |
6c0bd6bd PB |
211 | }; |
212 | ||
83f7d43a | 213 | static void max7310_register_types(void) |
6c0bd6bd | 214 | { |
39bffca2 | 215 | type_register_static(&max7310_info); |
6c0bd6bd PB |
216 | } |
217 | ||
83f7d43a | 218 | type_init(max7310_register_types) |