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