chrisdew chrisdew - 11 months ago 45
C Question

Writing kernel module(s) for a multi-function i2c device - one or many?

I have an i2c device, built into the board I am developing for. It responds to a single address, but the first byte (sometimes called "command" or "register") dictates the function addressed.

I can use the device from userspace with

, so I know that everything is working.

The device controls LEDs (and so should appear in

It also has GPIO (and so should appear in

There are a further half-dozen different places where parts of the device's functionality should appear.

Should I code this as:

  1. One i2c client module, and find a way to get things listed in the right places in

  2. One platform module and many driver modules, all sharing the single i2c device (somehow) with their module code in function-specific places.

(This is my first kernel module since late-2.4 or early-2.6, it was a long time ago.)

Answer Source

In linux sources take a look at wm8350 module. It consist of one core module bound to i2c bus and many child platform modules that uses core module API to access chip registers.

Core module consist of:

GPIO module consist of:

During core module initialization wm8350_i2c_probe() get called. It calls wm8350_device_init(). It create child platform devices using wm8350_client_dev_register().

Child module drivers/gpio/gpio-wm8350.c registers as module for "platform:wm8350-gpio" and it's entry point is wm8350_gpio_probe().

In order to get access to core module, GPIO module do:

107 static int wm8350_gpio_probe(struct platform_device *pdev)
108 {
109         struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent);

And then it calls something like

36         return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
37                                1 << offset);