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