xref: /openbmc/qemu/hw/gpio/max7310.c (revision c51a3f5d)
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/module.h"
17 
18 #define TYPE_MAX7310 "max7310"
19 #define MAX7310(obj) OBJECT_CHECK(MAX7310State, (obj), TYPE_MAX7310)
20 
21 typedef struct MAX7310State {
22     I2CSlave parent_obj;
23 
24     int i2c_command_byte;
25     int len;
26 
27     uint8_t level;
28     uint8_t direction;
29     uint8_t polarity;
30     uint8_t status;
31     uint8_t command;
32     qemu_irq handler[8];
33     qemu_irq *gpio_in;
34 } MAX7310State;
35 
36 static void max7310_reset(DeviceState *dev)
37 {
38     MAX7310State *s = MAX7310(dev);
39 
40     s->level &= s->direction;
41     s->direction = 0xff;
42     s->polarity = 0xf0;
43     s->status = 0x01;
44     s->command = 0x00;
45 }
46 
47 static uint8_t max7310_rx(I2CSlave *i2c)
48 {
49     MAX7310State *s = MAX7310(i2c);
50 
51     switch (s->command) {
52     case 0x00:	/* Input port */
53         return s->level ^ s->polarity;
54 
55     case 0x01:	/* Output port */
56         return s->level & ~s->direction;
57 
58     case 0x02:	/* Polarity inversion */
59         return s->polarity;
60 
61     case 0x03:	/* Configuration */
62         return s->direction;
63 
64     case 0x04:	/* Timeout */
65         return s->status;
66 
67     case 0xff:	/* Reserved */
68         return 0xff;
69 
70     default:
71 #ifdef VERBOSE
72         printf("%s: unknown register %02x\n", __func__, s->command);
73 #endif
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 #ifdef VERBOSE
126         printf("%s: unknown register %02x\n", __func__, s->command);
127 #endif
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     if (line >= ARRAY_SIZE(s->handler) || line  < 0)
177         hw_error("bad GPIO line");
178 
179     if (level)
180         s->level |= s->direction & (1 << line);
181     else
182         s->level &= ~(s->direction & (1 << line));
183 }
184 
185 /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
186  * but also accepts sequences that are not SMBus so return an I2C device.  */
187 static void max7310_realize(DeviceState *dev, Error **errp)
188 {
189     I2CSlave *i2c = I2C_SLAVE(dev);
190     MAX7310State *s = MAX7310(dev);
191 
192     qdev_init_gpio_in(&i2c->qdev, max7310_gpio_set, 8);
193     qdev_init_gpio_out(&i2c->qdev, s->handler, 8);
194 }
195 
196 static void max7310_class_init(ObjectClass *klass, void *data)
197 {
198     DeviceClass *dc = DEVICE_CLASS(klass);
199     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
200 
201     dc->realize = max7310_realize;
202     k->event = max7310_event;
203     k->recv = max7310_rx;
204     k->send = max7310_tx;
205     dc->reset = max7310_reset;
206     dc->vmsd = &vmstate_max7310;
207 }
208 
209 static const TypeInfo max7310_info = {
210     .name          = TYPE_MAX7310,
211     .parent        = TYPE_I2C_SLAVE,
212     .instance_size = sizeof(MAX7310State),
213     .class_init    = max7310_class_init,
214 };
215 
216 static void max7310_register_types(void)
217 {
218     type_register_static(&max7310_info);
219 }
220 
221 type_init(max7310_register_types)
222