Creating a Linux I2C Master Driver: A Step-by-Step Guide


Inter-Integrated Circuit, or I2C, is a popular communication protocol that connects a variety of devices to a system running Linux. To interface with I2C peripherals such as displays, sensors, and other devices, your Linux system needs an I2C master driver. You will be able to create an I2C master driver in the Linux kernel by following this detailed guide.


Make sure you have the following before starting to create an I2C master driver:

  1. A Linux development environment: On your development computer, you ought to have a Linux distribution installed. Popular options are Fedora, Debian, and Ubuntu.
  2. Development tools: Use the package manager in your distribution to install necessary development tools like GCC, make, and the necessary header files.
  3. Kernel source code: Obtain the source code of the Linux kernel through the package management system of your distribution or from the official webpage.

Understanding I2C:

It is important to understand the I2C protocol and how it operates before building a driver. I2C is a protocol that allows several slave devices connected to a common bus to communicate with a master, such as your Linux system. SDA (data) and SCL (clock) are the two wires used to convey messages. The master connects to each I2C device using its own address.

Identifying the Hardware:

Choose which device to communicate with and which I2C adapter to use. Use the i2cdetect command or look for the I2C adapter in /dev to locate it. Jot down the address of the gadget.

Writing the I2C Driver Module:

  1. Create a new directory for your driver code. For example:
    mkdir i2c_driver
    cd i2c_driver
  2. Make sure your driver has a C file. Although you may use any text editor, using one that is geared toward programming, such Vim or Nano, is advised:
    vim i2c_driver.c
  3. Write your I2C driver code. This code should include the following elements:
    • Include necessary kernel headers.
    • Define the I2C driver structure.
    • Implement the probe and remove functions.
    • Register the driver with the I2C subsystem.

    Here’s a simplified example of an I2C driver code:

    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/i2c.h>

    static struct i2c_driver my_i2c_driver = {
    .driver = {
    .name = "my_i2c_device",
    .of_match_table = of_match_ptr(of_device_id),
    .probe = my_i2c_probe,
    .remove = my_i2c_remove,

    static int my_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id) {
    // Driver initialization logic
    return 0;

    static int my_i2c_remove(struct i2c_client *client) {
    // Driver removal logic
    return 0;

    MODULE_DEVICE_TABLE(i2c, my_i2c_id);
    MODULE_AUTHOR("Your Name");


  4. Save and exit the text editor.

The Driver’s Compilation and Installation:

  1. In order to compile your driver, create a Makefile. Makefile should be created in the same directory, and the following material should be added to it:
    obj-m += i2c_driver.o

    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

  2. Compile the driver by running:
  3. Install the driver module:
    sudo insmod i2c_driver.ko

Examining I2C Driver:

  1. Verify that the driver module is loaded correctly by checking the kernel logs:
    dmesg | grep my_i2c_device
  2. You can now access your I2C device using standard Linux file I/O operations or create a user-space application that interacts with the driver.

Taking Out the Driver:

When you’re done using the driver, you can remove it:

sudo rmmod i2c_driver


The Linux kernel’s I2C master driver creation is a difficult undertaking that calls for a solid grasp of kernel development. Although this article offers a general overview of the procedure, driving in the real world may be far more complex. When creating I2C drivers for Linux, always refer to the kernel manual and other sources for further details and best practices.

Leave a Reply

Your email address will not be published. Required fields are marked *