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