]> git.proxmox.com Git - mirror_qemu.git/blob - hw/gpio/max7310.c
hw/gpio/max7310: Replace disabled printf() by qemu_log_mask(UNIMP)
[mirror_qemu.git] / hw / gpio / max7310.c
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
10 #include "qemu/osdep.h"
11 #include "hw/hw.h"
12 #include "hw/i2c/i2c.h"
13 #include "hw/hw.h"
14 #include "hw/irq.h"
15 #include "migration/vmstate.h"
16 #include "qemu/log.h"
17 #include "qemu/module.h"
18
19 #define TYPE_MAX7310 "max7310"
20 #define MAX7310(obj) OBJECT_CHECK(MAX7310State, (obj), TYPE_MAX7310)
21
22 typedef struct MAX7310State {
23 I2CSlave parent_obj;
24
25 int i2c_command_byte;
26 int len;
27
28 uint8_t level;
29 uint8_t direction;
30 uint8_t polarity;
31 uint8_t status;
32 uint8_t command;
33 qemu_irq handler[8];
34 qemu_irq *gpio_in;
35 } MAX7310State;
36
37 static void max7310_reset(DeviceState *dev)
38 {
39 MAX7310State *s = MAX7310(dev);
40
41 s->level &= s->direction;
42 s->direction = 0xff;
43 s->polarity = 0xf0;
44 s->status = 0x01;
45 s->command = 0x00;
46 }
47
48 static uint8_t max7310_rx(I2CSlave *i2c)
49 {
50 MAX7310State *s = MAX7310(i2c);
51
52 switch (s->command) {
53 case 0x00: /* Input port */
54 return s->level ^ s->polarity;
55
56 case 0x01: /* Output port */
57 return s->level & ~s->direction;
58
59 case 0x02: /* Polarity inversion */
60 return s->polarity;
61
62 case 0x03: /* Configuration */
63 return s->direction;
64
65 case 0x04: /* Timeout */
66 return s->status;
67
68 case 0xff: /* Reserved */
69 return 0xff;
70
71 default:
72 qemu_log_mask(LOG_UNIMP, "%s: Unsupported register 0x02%" PRIx8 "\n",
73 __func__, s->command);
74 break;
75 }
76 return 0xff;
77 }
78
79 static int max7310_tx(I2CSlave *i2c, uint8_t data)
80 {
81 MAX7310State *s = MAX7310(i2c);
82 uint8_t diff;
83 int line;
84
85 if (s->len ++ > 1) {
86 #ifdef VERBOSE
87 printf("%s: message too long (%i bytes)\n", __func__, s->len);
88 #endif
89 return 1;
90 }
91
92 if (s->i2c_command_byte) {
93 s->command = data;
94 s->i2c_command_byte = 0;
95 return 0;
96 }
97
98 switch (s->command) {
99 case 0x01: /* Output port */
100 for (diff = (data ^ s->level) & ~s->direction; diff;
101 diff &= ~(1 << line)) {
102 line = ctz32(diff);
103 if (s->handler[line])
104 qemu_set_irq(s->handler[line], (data >> line) & 1);
105 }
106 s->level = (s->level & s->direction) | (data & ~s->direction);
107 break;
108
109 case 0x02: /* Polarity inversion */
110 s->polarity = data;
111 break;
112
113 case 0x03: /* Configuration */
114 s->level &= ~(s->direction ^ data);
115 s->direction = data;
116 break;
117
118 case 0x04: /* Timeout */
119 s->status = data;
120 break;
121
122 case 0x00: /* Input port - ignore writes */
123 break;
124 default:
125 qemu_log_mask(LOG_UNIMP, "%s: Unsupported register 0x02%" PRIx8 "\n",
126 __func__, s->command);
127 return 1;
128 }
129
130 return 0;
131 }
132
133 static int max7310_event(I2CSlave *i2c, enum i2c_event event)
134 {
135 MAX7310State *s = MAX7310(i2c);
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:
143 #ifdef VERBOSE
144 if (s->len == 1)
145 printf("%s: message too short (%i bytes)\n", __func__, s->len);
146 #endif
147 break;
148 default:
149 break;
150 }
151
152 return 0;
153 }
154
155 static const VMStateDescription vmstate_max7310 = {
156 .name = "max7310",
157 .version_id = 0,
158 .minimum_version_id = 0,
159 .fields = (VMStateField[]) {
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),
167 VMSTATE_I2C_SLAVE(parent_obj, MAX7310State),
168 VMSTATE_END_OF_LIST()
169 }
170 };
171
172 static void max7310_gpio_set(void *opaque, int line, int level)
173 {
174 MAX7310State *s = (MAX7310State *) opaque;
175 if (line >= ARRAY_SIZE(s->handler) || line < 0)
176 hw_error("bad GPIO line");
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. */
186 static void max7310_realize(DeviceState *dev, Error **errp)
187 {
188 I2CSlave *i2c = I2C_SLAVE(dev);
189 MAX7310State *s = MAX7310(dev);
190
191 qdev_init_gpio_in(&i2c->qdev, max7310_gpio_set, 8);
192 qdev_init_gpio_out(&i2c->qdev, s->handler, 8);
193 }
194
195 static void max7310_class_init(ObjectClass *klass, void *data)
196 {
197 DeviceClass *dc = DEVICE_CLASS(klass);
198 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
199
200 dc->realize = max7310_realize;
201 k->event = max7310_event;
202 k->recv = max7310_rx;
203 k->send = max7310_tx;
204 dc->reset = max7310_reset;
205 dc->vmsd = &vmstate_max7310;
206 }
207
208 static const TypeInfo max7310_info = {
209 .name = TYPE_MAX7310,
210 .parent = TYPE_I2C_SLAVE,
211 .instance_size = sizeof(MAX7310State),
212 .class_init = max7310_class_init,
213 };
214
215 static void max7310_register_types(void)
216 {
217 type_register_static(&max7310_info);
218 }
219
220 type_init(max7310_register_types)