Alessandro
Alessandro

Reputation: 708

SPI kernel module, how to istantiate the driver?


I've been tasked to import the spi driver into an existing platform running Openwrt.
After "successfully" build the full Openwrt: packages and the kernel matching the one running into the platform, including the spidev kernel module I run into some trouble in make this module work.
**insmod** of the driver terminates without error, and I see that in the **/sys/class** the creation of the directory **spidev**, but it is empty.
Looking at the code of the spidev kernel module, the function **probe** has caught my eye. My feel is that this function is what actually allocates the minor device number an make the device available to be used. But it's not clear to me who, or what should call it.
Another doubt I have is about the architecture. The spi depends on the underlaying architecture, which in my case is the MT7620 soc which is a mipsel architecture have a specific spi code. In my understanding this SOC specific code is wrapped by the spidev kernel module and the link between these two entities should be

status = spi_register_driver(&spidev_spi_driver);

in the

static int __init spidev_init(void)

function.
Again, I'm far to be sure of what I'm writing and I'm here asking for directions.

Upvotes: 2

Views: 3849

Answers (2)

Dražen Grašovec
Dražen Grašovec

Reputation: 802

First are we talking about SPI controller driver or SPI device driver? I assume the latter.

In short spidev is just a userspace interface, a character device driver which is not aware of any underlying protocol.

spidev doesn't and cant recognize device it is communicating with, it doesn't know if it is FLASH, CODEC, EEPROM and is not protocol aware, it doesn't understand if data part it handles in address, opcode or data.

You need a specific kernel driver for your specific SPI device.

And kernel will try to match your driver with device by device-tree compatible property of device tree SPI node:

.compatible = "tcg,tpm_tis-spi"n

And if it makes a match it will then call probe() function inside driver which will initialize driver and device with data supplied in device tree SPI node.

And try to communicate with the device itself, read out ID registers to further identify device, because this driver knows the protocol used by this device.

Upvotes: 0

Federico
Federico

Reputation: 3902

First, you may want to read the Linux Device Driver - Chapter 14.

Generally speaking, in the Linux kernel you have devices and drivers on a bus (subsystem). You have to register them using the functions register_device() and register_driver() (the exact name depends on the subsystem). When a device or a driver get registered the subsystem will try to match devices with drivers. If they match, the subsystem calls the driver's function probe(). So, the execution of the probe() function can be triggered by the driver registration or the device registration. This also means that the device exists before the execution of probe()

Back to your specific case: SPI. To register a device instance you need to use spi_alloc_device and spi_add_device, or spi_new_device. Where to put this code? This depends on your need. Typically, the SPI devices are declared in some architecture file or device-tree description. Probably you can also do it in a module (not sure).

The spidev is a Linux device driver that exports the SPI raw interface to the user-space. This means that once you register an SPI device instance the driver spidev take control of it. Actually, the spidev does nothing: it waits for an user-space program to read/write data on the SPI bus. So, you will end up writing the driver for your device in userspace.

Upvotes: 1

Related Questions