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