How do I Create an I2c Linux Input Device Driver?

Written by christopher hanson
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How do I Create an I2c Linux Input Device Driver?
An input device with limited message types such as a mouse is ideal for the I2c bus. (3D Maus Seite 2 image by Flo from

The I2c (Intel-Integrated Circuit) is a type of protocol called a bus which is used by computer motherboards to communicate with devices. The I2c protocol is antiquated, but the protocol remains popular among amateur hardware builders because it is easy to implement. Device drivers in Linux interpret I2c calls via the I2c-core which is included in most Linux distributions. Writing a driver for an I2c input device on Linux requires the programmer to import the I2c-core and initiate the device. The device name and address should be provided by the device manufacturer.

Skill level:


  1. 1

    Launch the terminal by pressing "Ctrl," "Alt," and "t" simultaneously. The terminal command prompt window will appear on the screen.

  2. 2

    Install the i2c-core if it is not already available on the system. At the command prompt type "sudo apt-get install i2c-core", then press "Enter." The terminal will prompt for the root password, type the password and press "Enter." The terminal will install the i2c-core.

  3. 3

    Change directories to the code bin. For example, if your code bin is held at "/home/bin" type "cd /home/bin" at the command prompt and press "Enter."

  4. 4

    Launch "Gedit." Type "gedit" at the command prompt and press "Enter."

  5. 5

    Copy the following code to the Gedit window:

    include i2c-core.h

    static struct i2c_device_id <UNIQUE PREFIX>_idtable[] = {

        { "&lt;UNIQUE PREFIX&gt;", &lt;DEVICE ID&gt; },



    static struct i2c_driver <UNIQUE PREFIX>_driver = {

        .driver = {
                .name   = "&lt;UNIQUE PREFIX&gt;",
        .id_table       = &lt;UNIQUE PREFIX&gt;_ids,
        .probe          = &lt;UNIQUE PREFIX&gt;_probe,
        .remove         = &lt;UNIQUE PREFIX&gt;_remove,


    static int __init <UNIQUE PREFIX>_init(void)


        return i2c_add_driver(&amp;&lt;UNIQUE PREFIX&gt;_driver);


    static void __exit <UNIQUE PREFIX>_cleanup(void)




    <UNIQUE PREFIX>_init();

    Replace <DEVICE ID> with the ID of the device as prescribed by the manufacturer. Replace <UNIQUE PREFIX> with a relevant name for your driver. The prefix should consist of lower case letters and no spaces. Use the same prefix everywhere that <UNIQUE PREFIX> occurs.

  6. 6

    Save the code by clicking "File," "Save." Name it the same as your <UNIQUE PREFIX> from the previous Step with the extension ".c". For example, if your <UNIQUE PREFIX>" was "iicmouse" call the file "iicmouse.c".

  7. 7

    Close the "gedit" window by clicking the "x" in the upper right hand corner.

  8. 8

    Compile the device driver. At the command prompt, type: "gcc -c ./<DRIVER FILE>" replace <DRIVER FILE> with the name of the file you just created then press "Enter." The driver will compile.

  9. 9

    Plug an I2c input device into an I2c port.

  10. 10

    Type the name of the device driver that you just created at the command prompt, then press "Enter." The device will initiate.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.