diff options
-rw-r--r-- | drivers/i2c/i2c-core.c | 141 | ||||
-rw-r--r-- | include/linux/i2c.h | 13 |
2 files changed, 110 insertions, 44 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index fd921ce0b75b..a2ad83ad0f53 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c | |||
@@ -44,15 +44,58 @@ static DEFINE_IDR(i2c_adapter_idr); | |||
44 | 44 | ||
45 | /* ------------------------------------------------------------------------- */ | 45 | /* ------------------------------------------------------------------------- */ |
46 | 46 | ||
47 | /* match always succeeds, as we want the probe() to tell if we really accept this match */ | ||
48 | static int i2c_device_match(struct device *dev, struct device_driver *drv) | 47 | static int i2c_device_match(struct device *dev, struct device_driver *drv) |
49 | { | 48 | { |
50 | return 1; | 49 | struct i2c_client *client = to_i2c_client(dev); |
50 | struct i2c_driver *driver = to_i2c_driver(drv); | ||
51 | |||
52 | /* make legacy i2c drivers bypass driver model probing entirely; | ||
53 | * such drivers scan each i2c adapter/bus themselves. | ||
54 | */ | ||
55 | if (!driver->probe) | ||
56 | return 0; | ||
57 | |||
58 | /* new style drivers use the same kind of driver matching policy | ||
59 | * as platform devices or SPI: compare device and driver IDs. | ||
60 | */ | ||
61 | return strcmp(client->driver_name, drv->name) == 0; | ||
51 | } | 62 | } |
52 | 63 | ||
64 | #ifdef CONFIG_HOTPLUG | ||
65 | |||
66 | /* uevent helps with hotplug: modprobe -q $(MODALIAS) */ | ||
67 | static int i2c_device_uevent(struct device *dev, char **envp, int num_envp, | ||
68 | char *buffer, int buffer_size) | ||
69 | { | ||
70 | struct i2c_client *client = to_i2c_client(dev); | ||
71 | int i = 0, length = 0; | ||
72 | |||
73 | /* by definition, legacy drivers can't hotplug */ | ||
74 | if (dev->driver || !client->driver_name) | ||
75 | return 0; | ||
76 | |||
77 | if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, | ||
78 | "MODALIAS=%s", client->driver_name)) | ||
79 | return -ENOMEM; | ||
80 | envp[i] = NULL; | ||
81 | dev_dbg(dev, "uevent\n"); | ||
82 | return 0; | ||
83 | } | ||
84 | |||
85 | #else | ||
86 | #define i2c_device_uevent NULL | ||
87 | #endif /* CONFIG_HOTPLUG */ | ||
88 | |||
53 | static int i2c_device_probe(struct device *dev) | 89 | static int i2c_device_probe(struct device *dev) |
54 | { | 90 | { |
55 | return -ENODEV; | 91 | struct i2c_client *client = to_i2c_client(dev); |
92 | struct i2c_driver *driver = to_i2c_driver(dev->driver); | ||
93 | |||
94 | if (!driver->probe) | ||
95 | return -ENODEV; | ||
96 | client->driver = driver; | ||
97 | dev_dbg(dev, "probe\n"); | ||
98 | return driver->probe(client); | ||
56 | } | 99 | } |
57 | 100 | ||
58 | static int i2c_device_remove(struct device *dev) | 101 | static int i2c_device_remove(struct device *dev) |
@@ -95,9 +138,38 @@ static int i2c_device_resume(struct device * dev) | |||
95 | return driver->resume(to_i2c_client(dev)); | 138 | return driver->resume(to_i2c_client(dev)); |
96 | } | 139 | } |
97 | 140 | ||
141 | static void i2c_client_release(struct device *dev) | ||
142 | { | ||
143 | struct i2c_client *client = to_i2c_client(dev); | ||
144 | complete(&client->released); | ||
145 | } | ||
146 | |||
147 | static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf) | ||
148 | { | ||
149 | struct i2c_client *client = to_i2c_client(dev); | ||
150 | return sprintf(buf, "%s\n", client->name); | ||
151 | } | ||
152 | |||
153 | static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf) | ||
154 | { | ||
155 | struct i2c_client *client = to_i2c_client(dev); | ||
156 | return client->driver_name | ||
157 | ? sprintf(buf, "%s\n", client->driver_name) | ||
158 | : 0; | ||
159 | } | ||
160 | |||
161 | static struct device_attribute i2c_dev_attrs[] = { | ||
162 | __ATTR(name, S_IRUGO, show_client_name, NULL), | ||
163 | /* modalias helps coldplug: modprobe $(cat .../modalias) */ | ||
164 | __ATTR(modalias, S_IRUGO, show_modalias, NULL), | ||
165 | { }, | ||
166 | }; | ||
167 | |||
98 | struct bus_type i2c_bus_type = { | 168 | struct bus_type i2c_bus_type = { |
99 | .name = "i2c", | 169 | .name = "i2c", |
170 | .dev_attrs = i2c_dev_attrs, | ||
100 | .match = i2c_device_match, | 171 | .match = i2c_device_match, |
172 | .uevent = i2c_device_uevent, | ||
101 | .probe = i2c_device_probe, | 173 | .probe = i2c_device_probe, |
102 | .remove = i2c_device_remove, | 174 | .remove = i2c_device_remove, |
103 | .shutdown = i2c_device_shutdown, | 175 | .shutdown = i2c_device_shutdown, |
@@ -134,31 +206,6 @@ struct class i2c_adapter_class = { | |||
134 | }; | 206 | }; |
135 | 207 | ||
136 | 208 | ||
137 | static void i2c_client_release(struct device *dev) | ||
138 | { | ||
139 | struct i2c_client *client = to_i2c_client(dev); | ||
140 | complete(&client->released); | ||
141 | } | ||
142 | |||
143 | static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf) | ||
144 | { | ||
145 | struct i2c_client *client = to_i2c_client(dev); | ||
146 | return sprintf(buf, "%s\n", client->name); | ||
147 | } | ||
148 | |||
149 | /* | ||
150 | * We can't use the DEVICE_ATTR() macro here, as we used the same name for | ||
151 | * an i2c adapter attribute (above). | ||
152 | */ | ||
153 | static struct device_attribute dev_attr_client_name = | ||
154 | __ATTR(name, S_IRUGO, &show_client_name, NULL); | ||
155 | |||
156 | |||
157 | /* --------------------------------------------------- | ||
158 | * registering functions | ||
159 | * --------------------------------------------------- | ||
160 | */ | ||
161 | |||
162 | /* ----- | 209 | /* ----- |
163 | * i2c_add_adapter is called from within the algorithm layer, | 210 | * i2c_add_adapter is called from within the algorithm layer, |
164 | * when a new hw adapter registers. A new device is register to be | 211 | * when a new hw adapter registers. A new device is register to be |
@@ -208,7 +255,7 @@ int i2c_add_adapter(struct i2c_adapter *adap) | |||
208 | 255 | ||
209 | dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); | 256 | dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); |
210 | 257 | ||
211 | /* inform drivers of new adapters */ | 258 | /* let legacy drivers scan this bus for matching devices */ |
212 | list_for_each(item,&drivers) { | 259 | list_for_each(item,&drivers) { |
213 | driver = list_entry(item, struct i2c_driver, list); | 260 | driver = list_entry(item, struct i2c_driver, list); |
214 | if (driver->attach_adapter) | 261 | if (driver->attach_adapter) |
@@ -292,16 +339,32 @@ int i2c_del_adapter(struct i2c_adapter *adap) | |||
292 | } | 339 | } |
293 | 340 | ||
294 | 341 | ||
295 | /* ----- | 342 | /* ------------------------------------------------------------------------- */ |
296 | * What follows is the "upwards" interface: commands for talking to clients, | 343 | |
297 | * which implement the functions to access the physical information of the | 344 | /* |
298 | * chips. | 345 | * An i2c_driver is used with one or more i2c_client (device) nodes to access |
346 | * i2c slave chips, on a bus instance associated with some i2c_adapter. There | ||
347 | * are two models for binding the driver to its device: "new style" drivers | ||
348 | * follow the standard Linux driver model and just respond to probe() calls | ||
349 | * issued if the driver core sees they match(); "legacy" drivers create device | ||
350 | * nodes themselves. | ||
299 | */ | 351 | */ |
300 | 352 | ||
301 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) | 353 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) |
302 | { | 354 | { |
303 | int res; | 355 | int res; |
304 | 356 | ||
357 | /* new style driver methods can't mix with legacy ones */ | ||
358 | if (driver->probe) { | ||
359 | if (driver->attach_adapter || driver->detach_adapter | ||
360 | || driver->detach_client) { | ||
361 | printk(KERN_WARNING | ||
362 | "i2c-core: driver [%s] is confused\n", | ||
363 | driver->driver.name); | ||
364 | return -EINVAL; | ||
365 | } | ||
366 | } | ||
367 | |||
305 | /* add the driver to the list of i2c drivers in the driver core */ | 368 | /* add the driver to the list of i2c drivers in the driver core */ |
306 | driver->driver.owner = owner; | 369 | driver->driver.owner = owner; |
307 | driver->driver.bus = &i2c_bus_type; | 370 | driver->driver.bus = &i2c_bus_type; |
@@ -315,7 +378,7 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver) | |||
315 | list_add_tail(&driver->list,&drivers); | 378 | list_add_tail(&driver->list,&drivers); |
316 | pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); | 379 | pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); |
317 | 380 | ||
318 | /* now look for instances of driver on our adapters */ | 381 | /* legacy drivers scan i2c busses directly */ |
319 | if (driver->attach_adapter) { | 382 | if (driver->attach_adapter) { |
320 | struct i2c_adapter *adapter; | 383 | struct i2c_adapter *adapter; |
321 | 384 | ||
@@ -380,6 +443,8 @@ int i2c_del_driver(struct i2c_driver *driver) | |||
380 | return 0; | 443 | return 0; |
381 | } | 444 | } |
382 | 445 | ||
446 | /* ------------------------------------------------------------------------- */ | ||
447 | |||
383 | static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr) | 448 | static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr) |
384 | { | 449 | { |
385 | struct list_head *item; | 450 | struct list_head *item; |
@@ -430,9 +495,6 @@ int i2c_attach_client(struct i2c_client *client) | |||
430 | res = device_register(&client->dev); | 495 | res = device_register(&client->dev); |
431 | if (res) | 496 | if (res) |
432 | goto out_list; | 497 | goto out_list; |
433 | res = device_create_file(&client->dev, &dev_attr_client_name); | ||
434 | if (res) | ||
435 | goto out_unregister; | ||
436 | mutex_unlock(&adapter->clist_lock); | 498 | mutex_unlock(&adapter->clist_lock); |
437 | 499 | ||
438 | if (adapter->client_register) { | 500 | if (adapter->client_register) { |
@@ -445,10 +507,6 @@ int i2c_attach_client(struct i2c_client *client) | |||
445 | 507 | ||
446 | return 0; | 508 | return 0; |
447 | 509 | ||
448 | out_unregister: | ||
449 | init_completion(&client->released); /* Needed? */ | ||
450 | device_unregister(&client->dev); | ||
451 | wait_for_completion(&client->released); | ||
452 | out_list: | 510 | out_list: |
453 | list_del(&client->list); | 511 | list_del(&client->list); |
454 | dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x " | 512 | dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x " |
@@ -483,7 +541,6 @@ int i2c_detach_client(struct i2c_client *client) | |||
483 | mutex_lock(&adapter->clist_lock); | 541 | mutex_lock(&adapter->clist_lock); |
484 | list_del(&client->list); | 542 | list_del(&client->list); |
485 | init_completion(&client->released); | 543 | init_completion(&client->released); |
486 | device_remove_file(&client->dev, &dev_attr_client_name); | ||
487 | device_unregister(&client->dev); | 544 | device_unregister(&client->dev); |
488 | mutex_unlock(&adapter->clist_lock); | 545 | mutex_unlock(&adapter->clist_lock); |
489 | wait_for_completion(&client->released); | 546 | wait_for_completion(&client->released); |
diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 563c9651dd37..8dcccc0f4822 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h | |||
@@ -113,7 +113,7 @@ struct i2c_driver { | |||
113 | * can be used by the driver to test if the bus meets its conditions | 113 | * can be used by the driver to test if the bus meets its conditions |
114 | * & seek for the presence of the chip(s) it supports. If found, it | 114 | * & seek for the presence of the chip(s) it supports. If found, it |
115 | * registers the client(s) that are on the bus to the i2c admin. via | 115 | * registers the client(s) that are on the bus to the i2c admin. via |
116 | * i2c_attach_client. | 116 | * i2c_attach_client. (LEGACY I2C DRIVERS ONLY) |
117 | */ | 117 | */ |
118 | int (*attach_adapter)(struct i2c_adapter *); | 118 | int (*attach_adapter)(struct i2c_adapter *); |
119 | int (*detach_adapter)(struct i2c_adapter *); | 119 | int (*detach_adapter)(struct i2c_adapter *); |
@@ -121,10 +121,16 @@ struct i2c_driver { | |||
121 | /* tells the driver that a client is about to be deleted & gives it | 121 | /* tells the driver that a client is about to be deleted & gives it |
122 | * the chance to remove its private data. Also, if the client struct | 122 | * the chance to remove its private data. Also, if the client struct |
123 | * has been dynamically allocated by the driver in the function above, | 123 | * has been dynamically allocated by the driver in the function above, |
124 | * it must be freed here. | 124 | * it must be freed here. (LEGACY I2C DRIVERS ONLY) |
125 | */ | 125 | */ |
126 | int (*detach_client)(struct i2c_client *); | 126 | int (*detach_client)(struct i2c_client *); |
127 | 127 | ||
128 | /* Standard driver model interfaces, for "new style" i2c drivers. | ||
129 | * With the driver model, device enumeration is NEVER done by drivers; | ||
130 | * it's done by infrastructure. (NEW STYLE DRIVERS ONLY) | ||
131 | */ | ||
132 | int (*probe)(struct i2c_client *); | ||
133 | |||
128 | /* driver model interfaces that don't relate to enumeration */ | 134 | /* driver model interfaces that don't relate to enumeration */ |
129 | void (*shutdown)(struct i2c_client *); | 135 | void (*shutdown)(struct i2c_client *); |
130 | int (*suspend)(struct i2c_client *, pm_message_t mesg); | 136 | int (*suspend)(struct i2c_client *, pm_message_t mesg); |
@@ -148,6 +154,8 @@ struct i2c_driver { | |||
148 | * @name: Indicates the type of the device, usually a chip name that's | 154 | * @name: Indicates the type of the device, usually a chip name that's |
149 | * generic enough to hide second-sourcing and compatible revisions. | 155 | * generic enough to hide second-sourcing and compatible revisions. |
150 | * @dev: Driver model device node for the slave. | 156 | * @dev: Driver model device node for the slave. |
157 | * @driver_name: Identifies new-style driver used with this device; also | ||
158 | * used as the module name for hotplug/coldplug modprobe support. | ||
151 | * | 159 | * |
152 | * An i2c_client identifies a single device (i.e. chip) connected to an | 160 | * An i2c_client identifies a single device (i.e. chip) connected to an |
153 | * i2c bus. The behaviour is defined by the routines of the driver. | 161 | * i2c bus. The behaviour is defined by the routines of the driver. |
@@ -163,6 +171,7 @@ struct i2c_client { | |||
163 | int usage_count; /* How many accesses currently */ | 171 | int usage_count; /* How many accesses currently */ |
164 | /* to the client */ | 172 | /* to the client */ |
165 | struct device dev; /* the device structure */ | 173 | struct device dev; /* the device structure */ |
174 | char driver_name[KOBJ_NAME_LEN]; | ||
166 | struct list_head list; | 175 | struct list_head list; |
167 | struct completion released; | 176 | struct completion released; |
168 | }; | 177 | }; |