diff options
author | David Brownell <david-b@pacbell.net> | 2007-05-01 17:26:31 -0400 |
---|---|---|
committer | Jean Delvare <khali@hyperion.delvare> | 2007-05-01 17:26:31 -0400 |
commit | 9c1600eda42e52796f49b36cf15b9debcfd09bea (patch) | |
tree | fe93f5924c7777cd5c3cc1ef6033dd2dfae542b5 | |
parent | 4298cfc3eb6110df989f784be516c6340c597a66 (diff) |
i2c: Add i2c_board_info and i2c_new_device()
This provides partial support for new-style I2C driver binding. It builds
on "struct i2c_board_info" declarations that identify I2C devices on a given
board. This is needed on systems with I2C devices that can't be fully probed
and/or autoconfigured, such as many embedded Linux configurations where the
way a given I2C device is wired may affect how it must be used.
There are two models for declaring such devices:
* LATE -- using a public function i2c_new_device(). This lets modules
declare I2C devices found *AFTER* a given I2C adapter becomes available.
For example, a PCI card could create adapters giving access to utility
chips on that card, and this would be used to associate those chips with
those adapters.
* EARLY -- from arch_initcall() level code, using a non-exported function
i2c_register_board_info(). This copies the declarations *BEFORE* such
an i2c_adapter becomes available, arranging that i2c_new_device() will
be called later when i2c-core registers the relevant i2c_adapter.
For example, arch/.../.../board-*.c files would declare the I2C devices
along with their platform data, and I2C devices would behave much like
PNPACPI devices. (That is, both enumerate from board-specific tables.)
To match the exported i2c_new_device(), the previously-private function
i2c_unregister_device() is now exported.
Pending later patches using these new APIs, this is effectively a NOP.
Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Jean Delvare <khali@linux-fr.org>
-rw-r--r-- | drivers/Makefile | 2 | ||||
-rw-r--r-- | drivers/i2c/Kconfig | 5 | ||||
-rw-r--r-- | drivers/i2c/Makefile | 1 | ||||
-rw-r--r-- | drivers/i2c/i2c-boardinfo.c | 90 | ||||
-rw-r--r-- | drivers/i2c/i2c-core.c | 90 | ||||
-rw-r--r-- | drivers/i2c/i2c-core.h | 31 | ||||
-rw-r--r-- | include/linux/i2c.h | 62 |
7 files changed, 276 insertions, 5 deletions
diff --git a/drivers/Makefile b/drivers/Makefile index 920c975bb6d4..26ca9031ea49 100644 --- a/drivers/Makefile +++ b/drivers/Makefile | |||
@@ -58,7 +58,7 @@ obj-$(CONFIG_GAMEPORT) += input/gameport/ | |||
58 | obj-$(CONFIG_INPUT) += input/ | 58 | obj-$(CONFIG_INPUT) += input/ |
59 | obj-$(CONFIG_I2O) += message/ | 59 | obj-$(CONFIG_I2O) += message/ |
60 | obj-$(CONFIG_RTC_LIB) += rtc/ | 60 | obj-$(CONFIG_RTC_LIB) += rtc/ |
61 | obj-$(CONFIG_I2C) += i2c/ | 61 | obj-y += i2c/ |
62 | obj-$(CONFIG_W1) += w1/ | 62 | obj-$(CONFIG_W1) += w1/ |
63 | obj-$(CONFIG_HWMON) += hwmon/ | 63 | obj-$(CONFIG_HWMON) += hwmon/ |
64 | obj-$(CONFIG_PHONE) += telephony/ | 64 | obj-$(CONFIG_PHONE) += telephony/ |
diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig index 11935f66fcd8..74c8518b69e7 100644 --- a/drivers/i2c/Kconfig +++ b/drivers/i2c/Kconfig | |||
@@ -22,6 +22,11 @@ config I2C | |||
22 | This I2C support can also be built as a module. If so, the module | 22 | This I2C support can also be built as a module. If so, the module |
23 | will be called i2c-core. | 23 | will be called i2c-core. |
24 | 24 | ||
25 | config I2C_BOARDINFO | ||
26 | boolean | ||
27 | depends on I2C | ||
28 | default y | ||
29 | |||
25 | config I2C_CHARDEV | 30 | config I2C_CHARDEV |
26 | tristate "I2C device interface" | 31 | tristate "I2C device interface" |
27 | depends on I2C | 32 | depends on I2C |
diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile index 71c5a854ac5d..ba26e6cbe74e 100644 --- a/drivers/i2c/Makefile +++ b/drivers/i2c/Makefile | |||
@@ -2,6 +2,7 @@ | |||
2 | # Makefile for the i2c core. | 2 | # Makefile for the i2c core. |
3 | # | 3 | # |
4 | 4 | ||
5 | obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o | ||
5 | obj-$(CONFIG_I2C) += i2c-core.o | 6 | obj-$(CONFIG_I2C) += i2c-core.o |
6 | obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o | 7 | obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o |
7 | obj-y += busses/ chips/ algos/ | 8 | obj-y += busses/ chips/ algos/ |
diff --git a/drivers/i2c/i2c-boardinfo.c b/drivers/i2c/i2c-boardinfo.c new file mode 100644 index 000000000000..ffb35f09df03 --- /dev/null +++ b/drivers/i2c/i2c-boardinfo.c | |||
@@ -0,0 +1,90 @@ | |||
1 | /* | ||
2 | * i2c-boardinfo.h - collect pre-declarations of I2C devices | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the Free Software | ||
16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
17 | */ | ||
18 | |||
19 | #include <linux/kernel.h> | ||
20 | #include <linux/i2c.h> | ||
21 | |||
22 | #include "i2c-core.h" | ||
23 | |||
24 | |||
25 | /* These symbols are exported ONLY FOR the i2c core. | ||
26 | * No other users will be supported. | ||
27 | */ | ||
28 | DEFINE_MUTEX(__i2c_board_lock); | ||
29 | EXPORT_SYMBOL_GPL(__i2c_board_lock); | ||
30 | |||
31 | LIST_HEAD(__i2c_board_list); | ||
32 | EXPORT_SYMBOL_GPL(__i2c_board_list); | ||
33 | |||
34 | int __i2c_first_dynamic_bus_num; | ||
35 | EXPORT_SYMBOL_GPL(__i2c_first_dynamic_bus_num); | ||
36 | |||
37 | |||
38 | /** | ||
39 | * i2c_register_board_info - statically declare I2C devices | ||
40 | * @busnum: identifies the bus to which these devices belong | ||
41 | * @info: vector of i2c device descriptors | ||
42 | * @len: how many descriptors in the vector; may be zero to reserve | ||
43 | * the specified bus number. | ||
44 | * | ||
45 | * Systems using the Linux I2C driver stack can declare tables of board info | ||
46 | * while they initialize. This should be done in board-specific init code | ||
47 | * near arch_initcall() time, or equivalent, before any I2C adapter driver is | ||
48 | * registered. For example, mainboard init code could define several devices, | ||
49 | * as could the init code for each daughtercard in a board stack. | ||
50 | * | ||
51 | * The I2C devices will be created later, after the adapter for the relevant | ||
52 | * bus has been registered. After that moment, standard driver model tools | ||
53 | * are used to bind "new style" I2C drivers to the devices. The bus number | ||
54 | * for any device declared using this routine is not available for dynamic | ||
55 | * allocation. | ||
56 | * | ||
57 | * The board info passed can safely be __initdata, but be careful of embedded | ||
58 | * pointers (for platform_data, functions, etc) since that won't be copied. | ||
59 | */ | ||
60 | int __init | ||
61 | i2c_register_board_info(int busnum, | ||
62 | struct i2c_board_info const *info, unsigned len) | ||
63 | { | ||
64 | int status; | ||
65 | |||
66 | mutex_lock(&__i2c_board_lock); | ||
67 | |||
68 | /* dynamic bus numbers will be assigned after the last static one */ | ||
69 | if (busnum >= __i2c_first_dynamic_bus_num) | ||
70 | __i2c_first_dynamic_bus_num = busnum + 1; | ||
71 | |||
72 | for (status = 0; len; len--, info++) { | ||
73 | struct i2c_devinfo *devinfo; | ||
74 | |||
75 | devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL); | ||
76 | if (!devinfo) { | ||
77 | pr_debug("i2c-core: can't register boardinfo!\n"); | ||
78 | status = -ENOMEM; | ||
79 | break; | ||
80 | } | ||
81 | |||
82 | devinfo->busnum = busnum; | ||
83 | devinfo->board_info = *info; | ||
84 | list_add_tail(&devinfo->list, &__i2c_board_list); | ||
85 | } | ||
86 | |||
87 | mutex_unlock(&__i2c_board_lock); | ||
88 | |||
89 | return status; | ||
90 | } | ||
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index 3aac1b5c7cbd..a7dcdc69b9e7 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c | |||
@@ -35,6 +35,8 @@ | |||
35 | #include <linux/completion.h> | 35 | #include <linux/completion.h> |
36 | #include <asm/uaccess.h> | 36 | #include <asm/uaccess.h> |
37 | 37 | ||
38 | #include "i2c-core.h" | ||
39 | |||
38 | 40 | ||
39 | static LIST_HEAD(adapters); | 41 | static LIST_HEAD(adapters); |
40 | static LIST_HEAD(drivers); | 42 | static LIST_HEAD(drivers); |
@@ -162,6 +164,11 @@ static void i2c_client_release(struct device *dev) | |||
162 | complete(&client->released); | 164 | complete(&client->released); |
163 | } | 165 | } |
164 | 166 | ||
167 | static void i2c_client_dev_release(struct device *dev) | ||
168 | { | ||
169 | kfree(to_i2c_client(dev)); | ||
170 | } | ||
171 | |||
165 | static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf) | 172 | static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf) |
166 | { | 173 | { |
167 | struct i2c_client *client = to_i2c_client(dev); | 174 | struct i2c_client *client = to_i2c_client(dev); |
@@ -195,7 +202,60 @@ struct bus_type i2c_bus_type = { | |||
195 | .resume = i2c_device_resume, | 202 | .resume = i2c_device_resume, |
196 | }; | 203 | }; |
197 | 204 | ||
198 | static void i2c_unregister_device(struct i2c_client *client) | 205 | /** |
206 | * i2c_new_device - instantiate an i2c device for use with a new style driver | ||
207 | * @adap: the adapter managing the device | ||
208 | * @info: describes one I2C device; bus_num is ignored | ||
209 | * | ||
210 | * Create a device to work with a new style i2c driver, where binding is | ||
211 | * handled through driver model probe()/remove() methods. This call is not | ||
212 | * appropriate for use by mainboad initialization logic, which usually runs | ||
213 | * during an arch_initcall() long before any i2c_adapter could exist. | ||
214 | * | ||
215 | * This returns the new i2c client, which may be saved for later use with | ||
216 | * i2c_unregister_device(); or NULL to indicate an error. | ||
217 | */ | ||
218 | struct i2c_client * | ||
219 | i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) | ||
220 | { | ||
221 | struct i2c_client *client; | ||
222 | int status; | ||
223 | |||
224 | client = kzalloc(sizeof *client, GFP_KERNEL); | ||
225 | if (!client) | ||
226 | return NULL; | ||
227 | |||
228 | client->adapter = adap; | ||
229 | |||
230 | client->dev.platform_data = info->platform_data; | ||
231 | client->flags = info->flags; | ||
232 | client->addr = info->addr; | ||
233 | client->irq = info->irq; | ||
234 | |||
235 | strlcpy(client->driver_name, info->driver_name, | ||
236 | sizeof(client->driver_name)); | ||
237 | strlcpy(client->name, info->type, sizeof(client->name)); | ||
238 | |||
239 | /* a new style driver may be bound to this device when we | ||
240 | * return from this function, or any later moment (e.g. maybe | ||
241 | * hotplugging will load the driver module). and the device | ||
242 | * refcount model is the standard driver model one. | ||
243 | */ | ||
244 | status = i2c_attach_client(client); | ||
245 | if (status < 0) { | ||
246 | kfree(client); | ||
247 | client = NULL; | ||
248 | } | ||
249 | return client; | ||
250 | } | ||
251 | EXPORT_SYMBOL_GPL(i2c_new_device); | ||
252 | |||
253 | |||
254 | /** | ||
255 | * i2c_unregister_device - reverse effect of i2c_new_device() | ||
256 | * @client: value returned from i2c_new_device() | ||
257 | */ | ||
258 | void i2c_unregister_device(struct i2c_client *client) | ||
199 | { | 259 | { |
200 | struct i2c_adapter *adapter = client->adapter; | 260 | struct i2c_adapter *adapter = client->adapter; |
201 | struct i2c_driver *driver = client->driver; | 261 | struct i2c_driver *driver = client->driver; |
@@ -213,6 +273,7 @@ static void i2c_unregister_device(struct i2c_client *client) | |||
213 | 273 | ||
214 | device_unregister(&client->dev); | 274 | device_unregister(&client->dev); |
215 | } | 275 | } |
276 | EXPORT_SYMBOL_GPL(i2c_unregister_device); | ||
216 | 277 | ||
217 | 278 | ||
218 | /* ------------------------------------------------------------------------- */ | 279 | /* ------------------------------------------------------------------------- */ |
@@ -243,6 +304,22 @@ struct class i2c_adapter_class = { | |||
243 | .dev_attrs = i2c_adapter_attrs, | 304 | .dev_attrs = i2c_adapter_attrs, |
244 | }; | 305 | }; |
245 | 306 | ||
307 | static void i2c_scan_static_board_info(struct i2c_adapter *adapter) | ||
308 | { | ||
309 | struct i2c_devinfo *devinfo; | ||
310 | |||
311 | mutex_lock(&__i2c_board_lock); | ||
312 | list_for_each_entry(devinfo, &__i2c_board_list, list) { | ||
313 | if (devinfo->busnum == adapter->nr | ||
314 | && !i2c_new_device(adapter, | ||
315 | &devinfo->board_info)) | ||
316 | printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n", | ||
317 | i2c_adapter_id(adapter), | ||
318 | devinfo->board_info.addr); | ||
319 | } | ||
320 | mutex_unlock(&__i2c_board_lock); | ||
321 | } | ||
322 | |||
246 | 323 | ||
247 | /* ----- | 324 | /* ----- |
248 | * i2c_add_adapter is called from within the algorithm layer, | 325 | * i2c_add_adapter is called from within the algorithm layer, |
@@ -311,7 +388,6 @@ out_list: | |||
311 | goto out_unlock; | 388 | goto out_unlock; |
312 | } | 389 | } |
313 | 390 | ||
314 | |||
315 | int i2c_del_adapter(struct i2c_adapter *adap) | 391 | int i2c_del_adapter(struct i2c_adapter *adap) |
316 | { | 392 | { |
317 | struct list_head *item, *_n; | 393 | struct list_head *item, *_n; |
@@ -541,9 +617,15 @@ int i2c_attach_client(struct i2c_client *client) | |||
541 | client->usage_count = 0; | 617 | client->usage_count = 0; |
542 | 618 | ||
543 | client->dev.parent = &client->adapter->dev; | 619 | client->dev.parent = &client->adapter->dev; |
544 | client->dev.driver = &client->driver->driver; | ||
545 | client->dev.bus = &i2c_bus_type; | 620 | client->dev.bus = &i2c_bus_type; |
546 | client->dev.release = &i2c_client_release; | 621 | |
622 | if (client->driver) | ||
623 | client->dev.driver = &client->driver->driver; | ||
624 | |||
625 | if (client->driver && !is_newstyle_driver(client->driver)) | ||
626 | client->dev.release = i2c_client_release; | ||
627 | else | ||
628 | client->dev.release = i2c_client_dev_release; | ||
547 | 629 | ||
548 | snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id), | 630 | snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id), |
549 | "%d-%04x", i2c_adapter_id(adapter), client->addr); | 631 | "%d-%04x", i2c_adapter_id(adapter), client->addr); |
diff --git a/drivers/i2c/i2c-core.h b/drivers/i2c/i2c-core.h new file mode 100644 index 000000000000..cd5bff874855 --- /dev/null +++ b/drivers/i2c/i2c-core.h | |||
@@ -0,0 +1,31 @@ | |||
1 | /* | ||
2 | * i2c-core.h - interfaces internal to the I2C framework | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the Free Software | ||
16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
17 | */ | ||
18 | |||
19 | struct i2c_devinfo { | ||
20 | struct list_head list; | ||
21 | int busnum; | ||
22 | struct i2c_board_info board_info; | ||
23 | }; | ||
24 | |||
25 | /* board_lock protects board_list and first_dynamic_bus_num. | ||
26 | * only i2c core components are allowed to use these symbols. | ||
27 | */ | ||
28 | extern struct mutex __i2c_board_lock; | ||
29 | extern struct list_head __i2c_board_list; | ||
30 | extern int __i2c_first_dynamic_bus_num; | ||
31 | |||
diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 6802c3a0a3a3..382a43bf3ad5 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h | |||
@@ -172,6 +172,7 @@ struct i2c_client { | |||
172 | int usage_count; /* How many accesses currently */ | 172 | int usage_count; /* How many accesses currently */ |
173 | /* to the client */ | 173 | /* to the client */ |
174 | struct device dev; /* the device structure */ | 174 | struct device dev; /* the device structure */ |
175 | int irq; /* irq issued by device (or -1) */ | ||
175 | char driver_name[KOBJ_NAME_LEN]; | 176 | char driver_name[KOBJ_NAME_LEN]; |
176 | struct list_head list; | 177 | struct list_head list; |
177 | struct completion released; | 178 | struct completion released; |
@@ -193,6 +194,67 @@ static inline void i2c_set_clientdata (struct i2c_client *dev, void *data) | |||
193 | dev_set_drvdata (&dev->dev, data); | 194 | dev_set_drvdata (&dev->dev, data); |
194 | } | 195 | } |
195 | 196 | ||
197 | /** | ||
198 | * struct i2c_board_info - template for device creation | ||
199 | * @driver_name: identifies the driver to be bound to the device | ||
200 | * @type: optional chip type information, to initialize i2c_client.name | ||
201 | * @flags: to initialize i2c_client.flags | ||
202 | * @addr: stored in i2c_client.addr | ||
203 | * @platform_data: stored in i2c_client.dev.platform_data | ||
204 | * @irq: stored in i2c_client.irq | ||
205 | |||
206 | * I2C doesn't actually support hardware probing, although controllers and | ||
207 | * devices may be able to use I2C_SMBUS_QUICK to tell whether or not there's | ||
208 | * a device at a given address. Drivers commonly need more information than | ||
209 | * that, such as chip type, configuration, associated IRQ, and so on. | ||
210 | * | ||
211 | * i2c_board_info is used to build tables of information listing I2C devices | ||
212 | * that are present. This information is used to grow the driver model tree | ||
213 | * for "new style" I2C drivers. For mainboards this is done statically using | ||
214 | * i2c_register_board_info(), where @bus_num represents an adapter that isn't | ||
215 | * yet available. For add-on boards, i2c_new_device() does this dynamically | ||
216 | * with the adapter already known. | ||
217 | */ | ||
218 | struct i2c_board_info { | ||
219 | char driver_name[KOBJ_NAME_LEN]; | ||
220 | char type[I2C_NAME_SIZE]; | ||
221 | unsigned short flags; | ||
222 | unsigned short addr; | ||
223 | void *platform_data; | ||
224 | int irq; | ||
225 | }; | ||
226 | |||
227 | /** | ||
228 | * I2C_BOARD_INFO - macro used to list an i2c device and its driver | ||
229 | * @driver: identifies the driver to use with the device | ||
230 | * @dev_addr: the device's address on the bus. | ||
231 | * | ||
232 | * This macro initializes essential fields of a struct i2c_board_info, | ||
233 | * declaring what has been provided on a particular board. Optional | ||
234 | * fields (such as the chip type, its associated irq, or device-specific | ||
235 | * platform_data) are provided using conventional syntax. | ||
236 | */ | ||
237 | #define I2C_BOARD_INFO(driver,dev_addr) \ | ||
238 | .driver_name = (driver), .addr = (dev_addr) | ||
239 | |||
240 | |||
241 | /* Add-on boards should register/unregister their devices; e.g. a board | ||
242 | * with integrated I2C, a config eeprom, sensors, and a codec that's | ||
243 | * used in conjunction with the primary hardware. | ||
244 | */ | ||
245 | extern struct i2c_client * | ||
246 | i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info); | ||
247 | |||
248 | extern void i2c_unregister_device(struct i2c_client *); | ||
249 | |||
250 | /* Mainboard arch_initcall() code should register all its I2C devices. | ||
251 | * This is done at arch_initcall time, before declaring any i2c adapters. | ||
252 | * Modules for add-on boards must use other calls. | ||
253 | */ | ||
254 | extern int | ||
255 | i2c_register_board_info(int busnum, struct i2c_board_info const *info, unsigned n); | ||
256 | |||
257 | |||
196 | /* | 258 | /* |
197 | * The following structs are for those who like to implement new bus drivers: | 259 | * The following structs are for those who like to implement new bus drivers: |
198 | * i2c_algorithm is the interface to a class of hardware solutions which can | 260 | * i2c_algorithm is the interface to a class of hardware solutions which can |