# Enable I2C bus in UEFI

Source: [https://docs.qualcomm.com/doc/80-88500-1/topic/45_Enable_I2C_bus_in_UEFI.html](https://docs.qualcomm.com/doc/80-88500-1/topic/45_Enable_I2C_bus_in_UEFI.html)

## About this task

To enable the I^2^C bus in UEFI through `I2CApiLib`, do the following:

## Procedure

1. Add the I^2^C-related `lib/protocol` definition to the
                        `*.inf` file.
                
[LibraryClasses]
        I2CApiLib
        [Protocols]
        gQcomI2CProtocolGuid
        Copy to clipboard
2. Add `i2c_open ()`→ `i2c_read()` or
                        `i2c_write()`→ `i2c_close()` functions in the
                    same sequence in the code.
                
Note: These API details are available in
                            qcompkg\library\i2caplib\i2capi.c.
3. Choose the `cfg` codes applicable to the device.
                
cfg.bus_frequency_khz = I2cFreq; 
        cfg.slave_address = SlaveAddr; 
        cfg.mode = I2C; 
        cfg.slave_max_clock_stretch_us = 500; 
        cfg.core_configuration1 = 0; 
        cfg.core_configuration2 = 0;
        
        i2cstatus = i2c_open((i2c_instance) (Your_BLSP_QUP_INDEX), &pI2cHandle); 
        if (I2C_SUCCESS != i2cstatus)
        {
        DEBUG((EFI_D_ERROR, "Failed to initialize I2C %d\n", i2cstatus)); 
        }
        else 
        {
        DEBUG((EFI_D_ERROR, "i2c_open:BLSP%d\n", AW9120_I2C_CORE)); 
        }
        return i2cstatus; 
        } 
        unsigned int i2c_read_reg(unsigned char addr) 
        {
        uint32 bRead = 0; 
        unsigned int getdata = 0;
        i2c_status i2cstatus = I2C_SUCCESS; unsigned char rdbuf[2] = {0}; 
        DEBUG((EFI_D_ERROR, "test 1\n")); 
        gBS->Stall(600000); i2cstatus = i2c_read (pI2cHandle, &cfg, addr, 1, rdbuf, 2, &bRead, 2500); if(I2C_SUCCESS != i2cstatus) 
        { 
        DEBUG((EFI_D_ERROR, "Read addr:0x%X error\n", addr)); 
        } 
        DEBUG((EFI_D_ERROR, "test 4\n")); 
        gBS->Stall(600000); 
        getdata=rdbuf[0] & 0x00ff; 
        getdata<<= 8; 
        getdata |=rdbuf[1]; 
        return getdata; 
        }
        unsigned int i2c_write_reg(unsigned char addr, unsigned int reg_data) 
        { 
        uint32 bWrote = 0;
        i2c_status i2cstatus = I2C_SUCCESS; unsigned char wdbuf[2] = {0}; 
        wdbuf[1] = (unsigned char)(reg_data & 0x00ff); 
        wdbuf[0] = (unsigned char)((reg_data & 0xff00)>>8); 
        i2cstatus = i2c_write (pI2cHandle, &cfg, addr, 1, wdbuf, 2, &bWrote, 2500); 
        if(I2C_SUCCESS != i2cstatus) 
        {
        DEBUG((EFI_D_ERROR, "Write addr:0x%X data:0x%X error\n", addr, reg_data)
        ); 
        }
        return bWrote; 
        } i2c_status i2c_deinit() 
        {
        return i2c_close(pI2cHandle); 
        }
        Copy to clipboard

**Parent Topic:** [I2C in QUP](https://docs.qualcomm.com/doc/80-88500-1/topic/41_I2C_in_QUP_v3.html)

Last Published: Aug 18, 2023

[Previous Topic
Configure I2C in UEFI](https://docs.qualcomm.com/bundle/publicresource/80-88500-1/topics/44_Configure_I2C_in_UEFI.md) [Next Topic
Configure I2C in sensor core as sensor low-power island (SLPI) processor](https://docs.qualcomm.com/bundle/publicresource/80-88500-1/topics/46_Configure_I2C_in_sensor_core_as_sensor_low_power_island__SLPI__processor.md)