aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/i2c-core.c
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/i2c-core.c
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/i2c-core.c')
-rw-r--r--drivers/i2c/i2c-core.c90
1 files changed, 86 insertions, 4 deletions
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);