kelroy
Posts: 9
Joined: Wed May 22, 2019 8:17 pm

Fuel gauge devicetree/driver integration problem

Tue Nov 05, 2019 11:55 am

Hi, i am under a project (will be full opensource, hardware too with schematics) where i want to integrate to the raspberry pi computer module a display, fuel gauge... and others peripherals, to create a retropie console. Actually i have working the PCM and Display, but i am having problems with the Fuel Gauge.

The fuel gauge its a Max17260 (https://www.maximintegrated.com/en/prod ... ml/tb_tab2), it goes trough i2c with 1 interrupt pin on address 0x36. The maxim company give the C driver and an example of dtsi, but i really dont understand how to integrate the device tree into the raspberry pi.

I sucessfully installed the Ko driver, but nothing happens, neither prints on dmesg with printk command, so i supposse that my dtb its bad. Should it, when works correctly, show ups the data on /sys/class/device?, any idea?.

Example dtb of Maxim

Code: Select all

*

max1726x_battery
~~~~~~~~~~~~~~~~~~

Required properties:
 - compatible       : should contain one of the following:
              - "maxim,MAX1726x" for MAX1726x

Optional properties:
 - maxim,rcomp0         : ModelGauge RCOMP parameter, used for
                  temperature compensation (u8);
 - maxim,model-data     : ModelGauge ModelData data,
                  Custom Model calibration data (array_u8[64]).

Example:

*/

/******************************************************************************
 *** PLATFORM-SPECIFICS
 *****************************************************************************/
///***
&maxim_miniq_gpio {
    gpiocfg0 {
        reg = <0>; // GPIO_K1
        cfg = <0>; // 0=IN, 2=OUT_LO, 3=OUT_HI
    };
};

&maxim_miniq_i2c {
    MAX1726x: [email protected] {
        status              = "okay";
        reg                 = <0x36>; // MINIQUSB I2C ADDR=1, SIZE=0

        interrupt-parent    = <&maxim_miniq_gpio>;
        interrupts          = <0>; // MINIQUSB GPIO_K1(0)
    };
};
//***/
/*****************************************************************************/

&MAX1726x {
    compatible  = "maxim,MAX1726x";

    rsense          = <10>;     /* miliOhm */
    model-option    = <1>;      /* 1: EZ Model, 2: Custom INI without model table
                                    3: Custom INI with custom model table */

    talrt-min   = <0>;          /* DegreeC */
    talrt-max   = <50>;         /* DegreeC */
    valrt-min   = <3000>;       /* mV */
    valrt-max   = <4500>;       /* mV */
    salrt-min   = <1>;          /* Percent */
    salrt-max   = <99>;         /* Percent */
    ialrt-min   = <(-5000)>;    /* mA */
    ialrt-max   = <5000>;       /* mA */


    /*** INI parameters ***/
    /* Option 1/2/3 */
    designcap   = /bits/ 16 <0x0BB8>; /* 1500 mAh */
    ichgterm    = /bits/ 16 <0x0640>;
    vempty      = /bits/ 16 <0xA561>; /* 3300 mV */
    vcharge     = <4300>; /* mV */

    /* Option 2/3 */
    learncfg    = /bits/ 16 <0xC482>;
    rcomp0      = /bits/ 16 <0x0070>;
    tempco      = /bits/ 16 <0x223E>;
    qrtable00   = /bits/ 16 <0x1050>;
    qrtable10   = /bits/ 16 <0x2013>;
    qrtable20   = /bits/ 16 <0x0B04>;
    qrtable30   = /bits/ 16 <0x0885>;

    /* Option 2 */
    dpacc       = /bits/ 16 <0x0C80>;
    modelcfg    = /bits/ 16 <0x8000>;

    /* Option 3 */
    relaxcfg    = /bits/ 16 <0x2039>;
    config      = /bits/ 16 <0x2210>;
    config2     = /bits/ 16 <0x0658>;
    fullsocthr  = /bits/ 16 <0x5F05>;
    tgain       = /bits/ 16 <0xEE56>;
    toff        = /bits/ 16 <0x1DA4>;
    curve       = /bits/ 16 <0x0025>;
    model-data  = /bits/ 16 <
        /* OCV Table */
        0x9760 0xa510 0xb100 0xb600 0xb7a0 0xb900 0xba70 0xbc70
        0xbde0 0xbfc0 0xc250 0xc510 0xc990 0xcea0 0xd040 0xd750
        /* X Table */
        0x0060 0x0120 0x0240 0x0d80 0x08b0 0x0590 0x1200 0x3210
        0x0ee0 0x0a40 0x0950 0x08e0 0x0800 0x0780 0x06b0 0x06b0
        /* RCompSeg Table */
        0x0100 0x0100 0x0100 0x0100 0x0100 0x0100 0x0100 0x0100
        0x0100 0x0100 0x0100 0x0100 0x0100 0x0100 0x0100 0x0100
    >;
};

On the dtb i write, i dont know how to add the interrupt pin on the GPIO43, and neither if its correctly write.

On compile, it show ups a warning as follows:
max1726x.dtbo: Warning (unit_address_vs_reg): Node /[email protected] has a unit name, but no reg property

Code: Select all

/dts-v1/;
/plugin/;

/* Kelboy2.0 device tree for the fuel gague*/


/{
        compatible = "brcm,bcm2708";
        [email protected] {
                  target = <&i2c_arm>;
                  __overlay__ {
                              #address-cells = <1>;
                              #size-cells = <0>;
                              status = "okay";
                              [email protected] {
                                        compatible = "maxim,MAX1726x";
                                        reg = <0x36>;
                                        rsense          = <10>;     /* miliOhm */
                                        model-option    = <1>;      /* Model 1, 2 or 3*/
                                        talrt-min   = <0>;          /* DegreeC */
                                        talrt-max   = <50>;         /* DegreeC */
                                        valrt-min   = <3000>;       /* mV */
                                        valrt-max   = <4500>;       /* mV */
                                        salrt-min   = <1>;          /* Percent */
                                        salrt-max   = <99>;         /* Percent */
                                        ialrt-min   = <(-5000)>;    /* mA */
                                        ialrt-max   = <5000>;       /* mA */


                                        /*** INI parameters ***/
                                        /* Option 1/2/3 */
                                        designcap   = /bits/ 16 <0x4E20>; /* 10000 mAh */
                                        ichgterm    = /bits/ 16 <0x0640>;
                                        vempty      = /bits/ 16 <0xA561>; /* 3300 mV */
                                        vcharge     = <4300>; /* mV */

                                        status = "okay";
                                        };
                              };
                };
};


I am trying to look for a book or something to learn the device tree knowledge... but seems to be hard, i se people like @PhilE helping on this threads. Thanks to all who can help.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2589
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Fuel gauge devicetree/driver integration problem

Tue Nov 05, 2019 12:37 pm

The first step is to confirm whether your overlay is applying correctly to the base dtb. You can do this by typing "sudo vcdbg log msg" and looking for errors from the firmware. I think you will see errors because you appear to have misspelled "fragment". You can also verify if the overlay is present from Linux using something like "dtc -I fs /proc/device-tree | grep MAX1726x".

Step 2 would be to confirm that the driver is loading, but that will depend on whether it is a proper in-kernel driver or a userspace application that uses /dev/[email protected] to talk to the fuel gauge.

kelroy
Posts: 9
Joined: Wed May 22, 2019 8:17 pm

Re: Fuel gauge devicetree/driver integration problem

Tue Nov 05, 2019 2:00 pm

PhilE wrote:
Tue Nov 05, 2019 12:37 pm
The first step is to confirm whether your overlay is applying correctly to the base dtb. You can do this by typing "sudo vcdbg log msg" and looking for errors from the firmware. I think you will see errors because you appear to have misspelled "fragment". You can also verify if the overlay is present from Linux using something like "dtc -I fs /proc/device-tree | grep MAX1726x".

Step 2 would be to confirm that the driver is loading, but that will depend on whether it is a proper in-kernel driver or a userspace application that uses /dev/[email protected] to talk to the fuel gauge.
Actually i recompile the full kernel and add the Module, after this install the ko module a /sys/module/max1726x_battery appears, and no errors on dmesg.

About the device tree, i have the following information:

Code: Select all

012889.077: brfs: File read: 941 bytes
012893.672: brfs: File read: /mfs/sd/overlays/max1726x.dtbo
012903.889: Loaded overlay 'max1726x'
013921.222: Device tree loaded to 0x2eff9b00 (size 0x648b)
So it seems that all its going well... but i dont know how to exactly read the information, or if its working well. I gonna modify the .c of the driver to print some messages. But i still think that the driver isnt getting the parameters correctly from the dtbo.

I could post the Max1726X.c driver without any problem, if it helps.

Regards, Carlos.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2589
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Fuel gauge devicetree/driver integration problem

Tue Nov 05, 2019 2:11 pm

As I said above, "you appear to have misspelled "fragment". Without fixing that I think the firmware may treat your overlay as being empty. Please fix the typo and run "dtc -I fs /proc/device-tree | grep MAX1726x" to confirm that the overlay has been applied.

kelroy
Posts: 9
Joined: Wed May 22, 2019 8:17 pm

Re: Fuel gauge devicetree/driver integration problem

Tue Nov 05, 2019 2:46 pm

PhilE wrote:
Tue Nov 05, 2019 2:11 pm
As I said above, "you appear to have misspelled "fragment". Without fixing that I think the firmware may treat your overlay as being empty. Please run fix the typo and run "dtc -I fs /proc/device-tree | grep MAX1726x" to confirm that the overlay has been applied.

Oh, i really didint see that error,thanks PhilE. Fixed, now the driver send some kernel message, but it need the interrupts pin to work properly.

I am trying to investigate how godix do it to understand how to write it on the dtbs. My interrupt pin its the GPIO43 with a PullUp, interrupt on Low condition(falling edge).

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2589
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Fuel gauge devicetree/driver integration problem

Tue Nov 05, 2019 2:50 pm

You'll need to include something like this:

Code: Select all

        interrupt-parent = <&gpio>;
        interrupts = <43 2>; // falling edge triggered

kelroy
Posts: 9
Joined: Wed May 22, 2019 8:17 pm

Re: Fuel gauge devicetree/driver integration problem

Tue Nov 05, 2019 3:29 pm

PhilE wrote:
Tue Nov 05, 2019 2:50 pm
You'll need to include something like this:

Code: Select all

        interrupt-parent = <&gpio>;
        interrupts = <43 2>; // falling edge triggered
I understand that the interrupt-parent calls a gpio fragment, and this its made in other dtbs of the raspberry, right?. We just evoque the node to add our interrupt.

With this part PhilE all its working properly, the error on "fragment" word.... hahaha, so much hours of work i think.

The final dtbs its as follows, if maybe somebody need it as an example:

Code: Select all

dts-v1/;
/plugin/;

/* Kelboy2.0 project, device tree for the fuel gague*/


/{

        compatible = "brcm,bcm2708";
        [email protected] {
                   target = <&i2c_arm>;
                   __overlay__ {
                               status = "okay";
                               #address-cells = <1>;
                               #size-cells = <0>;
                               [email protected] {
                                        compatible = "maxim,MAX1726x";
                                        reg = <0x36>;
                                        interrupt-parent = <&gpio>;
                                        interrupts = <43 2>;
                                        rsense          = <10>;     /* miliOhm */
                                        clock-frequency = <400000>;
                                        model-option    = <1>;      /* Model 1, 2 or 3*/
                                        talrt-min   = <0>;          /* DegreeC */
                                        talrt-max   = <50>;         /* DegreeC */
                                        valrt-min   = <3000>;       /* mV */
                                        valrt-max   = <4500>;       /* mV */
                                        salrt-min   = <1>;          /* Percent */
                                        salrt-max   = <99>;         /* Percent */
                                        ialrt-min   = <(-5000)>;    /* mA */
                                        ialrt-max   = <5000>;       /* mA */


                                        /*** INI parameters ***/
                                        /* Option 1/2/3 */
                                        designcap   = /bits/ 16 <0x4E20>; /* 10000 mAh */
                                        ichgterm    = /bits/ 16 <0x0640>;
                                        vempty      = /bits/ 16 <0xA561>; /* 3300 mV */
                                        vcharge     = <4200>; /* mV */


                                        };
                              };
                };
};

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2589
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Fuel gauge devicetree/driver integration problem

Tue Nov 05, 2019 3:37 pm

The "interrupt-parent" property says "from this point downwards in the hierarchy, any interrupt declarations are aimed at the DT node with this label." The default "interrupt-parent" is declared with "interrupt-parent = <gicv2>;" on a Pi 4B, and "interrupt-parent = <intc>;" on older Pis.

kelroy
Posts: 9
Joined: Wed May 22, 2019 8:17 pm

Re: Fuel gauge devicetree/driver integration problem

Wed Nov 06, 2019 10:54 am

Thanks PhilE, its hard to understand the dtb... i will continue to read and improve it.

After a few more test, the fuel gauge its working perfectly. Thanks for your help¡¡.

Return to “Device Tree”