[0510cfd8] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @brief Switch NXP PCA9548A Driver Implementation |
---|
| 5 | * |
---|
| 6 | * @ingroup I2CSWITCHNXPPCA9548A |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /* |
---|
| 10 | * Copyright (c) 2014 embedded brains GmbH. All rights reserved. |
---|
| 11 | * |
---|
| 12 | * embedded brains GmbH |
---|
| 13 | * Dornierstr. 4 |
---|
| 14 | * 82178 Puchheim |
---|
| 15 | * Germany |
---|
| 16 | * <rtems@embedded-brains.de> |
---|
| 17 | * |
---|
| 18 | * The license and distribution terms for this file may be |
---|
| 19 | * found in the file LICENSE in this distribution or at |
---|
| 20 | * http://www.rtems.org/license/LICENSE. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | #if HAVE_CONFIG_H |
---|
| 24 | #include "config.h" |
---|
| 25 | #endif |
---|
| 26 | |
---|
| 27 | #include <dev/i2c/switch-nxp-pca9548a.h> |
---|
| 28 | |
---|
| 29 | static int switch_nxp_pca9548a_do_get_control( |
---|
| 30 | i2c_dev *dev, |
---|
| 31 | uint8_t *val |
---|
| 32 | ) |
---|
| 33 | { |
---|
| 34 | i2c_msg msg = { |
---|
| 35 | .addr = dev->address, |
---|
| 36 | .flags = I2C_M_RD, |
---|
| 37 | .len = (uint16_t) sizeof(*val), |
---|
| 38 | .buf = val |
---|
| 39 | }; |
---|
| 40 | |
---|
| 41 | return i2c_bus_transfer(dev->bus, &msg, 1); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | static int switch_nxp_pca9548a_do_set_control( |
---|
| 45 | i2c_dev *dev, |
---|
| 46 | uint8_t val |
---|
| 47 | ) |
---|
| 48 | { |
---|
| 49 | i2c_msg msg = { |
---|
| 50 | .addr = dev->address, |
---|
| 51 | .flags = 0, |
---|
| 52 | .len = (uint16_t) sizeof(val), |
---|
| 53 | .buf = &val |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | return i2c_bus_transfer(dev->bus, &msg, 1); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | static int switch_nxp_pca9548a_ioctl( |
---|
| 60 | i2c_dev *dev, |
---|
| 61 | ioctl_command_t command, |
---|
| 62 | void *arg |
---|
| 63 | ) |
---|
| 64 | { |
---|
| 65 | uint8_t v8 = (uint8_t)(uintptr_t) arg; |
---|
| 66 | int err; |
---|
| 67 | |
---|
| 68 | switch (command) { |
---|
| 69 | case SWITCH_NXP_PCA9548A_GET_CONTROL: |
---|
| 70 | err = switch_nxp_pca9548a_do_get_control(dev, arg); |
---|
| 71 | break; |
---|
| 72 | case SWITCH_NXP_PCA9548A_SET_CONTROL: |
---|
| 73 | err = switch_nxp_pca9548a_do_set_control(dev, v8); |
---|
| 74 | break; |
---|
| 75 | default: |
---|
| 76 | err = -ENOTTY; |
---|
| 77 | break; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | return err; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | int i2c_dev_register_switch_nxp_pca9548a( |
---|
| 84 | const char *bus_path, |
---|
| 85 | const char *dev_path, |
---|
| 86 | uint16_t address |
---|
| 87 | ) |
---|
| 88 | { |
---|
| 89 | i2c_dev *dev; |
---|
| 90 | |
---|
| 91 | dev = i2c_dev_alloc_and_init(sizeof(*dev), bus_path, address); |
---|
| 92 | if (dev == NULL) { |
---|
| 93 | return -1; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | dev->ioctl = switch_nxp_pca9548a_ioctl; |
---|
| 97 | |
---|
| 98 | return i2c_dev_register(dev, dev_path); |
---|
| 99 | } |
---|