bcmi2c.c (3648B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: bcmi2c.c 8 * Purpose: BCM I2C API 9 */ 10 11 #ifdef INCLUDE_I2C 12 13 #include <sal/types.h> 14 15 #include <soc/debug.h> 16 #include <soc/i2c.h> 17 18 #include <bcm/bcmi2c.h> 19 #include <bcm/error.h> 20 21 #include <bcm_int/esw_dispatch.h> 22 /* 23 * Function: 24 * bcm_esw_i2c_open 25 * Purpose: 26 * Open device, return valid file descriptor or -1 on error. 27 * Parameters: 28 * unit - StrataSwitch device number or I2C bus number 29 * devname - I2C device name string 30 * flags - arguments to pass to attach, default value should be zero 31 * speed - I2C bus speed, if non-zero, this speed is configured, normally 32 * this argument should be zero unless a speed is desired. 33 * Returns: 34 * Device identifier for all I2C operations 35 * Notes: 36 * This routine should be called before attempting to communicate 37 * with an I2C device which has a registered driver. 38 * A valid driver with this device name must be installed in the system. 39 */ 40 41 int 42 bcm_esw_i2c_open(int unit, char *devname, uint32 flags, int speed) 43 { 44 return soc_i2c_devopen(unit, devname, flags, speed); 45 } 46 47 /* 48 * Function: 49 * bcm_esw_i2c_write 50 * Purpose: 51 * Write to a device 52 * Parameters: 53 * unit - StrataSwitch device number or I2C bus number 54 * fd - I2C device ID 55 * addr - device register or memory address 56 * data - data byte buffer 57 * nbytes - number of bytes of data 58 * Returns: 59 * Number of bytes written on success 60 * BCM_E_XXX on failure. 61 * Notes: 62 * This routine requires a driver. 63 */ 64 65 int 66 bcm_esw_i2c_write(int unit, int fd, uint32 addr, uint8 *data, uint32 nbytes) 67 { 68 if ((fd < 0) || (fd >= soc_i2c_device_count(unit))) { 69 return BCM_E_PARAM; 70 } 71 72 if (soc_i2c_device(unit, fd)->driver == NULL) { 73 return BCM_E_PARAM; 74 } 75 76 return soc_i2c_device(unit, fd)->driver->write(unit, fd, 77 addr, data, nbytes); 78 } 79 80 /* 81 * Function: 82 * bcm_esw_i2c_read 83 * Purpose: 84 * Read from a device 85 * Parameters: 86 * unit - StrataSwitch device number or I2C bus number 87 * fd - I2C device ID 88 * addr - device register or memory address 89 * data - data byte buffer to read into 90 * nbytes - number of bytes of data, updated on success. 91 * Returns: 92 * On success, number of bytes read; nbytes updated with number 93 * of bytes read from device; BCM_E_XXX on failure. 94 * Notes: 95 * This routine requires a driver. 96 */ 97 98 int 99 bcm_esw_i2c_read(int unit, int fd, uint32 addr, uint8 *data, uint32 * nbytes) 100 { 101 if ((fd < 0) || (fd >= soc_i2c_device_count(unit))) { 102 return BCM_E_PARAM; 103 } 104 105 if (soc_i2c_device(unit, fd)->driver == NULL) { 106 return BCM_E_PARAM; 107 } 108 109 return soc_i2c_device(unit, fd)->driver->read(unit, fd, 110 addr, data, nbytes); 111 } 112 113 /* 114 * Function: 115 * bcm_esw_i2c_ioctl 116 * Purpose: 117 * Device specific I/O control 118 * Parameters: 119 * unit - StrataSwitch device number or I2C bus number 120 * fd - I2C device ID 121 * opcode - device command code (device-specific). 122 * data - data byte buffer for command 123 * nbytes - number of bytes of data 124 * Returns: 125 * On success, application specific value greater than zero, 126 * BCM_E_XXX otherwise. 127 * Notes: 128 * This routine requires a driver. 129 */ 130 131 int 132 bcm_esw_i2c_ioctl(int unit, int fd, int opcode, void *data, int len) 133 { 134 if ((fd < 0) || (fd >= soc_i2c_device_count(unit))) { 135 return BCM_E_PARAM; 136 } 137 138 if (soc_i2c_device(unit, fd)->driver == NULL) { 139 return BCM_E_PARAM; 140 } 141 142 return soc_i2c_device(unit, fd)->driver->ioctl(unit, fd, opcode, 143 data, len); 144 } 145 146 #endif /* INCLUDE_I2C */