181dee67eSSudip Mukherjee /******************************************************************* 281dee67eSSudip Mukherjee * 381dee67eSSudip Mukherjee * Copyright (c) 2007 by Silicon Motion, Inc. (SMI) 481dee67eSSudip Mukherjee * 581dee67eSSudip Mukherjee * All rights are reserved. Reproduction or in part is prohibited 681dee67eSSudip Mukherjee * without the written consent of the copyright owner. 781dee67eSSudip Mukherjee * 881dee67eSSudip Mukherjee * swi2c.c --- SM750/SM718 DDK 981dee67eSSudip Mukherjee * This file contains the source code for I2C using software 1081dee67eSSudip Mukherjee * implementation. 1181dee67eSSudip Mukherjee * 1281dee67eSSudip Mukherjee *******************************************************************/ 1381dee67eSSudip Mukherjee #include "ddk750_help.h" 1481dee67eSSudip Mukherjee #include "ddk750_reg.h" 1581dee67eSSudip Mukherjee #include "ddk750_swi2c.h" 1681dee67eSSudip Mukherjee #include "ddk750_power.h" 1781dee67eSSudip Mukherjee 1881dee67eSSudip Mukherjee 1981dee67eSSudip Mukherjee /******************************************************************* 2081dee67eSSudip Mukherjee * I2C Software Master Driver: 2181dee67eSSudip Mukherjee * =========================== 2281dee67eSSudip Mukherjee * Each i2c cycle is split into 4 sections. Each of these section marks 2381dee67eSSudip Mukherjee * a point in time where the SCL or SDA may be changed. 2481dee67eSSudip Mukherjee * 2581dee67eSSudip Mukherjee * 1 Cycle == | Section I. | Section 2. | Section 3. | Section 4. | 2681dee67eSSudip Mukherjee * +-------------+-------------+-------------+-------------+ 2781dee67eSSudip Mukherjee * | SCL set LOW |SCL no change| SCL set HIGH|SCL no change| 2881dee67eSSudip Mukherjee * 2981dee67eSSudip Mukherjee * ____________ _____________ 3081dee67eSSudip Mukherjee * SCL == XXXX _____________ ____________ / 3181dee67eSSudip Mukherjee * 3281dee67eSSudip Mukherjee * I.e. the SCL may only be changed in section 1. and section 3. while 3381dee67eSSudip Mukherjee * the SDA may only be changed in section 2. and section 4. The table 3481dee67eSSudip Mukherjee * below gives the changes for these 2 lines in the varios sections. 3581dee67eSSudip Mukherjee * 3681dee67eSSudip Mukherjee * Section changes Table: 3781dee67eSSudip Mukherjee * ====================== 3881dee67eSSudip Mukherjee * blank = no change, L = set bit LOW, H = set bit HIGH 3981dee67eSSudip Mukherjee * 4081dee67eSSudip Mukherjee * | 1.| 2.| 3.| 4.| 4181dee67eSSudip Mukherjee * ---------------+---+---+---+---+ 4281dee67eSSudip Mukherjee * Tx Start SDA | | H | | L | 4381dee67eSSudip Mukherjee * SCL | L | | H | | 4481dee67eSSudip Mukherjee * ---------------+---+---+---+---+ 4581dee67eSSudip Mukherjee * Tx Stop SDA | | L | | H | 4681dee67eSSudip Mukherjee * SCL | L | | H | | 4781dee67eSSudip Mukherjee * ---------------+---+---+---+---+ 4881dee67eSSudip Mukherjee * Tx bit H SDA | | H | | | 4981dee67eSSudip Mukherjee * SCL | L | | H | | 5081dee67eSSudip Mukherjee * ---------------+---+---+---+---+ 5181dee67eSSudip Mukherjee * Tx bit L SDA | | L | | | 5281dee67eSSudip Mukherjee * SCL | L | | H | | 5381dee67eSSudip Mukherjee * ---------------+---+---+---+---+ 5481dee67eSSudip Mukherjee * 5581dee67eSSudip Mukherjee ******************************************************************/ 5681dee67eSSudip Mukherjee 5781dee67eSSudip Mukherjee /* GPIO pins used for this I2C. It ranges from 0 to 63. */ 5881dee67eSSudip Mukherjee static unsigned char g_i2cClockGPIO = DEFAULT_I2C_SCL; 5981dee67eSSudip Mukherjee static unsigned char g_i2cDataGPIO = DEFAULT_I2C_SDA; 6081dee67eSSudip Mukherjee 6181dee67eSSudip Mukherjee /* 6281dee67eSSudip Mukherjee * Below is the variable declaration for the GPIO pin register usage 6381dee67eSSudip Mukherjee * for the i2c Clock and i2c Data. 6481dee67eSSudip Mukherjee * 6581dee67eSSudip Mukherjee * Note: 6681dee67eSSudip Mukherjee * Notice that the GPIO usage for the i2c clock and i2c Data are 6781dee67eSSudip Mukherjee * separated. This is to make this code flexible enough when 6881dee67eSSudip Mukherjee * two separate GPIO pins for the clock and data are located 6981dee67eSSudip Mukherjee * in two different GPIO register set (worst case). 7081dee67eSSudip Mukherjee */ 7181dee67eSSudip Mukherjee 7281dee67eSSudip Mukherjee /* i2c Clock GPIO Register usage */ 7381dee67eSSudip Mukherjee static unsigned long g_i2cClkGPIOMuxReg = GPIO_MUX; 7481dee67eSSudip Mukherjee static unsigned long g_i2cClkGPIODataReg = GPIO_DATA; 7581dee67eSSudip Mukherjee static unsigned long g_i2cClkGPIODataDirReg = GPIO_DATA_DIRECTION; 7681dee67eSSudip Mukherjee 7781dee67eSSudip Mukherjee /* i2c Data GPIO Register usage */ 7881dee67eSSudip Mukherjee static unsigned long g_i2cDataGPIOMuxReg = GPIO_MUX; 7981dee67eSSudip Mukherjee static unsigned long g_i2cDataGPIODataReg = GPIO_DATA; 8081dee67eSSudip Mukherjee static unsigned long g_i2cDataGPIODataDirReg = GPIO_DATA_DIRECTION; 8181dee67eSSudip Mukherjee 8281dee67eSSudip Mukherjee /* 8381dee67eSSudip Mukherjee * This function puts a delay between command 8481dee67eSSudip Mukherjee */ 8581dee67eSSudip Mukherjee static void swI2CWait(void) 8681dee67eSSudip Mukherjee { 8781dee67eSSudip Mukherjee /* find a bug: 8881dee67eSSudip Mukherjee * peekIO method works well before suspend/resume 8981dee67eSSudip Mukherjee * but after suspend, peekIO(0x3ce,0x61) & 0x10 9081dee67eSSudip Mukherjee * always be non-zero,which makes the while loop 9181dee67eSSudip Mukherjee * never finish. 9281dee67eSSudip Mukherjee * use non-ultimate for loop below is safe 9381dee67eSSudip Mukherjee * */ 9481dee67eSSudip Mukherjee #if 0 9581dee67eSSudip Mukherjee /* Change wait algorithm to use PCI bus clock, 9681dee67eSSudip Mukherjee it's more reliable than counter loop .. 9781dee67eSSudip Mukherjee write 0x61 to 0x3ce and read from 0x3cf 9881dee67eSSudip Mukherjee */ 9981dee67eSSudip Mukherjee while(peekIO(0x3ce,0x61) & 0x10); 10081dee67eSSudip Mukherjee #else 10181dee67eSSudip Mukherjee int i, Temp; 10281dee67eSSudip Mukherjee 10381dee67eSSudip Mukherjee for(i=0; i<600; i++) 10481dee67eSSudip Mukherjee { 10581dee67eSSudip Mukherjee Temp = i; 10681dee67eSSudip Mukherjee Temp += i; 10781dee67eSSudip Mukherjee } 10881dee67eSSudip Mukherjee #endif 10981dee67eSSudip Mukherjee } 11081dee67eSSudip Mukherjee 11181dee67eSSudip Mukherjee /* 11281dee67eSSudip Mukherjee * This function set/reset the SCL GPIO pin 11381dee67eSSudip Mukherjee * 11481dee67eSSudip Mukherjee * Parameters: 11581dee67eSSudip Mukherjee * value - Bit value to set to the SCL or SDA (0 = low, 1 = high) 11681dee67eSSudip Mukherjee * 11781dee67eSSudip Mukherjee * Notes: 11881dee67eSSudip Mukherjee * When setting SCL to high, just set the GPIO as input where the pull up 11981dee67eSSudip Mukherjee * resistor will pull the signal up. Do not use software to pull up the 12081dee67eSSudip Mukherjee * signal because the i2c will fail when other device try to drive the 12181dee67eSSudip Mukherjee * signal due to SM50x will drive the signal to always high. 12281dee67eSSudip Mukherjee */ 12381dee67eSSudip Mukherjee void swI2CSCL(unsigned char value) 12481dee67eSSudip Mukherjee { 12581dee67eSSudip Mukherjee unsigned long ulGPIOData; 12681dee67eSSudip Mukherjee unsigned long ulGPIODirection; 12781dee67eSSudip Mukherjee 12881dee67eSSudip Mukherjee ulGPIODirection = PEEK32(g_i2cClkGPIODataDirReg); 12981dee67eSSudip Mukherjee if (value) /* High */ 13081dee67eSSudip Mukherjee { 13181dee67eSSudip Mukherjee /* Set direction as input. This will automatically pull the signal up. */ 13281dee67eSSudip Mukherjee ulGPIODirection &= ~(1 << g_i2cClockGPIO); 13381dee67eSSudip Mukherjee POKE32(g_i2cClkGPIODataDirReg, ulGPIODirection); 13481dee67eSSudip Mukherjee } 13581dee67eSSudip Mukherjee else /* Low */ 13681dee67eSSudip Mukherjee { 13781dee67eSSudip Mukherjee /* Set the signal down */ 13881dee67eSSudip Mukherjee ulGPIOData = PEEK32(g_i2cClkGPIODataReg); 13981dee67eSSudip Mukherjee ulGPIOData &= ~(1 << g_i2cClockGPIO); 14081dee67eSSudip Mukherjee POKE32(g_i2cClkGPIODataReg, ulGPIOData); 14181dee67eSSudip Mukherjee 14281dee67eSSudip Mukherjee /* Set direction as output */ 14381dee67eSSudip Mukherjee ulGPIODirection |= (1 << g_i2cClockGPIO); 14481dee67eSSudip Mukherjee POKE32(g_i2cClkGPIODataDirReg, ulGPIODirection); 14581dee67eSSudip Mukherjee } 14681dee67eSSudip Mukherjee } 14781dee67eSSudip Mukherjee 14881dee67eSSudip Mukherjee /* 14981dee67eSSudip Mukherjee * This function set/reset the SDA GPIO pin 15081dee67eSSudip Mukherjee * 15181dee67eSSudip Mukherjee * Parameters: 15281dee67eSSudip Mukherjee * value - Bit value to set to the SCL or SDA (0 = low, 1 = high) 15381dee67eSSudip Mukherjee * 15481dee67eSSudip Mukherjee * Notes: 15581dee67eSSudip Mukherjee * When setting SCL to high, just set the GPIO as input where the pull up 15681dee67eSSudip Mukherjee * resistor will pull the signal up. Do not use software to pull up the 15781dee67eSSudip Mukherjee * signal because the i2c will fail when other device try to drive the 15881dee67eSSudip Mukherjee * signal due to SM50x will drive the signal to always high. 15981dee67eSSudip Mukherjee */ 16081dee67eSSudip Mukherjee void swI2CSDA(unsigned char value) 16181dee67eSSudip Mukherjee { 16281dee67eSSudip Mukherjee unsigned long ulGPIOData; 16381dee67eSSudip Mukherjee unsigned long ulGPIODirection; 16481dee67eSSudip Mukherjee 16581dee67eSSudip Mukherjee ulGPIODirection = PEEK32(g_i2cDataGPIODataDirReg); 16681dee67eSSudip Mukherjee if (value) /* High */ 16781dee67eSSudip Mukherjee { 16881dee67eSSudip Mukherjee /* Set direction as input. This will automatically pull the signal up. */ 16981dee67eSSudip Mukherjee ulGPIODirection &= ~(1 << g_i2cDataGPIO); 17081dee67eSSudip Mukherjee POKE32(g_i2cDataGPIODataDirReg, ulGPIODirection); 17181dee67eSSudip Mukherjee } 17281dee67eSSudip Mukherjee else /* Low */ 17381dee67eSSudip Mukherjee { 17481dee67eSSudip Mukherjee /* Set the signal down */ 17581dee67eSSudip Mukherjee ulGPIOData = PEEK32(g_i2cDataGPIODataReg); 17681dee67eSSudip Mukherjee ulGPIOData &= ~(1 << g_i2cDataGPIO); 17781dee67eSSudip Mukherjee POKE32(g_i2cDataGPIODataReg, ulGPIOData); 17881dee67eSSudip Mukherjee 17981dee67eSSudip Mukherjee /* Set direction as output */ 18081dee67eSSudip Mukherjee ulGPIODirection |= (1 << g_i2cDataGPIO); 18181dee67eSSudip Mukherjee POKE32(g_i2cDataGPIODataDirReg, ulGPIODirection); 18281dee67eSSudip Mukherjee } 18381dee67eSSudip Mukherjee } 18481dee67eSSudip Mukherjee 18581dee67eSSudip Mukherjee /* 18681dee67eSSudip Mukherjee * This function read the data from the SDA GPIO pin 18781dee67eSSudip Mukherjee * 18881dee67eSSudip Mukherjee * Return Value: 18981dee67eSSudip Mukherjee * The SDA data bit sent by the Slave 19081dee67eSSudip Mukherjee */ 19181dee67eSSudip Mukherjee static unsigned char swI2CReadSDA(void) 19281dee67eSSudip Mukherjee { 19381dee67eSSudip Mukherjee unsigned long ulGPIODirection; 19481dee67eSSudip Mukherjee unsigned long ulGPIOData; 19581dee67eSSudip Mukherjee 19681dee67eSSudip Mukherjee /* Make sure that the direction is input (High) */ 19781dee67eSSudip Mukherjee ulGPIODirection = PEEK32(g_i2cDataGPIODataDirReg); 19881dee67eSSudip Mukherjee if ((ulGPIODirection & (1 << g_i2cDataGPIO)) != (~(1 << g_i2cDataGPIO))) 19981dee67eSSudip Mukherjee { 20081dee67eSSudip Mukherjee ulGPIODirection &= ~(1 << g_i2cDataGPIO); 20181dee67eSSudip Mukherjee POKE32(g_i2cDataGPIODataDirReg, ulGPIODirection); 20281dee67eSSudip Mukherjee } 20381dee67eSSudip Mukherjee 20481dee67eSSudip Mukherjee /* Now read the SDA line */ 20581dee67eSSudip Mukherjee ulGPIOData = PEEK32(g_i2cDataGPIODataReg); 20681dee67eSSudip Mukherjee if (ulGPIOData & (1 << g_i2cDataGPIO)) 20781dee67eSSudip Mukherjee return 1; 20881dee67eSSudip Mukherjee else 20981dee67eSSudip Mukherjee return 0; 21081dee67eSSudip Mukherjee } 21181dee67eSSudip Mukherjee 21281dee67eSSudip Mukherjee /* 21381dee67eSSudip Mukherjee * This function sends ACK signal 21481dee67eSSudip Mukherjee */ 21581dee67eSSudip Mukherjee static void swI2CAck(void) 21681dee67eSSudip Mukherjee { 21781dee67eSSudip Mukherjee return; /* Single byte read is ok without it. */ 21881dee67eSSudip Mukherjee } 21981dee67eSSudip Mukherjee 22081dee67eSSudip Mukherjee /* 22181dee67eSSudip Mukherjee * This function sends the start command to the slave device 22281dee67eSSudip Mukherjee */ 2236a9df430SRicardo Ribalda Delgado static void swI2CStart(void) 22481dee67eSSudip Mukherjee { 22581dee67eSSudip Mukherjee /* Start I2C */ 22681dee67eSSudip Mukherjee swI2CSDA(1); 22781dee67eSSudip Mukherjee swI2CSCL(1); 22881dee67eSSudip Mukherjee swI2CSDA(0); 22981dee67eSSudip Mukherjee } 23081dee67eSSudip Mukherjee 23181dee67eSSudip Mukherjee /* 23281dee67eSSudip Mukherjee * This function sends the stop command to the slave device 23381dee67eSSudip Mukherjee */ 2346a9df430SRicardo Ribalda Delgado static void swI2CStop(void) 23581dee67eSSudip Mukherjee { 23681dee67eSSudip Mukherjee /* Stop the I2C */ 23781dee67eSSudip Mukherjee swI2CSCL(1); 23881dee67eSSudip Mukherjee swI2CSDA(0); 23981dee67eSSudip Mukherjee swI2CSDA(1); 24081dee67eSSudip Mukherjee } 24181dee67eSSudip Mukherjee 24281dee67eSSudip Mukherjee /* 24381dee67eSSudip Mukherjee * This function writes one byte to the slave device 24481dee67eSSudip Mukherjee * 24581dee67eSSudip Mukherjee * Parameters: 24681dee67eSSudip Mukherjee * data - Data to be write to the slave device 24781dee67eSSudip Mukherjee * 24881dee67eSSudip Mukherjee * Return Value: 24981dee67eSSudip Mukherjee * 0 - Success 25081dee67eSSudip Mukherjee * -1 - Fail to write byte 25181dee67eSSudip Mukherjee */ 2526a9df430SRicardo Ribalda Delgado static long swI2CWriteByte(unsigned char data) 25381dee67eSSudip Mukherjee { 25481dee67eSSudip Mukherjee unsigned char value = data; 25581dee67eSSudip Mukherjee int i; 25681dee67eSSudip Mukherjee 25781dee67eSSudip Mukherjee /* Sending the data bit by bit */ 25881dee67eSSudip Mukherjee for (i=0; i<8; i++) 25981dee67eSSudip Mukherjee { 26081dee67eSSudip Mukherjee /* Set SCL to low */ 26181dee67eSSudip Mukherjee swI2CSCL(0); 26281dee67eSSudip Mukherjee 26381dee67eSSudip Mukherjee /* Send data bit */ 26481dee67eSSudip Mukherjee if ((value & 0x80) != 0) 26581dee67eSSudip Mukherjee swI2CSDA(1); 26681dee67eSSudip Mukherjee else 26781dee67eSSudip Mukherjee swI2CSDA(0); 26881dee67eSSudip Mukherjee 26981dee67eSSudip Mukherjee swI2CWait(); 27081dee67eSSudip Mukherjee 27181dee67eSSudip Mukherjee /* Toggle clk line to one */ 27281dee67eSSudip Mukherjee swI2CSCL(1); 27381dee67eSSudip Mukherjee swI2CWait(); 27481dee67eSSudip Mukherjee 27581dee67eSSudip Mukherjee /* Shift byte to be sent */ 27681dee67eSSudip Mukherjee value = value << 1; 27781dee67eSSudip Mukherjee } 27881dee67eSSudip Mukherjee 27981dee67eSSudip Mukherjee /* Set the SCL Low and SDA High (prepare to get input) */ 28081dee67eSSudip Mukherjee swI2CSCL(0); 28181dee67eSSudip Mukherjee swI2CSDA(1); 28281dee67eSSudip Mukherjee 28381dee67eSSudip Mukherjee /* Set the SCL High for ack */ 28481dee67eSSudip Mukherjee swI2CWait(); 28581dee67eSSudip Mukherjee swI2CSCL(1); 28681dee67eSSudip Mukherjee swI2CWait(); 28781dee67eSSudip Mukherjee 28881dee67eSSudip Mukherjee /* Read SDA, until SDA==0 */ 28981dee67eSSudip Mukherjee for(i=0; i<0xff; i++) 29081dee67eSSudip Mukherjee { 29181dee67eSSudip Mukherjee if (!swI2CReadSDA()) 29281dee67eSSudip Mukherjee break; 29381dee67eSSudip Mukherjee 29481dee67eSSudip Mukherjee swI2CSCL(0); 29581dee67eSSudip Mukherjee swI2CWait(); 29681dee67eSSudip Mukherjee swI2CSCL(1); 29781dee67eSSudip Mukherjee swI2CWait(); 29881dee67eSSudip Mukherjee } 29981dee67eSSudip Mukherjee 30081dee67eSSudip Mukherjee /* Set the SCL Low and SDA High */ 30181dee67eSSudip Mukherjee swI2CSCL(0); 30281dee67eSSudip Mukherjee swI2CSDA(1); 30381dee67eSSudip Mukherjee 30481dee67eSSudip Mukherjee if (i<0xff) 30581dee67eSSudip Mukherjee return 0; 30681dee67eSSudip Mukherjee else 3076946edd0SSupriya Karanth return -1; 30881dee67eSSudip Mukherjee } 30981dee67eSSudip Mukherjee 31081dee67eSSudip Mukherjee /* 31181dee67eSSudip Mukherjee * This function reads one byte from the slave device 31281dee67eSSudip Mukherjee * 31381dee67eSSudip Mukherjee * Parameters: 31481dee67eSSudip Mukherjee * ack - Flag to indicate either to send the acknowledge 31581dee67eSSudip Mukherjee * message to the slave device or not 31681dee67eSSudip Mukherjee * 31781dee67eSSudip Mukherjee * Return Value: 31881dee67eSSudip Mukherjee * One byte data read from the Slave device 31981dee67eSSudip Mukherjee */ 3206a9df430SRicardo Ribalda Delgado static unsigned char swI2CReadByte(unsigned char ack) 32181dee67eSSudip Mukherjee { 32281dee67eSSudip Mukherjee int i; 32381dee67eSSudip Mukherjee unsigned char data = 0; 32481dee67eSSudip Mukherjee 32581dee67eSSudip Mukherjee for(i=7; i>=0; i--) 32681dee67eSSudip Mukherjee { 32781dee67eSSudip Mukherjee /* Set the SCL to Low and SDA to High (Input) */ 32881dee67eSSudip Mukherjee swI2CSCL(0); 32981dee67eSSudip Mukherjee swI2CSDA(1); 33081dee67eSSudip Mukherjee swI2CWait(); 33181dee67eSSudip Mukherjee 33281dee67eSSudip Mukherjee /* Set the SCL High */ 33381dee67eSSudip Mukherjee swI2CSCL(1); 33481dee67eSSudip Mukherjee swI2CWait(); 33581dee67eSSudip Mukherjee 33681dee67eSSudip Mukherjee /* Read data bits from SDA */ 33781dee67eSSudip Mukherjee data |= (swI2CReadSDA() << i); 33881dee67eSSudip Mukherjee } 33981dee67eSSudip Mukherjee 34081dee67eSSudip Mukherjee if (ack) 34181dee67eSSudip Mukherjee swI2CAck(); 34281dee67eSSudip Mukherjee 34381dee67eSSudip Mukherjee /* Set the SCL Low and SDA High */ 34481dee67eSSudip Mukherjee swI2CSCL(0); 34581dee67eSSudip Mukherjee swI2CSDA(1); 34681dee67eSSudip Mukherjee 34781dee67eSSudip Mukherjee return data; 34881dee67eSSudip Mukherjee } 34981dee67eSSudip Mukherjee 35081dee67eSSudip Mukherjee /* 35181dee67eSSudip Mukherjee * This function initializes GPIO port for SW I2C communication. 35281dee67eSSudip Mukherjee * 35381dee67eSSudip Mukherjee * Parameters: 35481dee67eSSudip Mukherjee * i2cClkGPIO - The GPIO pin to be used as i2c SCL 35581dee67eSSudip Mukherjee * i2cDataGPIO - The GPIO pin to be used as i2c SDA 35681dee67eSSudip Mukherjee * 35781dee67eSSudip Mukherjee * Return Value: 35881dee67eSSudip Mukherjee * -1 - Fail to initialize the i2c 35981dee67eSSudip Mukherjee * 0 - Success 36081dee67eSSudip Mukherjee */ 3616a9df430SRicardo Ribalda Delgado static long swI2CInit_SM750LE(unsigned char i2cClkGPIO, 3626a9df430SRicardo Ribalda Delgado unsigned char i2cDataGPIO) 36381dee67eSSudip Mukherjee { 36481dee67eSSudip Mukherjee int i; 36581dee67eSSudip Mukherjee 36681dee67eSSudip Mukherjee /* Initialize the GPIO pin for the i2c Clock Register */ 36781dee67eSSudip Mukherjee g_i2cClkGPIODataReg = GPIO_DATA_SM750LE; 36881dee67eSSudip Mukherjee g_i2cClkGPIODataDirReg = GPIO_DATA_DIRECTION_SM750LE; 36981dee67eSSudip Mukherjee 37081dee67eSSudip Mukherjee /* Initialize the Clock GPIO Offset */ 37181dee67eSSudip Mukherjee g_i2cClockGPIO = i2cClkGPIO; 37281dee67eSSudip Mukherjee 37381dee67eSSudip Mukherjee /* Initialize the GPIO pin for the i2c Data Register */ 37481dee67eSSudip Mukherjee g_i2cDataGPIODataReg = GPIO_DATA_SM750LE; 37581dee67eSSudip Mukherjee g_i2cDataGPIODataDirReg = GPIO_DATA_DIRECTION_SM750LE; 37681dee67eSSudip Mukherjee 37781dee67eSSudip Mukherjee /* Initialize the Data GPIO Offset */ 37881dee67eSSudip Mukherjee g_i2cDataGPIO = i2cDataGPIO; 37981dee67eSSudip Mukherjee 38081dee67eSSudip Mukherjee /* Note that SM750LE don't have GPIO MUX and power is always on */ 38181dee67eSSudip Mukherjee 38281dee67eSSudip Mukherjee /* Clear the i2c lines. */ 38381dee67eSSudip Mukherjee for(i=0; i<9; i++) 38481dee67eSSudip Mukherjee swI2CStop(); 38581dee67eSSudip Mukherjee 38681dee67eSSudip Mukherjee return 0; 38781dee67eSSudip Mukherjee } 38881dee67eSSudip Mukherjee 38981dee67eSSudip Mukherjee /* 39081dee67eSSudip Mukherjee * This function initializes the i2c attributes and bus 39181dee67eSSudip Mukherjee * 39281dee67eSSudip Mukherjee * Parameters: 39381dee67eSSudip Mukherjee * i2cClkGPIO - The GPIO pin to be used as i2c SCL 39481dee67eSSudip Mukherjee * i2cDataGPIO - The GPIO pin to be used as i2c SDA 39581dee67eSSudip Mukherjee * 39681dee67eSSudip Mukherjee * Return Value: 39781dee67eSSudip Mukherjee * -1 - Fail to initialize the i2c 39881dee67eSSudip Mukherjee * 0 - Success 39981dee67eSSudip Mukherjee */ 40081dee67eSSudip Mukherjee long swI2CInit( 40181dee67eSSudip Mukherjee unsigned char i2cClkGPIO, 40281dee67eSSudip Mukherjee unsigned char i2cDataGPIO 40381dee67eSSudip Mukherjee ) 40481dee67eSSudip Mukherjee { 40581dee67eSSudip Mukherjee int i; 40681dee67eSSudip Mukherjee 40781dee67eSSudip Mukherjee /* Return 0 if the GPIO pins to be used is out of range. The range is only from [0..63] */ 40881dee67eSSudip Mukherjee if ((i2cClkGPIO > 31) || (i2cDataGPIO > 31)) 4096946edd0SSupriya Karanth return -1; 41081dee67eSSudip Mukherjee 41181dee67eSSudip Mukherjee if (getChipType() == SM750LE) 4126946edd0SSupriya Karanth return swI2CInit_SM750LE(i2cClkGPIO, i2cDataGPIO); 41381dee67eSSudip Mukherjee 41481dee67eSSudip Mukherjee /* Initialize the GPIO pin for the i2c Clock Register */ 41581dee67eSSudip Mukherjee g_i2cClkGPIOMuxReg = GPIO_MUX; 41681dee67eSSudip Mukherjee g_i2cClkGPIODataReg = GPIO_DATA; 41781dee67eSSudip Mukherjee g_i2cClkGPIODataDirReg = GPIO_DATA_DIRECTION; 41881dee67eSSudip Mukherjee 41981dee67eSSudip Mukherjee /* Initialize the Clock GPIO Offset */ 42081dee67eSSudip Mukherjee g_i2cClockGPIO = i2cClkGPIO; 42181dee67eSSudip Mukherjee 42281dee67eSSudip Mukherjee /* Initialize the GPIO pin for the i2c Data Register */ 42381dee67eSSudip Mukherjee g_i2cDataGPIOMuxReg = GPIO_MUX; 42481dee67eSSudip Mukherjee g_i2cDataGPIODataReg = GPIO_DATA; 42581dee67eSSudip Mukherjee g_i2cDataGPIODataDirReg = GPIO_DATA_DIRECTION; 42681dee67eSSudip Mukherjee 42781dee67eSSudip Mukherjee /* Initialize the Data GPIO Offset */ 42881dee67eSSudip Mukherjee g_i2cDataGPIO = i2cDataGPIO; 42981dee67eSSudip Mukherjee 43081dee67eSSudip Mukherjee /* Enable the GPIO pins for the i2c Clock and Data (GPIO MUX) */ 43181dee67eSSudip Mukherjee POKE32(g_i2cClkGPIOMuxReg, 43281dee67eSSudip Mukherjee PEEK32(g_i2cClkGPIOMuxReg) & ~(1 << g_i2cClockGPIO)); 43381dee67eSSudip Mukherjee POKE32(g_i2cDataGPIOMuxReg, 43481dee67eSSudip Mukherjee PEEK32(g_i2cDataGPIOMuxReg) & ~(1 << g_i2cDataGPIO)); 43581dee67eSSudip Mukherjee 43681dee67eSSudip Mukherjee /* Enable GPIO power */ 43781dee67eSSudip Mukherjee enableGPIO(1); 43881dee67eSSudip Mukherjee 43981dee67eSSudip Mukherjee /* Clear the i2c lines. */ 44081dee67eSSudip Mukherjee for(i=0; i<9; i++) 44181dee67eSSudip Mukherjee swI2CStop(); 44281dee67eSSudip Mukherjee 44381dee67eSSudip Mukherjee return 0; 44481dee67eSSudip Mukherjee } 44581dee67eSSudip Mukherjee 44681dee67eSSudip Mukherjee /* 44781dee67eSSudip Mukherjee * This function reads the slave device's register 44881dee67eSSudip Mukherjee * 44981dee67eSSudip Mukherjee * Parameters: 45081dee67eSSudip Mukherjee * deviceAddress - i2c Slave device address which register 45181dee67eSSudip Mukherjee * to be read from 45281dee67eSSudip Mukherjee * registerIndex - Slave device's register to be read 45381dee67eSSudip Mukherjee * 45481dee67eSSudip Mukherjee * Return Value: 45581dee67eSSudip Mukherjee * Register value 45681dee67eSSudip Mukherjee */ 45781dee67eSSudip Mukherjee unsigned char swI2CReadReg( 45881dee67eSSudip Mukherjee unsigned char deviceAddress, 45981dee67eSSudip Mukherjee unsigned char registerIndex 46081dee67eSSudip Mukherjee ) 46181dee67eSSudip Mukherjee { 46281dee67eSSudip Mukherjee unsigned char data; 46381dee67eSSudip Mukherjee 46481dee67eSSudip Mukherjee /* Send the Start signal */ 46581dee67eSSudip Mukherjee swI2CStart(); 46681dee67eSSudip Mukherjee 46781dee67eSSudip Mukherjee /* Send the device address */ 46881dee67eSSudip Mukherjee swI2CWriteByte(deviceAddress); 46981dee67eSSudip Mukherjee 47081dee67eSSudip Mukherjee /* Send the register index */ 47181dee67eSSudip Mukherjee swI2CWriteByte(registerIndex); 47281dee67eSSudip Mukherjee 47381dee67eSSudip Mukherjee /* Get the bus again and get the data from the device read address */ 47481dee67eSSudip Mukherjee swI2CStart(); 47581dee67eSSudip Mukherjee swI2CWriteByte(deviceAddress + 1); 47681dee67eSSudip Mukherjee data = swI2CReadByte(1); 47781dee67eSSudip Mukherjee 47881dee67eSSudip Mukherjee /* Stop swI2C and release the bus */ 47981dee67eSSudip Mukherjee swI2CStop(); 48081dee67eSSudip Mukherjee 48181dee67eSSudip Mukherjee return data; 48281dee67eSSudip Mukherjee } 48381dee67eSSudip Mukherjee 48481dee67eSSudip Mukherjee /* 48581dee67eSSudip Mukherjee * This function writes a value to the slave device's register 48681dee67eSSudip Mukherjee * 48781dee67eSSudip Mukherjee * Parameters: 48881dee67eSSudip Mukherjee * deviceAddress - i2c Slave device address which register 48981dee67eSSudip Mukherjee * to be written 49081dee67eSSudip Mukherjee * registerIndex - Slave device's register to be written 49181dee67eSSudip Mukherjee * data - Data to be written to the register 49281dee67eSSudip Mukherjee * 49381dee67eSSudip Mukherjee * Result: 49481dee67eSSudip Mukherjee * 0 - Success 49581dee67eSSudip Mukherjee * -1 - Fail 49681dee67eSSudip Mukherjee */ 49781dee67eSSudip Mukherjee long swI2CWriteReg( 49881dee67eSSudip Mukherjee unsigned char deviceAddress, 49981dee67eSSudip Mukherjee unsigned char registerIndex, 50081dee67eSSudip Mukherjee unsigned char data 50181dee67eSSudip Mukherjee ) 50281dee67eSSudip Mukherjee { 50381dee67eSSudip Mukherjee long returnValue = 0; 50481dee67eSSudip Mukherjee 50581dee67eSSudip Mukherjee /* Send the Start signal */ 50681dee67eSSudip Mukherjee swI2CStart(); 50781dee67eSSudip Mukherjee 50881dee67eSSudip Mukherjee /* Send the device address and read the data. All should return success 50981dee67eSSudip Mukherjee in order for the writing processed to be successful 51081dee67eSSudip Mukherjee */ 51181dee67eSSudip Mukherjee if ((swI2CWriteByte(deviceAddress) != 0) || 51281dee67eSSudip Mukherjee (swI2CWriteByte(registerIndex) != 0) || 51381dee67eSSudip Mukherjee (swI2CWriteByte(data) != 0)) 51481dee67eSSudip Mukherjee { 51581dee67eSSudip Mukherjee returnValue = -1; 51681dee67eSSudip Mukherjee } 51781dee67eSSudip Mukherjee 51881dee67eSSudip Mukherjee /* Stop i2c and release the bus */ 51981dee67eSSudip Mukherjee swI2CStop(); 52081dee67eSSudip Mukherjee 52181dee67eSSudip Mukherjee return returnValue; 52281dee67eSSudip Mukherjee } 523