odroid-c4:application_note:gpio:i2c

'ODROID-N2' on this page refers to the ODROID-N2 series (N2, N2+, N2L).

ODROID-N2/C4 I2C Controller(Device Node) Information

In ODROID-N2/C4, there are two I2C controllers, I2C0 and I2C1.

Here is the I2C controller information for our Ubuntu image.

Pin Number SDA(#GPIO) SCL(#GPIO) Default Speed Max Speed Device Node
3(SDA), 5(SCL) GPIOX.17(#493) GPIOX.18(#494) 400 KHz 2 MHz /dev/i2c-0
27(SDA), 28(SCL) GPIOA.14(#474) GPIOA.15(#475) 100 KHz 2 MHz /dev/i2c-1

DTBO has been introduced in our ubuntu 20.04 official image. Follow the Ubuntu 20.04 guide if you use it, otherwise follow Ubuntu 18.04 guide.
See this link for more details of Device Tree Overlay.

If you are using ODROID-C4 and the image “ubuntu-20.04-4.9-minimal-odroid-c4-20200526.img.xz” or before, you should upgrade as follow.

odroid@odroid:~$ uname -r
4.9.218-25
odroid@odroid:~$ sudo sed -i 's/bionic/focal/g' /etc/apt/sources.list.d/odroid.list
odroid@odroid:~$ cat /etc/apt/sources.list.d/odroid.list
deb http://deb.odroid.in/c4/ focal main
odroid@odroid:~$ sudo apt update && sudo apt upgrade

Reboot when all the upgrade is done.

odroid@odroid:~$ sudo reboot
odroid@odroid:~$ uname -r
4.9.230-32

If you are using Ubuntu 18.04 or lower, you have to follow this link.

By default, the two I2C buses are enabled without any edits.
Check your DT Overlays file in “/media/boot/config.ini”

odroid@odroid:~$ sudo vi /media/boot/config.ini
; Device Tree Overlay
overlay_resize=16384
overlay_profile=hktft32
overlays="spi0 i2c0 i2c1 uart0"

Add the “i2c0” or “i2c1” you want in “overlays=” if they are absent.
After reboot, you can find the I2C node with this command.

odroid@odroid:~$ ls /dev/i2c*
/dev/i2c-0  /dev/i2c-1

If you want to use Pin #3, #5, #27, #28 in 40Pin as GPIO, you have to disable the I2C function first.
Open the “config.ini” file in “/media/boot”.

odroid@odroid:~$ sudo vi /media/boot/config.ini

And then, remove the i2c you want to disable.
This config.ini means that the i2c0 is disabled.

; Device Tree Overlay
overlay_resize=16384
overlay_profile=hktft32
overlays="spi0 i2c1 uart0"

After reboot, you can check the “/dev/” node.

odroid@odroid:~$ ls /dev/i2c*
/dev/i2c-1

You can change speed of each I2C node by editing /sys/bus/i2c/devices/i2c-{0,1}/device/speed file.

target
# Change user to root
odroid@odroid:~$ su root
 
# To change I2C0 clock from 400KHz to 100KHz
root@odroid:~# cat /sys/bus/i2c/devices/i2c-0/device/speed
400000
root@odroid:~# echo 100000 > /sys/bus/i2c/devices/i2c-0/device/speed
root@odroid:~# cat /sys/bus/i2c/devices/i2c-0/device/speed
100000
target
# To change I2C1 clock from 100KHz to 400KHz
root@odroid:~# cat /sys/bus/i2c/devices/i2c-1/device/speed
100000
root@odroid:~# echo 400000 > /sys/bus/i2c/devices/i2c-1/device/speed
root@odroid:~# cat /sys/bus/i2c/devices/i2c-1/device/speed
400000

Note that the device node numbers vary depending on the kernel version.
Please make sure that the kernel version you are using.

odroid@odroid:~$ uname -r
4.9.230-89

And then, refer to the node number of "I2C Pin information table"

Pin Number SDA(#GPIO) SCL(#GPIO) Default Speed Max Speed Device Node(kernel 4.9.230-73 or later) Device Node(kernel 4.9.219-72 or lower)
3(SDA), 5(SCL) GPIOX.17(#493) GPIOX.18(#494) 400 KHz 2 MHz /dev/i2c-0 /dev/i2c-2
27(SDA), 28(SCL) GPIOA.14(#474) GPIOA.15(#475) 100 KHz 2 MHz /dev/i2c-1 /dev/i2c-3

By default, ODROID-N2/C4 are configured to load I2C drivers and you can find two I2C buses, I2C0 and I2C1, In order to switch them to generic GPIOs, fdtput command is required and this command can be installed with the package device-tree-compiler.

root@odroid:~# apt install device-tree-compiler

The default configuration is for I2C buses and can be checked with fdtget command. In order to apply the change, the changed device tree file must be located in the boot directory and ODROID-N2/C4 must be rebooted.

ODROID-N2

I2C0

root@odroid:~# fdtget /media/boot/meson64_odroidn2.dtb /soc/cbus@ffd00000/i2c@1d000 status
okay

I2C1

root@odroid:~# fdtget /media/boot/meson64_odroidn2.dtb /soc/cbus@ffd00000/i2c@1c000 status
okay

They can be switched to GPIO from I2C when the value of status is disabled.

I2C0

root@odroid:~# fdtput -t s /media/boot/meson64_odroidn2.dtb /soc/cbus@ffd00000/i2c@1d000 status disabled

I2C1

root@odroid:~# fdtput -t s /media/boot/meson64_odroidn2.dtb /soc/cbus@ffd00000/i2c@1c000 status disabled

I2C0

root@odroid:~# fdtput -t s /media/boot/meson64_odroidn2.dtb /soc/cbus@ffd00000/i2c@1d000 status okay

I2C1

root@odroid:~# fdtput -t s /media/boot/meson64_odroidn2.dtb /soc/cbus@ffd00000/i2c@1c000 status okay
ODROID-C4

I2C0

root@odroid:~# fdtget /media/boot/meson64_odroidc4.dtb /soc/cbus@ffd00000/i2c@1d000 status
okay

I2C1

root@odroid:~# fdtget /media/boot/meson64_odroidc4.dtb /soc/cbus@ffd00000/i2c@1c000 status
okay

They can be switched to GPIO from I2C when the value of status is disabled.

I2C0

root@odroid:~# fdtput -t s /media/boot/meson64_odroidc4.dtb /soc/cbus@ffd00000/i2c@1d000 status disabled

I2C1

root@odroid:~# fdtput -t s /media/boot/meson64_odroidc4.dtb /soc/cbus@ffd00000/i2c@1c000 status disabled

I2C0

root@odroid:~# fdtput -t s /media/boot/meson64_odroidc4.dtb /soc/cbus@ffd00000/i2c@1d000 status okay

I2C1

root@odroid:~# fdtput -t s /media/boot/meson64_odroidc4.dtb /soc/cbus@ffd00000/i2c@1c000 status okay

After rebooting, your change will be applied and if i2c_meson_master is loaded then I2C bus 0 or 1 are running as I2C buses, this can be checked with the command lsmod command.

root@odroid:~# lsmod | grep i2c
i2c_meson_master       20480  0

You can unload the module if you want to use I2C gripped pins as on GPIO mode.

target
# Unload
root@odroid:~# modprobe -r i2c_meson_master
root@odroid:~# ll /dev/i2c*
ls: cannot access '/dev/i2c*': No such file or directory
 
# Load
root@odroid:~# modprobe i2c_meson_master
root@odroid:~# ll /dev/i2c*
crw-rw---- 1 root i2c 89, 2 Jan 11 12:09 /dev/i2c-0
crw-rw---- 1 root i2c 89, 3 Jan 11 12:09 /dev/i2c-1

Check your I2C node number depending on your kernel version.
checking_kernel_version_for_i2c_node_number

In ODROID-N2/C4, I2C devices are mapped in /sys/bus/i2c/devices directory.

target
root@odroid:~# ll /sys/bus/i2c/devices
total 0
drwxr-xr-x 2 root root 0 Jan  1  1970 ./
drwxr-xr-x 4 root root 0 Jan  1  1970 ../
lrwxrwxrwx 1 root root 0 Jan 29  2018 1-0051 -> ../../../devices/platform/soc/ffd00000.cbus/ffd1c000.i2c/i2c-1/1-0051/
lrwxrwxrwx 1 root root 0 Jan 11 10:00 i2c-0 -> ../../../devices/platform/soc/ffd00000.cbus/ffd1d000.i2c/i2c-0/
lrwxrwxrwx 1 root root 0 Jan 11 10:01 i2c-1 -> ../../../devices/platform/soc/ffd00000.cbus/ffd1c000.i2c/i2c-1/

As an example, see the files in /sys/bus/i2c/devices/i2c-0 directory.

target
root@odroid:~# ll /sys/bus/i2c/devices/i2c-0/
total 0
drwxr-xr-x 4 root root    0 Jan 29  2018 ./
drwxr-xr-x 4 root root    0 Jan  1  1970 ../
--w------- 1 root root 4096 Jan 11 10:01 delete_device
lrwxrwxrwx 1 root root    0 Jan 11 10:00 device -> ../../ffd1d000.i2c/
drwxr-xr-x 3 root root    0 Jan 29  2018 i2c-dev/
-r--r--r-- 1 root root 4096 Jan 10 15:52 name
--w------- 1 root root 4096 Jan 11 10:01 new_device
lrwxrwxrwx 1 root root    0 Jan 11 10:01 of_node -> '../../../../../../firmware/devicetree/base/soc/cbus@ffd00000/i2c@1d000'/
drwxr-xr-x 2 root root    0 Jan 11 10:01 power/
lrwxrwxrwx 1 root root    0 Jan 29  2018 subsystem -> ../../../../../../bus/i2c/
-rw-r--r-- 1 root root 4096 Jan 29  2018 uevent

To know its properties in device tree, see of_node directory.

target
root@odroid:~# ll /sys/bus/i2c/devices/i2c-0/of_node/
total 0
-r--r--r--  1 root root  4 Jan 11 10:07 '#address-cells'
-r--r--r--  1 root root  4 Jan 11 10:07 '#size-cells'
drwxr-xr-x  2 root root  0 Jan 11 10:01  ./
drwxr-xr-x 13 root root  0 Jan 11 10:01  ../
-r--r--r--  1 root root  4 Jan 11 10:07  clock-frequency
-r--r--r--  1 root root  8 Jan 11 10:07  clock-names
-r--r--r--  1 root root  8 Jan 11 10:07  clocks
-r--r--r--  1 root root 23 Jan 11 10:07  compatible
-r--r--r--  1 root root 24 Jan 11 10:07  interrupts
-r--r--r--  1 root root  4 Jan 11 10:07  name
-r--r--r--  1 root root  4 Jan 11 10:07  phandle
-r--r--r--  1 root root  4 Jan 11 10:07  pinctrl-0
-r--r--r--  1 root root  4 Jan 11 10:07  pinctrl-1
-r--r--r--  1 root root 21 Jan 11 10:07  pinctrl-names
-r--r--r--  1 root root 16 Jan 11 10:07  reg
-r--r--r--  1 root root  5 Jan 11 10:07  status

Check your I2C node number depending on your kernel version.
checking_kernel_version_for_i2c_node_number

You can change speed of each I2C node by editing /sys/bus/i2c/devices/i2c-{0,1}/device/speed file.

target
# To change I2C0 clock from 400KHz to 100KHz
root@odroid:~# cat /sys/bus/i2c/devices/i2c-0/device/speed
400000
root@odroid:~# echo 100000 > /sys/bus/i2c/devices/i2c-0/device/speed
root@odroid:~# cat /sys/bus/i2c/devices/i2c-0/device/speed
100000
target
# To change I2C1 clock from 100KHz to 400KHz
root@odroid:~# cat /sys/bus/i2c/devices/i2c-1/device/speed
100000
root@odroid:~# echo 400000 > /sys/bus/i2c/devices/i2c-1/device/speed
root@odroid:~# cat /sys/bus/i2c/devices/i2c-1/device/speed
400000
2020/08/20 09:57 · john1117
  • odroid-c4/application_note/gpio/i2c.txt
  • Last modified: 2020/08/21 15:05
  • by john1117