Blame view

Documentation/i2c/instantiating-devices 8.56 KB
764c16918   Jean Delvare   i2c: Document the...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  How to instantiate I2C devices
  ==============================
  
  Unlike PCI or USB devices, I2C devices are not enumerated at the hardware
  level. Instead, the software must know which devices are connected on each
  I2C bus segment, and what address these devices are using. For this
  reason, the kernel code must instantiate I2C devices explicitly. There are
  several ways to achieve this, depending on the context and requirements.
  
  
  Method 1: Declare the I2C devices by bus number
  -----------------------------------------------
  
  This method is appropriate when the I2C bus is a system bus as is the case
  for many embedded systems. On such systems, each I2C bus has a number
  which is known in advance. It is thus possible to pre-declare the I2C
  devices which live on this bus. This is done with an array of struct
  i2c_board_info which is registered by calling i2c_register_board_info().
  
  Example (from omap2 h4):
  
  static struct i2c_board_info __initdata h4_i2c_board_info[] = {
  	{
  		I2C_BOARD_INFO("isp1301_omap", 0x2d),
  		.irq		= OMAP_GPIO_IRQ(125),
  	},
  	{	/* EEPROM on mainboard */
  		I2C_BOARD_INFO("24c01", 0x52),
  		.platform_data	= &m24c01,
  	},
  	{	/* EEPROM on cpu card */
  		I2C_BOARD_INFO("24c01", 0x57),
  		.platform_data	= &m24c01,
  	},
  };
  
  static void __init omap_h4_init(void)
  {
  	(...)
  	i2c_register_board_info(1, h4_i2c_board_info,
  			ARRAY_SIZE(h4_i2c_board_info));
  	(...)
  }
  
  The above code declares 3 devices on I2C bus 1, including their respective
  addresses and custom data needed by their drivers. When the I2C bus in
  question is registered, the I2C devices will be instantiated automatically
  by i2c-core.
  
  The devices will be automatically unbound and destroyed when the I2C bus
  they sit on goes away (if ever.)
  
  
  Method 2: Instantiate the devices explicitly
  --------------------------------------------
  
  This method is appropriate when a larger device uses an I2C bus for
  internal communication. A typical case is TV adapters. These can have a
  tuner, a video decoder, an audio decoder, etc. usually connected to the
  main chip by the means of an I2C bus. You won't know the number of the I2C
  bus in advance, so the method 1 described above can't be used. Instead,
  you can instantiate your I2C devices explicitly. This is done by filling
  a struct i2c_board_info and calling i2c_new_device().
  
  Example (from the sfe4001 network driver):
  
  static struct i2c_board_info sfe4001_hwmon_info = {
  	I2C_BOARD_INFO("max6647", 0x4e),
  };
  
  int sfe4001_init(struct efx_nic *efx)
  {
  	(...)
  	efx->board_info.hwmon_client =
  		i2c_new_device(&efx->i2c_adap, &sfe4001_hwmon_info);
  
  	(...)
  }
  
  The above code instantiates 1 I2C device on the I2C bus which is on the
  network adapter in question.
  
  A variant of this is when you don't know for sure if an I2C device is
  present or not (for example for an optional feature which is not present
  on cheap variants of a board but you have no way to tell them apart), or
  it may have different addresses from one board to the next (manufacturer
  changing its design without notice). In this case, you can call
  i2c_new_probed_device() instead of i2c_new_device().
  
  Example (from the pnx4008 OHCI driver):
  
  static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
  
  static int __devinit usb_hcd_pnx4008_probe(struct platform_device *pdev)
  {
  	(...)
  	struct i2c_adapter *i2c_adap;
  	struct i2c_board_info i2c_info;
  
  	(...)
  	i2c_adap = i2c_get_adapter(2);
  	memset(&i2c_info, 0, sizeof(struct i2c_board_info));
6ced9e6b3   Roman Fietze   i2c: Fix typo in ...
103
  	strlcpy(i2c_info.type, "isp1301_pnx", I2C_NAME_SIZE);
764c16918   Jean Delvare   i2c: Document the...
104
  	isp1301_i2c_client = i2c_new_probed_device(i2c_adap, &i2c_info,
9a94241af   Jean Delvare   i2c: Add support ...
105
  						   normal_i2c, NULL);
764c16918   Jean Delvare   i2c: Document the...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  	i2c_put_adapter(i2c_adap);
  	(...)
  }
  
  The above code instantiates up to 1 I2C device on the I2C bus which is on
  the OHCI adapter in question. It first tries at address 0x2c, if nothing
  is found there it tries address 0x2d, and if still nothing is found, it
  simply gives up.
  
  The driver which instantiated the I2C device is responsible for destroying
  it on cleanup. This is done by calling i2c_unregister_device() on the
  pointer that was earlier returned by i2c_new_device() or
  i2c_new_probed_device().
  
  
  Method 3: Probe an I2C bus for certain devices
  ----------------------------------------------
  
  Sometimes you do not have enough information about an I2C device, not even
  to call i2c_new_probed_device(). The typical case is hardware monitoring
  chips on PC mainboards. There are several dozen models, which can live
  at 25 different addresses. Given the huge number of mainboards out there,
  it is next to impossible to build an exhaustive list of the hardware
  monitoring chips being used. Fortunately, most of these chips have
  manufacturer and device ID registers, so they can be identified by
  probing.
  
  In that case, I2C devices are neither declared nor instantiated
  explicitly. Instead, i2c-core will probe for such devices as soon as their
  drivers are loaded, and if any is found, an I2C device will be
  instantiated automatically. In order to prevent any misbehavior of this
  mechanism, the following restrictions apply:
  * The I2C device driver must implement the detect() method, which
    identifies a supported device by reading from arbitrary registers.
  * Only buses which are likely to have a supported device and agree to be
    probed, will be probed. For example this avoids probing for hardware
    monitoring chips on a TV adapter.
  
  Example:
  See lm90_driver and lm90_detect() in drivers/hwmon/lm90.c
  
  I2C devices instantiated as a result of such a successful probe will be
  destroyed automatically when the driver which detected them is removed,
  or when the underlying I2C bus is itself destroyed, whichever happens
  first.
  
  Those of you familiar with the i2c subsystem of 2.4 kernels and early 2.6
  kernels will find out that this method 3 is essentially similar to what
  was done there. Two significant differences are:
  * Probing is only one way to instantiate I2C devices now, while it was the
    only way back then. Where possible, methods 1 and 2 should be preferred.
    Method 3 should only be used when there is no other way, as it can have
    undesirable side effects.
  * I2C buses must now explicitly say which I2C driver classes can probe
    them (by the means of the class bitfield), while all I2C buses were
    probed by default back then. The default is an empty class which means
    that no probing happens. The purpose of the class bitfield is to limit
    the aforementioned undesirable side effects.
  
  Once again, method 3 should be avoided wherever possible. Explicit device
  instantiation (methods 1 and 2) is much preferred for it is safer and
  faster.
