aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c
diff options
context:
space:
mode:
authorDavid Brownell <david-b@pacbell.net>2007-05-01 17:26:31 -0400
committerJean Delvare <khali@hyperion.delvare>2007-05-01 17:26:31 -0400
commit9c1600eda42e52796f49b36cf15b9debcfd09bea (patch)
treefe93f5924c7777cd5c3cc1ef6033dd2dfae542b5 /drivers/i2c
parent4298cfc3eb6110df989f784be516c6340c597a66 (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>
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/Kconfig5
-rw-r--r--drivers/i2c/Makefile1
-rw-r--r--drivers/i2c/i2c-boardinfo.c90
-rw-r--r--drivers/i2c/i2c-core.c90
-rw-r--r--drivers/i2c/i2c-core.h31
5 files changed, 213 insertions, 4 deletions
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
25config I2C_BOARDINFO
26 boolean
27 depends on I2C
28 default y
29
25config I2C_CHARDEV 30config 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
5obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o
5obj-$(CONFIG_I2C) += i2c-core.o 6obj-$(CONFIG_I2C) += i2c-core.o
6obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o 7obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o
7obj-y += busses/ chips/ algos/ 8obj-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 */
28DEFINE_MUTEX(__i2c_board_lock);
29EXPORT_SYMBOL_GPL(__i2c_board_lock);
30
31LIST_HEAD(__i2c_board_list);
32EXPORT_SYMBOL_GPL(__i2c_board_list);
33
34int __i2c_first_dynamic_bus_num;
35EXPORT_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 */
60int __init
61i2c_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
39static LIST_HEAD(adapters); 41static LIST_HEAD(adapters);
40static LIST_HEAD(drivers); 42static 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
167static void i2c_client_dev_release(struct device *dev)
168{
169 kfree(to_i2c_client(dev));
170}
171
165static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf) 172static 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
198static 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 */
218struct i2c_client *
219i2c_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}
251EXPORT_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 */
258void 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}
276EXPORT_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
307static 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
315int i2c_del_adapter(struct i2c_adapter *adap) 391int 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
19struct 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 */
28extern struct mutex __i2c_board_lock;
29extern struct list_head __i2c_board_list;
30extern int __i2c_first_dynamic_bus_num;
31