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