| 1 | /* |
| 2 | * PCA955X I2C LED blinker and I/O expanders |
| 3 | * |
| 4 | * https://www.nxp.com/docs/en/application-note/AN264.pdf |
| 5 | * https://www.nxp.com/docs/en/data-sheet/PCA9552.pdf |
| 6 | * https://www.nxp.com/docs/en/data-sheet/PCA9555.pdf |
| 7 | * https://www.nxp.com/docs/en/data-sheet/PCA9535_PCA9535C.pdf |
| 8 | * |
| 9 | * Copyright (c) 2017-2018, IBM Corporation. |
| 10 | * Copyright (c) 2020 Philippe Mathieu-Daudé |
| 11 | * |
| 12 | * This work is licensed under the terms of the GNU GPL, version 2 or |
| 13 | * later. See the COPYING file in the top-level directory. |
| 14 | */ |
| 15 | |
| 16 | #include "qemu/osdep.h" |
| 17 | #include "qemu/log.h" |
| 18 | #include "qemu/bitops.h" |
| 19 | #include "hw/core/qdev-properties.h" |
| 20 | #include "hw/i2c/i2c.h" |
| 21 | #include "hw/gpio/pca9552.h" |
| 22 | #include "hw/gpio/pca9552_regs.h" |
| 23 | #include "hw/core/irq.h" |
| 24 | #include "migration/vmstate.h" |
| 25 | #include "qapi/error.h" |
| 26 | #include "qapi/visitor.h" |
| 27 | #include "trace.h" |
| 28 | #include "qom/object.h" |
| 29 | |
| 30 | #define PCA955X_NR_REGS 10 |
| 31 | #define PCA955X_PIN_COUNT_MAX 16 |
| 32 | |
| 33 | OBJECT_DECLARE_TYPE(PCA955xState, PCA955xClass, PCA955X) |
| 34 | |
| 35 | struct PCA955xState { |
| 36 | /*< private >*/ |
| 37 | I2CSlave parent_obj; |
| 38 | /*< public >*/ |
| 39 | |
| 40 | uint8_t len; |
| 41 | uint8_t pointer; |
| 42 | |
| 43 | uint8_t regs[PCA955X_NR_REGS]; |
| 44 | qemu_irq gpio_out[PCA955X_PIN_COUNT_MAX]; |
| 45 | uint8_t ext_state[PCA955X_PIN_COUNT_MAX]; |
| 46 | char *description; /* For debugging purpose only */ |
| 47 | }; |
| 48 | |
| 49 | struct PCA955xClass { |
| 50 | /*< private >*/ |
| 51 | I2CSlaveClass parent_class; |
| 52 | /*< public >*/ |
| 53 | |
| 54 | uint8_t pin_count; |
| 55 | uint8_t max_reg; |
| 56 | bool has_led_support; |
| 57 | }; |
| 58 | |
| 59 | /* |
| 60 | * Note: The LED_ON and LED_OFF configuration values for the PCA955X |
| 61 | * chips are the reverse of the PCA953X family of chips. |
| 62 | */ |
| 63 | #define PCA9552_LED_ON 0x0 |
| 64 | #define PCA9552_LED_OFF 0x1 |
| 65 | #define PCA9552_LED_PWM0 0x2 |
| 66 | #define PCA9552_LED_PWM1 0x3 |
| 67 | #define PCA9552_PIN_LOW 0x0 |
| 68 | #define PCA9552_PIN_HIZ 0x1 |
| 69 | |
| 70 | static const char *led_state[] = {"on", "off", "pwm0", "pwm1"}; |
| 71 | static const char *pin_state[] = {"low", "high"}; |
| 72 | |
| 73 | static uint8_t pca955x_pin_get_config(PCA955xState *s, int pin) |
| 74 | { |
| 75 | uint8_t reg = PCA9552_LS0 + (pin / 4); |
| 76 | uint8_t shift = (pin % 4) << 1; |
| 77 | |
| 78 | return extract32(s->regs[reg], shift, 2); |
| 79 | } |
| 80 | |
| 81 | /* Return INPUT status (bit #N belongs to GPIO #N) */ |
| 82 | static uint16_t pca955x_pins_get_status(PCA955xState *s) |
| 83 | { |
| 84 | return (s->regs[PCA9552_INPUT1] << 8) | s->regs[PCA9552_INPUT0]; |
| 85 | } |
| 86 | |
| 87 | static void pca955x_display_pins_status(PCA955xState *s, |
| 88 | uint16_t previous_pins_status) |
| 89 | { |
| 90 | PCA955xClass *k = PCA955X_GET_CLASS(s); |
| 91 | uint16_t pins_status, pins_changed; |
| 92 | int i; |
| 93 | |
| 94 | pins_status = pca955x_pins_get_status(s); |
| 95 | pins_changed = previous_pins_status ^ pins_status; |
| 96 | if (!pins_changed) { |
| 97 | return; |
| 98 | } |
| 99 | if (trace_event_get_state_backends(TRACE_PCA955X_GPIO_STATUS)) { |
| 100 | char buf[PCA955X_PIN_COUNT_MAX + 1]; |
| 101 | |
| 102 | for (i = 0; i < k->pin_count; i++) { |
| 103 | if (extract32(pins_status, i, 1)) { |
| 104 | buf[i] = '*'; |
| 105 | } else { |
| 106 | buf[i] = '.'; |
| 107 | } |
| 108 | } |
| 109 | buf[i] = '\0'; |
| 110 | trace_pca955x_gpio_status(s->description, buf); |
| 111 | } |
| 112 | if (trace_event_get_state_backends(TRACE_PCA955X_GPIO_CHANGE)) { |
| 113 | for (i = 0; i < k->pin_count; i++) { |
| 114 | if (extract32(pins_changed, i, 1)) { |
| 115 | unsigned new_state = extract32(pins_status, i, 1); |
| 116 | |
| 117 | /* |
| 118 | * We display the state using the PCA logic ("active-high"). |
| 119 | * This is not the state of the LED, which signal might be |
| 120 | * wired "active-low" on the board. |
| 121 | */ |
| 122 | trace_pca955x_gpio_change(s->description, i, |
| 123 | !new_state, new_state); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static void pca955x_update_pin_input(PCA955xState *s) |
| 130 | { |
| 131 | PCA955xClass *k = PCA955X_GET_CLASS(s); |
| 132 | int i; |
| 133 | |
| 134 | for (i = 0; i < k->pin_count; i++) { |
| 135 | uint8_t input_reg = PCA9552_INPUT0 + (i / 8); |
| 136 | uint8_t bit_mask = 1 << (i % 8); |
| 137 | uint8_t old_value = s->regs[input_reg] & bit_mask; |
| 138 | uint8_t new_value; |
| 139 | |
| 140 | if (k->has_led_support) { |
| 141 | /* PCA9552: LED control behavior */ |
| 142 | uint8_t config = pca955x_pin_get_config(s, i); |
| 143 | |
| 144 | switch (config) { |
| 145 | case PCA9552_LED_ON: |
| 146 | /* Pin is set to 0V to turn on LED */ |
| 147 | s->regs[input_reg] &= ~bit_mask; |
| 148 | break; |
| 149 | case PCA9552_LED_OFF: |
| 150 | /* |
| 151 | * Pin is set to Hi-Z to turn off LED and |
| 152 | * pullup sets it to a logical 1 unless |
| 153 | * external device drives it low. |
| 154 | */ |
| 155 | if (s->ext_state[i] == PCA9552_PIN_LOW) { |
| 156 | s->regs[input_reg] &= ~bit_mask; |
| 157 | } else { |
| 158 | s->regs[input_reg] |= bit_mask; |
| 159 | } |
| 160 | break; |
| 161 | case PCA9552_LED_PWM0: |
| 162 | case PCA9552_LED_PWM1: |
| 163 | /* TODO */ |
| 164 | default: |
| 165 | break; |
| 166 | } |
| 167 | } else { |
| 168 | /* PCA9535: Simple GPIO behavior */ |
| 169 | uint8_t config_reg = PCA9535_CONFIG0 + (i / 8); |
| 170 | uint8_t output_reg = PCA9535_OUTPUT0 + (i / 8); |
| 171 | |
| 172 | /* |
| 173 | * The input register holds the raw pin logic level; the |
| 174 | * polarity inversion register is only applied when the input |
| 175 | * port is read (see pca955x_read()). |
| 176 | */ |
| 177 | if (s->regs[config_reg] & bit_mask) { |
| 178 | /* Input mode - reflect external state */ |
| 179 | if (s->ext_state[i] == PCA9552_PIN_LOW) { |
| 180 | s->regs[input_reg] &= ~bit_mask; |
| 181 | } else { |
| 182 | s->regs[input_reg] |= bit_mask; |
| 183 | } |
| 184 | } else { |
| 185 | /* Output mode - reflect output register value */ |
| 186 | s->regs[input_reg] = (s->regs[input_reg] & ~bit_mask) | |
| 187 | (s->regs[output_reg] & bit_mask); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* update irq state only if pin state changed */ |
| 192 | new_value = s->regs[input_reg] & bit_mask; |
| 193 | if (new_value != old_value) { |
| 194 | qemu_set_irq(s->gpio_out[i], !!new_value); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static uint8_t pca955x_read(PCA955xState *s, uint8_t reg) |
| 200 | { |
| 201 | PCA955xClass *k = PCA955X_GET_CLASS(s); |
| 202 | |
| 203 | if (reg > k->max_reg) { |
| 204 | qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected read to register %d\n", |
| 205 | __func__, reg); |
| 206 | return 0xFF; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * On the GPIO variants, reading an input port returns the raw pin |
| 211 | * levels XORed with the polarity inversion register, as specified by |
| 212 | * the datasheet. |
| 213 | */ |
| 214 | if (!k->has_led_support && |
| 215 | (reg == PCA9535_INPUT0 || reg == PCA9535_INPUT1)) { |
| 216 | uint8_t polarity_reg = PCA9535_POLARITY0 + (reg - PCA9535_INPUT0); |
| 217 | |
| 218 | return s->regs[reg] ^ s->regs[polarity_reg]; |
| 219 | } |
| 220 | |
| 221 | return s->regs[reg]; |
| 222 | } |
| 223 | |
| 224 | static void pca955x_write(PCA955xState *s, uint8_t reg, uint8_t data) |
| 225 | { |
| 226 | PCA955xClass *k = PCA955X_GET_CLASS(s); |
| 227 | uint16_t pins_status; |
| 228 | |
| 229 | if (reg > k->max_reg) { |
| 230 | qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected write to register %d\n", |
| 231 | __func__, reg); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | /* Handle read-only registers */ |
| 236 | if (reg == PCA9552_INPUT0 || reg == PCA9552_INPUT1) { |
| 237 | qemu_log_mask(LOG_GUEST_ERROR, |
| 238 | "%s: unexpected write to read-only register %d\n", |
| 239 | __func__, reg); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | pins_status = pca955x_pins_get_status(s); |
| 244 | s->regs[reg] = data; |
| 245 | |
| 246 | /* Update GPIO state if this register affects outputs */ |
| 247 | if (k->has_led_support) { |
| 248 | /* PCA9552: Update on LED selector register writes */ |
| 249 | if (reg >= PCA9552_LS0 && reg <= PCA9552_LS3) { |
| 250 | pca955x_update_pin_input(s); |
| 251 | pca955x_display_pins_status(s, pins_status); |
| 252 | } |
| 253 | } else { |
| 254 | /* PCA9535: Update on OUTPUT, POLARITY, or CONFIG register writes */ |
| 255 | if (reg >= PCA9535_OUTPUT0 && reg <= PCA9535_CONFIG1) { |
| 256 | pca955x_update_pin_input(s); |
| 257 | pca955x_display_pins_status(s, pins_status); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Advance the command pointer after each byte sent to or received from the |
| 264 | * device. |
| 265 | * |
| 266 | * The LED variant auto-increments only when the AI bit (bit 4) is set in the |
| 267 | * command byte, rolling over to 0 once the maximum register address is |
| 268 | * reached. |
| 269 | * |
| 270 | * The GPIO variants auto-increment on every access, toggling bit 0 so the |
| 271 | * pointer stays within the addressed register pair |
| 272 | * (input/output/polarity/config), as specified by their datasheet. |
| 273 | */ |
| 274 | static void pca955x_autoinc(PCA955xState *s) |
| 275 | { |
| 276 | PCA955xClass *k = PCA955X_GET_CLASS(s); |
| 277 | |
| 278 | if (!k->has_led_support) { |
| 279 | s->pointer ^= 0x1; |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | if (s->pointer != 0xFF && s->pointer & PCA9552_AUTOINC) { |
| 284 | uint8_t reg = s->pointer & 0xf; |
| 285 | |
| 286 | reg = (reg + 1) % (k->max_reg + 1); |
| 287 | s->pointer = reg | PCA9552_AUTOINC; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * The LED variant addresses its registers with a 4-bit command field, while |
| 293 | * the GPIO variants only decode 3 bits (the command wraps into the 8-register |
| 294 | * window). |
| 295 | */ |
| 296 | static inline uint8_t pca955x_cmd_reg(PCA955xState *s) |
| 297 | { |
| 298 | PCA955xClass *k = PCA955X_GET_CLASS(s); |
| 299 | |
| 300 | return s->pointer & (k->has_led_support ? 0xf : 0x7); |
| 301 | } |
| 302 | |
| 303 | static uint8_t pca955x_recv(I2CSlave *i2c) |
| 304 | { |
| 305 | PCA955xState *s = PCA955X(i2c); |
| 306 | PCA955xClass *k = PCA955X_GET_CLASS(s); |
| 307 | uint8_t ret; |
| 308 | |
| 309 | ret = pca955x_read(s, pca955x_cmd_reg(s)); |
| 310 | |
| 311 | /* |
| 312 | * From the Specs: |
| 313 | * |
| 314 | * Important Note: When a Read sequence is initiated and the |
| 315 | * AI bit is set to Logic Level 1, the Read Sequence MUST |
| 316 | * start by a register different from 0. |
| 317 | * |
| 318 | * I don't know what should be done in this case, so throw an |
| 319 | * error. |
| 320 | */ |
| 321 | if (k->has_led_support && s->pointer == PCA9552_AUTOINC) { |
| 322 | qemu_log_mask(LOG_GUEST_ERROR, |
| 323 | "%s: Autoincrement read starting with register 0\n", |
| 324 | __func__); |
| 325 | } |
| 326 | |
| 327 | pca955x_autoinc(s); |
| 328 | |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | static int pca955x_send(I2CSlave *i2c, uint8_t data) |
| 333 | { |
| 334 | PCA955xState *s = PCA955X(i2c); |
| 335 | |
| 336 | /* First byte sent by is the register address */ |
| 337 | if (s->len == 0) { |
| 338 | s->pointer = data; |
| 339 | s->len++; |
| 340 | } else { |
| 341 | pca955x_write(s, pca955x_cmd_reg(s), data); |
| 342 | |
| 343 | pca955x_autoinc(s); |
| 344 | } |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int pca955x_event(I2CSlave *i2c, enum i2c_event event) |
| 350 | { |
| 351 | PCA955xState *s = PCA955X(i2c); |
| 352 | |
| 353 | s->len = 0; |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static void pca955x_get_led(Object *obj, Visitor *v, const char *name, |
| 358 | void *opaque, Error **errp) |
| 359 | { |
| 360 | PCA955xClass *k = PCA955X_GET_CLASS(obj); |
| 361 | PCA955xState *s = PCA955X(obj); |
| 362 | int led, rc, reg; |
| 363 | uint8_t state; |
| 364 | |
| 365 | rc = sscanf(name, "led%2d", &led); |
| 366 | if (rc != 1) { |
| 367 | error_setg(errp, "%s: error reading %s", __func__, name); |
| 368 | return; |
| 369 | } |
| 370 | if (led < 0 || led >= k->pin_count) { |
| 371 | error_setg(errp, "%s: invalid led %s", __func__, name); |
| 372 | return; |
| 373 | } |
| 374 | /* |
| 375 | * Get the LSx register as the qom interface should expose the device |
| 376 | * state, not the modeled 'input line' behaviour which would come from |
| 377 | * reading the INPUTx reg |
| 378 | */ |
| 379 | reg = PCA9552_LS0 + led / 4; |
| 380 | state = (pca955x_read(s, reg) >> ((led % 4) * 2)) & 0x3; |
| 381 | visit_type_str(v, name, (char **)&led_state[state], errp); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Return an LED selector register value based on an existing one, with |
| 386 | * the appropriate 2-bit state value set for the given LED number (0-3). |
| 387 | */ |
| 388 | static inline uint8_t pca955x_ledsel(uint8_t oldval, int led_num, int state) |
| 389 | { |
| 390 | return (oldval & (~(0x3 << (led_num << 1)))) | |
| 391 | ((state & 0x3) << (led_num << 1)); |
| 392 | } |
| 393 | |
| 394 | static void pca955x_set_led(Object *obj, Visitor *v, const char *name, |
| 395 | void *opaque, Error **errp) |
| 396 | { |
| 397 | PCA955xClass *k = PCA955X_GET_CLASS(obj); |
| 398 | PCA955xState *s = PCA955X(obj); |
| 399 | int led, rc, reg, val; |
| 400 | uint8_t state; |
| 401 | g_autofree char *state_str = NULL; |
| 402 | |
| 403 | if (!visit_type_str(v, name, &state_str, errp)) { |
| 404 | return; |
| 405 | } |
| 406 | rc = sscanf(name, "led%2d", &led); |
| 407 | if (rc != 1) { |
| 408 | error_setg(errp, "%s: error reading %s", __func__, name); |
| 409 | return; |
| 410 | } |
| 411 | if (led < 0 || led >= k->pin_count) { |
| 412 | error_setg(errp, "%s: invalid led %s", __func__, name); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | for (state = 0; state < ARRAY_SIZE(led_state); state++) { |
| 417 | if (!strcmp(state_str, led_state[state])) { |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | if (state >= ARRAY_SIZE(led_state)) { |
| 422 | error_setg(errp, "%s invalid led state %s", __func__, state_str); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | reg = PCA9552_LS0 + led / 4; |
| 427 | val = pca955x_read(s, reg); |
| 428 | val = pca955x_ledsel(val, led % 4, state); |
| 429 | pca955x_write(s, reg, val); |
| 430 | } |
| 431 | |
| 432 | static void pca955x_set_ext_state(PCA955xState *s, int pin, int level); |
| 433 | |
| 434 | static void pca955x_get_pin(Object *obj, Visitor *v, const char *name, |
| 435 | void *opaque, Error **errp) |
| 436 | { |
| 437 | PCA955xClass *k = PCA955X_GET_CLASS(obj); |
| 438 | PCA955xState *s = PCA955X(obj); |
| 439 | int pin, rc; |
| 440 | uint8_t input_reg, state; |
| 441 | |
| 442 | rc = sscanf(name, "pin%2d", &pin); |
| 443 | if (rc != 1) { |
| 444 | error_setg(errp, "%s: error reading %s", __func__, name); |
| 445 | return; |
| 446 | } |
| 447 | if (pin < 0 || pin >= k->pin_count) { |
| 448 | error_setg(errp, "%s invalid pin %s", __func__, name); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Report the raw pin logic level; polarity inversion is a read-time |
| 454 | * transform applied to the INPUT register, not to the pin state itself. |
| 455 | */ |
| 456 | input_reg = PCA9535_INPUT0 + (pin / 8); |
| 457 | state = (s->regs[input_reg] >> (pin % 8)) & 0x1; |
| 458 | visit_type_str(v, name, (char **)&pin_state[state], errp); |
| 459 | } |
| 460 | |
| 461 | static void pca955x_set_pin(Object *obj, Visitor *v, const char *name, |
| 462 | void *opaque, Error **errp) |
| 463 | { |
| 464 | PCA955xClass *k = PCA955X_GET_CLASS(obj); |
| 465 | PCA955xState *s = PCA955X(obj); |
| 466 | int pin, rc; |
| 467 | uint8_t state, config_reg; |
| 468 | g_autofree char *state_str = NULL; |
| 469 | |
| 470 | if (!visit_type_str(v, name, &state_str, errp)) { |
| 471 | return; |
| 472 | } |
| 473 | rc = sscanf(name, "pin%2d", &pin); |
| 474 | if (rc != 1) { |
| 475 | error_setg(errp, "%s: error reading %s", __func__, name); |
| 476 | return; |
| 477 | } |
| 478 | if (pin < 0 || pin >= k->pin_count) { |
| 479 | error_setg(errp, "%s invalid pin %s", __func__, name); |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | for (state = 0; state < ARRAY_SIZE(pin_state); state++) { |
| 484 | if (!strcmp(state_str, pin_state[state])) { |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | if (state >= ARRAY_SIZE(pin_state)) { |
| 489 | error_setg(errp, "%s invalid pin state %s", __func__, state_str); |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | /* Only input-configured pins can be driven by an external device. */ |
| 494 | config_reg = PCA9535_CONFIG0 + (pin / 8); |
| 495 | if (!((s->regs[config_reg] >> (pin % 8)) & 0x1)) { |
| 496 | qemu_log_mask(LOG_UNIMP, |
| 497 | "%s: pin %d is configured as output, ignoring set\n", |
| 498 | s->description, pin); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | pca955x_set_ext_state(s, pin, state != PCA9552_PIN_LOW); |
| 503 | } |
| 504 | |
| 505 | static const VMStateDescription pca9552_vmstate = { |
| 506 | .name = "PCA9552", |
| 507 | .version_id = 0, |
| 508 | .minimum_version_id = 0, |
| 509 | .fields = (const VMStateField[]) { |
| 510 | VMSTATE_UINT8(len, PCA955xState), |
| 511 | VMSTATE_UINT8(pointer, PCA955xState), |
| 512 | VMSTATE_UINT8_ARRAY(regs, PCA955xState, PCA955X_NR_REGS), |
| 513 | VMSTATE_UINT8_ARRAY(ext_state, PCA955xState, PCA955X_PIN_COUNT_MAX), |
| 514 | VMSTATE_I2C_SLAVE(parent_obj, PCA955xState), |
| 515 | VMSTATE_END_OF_LIST() |
| 516 | } |
| 517 | }; |
| 518 | |
| 519 | static void pca9552_reset_hold(Object *obj, ResetType type) |
| 520 | { |
| 521 | PCA955xState *s = PCA955X(obj); |
| 522 | |
| 523 | s->regs[PCA9552_PSC0] = 0xFF; |
| 524 | s->regs[PCA9552_PWM0] = 0x80; |
| 525 | s->regs[PCA9552_PSC1] = 0xFF; |
| 526 | s->regs[PCA9552_PWM1] = 0x80; |
| 527 | s->regs[PCA9552_LS0] = 0x55; /* all OFF */ |
| 528 | s->regs[PCA9552_LS1] = 0x55; |
| 529 | s->regs[PCA9552_LS2] = 0x55; |
| 530 | s->regs[PCA9552_LS3] = 0x55; |
| 531 | |
| 532 | memset(s->ext_state, PCA9552_PIN_HIZ, PCA955X_PIN_COUNT_MAX); |
| 533 | pca955x_update_pin_input(s); |
| 534 | |
| 535 | s->pointer = 0xFF; |
| 536 | s->len = 0; |
| 537 | } |
| 538 | |
| 539 | static void pca9535_reset_hold(Object *obj, ResetType type) |
| 540 | { |
| 541 | PCA955xState *s = PCA955X(obj); |
| 542 | |
| 543 | s->regs[PCA9535_INPUT0] = 0xFF; /* All inputs high (pull-ups) */ |
| 544 | s->regs[PCA9535_INPUT1] = 0xFF; /* All inputs high (pull-ups) */ |
| 545 | s->regs[PCA9535_OUTPUT0] = 0xFF; /* All outputs high */ |
| 546 | s->regs[PCA9535_OUTPUT1] = 0xFF; /* All outputs high */ |
| 547 | s->regs[PCA9535_POLARITY0] = 0x00; /* No polarity inversion */ |
| 548 | s->regs[PCA9535_POLARITY1] = 0x00; /* No polarity inversion */ |
| 549 | s->regs[PCA9535_CONFIG0] = 0xFF; /* All pins as inputs */ |
| 550 | s->regs[PCA9535_CONFIG1] = 0xFF; /* All pins as inputs */ |
| 551 | |
| 552 | memset(s->ext_state, PCA9552_PIN_HIZ, PCA955X_PIN_COUNT_MAX); |
| 553 | pca955x_update_pin_input(s); |
| 554 | |
| 555 | s->pointer = 0xFF; |
| 556 | s->len = 0; |
| 557 | } |
| 558 | |
| 559 | static void pca955x_initfn(Object *obj) |
| 560 | { |
| 561 | PCA955xClass *k = PCA955X_GET_CLASS(obj); |
| 562 | |
| 563 | assert(k->pin_count <= PCA955X_PIN_COUNT_MAX); |
| 564 | for (int ix = 0; ix < k->pin_count; ix++) { |
| 565 | char *name; |
| 566 | |
| 567 | if (k->has_led_support) { |
| 568 | /* LED variant: expose the LED selector state as led%d. */ |
| 569 | name = g_strdup_printf("led%d", ix); |
| 570 | object_property_add(obj, name, "bool", |
| 571 | pca955x_get_led, pca955x_set_led, NULL, NULL); |
| 572 | } else { |
| 573 | /* GPIO variant: expose the pin logic level as pin%d. */ |
| 574 | name = g_strdup_printf("pin%d", ix); |
| 575 | object_property_add(obj, name, "str", |
| 576 | pca955x_get_pin, pca955x_set_pin, NULL, NULL); |
| 577 | } |
| 578 | g_free(name); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | static void pca955x_set_ext_state(PCA955xState *s, int pin, int level) |
| 583 | { |
| 584 | if (s->ext_state[pin] != level) { |
| 585 | uint16_t pins_status = pca955x_pins_get_status(s); |
| 586 | s->ext_state[pin] = level; |
| 587 | pca955x_update_pin_input(s); |
| 588 | pca955x_display_pins_status(s, pins_status); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | static void pca955x_gpio_in_handler(void *opaque, int pin, int level) |
| 593 | { |
| 594 | |
| 595 | PCA955xState *s = PCA955X(opaque); |
| 596 | PCA955xClass *k = PCA955X_GET_CLASS(s); |
| 597 | |
| 598 | assert((pin >= 0) && (pin < k->pin_count)); |
| 599 | pca955x_set_ext_state(s, pin, level); |
| 600 | } |
| 601 | |
| 602 | static void pca955x_realize(DeviceState *dev, Error **errp) |
| 603 | { |
| 604 | PCA955xClass *k = PCA955X_GET_CLASS(dev); |
| 605 | PCA955xState *s = PCA955X(dev); |
| 606 | |
| 607 | if (!s->description) { |
| 608 | s->description = g_strdup(object_get_typename(OBJECT(dev))); |
| 609 | } |
| 610 | |
| 611 | qdev_init_gpio_out(dev, s->gpio_out, k->pin_count); |
| 612 | qdev_init_gpio_in(dev, pca955x_gpio_in_handler, k->pin_count); |
| 613 | } |
| 614 | |
| 615 | static const Property pca955x_properties[] = { |
| 616 | DEFINE_PROP_STRING("description", PCA955xState, description), |
| 617 | }; |
| 618 | |
| 619 | static void pca955x_class_init(ObjectClass *klass, const void *data) |
| 620 | { |
| 621 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 622 | I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); |
| 623 | |
| 624 | k->event = pca955x_event; |
| 625 | k->recv = pca955x_recv; |
| 626 | k->send = pca955x_send; |
| 627 | dc->realize = pca955x_realize; |
| 628 | device_class_set_props(dc, pca955x_properties); |
| 629 | } |
| 630 | |
| 631 | static void pca9552_class_init(ObjectClass *oc, const void *data) |
| 632 | { |
| 633 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 634 | ResettableClass *rc = RESETTABLE_CLASS(oc); |
| 635 | PCA955xClass *pc = PCA955X_CLASS(oc); |
| 636 | |
| 637 | rc->phases.hold = pca9552_reset_hold; |
| 638 | dc->vmsd = &pca9552_vmstate; |
| 639 | pc->max_reg = PCA9552_LS3; |
| 640 | pc->pin_count = 16; |
| 641 | pc->has_led_support = true; |
| 642 | } |
| 643 | |
| 644 | static void pca95x5_class_init(ObjectClass *oc, const void *data) |
| 645 | { |
| 646 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 647 | ResettableClass *rc = RESETTABLE_CLASS(oc); |
| 648 | PCA955xClass *pc = PCA955X_CLASS(oc); |
| 649 | |
| 650 | rc->phases.hold = pca9535_reset_hold; |
| 651 | dc->vmsd = &pca9552_vmstate; |
| 652 | pc->max_reg = PCA9535_CONFIG1; |
| 653 | pc->pin_count = 16; |
| 654 | pc->has_led_support = false; |
| 655 | } |
| 656 | |
| 657 | static const TypeInfo pca955x_types[] = { |
| 658 | { |
| 659 | .name = TYPE_PCA955X, |
| 660 | .parent = TYPE_I2C_SLAVE, |
| 661 | .instance_init = pca955x_initfn, |
| 662 | .instance_size = sizeof(PCA955xState), |
| 663 | .class_init = pca955x_class_init, |
| 664 | .class_size = sizeof(PCA955xClass), |
| 665 | .abstract = true, |
| 666 | }, |
| 667 | { |
| 668 | .name = TYPE_PCA9552, |
| 669 | .parent = TYPE_PCA955X, |
| 670 | .class_init = pca9552_class_init, |
| 671 | }, |
| 672 | { |
| 673 | .name = TYPE_PCA9535, |
| 674 | .parent = TYPE_PCA955X, |
| 675 | .class_init = pca95x5_class_init, |
| 676 | }, |
| 677 | { |
| 678 | .name = TYPE_PCA9555, |
| 679 | .parent = TYPE_PCA955X, |
| 680 | .class_init = pca95x5_class_init, |
| 681 | } |
| 682 | }; |
| 683 | |
| 684 | DEFINE_TYPES(pca955x_types) |