First, start by writing a generic kernel module. There are multiple places to look up for information but I found this link to be very useful. After you have gone through all examples specified there you can start writing your own Linux Driver Module.

Please note, that you will not get away with just copy-pasting the example code and hope it will work, no. Kernel API can sometimes change and examples will not work. Examples provided there should be looked at as a guide on how to do something. Depending on the kernel version you are using you have to modify the example in order to work.

Consider using TI platform-provided functions as much as you can, because that can really do a lot of work for you, like requesting and enabling needed clocks, buses, and power supplies. If I recall correctly you can use the functions to acquire memory-mapped address ranges for direct access to registers. I have to mention that I have a bad experience with TI-provided functions because they do not properly release/clean up all acquired resources, so for some resources, I had to call other kernel services to release them during module unload.

Edit 1:

I'm not entirely familiar with Linux SPI implementation but I would start by looking at omap2_mcspi_probe() function in drivers/spi/spi-omap2-mcspi.c file. As you can see there, it registers it's methods to Linux master SPI driver using this API: Linux/include/linux/spi/spi.h. In contrast to char driver, the main functions here are *_transfer() functions. Look up the struct descriptions in spi.h file for further details. Also, have a look at this alternative device driver API, too.

Answer from Nenad Radulovic on Stack Overflow
🌐
GitHub
github.com › rrmhearts › linux-driver-examples
GitHub - rrmhearts/linux-driver-examples: Kernel 4.10 Examples of linux drivers.. for practice and reference · GitHub
Kernel 4.10 Examples of linux drivers.. for practice and reference - rrmhearts/linux-driver-examples
Starred by 68 users
Forked by 15 users
Languages   C 92.1% | Rust 4.7% | Makefile 2.7% | Shell 0.5%
🌐
GitLab
resources.oreilly.com › examples › linux device drivers 3rd edition
examples / Linux Device Drivers 3rd Edition · GitLab
© 2026 O’Reilly Media, Inc. All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners. Terms of Service • Privacy Policy • Editorial Independence
🌐
Apriorit
apriorit.com › home › blog › software development blog › linux device drivers: tutorial for linux driver development
Linux Device Drivers: Linux Driver Development Tutorial – Apriorit
February 16, 2026 - In this tutorial, Apriorit experts provide you with step-by-step instructions on how to build a driver in Linux (starting from kernel version 6.14.0), including code samples.
🌐
GitHub
github.com › d0u9 › Linux-Device-Driver
GitHub - d0u9/Linux-Device-Driver: Advanced examples of Linux Device Drivers (LDD3) and detailed manual for running examples in QEMU which is patched with virtual PCI, USB, serial devices. I am actively composing a new book about Driver Development in Linux Kernel. · GitHub
Advanced examples of Linux Device Drivers (LDD3) and detailed manual for running examples in QEMU which is patched with virtual PCI, USB, serial devices. I am actively composing a new book about Driver Development in Linux Kernel. - GitHub - d0u9/Linux-Device-Driver: Advanced examples of Linux ...
Starred by 557 users
Forked by 167 users
Languages   C 86.0% | Shell 8.0% | Makefile 6.0%
🌐
EmbeTronicX
embetronicx.com › tutorials › linux › device-drivers › linux-device-driver-tutorial-programming
Linux Device Driver Tutorial Programming - Linux Device Driver Tutorial Part 7
October 5, 2022 - This article is a continuation of the Series on Linux Device Drivers and carries the discussion on character drivers and their implementation. The aim of this series is to provide easy and practical examples that anyone can understand.
Top answer
1 of 4
75

First, start by writing a generic kernel module. There are multiple places to look up for information but I found this link to be very useful. After you have gone through all examples specified there you can start writing your own Linux Driver Module.

Please note, that you will not get away with just copy-pasting the example code and hope it will work, no. Kernel API can sometimes change and examples will not work. Examples provided there should be looked at as a guide on how to do something. Depending on the kernel version you are using you have to modify the example in order to work.