99cd8e258   Jean Delvare   i2c: Add a sysfs ...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  
  
  Method 4: Instantiate from user-space
  -------------------------------------
  
  In general, the kernel should know which I2C devices are connected and
  what addresses they live at. However, in certain cases, it does not, so a
  sysfs interface was added to let the user provide the information. This
  interface is made of 2 attribute files which are created in every I2C bus
  directory: new_device and delete_device. Both files are write only and you
  must write the right parameters to them in order to properly instantiate,
  respectively delete, an I2C device.
  
  File new_device takes 2 parameters: the name of the I2C device (a string)
  and the address of the I2C device (a number, typically expressed in
  hexadecimal starting with 0x, but can also be expressed in decimal.)
  
  File delete_device takes a single parameter: the address of the I2C
  device. As no two devices can live at the same address on a given I2C
  segment, the address is sufficient to uniquely identify the device to be
  deleted.
  
  Example:
03f1805ad   Jean Delvare   i2c: Minor docume...
191
  # echo eeprom 0x50 > /sys/bus/i2c/devices/i2c-3/new_device
99cd8e258   Jean Delvare   i2c: Add a sysfs ...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  
  While this interface should only be used when in-kernel device declaration
  can't be done, there is a variety of cases where it can be helpful:
  * The I2C driver usually detects devices (method 3 above) but the bus
    segment your device lives on doesn't have the proper class bit set and
    thus detection doesn't trigger.
  * The I2C driver usually detects devices, but your device lives at an
    unexpected address.
  * The I2C driver usually detects devices, but your device is not detected,
    either because the detection routine is too strict, or because your
    device is not officially supported yet but you know it is compatible.
  * You are developing a driver on a test board, where you soldered the I2C
    device yourself.
  
  This interface is a replacement for the force_* module parameters some I2C
  drivers implement. Being implemented in i2c-core rather than in each
  device driver individually, it is much more efficient, and also has the
  advantage that you do not have to reload the driver to change a setting.
  You can also instantiate the device before the driver is loaded or even
  available, and you don't need to know what driver the device needs.