xref: /OK3568_Linux_fs/kernel/Documentation/i2c/muxes/i2c-mux-gpio.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun==========================
2*4882a593SmuzhiyunKernel driver i2c-mux-gpio
3*4882a593Smuzhiyun==========================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunAuthor: Peter Korsgaard <peter.korsgaard@barco.com>
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunDescription
8*4882a593Smuzhiyun-----------
9*4882a593Smuzhiyun
10*4882a593Smuzhiyuni2c-mux-gpio is an i2c mux driver providing access to I2C bus segments
11*4882a593Smuzhiyunfrom a master I2C bus and a hardware MUX controlled through GPIO pins.
12*4882a593Smuzhiyun
13*4882a593SmuzhiyunE.G.::
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun  ----------              ----------  Bus segment 1   - - - - -
16*4882a593Smuzhiyun |          | SCL/SDA    |          |-------------- |           |
17*4882a593Smuzhiyun |          |------------|          |
18*4882a593Smuzhiyun |          |            |          | Bus segment 2 |           |
19*4882a593Smuzhiyun |  Linux   | GPIO 1..N  |   MUX    |---------------   Devices
20*4882a593Smuzhiyun |          |------------|          |               |           |
21*4882a593Smuzhiyun |          |            |          | Bus segment M
22*4882a593Smuzhiyun |          |            |          |---------------|           |
23*4882a593Smuzhiyun  ----------              ----------                  - - - - -
24*4882a593Smuzhiyun
25*4882a593SmuzhiyunSCL/SDA of the master I2C bus is multiplexed to bus segment 1..M
26*4882a593Smuzhiyunaccording to the settings of the GPIO pins 1..N.
27*4882a593Smuzhiyun
28*4882a593SmuzhiyunUsage
29*4882a593Smuzhiyun-----
30*4882a593Smuzhiyun
31*4882a593Smuzhiyuni2c-mux-gpio uses the platform bus, so you need to provide a struct
32*4882a593Smuzhiyunplatform_device with the platform_data pointing to a struct
33*4882a593Smuzhiyuni2c_mux_gpio_platform_data with the I2C adapter number of the master
34*4882a593Smuzhiyunbus, the number of bus segments to create and the GPIO pins used
35*4882a593Smuzhiyunto control it. See include/linux/platform_data/i2c-mux-gpio.h for details.
36*4882a593Smuzhiyun
37*4882a593SmuzhiyunE.G. something like this for a MUX providing 4 bus segments
38*4882a593Smuzhiyuncontrolled through 3 GPIO pins::
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun  #include <linux/platform_data/i2c-mux-gpio.h>
41*4882a593Smuzhiyun  #include <linux/platform_device.h>
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun  static const unsigned myboard_gpiomux_gpios[] = {
44*4882a593Smuzhiyun	AT91_PIN_PC26, AT91_PIN_PC25, AT91_PIN_PC24
45*4882a593Smuzhiyun  };
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun  static const unsigned myboard_gpiomux_values[] = {
48*4882a593Smuzhiyun	0, 1, 2, 3
49*4882a593Smuzhiyun  };
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun  static struct i2c_mux_gpio_platform_data myboard_i2cmux_data = {
52*4882a593Smuzhiyun	.parent		= 1,
53*4882a593Smuzhiyun	.base_nr	= 2, /* optional */
54*4882a593Smuzhiyun	.values		= myboard_gpiomux_values,
55*4882a593Smuzhiyun	.n_values	= ARRAY_SIZE(myboard_gpiomux_values),
56*4882a593Smuzhiyun	.gpios		= myboard_gpiomux_gpios,
57*4882a593Smuzhiyun	.n_gpios	= ARRAY_SIZE(myboard_gpiomux_gpios),
58*4882a593Smuzhiyun	.idle		= 4, /* optional */
59*4882a593Smuzhiyun  };
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun  static struct platform_device myboard_i2cmux = {
62*4882a593Smuzhiyun	.name		= "i2c-mux-gpio",
63*4882a593Smuzhiyun	.id		= 0,
64*4882a593Smuzhiyun	.dev		= {
65*4882a593Smuzhiyun		.platform_data	= &myboard_i2cmux_data,
66*4882a593Smuzhiyun	},
67*4882a593Smuzhiyun  };
68*4882a593Smuzhiyun
69*4882a593SmuzhiyunIf you don't know the absolute GPIO pin numbers at registration time,
70*4882a593Smuzhiyunyou can instead provide a chip name (.chip_name) and relative GPIO pin
71*4882a593Smuzhiyunnumbers, and the i2c-mux-gpio driver will do the work for you,
72*4882a593Smuzhiyunincluding deferred probing if the GPIO chip isn't immediately
73*4882a593Smuzhiyunavailable.
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunDevice Registration
76*4882a593Smuzhiyun-------------------
77*4882a593Smuzhiyun
78*4882a593SmuzhiyunWhen registering your i2c-mux-gpio device, you should pass the number
79*4882a593Smuzhiyunof any GPIO pin it uses as the device ID. This guarantees that every
80*4882a593Smuzhiyuninstance has a different ID.
81*4882a593Smuzhiyun
82*4882a593SmuzhiyunAlternatively, if you don't need a stable device name, you can simply
83*4882a593Smuzhiyunpass PLATFORM_DEVID_AUTO as the device ID, and the platform core will
84*4882a593Smuzhiyunassign a dynamic ID to your device. If you do not know the absolute
85*4882a593SmuzhiyunGPIO pin numbers at registration time, this is even the only option.
86