xref: /qemu/hw/gpio/max7310.c (revision e3a6e0da)
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/i2c/i2c.h"
12 #include "hw/irq.h"
13 #include "migration/vmstate.h"
14 #include "qemu/log.h"
15 #include "qemu/module.h"
16 #include "qom/object.h"
17 
18 #define TYPE_MAX7310 "max7310"
19 typedef struct MAX7310State MAX7310State;
20 DECLARE_INSTANCE_CHECKER(MAX7310State, MAX7310,
21                          TYPE_MAX7310)
22 
23 struct MAX7310State {
24     I2CSlave parent_obj;
25 
26     int i2c_command_byte;
27     int len;
28 
29     uint8_t level;
30     uint8_t direction;
31     uint8_t polarity;
32     uint8_t status;
33     uint8_t command;
34     qemu_irq handler[8];
35     qemu_irq *gpio_in;
36 };
37 
38 static void max7310_reset(DeviceState *dev)
39 {
40     MAX7310State *s = MAX7310(dev);
41 
42     s->level &= s->direction;
43     s->direction = 0xff;
44     s->polarity = 0xf0;
45     s->status = 0x01;
46     s->command = 0x00;
47 }
48 
49 static uint8_t max7310_rx(I2CSlave *i2c)
50 {
51     MAX7310State *s = MAX7310(i2c);
52 
53     switch (s->command) {
54     case 0x00:	/* Input port */
55         return s->level ^ s->polarity;
56 
57     case 0x01:	/* Output port */
58         return s->level & ~s->direction;
59 
60     case 0x02:	/* Polarity inversion */
61         return s->polarity;
62 
63     case 0x03:	/* Configuration */
64         return s->direction;
65 
66     case 0x04:	/* Timeout */
67         return s->status;
68 
69     case 0xff:	/* Reserved */
70         return 0xff;
71 
72     default:
73         qemu_log_mask(LOG_UNIMP, "%s: Unsupported register 0x02%" PRIx8 "\n",
74                       __func__, s->command);
75         break;
76     }
77     return 0xff;
78 }
79 
80 static int max7310_tx(I2CSlave *i2c, uint8_t data)
81 {
82     MAX7310State *s = MAX7310(i2c);
83     uint8_t diff;
84     int line;
85 
86     if (s->len ++ > 1) {
87 #ifdef VERBOSE
88         printf("%s: message too long (%i bytes)\n", __func__, s->len);
89 #endif
90         return 1;
91     }
92 
93     if (s->i2c_command_byte) {
94         s->command = data;
95         s->i2c_command_byte = 0;
96         return 0;
97     }
98 
99     switch (s->command) {
100     case 0x01:	/* Output port */
101         for (diff = (data ^ s->level) & ~s->direction; diff;
102                         diff &= ~(1 << line)) {
103             line = ctz32(diff);
104             if (s->handler[line])
105                 qemu_set_irq(s->handler[line], (data >> line) & 1);
106         }
107         s->level = (s->level & s->direction) | (data & ~s->direction);
108         break;
109 
110     case 0x02:	/* Polarity inversion */
111         s->polarity = data;
112         break;
113 
114     case 0x03:	/* Configuration */
115         s->level &= ~(s->direction ^ data);
116         s->direction = data;
117         break;
118 
119     case 0x04:	/* Timeout */
120         s->status = data;
121         break;
122 
123     case 0x00:	/* Input port - ignore writes */
124         break;
125     default:
126         qemu_log_mask(LOG_UNIMP, "%s: Unsupported register 0x02%" PRIx8 "\n",
127                       __func__, s->command);
128         return 1;
129     }
130 
131     return 0;
132 }
133 
134 static int max7310_event(I2CSlave *i2c, enum i2c_event event)
135 {
136     MAX7310State *s = MAX7310(i2c);
137     s->len = 0;
138 
139     switch (event) {
140     case I2C_START_SEND:
141         s->i2c_command_byte = 1;
142         break;
143     case I2C_FINISH:
144 #ifdef VERBOSE
145         if (s->len == 1)
146             printf("%s: message too short (%i bytes)\n", __func__, s->len);
147 #endif
148         break;
149     default:
150         break;
151     }
152 
153     return 0;
154 }
155 
156 static const VMStateDescription vmstate_max7310 = {
157     .name = "max7310",
158     .version_id = 0,
159     .minimum_version_id = 0,
160     .fields = (VMStateField[]) {
161         VMSTATE_INT32(i2c_command_byte, MAX7310State),
162         VMSTATE_INT32(len, MAX7310State),
163         VMSTATE_UINT8(level, MAX7310State),
164         VMSTATE_UINT8(direction, MAX7310State),
165         VMSTATE_UINT8(polarity, MAX7310State),
166         VMSTATE_UINT8(status, MAX7310State),
167         VMSTATE_UINT8(command, MAX7310State),
168         VMSTATE_I2C_SLAVE(parent_obj, MAX7310State),
169         VMSTATE_END_OF_LIST()
170     }
171 };
172 
173 static void max7310_gpio_set(void *opaque, int line, int level)
174 {
175     MAX7310State *s = (MAX7310State *) opaque;
176     assert(line >= 0 && line < ARRAY_SIZE(s->handler));
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)
221