linux-kernellinux-device-drivereeprom

Where does exactly eeprom file get created in Linux at24 driver


I have the following overlay inserted to Kernel:

/dts-v1/;
/plugin/;
/ {
    fragment@0 {
        target = <&i2c_arm>;
        __overlay__ {
            mem@50 {
                compatible = "atmel,24c256";
                status = "okay";
                pagesize = <64>;
                reg = <0x50>;
            };
        };
    };
};

Using these commands:

dtc -@ -I dts -O dtb -o i2c-eeprom.dtbo i2c-eeprom.dtso 
mkdir /sys/kernel/config/device-tree/overlays/i2c-eeprom
cat i2c-eeprom.dtbo > /sys/kernel/config/device-tree/overlays/i2c-eeprom/dtbo

Then I can see the following under sysfs:

mohammad@raspberrypi:~ $ tree /sys/bus/i2c/devices/i2c-1/1-0050/
/sys/bus/i2c/devices/i2c-1/1-0050/
├── 1-00502
│   ├── force_ro
│   ├── nvmem
│   ├── of_node -> ../../../../../../../firmware/devicetree/base/soc/i2c@7e804000/mem@50
│   ├── power
│   │   ├── autosuspend_delay_ms
│   │   ├── control
│   │   ├── runtime_active_time
│   │   ├── runtime_status
│   │   └── runtime_suspended_time
│   ├── subsystem -> ../../../../../../../bus/nvmem
│   ├── type
│   └── uevent
├── driver -> ../../../../../../bus/i2c/drivers/at24
├── eeprom
├── modalias
├── name
├── of_node -> ../../../../../../firmware/devicetree/base/soc/i2c@7e804000/mem@50
├── power
│   ├── autosuspend_delay_ms
│   ├── control
│   ├── runtime_active_time
│   ├── runtime_status
│   └── runtime_suspended_time
├── subsystem -> ../../../../../../bus/i2c
├── supplier:regulator:regulator.0 -> ../../../../../virtual/devlink/regulator:regulator.0--i2c:1-0050
└── uevent

10 directories, 18 files

I know the nvmem file is created by NVMEM module in Linux. Where and who creates the eeprom file? To me it seems both are serving the same purpose.


Solution

  • You answered your question already in the post. When you dumped the sysfs entry for the I²C device on bus 1 at address 0x50 (/sys/bus/i2c/devices/i2c-1/1-0050/):

    ├── 1-00502
    │   ├── force_ro
    │   ├── nvmem
    ...
    ├── driver -> ../../../../../../bus/i2c/drivers/at24
    ├── eeprom
    ├── modalias
    ├── name
    ...
    

    So, the binary file called eeprom is the one you may access via shell (dd, cat, strings, hexdump, etc...).

    The code, that is responsible for the file creation, is located in drivers/nvmem/core.c, i.e. in the nvmem_sysfs_setup_compat(). The at24 driver uses this facility via setting the respective flag to true.

    Modern way (since ca. v4.6), as suggested by Ian Abbott, is to look into different folder for a different file, i.e /sys/bus/nvmem/devices/$DEVNAME/nvmem, where $DEVNAME is a name of the respective device.