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); |
Jean Delvare | 8a99475 | 2005-11-26 20:28:06 +0100 | [diff] [blame] | 200 | if (driver->attach_adapter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 " |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 238 | "for driver [%s]\n", |
| 239 | driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | goto out_unlock; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* detach any active clients. This must be done first, because |
Tobias Klauser | a551acc | 2005-05-19 21:40:38 +0200 | [diff] [blame] | 245 | * it can fail; in which case we give up. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | list_for_each_safe(item, _n, &adap->clients) { |
| 247 | client = list_entry(item, struct i2c_client, list); |
| 248 | |
| 249 | /* detaching devices is unconditional of the set notify |
| 250 | * flag, as _all_ clients that reside on the adapter |
| 251 | * must be deleted, as this would cause invalid states. |
| 252 | */ |
| 253 | if ((res=client->driver->detach_client(client))) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 254 | dev_err(&adap->dev, "detach_client failed for client " |
| 255 | "[%s] at address 0x%02x\n", client->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | client->addr); |
| 257 | goto out_unlock; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* clean up the sysfs representation */ |
| 262 | init_completion(&adap->dev_released); |
| 263 | init_completion(&adap->class_dev_released); |
| 264 | class_device_unregister(&adap->class_dev); |
| 265 | device_remove_file(&adap->dev, &dev_attr_name); |
| 266 | device_unregister(&adap->dev); |
| 267 | list_del(&adap->list); |
| 268 | |
| 269 | /* wait for sysfs to drop all references */ |
| 270 | wait_for_completion(&adap->dev_released); |
| 271 | wait_for_completion(&adap->class_dev_released); |
| 272 | |
| 273 | /* free dynamically allocated bus id */ |
| 274 | idr_remove(&i2c_adapter_idr, adap->nr); |
| 275 | |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 276 | dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
| 278 | out_unlock: |
| 279 | up(&core_lists); |
| 280 | return res; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | /* ----- |
| 285 | * What follows is the "upwards" interface: commands for talking to clients, |
| 286 | * which implement the functions to access the physical information of the |
| 287 | * chips. |
| 288 | */ |
| 289 | |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame^] | 290 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
| 292 | struct list_head *item; |
| 293 | struct i2c_adapter *adapter; |
| 294 | int res = 0; |
| 295 | |
| 296 | down(&core_lists); |
| 297 | |
| 298 | /* 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^] | 299 | driver->driver.owner = owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 309 | pr_debug("i2c-core: driver [%s] registered\n", driver->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 */ |
Jean Delvare | 8a99475 | 2005-11-26 20:28:06 +0100 | [diff] [blame] | 312 | if (driver->attach_adapter) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame^] | 323 | EXPORT_SYMBOL(i2c_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
| 325 | int i2c_del_driver(struct i2c_driver *driver) |
| 326 | { |
| 327 | struct list_head *item1, *item2, *_n; |
| 328 | struct i2c_client *client; |
| 329 | struct i2c_adapter *adap; |
| 330 | |
| 331 | int res = 0; |
| 332 | |
| 333 | down(&core_lists); |
| 334 | |
| 335 | /* Have a look at each adapter, if clients of this driver are still |
| 336 | * attached. If so, detach them to be able to kill the driver |
| 337 | * afterwards. |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 338 | * |
| 339 | * Removing clients does not depend on the notify flag, else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | * invalid operation might (will!) result, when using stale client |
| 341 | * pointers. |
| 342 | */ |
| 343 | list_for_each(item1,&adapters) { |
| 344 | adap = list_entry(item1, struct i2c_adapter, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | if (driver->detach_adapter) { |
| 346 | if ((res = driver->detach_adapter(adap))) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 347 | dev_err(&adap->dev, "detach_adapter failed " |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 348 | "for driver [%s]\n", |
| 349 | driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | goto out_unlock; |
| 351 | } |
| 352 | } else { |
| 353 | list_for_each_safe(item2, _n, &adap->clients) { |
| 354 | client = list_entry(item2, struct i2c_client, list); |
| 355 | if (client->driver != driver) |
| 356 | continue; |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 357 | dev_dbg(&adap->dev, "detaching client [%s] " |
| 358 | "at 0x%02x\n", client->name, |
| 359 | client->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | if ((res = driver->detach_client(client))) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 361 | dev_err(&adap->dev, "detach_client " |
| 362 | "failed for client [%s] at " |
| 363 | "0x%02x\n", client->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | client->addr); |
| 365 | goto out_unlock; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | driver_unregister(&driver->driver); |
| 372 | list_del(&driver->list); |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 373 | pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | out_unlock: |
| 376 | up(&core_lists); |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr) |
| 381 | { |
| 382 | struct list_head *item; |
| 383 | struct i2c_client *client; |
| 384 | |
| 385 | list_for_each(item,&adapter->clients) { |
| 386 | client = list_entry(item, struct i2c_client, list); |
| 387 | if (client->addr == addr) |
| 388 | return -EBUSY; |
| 389 | } |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | int i2c_check_addr(struct i2c_adapter *adapter, int addr) |
| 394 | { |
| 395 | int rval; |
| 396 | |
| 397 | down(&adapter->clist_lock); |
| 398 | rval = __i2c_check_addr(adapter, addr); |
| 399 | up(&adapter->clist_lock); |
| 400 | |
| 401 | return rval; |
| 402 | } |
| 403 | |
| 404 | int i2c_attach_client(struct i2c_client *client) |
| 405 | { |
| 406 | struct i2c_adapter *adapter = client->adapter; |
| 407 | |
| 408 | down(&adapter->clist_lock); |
| 409 | if (__i2c_check_addr(client->adapter, client->addr)) { |
| 410 | up(&adapter->clist_lock); |
| 411 | return -EBUSY; |
| 412 | } |
| 413 | list_add_tail(&client->list,&adapter->clients); |
| 414 | up(&adapter->clist_lock); |
| 415 | |
| 416 | if (adapter->client_register) { |
| 417 | if (adapter->client_register(client)) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 418 | dev_dbg(&adapter->dev, "client_register " |
| 419 | "failed for client [%s] at 0x%02x\n", |
| 420 | client->name, client->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Jean Delvare | cde7859 | 2005-11-26 21:00:54 +0100 | [diff] [blame] | 424 | client->usage_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | client->dev.parent = &client->adapter->dev; |
| 427 | client->dev.driver = &client->driver->driver; |
| 428 | client->dev.bus = &i2c_bus_type; |
| 429 | client->dev.release = &i2c_client_release; |
| 430 | |
| 431 | snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id), |
| 432 | "%d-%04x", i2c_adapter_id(adapter), client->addr); |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 433 | dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n", |
| 434 | client->name, client->dev.bus_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | device_register(&client->dev); |
| 436 | device_create_file(&client->dev, &dev_attr_client_name); |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | int i2c_detach_client(struct i2c_client *client) |
| 443 | { |
| 444 | struct i2c_adapter *adapter = client->adapter; |
| 445 | int res = 0; |
| 446 | |
Jean Delvare | cde7859 | 2005-11-26 21:00:54 +0100 | [diff] [blame] | 447 | if (client->usage_count > 0) { |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 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 | |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 478 | if (!try_module_get(client->driver->driver.owner)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | return -ENODEV; |
| 480 | if (!try_module_get(client->adapter->owner)) { |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 481 | module_put(client->driver->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | return -ENODEV; |
| 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static void i2c_dec_use_client(struct i2c_client *client) |
| 489 | { |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 490 | module_put(client->driver->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jean Delvare | cde7859 | 2005-11-26 21:00:54 +0100 | [diff] [blame] | 502 | client->usage_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
| 504 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | int i2c_release_client(struct i2c_client *client) |
| 508 | { |
Jean Delvare | cde7859 | 2005-11-26 21:00:54 +0100 | [diff] [blame] | 509 | if (!client->usage_count) { |
| 510 | pr_debug("i2c-core: %s used one too many times\n", |
| 511 | __FUNCTION__); |
| 512 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Jean Delvare | cde7859 | 2005-11-26 21:00:54 +0100 | [diff] [blame] | 515 | client->usage_count--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | i2c_dec_use_client(client); |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) |
| 522 | { |
| 523 | struct list_head *item; |
| 524 | struct i2c_client *client; |
| 525 | |
| 526 | down(&adap->clist_lock); |
| 527 | list_for_each(item,&adap->clients) { |
| 528 | client = list_entry(item, struct i2c_client, list); |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 529 | if (!try_module_get(client->driver->driver.owner)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | continue; |
| 531 | if (NULL != client->driver->command) { |
| 532 | up(&adap->clist_lock); |
| 533 | client->driver->command(client,cmd,arg); |
| 534 | down(&adap->clist_lock); |
| 535 | } |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 536 | module_put(client->driver->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | up(&adap->clist_lock); |
| 539 | } |
| 540 | |
| 541 | static int __init i2c_init(void) |
| 542 | { |
| 543 | int retval; |
| 544 | |
| 545 | retval = bus_register(&i2c_bus_type); |
| 546 | if (retval) |
| 547 | return retval; |
| 548 | retval = driver_register(&i2c_adapter_driver); |
| 549 | if (retval) |
| 550 | return retval; |
| 551 | return class_register(&i2c_adapter_class); |
| 552 | } |
| 553 | |
| 554 | static void __exit i2c_exit(void) |
| 555 | { |
| 556 | class_unregister(&i2c_adapter_class); |
| 557 | driver_unregister(&i2c_adapter_driver); |
| 558 | bus_unregister(&i2c_bus_type); |
| 559 | } |
| 560 | |
| 561 | subsys_initcall(i2c_init); |
| 562 | module_exit(i2c_exit); |
| 563 | |
| 564 | /* ---------------------------------------------------- |
| 565 | * the functional interface to the i2c busses. |
| 566 | * ---------------------------------------------------- |
| 567 | */ |
| 568 | |
| 569 | int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num) |
| 570 | { |
| 571 | int ret; |
| 572 | |
| 573 | if (adap->algo->master_xfer) { |
| 574 | #ifdef DEBUG |
| 575 | for (ret = 0; ret < num; ret++) { |
| 576 | dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, " |
| 577 | "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ? |
| 578 | 'R' : 'W', msgs[ret].addr, msgs[ret].len); |
| 579 | } |
| 580 | #endif |
| 581 | |
| 582 | down(&adap->bus_lock); |
| 583 | ret = adap->algo->master_xfer(adap,msgs,num); |
| 584 | up(&adap->bus_lock); |
| 585 | |
| 586 | return ret; |
| 587 | } else { |
| 588 | dev_dbg(&adap->dev, "I2C level transfers not supported\n"); |
| 589 | return -ENOSYS; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | int i2c_master_send(struct i2c_client *client,const char *buf ,int count) |
| 594 | { |
| 595 | int ret; |
| 596 | struct i2c_adapter *adap=client->adapter; |
| 597 | struct i2c_msg msg; |
| 598 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 599 | msg.addr = client->addr; |
| 600 | msg.flags = client->flags & I2C_M_TEN; |
| 601 | msg.len = count; |
| 602 | msg.buf = (char *)buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 604 | ret = i2c_transfer(adap, &msg, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 606 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes |
| 607 | transmitted, else error code. */ |
| 608 | return (ret == 1) ? count : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | int i2c_master_recv(struct i2c_client *client, char *buf ,int count) |
| 612 | { |
| 613 | struct i2c_adapter *adap=client->adapter; |
| 614 | struct i2c_msg msg; |
| 615 | int ret; |
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 | msg.addr = client->addr; |
| 618 | msg.flags = client->flags & I2C_M_TEN; |
| 619 | msg.flags |= I2C_M_RD; |
| 620 | msg.len = count; |
| 621 | msg.buf = buf; |
| 622 | |
| 623 | ret = i2c_transfer(adap, &msg, 1); |
| 624 | |
| 625 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes |
| 626 | transmitted, else error code. */ |
| 627 | return (ret == 1) ? count : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | |
| 631 | int i2c_control(struct i2c_client *client, |
| 632 | unsigned int cmd, unsigned long arg) |
| 633 | { |
| 634 | int ret = 0; |
| 635 | struct i2c_adapter *adap = client->adapter; |
| 636 | |
| 637 | dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg); |
| 638 | switch (cmd) { |
| 639 | case I2C_RETRIES: |
| 640 | adap->retries = arg; |
| 641 | break; |
| 642 | case I2C_TIMEOUT: |
| 643 | adap->timeout = arg; |
| 644 | break; |
| 645 | default: |
| 646 | if (adap->algo->algo_control!=NULL) |
| 647 | ret = adap->algo->algo_control(adap,cmd,arg); |
| 648 | } |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | /* ---------------------------------------------------- |
| 653 | * the i2c address scanning function |
| 654 | * Will not work for 10-bit addresses! |
| 655 | * ---------------------------------------------------- |
| 656 | */ |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 657 | static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind, |
| 658 | int (*found_proc) (struct i2c_adapter *, int, int)) |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 659 | { |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 660 | int err; |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 661 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 662 | /* Make sure the address is valid */ |
| 663 | if (addr < 0x03 || addr > 0x77) { |
| 664 | dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", |
| 665 | addr); |
| 666 | return -EINVAL; |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 667 | } |
| 668 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 669 | /* Skip if already in use */ |
| 670 | if (i2c_check_addr(adapter, addr)) |
| 671 | return 0; |
| 672 | |
| 673 | /* Make sure there is something at this address, unless forced */ |
Jean Delvare | 4c9337d | 2005-08-09 20:28:10 +0200 | [diff] [blame] | 674 | if (kind < 0) { |
| 675 | if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, |
| 676 | I2C_SMBUS_QUICK, NULL) < 0) |
| 677 | return 0; |
| 678 | |
| 679 | /* prevent 24RF08 corruption */ |
| 680 | if ((addr & ~0x0f) == 0x50) |
| 681 | i2c_smbus_xfer(adapter, addr, 0, 0, 0, |
| 682 | I2C_SMBUS_QUICK, NULL); |
| 683 | } |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 684 | |
| 685 | /* Finally call the custom detection function */ |
| 686 | err = found_proc(adapter, addr, kind); |
| 687 | |
| 688 | /* -ENODEV can be returned if there is a chip at the given address |
| 689 | but it isn't supported by this chip driver. We catch it here as |
| 690 | this isn't an error. */ |
| 691 | return (err == -ENODEV) ? 0 : err; |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 692 | } |
| 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | int i2c_probe(struct i2c_adapter *adapter, |
| 695 | struct i2c_client_address_data *address_data, |
| 696 | int (*found_proc) (struct i2c_adapter *, int, int)) |
| 697 | { |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 698 | int i, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | int adap_id = i2c_adapter_id(adapter); |
| 700 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 701 | /* Force entries are done first, and are not affected by ignore |
| 702 | entries */ |
| 703 | if (address_data->forces) { |
| 704 | unsigned short **forces = address_data->forces; |
| 705 | int kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 707 | for (kind = 0; forces[kind]; kind++) { |
| 708 | for (i = 0; forces[kind][i] != I2C_CLIENT_END; |
| 709 | i += 2) { |
| 710 | if (forces[kind][i] == adap_id |
| 711 | || forces[kind][i] == ANY_I2C_BUS) { |
| 712 | dev_dbg(&adapter->dev, "found force " |
| 713 | "parameter for adapter %d, " |
| 714 | "addr 0x%02x, kind %d\n", |
| 715 | adap_id, forces[kind][i + 1], |
| 716 | kind); |
| 717 | err = i2c_probe_address(adapter, |
| 718 | forces[kind][i + 1], |
| 719 | kind, found_proc); |
| 720 | if (err) |
| 721 | return err; |
| 722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | } |
| 724 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 726 | |
Jean Delvare | 4366dc9 | 2005-09-25 16:50:06 +0200 | [diff] [blame] | 727 | /* Stop here if we can't use SMBUS_QUICK */ |
| 728 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { |
| 729 | if (address_data->probe[0] == I2C_CLIENT_END |
| 730 | && address_data->normal_i2c[0] == I2C_CLIENT_END) |
| 731 | return 0; |
| 732 | |
| 733 | dev_warn(&adapter->dev, "SMBus Quick command not supported, " |
| 734 | "can't probe for chips\n"); |
| 735 | return -1; |
| 736 | } |
| 737 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 738 | /* Probe entries are done second, and are not affected by ignore |
| 739 | entries either */ |
| 740 | for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) { |
| 741 | if (address_data->probe[i] == adap_id |
| 742 | || address_data->probe[i] == ANY_I2C_BUS) { |
| 743 | dev_dbg(&adapter->dev, "found probe parameter for " |
| 744 | "adapter %d, addr 0x%02x\n", adap_id, |
| 745 | address_data->probe[i + 1]); |
| 746 | err = i2c_probe_address(adapter, |
| 747 | address_data->probe[i + 1], |
| 748 | -1, found_proc); |
| 749 | if (err) |
| 750 | return err; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | /* Normal entries are done last, unless shadowed by an ignore entry */ |
| 755 | for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) { |
| 756 | int j, ignore; |
| 757 | |
| 758 | ignore = 0; |
| 759 | for (j = 0; address_data->ignore[j] != I2C_CLIENT_END; |
| 760 | j += 2) { |
| 761 | if ((address_data->ignore[j] == adap_id || |
| 762 | address_data->ignore[j] == ANY_I2C_BUS) |
| 763 | && address_data->ignore[j + 1] |
| 764 | == address_data->normal_i2c[i]) { |
| 765 | dev_dbg(&adapter->dev, "found ignore " |
| 766 | "parameter for adapter %d, " |
| 767 | "addr 0x%02x\n", adap_id, |
| 768 | address_data->ignore[j + 1]); |
| 769 | } |
| 770 | ignore = 1; |
| 771 | break; |
| 772 | } |
| 773 | if (ignore) |
| 774 | continue; |
| 775 | |
| 776 | dev_dbg(&adapter->dev, "found normal entry for adapter %d, " |
| 777 | "addr 0x%02x\n", adap_id, |
| 778 | address_data->normal_i2c[i]); |
| 779 | err = i2c_probe_address(adapter, address_data->normal_i2c[i], |
| 780 | -1, found_proc); |
| 781 | if (err) |
| 782 | return err; |
| 783 | } |
| 784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | return 0; |
| 786 | } |
| 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | struct i2c_adapter* i2c_get_adapter(int id) |
| 789 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | struct i2c_adapter *adapter; |
| 791 | |
| 792 | down(&core_lists); |
Mark M. Hoffman | a0920e1 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 793 | adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id); |
| 794 | if (adapter && !try_module_get(adapter->owner)) |
| 795 | adapter = NULL; |
| 796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | up(&core_lists); |
Mark M. Hoffman | a0920e1 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 798 | return adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | void i2c_put_adapter(struct i2c_adapter *adap) |
| 802 | { |
| 803 | module_put(adap->owner); |
| 804 | } |
| 805 | |
| 806 | /* The SMBus parts */ |
| 807 | |
| 808 | #define POLY (0x1070U << 3) |
| 809 | static u8 |
| 810 | crc8(u16 data) |
| 811 | { |
| 812 | int i; |
| 813 | |
| 814 | for(i = 0; i < 8; i++) { |
| 815 | if (data & 0x8000) |
| 816 | data = data ^ POLY; |
| 817 | data = data << 1; |
| 818 | } |
| 819 | return (u8)(data >> 8); |
| 820 | } |
| 821 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 822 | /* Incremental CRC8 over count bytes in the array pointed to by p */ |
| 823 | static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | { |
| 825 | int i; |
| 826 | |
| 827 | for(i = 0; i < count; i++) |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 828 | crc = crc8((crc ^ p[i]) << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | return crc; |
| 830 | } |
| 831 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 832 | /* Assume a 7-bit address, which is reasonable for SMBus */ |
| 833 | static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 835 | /* The address will be sent first */ |
| 836 | u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD); |
| 837 | pec = i2c_smbus_pec(pec, &addr, 1); |
| 838 | |
| 839 | /* The data buffer follows */ |
| 840 | return i2c_smbus_pec(pec, msg->buf, msg->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 843 | /* Used for write only transactions */ |
| 844 | static inline void i2c_smbus_add_pec(struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 846 | msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); |
| 847 | msg->len++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 850 | /* Return <0 on CRC error |
| 851 | If there was a write before this read (most cases) we need to take the |
| 852 | partial CRC from the write part into account. |
| 853 | Note that this function does modify the message (we need to decrease the |
| 854 | message length to hide the CRC byte from the caller). */ |
| 855 | static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 857 | u8 rpec = msg->buf[--msg->len]; |
| 858 | cpec = i2c_smbus_msg_pec(cpec, msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | if (rpec != cpec) { |
| 861 | pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n", |
| 862 | rpec, cpec); |
| 863 | return -1; |
| 864 | } |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value) |
| 869 | { |
| 870 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 871 | value,0,I2C_SMBUS_QUICK,NULL); |
| 872 | } |
| 873 | |
| 874 | s32 i2c_smbus_read_byte(struct i2c_client *client) |
| 875 | { |
| 876 | union i2c_smbus_data data; |
| 877 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 878 | I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data)) |
| 879 | return -1; |
| 880 | else |
| 881 | return 0x0FF & data.byte; |
| 882 | } |
| 883 | |
| 884 | s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value) |
| 885 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 887 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command) |
| 891 | { |
| 892 | union i2c_smbus_data data; |
| 893 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 894 | I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data)) |
| 895 | return -1; |
| 896 | else |
| 897 | return 0x0FF & data.byte; |
| 898 | } |
| 899 | |
| 900 | s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value) |
| 901 | { |
| 902 | union i2c_smbus_data data; |
| 903 | data.byte = value; |
| 904 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 905 | I2C_SMBUS_WRITE,command, |
| 906 | I2C_SMBUS_BYTE_DATA,&data); |
| 907 | } |
| 908 | |
| 909 | s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command) |
| 910 | { |
| 911 | union i2c_smbus_data data; |
| 912 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 913 | I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data)) |
| 914 | return -1; |
| 915 | else |
| 916 | return 0x0FFFF & data.word; |
| 917 | } |
| 918 | |
| 919 | s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value) |
| 920 | { |
| 921 | union i2c_smbus_data data; |
| 922 | data.word = value; |
| 923 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 924 | I2C_SMBUS_WRITE,command, |
| 925 | I2C_SMBUS_WORD_DATA,&data); |
| 926 | } |
| 927 | |
| 928 | s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command, |
| 929 | u8 length, u8 *values) |
| 930 | { |
| 931 | union i2c_smbus_data data; |
| 932 | int i; |
| 933 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 934 | length = I2C_SMBUS_BLOCK_MAX; |
| 935 | for (i = 1; i <= length; i++) |
| 936 | data.block[i] = values[i-1]; |
| 937 | data.block[0] = length; |
| 938 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 939 | I2C_SMBUS_WRITE,command, |
| 940 | I2C_SMBUS_BLOCK_DATA,&data); |
| 941 | } |
| 942 | |
| 943 | /* Returns the number of read bytes */ |
| 944 | s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values) |
| 945 | { |
| 946 | union i2c_smbus_data data; |
| 947 | int i; |
| 948 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 949 | I2C_SMBUS_READ,command, |
| 950 | I2C_SMBUS_I2C_BLOCK_DATA,&data)) |
| 951 | return -1; |
| 952 | else { |
| 953 | for (i = 1; i <= data.block[0]; i++) |
| 954 | values[i-1] = data.block[i]; |
| 955 | return data.block[0]; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | /* Simulate a SMBus command using the i2c protocol |
| 960 | No checking of parameters is done! */ |
| 961 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr, |
| 962 | unsigned short flags, |
| 963 | char read_write, u8 command, int size, |
| 964 | union i2c_smbus_data * data) |
| 965 | { |
| 966 | /* So we need to generate a series of msgs. In the case of writing, we |
| 967 | need to use only one message; when reading, we need two. We initialize |
| 968 | most things with sane defaults, to keep the code below somewhat |
| 969 | simpler. */ |
Hideki Iwamoto | 5c50d18 | 2005-09-25 17:01:11 +0200 | [diff] [blame] | 970 | unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; |
| 971 | unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | int num = read_write == I2C_SMBUS_READ?2:1; |
| 973 | struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 }, |
| 974 | { addr, flags | I2C_M_RD, 0, msgbuf1 } |
| 975 | }; |
| 976 | int i; |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 977 | u8 partial_pec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | |
| 979 | msgbuf0[0] = command; |
| 980 | switch(size) { |
| 981 | case I2C_SMBUS_QUICK: |
| 982 | msg[0].len = 0; |
| 983 | /* Special case: The read/write field is used as data */ |
| 984 | msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0; |
| 985 | num = 1; |
| 986 | break; |
| 987 | case I2C_SMBUS_BYTE: |
| 988 | if (read_write == I2C_SMBUS_READ) { |
| 989 | /* Special case: only a read! */ |
| 990 | msg[0].flags = I2C_M_RD | flags; |
| 991 | num = 1; |
| 992 | } |
| 993 | break; |
| 994 | case I2C_SMBUS_BYTE_DATA: |
| 995 | if (read_write == I2C_SMBUS_READ) |
| 996 | msg[1].len = 1; |
| 997 | else { |
| 998 | msg[0].len = 2; |
| 999 | msgbuf0[1] = data->byte; |
| 1000 | } |
| 1001 | break; |
| 1002 | case I2C_SMBUS_WORD_DATA: |
| 1003 | if (read_write == I2C_SMBUS_READ) |
| 1004 | msg[1].len = 2; |
| 1005 | else { |
| 1006 | msg[0].len=3; |
| 1007 | msgbuf0[1] = data->word & 0xff; |
| 1008 | msgbuf0[2] = (data->word >> 8) & 0xff; |
| 1009 | } |
| 1010 | break; |
| 1011 | case I2C_SMBUS_PROC_CALL: |
| 1012 | num = 2; /* Special case */ |
| 1013 | read_write = I2C_SMBUS_READ; |
| 1014 | msg[0].len = 3; |
| 1015 | msg[1].len = 2; |
| 1016 | msgbuf0[1] = data->word & 0xff; |
| 1017 | msgbuf0[2] = (data->word >> 8) & 0xff; |
| 1018 | break; |
| 1019 | case I2C_SMBUS_BLOCK_DATA: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | if (read_write == I2C_SMBUS_READ) { |
| 1021 | dev_err(&adapter->dev, "Block read not supported " |
| 1022 | "under I2C emulation!\n"); |
| 1023 | return -1; |
| 1024 | } else { |
| 1025 | msg[0].len = data->block[0] + 2; |
| 1026 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { |
| 1027 | dev_err(&adapter->dev, "smbus_access called with " |
| 1028 | "invalid block write size (%d)\n", |
| 1029 | data->block[0]); |
| 1030 | return -1; |
| 1031 | } |
Hideki Iwamoto | 5c50d18 | 2005-09-25 17:01:11 +0200 | [diff] [blame] | 1032 | for (i = 1; i < msg[0].len; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | msgbuf0[i] = data->block[i-1]; |
| 1034 | } |
| 1035 | break; |
| 1036 | case I2C_SMBUS_BLOCK_PROC_CALL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | dev_dbg(&adapter->dev, "Block process call not supported " |
| 1038 | "under I2C emulation!\n"); |
| 1039 | return -1; |
| 1040 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 1041 | if (read_write == I2C_SMBUS_READ) { |
Jean Delvare | 30dac74 | 2005-10-08 00:15:59 +0200 | [diff] [blame] | 1042 | msg[1].len = I2C_SMBUS_BLOCK_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | } else { |
| 1044 | msg[0].len = data->block[0] + 1; |
Jean Delvare | 30dac74 | 2005-10-08 00:15:59 +0200 | [diff] [blame] | 1045 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with " |
| 1047 | "invalid block write size (%d)\n", |
| 1048 | data->block[0]); |
| 1049 | return -1; |
| 1050 | } |
| 1051 | for (i = 1; i <= data->block[0]; i++) |
| 1052 | msgbuf0[i] = data->block[i]; |
| 1053 | } |
| 1054 | break; |
| 1055 | default: |
| 1056 | dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n", |
| 1057 | size); |
| 1058 | return -1; |
| 1059 | } |
| 1060 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1061 | i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK |
| 1062 | && size != I2C_SMBUS_I2C_BLOCK_DATA); |
| 1063 | if (i) { |
| 1064 | /* Compute PEC if first message is a write */ |
| 1065 | if (!(msg[0].flags & I2C_M_RD)) { |
| 1066 | if (num == 1) /* Write only */ |
| 1067 | i2c_smbus_add_pec(&msg[0]); |
| 1068 | else /* Write followed by read */ |
| 1069 | partial_pec = i2c_smbus_msg_pec(0, &msg[0]); |
| 1070 | } |
| 1071 | /* Ask for PEC if last message is a read */ |
| 1072 | if (msg[num-1].flags & I2C_M_RD) |
| 1073 | msg[num-1].len++; |
| 1074 | } |
| 1075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | if (i2c_transfer(adapter, msg, num) < 0) |
| 1077 | return -1; |
| 1078 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1079 | /* Check PEC if last message is a read */ |
| 1080 | if (i && (msg[num-1].flags & I2C_M_RD)) { |
| 1081 | if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0) |
| 1082 | return -1; |
| 1083 | } |
| 1084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | if (read_write == I2C_SMBUS_READ) |
| 1086 | switch(size) { |
| 1087 | case I2C_SMBUS_BYTE: |
| 1088 | data->byte = msgbuf0[0]; |
| 1089 | break; |
| 1090 | case I2C_SMBUS_BYTE_DATA: |
| 1091 | data->byte = msgbuf1[0]; |
| 1092 | break; |
| 1093 | case I2C_SMBUS_WORD_DATA: |
| 1094 | case I2C_SMBUS_PROC_CALL: |
| 1095 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); |
| 1096 | break; |
| 1097 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 1098 | /* fixed at 32 for now */ |
Jean Delvare | 30dac74 | 2005-10-08 00:15:59 +0200 | [diff] [blame] | 1099 | data->block[0] = I2C_SMBUS_BLOCK_MAX; |
| 1100 | for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | data->block[i+1] = msgbuf1[i]; |
| 1102 | break; |
| 1103 | } |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags, |
| 1109 | char read_write, u8 command, int size, |
| 1110 | union i2c_smbus_data * data) |
| 1111 | { |
| 1112 | s32 res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | |
| 1114 | flags &= I2C_M_TEN | I2C_CLIENT_PEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | if (adapter->algo->smbus_xfer) { |
| 1117 | down(&adapter->bus_lock); |
| 1118 | res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write, |
| 1119 | command,size,data); |
| 1120 | up(&adapter->bus_lock); |
| 1121 | } else |
| 1122 | res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, |
| 1123 | command,size,data); |
| 1124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | return res; |
| 1126 | } |
| 1127 | |
| 1128 | |
Jean Delvare | efde723 | 2005-07-20 23:03:50 +0200 | [diff] [blame] | 1129 | /* Next four are needed by i2c-isa */ |
| 1130 | EXPORT_SYMBOL_GPL(i2c_adapter_dev_release); |
| 1131 | EXPORT_SYMBOL_GPL(i2c_adapter_driver); |
| 1132 | EXPORT_SYMBOL_GPL(i2c_adapter_class); |
| 1133 | EXPORT_SYMBOL_GPL(i2c_bus_type); |
| 1134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | EXPORT_SYMBOL(i2c_add_adapter); |
| 1136 | EXPORT_SYMBOL(i2c_del_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | EXPORT_SYMBOL(i2c_del_driver); |
| 1138 | EXPORT_SYMBOL(i2c_attach_client); |
| 1139 | EXPORT_SYMBOL(i2c_detach_client); |
| 1140 | EXPORT_SYMBOL(i2c_use_client); |
| 1141 | EXPORT_SYMBOL(i2c_release_client); |
| 1142 | EXPORT_SYMBOL(i2c_clients_command); |
| 1143 | EXPORT_SYMBOL(i2c_check_addr); |
| 1144 | |
| 1145 | EXPORT_SYMBOL(i2c_master_send); |
| 1146 | EXPORT_SYMBOL(i2c_master_recv); |
| 1147 | EXPORT_SYMBOL(i2c_control); |
| 1148 | EXPORT_SYMBOL(i2c_transfer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | EXPORT_SYMBOL(i2c_get_adapter); |
| 1150 | EXPORT_SYMBOL(i2c_put_adapter); |
| 1151 | EXPORT_SYMBOL(i2c_probe); |
| 1152 | |
| 1153 | EXPORT_SYMBOL(i2c_smbus_xfer); |
| 1154 | EXPORT_SYMBOL(i2c_smbus_write_quick); |
| 1155 | EXPORT_SYMBOL(i2c_smbus_read_byte); |
| 1156 | EXPORT_SYMBOL(i2c_smbus_write_byte); |
| 1157 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
| 1158 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
| 1159 | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
| 1160 | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
| 1161 | EXPORT_SYMBOL(i2c_smbus_write_block_data); |
| 1162 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); |
| 1163 | |
| 1164 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); |
| 1165 | MODULE_DESCRIPTION("I2C-Bus main module"); |
| 1166 | MODULE_LICENSE("GPL"); |