Consider using TI platform-provided functions as much as you can, because that can really do a lot of work for you, like requesting and enabling needed clocks, buses, and power supplies. If I recall correctly you can use the functions to acquire memory-mapped address ranges for direct access to registers. I have to mention that I have a bad experience with TI-provided functions because they do not properly release/clean up all acquired resources, so for some resources, I had to call other kernel services to release them during module unload.

Edit 1:

I'm not entirely familiar with Linux SPI implementation but I would start by looking at omap2_mcspi_probe() function in drivers/spi/spi-omap2-mcspi.c file. As you can see there, it registers it's methods to Linux master SPI driver using this API: Linux/include/linux/spi/spi.h. In contrast to char driver, the main functions here are *_transfer() functions. Look up the struct descriptions in spi.h file for further details. Also, have a look at this alternative device driver API, too.

2 of 4
21

I assume your OMAP4 linux uses one of arch/arm/boot/dts/{omap4.dtsi,am33xx.dtsi} device-tree, thus it compiles drivers/spi/spi-omap2-mcspi.c (if you don't know about device-tree, read this). Then:

  • the SPI master driver is done,
  • it (most probably) registers with Linux SPI core framework drivers/spi/spi.c,
  • it (probably) works fine on your OMAP4.

You actually don't need to care about the master driver to write your slave device driver. How do I know spi-omap2-mcspi.c is a master driver? It calls spi_register_master().

SPI master, SPI slave ?

Please refer to Documentation/spi/spi_summary. The doc refers to Controller driver (master) and Protocol drivers (slave). From your description, I understand you want to write a Protocol/Device driver.

SPI protocol ?

To understand that, you need your slave device datasheet, it shall tell you:

  • the SPI mode understood by your device,
  • the protocol it expects on the bus.

Contrary to i2c, SPI does not define a protocol or handshake, SPI chips manufacturers have to define their own. So check the datasheet.

SPI mode

From include/linux/spi/spi.h:

 * @mode: The spi mode defines how data is clocked out and in.
 *  This may be changed by the device's driver.
 *  The "active low" default for chipselect mode can be overridden
 *  (by specifying SPI_CS_HIGH) as can the "MSB first" default for
 *  each word in a transfer (by specifying SPI_LSB_FIRST).

Again, check your SPI device datasheet.

An example SPI device driver?

To give you a relevant example, I need to know your SPI device type. You would understand that a SPI flash device driver is different from a SPI FPGA device driver. Unfortunately there are not so many SPI device drivers out there. To find them:

$ cd linux 
$ git grep "spi_new_device\|spi_add_device"
🌐
EmbeTronicX
embetronicx.com › tutorials › linux › device-drivers › linux-device-driver-part-1-introduction
Linux Device Driver Tutorial – Part 1 | Introduction
Understand basic concepts about Linux device drivers and practical examples for understanding device drivers in Linux.
Published   July 2, 2023
Views   0
🌐
GitHub
github.com › Scott31393 › linux_device_driver_examples
GitHub - Scott31393/linux_device_driver_examples: Linux device driver examples · GitHub
In this example we can see how to dynamically get major and minor numbers for 4 character devices driver. (Like scull example) [Linux device driver]: https://www.xml.com/ldd/chapter/book/ch03.html
Author   Scott31393
Find elsewhere
🌐
EmbeTronicX
embetronicx.com › tutorials › linux › device-drivers › usb-device-driver-example
USB Device Driver Example – Linux Device Driver Tutorial Part 34
March 8, 2024 - in this Linux Device Driver series, this is the Linux Device Driver Tutorial Part 34 – USB Device Driver Example program in the Linux Driver.
🌐
GitHub
github.com › xianrenqiu › linux-driver-samples
GitHub - xianrenqiu/linux-driver-samples: # Linux device driver samples
July 10, 2017 - # Linux device driver samples. Contribute to xianrenqiu/linux-driver-samples development by creating an account on GitHub.
Author   xianrenqiu
🌐
Linux Hint
linuxhint.com › linux-device-driver-tutorial
Linux Device Driver Tutorial For Beginners – Linux Hint
Here in this example we used command uname -r to find the current version of your system’s linux kernel. We have used option M=$(PWD) to indicate in the kernel makefile that the source for driver is in present working directory and we are specifying the word modules to tell kernel makefile to just build modules and not to build the complete kernel source code.
🌐
GitHub
github.com › makelinux › ldt
GitHub - makelinux/ldt: Linux Driver Template · GitHub
LDT project is useful for Linux driver development beginners and as starting point for a new drivers.
Starred by 540 users
Forked by 196 users
Languages   C 87.0% | Shell 9.9% | Makefile 3.1%
🌐
GitHub
github.com › Johannes4Linux › Linux_Driver_Tutorial
GitHub - Johannes4Linux/Linux_Driver_Tutorial: My new Tutorial how to get started with Linux Kernel Modules and Linux Drivers. Started in 2024 · GitHub
Here you can find examples for simple Linux Kernel Modules and Linux Drivers.
Starred by 243 users
Forked by 43 users
Languages   C 89.5% | Makefile 10.5%
🌐
Medium
medium.com › @TheLittleNaruto › writing-hello-world-driver-for-the-linux-kernel-d591a2fa214b
Writing “hello world” driver for the Linux Kernel | by Kumar Gaurav | Medium
October 26, 2019 - Few more things we need to tell in the driver code which is optional; we may skip it; but I think it is necessary. They are Author, Licence and Description. To add these; below is the sample code:
🌐
O'Reilly
oreilly.com › library › view › linux-device-drivers › 9781785280009 › 6336a283-1ae1-4e22-9a38-f39feb53738e.xhtml
Driver example - Linux Device Drivers Development [Book]
October 20, 2017 - We can summarize the concepts discussed previously in the following fake Ethernet driver: #include <linux/module.h> #include <linux/kernel.h> #include <linux/errno.h> #include <linux/init.h> #include <linux/netdevice.h> #include <linux/etherdevice.h> #include <linux/ethtool.h> #include <linux/skbuff.h> #include <linux/slab.h> #include <linux/of.h> /* For DT*/ #include <linux/platform_device.h> /* For platform devices */ struct eth_struct { int bar; int foo; struct net_device *dummy_ndev; }; static int fake_eth_open(struct net_device *dev) { printk("fake_eth_open called\n"); /* We are now ready to accept transmit requests from * the queueing layer of the networking.
Author   John Madieu
Published   2017
Pages   586
🌐
Linux Journal
linuxjournal.com › article › 2476
Writing a Linux Driver | Linux Journal
The driver reads from and writes to the hardware through ports (memory addresses where the hardware links physically), using the internal functions out_p and in_p: ... Note that these functions are not available to the user. Since the Linux kernel runs in protected mode, the low memory addresses, where the ports addresses reside, are not user accessible.
🌐
Sysplay
sysplay.github.io › books › LinuxDrivers › book › Content › Part02.html
Writing Your First Linux Driver in the Classroom | Introduction
So, then explain me about dynamic loading in Linux. You get it right and you two are excused”, professor emphasized. Pugs was more than happy. And he very well knew, how to make his professor happy – criticize Windows. So, this is what he said. As we know, a typical driver installation on Windows needs a reboot for it to get activated.
🌐
Medium
medium.com › @knownsec404team › how-to-develop-linux-driver-from-scratch-cc143e0c08a1
How to Develop Linux Driver from Scratch | by Knownsec 404 team | Medium
July 10, 2019 - The series I wrote is mainly about practice, which doesn’t talk much about theory. I learn how to develop the driver from the book Linux Device Drivers, and there is the code for the examples explained in this book on the GitHub [1].
🌐
Medium
medium.com › @mwafa2sh › write-your-own-linux-device-driver-6996ccb815db
Write your own Linux Device Driver | by Mo Aljabi | Medium
April 6, 2025 - Write your own Linux Device Driver In this article, we’ll explore how to start developing Linux kernel device drivers. Specifically, we’ll write a simple driver to control an LED on a Raspberry …
🌐
Sysplay
sysplay.github.io › books › LinuxDrivers › book › Content › Part01.html
Linux Device Drivers for Your Girl Friend | Introduction
Later, I shall show some examples of decoding data sheets as well. However, the OS-specific portion is the one which is tightly coupled with the OS mechanisms of user interfaces. This is the one which differentiates a Linux device driver from a Windows device driver from a MAC device driver.