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