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