高通UEFI中的I2C的方式读取TP的id

高通UEFI中的I2C的方式读取TP的id

原文:https://blog.csdn.net/mengluoxixiang/article/details/100103347

老规矩,先说要实现的功能:用I2C的方式读取TP的ID,然后通过TP的ID不同来做LCD兼容。

文件路径:BOOT.XF.1.4/boot_images/QcomPkg/Sdm660Pkg/Library/MDPPlatformLib/MDPPlatformLib.c

1、定义要用到的pin脚

#define CDP_TP_INT_GPIO                  67  //TP中断pin脚
#define CDP_TP_RES_GPIO                  66  //TP重置pin脚
#define CDP_TP_I2C_SDA_GPIO              14  //i2c数据读写pin脚
#define CDP_TP_I2C_CLK_GPIO              15  //i2c时钟控制pin脚

2、引入相关头文件,定义全局变量

/* I2C Interfaces */
#include <Protocol/EFII2C.h>
static i2c_config cfg;
static void *pI2cHandle = NULL;

3、i2c的init函数

i2c_status i2c_init(UINT32 SlaveAddr, UINT32 I2cFreq)
{
	i2c_status i2cstatus = I2C_SUCCESS;
 
	cfg.bus_frequency_khz = I2cFreq;
	cfg.slave_address = SlaveAddr;
	cfg.slave_address_type =  I2C_07_BIT_SLAVE_ADDRESS;
	MDP_Status                    Status           = MDP_STATUS_OK;
	EFI_TLMM_PROTOCOL			 *TLMMProtocol	   = NULL;
	
	if (EFI_SUCCESS != gBS->LocateProtocol(&gEfiTLMMProtocolGuid, NULL, (void **)&TLMMProtocol))
	{
	  DEBUG((EFI_D_ERROR, "DisplayDxe: Locate TLMM protocol failed!\n"));
	  Status = MDP_STATUS_NO_RESOURCES;
	}
	else
	{
		/* Setup tp i2c sda Pin */
		if (EFI_SUCCESS != TLMMProtocol->ConfigGpio((UINT32)EFI_GPIO_CFG(CDP_TP_I2C_SDA_GPIO, 2, GPIO_OUTPUT, GPIO_PULL_DOWN, GPIO_2MA), TLMM_GPIO_ENABLE))
		{
		  DEBUG((EFI_D_WARN, "DisplayDxe: Configure GPIO %d for Reset_N line Failed!\n", CDP_TP_I2C_SDA_GPIO));
		}
		/* Setup tp i2c clk Pin */
		if (EFI_SUCCESS != TLMMProtocol->ConfigGpio((UINT32)EFI_GPIO_CFG(CDP_TP_I2C_CLK_GPIO, 2, GPIO_OUTPUT, GPIO_PULL_DOWN, GPIO_2MA), TLMM_GPIO_ENABLE))
		{
		  DEBUG((EFI_D_WARN, "DisplayDxe: Configure GPIO %d for Reset_N line Failed!\n", CDP_TP_I2C_CLK_GPIO));
		}
		/* Setup tp int Pin */
		if (EFI_SUCCESS != TLMMProtocol->ConfigGpio((UINT32)EFI_GPIO_CFG(CDP_TP_INT_GPIO, 0, GPIO_OUTPUT, GPIO_PULL_UP, GPIO_2MA), TLMM_GPIO_ENABLE))
		{
		  DEBUG((EFI_D_WARN, "DisplayDxe: Configure GPIO %d for Reset_N line Failed!\n", CDP_TP_INT_GPIO));
		}
 
		/* Setup tp reset Pin */
		if (EFI_SUCCESS != TLMMProtocol->ConfigGpio((UINT32)EFI_GPIO_CFG(CDP_TP_RES_GPIO, 0, GPIO_OUTPUT, GPIO_PULL_UP, GPIO_2MA), TLMM_GPIO_ENABLE))
		{
		  DEBUG((EFI_D_WARN, "DisplayDxe: Configure GPIO %d for Reset_N line Failed!\n", CDP_TP_RES_GPIO));
		}
 
		/* Set tp int line HIGH */
		if (EFI_SUCCESS != TLMMProtocol->GpioOut((UINT32)EFI_GPIO_CFG(CDP_TP_INT_GPIO, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_2MA), GPIO_HIGH_VALUE))
		{
		  DEBUG((EFI_D_WARN, "DisplayDxe: Reset_N line HIGH failed!\n"));
		}
        MDP_OSAL_DELAYUS(150);
 
		/* Set tp reset line HIGH */
		if (EFI_SUCCESS != TLMMProtocol->GpioOut((UINT32)EFI_GPIO_CFG(CDP_TP_RES_GPIO, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_2MA), GPIO_HIGH_VALUE))
		{
		  DEBUG((EFI_D_WARN, "DisplayDxe: Reset_N line HIGH failed!\n"));
		}
		MDP_OSAL_DELAYMS(8);
 
		/* Set tp int line LOW */
		if (EFI_SUCCESS != TLMMProtocol->GpioOut((UINT32)EFI_GPIO_CFG(CDP_TP_INT_GPIO, 0, GPIO_OUTPUT, GPIO_NO_PULL, GPIO_2MA), GPIO_LOW_VALUE))
		{
		  DEBUG((EFI_D_WARN, "DisplayDxe: Reset_N line HIGH failed!\n"));
		}
        MDP_OSAL_DELAYMS(60);
		}
 
	i2cstatus = i2c_open((i2c_instance) (I2C_INSTANCE_004), &pI2cHandle);
	if (I2C_SUCCESS != i2cstatus)
	{
		DEBUG((EFI_D_ERROR, "Failed to initialize I2C %d\n", i2cstatus));
	}
	return i2cstatus;
}

4、i2c读取数据

unsigned int i2c_read_reg(unsigned int addr)
{
	uint32 bRead = 0;
	unsigned int getdata = 0;
	i2c_status i2cstatus = I2C_SUCCESS;
	unsigned char rdbuf[2] = {0};
	gBS->Stall(600000);
	i2cstatus = i2c_read (pI2cHandle, &cfg, addr, 2, rdbuf, 1, &bRead, 2500);
	if(I2C_SUCCESS != i2cstatus)
	{
		DEBUG((EFI_D_ERROR, "Read addr:0x%X error\n", addr));
	}
	gBS->Stall(600000);
	getdata=rdbuf[0] & 0x00ff;
	getdata<<= 8;
	getdata |=rdbuf[1];
	DEBUG((EFI_D_ERROR, "[dong]rdbuf[0] & 0x00ff is %d\n\n", rdbuf[0] & 0x00ff));
	return (rdbuf[0] & 0x00ff);//getdata;//
	
}

5、i2c写数据

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;
}

6、i2c关闭

i2c_status i2c_deinit()
{
	return i2c_close(pI2cHandle);
}

7、i2c读tp的id

unsigned int i2c_read_tp_sensor(void)
{
	unsigned int tp_sensor_id = 0;
 
	i2c_init(0x14, 400000);//i2c从机地址、读写速率
	tp_sensor_id = i2c_read_reg(0x814a);//tp中存id的寄存器地址
	i2c_deinit();
		
	return tp_sensor_id;
}
posted @ 2021-04-22 16:00  schips  阅读(1017)  评论(0编辑  收藏  举报