Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* i2c-core.c - a device driver for the iic-bus interface */ |
| 2 | /* ------------------------------------------------------------------------- */ |
| 3 | /* Copyright (C) 1995-99 Simon G. Vogl |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 18 | /* ------------------------------------------------------------------------- */ |
| 19 | |
| 20 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. |
| 21 | All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 22 | SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and |
| 23 | Jean Delvare <khali@linux-fr.org> */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/i2c.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/idr.h> |
| 32 | #include <linux/seq_file.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 33 | #include <linux/platform_device.h> |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 34 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <asm/uaccess.h> |
| 36 | |
| 37 | |
| 38 | static LIST_HEAD(adapters); |
| 39 | static LIST_HEAD(drivers); |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 40 | static DEFINE_MUTEX(core_lists); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static DEFINE_IDR(i2c_adapter_idr); |
| 42 | |
| 43 | /* match always succeeds, as we want the probe() to tell if we really accept this match */ |
| 44 | static int i2c_device_match(struct device *dev, struct device_driver *drv) |
| 45 | { |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | static int i2c_bus_suspend(struct device * dev, pm_message_t state) |
| 50 | { |
| 51 | int rc = 0; |
| 52 | |
| 53 | if (dev->driver && dev->driver->suspend) |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 54 | rc = dev->driver->suspend(dev, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | return rc; |
| 56 | } |
| 57 | |
| 58 | static int i2c_bus_resume(struct device * dev) |
| 59 | { |
| 60 | int rc = 0; |
| 61 | |
| 62 | if (dev->driver && dev->driver->resume) |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 63 | rc = dev->driver->resume(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return rc; |
| 65 | } |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static int i2c_device_probe(struct device *dev) |
| 68 | { |
| 69 | return -ENODEV; |
| 70 | } |
| 71 | |
| 72 | static int i2c_device_remove(struct device *dev) |
| 73 | { |
| 74 | return 0; |
| 75 | } |
| 76 | |
Russell King | b864c7d | 2006-01-05 14:37:50 +0000 | [diff] [blame] | 77 | struct bus_type i2c_bus_type = { |
| 78 | .name = "i2c", |
| 79 | .match = i2c_device_match, |
| 80 | .probe = i2c_device_probe, |
| 81 | .remove = i2c_device_remove, |
| 82 | .suspend = i2c_bus_suspend, |
| 83 | .resume = i2c_bus_resume, |
| 84 | }; |
| 85 | |
Jean Delvare | efde723 | 2005-07-20 23:03:50 +0200 | [diff] [blame] | 86 | void i2c_adapter_dev_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
| 88 | struct i2c_adapter *adap = dev_to_i2c_adapter(dev); |
| 89 | complete(&adap->dev_released); |
| 90 | } |
| 91 | |
Jean Delvare | efde723 | 2005-07-20 23:03:50 +0200 | [diff] [blame] | 92 | struct device_driver i2c_adapter_driver = { |
Laurent Riffard | 6586bcd | 2005-10-17 22:54:45 +0200 | [diff] [blame] | 93 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | .name = "i2c_adapter", |
| 95 | .bus = &i2c_bus_type, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | static void i2c_adapter_class_dev_release(struct class_device *dev) |
| 99 | { |
| 100 | struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev); |
| 101 | complete(&adap->class_dev_released); |
| 102 | } |
| 103 | |
Jean Delvare | efde723 | 2005-07-20 23:03:50 +0200 | [diff] [blame] | 104 | struct class i2c_adapter_class = { |
Laurent Riffard | 6586bcd | 2005-10-17 22:54:45 +0200 | [diff] [blame] | 105 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | .name = "i2c-adapter", |
| 107 | .release = &i2c_adapter_class_dev_release, |
| 108 | }; |
| 109 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 110 | static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
| 112 | struct i2c_adapter *adap = dev_to_i2c_adapter(dev); |
| 113 | return sprintf(buf, "%s\n", adap->name); |
| 114 | } |
| 115 | static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL); |
| 116 | |
| 117 | |
| 118 | static void i2c_client_release(struct device *dev) |
| 119 | { |
| 120 | struct i2c_client *client = to_i2c_client(dev); |
| 121 | complete(&client->released); |
| 122 | } |
| 123 | |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 124 | static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | struct i2c_client *client = to_i2c_client(dev); |
| 127 | return sprintf(buf, "%s\n", client->name); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * We can't use the DEVICE_ATTR() macro here as we want the same filename for a |
| 132 | * different type of a device. So beware if the DEVICE_ATTR() macro ever |
| 133 | * changes, this definition will also have to change. |
| 134 | */ |
| 135 | static struct device_attribute dev_attr_client_name = { |
| 136 | .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE }, |
| 137 | .show = &show_client_name, |
| 138 | }; |
| 139 | |
| 140 | |
| 141 | /* --------------------------------------------------- |
| 142 | * registering functions |
| 143 | * --------------------------------------------------- |
| 144 | */ |
| 145 | |
| 146 | /* ----- |
| 147 | * i2c_add_adapter is called from within the algorithm layer, |
| 148 | * when a new hw adapter registers. A new device is register to be |
| 149 | * available for clients. |
| 150 | */ |
| 151 | int i2c_add_adapter(struct i2c_adapter *adap) |
| 152 | { |
| 153 | int id, res = 0; |
| 154 | struct list_head *item; |
| 155 | struct i2c_driver *driver; |
| 156 | |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 157 | mutex_lock(&core_lists); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) { |
| 160 | res = -ENOMEM; |
| 161 | goto out_unlock; |
| 162 | } |
| 163 | |
Mark M. Hoffman | a0920e1 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 164 | res = idr_get_new(&i2c_adapter_idr, adap, &id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | if (res < 0) { |
| 166 | if (res == -EAGAIN) |
| 167 | res = -ENOMEM; |
| 168 | goto out_unlock; |
| 169 | } |
| 170 | |
| 171 | adap->nr = id & MAX_ID_MASK; |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 172 | mutex_init(&adap->bus_lock); |
| 173 | mutex_init(&adap->clist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | list_add_tail(&adap->list,&adapters); |
| 175 | INIT_LIST_HEAD(&adap->clients); |
| 176 | |
| 177 | /* Add the adapter to the driver core. |
| 178 | * If the parent pointer is not set up, |
| 179 | * we add this adapter to the host bus. |
| 180 | */ |
| 181 | if (adap->dev.parent == NULL) |
| 182 | adap->dev.parent = &platform_bus; |
| 183 | sprintf(adap->dev.bus_id, "i2c-%d", adap->nr); |
| 184 | adap->dev.driver = &i2c_adapter_driver; |
| 185 | adap->dev.release = &i2c_adapter_dev_release; |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame^] | 186 | res = device_register(&adap->dev); |
| 187 | if (res) |
| 188 | goto out_list; |
| 189 | res = device_create_file(&adap->dev, &dev_attr_name); |
| 190 | if (res) |
| 191 | goto out_unregister; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | /* Add this adapter to the i2c_adapter class */ |
| 194 | memset(&adap->class_dev, 0x00, sizeof(struct class_device)); |
| 195 | adap->class_dev.dev = &adap->dev; |
| 196 | adap->class_dev.class = &i2c_adapter_class; |
| 197 | strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE); |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame^] | 198 | res = class_device_register(&adap->class_dev); |
| 199 | if (res) |
| 200 | goto out_remove_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 202 | dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); |
| 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | /* inform drivers of new adapters */ |
| 205 | list_for_each(item,&drivers) { |
| 206 | driver = list_entry(item, struct i2c_driver, list); |
Jean Delvare | 8a99475 | 2005-11-26 20:28:06 +0100 | [diff] [blame] | 207 | if (driver->attach_adapter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | /* We ignore the return code; if it fails, too bad */ |
| 209 | driver->attach_adapter(adap); |
| 210 | } |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | out_unlock: |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 213 | mutex_unlock(&core_lists); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | return res; |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame^] | 215 | |
| 216 | out_remove_name: |
| 217 | device_remove_file(&adap->dev, &dev_attr_name); |
| 218 | out_unregister: |
| 219 | init_completion(&adap->dev_released); /* Needed? */ |
| 220 | device_unregister(&adap->dev); |
| 221 | wait_for_completion(&adap->dev_released); |
| 222 | out_list: |
| 223 | list_del(&adap->list); |
| 224 | idr_remove(&i2c_adapter_idr, adap->nr); |
| 225 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
| 229 | int i2c_del_adapter(struct i2c_adapter *adap) |
| 230 | { |
| 231 | struct list_head *item, *_n; |
| 232 | struct i2c_adapter *adap_from_list; |
| 233 | struct i2c_driver *driver; |
| 234 | struct i2c_client *client; |
| 235 | int res = 0; |
| 236 | |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 237 | mutex_lock(&core_lists); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | /* First make sure that this adapter was ever added */ |
| 240 | list_for_each_entry(adap_from_list, &adapters, list) { |
| 241 | if (adap_from_list == adap) |
| 242 | break; |
| 243 | } |
| 244 | if (adap_from_list != adap) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 245 | pr_debug("i2c-core: attempting to delete unregistered " |
| 246 | "adapter [%s]\n", adap->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | res = -EINVAL; |
| 248 | goto out_unlock; |
| 249 | } |
| 250 | |
| 251 | list_for_each(item,&drivers) { |
| 252 | driver = list_entry(item, struct i2c_driver, list); |
| 253 | if (driver->detach_adapter) |
| 254 | if ((res = driver->detach_adapter(adap))) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 255 | dev_err(&adap->dev, "detach_adapter failed " |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 256 | "for driver [%s]\n", |
| 257 | driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | goto out_unlock; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* detach any active clients. This must be done first, because |
Tobias Klauser | a551acc | 2005-05-19 21:40:38 +0200 | [diff] [blame] | 263 | * it can fail; in which case we give up. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | list_for_each_safe(item, _n, &adap->clients) { |
| 265 | client = list_entry(item, struct i2c_client, list); |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if ((res=client->driver->detach_client(client))) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 268 | dev_err(&adap->dev, "detach_client failed for client " |
| 269 | "[%s] at address 0x%02x\n", client->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | client->addr); |
| 271 | goto out_unlock; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /* clean up the sysfs representation */ |
| 276 | init_completion(&adap->dev_released); |
| 277 | init_completion(&adap->class_dev_released); |
| 278 | class_device_unregister(&adap->class_dev); |
| 279 | device_remove_file(&adap->dev, &dev_attr_name); |
| 280 | device_unregister(&adap->dev); |
| 281 | list_del(&adap->list); |
| 282 | |
| 283 | /* wait for sysfs to drop all references */ |
| 284 | wait_for_completion(&adap->dev_released); |
| 285 | wait_for_completion(&adap->class_dev_released); |
| 286 | |
| 287 | /* free dynamically allocated bus id */ |
| 288 | idr_remove(&i2c_adapter_idr, adap->nr); |
| 289 | |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 290 | dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | out_unlock: |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 293 | mutex_unlock(&core_lists); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | return res; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | /* ----- |
| 299 | * What follows is the "upwards" interface: commands for talking to clients, |
| 300 | * which implement the functions to access the physical information of the |
| 301 | * chips. |
| 302 | */ |
| 303 | |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 304 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { |
| 306 | struct list_head *item; |
| 307 | struct i2c_adapter *adapter; |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 308 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
| 310 | /* add the driver to the list of i2c drivers in the driver core */ |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 311 | driver->driver.owner = owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | driver->driver.bus = &i2c_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | res = driver_register(&driver->driver); |
| 315 | if (res) |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 316 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 318 | mutex_lock(&core_lists); |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | list_add_tail(&driver->list,&drivers); |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 321 | pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
| 323 | /* now look for instances of driver on our adapters */ |
Jean Delvare | 8a99475 | 2005-11-26 20:28:06 +0100 | [diff] [blame] | 324 | if (driver->attach_adapter) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | list_for_each(item,&adapters) { |
| 326 | adapter = list_entry(item, struct i2c_adapter, list); |
| 327 | driver->attach_adapter(adapter); |
| 328 | } |
| 329 | } |
| 330 | |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 331 | mutex_unlock(&core_lists); |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 332 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 334 | EXPORT_SYMBOL(i2c_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | int i2c_del_driver(struct i2c_driver *driver) |
| 337 | { |
| 338 | struct list_head *item1, *item2, *_n; |
| 339 | struct i2c_client *client; |
| 340 | struct i2c_adapter *adap; |
| 341 | |
| 342 | int res = 0; |
| 343 | |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 344 | mutex_lock(&core_lists); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | /* Have a look at each adapter, if clients of this driver are still |
| 347 | * attached. If so, detach them to be able to kill the driver |
| 348 | * afterwards. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | */ |
| 350 | list_for_each(item1,&adapters) { |
| 351 | adap = list_entry(item1, struct i2c_adapter, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | if (driver->detach_adapter) { |
| 353 | if ((res = driver->detach_adapter(adap))) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 354 | dev_err(&adap->dev, "detach_adapter failed " |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 355 | "for driver [%s]\n", |
| 356 | driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | goto out_unlock; |
| 358 | } |
| 359 | } else { |
| 360 | list_for_each_safe(item2, _n, &adap->clients) { |
| 361 | client = list_entry(item2, struct i2c_client, list); |
| 362 | if (client->driver != driver) |
| 363 | continue; |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 364 | dev_dbg(&adap->dev, "detaching client [%s] " |
| 365 | "at 0x%02x\n", client->name, |
| 366 | client->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | if ((res = driver->detach_client(client))) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 368 | dev_err(&adap->dev, "detach_client " |
| 369 | "failed for client [%s] at " |
| 370 | "0x%02x\n", client->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | client->addr); |
| 372 | goto out_unlock; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | driver_unregister(&driver->driver); |
| 379 | list_del(&driver->list); |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 380 | pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
| 382 | out_unlock: |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 383 | mutex_unlock(&core_lists); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr) |
| 388 | { |
| 389 | struct list_head *item; |
| 390 | struct i2c_client *client; |
| 391 | |
| 392 | list_for_each(item,&adapter->clients) { |
| 393 | client = list_entry(item, struct i2c_client, list); |
| 394 | if (client->addr == addr) |
| 395 | return -EBUSY; |
| 396 | } |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | int i2c_check_addr(struct i2c_adapter *adapter, int addr) |
| 401 | { |
| 402 | int rval; |
| 403 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 404 | mutex_lock(&adapter->clist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | rval = __i2c_check_addr(adapter, addr); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 406 | mutex_unlock(&adapter->clist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
| 408 | return rval; |
| 409 | } |
| 410 | |
| 411 | int i2c_attach_client(struct i2c_client *client) |
| 412 | { |
| 413 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame^] | 414 | int res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 416 | mutex_lock(&adapter->clist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | if (__i2c_check_addr(client->adapter, client->addr)) { |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame^] | 418 | res = -EBUSY; |
| 419 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | list_add_tail(&client->list,&adapter->clients); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
| 423 | if (adapter->client_register) { |
| 424 | if (adapter->client_register(client)) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 425 | dev_dbg(&adapter->dev, "client_register " |
| 426 | "failed for client [%s] at 0x%02x\n", |
| 427 | client->name, client->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Jean Delvare | cde7859 | 2005-11-26 21:00:54 +0100 | [diff] [blame] | 431 | client->usage_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | client->dev.parent = &client->adapter->dev; |
| 434 | client->dev.driver = &client->driver->driver; |
| 435 | client->dev.bus = &i2c_bus_type; |
| 436 | client->dev.release = &i2c_client_release; |
| 437 | |
| 438 | snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id), |
| 439 | "%d-%04x", i2c_adapter_id(adapter), client->addr); |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 440 | dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n", |
| 441 | client->name, client->dev.bus_id); |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame^] | 442 | res = device_register(&client->dev); |
| 443 | if (res) |
| 444 | goto out_list; |
| 445 | res = device_create_file(&client->dev, &dev_attr_client_name); |
| 446 | if (res) |
| 447 | goto out_unregister; |
| 448 | |
| 449 | out_unlock: |
| 450 | mutex_unlock(&adapter->clist_lock); |
| 451 | return res; |
| 452 | |
| 453 | out_unregister: |
| 454 | init_completion(&client->released); /* Needed? */ |
| 455 | device_unregister(&client->dev); |
| 456 | wait_for_completion(&client->released); |
| 457 | out_list: |
| 458 | list_del(&client->list); |
| 459 | dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x " |
| 460 | "(%d)\n", client->name, client->addr, res); |
| 461 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | |
| 465 | int i2c_detach_client(struct i2c_client *client) |
| 466 | { |
| 467 | struct i2c_adapter *adapter = client->adapter; |
| 468 | int res = 0; |
| 469 | |
Jean Delvare | cde7859 | 2005-11-26 21:00:54 +0100 | [diff] [blame] | 470 | if (client->usage_count > 0) { |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 471 | dev_warn(&client->dev, "Client [%s] still busy, " |
| 472 | "can't detach\n", client->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | return -EBUSY; |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 474 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
| 476 | if (adapter->client_unregister) { |
| 477 | res = adapter->client_unregister(client); |
| 478 | if (res) { |
| 479 | dev_err(&client->dev, |
Jean Delvare | 86749e8 | 2005-07-29 12:15:29 -0700 | [diff] [blame] | 480 | "client_unregister [%s] failed, " |
| 481 | "client not detached\n", client->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | goto out; |
| 483 | } |
| 484 | } |
| 485 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 486 | mutex_lock(&adapter->clist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | list_del(&client->list); |
| 488 | init_completion(&client->released); |
| 489 | device_remove_file(&client->dev, &dev_attr_client_name); |
| 490 | device_unregister(&client->dev); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 491 | mutex_unlock(&adapter->clist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | wait_for_completion(&client->released); |
| 493 | |
| 494 | out: |
| 495 | return res; |
| 496 | } |
| 497 | |
| 498 | static int i2c_inc_use_client(struct i2c_client *client) |
| 499 | { |
| 500 | |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 501 | if (!try_module_get(client->driver->driver.owner)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | return -ENODEV; |
| 503 | if (!try_module_get(client->adapter->owner)) { |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 504 | module_put(client->driver->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return -ENODEV; |
| 506 | } |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static void i2c_dec_use_client(struct i2c_client *client) |
| 512 | { |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 513 | module_put(client->driver->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | module_put(client->adapter->owner); |
| 515 | } |
| 516 | |
| 517 | int i2c_use_client(struct i2c_client *client) |
| 518 | { |
| 519 | int ret; |
| 520 | |
| 521 | ret = i2c_inc_use_client(client); |
| 522 | if (ret) |
| 523 | return ret; |
| 524 | |
Jean Delvare | cde7859 | 2005-11-26 21:00:54 +0100 | [diff] [blame] | 525 | client->usage_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | int i2c_release_client(struct i2c_client *client) |
| 531 | { |
Jean Delvare | cde7859 | 2005-11-26 21:00:54 +0100 | [diff] [blame] | 532 | if (!client->usage_count) { |
| 533 | pr_debug("i2c-core: %s used one too many times\n", |
| 534 | __FUNCTION__); |
| 535 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Jean Delvare | cde7859 | 2005-11-26 21:00:54 +0100 | [diff] [blame] | 538 | client->usage_count--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | i2c_dec_use_client(client); |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) |
| 545 | { |
| 546 | struct list_head *item; |
| 547 | struct i2c_client *client; |
| 548 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 549 | mutex_lock(&adap->clist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | list_for_each(item,&adap->clients) { |
| 551 | client = list_entry(item, struct i2c_client, list); |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 552 | if (!try_module_get(client->driver->driver.owner)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | continue; |
| 554 | if (NULL != client->driver->command) { |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 555 | mutex_unlock(&adap->clist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | client->driver->command(client,cmd,arg); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 557 | mutex_lock(&adap->clist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 559 | module_put(client->driver->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 561 | mutex_unlock(&adap->clist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static int __init i2c_init(void) |
| 565 | { |
| 566 | int retval; |
| 567 | |
| 568 | retval = bus_register(&i2c_bus_type); |
| 569 | if (retval) |
| 570 | return retval; |
| 571 | retval = driver_register(&i2c_adapter_driver); |
| 572 | if (retval) |
| 573 | return retval; |
| 574 | return class_register(&i2c_adapter_class); |
| 575 | } |
| 576 | |
| 577 | static void __exit i2c_exit(void) |
| 578 | { |
| 579 | class_unregister(&i2c_adapter_class); |
| 580 | driver_unregister(&i2c_adapter_driver); |
| 581 | bus_unregister(&i2c_bus_type); |
| 582 | } |
| 583 | |
| 584 | subsys_initcall(i2c_init); |
| 585 | module_exit(i2c_exit); |
| 586 | |
| 587 | /* ---------------------------------------------------- |
| 588 | * the functional interface to the i2c busses. |
| 589 | * ---------------------------------------------------- |
| 590 | */ |
| 591 | |
| 592 | int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num) |
| 593 | { |
| 594 | int ret; |
| 595 | |
| 596 | if (adap->algo->master_xfer) { |
| 597 | #ifdef DEBUG |
| 598 | for (ret = 0; ret < num; ret++) { |
| 599 | dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, " |
| 600 | "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ? |
| 601 | 'R' : 'W', msgs[ret].addr, msgs[ret].len); |
| 602 | } |
| 603 | #endif |
| 604 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 605 | mutex_lock(&adap->bus_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | ret = adap->algo->master_xfer(adap,msgs,num); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 607 | mutex_unlock(&adap->bus_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
| 609 | return ret; |
| 610 | } else { |
| 611 | dev_dbg(&adap->dev, "I2C level transfers not supported\n"); |
| 612 | return -ENOSYS; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | int i2c_master_send(struct i2c_client *client,const char *buf ,int count) |
| 617 | { |
| 618 | int ret; |
| 619 | struct i2c_adapter *adap=client->adapter; |
| 620 | struct i2c_msg msg; |
| 621 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 622 | msg.addr = client->addr; |
| 623 | msg.flags = client->flags & I2C_M_TEN; |
| 624 | msg.len = count; |
| 625 | msg.buf = (char *)buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 627 | ret = i2c_transfer(adap, &msg, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 629 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes |
| 630 | transmitted, else error code. */ |
| 631 | return (ret == 1) ? count : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | int i2c_master_recv(struct i2c_client *client, char *buf ,int count) |
| 635 | { |
| 636 | struct i2c_adapter *adap=client->adapter; |
| 637 | struct i2c_msg msg; |
| 638 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 640 | msg.addr = client->addr; |
| 641 | msg.flags = client->flags & I2C_M_TEN; |
| 642 | msg.flags |= I2C_M_RD; |
| 643 | msg.len = count; |
| 644 | msg.buf = buf; |
| 645 | |
| 646 | ret = i2c_transfer(adap, &msg, 1); |
| 647 | |
| 648 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes |
| 649 | transmitted, else error code. */ |
| 650 | return (ret == 1) ? count : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | |
| 654 | int i2c_control(struct i2c_client *client, |
| 655 | unsigned int cmd, unsigned long arg) |
| 656 | { |
| 657 | int ret = 0; |
| 658 | struct i2c_adapter *adap = client->adapter; |
| 659 | |
| 660 | dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg); |
| 661 | switch (cmd) { |
| 662 | case I2C_RETRIES: |
| 663 | adap->retries = arg; |
| 664 | break; |
| 665 | case I2C_TIMEOUT: |
| 666 | adap->timeout = arg; |
| 667 | break; |
| 668 | default: |
| 669 | if (adap->algo->algo_control!=NULL) |
| 670 | ret = adap->algo->algo_control(adap,cmd,arg); |
| 671 | } |
| 672 | return ret; |
| 673 | } |
| 674 | |
| 675 | /* ---------------------------------------------------- |
| 676 | * the i2c address scanning function |
| 677 | * Will not work for 10-bit addresses! |
| 678 | * ---------------------------------------------------- |
| 679 | */ |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 680 | static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind, |
| 681 | int (*found_proc) (struct i2c_adapter *, int, int)) |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 682 | { |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 683 | int err; |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 684 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 685 | /* Make sure the address is valid */ |
| 686 | if (addr < 0x03 || addr > 0x77) { |
| 687 | dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", |
| 688 | addr); |
| 689 | return -EINVAL; |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 692 | /* Skip if already in use */ |
| 693 | if (i2c_check_addr(adapter, addr)) |
| 694 | return 0; |
| 695 | |
| 696 | /* Make sure there is something at this address, unless forced */ |
Jean Delvare | 4c9337d | 2005-08-09 20:28:10 +0200 | [diff] [blame] | 697 | if (kind < 0) { |
| 698 | if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, |
| 699 | I2C_SMBUS_QUICK, NULL) < 0) |
| 700 | return 0; |
| 701 | |
| 702 | /* prevent 24RF08 corruption */ |
| 703 | if ((addr & ~0x0f) == 0x50) |
| 704 | i2c_smbus_xfer(adapter, addr, 0, 0, 0, |
| 705 | I2C_SMBUS_QUICK, NULL); |
| 706 | } |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 707 | |
| 708 | /* Finally call the custom detection function */ |
| 709 | err = found_proc(adapter, addr, kind); |
| 710 | |
| 711 | /* -ENODEV can be returned if there is a chip at the given address |
| 712 | but it isn't supported by this chip driver. We catch it here as |
| 713 | this isn't an error. */ |
| 714 | return (err == -ENODEV) ? 0 : err; |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 715 | } |
| 716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | int i2c_probe(struct i2c_adapter *adapter, |
| 718 | struct i2c_client_address_data *address_data, |
| 719 | int (*found_proc) (struct i2c_adapter *, int, int)) |
| 720 | { |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 721 | int i, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | int adap_id = i2c_adapter_id(adapter); |
| 723 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 724 | /* Force entries are done first, and are not affected by ignore |
| 725 | entries */ |
| 726 | if (address_data->forces) { |
| 727 | unsigned short **forces = address_data->forces; |
| 728 | int kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 730 | for (kind = 0; forces[kind]; kind++) { |
| 731 | for (i = 0; forces[kind][i] != I2C_CLIENT_END; |
| 732 | i += 2) { |
| 733 | if (forces[kind][i] == adap_id |
| 734 | || forces[kind][i] == ANY_I2C_BUS) { |
| 735 | dev_dbg(&adapter->dev, "found force " |
| 736 | "parameter for adapter %d, " |
| 737 | "addr 0x%02x, kind %d\n", |
| 738 | adap_id, forces[kind][i + 1], |
| 739 | kind); |
| 740 | err = i2c_probe_address(adapter, |
| 741 | forces[kind][i + 1], |
| 742 | kind, found_proc); |
| 743 | if (err) |
| 744 | return err; |
| 745 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 749 | |
Jean Delvare | 4366dc9 | 2005-09-25 16:50:06 +0200 | [diff] [blame] | 750 | /* Stop here if we can't use SMBUS_QUICK */ |
| 751 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { |
| 752 | if (address_data->probe[0] == I2C_CLIENT_END |
| 753 | && address_data->normal_i2c[0] == I2C_CLIENT_END) |
| 754 | return 0; |
| 755 | |
| 756 | dev_warn(&adapter->dev, "SMBus Quick command not supported, " |
| 757 | "can't probe for chips\n"); |
| 758 | return -1; |
| 759 | } |
| 760 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 761 | /* Probe entries are done second, and are not affected by ignore |
| 762 | entries either */ |
| 763 | for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) { |
| 764 | if (address_data->probe[i] == adap_id |
| 765 | || address_data->probe[i] == ANY_I2C_BUS) { |
| 766 | dev_dbg(&adapter->dev, "found probe parameter for " |
| 767 | "adapter %d, addr 0x%02x\n", adap_id, |
| 768 | address_data->probe[i + 1]); |
| 769 | err = i2c_probe_address(adapter, |
| 770 | address_data->probe[i + 1], |
| 771 | -1, found_proc); |
| 772 | if (err) |
| 773 | return err; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | /* Normal entries are done last, unless shadowed by an ignore entry */ |
| 778 | for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) { |
| 779 | int j, ignore; |
| 780 | |
| 781 | ignore = 0; |
| 782 | for (j = 0; address_data->ignore[j] != I2C_CLIENT_END; |
| 783 | j += 2) { |
| 784 | if ((address_data->ignore[j] == adap_id || |
| 785 | address_data->ignore[j] == ANY_I2C_BUS) |
| 786 | && address_data->ignore[j + 1] |
| 787 | == address_data->normal_i2c[i]) { |
| 788 | dev_dbg(&adapter->dev, "found ignore " |
| 789 | "parameter for adapter %d, " |
| 790 | "addr 0x%02x\n", adap_id, |
| 791 | address_data->ignore[j + 1]); |
Mark M. Hoffman | 2369df9 | 2006-07-01 17:01:59 +0200 | [diff] [blame] | 792 | ignore = 1; |
| 793 | break; |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 794 | } |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 795 | } |
| 796 | if (ignore) |
| 797 | continue; |
| 798 | |
| 799 | dev_dbg(&adapter->dev, "found normal entry for adapter %d, " |
| 800 | "addr 0x%02x\n", adap_id, |
| 801 | address_data->normal_i2c[i]); |
| 802 | err = i2c_probe_address(adapter, address_data->normal_i2c[i], |
| 803 | -1, found_proc); |
| 804 | if (err) |
| 805 | return err; |
| 806 | } |
| 807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | return 0; |
| 809 | } |
| 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | struct i2c_adapter* i2c_get_adapter(int id) |
| 812 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | struct i2c_adapter *adapter; |
| 814 | |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 815 | mutex_lock(&core_lists); |
Mark M. Hoffman | a0920e1 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 816 | adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id); |
| 817 | if (adapter && !try_module_get(adapter->owner)) |
| 818 | adapter = NULL; |
| 819 | |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 820 | mutex_unlock(&core_lists); |
Mark M. Hoffman | a0920e1 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 821 | return adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | void i2c_put_adapter(struct i2c_adapter *adap) |
| 825 | { |
| 826 | module_put(adap->owner); |
| 827 | } |
| 828 | |
| 829 | /* The SMBus parts */ |
| 830 | |
| 831 | #define POLY (0x1070U << 3) |
| 832 | static u8 |
| 833 | crc8(u16 data) |
| 834 | { |
| 835 | int i; |
| 836 | |
| 837 | for(i = 0; i < 8; i++) { |
| 838 | if (data & 0x8000) |
| 839 | data = data ^ POLY; |
| 840 | data = data << 1; |
| 841 | } |
| 842 | return (u8)(data >> 8); |
| 843 | } |
| 844 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 845 | /* Incremental CRC8 over count bytes in the array pointed to by p */ |
| 846 | static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | { |
| 848 | int i; |
| 849 | |
| 850 | for(i = 0; i < count; i++) |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 851 | crc = crc8((crc ^ p[i]) << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | return crc; |
| 853 | } |
| 854 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 855 | /* Assume a 7-bit address, which is reasonable for SMBus */ |
| 856 | static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 858 | /* The address will be sent first */ |
| 859 | u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD); |
| 860 | pec = i2c_smbus_pec(pec, &addr, 1); |
| 861 | |
| 862 | /* The data buffer follows */ |
| 863 | return i2c_smbus_pec(pec, msg->buf, msg->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 866 | /* Used for write only transactions */ |
| 867 | static inline void i2c_smbus_add_pec(struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 869 | msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); |
| 870 | msg->len++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 873 | /* Return <0 on CRC error |
| 874 | If there was a write before this read (most cases) we need to take the |
| 875 | partial CRC from the write part into account. |
| 876 | Note that this function does modify the message (we need to decrease the |
| 877 | message length to hide the CRC byte from the caller). */ |
| 878 | static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 880 | u8 rpec = msg->buf[--msg->len]; |
| 881 | cpec = i2c_smbus_msg_pec(cpec, msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | if (rpec != cpec) { |
| 884 | pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n", |
| 885 | rpec, cpec); |
| 886 | return -1; |
| 887 | } |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value) |
| 892 | { |
| 893 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 894 | value,0,I2C_SMBUS_QUICK,NULL); |
| 895 | } |
| 896 | |
| 897 | s32 i2c_smbus_read_byte(struct i2c_client *client) |
| 898 | { |
| 899 | union i2c_smbus_data data; |
| 900 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 901 | I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data)) |
| 902 | return -1; |
| 903 | else |
| 904 | return 0x0FF & data.byte; |
| 905 | } |
| 906 | |
| 907 | s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value) |
| 908 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 910 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command) |
| 914 | { |
| 915 | union i2c_smbus_data data; |
| 916 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 917 | I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data)) |
| 918 | return -1; |
| 919 | else |
| 920 | return 0x0FF & data.byte; |
| 921 | } |
| 922 | |
| 923 | s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value) |
| 924 | { |
| 925 | union i2c_smbus_data data; |
| 926 | data.byte = value; |
| 927 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 928 | I2C_SMBUS_WRITE,command, |
| 929 | I2C_SMBUS_BYTE_DATA,&data); |
| 930 | } |
| 931 | |
| 932 | s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command) |
| 933 | { |
| 934 | union i2c_smbus_data data; |
| 935 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 936 | I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data)) |
| 937 | return -1; |
| 938 | else |
| 939 | return 0x0FFFF & data.word; |
| 940 | } |
| 941 | |
| 942 | s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value) |
| 943 | { |
| 944 | union i2c_smbus_data data; |
| 945 | data.word = value; |
| 946 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 947 | I2C_SMBUS_WRITE,command, |
| 948 | I2C_SMBUS_WORD_DATA,&data); |
| 949 | } |
| 950 | |
| 951 | s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command, |
Krzysztof Halasa | 46f5ed7 | 2006-06-12 21:42:20 +0200 | [diff] [blame] | 952 | u8 length, const u8 *values) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | { |
| 954 | union i2c_smbus_data data; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 957 | length = I2C_SMBUS_BLOCK_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | data.block[0] = length; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 959 | memcpy(&data.block[1], values, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 961 | I2C_SMBUS_WRITE,command, |
| 962 | I2C_SMBUS_BLOCK_DATA,&data); |
| 963 | } |
| 964 | |
| 965 | /* Returns the number of read bytes */ |
| 966 | s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values) |
| 967 | { |
| 968 | union i2c_smbus_data data; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 971 | I2C_SMBUS_READ,command, |
| 972 | I2C_SMBUS_I2C_BLOCK_DATA,&data)) |
| 973 | return -1; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 974 | |
| 975 | memcpy(values, &data.block[1], data.block[0]); |
| 976 | return data.block[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 979 | s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command, |
Krzysztof Halasa | 46f5ed7 | 2006-06-12 21:42:20 +0200 | [diff] [blame] | 980 | u8 length, const u8 *values) |
Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 981 | { |
| 982 | union i2c_smbus_data data; |
| 983 | |
| 984 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 985 | length = I2C_SMBUS_BLOCK_MAX; |
| 986 | data.block[0] = length; |
| 987 | memcpy(data.block + 1, values, length); |
| 988 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 989 | I2C_SMBUS_WRITE, command, |
| 990 | I2C_SMBUS_I2C_BLOCK_DATA, &data); |
| 991 | } |
| 992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | /* Simulate a SMBus command using the i2c protocol |
| 994 | No checking of parameters is done! */ |
| 995 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr, |
| 996 | unsigned short flags, |
| 997 | char read_write, u8 command, int size, |
| 998 | union i2c_smbus_data * data) |
| 999 | { |
| 1000 | /* So we need to generate a series of msgs. In the case of writing, we |
| 1001 | need to use only one message; when reading, we need two. We initialize |
| 1002 | most things with sane defaults, to keep the code below somewhat |
| 1003 | simpler. */ |
Hideki Iwamoto | 5c50d18 | 2005-09-25 17:01:11 +0200 | [diff] [blame] | 1004 | unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; |
| 1005 | unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | int num = read_write == I2C_SMBUS_READ?2:1; |
| 1007 | struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 }, |
| 1008 | { addr, flags | I2C_M_RD, 0, msgbuf1 } |
| 1009 | }; |
| 1010 | int i; |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1011 | u8 partial_pec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
| 1013 | msgbuf0[0] = command; |
| 1014 | switch(size) { |
| 1015 | case I2C_SMBUS_QUICK: |
| 1016 | msg[0].len = 0; |
| 1017 | /* Special case: The read/write field is used as data */ |
| 1018 | msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0; |
| 1019 | num = 1; |
| 1020 | break; |
| 1021 | case I2C_SMBUS_BYTE: |
| 1022 | if (read_write == I2C_SMBUS_READ) { |
| 1023 | /* Special case: only a read! */ |
| 1024 | msg[0].flags = I2C_M_RD | flags; |
| 1025 | num = 1; |
| 1026 | } |
| 1027 | break; |
| 1028 | case I2C_SMBUS_BYTE_DATA: |
| 1029 | if (read_write == I2C_SMBUS_READ) |
| 1030 | msg[1].len = 1; |
| 1031 | else { |
| 1032 | msg[0].len = 2; |
| 1033 | msgbuf0[1] = data->byte; |
| 1034 | } |
| 1035 | break; |
| 1036 | case I2C_SMBUS_WORD_DATA: |
| 1037 | if (read_write == I2C_SMBUS_READ) |
| 1038 | msg[1].len = 2; |
| 1039 | else { |
| 1040 | msg[0].len=3; |
| 1041 | msgbuf0[1] = data->word & 0xff; |
| 1042 | msgbuf0[2] = (data->word >> 8) & 0xff; |
| 1043 | } |
| 1044 | break; |
| 1045 | case I2C_SMBUS_PROC_CALL: |
| 1046 | num = 2; /* Special case */ |
| 1047 | read_write = I2C_SMBUS_READ; |
| 1048 | msg[0].len = 3; |
| 1049 | msg[1].len = 2; |
| 1050 | msgbuf0[1] = data->word & 0xff; |
| 1051 | msgbuf0[2] = (data->word >> 8) & 0xff; |
| 1052 | break; |
| 1053 | case I2C_SMBUS_BLOCK_DATA: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | if (read_write == I2C_SMBUS_READ) { |
| 1055 | dev_err(&adapter->dev, "Block read not supported " |
| 1056 | "under I2C emulation!\n"); |
| 1057 | return -1; |
| 1058 | } else { |
| 1059 | msg[0].len = data->block[0] + 2; |
| 1060 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { |
| 1061 | dev_err(&adapter->dev, "smbus_access called with " |
| 1062 | "invalid block write size (%d)\n", |
| 1063 | data->block[0]); |
| 1064 | return -1; |
| 1065 | } |
Hideki Iwamoto | 5c50d18 | 2005-09-25 17:01:11 +0200 | [diff] [blame] | 1066 | for (i = 1; i < msg[0].len; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | msgbuf0[i] = data->block[i-1]; |
| 1068 | } |
| 1069 | break; |
| 1070 | case I2C_SMBUS_BLOCK_PROC_CALL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | dev_dbg(&adapter->dev, "Block process call not supported " |
| 1072 | "under I2C emulation!\n"); |
| 1073 | return -1; |
| 1074 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 1075 | if (read_write == I2C_SMBUS_READ) { |
Jean Delvare | 30dac74 | 2005-10-08 00:15:59 +0200 | [diff] [blame] | 1076 | msg[1].len = I2C_SMBUS_BLOCK_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } else { |
| 1078 | msg[0].len = data->block[0] + 1; |
Jean Delvare | 30dac74 | 2005-10-08 00:15:59 +0200 | [diff] [blame] | 1079 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with " |
| 1081 | "invalid block write size (%d)\n", |
| 1082 | data->block[0]); |
| 1083 | return -1; |
| 1084 | } |
| 1085 | for (i = 1; i <= data->block[0]; i++) |
| 1086 | msgbuf0[i] = data->block[i]; |
| 1087 | } |
| 1088 | break; |
| 1089 | default: |
| 1090 | dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n", |
| 1091 | size); |
| 1092 | return -1; |
| 1093 | } |
| 1094 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1095 | i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK |
| 1096 | && size != I2C_SMBUS_I2C_BLOCK_DATA); |
| 1097 | if (i) { |
| 1098 | /* Compute PEC if first message is a write */ |
| 1099 | if (!(msg[0].flags & I2C_M_RD)) { |
| 1100 | if (num == 1) /* Write only */ |
| 1101 | i2c_smbus_add_pec(&msg[0]); |
| 1102 | else /* Write followed by read */ |
| 1103 | partial_pec = i2c_smbus_msg_pec(0, &msg[0]); |
| 1104 | } |
| 1105 | /* Ask for PEC if last message is a read */ |
| 1106 | if (msg[num-1].flags & I2C_M_RD) |
| 1107 | msg[num-1].len++; |
| 1108 | } |
| 1109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | if (i2c_transfer(adapter, msg, num) < 0) |
| 1111 | return -1; |
| 1112 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1113 | /* Check PEC if last message is a read */ |
| 1114 | if (i && (msg[num-1].flags & I2C_M_RD)) { |
| 1115 | if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0) |
| 1116 | return -1; |
| 1117 | } |
| 1118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | if (read_write == I2C_SMBUS_READ) |
| 1120 | switch(size) { |
| 1121 | case I2C_SMBUS_BYTE: |
| 1122 | data->byte = msgbuf0[0]; |
| 1123 | break; |
| 1124 | case I2C_SMBUS_BYTE_DATA: |
| 1125 | data->byte = msgbuf1[0]; |
| 1126 | break; |
| 1127 | case I2C_SMBUS_WORD_DATA: |
| 1128 | case I2C_SMBUS_PROC_CALL: |
| 1129 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); |
| 1130 | break; |
| 1131 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 1132 | /* fixed at 32 for now */ |
Jean Delvare | 30dac74 | 2005-10-08 00:15:59 +0200 | [diff] [blame] | 1133 | data->block[0] = I2C_SMBUS_BLOCK_MAX; |
| 1134 | for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | data->block[i+1] = msgbuf1[i]; |
| 1136 | break; |
| 1137 | } |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | |
| 1142 | s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags, |
| 1143 | char read_write, u8 command, int size, |
| 1144 | union i2c_smbus_data * data) |
| 1145 | { |
| 1146 | s32 res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | |
| 1148 | flags &= I2C_M_TEN | I2C_CLIENT_PEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | |
| 1150 | if (adapter->algo->smbus_xfer) { |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 1151 | mutex_lock(&adapter->bus_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write, |
| 1153 | command,size,data); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 1154 | mutex_unlock(&adapter->bus_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | } else |
| 1156 | res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, |
| 1157 | command,size,data); |
| 1158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | return res; |
| 1160 | } |
| 1161 | |
| 1162 | |
Jean Delvare | efde723 | 2005-07-20 23:03:50 +0200 | [diff] [blame] | 1163 | /* Next four are needed by i2c-isa */ |
| 1164 | EXPORT_SYMBOL_GPL(i2c_adapter_dev_release); |
| 1165 | EXPORT_SYMBOL_GPL(i2c_adapter_driver); |
| 1166 | EXPORT_SYMBOL_GPL(i2c_adapter_class); |
| 1167 | EXPORT_SYMBOL_GPL(i2c_bus_type); |
| 1168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | EXPORT_SYMBOL(i2c_add_adapter); |
| 1170 | EXPORT_SYMBOL(i2c_del_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | EXPORT_SYMBOL(i2c_del_driver); |
| 1172 | EXPORT_SYMBOL(i2c_attach_client); |
| 1173 | EXPORT_SYMBOL(i2c_detach_client); |
| 1174 | EXPORT_SYMBOL(i2c_use_client); |
| 1175 | EXPORT_SYMBOL(i2c_release_client); |
| 1176 | EXPORT_SYMBOL(i2c_clients_command); |
| 1177 | EXPORT_SYMBOL(i2c_check_addr); |
| 1178 | |
| 1179 | EXPORT_SYMBOL(i2c_master_send); |
| 1180 | EXPORT_SYMBOL(i2c_master_recv); |
| 1181 | EXPORT_SYMBOL(i2c_control); |
| 1182 | EXPORT_SYMBOL(i2c_transfer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | EXPORT_SYMBOL(i2c_get_adapter); |
| 1184 | EXPORT_SYMBOL(i2c_put_adapter); |
| 1185 | EXPORT_SYMBOL(i2c_probe); |
| 1186 | |
| 1187 | EXPORT_SYMBOL(i2c_smbus_xfer); |
| 1188 | EXPORT_SYMBOL(i2c_smbus_write_quick); |
| 1189 | EXPORT_SYMBOL(i2c_smbus_read_byte); |
| 1190 | EXPORT_SYMBOL(i2c_smbus_write_byte); |
| 1191 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
| 1192 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
| 1193 | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
| 1194 | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
| 1195 | EXPORT_SYMBOL(i2c_smbus_write_block_data); |
| 1196 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); |
Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 1197 | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | |
| 1199 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); |
| 1200 | MODULE_DESCRIPTION("I2C-Bus main module"); |
| 1201 | MODULE_LICENSE("GPL"); |