SPI DTS skeleton example

485 views
Skip to first unread message

bruce bushby

unread,
Aug 3, 2014, 2:45:22 AM8/3/14
to linux...@googlegroups.com
Hi

I've been wanting to write an SPI skeleton driver to learn the basics. I'm hoping a driver guru could offer some guidance.

The idea is to write a skeleton driver that simply prints the chip id

Kernel: stock standard mainline 3.16.0-rc7 kernel on Olimex A20-SOM (EVK)
Device: MPU9250 Break out board

My thinking so far:

1. DTS
Add an SPI slave device to my DTS file.

I will connect the Break out board to the second SPI bus .... so need to add the slave device to SPI1

Existing SPI1 device (from dts file)

        spi1: spi@01c06000 {
            pinctrl-names = "default";
            pinctrl-0 = <&spi1_pins_a>;
            status = "okay";
        };


Adding SPI1 slave device:

        spi1: spi@01c06000 {
            pinctrl-names = "default";
            pinctrl-0 = <&spi1_pins_a>;
            status = "okay";
 
                       
mpu9250@0 {
                                 compatible = "mpu9250";
                                 reg = <0>;
                                 spi-max-frequency = <1000000>;
                                 ???? interrupt pin .... gpio ???????
                         };

        };
2. The driver will create an entry in "sysfs" and a "corresponding" entry in /dev. Catting the device " /dev/mpu9250 " will return the device id.

Then the driver will be a standard Linux kernel module with support for SPI
....
#include <linux/module.h>
#include <linux/spi/spi.h>
....

struct spi_driver
spi_register_driver  (struct device dev;   dictates the name of the device)
spi_alloc_device
spi_add_device



Am I on the right track? Any similar simple examples floating about?


Thanks
Bruce



















jons...@gmail.com

unread,
Aug 3, 2014, 8:28:20 AM8/3/14
to linux-sunxi
I attached the Allwinner SPI driver from the 3.4 kernel. That would be
a good place to start when modifying it for mainline.

Look at other spi drivers in the mainline kernel. drivers/spi/...
They will show you how to fix probe up to work with device trees.
> --
> You received this message because you are subscribed to the Google Groups
> "linux-sunxi" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to linux-sunxi...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Jon Smirl
jons...@gmail.com
spi_sunxi.c

Maxime Ripard

unread,
Aug 3, 2014, 9:25:04 AM8/3/14
to linux...@googlegroups.com
This one is okay.

> 2. The driver will create an entry in "sysfs" and a "corresponding" entry
> in /dev. Catting the device " /dev/mpu9250 " will return the device id.
>
> Then the driver will be a standard Linux kernel module with support for SPI
> ....
> #include <linux/module.h>
> #include <linux/spi/spi.h>
> ....
>
> struct spi_driver
> spi_register_driver (struct device dev; dictates the name of the device)
> spi_alloc_device
> spi_add_device

Not quite.

You should declare a struct spi_driver, register it using
spi_register_module, and in that structure, put the compatibles you
support. You'll also have to implement the probe and remove functions,
give in that spi_driver structure pointers to these functions, and
Linux will call probe whenever a device shows up, and call remove
whenever that device disappears.

Note that all this is covered in LDD3 and
http://free-electrons.com/doc/training/linux-kernel/slides.pdf, so I'd
suggest you go read these two, like I suggested you already (or should
have)

As for how to find simple SPI drivers, I don't really have an example
in mind, but you can search for spi_register_module, you should have a
huge number of hits.

Maxime

--
Maxime Ripard, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com
signature.asc

bruce bushby

unread,
Aug 6, 2014, 4:09:59 PM8/6/14
to linux...@googlegroups.com
Thanks guys, I think I'm making some progress now.....if only I had more time :)

I found this little guy:

Only 181 lines so not too complicated, will have a go at adapting it to read the device id from my breakout board.

Also need to pop over to Emilio's repo as he was working on releasing dma enabled spi for sunxi.



Reply all
Reply to author
Forward
0 new messages