qemu

FORK: QEMU emulator
git clone https://git.neptards.moe/neptards/qemu.git
Log | Files | Refs | Submodules | LICENSE

max7310.c (5214B)


      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 OBJECT_DECLARE_SIMPLE_TYPE(MAX7310State, MAX7310)
     20 
     21 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 };
     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         qemu_log_mask(LOG_UNIMP, "%s: Unsupported register 0x02%" PRIx8 "\n",
     72                       __func__, s->command);
     73         break;
     74     }
     75     return 0xff;
     76 }
     77 
     78 static int max7310_tx(I2CSlave *i2c, uint8_t data)
     79 {
     80     MAX7310State *s = MAX7310(i2c);
     81     uint8_t diff;
     82     int line;
     83 
     84     if (s->len ++ > 1) {
     85 #ifdef VERBOSE
     86         printf("%s: message too long (%i bytes)\n", __func__, s->len);
     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)) {
    101             line = ctz32(diff);
    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 */
    122         break;
    123     default:
    124         qemu_log_mask(LOG_UNIMP, "%s: Unsupported register 0x02%" PRIx8 "\n",
    125                       __func__, s->command);
    126         return 1;
    127     }
    128 
    129     return 0;
    130 }
    131 
    132 static int max7310_event(I2CSlave *i2c, enum i2c_event event)
    133 {
    134     MAX7310State *s = MAX7310(i2c);
    135     s->len = 0;
    136 
    137     switch (event) {
    138     case I2C_START_SEND:
    139         s->i2c_command_byte = 1;
    140         break;
    141     case I2C_FINISH:
    142 #ifdef VERBOSE
    143         if (s->len == 1)
    144             printf("%s: message too short (%i bytes)\n", __func__, s->len);
    145 #endif
    146         break;
    147     default:
    148         break;
    149     }
    150 
    151     return 0;
    152 }
    153 
    154 static const VMStateDescription vmstate_max7310 = {
    155     .name = "max7310",
    156     .version_id = 0,
    157     .minimum_version_id = 0,
    158     .fields = (VMStateField[]) {
    159         VMSTATE_INT32(i2c_command_byte, MAX7310State),
    160         VMSTATE_INT32(len, MAX7310State),
    161         VMSTATE_UINT8(level, MAX7310State),
    162         VMSTATE_UINT8(direction, MAX7310State),
    163         VMSTATE_UINT8(polarity, MAX7310State),
    164         VMSTATE_UINT8(status, MAX7310State),
    165         VMSTATE_UINT8(command, MAX7310State),
    166         VMSTATE_I2C_SLAVE(parent_obj, MAX7310State),
    167         VMSTATE_END_OF_LIST()
    168     }
    169 };
    170 
    171 static void max7310_gpio_set(void *opaque, int line, int level)
    172 {
    173     MAX7310State *s = (MAX7310State *) opaque;
    174     assert(line >= 0 && line < ARRAY_SIZE(s->handler));
    175 
    176     if (level)
    177         s->level |= s->direction & (1 << line);
    178     else
    179         s->level &= ~(s->direction & (1 << line));
    180 }
    181 
    182 /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
    183  * but also accepts sequences that are not SMBus so return an I2C device.  */
    184 static void max7310_realize(DeviceState *dev, Error **errp)
    185 {
    186     I2CSlave *i2c = I2C_SLAVE(dev);
    187     MAX7310State *s = MAX7310(dev);
    188 
    189     qdev_init_gpio_in(&i2c->qdev, max7310_gpio_set, 8);
    190     qdev_init_gpio_out(&i2c->qdev, s->handler, 8);
    191 }
    192 
    193 static void max7310_class_init(ObjectClass *klass, void *data)
    194 {
    195     DeviceClass *dc = DEVICE_CLASS(klass);
    196     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
    197 
    198     dc->realize = max7310_realize;
    199     k->event = max7310_event;
    200     k->recv = max7310_rx;
    201     k->send = max7310_tx;
    202     dc->reset = max7310_reset;
    203     dc->vmsd = &vmstate_max7310;
    204 }
    205 
    206 static const TypeInfo max7310_info = {
    207     .name          = TYPE_MAX7310,
    208     .parent        = TYPE_I2C_SLAVE,
    209     .instance_size = sizeof(MAX7310State),
    210     .class_init    = max7310_class_init,
    211 };
    212 
    213 static void max7310_register_types(void)
    214 {
    215     type_register_static(&max7310_info);
    216 }
    217 
    218 type_init(max7310_register_types)