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