1*48c6a6b6SBharat Gooty /* 2*48c6a6b6SBharat Gooty * Copyright (c) 2016 - 2021, Broadcom 3*48c6a6b6SBharat Gooty * 4*48c6a6b6SBharat Gooty * SPDX-License-Identifier: BSD-3-Clause 5*48c6a6b6SBharat Gooty */ 6*48c6a6b6SBharat Gooty 7*48c6a6b6SBharat Gooty #ifndef I2C_H 8*48c6a6b6SBharat Gooty #define I2C_H 9*48c6a6b6SBharat Gooty 10*48c6a6b6SBharat Gooty #include <stdint.h> 11*48c6a6b6SBharat Gooty 12*48c6a6b6SBharat Gooty #define I2C_SPEED_100KHz 100000 13*48c6a6b6SBharat Gooty #define I2C_SPEED_400KHz 400000 14*48c6a6b6SBharat Gooty #define I2C_SPEED_DEFAULT I2C_SPEED_100KHz 15*48c6a6b6SBharat Gooty 16*48c6a6b6SBharat Gooty /* 17*48c6a6b6SBharat Gooty * Function Name: i2c_probe 18*48c6a6b6SBharat Gooty * 19*48c6a6b6SBharat Gooty * Description: 20*48c6a6b6SBharat Gooty * This function probes the I2C bus for the existence of the specified 21*48c6a6b6SBharat Gooty * device. 22*48c6a6b6SBharat Gooty * 23*48c6a6b6SBharat Gooty * Parameters: 24*48c6a6b6SBharat Gooty * bus_id - I2C bus ID 25*48c6a6b6SBharat Gooty * devaddr - Device Address 26*48c6a6b6SBharat Gooty * 27*48c6a6b6SBharat Gooty * Return: 28*48c6a6b6SBharat Gooty * 0 on success, or -1 on failure. 29*48c6a6b6SBharat Gooty */ 30*48c6a6b6SBharat Gooty int i2c_probe(uint32_t bus_id, uint8_t devaddr); 31*48c6a6b6SBharat Gooty 32*48c6a6b6SBharat Gooty /* 33*48c6a6b6SBharat Gooty * Function Name: i2c_init 34*48c6a6b6SBharat Gooty * 35*48c6a6b6SBharat Gooty * Description: 36*48c6a6b6SBharat Gooty * This function initializes the SMBUS. 37*48c6a6b6SBharat Gooty * 38*48c6a6b6SBharat Gooty * Parameters: 39*48c6a6b6SBharat Gooty * bus_id - I2C bus ID 40*48c6a6b6SBharat Gooty * speed - I2C bus speed in Hz 41*48c6a6b6SBharat Gooty * 42*48c6a6b6SBharat Gooty * Return: 43*48c6a6b6SBharat Gooty * 0 on success, or -1 on failure. 44*48c6a6b6SBharat Gooty */ 45*48c6a6b6SBharat Gooty int i2c_init(uint32_t bus_id, int speed); 46*48c6a6b6SBharat Gooty 47*48c6a6b6SBharat Gooty /* 48*48c6a6b6SBharat Gooty * Function Name: i2c_set_bus_speed 49*48c6a6b6SBharat Gooty * 50*48c6a6b6SBharat Gooty * Description: 51*48c6a6b6SBharat Gooty * This function configures the SMBUS speed 52*48c6a6b6SBharat Gooty * 53*48c6a6b6SBharat Gooty * Parameters: 54*48c6a6b6SBharat Gooty * bus_id - I2C bus ID 55*48c6a6b6SBharat Gooty * speed - I2C bus speed in Hz 56*48c6a6b6SBharat Gooty * 57*48c6a6b6SBharat Gooty * Return: 58*48c6a6b6SBharat Gooty * 0 on success, or -1 on failure. 59*48c6a6b6SBharat Gooty */ 60*48c6a6b6SBharat Gooty int i2c_set_bus_speed(uint32_t bus_id, uint32_t speed); 61*48c6a6b6SBharat Gooty 62*48c6a6b6SBharat Gooty /* 63*48c6a6b6SBharat Gooty * Function Name: i2c_get_bus_speed 64*48c6a6b6SBharat Gooty * 65*48c6a6b6SBharat Gooty * Description: 66*48c6a6b6SBharat Gooty * This function returns the SMBUS speed. 67*48c6a6b6SBharat Gooty * 68*48c6a6b6SBharat Gooty * Parameters: 69*48c6a6b6SBharat Gooty * bus_id - I2C bus ID 70*48c6a6b6SBharat Gooty * 71*48c6a6b6SBharat Gooty * Return: 72*48c6a6b6SBharat Gooty * Bus speed in Hz, 0 on failure 73*48c6a6b6SBharat Gooty */ 74*48c6a6b6SBharat Gooty uint32_t i2c_get_bus_speed(uint32_t bus_id); 75*48c6a6b6SBharat Gooty 76*48c6a6b6SBharat Gooty /* 77*48c6a6b6SBharat Gooty * Function Name: i2c_recv_byte 78*48c6a6b6SBharat Gooty * 79*48c6a6b6SBharat Gooty * Description: 80*48c6a6b6SBharat Gooty * This function reads I2C data from a device without specifying 81*48c6a6b6SBharat Gooty * a command regsiter. 82*48c6a6b6SBharat Gooty * 83*48c6a6b6SBharat Gooty * Parameters: 84*48c6a6b6SBharat Gooty * bus_id - I2C bus ID 85*48c6a6b6SBharat Gooty * devaddr - Device Address 86*48c6a6b6SBharat Gooty * value - Data Read 87*48c6a6b6SBharat Gooty * 88*48c6a6b6SBharat Gooty * Return: 89*48c6a6b6SBharat Gooty * 0 on success, or -1 on failure. 90*48c6a6b6SBharat Gooty */ 91*48c6a6b6SBharat Gooty int i2c_recv_byte(uint32_t bus_id, uint8_t devaddr, uint8_t *value); 92*48c6a6b6SBharat Gooty 93*48c6a6b6SBharat Gooty /* 94*48c6a6b6SBharat Gooty * Function Name: i2c_send_byte 95*48c6a6b6SBharat Gooty * 96*48c6a6b6SBharat Gooty * Description: 97*48c6a6b6SBharat Gooty * This function send I2C data to a device without specifying 98*48c6a6b6SBharat Gooty * a command regsiter. 99*48c6a6b6SBharat Gooty * 100*48c6a6b6SBharat Gooty * Parameters: 101*48c6a6b6SBharat Gooty * bus_id - I2C bus ID 102*48c6a6b6SBharat Gooty * devaddr - Device Address 103*48c6a6b6SBharat Gooty * value - Data Send 104*48c6a6b6SBharat Gooty * 105*48c6a6b6SBharat Gooty * Return: 106*48c6a6b6SBharat Gooty * 0 on success, or -1 on failure. 107*48c6a6b6SBharat Gooty */ 108*48c6a6b6SBharat Gooty int i2c_send_byte(uint32_t bus_id, uint8_t devaddr, uint8_t value); 109*48c6a6b6SBharat Gooty 110*48c6a6b6SBharat Gooty /* 111*48c6a6b6SBharat Gooty * Function Name: i2c_read 112*48c6a6b6SBharat Gooty * 113*48c6a6b6SBharat Gooty * Description: 114*48c6a6b6SBharat Gooty * This function reads I2C data from a device with a designated 115*48c6a6b6SBharat Gooty * command register 116*48c6a6b6SBharat Gooty * 117*48c6a6b6SBharat Gooty * Parameters: 118*48c6a6b6SBharat Gooty * bus_id - I2C bus ID 119*48c6a6b6SBharat Gooty * devaddr - Device Address 120*48c6a6b6SBharat Gooty * addr - Register Offset 121*48c6a6b6SBharat Gooty * alen - Address Length, 1 for byte, 2 for word (not supported) 122*48c6a6b6SBharat Gooty * buffer - Data Buffer 123*48c6a6b6SBharat Gooty * len - Data Length in bytes 124*48c6a6b6SBharat Gooty * 125*48c6a6b6SBharat Gooty * Return: 126*48c6a6b6SBharat Gooty * 0 on success, or -1 on failure. 127*48c6a6b6SBharat Gooty */ 128*48c6a6b6SBharat Gooty int i2c_read(uint32_t bus_id, 129*48c6a6b6SBharat Gooty uint8_t devaddr, 130*48c6a6b6SBharat Gooty uint32_t addr, 131*48c6a6b6SBharat Gooty int alen, 132*48c6a6b6SBharat Gooty uint8_t *buffer, 133*48c6a6b6SBharat Gooty int len); 134*48c6a6b6SBharat Gooty 135*48c6a6b6SBharat Gooty /* 136*48c6a6b6SBharat Gooty * Function Name: i2c_write 137*48c6a6b6SBharat Gooty * 138*48c6a6b6SBharat Gooty * Description: 139*48c6a6b6SBharat Gooty * This function write I2C data to a device with a designated 140*48c6a6b6SBharat Gooty * command register 141*48c6a6b6SBharat Gooty * 142*48c6a6b6SBharat Gooty * Parameters: 143*48c6a6b6SBharat Gooty * bus_id - I2C bus ID 144*48c6a6b6SBharat Gooty * devaddr - Device Address 145*48c6a6b6SBharat Gooty * addr - Register Offset 146*48c6a6b6SBharat Gooty * alen - Address Length, 1 for byte, 2 for word (not supported) 147*48c6a6b6SBharat Gooty * buffer - Data Buffer 148*48c6a6b6SBharat Gooty * len - Data Length in bytes 149*48c6a6b6SBharat Gooty * 150*48c6a6b6SBharat Gooty * Return: 151*48c6a6b6SBharat Gooty * 0 on success, or -1 on failure. 152*48c6a6b6SBharat Gooty */ 153*48c6a6b6SBharat Gooty int i2c_write(uint32_t bus_id, 154*48c6a6b6SBharat Gooty uint8_t devaddr, 155*48c6a6b6SBharat Gooty uint32_t addr, 156*48c6a6b6SBharat Gooty int alen, 157*48c6a6b6SBharat Gooty uint8_t *buffer, 158*48c6a6b6SBharat Gooty int len); 159*48c6a6b6SBharat Gooty 160*48c6a6b6SBharat Gooty 161*48c6a6b6SBharat Gooty #endif /* I2C_H */ 162