How does linux know which I2C it is asked to use?

Hi all,

I am working on my Arrow Cyclone V SoCKit I2C demonstration program. And basically, the code first open I2C driver, then write the I2C address(0b01010011) of the external device, and then do everything necessary through I2C protocol, like below:

const char *filename = “/dev/i2c-0”;
(file = open(filename, O_RDWR)
int addr = 0b01010011;

 if (ioctl(file, I2C_SLAVE, addr) < 0) 
 {
      printf("Failed to acquire bus access and/or talk to slave.\n");
      /* ERROR HANDLING; you can check errno to see what went wrong */
      exit(1);
 }

However, I have several questions here:
1: how does linux know which I2C port is asked to work as I2C interface, and has been attached with a I2C device? There are several I2C interface on Cyclone V SoC chip, and pins of each of these interfaces can be configured as GPIO, SPI, I2C, and CAN maybe. How does it know?

One of my colleague told me there are 2 ways:
(1) Kernel can scan all I2C ports with all possible I2C addresses, and check which port has a ACK back. Then kernel can determine which port is configured with I2C device.

Sounds reasonable, and fine with I2C. But if this is the case, how can it work with SPI? There is no address and ACK scheme with SPI. Then how does kernel know which SPI port is necessary to be configured as SPI?

(2) You can assign the port configuration in u-boot. Wow, I have been working on the board for several month, and even don’t know how, and where to start with u-boot…

2: I checked the kernel image produced by Rocketboard with SD boot, there are 2 different item related to I2C driver under /sys/class, which are i2c-dev and i2c-adapter. I tried to search i2c-adapter, and didn’t find too much information about it. What is it? any difference with i2c-dev?

3:I also checked the /dev/ of my linux pc, there are i2c-0, i2c-1, i2c-2,…,i2c-7, while the Rocketboard image only has i2c-0. What is the difference behind this? What are i2c-[0:7]? 7 copies of i2c driver files? What is the big picture here?

Thanks so much for your time, and my question is a bit long.
Thanks again~~

@momoxinduop Hi! I’m having some similar issues regarding your post. Did you manage to solve/understand it?

Cheers