AlexKrish
Posts: 4
Joined: Tue Aug 02, 2016 10:39 am

Problem in loading I2C Driver for multiple MCP23017 chips

Tue Aug 02, 2016 11:04 am

Hello,

I am using the following I2C/GPIO Device driver to access the MCP23017 GPIOs. With the insmod command I am able to load the driver and its listed in /proc/modules. I have two MCP23017 chips connected to my Raspberry Pi. Both are detected at addresses 0x20 and 0x21. I had problems with the probe fucntion being called. After loading the overlay with the command:

Code: Select all

$ sudo dtoverlay mcp23017.dtbo
the overlay was loaded and the probe function is being called.

The mcp23017 chip is detected and I can find it under /sys/bus/i2c/devices as 1-0020 and it has a name mcp23017. But when I try the i2cdetect command it shows UU.

Before loading the overlay or after rmmod mcp23017:

Code: Select all

pi@raspberrypi:/lib/modules/4.6.4-v7 $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 21 -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

After loading the overlay:

Code: Select all

pi@raspberrypi:/lib/modules/4.6.4-v7 $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: UU 21 -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

With a rmmod mcp23017 command I see the device still under devices and i2cdetect shows 0x20.

Also I find the alias with gpio_mcp23s08:

Code: Select all

pi@raspberrypi:/lib/modules/4.6.4-v7 $ grep mcp23017 modules.alias
alias of:N*T*Cmcp,mcp23017C* gpio_mcp23s08
alias of:N*T*Cmcp,mcp23017 gpio_mcp23s08
alias of:N*T*Cmicrochip,mcp23017C* gpio_mcp23s08
alias of:N*T*Cmicrochip,mcp23017 gpio_mcp23s08
alias i2c:mcp23017 gpio_mcp23s08

How do I resolve the problem here with UU getting displayed and support more chips? Also why is there only one device under But I can see only the device at 0x20 under devices.

Code: Select all

pi@raspberrypi:/sys/bus/i2c/devices $ ls
1-0020  i2c-1
The I2C chip at 0x21 is still not detected, though the driver says it supports upto 8 chips. I am using the following driver : https://github.com/torvalds/linux/blob/ ... mcp23s08.c

As a newbie to Linux/RPi environment I am trying to understand the basics of the Kernel and device drivers. I would like to use the driver to support upto 8 MCP23017 chips. I hope I am in the right direction. Would be great if you could help me solve the problem.

christian-nils
Posts: 11
Joined: Wed Jun 22, 2016 9:27 am

Re: Problem in loading I2C Driver for multiple MCP23017 chip

Thu Aug 04, 2016 7:25 pm

Hello,

