balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 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 | |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 10 | #include "i2c.h" |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 11 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 12 | typedef struct { |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 13 | i2c_slave i2c; |
| 14 | int i2c_command_byte; |
| 15 | int len; |
| 16 | |
| 17 | uint8_t level; |
| 18 | uint8_t direction; |
| 19 | uint8_t polarity; |
| 20 | uint8_t status; |
| 21 | uint8_t command; |
| 22 | qemu_irq handler[8]; |
| 23 | qemu_irq *gpio_in; |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 24 | } MAX7310State; |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 25 | |
Dmitry Eremin-Solenikov | 987e8b3 | 2011-02-11 23:57:38 +0300 | [diff] [blame] | 26 | static void max7310_reset(DeviceState *dev) |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 27 | { |
Dmitry Eremin-Solenikov | 987e8b3 | 2011-02-11 23:57:38 +0300 | [diff] [blame] | 28 | MAX7310State *s = FROM_I2C_SLAVE(MAX7310State, I2C_SLAVE_FROM_QDEV(dev)); |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 29 | s->level &= s->direction; |
| 30 | s->direction = 0xff; |
| 31 | s->polarity = 0xf0; |
| 32 | s->status = 0x01; |
| 33 | s->command = 0x00; |
| 34 | } |
| 35 | |
| 36 | static int max7310_rx(i2c_slave *i2c) |
| 37 | { |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 38 | MAX7310State *s = (MAX7310State *) i2c; |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 39 | |
| 40 | switch (s->command) { |
| 41 | case 0x00: /* Input port */ |
| 42 | return s->level ^ s->polarity; |
| 43 | break; |
| 44 | |
| 45 | case 0x01: /* Output port */ |
| 46 | return s->level & ~s->direction; |
| 47 | break; |
| 48 | |
| 49 | case 0x02: /* Polarity inversion */ |
| 50 | return s->polarity; |
| 51 | |
| 52 | case 0x03: /* Configuration */ |
| 53 | return s->direction; |
| 54 | |
| 55 | case 0x04: /* Timeout */ |
| 56 | return s->status; |
| 57 | break; |
| 58 | |
| 59 | case 0xff: /* Reserved */ |
| 60 | return 0xff; |
| 61 | |
| 62 | default: |
| 63 | #ifdef VERBOSE |
| 64 | printf("%s: unknown register %02x\n", __FUNCTION__, s->command); |
| 65 | #endif |
| 66 | break; |
| 67 | } |
| 68 | return 0xff; |
| 69 | } |
| 70 | |
| 71 | static int max7310_tx(i2c_slave *i2c, uint8_t data) |
| 72 | { |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 73 | MAX7310State *s = (MAX7310State *) i2c; |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 74 | uint8_t diff; |
| 75 | int line; |
| 76 | |
| 77 | if (s->len ++ > 1) { |
| 78 | #ifdef VERBOSE |
| 79 | printf("%s: message too long (%i bytes)\n", __FUNCTION__, s->len); |
| 80 | #endif |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | if (s->i2c_command_byte) { |
| 85 | s->command = data; |
| 86 | s->i2c_command_byte = 0; |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | switch (s->command) { |
| 91 | case 0x01: /* Output port */ |
| 92 | for (diff = (data ^ s->level) & ~s->direction; diff; |
| 93 | diff &= ~(1 << line)) { |
| 94 | line = ffs(diff) - 1; |
| 95 | if (s->handler[line]) |
| 96 | qemu_set_irq(s->handler[line], (data >> line) & 1); |
| 97 | } |
| 98 | s->level = (s->level & s->direction) | (data & ~s->direction); |
| 99 | break; |
| 100 | |
| 101 | case 0x02: /* Polarity inversion */ |
| 102 | s->polarity = data; |
| 103 | break; |
| 104 | |
| 105 | case 0x03: /* Configuration */ |
| 106 | s->level &= ~(s->direction ^ data); |
| 107 | s->direction = data; |
| 108 | break; |
| 109 | |
| 110 | case 0x04: /* Timeout */ |
| 111 | s->status = data; |
| 112 | break; |
| 113 | |
| 114 | case 0x00: /* Input port - ignore writes */ |
| 115 | break; |
| 116 | default: |
| 117 | #ifdef VERBOSE |
| 118 | printf("%s: unknown register %02x\n", __FUNCTION__, s->command); |
| 119 | #endif |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static void max7310_event(i2c_slave *i2c, enum i2c_event event) |
| 127 | { |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 128 | MAX7310State *s = (MAX7310State *) i2c; |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 129 | s->len = 0; |
| 130 | |
| 131 | switch (event) { |
| 132 | case I2C_START_SEND: |
| 133 | s->i2c_command_byte = 1; |
| 134 | break; |
| 135 | case I2C_FINISH: |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 136 | #ifdef VERBOSE |
balrog | e62ab7a | 2008-05-09 22:17:18 +0000 | [diff] [blame] | 137 | if (s->len == 1) |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 138 | printf("%s: message too short (%i bytes)\n", __FUNCTION__, s->len); |
| 139 | #endif |
| 140 | break; |
| 141 | default: |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
Juan Quintela | 7cb45fa | 2009-09-29 22:48:32 +0200 | [diff] [blame] | 146 | static const VMStateDescription vmstate_max7310 = { |
| 147 | .name = "max7310", |
| 148 | .version_id = 0, |
| 149 | .minimum_version_id = 0, |
| 150 | .minimum_version_id_old = 0, |
| 151 | .fields = (VMStateField []) { |
| 152 | VMSTATE_INT32(i2c_command_byte, MAX7310State), |
| 153 | VMSTATE_INT32(len, MAX7310State), |
| 154 | VMSTATE_UINT8(level, MAX7310State), |
| 155 | VMSTATE_UINT8(direction, MAX7310State), |
| 156 | VMSTATE_UINT8(polarity, MAX7310State), |
| 157 | VMSTATE_UINT8(status, MAX7310State), |
| 158 | VMSTATE_UINT8(command, MAX7310State), |
| 159 | VMSTATE_I2C_SLAVE(i2c, MAX7310State), |
| 160 | VMSTATE_END_OF_LIST() |
| 161 | } |
| 162 | }; |
balrog | aa941b9 | 2007-05-24 18:50:09 +0000 | [diff] [blame] | 163 | |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 164 | static void max7310_gpio_set(void *opaque, int line, int level) |
| 165 | { |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 166 | MAX7310State *s = (MAX7310State *) opaque; |
malc | b1503cd | 2008-12-22 20:33:55 +0000 | [diff] [blame] | 167 | if (line >= ARRAY_SIZE(s->handler) || line < 0) |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 168 | hw_error("bad GPIO line"); |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 169 | |
| 170 | if (level) |
| 171 | s->level |= s->direction & (1 << line); |
| 172 | else |
| 173 | s->level &= ~(s->direction & (1 << line)); |
| 174 | } |
| 175 | |
| 176 | /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols), |
| 177 | * but also accepts sequences that are not SMBus so return an I2C device. */ |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 178 | static int max7310_init(i2c_slave *i2c) |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 179 | { |
Paul Brook | 6c0bd6b | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 180 | MAX7310State *s = FROM_I2C_SLAVE(MAX7310State, i2c); |
| 181 | |
Dmitry Eremin-Solenikov | 987e8b3 | 2011-02-11 23:57:38 +0300 | [diff] [blame] | 182 | qdev_init_gpio_in(&i2c->qdev, max7310_gpio_set, 8); |
| 183 | qdev_init_gpio_out(&i2c->qdev, s->handler, 8); |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 184 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 185 | return 0; |
balrog | adb86c3 | 2007-05-23 22:04:23 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Paul Brook | 6c0bd6b | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 188 | static I2CSlaveInfo max7310_info = { |
Gerd Hoffmann | 074f2ff | 2009-06-10 09:41:42 +0200 | [diff] [blame] | 189 | .qdev.name = "max7310", |
| 190 | .qdev.size = sizeof(MAX7310State), |
Juan Quintela | be73cfe | 2009-12-02 12:36:46 +0100 | [diff] [blame] | 191 | .qdev.vmsd = &vmstate_max7310, |
Dmitry Eremin-Solenikov | 987e8b3 | 2011-02-11 23:57:38 +0300 | [diff] [blame] | 192 | .qdev.reset = max7310_reset, |
Paul Brook | 6c0bd6b | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 193 | .init = max7310_init, |
| 194 | .event = max7310_event, |
| 195 | .recv = max7310_rx, |
| 196 | .send = max7310_tx |
| 197 | }; |
| 198 | |
| 199 | static void max7310_register_devices(void) |
| 200 | { |
Gerd Hoffmann | 074f2ff | 2009-06-10 09:41:42 +0200 | [diff] [blame] | 201 | i2c_register_slave(&max7310_info); |
Paul Brook | 6c0bd6b | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | device_init(max7310_register_devices) |