fadedbee
fadedbee

Reputation: 44739

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 i2cset/get/detect, so I know that everything is working.

The device controls LEDs (and so should appear in /sys/class/leds).

It also has GPIO (and so should appear in /sys/class/gpio).

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 /sys.
  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.)

Upvotes: 4

Views: 1320

Answers (1)

alexander
alexander

Reputation: 2753

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);

Upvotes: 2

Related Questions