If you have UU shown on i2cdetect, it means that the probing was skipped because the address is in use by a driver ( http://linux.die.net/man/8/i2cdetect). So this is normal.
That you have only one device "detected" is also normal, in the binding that you do, you have only one address registered (if you have not changed the default values in the overlay). See the dts file of the overlay, you'll see that reg=0x20.

Where did you see that you could have up to 8 devices connected with this driver?

Hope it helps,

Greetings,

Christian-Nils

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9307
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Problem in loading I2C Driver for multiple MCP23017 chip

Thu Aug 04, 2016 9:27 pm

The driver supports up to 8 MCP23017 family chips, but the dtoverlay only supports one.

If you want to use more than one, take the existing overlay as a template, rename it, rename the mcp23017 node to mcp23017_2 or similar, renumber the reg from 0x20 to 0x21, and rebuild that overlay. Load both. The driver then sees the config for 2 chips and will probe both.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

AlexKrish
Posts: 4
Joined: Tue Aug 02, 2016 10:39 am

Re: Problem in loading I2C Driver for multiple MCP23017 chip

Fri Aug 05, 2016 9:10 am

6by9 wrote:The driver supports up to 8 MCP23017 family chips, but the dtoverlay only supports one.

If you want to use more than one, take the existing overlay as a template, rename it, rename the mcp23017 node to mcp23017_2 or similar, renumber the reg from 0x20 to 0x21, and rebuild that overlay. Load both. The driver then sees the config for 2 chips and will probe both.
Yes, I modified the overlay and all the chips are detected. Now I am trying to ahndle the GPIO Interrupts. But doesnt seem to work.

I am trying to check the interrupt handling with the gpio pins. I did the following to connect the pins to interrupt(496 is the base which is GPA0 of MCP23017):

Code: Select all

echo "496" > /sys/class/gpio/export
echo "both" > /sys/class/gpio/gpio496/edge
and then to trigger:

Code: Select all

cat gpio496/value
But when i check /proc/interrupts it always shows 0 interrupts:

Code: Select all

root@raspberrypi:/sys/class/gpio# cat /proc/interrupts
       CPU0       CPU1       CPU2       CPU3
16:   0          0          0          0   bcm2836-timer   0 Edge arch_timer
17:   41827      52799      8260     86838  bcm2836-timer   1 Edge arch_timer
23:   67          0          0          0  ARMCTRL-level   1 Edge      3f00b880.mailbox
24:  248          0          0          0  ARMCTRL-level   2 Edge      VCHIQ doorbell
46:  0            0          0          0  ARMCTRL-level  48 Edge      bcm2708_fb dma
48:  4125         0          0          0  ARMCTRL-level  50 Edge      DMA IRQ
50:  0            0          0          0  ARMCTRL-level  52 Edge      DMA IRQ
62:     415706          0          0          0  ARMCTRL-level  64 Edge      dwc_otg, dwc_otg_pcd, dwc_otg_hcd:usb1
79:          0          0          0          0  ARMCTRL-level  81 Edge      3f200000.gpio:bank0
80:          0          0          0          0  ARMCTRL-level  82 Edge      3f200000.gpio:bank1
83:        945          0          0          0  ARMCTRL-level  85 Edge      3f804000.i2c
86:        502          0          0          0  ARMCTRL-level  88 Edge      mmc0
87:       5032          0          0          0  ARMCTRL-level  89 Edge      uart-pl011
92:       6828          0          0          0  ARMCTRL-level  94 Edge      mmc1
170:         0          0          0          0  pinctrl-bcm2835   4 Edge      1-0020, 1-0021, 1-0022, 1-0023
220:         0          0          0          0  gpio-mcp23xxx   0 Edge      gpiolib
Also, when the overlay is loaded, the probe funtion is called and i get the following message:

Code: Select all

genirq: irq 170 uses trigger mode 8; requested 2
I could not figure out the problem. I am not sure if there is something wrong in the overlay file. Below is the .dts overlay file for 2 chips:

Code: Select all

// Definitions for MCP23017 Gpio Extender from Microchip Semiconductor

/dts-v1/;
/plugin/;

/ {
compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";

fragment@0 {
    target = <&i2c1>;
    __overlay__ {
        status = "okay";
    };
};

fragment@1 {
    target = <&gpio>;
    __overlay__ {
        mcp23017_pins: mcp23017_pins {
            brcm,pins = <4>;
            brcm,function = <0>;
        };
    };
};

fragment@2 {
    target = <&i2c1>;
    __overlay__ {
        #address-cells = <1>;
        #size-cells = <0>;

        mcp20:mcp23017@20 {
            compatible = "microchip,mcp23017";
            reg = <0x20>;
            gpio-controller;
            #gpio-cells = <2>;

            interrupt-parent = <&gpio>;
            interrupts = <4 2>;
            interrupt-controller;
            #interrupt-cells=<2>;
            microchip,irq-mirror;

            status = "okay";
        };
mcp21:mcp23017@21 {
                                compatible = "microchip,mcp23017";
                                reg = <0x21>;
                                gpio-controller;
                                #gpio-cells = <2>;

                                interrupt-parent = <&gpio>;
                                interrupts = <5 2>;
                                interrupt-controller;
                                #interrupt-cells=<2>;
                                microchip,irq-mirror;

                                status = "okay";
                        };
    };
};
 __overrides__ {
    gpiopin = <&mcp23017_pins>,"brcm,pins:0",
            <&mcp20>,"interrupts:0";
    addr = <&mcp20>,"reg:0";
};
};
I am not sure of the following line :

Code: Select all

interrupts = <4 2>;
Does 4 here denote the GPIO4 on the Raspberry Pi or 4th pin on the MCP23017 (GPB3) or is it GPA4? Where do I connect it to receive the interrupts? And 2 denotes the trigger mode IRQ_TYPE_EDGE_FALLING. I want it to check it for one device first and then with multiple chips. How should I modify the overlay to acheive this?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9307
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Problem in loading I2C Driver for multiple MCP23017 chip

Fri Aug 05, 2016 10:49 am

You're starting to duplicate between here viewtopic.php?f=29&t=104638&start=25#p1019624
Please choose one thread (I'd suggest the other one) and stick to it, otherwise people will be duplicating effort in responding to you.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Return to “Interfacing (DSI, CSI, I2C, etc.)”