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 103d3f431d0SIsaac Assegai for (i = 0; i < 600; i++) { 10481dee67eSSudip Mukherjee Temp = i; 10581dee67eSSudip Mukherjee Temp += i; 10681dee67eSSudip Mukherjee } 10781dee67eSSudip Mukherjee #endif 10881dee67eSSudip Mukherjee } 10981dee67eSSudip Mukherjee 11081dee67eSSudip Mukherjee /* 11181dee67eSSudip Mukherjee * This function set/reset the SCL GPIO pin 11281dee67eSSudip Mukherjee * 11381dee67eSSudip Mukherjee * Parameters: 11481dee67eSSudip Mukherjee * value - Bit value to set to the SCL or SDA (0 = low, 1 = high) 11581dee67eSSudip Mukherjee * 11681dee67eSSudip Mukherjee * Notes: 11781dee67eSSudip Mukherjee * When setting SCL to high, just set the GPIO as input where the pull up 11881dee67eSSudip Mukherjee * resistor will pull the signal up. Do not use software to pull up the 11981dee67eSSudip Mukherjee * signal because the i2c will fail when other device try to drive the 12081dee67eSSudip Mukherjee * signal due to SM50x will drive the signal to always high. 12181dee67eSSudip Mukherjee */ 12281dee67eSSudip Mukherjee void swI2CSCL(unsigned char value) 12381dee67eSSudip Mukherjee { 12481dee67eSSudip Mukherjee unsigned long ulGPIOData; 12581dee67eSSudip Mukherjee unsigned long ulGPIODirection; 12681dee67eSSudip Mukherjee 12781dee67eSSudip Mukherjee ulGPIODirection = PEEK32(g_i2cClkGPIODataDirReg); 128d3f431d0SIsaac Assegai if (value) { /* High */ 12981dee67eSSudip Mukherjee /* Set direction as input. This will automatically pull the signal up. */ 13081dee67eSSudip Mukherjee ulGPIODirection &= ~(1 << g_i2cClockGPIO); 13181dee67eSSudip Mukherjee POKE32(g_i2cClkGPIODataDirReg, ulGPIODirection); 132d3f431d0SIsaac Assegai } else { /* Low */ 13381dee67eSSudip Mukherjee /* Set the signal down */ 13481dee67eSSudip Mukherjee ulGPIOData = PEEK32(g_i2cClkGPIODataReg); 13581dee67eSSudip Mukherjee ulGPIOData &= ~(1 << g_i2cClockGPIO); 13681dee67eSSudip Mukherjee POKE32(g_i2cClkGPIODataReg, ulGPIOData); 13781dee67eSSudip Mukherjee 13881dee67eSSudip Mukherjee /* Set direction as output */ 13981dee67eSSudip Mukherjee ulGPIODirection |= (1 << g_i2cClockGPIO); 14081dee67eSSudip Mukherjee POKE32(g_i2cClkGPIODataDirReg, ulGPIODirection); 14181dee67eSSudip Mukherjee } 14281dee67eSSudip Mukherjee } 14381dee67eSSudip Mukherjee 14481dee67eSSudip Mukherjee /* 14581dee67eSSudip Mukherjee * This function set/reset the SDA GPIO pin 14681dee67eSSudip Mukherjee * 14781dee67eSSudip Mukherjee * Parameters: 14881dee67eSSudip Mukherjee * value - Bit value to set to the SCL or SDA (0 = low, 1 = high) 14981dee67eSSudip Mukherjee * 15081dee67eSSudip Mukherjee * Notes: 15181dee67eSSudip Mukherjee * When setting SCL to high, just set the GPIO as input where the pull up 15281dee67eSSudip Mukherjee * resistor will pull the signal up. Do not use software to pull up the 15381dee67eSSudip Mukherjee * signal because the i2c will fail when other device try to drive the 15481dee67eSSudip Mukherjee * signal due to SM50x will drive the signal to always high. 15581dee67eSSudip Mukherjee */ 15681dee67eSSudip Mukherjee void swI2CSDA(unsigned char value) 15781dee67eSSudip Mukherjee { 15881dee67eSSudip Mukherjee unsigned long ulGPIOData; 15981dee67eSSudip Mukherjee unsigned long ulGPIODirection; 16081dee67eSSudip Mukherjee 16181dee67eSSudip Mukherjee ulGPIODirection = PEEK32(g_i2cDataGPIODataDirReg); 162d3f431d0SIsaac Assegai if (value) { /* High */ 16381dee67eSSudip Mukherjee /* Set direction as input. This will automatically pull the signal up. */ 16481dee67eSSudip Mukherjee ulGPIODirection &= ~(1 << g_i2cDataGPIO); 16581dee67eSSudip Mukherjee POKE32(g_i2cDataGPIODataDirReg, ulGPIODirection); 166d3f431d0SIsaac Assegai } else { /* Low */ 16781dee67eSSudip Mukherjee /* Set the signal down */ 16881dee67eSSudip Mukherjee ulGPIOData = PEEK32(g_i2cDataGPIODataReg); 16981dee67eSSudip Mukherjee ulGPIOData &= ~(1 << g_i2cDataGPIO); 17081dee67eSSudip Mukherjee POKE32(g_i2cDataGPIODataReg, ulGPIOData); 17181dee67eSSudip Mukherjee 17281dee67eSSudip Mukherjee /* Set direction as output */ 17381dee67eSSudip Mukherjee ulGPIODirection |= (1 << g_i2cDataGPIO); 17481dee67eSSudip Mukherjee POKE32(g_i2cDataGPIODataDirReg, ulGPIODirection); 17581dee67eSSudip Mukherjee } 17681dee67eSSudip Mukherjee } 17781dee67eSSudip Mukherjee 17881dee67eSSudip Mukherjee /* 17981dee67eSSudip Mukherjee * This function read the data from the SDA GPIO pin 18081dee67eSSudip Mukherjee * 18181dee67eSSudip Mukherjee * Return Value: 18281dee67eSSudip Mukherjee * The SDA data bit sent by the Slave 18381dee67eSSudip Mukherjee */ 18481dee67eSSudip Mukherjee static unsigned char swI2CReadSDA(void) 18581dee67eSSudip Mukherjee { 18681dee67eSSudip Mukherjee unsigned long ulGPIODirection; 18781dee67eSSudip Mukherjee unsigned long ulGPIOData; 18881dee67eSSudip Mukherjee 18981dee67eSSudip Mukherjee /* Make sure that the direction is input (High) */ 19081dee67eSSudip Mukherjee ulGPIODirection = PEEK32(g_i2cDataGPIODataDirReg); 191d3f431d0SIsaac Assegai if ((ulGPIODirection & (1 << g_i2cDataGPIO)) != (~(1 << g_i2cDataGPIO))) { 19281dee67eSSudip Mukherjee ulGPIODirection &= ~(1 << g_i2cDataGPIO); 19381dee67eSSudip Mukherjee POKE32(g_i2cDataGPIODataDirReg, ulGPIODirection); 19481dee67eSSudip Mukherjee } 19581dee67eSSudip Mukherjee 19681dee67eSSudip Mukherjee /* Now read the SDA line */ 19781dee67eSSudip Mukherjee ulGPIOData = PEEK32(g_i2cDataGPIODataReg); 19881dee67eSSudip Mukherjee if (ulGPIOData & (1 << g_i2cDataGPIO)) 19981dee67eSSudip Mukherjee return 1; 20081dee67eSSudip Mukherjee else 20181dee67eSSudip Mukherjee return 0; 20281dee67eSSudip Mukherjee } 20381dee67eSSudip Mukherjee 20481dee67eSSudip Mukherjee /* 20581dee67eSSudip Mukherjee * This function sends ACK signal 20681dee67eSSudip Mukherjee */ 20781dee67eSSudip Mukherjee static void swI2CAck(void) 20881dee67eSSudip Mukherjee { 20981dee67eSSudip Mukherjee return; /* Single byte read is ok without it. */ 21081dee67eSSudip Mukherjee } 21181dee67eSSudip Mukherjee 21281dee67eSSudip Mukherjee /* 21381dee67eSSudip Mukherjee * This function sends the start command to the slave device 21481dee67eSSudip Mukherjee */ 2156a9df430SRicardo Ribalda Delgado static void swI2CStart(void) 21681dee67eSSudip Mukherjee { 21781dee67eSSudip Mukherjee /* Start I2C */ 21881dee67eSSudip Mukherjee swI2CSDA(1); 21981dee67eSSudip Mukherjee swI2CSCL(1); 22081dee67eSSudip Mukherjee swI2CSDA(0); 22181dee67eSSudip Mukherjee } 22281dee67eSSudip Mukherjee 22381dee67eSSudip Mukherjee /* 22481dee67eSSudip Mukherjee * This function sends the stop command to the slave device 22581dee67eSSudip Mukherjee */ 2266a9df430SRicardo Ribalda Delgado static void swI2CStop(void) 22781dee67eSSudip Mukherjee { 22881dee67eSSudip Mukherjee /* Stop the I2C */ 22981dee67eSSudip Mukherjee swI2CSCL(1); 23081dee67eSSudip Mukherjee swI2CSDA(0); 23181dee67eSSudip Mukherjee swI2CSDA(1); 23281dee67eSSudip Mukherjee } 23381dee67eSSudip Mukherjee 23481dee67eSSudip Mukherjee /* 23581dee67eSSudip Mukherjee * This function writes one byte to the slave device 23681dee67eSSudip Mukherjee * 23781dee67eSSudip Mukherjee * Parameters: 23881dee67eSSudip Mukherjee * data - Data to be write to the slave device 23981dee67eSSudip Mukherjee * 24081dee67eSSudip Mukherjee * Return Value: 24181dee67eSSudip Mukherjee * 0 - Success 24281dee67eSSudip Mukherjee * -1 - Fail to write byte 24381dee67eSSudip Mukherjee */ 2446a9df430SRicardo Ribalda Delgado static long swI2CWriteByte(unsigned char data) 24581dee67eSSudip Mukherjee { 24681dee67eSSudip Mukherjee unsigned char value = data; 24781dee67eSSudip Mukherjee int i; 24881dee67eSSudip Mukherjee 24981dee67eSSudip Mukherjee /* Sending the data bit by bit */ 250d3f431d0SIsaac Assegai for (i = 0; i < 8; i++) { 25181dee67eSSudip Mukherjee /* Set SCL to low */ 25281dee67eSSudip Mukherjee swI2CSCL(0); 25381dee67eSSudip Mukherjee 25481dee67eSSudip Mukherjee /* Send data bit */ 25581dee67eSSudip Mukherjee if ((value & 0x80) != 0) 25681dee67eSSudip Mukherjee swI2CSDA(1); 25781dee67eSSudip Mukherjee else 25881dee67eSSudip Mukherjee swI2CSDA(0); 25981dee67eSSudip Mukherjee 26081dee67eSSudip Mukherjee swI2CWait(); 26181dee67eSSudip Mukherjee 26281dee67eSSudip Mukherjee /* Toggle clk line to one */ 26381dee67eSSudip Mukherjee swI2CSCL(1); 26481dee67eSSudip Mukherjee swI2CWait(); 26581dee67eSSudip Mukherjee 26681dee67eSSudip Mukherjee /* Shift byte to be sent */ 26781dee67eSSudip Mukherjee value = value << 1; 26881dee67eSSudip Mukherjee } 26981dee67eSSudip Mukherjee 27081dee67eSSudip Mukherjee /* Set the SCL Low and SDA High (prepare to get input) */ 27181dee67eSSudip Mukherjee swI2CSCL(0); 27281dee67eSSudip Mukherjee swI2CSDA(1); 27381dee67eSSudip Mukherjee 27481dee67eSSudip Mukherjee /* Set the SCL High for ack */ 27581dee67eSSudip Mukherjee swI2CWait(); 27681dee67eSSudip Mukherjee swI2CSCL(1); 27781dee67eSSudip Mukherjee swI2CWait(); 27881dee67eSSudip Mukherjee 27981dee67eSSudip Mukherjee /* Read SDA, until SDA==0 */ 280d3f431d0SIsaac Assegai for (i = 0; i < 0xff; i++) { 28181dee67eSSudip Mukherjee if (!swI2CReadSDA()) 28281dee67eSSudip Mukherjee break; 28381dee67eSSudip Mukherjee 28481dee67eSSudip Mukherjee swI2CSCL(0); 28581dee67eSSudip Mukherjee swI2CWait(); 28681dee67eSSudip Mukherjee swI2CSCL(1); 28781dee67eSSudip Mukherjee swI2CWait(); 28881dee67eSSudip Mukherjee } 28981dee67eSSudip Mukherjee 29081dee67eSSudip Mukherjee /* Set the SCL Low and SDA High */ 29181dee67eSSudip Mukherjee swI2CSCL(0); 29281dee67eSSudip Mukherjee swI2CSDA(1); 29381dee67eSSudip Mukherjee 29481dee67eSSudip Mukherjee if (i < 0xff) 29581dee67eSSudip Mukherjee return 0; 29681dee67eSSudip Mukherjee else 2976946edd0SSupriya Karanth return -1; 29881dee67eSSudip Mukherjee } 29981dee67eSSudip Mukherjee 30081dee67eSSudip Mukherjee /* 30181dee67eSSudip Mukherjee * This function reads one byte from the slave device 30281dee67eSSudip Mukherjee * 30381dee67eSSudip Mukherjee * Parameters: 30481dee67eSSudip Mukherjee * ack - Flag to indicate either to send the acknowledge 30581dee67eSSudip Mukherjee * message to the slave device or not 30681dee67eSSudip Mukherjee * 30781dee67eSSudip Mukherjee * Return Value: 30881dee67eSSudip Mukherjee * One byte data read from the Slave device 30981dee67eSSudip Mukherjee */ 3106a9df430SRicardo Ribalda Delgado static unsigned char swI2CReadByte(unsigned char ack) 31181dee67eSSudip Mukherjee { 31281dee67eSSudip Mukherjee int i; 31381dee67eSSudip Mukherjee unsigned char data = 0; 31481dee67eSSudip Mukherjee 315d3f431d0SIsaac Assegai for (i = 7; i >= 0; i--) { 31681dee67eSSudip Mukherjee /* Set the SCL to Low and SDA to High (Input) */ 31781dee67eSSudip Mukherjee swI2CSCL(0); 31881dee67eSSudip Mukherjee swI2CSDA(1); 31981dee67eSSudip Mukherjee swI2CWait(); 32081dee67eSSudip Mukherjee 32181dee67eSSudip Mukherjee /* Set the SCL High */ 32281dee67eSSudip Mukherjee swI2CSCL(1); 32381dee67eSSudip Mukherjee swI2CWait(); 32481dee67eSSudip Mukherjee 32581dee67eSSudip Mukherjee /* Read data bits from SDA */ 32681dee67eSSudip Mukherjee data |= (swI2CReadSDA() << i); 32781dee67eSSudip Mukherjee } 32881dee67eSSudip Mukherjee 32981dee67eSSudip Mukherjee if (ack) 33081dee67eSSudip Mukherjee swI2CAck(); 33181dee67eSSudip Mukherjee 33281dee67eSSudip Mukherjee /* Set the SCL Low and SDA High */ 33381dee67eSSudip Mukherjee swI2CSCL(0); 33481dee67eSSudip Mukherjee swI2CSDA(1); 33581dee67eSSudip Mukherjee 33681dee67eSSudip Mukherjee return data; 33781dee67eSSudip Mukherjee } 33881dee67eSSudip Mukherjee 33981dee67eSSudip Mukherjee /* 34081dee67eSSudip Mukherjee * This function initializes GPIO port for SW I2C communication. 34181dee67eSSudip Mukherjee * 34281dee67eSSudip Mukherjee * Parameters: 34381dee67eSSudip Mukherjee * i2cClkGPIO - The GPIO pin to be used as i2c SCL 34481dee67eSSudip Mukherjee * i2cDataGPIO - The GPIO pin to be used as i2c SDA 34581dee67eSSudip Mukherjee * 34681dee67eSSudip Mukherjee * Return Value: 34781dee67eSSudip Mukherjee * -1 - Fail to initialize the i2c 34881dee67eSSudip Mukherjee * 0 - Success 34981dee67eSSudip Mukherjee */ 3506a9df430SRicardo Ribalda Delgado static long swI2CInit_SM750LE(unsigned char i2cClkGPIO, 3516a9df430SRicardo Ribalda Delgado unsigned char i2cDataGPIO) 35281dee67eSSudip Mukherjee { 35381dee67eSSudip Mukherjee int i; 35481dee67eSSudip Mukherjee 35581dee67eSSudip Mukherjee /* Initialize the GPIO pin for the i2c Clock Register */ 35681dee67eSSudip Mukherjee g_i2cClkGPIODataReg = GPIO_DATA_SM750LE; 35781dee67eSSudip Mukherjee g_i2cClkGPIODataDirReg = GPIO_DATA_DIRECTION_SM750LE; 35881dee67eSSudip Mukherjee 35981dee67eSSudip Mukherjee /* Initialize the Clock GPIO Offset */ 36081dee67eSSudip Mukherjee g_i2cClockGPIO = i2cClkGPIO; 36181dee67eSSudip Mukherjee 36281dee67eSSudip Mukherjee /* Initialize the GPIO pin for the i2c Data Register */ 36381dee67eSSudip Mukherjee g_i2cDataGPIODataReg = GPIO_DATA_SM750LE; 36481dee67eSSudip Mukherjee g_i2cDataGPIODataDirReg = GPIO_DATA_DIRECTION_SM750LE; 36581dee67eSSudip Mukherjee 36681dee67eSSudip Mukherjee /* Initialize the Data GPIO Offset */ 36781dee67eSSudip Mukherjee g_i2cDataGPIO = i2cDataGPIO; 36881dee67eSSudip Mukherjee 36981dee67eSSudip Mukherjee /* Note that SM750LE don't have GPIO MUX and power is always on */ 37081dee67eSSudip Mukherjee 37181dee67eSSudip Mukherjee /* Clear the i2c lines. */ 37281dee67eSSudip Mukherjee for (i = 0; i < 9; i++) 37381dee67eSSudip Mukherjee swI2CStop(); 37481dee67eSSudip Mukherjee 37581dee67eSSudip Mukherjee return 0; 37681dee67eSSudip Mukherjee } 37781dee67eSSudip Mukherjee 37881dee67eSSudip Mukherjee /* 37981dee67eSSudip Mukherjee * This function initializes the i2c attributes and bus 38081dee67eSSudip Mukherjee * 38181dee67eSSudip Mukherjee * Parameters: 38281dee67eSSudip Mukherjee * i2cClkGPIO - The GPIO pin to be used as i2c SCL 38381dee67eSSudip Mukherjee * i2cDataGPIO - The GPIO pin to be used as i2c SDA 38481dee67eSSudip Mukherjee * 38581dee67eSSudip Mukherjee * Return Value: 38681dee67eSSudip Mukherjee * -1 - Fail to initialize the i2c 38781dee67eSSudip Mukherjee * 0 - Success 38881dee67eSSudip Mukherjee */ 389f2ea7733SMike Rapoport long sm750_sw_i2c_init( 39081dee67eSSudip Mukherjee unsigned char i2cClkGPIO, 39181dee67eSSudip Mukherjee unsigned char i2cDataGPIO 39281dee67eSSudip Mukherjee ) 39381dee67eSSudip Mukherjee { 39481dee67eSSudip Mukherjee int i; 39581dee67eSSudip Mukherjee 39681dee67eSSudip Mukherjee /* Return 0 if the GPIO pins to be used is out of range. The range is only from [0..63] */ 39781dee67eSSudip Mukherjee if ((i2cClkGPIO > 31) || (i2cDataGPIO > 31)) 3986946edd0SSupriya Karanth return -1; 39981dee67eSSudip Mukherjee 40081dee67eSSudip Mukherjee if (getChipType() == SM750LE) 4016946edd0SSupriya Karanth return swI2CInit_SM750LE(i2cClkGPIO, i2cDataGPIO); 40281dee67eSSudip Mukherjee 40381dee67eSSudip Mukherjee /* Initialize the GPIO pin for the i2c Clock Register */ 40481dee67eSSudip Mukherjee g_i2cClkGPIOMuxReg = GPIO_MUX; 40581dee67eSSudip Mukherjee g_i2cClkGPIODataReg = GPIO_DATA; 40681dee67eSSudip Mukherjee g_i2cClkGPIODataDirReg = GPIO_DATA_DIRECTION; 40781dee67eSSudip Mukherjee 40881dee67eSSudip Mukherjee /* Initialize the Clock GPIO Offset */ 40981dee67eSSudip Mukherjee g_i2cClockGPIO = i2cClkGPIO; 41081dee67eSSudip Mukherjee 41181dee67eSSudip Mukherjee /* Initialize the GPIO pin for the i2c Data Register */ 41281dee67eSSudip Mukherjee g_i2cDataGPIOMuxReg = GPIO_MUX; 41381dee67eSSudip Mukherjee g_i2cDataGPIODataReg = GPIO_DATA; 41481dee67eSSudip Mukherjee g_i2cDataGPIODataDirReg = GPIO_DATA_DIRECTION; 41581dee67eSSudip Mukherjee 41681dee67eSSudip Mukherjee /* Initialize the Data GPIO Offset */ 41781dee67eSSudip Mukherjee g_i2cDataGPIO = i2cDataGPIO; 41881dee67eSSudip Mukherjee 41981dee67eSSudip Mukherjee /* Enable the GPIO pins for the i2c Clock and Data (GPIO MUX) */ 42081dee67eSSudip Mukherjee POKE32(g_i2cClkGPIOMuxReg, 42181dee67eSSudip Mukherjee PEEK32(g_i2cClkGPIOMuxReg) & ~(1 << g_i2cClockGPIO)); 42281dee67eSSudip Mukherjee POKE32(g_i2cDataGPIOMuxReg, 42381dee67eSSudip Mukherjee PEEK32(g_i2cDataGPIOMuxReg) & ~(1 << g_i2cDataGPIO)); 42481dee67eSSudip Mukherjee 42581dee67eSSudip Mukherjee /* Enable GPIO power */ 42681dee67eSSudip Mukherjee enableGPIO(1); 42781dee67eSSudip Mukherjee 42881dee67eSSudip Mukherjee /* Clear the i2c lines. */ 42981dee67eSSudip Mukherjee for (i = 0; i < 9; i++) 43081dee67eSSudip Mukherjee swI2CStop(); 43181dee67eSSudip Mukherjee 43281dee67eSSudip Mukherjee return 0; 43381dee67eSSudip Mukherjee } 43481dee67eSSudip Mukherjee 43581dee67eSSudip Mukherjee /* 43681dee67eSSudip Mukherjee * This function reads the slave device's register 43781dee67eSSudip Mukherjee * 43881dee67eSSudip Mukherjee * Parameters: 43981dee67eSSudip Mukherjee * deviceAddress - i2c Slave device address which register 44081dee67eSSudip Mukherjee * to be read from 44181dee67eSSudip Mukherjee * registerIndex - Slave device's register to be read 44281dee67eSSudip Mukherjee * 44381dee67eSSudip Mukherjee * Return Value: 44481dee67eSSudip Mukherjee * Register value 44581dee67eSSudip Mukherjee */ 44681dee67eSSudip Mukherjee unsigned char swI2CReadReg( 44781dee67eSSudip Mukherjee unsigned char deviceAddress, 44881dee67eSSudip Mukherjee unsigned char registerIndex 44981dee67eSSudip Mukherjee ) 45081dee67eSSudip Mukherjee { 45181dee67eSSudip Mukherjee unsigned char data; 45281dee67eSSudip Mukherjee 45381dee67eSSudip Mukherjee /* Send the Start signal */ 45481dee67eSSudip Mukherjee swI2CStart(); 45581dee67eSSudip Mukherjee 45681dee67eSSudip Mukherjee /* Send the device address */ 45781dee67eSSudip Mukherjee swI2CWriteByte(deviceAddress); 45881dee67eSSudip Mukherjee 45981dee67eSSudip Mukherjee /* Send the register index */ 46081dee67eSSudip Mukherjee swI2CWriteByte(registerIndex); 46181dee67eSSudip Mukherjee 46281dee67eSSudip Mukherjee /* Get the bus again and get the data from the device read address */ 46381dee67eSSudip Mukherjee swI2CStart(); 46481dee67eSSudip Mukherjee swI2CWriteByte(deviceAddress + 1); 46581dee67eSSudip Mukherjee data = swI2CReadByte(1); 46681dee67eSSudip Mukherjee 46781dee67eSSudip Mukherjee /* Stop swI2C and release the bus */ 46881dee67eSSudip Mukherjee swI2CStop(); 46981dee67eSSudip Mukherjee 47081dee67eSSudip Mukherjee return data; 47181dee67eSSudip Mukherjee } 47281dee67eSSudip Mukherjee 47381dee67eSSudip Mukherjee /* 47481dee67eSSudip Mukherjee * This function writes a value to the slave device's register 47581dee67eSSudip Mukherjee * 47681dee67eSSudip Mukherjee * Parameters: 47781dee67eSSudip Mukherjee * deviceAddress - i2c Slave device address which register 47881dee67eSSudip Mukherjee * to be written 47981dee67eSSudip Mukherjee * registerIndex - Slave device's register to be written 48081dee67eSSudip Mukherjee * data - Data to be written to the register 48181dee67eSSudip Mukherjee * 48281dee67eSSudip Mukherjee * Result: 48381dee67eSSudip Mukherjee * 0 - Success 48481dee67eSSudip Mukherjee * -1 - Fail 48581dee67eSSudip Mukherjee */ 48681dee67eSSudip Mukherjee long swI2CWriteReg( 48781dee67eSSudip Mukherjee unsigned char deviceAddress, 48881dee67eSSudip Mukherjee unsigned char registerIndex, 48981dee67eSSudip Mukherjee unsigned char data 49081dee67eSSudip Mukherjee ) 49181dee67eSSudip Mukherjee { 49281dee67eSSudip Mukherjee long returnValue = 0; 49381dee67eSSudip Mukherjee 49481dee67eSSudip Mukherjee /* Send the Start signal */ 49581dee67eSSudip Mukherjee swI2CStart(); 49681dee67eSSudip Mukherjee 49781dee67eSSudip Mukherjee /* Send the device address and read the data. All should return success 49881dee67eSSudip Mukherjee in order for the writing processed to be successful 49981dee67eSSudip Mukherjee */ 50081dee67eSSudip Mukherjee if ((swI2CWriteByte(deviceAddress) != 0) || 50181dee67eSSudip Mukherjee (swI2CWriteByte(registerIndex) != 0) || 502d3f431d0SIsaac Assegai (swI2CWriteByte(data) != 0)) { 50381dee67eSSudip Mukherjee returnValue = -1; 50481dee67eSSudip Mukherjee } 50581dee67eSSudip Mukherjee 50681dee67eSSudip Mukherjee /* Stop i2c and release the bus */ 50781dee67eSSudip Mukherjee swI2CStop(); 50881dee67eSSudip Mukherjee 50981dee67eSSudip Mukherjee return returnValue; 51081dee67eSSudip Mukherjee } 511