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 | |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 20 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Jean Delvare | b8d6f45 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 33 | #include <linux/completion.h> |
Mike Rapoport | cea443a8 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 34 | #include <linux/hardirq.h> |
| 35 | #include <linux/irqflags.h> |
Rodolfo Giometti | f18c41d | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 36 | #include <linux/rwsem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <asm/uaccess.h> |
| 38 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 39 | #include "i2c-core.h" |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 42 | /* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 43 | that device detection, deletion of detected devices, and attach_adapter |
| 44 | and detach_adapter calls are serialized */ |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 45 | static DEFINE_MUTEX(core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | static DEFINE_IDR(i2c_adapter_idr); |
Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 47 | static LIST_HEAD(userspace_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 49 | static struct device_type i2c_client_type; |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 50 | static int i2c_check_addr(struct i2c_adapter *adapter, int addr); |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 51 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 52 | |
| 53 | /* ------------------------------------------------------------------------- */ |
| 54 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 55 | static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, |
| 56 | const struct i2c_client *client) |
| 57 | { |
| 58 | while (id->name[0]) { |
| 59 | if (strcmp(client->name, id->name) == 0) |
| 60 | return id; |
| 61 | id++; |
| 62 | } |
| 63 | return NULL; |
| 64 | } |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static int i2c_device_match(struct device *dev, struct device_driver *drv) |
| 67 | { |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 68 | struct i2c_client *client = i2c_verify_client(dev); |
| 69 | struct i2c_driver *driver; |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 70 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 71 | if (!client) |
| 72 | return 0; |
| 73 | |
| 74 | driver = to_i2c_driver(drv); |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 75 | /* match on an id table if there is one */ |
| 76 | if (driver->id_table) |
| 77 | return i2c_match_id(driver->id_table, client) != NULL; |
| 78 | |
Jean Delvare | eb8a790 | 2008-05-18 20:49:41 +0200 | [diff] [blame] | 79 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 82 | #ifdef CONFIG_HOTPLUG |
| 83 | |
| 84 | /* uevent helps with hotplug: modprobe -q $(MODALIAS) */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 85 | static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 86 | { |
| 87 | struct i2c_client *client = to_i2c_client(dev); |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 88 | |
Jean Delvare | eb8a790 | 2008-05-18 20:49:41 +0200 | [diff] [blame] | 89 | if (add_uevent_var(env, "MODALIAS=%s%s", |
| 90 | I2C_MODULE_PREFIX, client->name)) |
| 91 | return -ENOMEM; |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 92 | dev_dbg(dev, "uevent\n"); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | #else |
| 97 | #define i2c_device_uevent NULL |
| 98 | #endif /* CONFIG_HOTPLUG */ |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | static int i2c_device_probe(struct device *dev) |
| 101 | { |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 102 | struct i2c_client *client = i2c_verify_client(dev); |
| 103 | struct i2c_driver *driver; |
Hans Verkuil | 50c3304 | 2008-03-12 14:15:00 +0100 | [diff] [blame] | 104 | int status; |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 105 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 106 | if (!client) |
| 107 | return 0; |
| 108 | |
| 109 | driver = to_i2c_driver(dev->driver); |
Jean Delvare | e045744 | 2008-07-14 22:38:30 +0200 | [diff] [blame] | 110 | if (!driver->probe || !driver->id_table) |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 111 | return -ENODEV; |
| 112 | client->driver = driver; |
Marc Pignat | ee35425 | 2008-08-28 08:33:22 +0200 | [diff] [blame] | 113 | if (!device_can_wakeup(&client->dev)) |
| 114 | device_init_wakeup(&client->dev, |
| 115 | client->flags & I2C_CLIENT_WAKE); |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 116 | dev_dbg(dev, "probe\n"); |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 117 | |
Jean Delvare | e045744 | 2008-07-14 22:38:30 +0200 | [diff] [blame] | 118 | status = driver->probe(client, i2c_match_id(driver->id_table, client)); |
Hans Verkuil | 50c3304 | 2008-03-12 14:15:00 +0100 | [diff] [blame] | 119 | if (status) |
| 120 | client->driver = NULL; |
| 121 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static int i2c_device_remove(struct device *dev) |
| 125 | { |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 126 | struct i2c_client *client = i2c_verify_client(dev); |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 127 | struct i2c_driver *driver; |
| 128 | int status; |
| 129 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 130 | if (!client || !dev->driver) |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 131 | return 0; |
| 132 | |
| 133 | driver = to_i2c_driver(dev->driver); |
| 134 | if (driver->remove) { |
| 135 | dev_dbg(dev, "remove\n"); |
| 136 | status = driver->remove(client); |
| 137 | } else { |
| 138 | dev->driver = NULL; |
| 139 | status = 0; |
| 140 | } |
| 141 | if (status == 0) |
| 142 | client->driver = NULL; |
| 143 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 146 | static void i2c_device_shutdown(struct device *dev) |
| 147 | { |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 148 | struct i2c_client *client = i2c_verify_client(dev); |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 149 | struct i2c_driver *driver; |
| 150 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 151 | if (!client || !dev->driver) |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 152 | return; |
| 153 | driver = to_i2c_driver(dev->driver); |
| 154 | if (driver->shutdown) |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 155 | driver->shutdown(client); |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 158 | static int i2c_device_suspend(struct device *dev, pm_message_t mesg) |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 159 | { |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 160 | struct i2c_client *client = i2c_verify_client(dev); |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 161 | struct i2c_driver *driver; |
| 162 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 163 | if (!client || !dev->driver) |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 164 | return 0; |
| 165 | driver = to_i2c_driver(dev->driver); |
| 166 | if (!driver->suspend) |
| 167 | return 0; |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 168 | return driver->suspend(client, mesg); |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 171 | static int i2c_device_resume(struct device *dev) |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 172 | { |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 173 | struct i2c_client *client = i2c_verify_client(dev); |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 174 | struct i2c_driver *driver; |
| 175 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 176 | if (!client || !dev->driver) |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 177 | return 0; |
| 178 | driver = to_i2c_driver(dev->driver); |
| 179 | if (!driver->resume) |
| 180 | return 0; |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 181 | return driver->resume(client); |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 182 | } |
| 183 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 184 | static void i2c_client_dev_release(struct device *dev) |
| 185 | { |
| 186 | kfree(to_i2c_client(dev)); |
| 187 | } |
| 188 | |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 189 | static ssize_t |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 190 | show_name(struct device *dev, struct device_attribute *attr, char *buf) |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 191 | { |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 192 | return sprintf(buf, "%s\n", dev->type == &i2c_client_type ? |
| 193 | to_i2c_client(dev)->name : to_i2c_adapter(dev)->name); |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 196 | static ssize_t |
| 197 | show_modalias(struct device *dev, struct device_attribute *attr, char *buf) |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 198 | { |
| 199 | struct i2c_client *client = to_i2c_client(dev); |
Jean Delvare | eb8a790 | 2008-05-18 20:49:41 +0200 | [diff] [blame] | 200 | return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name); |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 203 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 204 | static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); |
| 205 | |
| 206 | static struct attribute *i2c_dev_attrs[] = { |
| 207 | &dev_attr_name.attr, |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 208 | /* modalias helps coldplug: modprobe $(cat .../modalias) */ |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 209 | &dev_attr_modalias.attr, |
| 210 | NULL |
| 211 | }; |
| 212 | |
| 213 | static struct attribute_group i2c_dev_attr_group = { |
| 214 | .attrs = i2c_dev_attrs, |
| 215 | }; |
| 216 | |
| 217 | static const struct attribute_group *i2c_dev_attr_groups[] = { |
| 218 | &i2c_dev_attr_group, |
| 219 | NULL |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 220 | }; |
| 221 | |
Jon Smirl | e9ca9eb | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 222 | struct bus_type i2c_bus_type = { |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 223 | .name = "i2c", |
| 224 | .match = i2c_device_match, |
| 225 | .probe = i2c_device_probe, |
| 226 | .remove = i2c_device_remove, |
| 227 | .shutdown = i2c_device_shutdown, |
| 228 | .suspend = i2c_device_suspend, |
| 229 | .resume = i2c_device_resume, |
Russell King | b864c7d | 2006-01-05 14:37:50 +0000 | [diff] [blame] | 230 | }; |
Jon Smirl | e9ca9eb | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 231 | EXPORT_SYMBOL_GPL(i2c_bus_type); |
Russell King | b864c7d | 2006-01-05 14:37:50 +0000 | [diff] [blame] | 232 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 233 | static struct device_type i2c_client_type = { |
| 234 | .groups = i2c_dev_attr_groups, |
| 235 | .uevent = i2c_device_uevent, |
| 236 | .release = i2c_client_dev_release, |
| 237 | }; |
| 238 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * i2c_verify_client - return parameter as i2c_client, or NULL |
| 242 | * @dev: device, probably from some driver model iterator |
| 243 | * |
| 244 | * When traversing the driver model tree, perhaps using driver model |
| 245 | * iterators like @device_for_each_child(), you can't assume very much |
| 246 | * about the nodes you find. Use this function to avoid oopses caused |
| 247 | * by wrongly treating some non-I2C device as an i2c_client. |
| 248 | */ |
| 249 | struct i2c_client *i2c_verify_client(struct device *dev) |
| 250 | { |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 251 | return (dev->type == &i2c_client_type) |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 252 | ? to_i2c_client(dev) |
| 253 | : NULL; |
| 254 | } |
| 255 | EXPORT_SYMBOL(i2c_verify_client); |
| 256 | |
| 257 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 258 | /** |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 259 | * i2c_new_device - instantiate an i2c device |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 260 | * @adap: the adapter managing the device |
| 261 | * @info: describes one I2C device; bus_num is ignored |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 262 | * Context: can sleep |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 263 | * |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 264 | * Create an i2c device. Binding is handled through driver model |
| 265 | * probe()/remove() methods. A driver may be bound to this device when we |
| 266 | * return from this function, or any later moment (e.g. maybe hotplugging will |
| 267 | * load the driver module). This call is not appropriate for use by mainboard |
| 268 | * initialization logic, which usually runs during an arch_initcall() long |
| 269 | * before any i2c_adapter could exist. |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 270 | * |
| 271 | * This returns the new i2c client, which may be saved for later use with |
| 272 | * i2c_unregister_device(); or NULL to indicate an error. |
| 273 | */ |
| 274 | struct i2c_client * |
| 275 | i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) |
| 276 | { |
| 277 | struct i2c_client *client; |
| 278 | int status; |
| 279 | |
| 280 | client = kzalloc(sizeof *client, GFP_KERNEL); |
| 281 | if (!client) |
| 282 | return NULL; |
| 283 | |
| 284 | client->adapter = adap; |
| 285 | |
| 286 | client->dev.platform_data = info->platform_data; |
David Brownell | 3bbb835 | 2007-10-13 23:56:29 +0200 | [diff] [blame] | 287 | |
Anton Vorontsov | 11f1f2a | 2008-10-22 20:21:33 +0200 | [diff] [blame] | 288 | if (info->archdata) |
| 289 | client->dev.archdata = *info->archdata; |
| 290 | |
Marc Pignat | ee35425 | 2008-08-28 08:33:22 +0200 | [diff] [blame] | 291 | client->flags = info->flags; |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 292 | client->addr = info->addr; |
| 293 | client->irq = info->irq; |
| 294 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 295 | strlcpy(client->name, info->type, sizeof(client->name)); |
| 296 | |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 297 | /* Check for address business */ |
| 298 | status = i2c_check_addr(adap, client->addr); |
| 299 | if (status) |
| 300 | goto out_err; |
| 301 | |
| 302 | client->dev.parent = &client->adapter->dev; |
| 303 | client->dev.bus = &i2c_bus_type; |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 304 | client->dev.type = &i2c_client_type; |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 305 | |
| 306 | dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), |
| 307 | client->addr); |
| 308 | status = device_register(&client->dev); |
| 309 | if (status) |
| 310 | goto out_err; |
| 311 | |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 312 | dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", |
| 313 | client->name, dev_name(&client->dev)); |
| 314 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 315 | return client; |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 316 | |
| 317 | out_err: |
| 318 | dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x " |
| 319 | "(%d)\n", client->name, client->addr, status); |
| 320 | kfree(client); |
| 321 | return NULL; |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 322 | } |
| 323 | EXPORT_SYMBOL_GPL(i2c_new_device); |
| 324 | |
| 325 | |
| 326 | /** |
| 327 | * i2c_unregister_device - reverse effect of i2c_new_device() |
| 328 | * @client: value returned from i2c_new_device() |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 329 | * Context: can sleep |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 330 | */ |
| 331 | void i2c_unregister_device(struct i2c_client *client) |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 332 | { |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 333 | device_unregister(&client->dev); |
| 334 | } |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 335 | EXPORT_SYMBOL_GPL(i2c_unregister_device); |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 336 | |
| 337 | |
Jean Delvare | 60b129d | 2008-05-11 20:37:06 +0200 | [diff] [blame] | 338 | static const struct i2c_device_id dummy_id[] = { |
| 339 | { "dummy", 0 }, |
| 340 | { }, |
| 341 | }; |
| 342 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 343 | static int dummy_probe(struct i2c_client *client, |
| 344 | const struct i2c_device_id *id) |
| 345 | { |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int dummy_remove(struct i2c_client *client) |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 350 | { |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static struct i2c_driver dummy_driver = { |
| 355 | .driver.name = "dummy", |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 356 | .probe = dummy_probe, |
| 357 | .remove = dummy_remove, |
Jean Delvare | 60b129d | 2008-05-11 20:37:06 +0200 | [diff] [blame] | 358 | .id_table = dummy_id, |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | /** |
| 362 | * i2c_new_dummy - return a new i2c device bound to a dummy driver |
| 363 | * @adapter: the adapter managing the device |
| 364 | * @address: seven bit address to be used |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 365 | * Context: can sleep |
| 366 | * |
| 367 | * This returns an I2C client bound to the "dummy" driver, intended for use |
| 368 | * with devices that consume multiple addresses. Examples of such chips |
| 369 | * include various EEPROMS (like 24c04 and 24c08 models). |
| 370 | * |
| 371 | * These dummy devices have two main uses. First, most I2C and SMBus calls |
| 372 | * except i2c_transfer() need a client handle; the dummy will be that handle. |
| 373 | * And second, this prevents the specified address from being bound to a |
| 374 | * different driver. |
| 375 | * |
| 376 | * This returns the new i2c client, which should be saved for later use with |
| 377 | * i2c_unregister_device(); or NULL to indicate an error. |
| 378 | */ |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 379 | struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address) |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 380 | { |
| 381 | struct i2c_board_info info = { |
Jean Delvare | 60b129d | 2008-05-11 20:37:06 +0200 | [diff] [blame] | 382 | I2C_BOARD_INFO("dummy", address), |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 383 | }; |
| 384 | |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 385 | return i2c_new_device(adapter, &info); |
| 386 | } |
| 387 | EXPORT_SYMBOL_GPL(i2c_new_dummy); |
| 388 | |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 389 | /* ------------------------------------------------------------------------- */ |
| 390 | |
David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 391 | /* I2C bus adapters -- one roots each I2C or SMBUS segment */ |
| 392 | |
Adrian Bunk | 83eaaed | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 393 | static void i2c_adapter_dev_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
David Brownell | ef2c8321 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 395 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | complete(&adap->dev_released); |
| 397 | } |
| 398 | |
Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 399 | /* |
| 400 | * Let users instantiate I2C devices through sysfs. This can be used when |
| 401 | * platform initialization code doesn't contain the proper data for |
| 402 | * whatever reason. Also useful for drivers that do device detection and |
| 403 | * detection fails, either because the device uses an unexpected address, |
| 404 | * or this is a compatible device with different ID register values. |
| 405 | * |
| 406 | * Parameter checking may look overzealous, but we really don't want |
| 407 | * the user to provide incorrect parameters. |
| 408 | */ |
| 409 | static ssize_t |
| 410 | i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr, |
| 411 | const char *buf, size_t count) |
| 412 | { |
| 413 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
| 414 | struct i2c_board_info info; |
| 415 | struct i2c_client *client; |
| 416 | char *blank, end; |
| 417 | int res; |
| 418 | |
| 419 | dev_warn(dev, "The new_device interface is still experimental " |
| 420 | "and may change in a near future\n"); |
| 421 | memset(&info, 0, sizeof(struct i2c_board_info)); |
| 422 | |
| 423 | blank = strchr(buf, ' '); |
| 424 | if (!blank) { |
| 425 | dev_err(dev, "%s: Missing parameters\n", "new_device"); |
| 426 | return -EINVAL; |
| 427 | } |
| 428 | if (blank - buf > I2C_NAME_SIZE - 1) { |
| 429 | dev_err(dev, "%s: Invalid device name\n", "new_device"); |
| 430 | return -EINVAL; |
| 431 | } |
| 432 | memcpy(info.type, buf, blank - buf); |
| 433 | |
| 434 | /* Parse remaining parameters, reject extra parameters */ |
| 435 | res = sscanf(++blank, "%hi%c", &info.addr, &end); |
| 436 | if (res < 1) { |
| 437 | dev_err(dev, "%s: Can't parse I2C address\n", "new_device"); |
| 438 | return -EINVAL; |
| 439 | } |
| 440 | if (res > 1 && end != '\n') { |
| 441 | dev_err(dev, "%s: Extra parameters\n", "new_device"); |
| 442 | return -EINVAL; |
| 443 | } |
| 444 | |
| 445 | if (info.addr < 0x03 || info.addr > 0x77) { |
| 446 | dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device", |
| 447 | info.addr); |
| 448 | return -EINVAL; |
| 449 | } |
| 450 | |
| 451 | client = i2c_new_device(adap, &info); |
| 452 | if (!client) |
| 453 | return -EEXIST; |
| 454 | |
| 455 | /* Keep track of the added device */ |
| 456 | mutex_lock(&core_lock); |
| 457 | list_add_tail(&client->detected, &userspace_devices); |
| 458 | mutex_unlock(&core_lock); |
| 459 | dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device", |
| 460 | info.type, info.addr); |
| 461 | |
| 462 | return count; |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * And of course let the users delete the devices they instantiated, if |
| 467 | * they got it wrong. This interface can only be used to delete devices |
| 468 | * instantiated by i2c_sysfs_new_device above. This guarantees that we |
| 469 | * don't delete devices to which some kernel code still has references. |
| 470 | * |
| 471 | * Parameter checking may look overzealous, but we really don't want |
| 472 | * the user to delete the wrong device. |
| 473 | */ |
| 474 | static ssize_t |
| 475 | i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr, |
| 476 | const char *buf, size_t count) |
| 477 | { |
| 478 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
| 479 | struct i2c_client *client, *next; |
| 480 | unsigned short addr; |
| 481 | char end; |
| 482 | int res; |
| 483 | |
| 484 | /* Parse parameters, reject extra parameters */ |
| 485 | res = sscanf(buf, "%hi%c", &addr, &end); |
| 486 | if (res < 1) { |
| 487 | dev_err(dev, "%s: Can't parse I2C address\n", "delete_device"); |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | if (res > 1 && end != '\n') { |
| 491 | dev_err(dev, "%s: Extra parameters\n", "delete_device"); |
| 492 | return -EINVAL; |
| 493 | } |
| 494 | |
| 495 | /* Make sure the device was added through sysfs */ |
| 496 | res = -ENOENT; |
| 497 | mutex_lock(&core_lock); |
| 498 | list_for_each_entry_safe(client, next, &userspace_devices, detected) { |
| 499 | if (client->addr == addr && client->adapter == adap) { |
| 500 | dev_info(dev, "%s: Deleting device %s at 0x%02hx\n", |
| 501 | "delete_device", client->name, client->addr); |
| 502 | |
| 503 | list_del(&client->detected); |
| 504 | i2c_unregister_device(client); |
| 505 | res = count; |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | mutex_unlock(&core_lock); |
| 510 | |
| 511 | if (res < 0) |
| 512 | dev_err(dev, "%s: Can't find device in list\n", |
| 513 | "delete_device"); |
| 514 | return res; |
| 515 | } |
| 516 | |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 517 | static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device); |
| 518 | static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device); |
| 519 | |
| 520 | static struct attribute *i2c_adapter_attrs[] = { |
| 521 | &dev_attr_name.attr, |
| 522 | &dev_attr_new_device.attr, |
| 523 | &dev_attr_delete_device.attr, |
| 524 | NULL |
David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 525 | }; |
| 526 | |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 527 | static struct attribute_group i2c_adapter_attr_group = { |
| 528 | .attrs = i2c_adapter_attrs, |
| 529 | }; |
| 530 | |
| 531 | static const struct attribute_group *i2c_adapter_attr_groups[] = { |
| 532 | &i2c_adapter_attr_group, |
| 533 | NULL |
| 534 | }; |
| 535 | |
| 536 | static struct device_type i2c_adapter_type = { |
| 537 | .groups = i2c_adapter_attr_groups, |
| 538 | .release = i2c_adapter_dev_release, |
David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 539 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame^] | 541 | #ifdef CONFIG_I2C_COMPAT |
| 542 | static struct class_compat *i2c_adapter_compat_class; |
| 543 | #endif |
| 544 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 545 | static void i2c_scan_static_board_info(struct i2c_adapter *adapter) |
| 546 | { |
| 547 | struct i2c_devinfo *devinfo; |
| 548 | |
Rodolfo Giometti | f18c41d | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 549 | down_read(&__i2c_board_lock); |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 550 | list_for_each_entry(devinfo, &__i2c_board_list, list) { |
| 551 | if (devinfo->busnum == adapter->nr |
| 552 | && !i2c_new_device(adapter, |
| 553 | &devinfo->board_info)) |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 554 | dev_err(&adapter->dev, |
| 555 | "Can't create device at 0x%02x\n", |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 556 | devinfo->board_info.addr); |
| 557 | } |
Rodolfo Giometti | f18c41d | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 558 | up_read(&__i2c_board_lock); |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 561 | static int i2c_do_add_adapter(struct device_driver *d, void *data) |
| 562 | { |
| 563 | struct i2c_driver *driver = to_i2c_driver(d); |
| 564 | struct i2c_adapter *adap = data; |
| 565 | |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 566 | /* Detect supported devices on that bus, and instantiate them */ |
| 567 | i2c_detect(adap, driver); |
| 568 | |
| 569 | /* Let legacy drivers scan this bus for matching devices */ |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 570 | if (driver->attach_adapter) { |
| 571 | /* We ignore the return code; if it fails, too bad */ |
| 572 | driver->attach_adapter(adap); |
| 573 | } |
| 574 | return 0; |
| 575 | } |
| 576 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 577 | static int i2c_register_adapter(struct i2c_adapter *adap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 579 | int res = 0, dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
David Brownell | 1d0b19c | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 581 | /* Can't register until after driver model init */ |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 582 | if (unlikely(WARN_ON(!i2c_bus_type.p))) { |
| 583 | res = -EAGAIN; |
| 584 | goto out_list; |
| 585 | } |
David Brownell | 1d0b19c | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 586 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 587 | mutex_init(&adap->bus_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
Jean Delvare | 8fcfef6 | 2009-03-28 21:34:43 +0100 | [diff] [blame] | 589 | /* Set default timeout to 1 second if not already set */ |
| 590 | if (adap->timeout == 0) |
| 591 | adap->timeout = HZ; |
| 592 | |
Kay Sievers | 27d9c18 | 2009-01-07 14:29:16 +0100 | [diff] [blame] | 593 | dev_set_name(&adap->dev, "i2c-%d", adap->nr); |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 594 | adap->dev.bus = &i2c_bus_type; |
| 595 | adap->dev.type = &i2c_adapter_type; |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 596 | res = device_register(&adap->dev); |
| 597 | if (res) |
| 598 | goto out_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 600 | dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); |
| 601 | |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame^] | 602 | #ifdef CONFIG_I2C_COMPAT |
| 603 | res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev, |
| 604 | adap->dev.parent); |
| 605 | if (res) |
| 606 | dev_warn(&adap->dev, |
| 607 | "Failed to create compatibility class link\n"); |
| 608 | #endif |
| 609 | |
Jean Delvare | 729d6dd | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 610 | /* create pre-declared device nodes */ |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 611 | if (adap->nr < __i2c_first_dynamic_bus_num) |
| 612 | i2c_scan_static_board_info(adap); |
| 613 | |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 614 | /* Notify drivers */ |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 615 | mutex_lock(&core_lock); |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 616 | dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
| 617 | i2c_do_add_adapter); |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 618 | mutex_unlock(&core_lock); |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 619 | |
| 620 | return 0; |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 621 | |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 622 | out_list: |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 623 | mutex_lock(&core_lock); |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 624 | idr_remove(&i2c_adapter_idr, adap->nr); |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 625 | mutex_unlock(&core_lock); |
| 626 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 629 | /** |
| 630 | * i2c_add_adapter - declare i2c adapter, use dynamic bus number |
| 631 | * @adapter: the adapter to add |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 632 | * Context: can sleep |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 633 | * |
| 634 | * This routine is used to declare an I2C adapter when its bus number |
| 635 | * doesn't matter. Examples: for I2C adapters dynamically added by |
| 636 | * USB links or PCI plugin cards. |
| 637 | * |
| 638 | * When this returns zero, a new bus number was allocated and stored |
| 639 | * in adap->nr, and the specified adapter became available for clients. |
| 640 | * Otherwise, a negative errno value is returned. |
| 641 | */ |
| 642 | int i2c_add_adapter(struct i2c_adapter *adapter) |
| 643 | { |
| 644 | int id, res = 0; |
| 645 | |
| 646 | retry: |
| 647 | if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) |
| 648 | return -ENOMEM; |
| 649 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 650 | mutex_lock(&core_lock); |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 651 | /* "above" here means "above or equal to", sigh */ |
| 652 | res = idr_get_new_above(&i2c_adapter_idr, adapter, |
| 653 | __i2c_first_dynamic_bus_num, &id); |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 654 | mutex_unlock(&core_lock); |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 655 | |
| 656 | if (res < 0) { |
| 657 | if (res == -EAGAIN) |
| 658 | goto retry; |
| 659 | return res; |
| 660 | } |
| 661 | |
| 662 | adapter->nr = id; |
| 663 | return i2c_register_adapter(adapter); |
| 664 | } |
| 665 | EXPORT_SYMBOL(i2c_add_adapter); |
| 666 | |
| 667 | /** |
| 668 | * i2c_add_numbered_adapter - declare i2c adapter, use static bus number |
| 669 | * @adap: the adapter to register (with adap->nr initialized) |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 670 | * Context: can sleep |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 671 | * |
| 672 | * This routine is used to declare an I2C adapter when its bus number |
Randy Dunlap | 8c07e46 | 2008-03-23 20:28:20 +0100 | [diff] [blame] | 673 | * matters. For example, use it for I2C adapters from system-on-chip CPUs, |
| 674 | * or otherwise built in to the system's mainboard, and where i2c_board_info |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 675 | * is used to properly configure I2C devices. |
| 676 | * |
| 677 | * If no devices have pre-been declared for this bus, then be sure to |
| 678 | * register the adapter before any dynamically allocated ones. Otherwise |
| 679 | * the required bus ID may not be available. |
| 680 | * |
| 681 | * When this returns zero, the specified adapter became available for |
| 682 | * clients using the bus number provided in adap->nr. Also, the table |
| 683 | * of I2C devices pre-declared using i2c_register_board_info() is scanned, |
| 684 | * and the appropriate driver model device nodes are created. Otherwise, a |
| 685 | * negative errno value is returned. |
| 686 | */ |
| 687 | int i2c_add_numbered_adapter(struct i2c_adapter *adap) |
| 688 | { |
| 689 | int id; |
| 690 | int status; |
| 691 | |
| 692 | if (adap->nr & ~MAX_ID_MASK) |
| 693 | return -EINVAL; |
| 694 | |
| 695 | retry: |
| 696 | if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) |
| 697 | return -ENOMEM; |
| 698 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 699 | mutex_lock(&core_lock); |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 700 | /* "above" here means "above or equal to", sigh; |
| 701 | * we need the "equal to" result to force the result |
| 702 | */ |
| 703 | status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id); |
| 704 | if (status == 0 && id != adap->nr) { |
| 705 | status = -EBUSY; |
| 706 | idr_remove(&i2c_adapter_idr, id); |
| 707 | } |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 708 | mutex_unlock(&core_lock); |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 709 | if (status == -EAGAIN) |
| 710 | goto retry; |
| 711 | |
| 712 | if (status == 0) |
| 713 | status = i2c_register_adapter(adap); |
| 714 | return status; |
| 715 | } |
| 716 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); |
| 717 | |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 718 | static int i2c_do_del_adapter(struct device_driver *d, void *data) |
| 719 | { |
| 720 | struct i2c_driver *driver = to_i2c_driver(d); |
| 721 | struct i2c_adapter *adapter = data; |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 722 | struct i2c_client *client, *_n; |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 723 | int res; |
| 724 | |
Jean Delvare | acec211 | 2009-03-28 21:34:40 +0100 | [diff] [blame] | 725 | /* Remove the devices we created ourselves as the result of hardware |
| 726 | * probing (using a driver's detect method) */ |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 727 | list_for_each_entry_safe(client, _n, &driver->clients, detected) { |
| 728 | if (client->adapter == adapter) { |
| 729 | dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", |
| 730 | client->name, client->addr); |
| 731 | list_del(&client->detected); |
| 732 | i2c_unregister_device(client); |
| 733 | } |
| 734 | } |
| 735 | |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 736 | if (!driver->detach_adapter) |
| 737 | return 0; |
| 738 | res = driver->detach_adapter(adapter); |
| 739 | if (res) |
| 740 | dev_err(&adapter->dev, "detach_adapter failed (%d) " |
| 741 | "for driver [%s]\n", res, driver->driver.name); |
| 742 | return res; |
| 743 | } |
| 744 | |
Jean Delvare | e549c2b | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 745 | static int __unregister_client(struct device *dev, void *dummy) |
| 746 | { |
| 747 | struct i2c_client *client = i2c_verify_client(dev); |
| 748 | if (client) |
| 749 | i2c_unregister_device(client); |
| 750 | return 0; |
| 751 | } |
| 752 | |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 753 | /** |
| 754 | * i2c_del_adapter - unregister I2C adapter |
| 755 | * @adap: the adapter being unregistered |
| 756 | * Context: can sleep |
| 757 | * |
| 758 | * This unregisters an I2C adapter which was previously registered |
| 759 | * by @i2c_add_adapter or @i2c_add_numbered_adapter. |
| 760 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | int i2c_del_adapter(struct i2c_adapter *adap) |
| 762 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | int res = 0; |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 764 | struct i2c_adapter *found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
| 766 | /* First make sure that this adapter was ever added */ |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 767 | mutex_lock(&core_lock); |
| 768 | found = idr_find(&i2c_adapter_idr, adap->nr); |
| 769 | mutex_unlock(&core_lock); |
| 770 | if (found != adap) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 771 | pr_debug("i2c-core: attempting to delete unregistered " |
| 772 | "adapter [%s]\n", adap->name); |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 773 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 776 | /* Tell drivers about this removal */ |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 777 | mutex_lock(&core_lock); |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 778 | res = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
| 779 | i2c_do_del_adapter); |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 780 | mutex_unlock(&core_lock); |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 781 | if (res) |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 782 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Jean Delvare | e549c2b | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 784 | /* Detach any active clients. This can't fail, thus we do not |
| 785 | checking the returned value. */ |
| 786 | res = device_for_each_child(&adap->dev, NULL, __unregister_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame^] | 788 | #ifdef CONFIG_I2C_COMPAT |
| 789 | class_compat_remove_link(i2c_adapter_compat_class, &adap->dev, |
| 790 | adap->dev.parent); |
| 791 | #endif |
| 792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | /* clean up the sysfs representation */ |
| 794 | init_completion(&adap->dev_released); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | device_unregister(&adap->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
| 797 | /* wait for sysfs to drop all references */ |
| 798 | wait_for_completion(&adap->dev_released); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 800 | /* free bus id */ |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 801 | mutex_lock(&core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | idr_remove(&i2c_adapter_idr, adap->nr); |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 803 | mutex_unlock(&core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 805 | dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
Jean Delvare | bd4bc3db | 2008-07-16 19:30:05 +0200 | [diff] [blame] | 807 | /* Clear the device structure in case this adapter is ever going to be |
| 808 | added again */ |
| 809 | memset(&adap->dev, 0, sizeof(adap->dev)); |
| 810 | |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 811 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 813 | EXPORT_SYMBOL(i2c_del_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
| 815 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 816 | /* ------------------------------------------------------------------------- */ |
| 817 | |
Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 818 | static int __attach_adapter(struct device *dev, void *data) |
| 819 | { |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 820 | struct i2c_adapter *adapter; |
Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 821 | struct i2c_driver *driver = data; |
| 822 | |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 823 | if (dev->type != &i2c_adapter_type) |
| 824 | return 0; |
| 825 | adapter = to_i2c_adapter(dev); |
| 826 | |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 827 | i2c_detect(adapter, driver); |
| 828 | |
| 829 | /* Legacy drivers scan i2c busses directly */ |
| 830 | if (driver->attach_adapter) |
| 831 | driver->attach_adapter(adapter); |
Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 836 | /* |
| 837 | * An i2c_driver is used with one or more i2c_client (device) nodes to access |
Jean Delvare | 729d6dd | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 838 | * i2c slave chips, on a bus instance associated with some i2c_adapter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | */ |
| 840 | |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 841 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | { |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 843 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
David Brownell | 1d0b19c | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 845 | /* Can't register until after driver model init */ |
| 846 | if (unlikely(WARN_ON(!i2c_bus_type.p))) |
| 847 | return -EAGAIN; |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | /* 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] | 850 | driver->driver.owner = owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | driver->driver.bus = &i2c_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
Jean Delvare | 729d6dd | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 853 | /* When registration returns, the driver core |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 854 | * will have called probe() for all matching-but-unbound devices. |
| 855 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | res = driver_register(&driver->driver); |
| 857 | if (res) |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 858 | return res; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 859 | |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 860 | pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 862 | INIT_LIST_HEAD(&driver->clients); |
| 863 | /* Walk the adapters that are already present */ |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 864 | mutex_lock(&core_lock); |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 865 | bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter); |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 866 | mutex_unlock(&core_lock); |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 867 | |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 868 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | } |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 870 | EXPORT_SYMBOL(i2c_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 872 | static int __detach_adapter(struct device *dev, void *data) |
| 873 | { |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 874 | struct i2c_adapter *adapter; |
Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 875 | struct i2c_driver *driver = data; |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 876 | struct i2c_client *client, *_n; |
| 877 | |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 878 | if (dev->type != &i2c_adapter_type) |
| 879 | return 0; |
| 880 | adapter = to_i2c_adapter(dev); |
| 881 | |
Jean Delvare | acec211 | 2009-03-28 21:34:40 +0100 | [diff] [blame] | 882 | /* Remove the devices we created ourselves as the result of hardware |
| 883 | * probing (using a driver's detect method) */ |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 884 | list_for_each_entry_safe(client, _n, &driver->clients, detected) { |
| 885 | dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", |
| 886 | client->name, client->addr); |
| 887 | list_del(&client->detected); |
| 888 | i2c_unregister_device(client); |
| 889 | } |
| 890 | |
Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 891 | if (driver->detach_adapter) { |
| 892 | if (driver->detach_adapter(adapter)) |
| 893 | dev_err(&adapter->dev, |
| 894 | "detach_adapter failed for driver [%s]\n", |
| 895 | driver->driver.name); |
Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | return 0; |
| 899 | } |
| 900 | |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 901 | /** |
| 902 | * i2c_del_driver - unregister I2C driver |
| 903 | * @driver: the driver being unregistered |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 904 | * Context: can sleep |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 905 | */ |
Jean Delvare | b3e8209 | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 906 | void i2c_del_driver(struct i2c_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | { |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 908 | mutex_lock(&core_lock); |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 909 | bus_for_each_dev(&i2c_bus_type, NULL, driver, __detach_adapter); |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 910 | mutex_unlock(&core_lock); |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | driver_unregister(&driver->driver); |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 913 | pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 915 | EXPORT_SYMBOL(i2c_del_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 917 | /* ------------------------------------------------------------------------- */ |
| 918 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 919 | static int __i2c_check_addr(struct device *dev, void *addrp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | { |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 921 | struct i2c_client *client = i2c_verify_client(dev); |
| 922 | int addr = *(int *)addrp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 924 | if (client && client->addr == addr) |
| 925 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | return 0; |
| 927 | } |
| 928 | |
Jean Delvare | 5e31c2b | 2007-11-15 19:24:02 +0100 | [diff] [blame] | 929 | static int i2c_check_addr(struct i2c_adapter *adapter, int addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | { |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 931 | return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Jean Delvare | e48d331 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 934 | /** |
| 935 | * i2c_use_client - increments the reference count of the i2c client structure |
| 936 | * @client: the client being referenced |
| 937 | * |
| 938 | * Each live reference to a client should be refcounted. The driver model does |
| 939 | * that automatically as part of driver binding, so that most drivers don't |
| 940 | * need to do this explicitly: they hold a reference until they're unbound |
| 941 | * from the device. |
| 942 | * |
| 943 | * A pointer to the client with the incremented reference counter is returned. |
| 944 | */ |
| 945 | struct i2c_client *i2c_use_client(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | { |
David Brownell | 6ea438e | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 947 | if (client && get_device(&client->dev)) |
| 948 | return client; |
| 949 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 951 | EXPORT_SYMBOL(i2c_use_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
Jean Delvare | e48d331 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 953 | /** |
| 954 | * i2c_release_client - release a use of the i2c client structure |
| 955 | * @client: the client being no longer referenced |
| 956 | * |
| 957 | * Must be called when a user of a client is finished with it. |
| 958 | */ |
| 959 | void i2c_release_client(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | { |
David Brownell | 6ea438e | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 961 | if (client) |
| 962 | put_device(&client->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 964 | EXPORT_SYMBOL(i2c_release_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 966 | struct i2c_cmd_arg { |
| 967 | unsigned cmd; |
| 968 | void *arg; |
| 969 | }; |
| 970 | |
| 971 | static int i2c_cmd(struct device *dev, void *_arg) |
| 972 | { |
| 973 | struct i2c_client *client = i2c_verify_client(dev); |
| 974 | struct i2c_cmd_arg *arg = _arg; |
| 975 | |
| 976 | if (client && client->driver && client->driver->command) |
| 977 | client->driver->command(client, arg->cmd, arg->arg); |
| 978 | return 0; |
| 979 | } |
| 980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) |
| 982 | { |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 983 | struct i2c_cmd_arg cmd_arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 985 | cmd_arg.cmd = cmd; |
| 986 | cmd_arg.arg = arg; |
| 987 | device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 989 | EXPORT_SYMBOL(i2c_clients_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
| 991 | static int __init i2c_init(void) |
| 992 | { |
| 993 | int retval; |
| 994 | |
| 995 | retval = bus_register(&i2c_bus_type); |
| 996 | if (retval) |
| 997 | return retval; |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame^] | 998 | #ifdef CONFIG_I2C_COMPAT |
| 999 | i2c_adapter_compat_class = class_compat_register("i2c-adapter"); |
| 1000 | if (!i2c_adapter_compat_class) { |
| 1001 | retval = -ENOMEM; |
| 1002 | goto bus_err; |
| 1003 | } |
| 1004 | #endif |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1005 | retval = i2c_add_driver(&dummy_driver); |
| 1006 | if (retval) |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame^] | 1007 | goto class_err; |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1008 | return 0; |
| 1009 | |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame^] | 1010 | class_err: |
| 1011 | #ifdef CONFIG_I2C_COMPAT |
| 1012 | class_compat_unregister(i2c_adapter_compat_class); |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1013 | bus_err: |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame^] | 1014 | #endif |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1015 | bus_unregister(&i2c_bus_type); |
| 1016 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | static void __exit i2c_exit(void) |
| 1020 | { |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1021 | i2c_del_driver(&dummy_driver); |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame^] | 1022 | #ifdef CONFIG_I2C_COMPAT |
| 1023 | class_compat_unregister(i2c_adapter_compat_class); |
| 1024 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | bus_unregister(&i2c_bus_type); |
| 1026 | } |
| 1027 | |
David Brownell | a10f9e7 | 2008-10-14 17:30:06 +0200 | [diff] [blame] | 1028 | /* We must initialize early, because some subsystems register i2c drivers |
| 1029 | * in subsys_initcall() code, but are linked (and initialized) before i2c. |
| 1030 | */ |
| 1031 | postcore_initcall(i2c_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | module_exit(i2c_exit); |
| 1033 | |
| 1034 | /* ---------------------------------------------------- |
| 1035 | * the functional interface to the i2c busses. |
| 1036 | * ---------------------------------------------------- |
| 1037 | */ |
| 1038 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1039 | /** |
| 1040 | * i2c_transfer - execute a single or combined I2C message |
| 1041 | * @adap: Handle to I2C bus |
| 1042 | * @msgs: One or more messages to execute before STOP is issued to |
| 1043 | * terminate the operation; each message begins with a START. |
| 1044 | * @num: Number of messages to be executed. |
| 1045 | * |
| 1046 | * Returns negative errno, else the number of messages executed. |
| 1047 | * |
| 1048 | * Note that there is no requirement that each message be sent to |
| 1049 | * the same slave address, although that is the most common model. |
| 1050 | */ |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 1051 | int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | { |
Clifford Wolf | 66b650f | 2009-06-15 18:01:46 +0200 | [diff] [blame] | 1053 | unsigned long orig_jiffies; |
| 1054 | int ret, try; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1056 | /* REVISIT the fault reporting model here is weak: |
| 1057 | * |
| 1058 | * - When we get an error after receiving N bytes from a slave, |
| 1059 | * there is no way to report "N". |
| 1060 | * |
| 1061 | * - When we get a NAK after transmitting N bytes to a slave, |
| 1062 | * there is no way to report "N" ... or to let the master |
| 1063 | * continue executing the rest of this combined message, if |
| 1064 | * that's the appropriate response. |
| 1065 | * |
| 1066 | * - When for example "num" is two and we successfully complete |
| 1067 | * the first message but get an error part way through the |
| 1068 | * second, it's unclear whether that should be reported as |
| 1069 | * one (discarding status on the second message) or errno |
| 1070 | * (discarding status on the first one). |
| 1071 | */ |
| 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | if (adap->algo->master_xfer) { |
| 1074 | #ifdef DEBUG |
| 1075 | for (ret = 0; ret < num; ret++) { |
| 1076 | dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, " |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 1077 | "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD) |
| 1078 | ? 'R' : 'W', msgs[ret].addr, msgs[ret].len, |
| 1079 | (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | } |
| 1081 | #endif |
| 1082 | |
Mike Rapoport | cea443a8 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1083 | if (in_atomic() || irqs_disabled()) { |
| 1084 | ret = mutex_trylock(&adap->bus_lock); |
| 1085 | if (!ret) |
| 1086 | /* I2C activity is ongoing. */ |
| 1087 | return -EAGAIN; |
| 1088 | } else { |
| 1089 | mutex_lock_nested(&adap->bus_lock, adap->level); |
| 1090 | } |
| 1091 | |
Clifford Wolf | 66b650f | 2009-06-15 18:01:46 +0200 | [diff] [blame] | 1092 | /* Retry automatically on arbitration loss */ |
| 1093 | orig_jiffies = jiffies; |
| 1094 | for (ret = 0, try = 0; try <= adap->retries; try++) { |
| 1095 | ret = adap->algo->master_xfer(adap, msgs, num); |
| 1096 | if (ret != -EAGAIN) |
| 1097 | break; |
| 1098 | if (time_after(jiffies, orig_jiffies + adap->timeout)) |
| 1099 | break; |
| 1100 | } |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 1101 | mutex_unlock(&adap->bus_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | |
| 1103 | return ret; |
| 1104 | } else { |
| 1105 | dev_dbg(&adap->dev, "I2C level transfers not supported\n"); |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1106 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | } |
| 1108 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1109 | EXPORT_SYMBOL(i2c_transfer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1111 | /** |
| 1112 | * i2c_master_send - issue a single I2C message in master transmit mode |
| 1113 | * @client: Handle to slave device |
| 1114 | * @buf: Data that will be written to the slave |
| 1115 | * @count: How many bytes to write |
| 1116 | * |
| 1117 | * Returns negative errno, or else the number of bytes written. |
| 1118 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | int i2c_master_send(struct i2c_client *client,const char *buf ,int count) |
| 1120 | { |
| 1121 | int ret; |
| 1122 | struct i2c_adapter *adap=client->adapter; |
| 1123 | struct i2c_msg msg; |
| 1124 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 1125 | msg.addr = client->addr; |
| 1126 | msg.flags = client->flags & I2C_M_TEN; |
| 1127 | msg.len = count; |
| 1128 | msg.buf = (char *)buf; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1129 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 1130 | ret = i2c_transfer(adap, &msg, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 1132 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes |
| 1133 | transmitted, else error code. */ |
| 1134 | return (ret == 1) ? count : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1136 | EXPORT_SYMBOL(i2c_master_send); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1138 | /** |
| 1139 | * i2c_master_recv - issue a single I2C message in master receive mode |
| 1140 | * @client: Handle to slave device |
| 1141 | * @buf: Where to store data read from slave |
| 1142 | * @count: How many bytes to read |
| 1143 | * |
| 1144 | * Returns negative errno, or else the number of bytes read. |
| 1145 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | int i2c_master_recv(struct i2c_client *client, char *buf ,int count) |
| 1147 | { |
| 1148 | struct i2c_adapter *adap=client->adapter; |
| 1149 | struct i2c_msg msg; |
| 1150 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 1152 | msg.addr = client->addr; |
| 1153 | msg.flags = client->flags & I2C_M_TEN; |
| 1154 | msg.flags |= I2C_M_RD; |
| 1155 | msg.len = count; |
| 1156 | msg.buf = buf; |
| 1157 | |
| 1158 | ret = i2c_transfer(adap, &msg, 1); |
| 1159 | |
| 1160 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes |
| 1161 | transmitted, else error code. */ |
| 1162 | return (ret == 1) ? count : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1164 | EXPORT_SYMBOL(i2c_master_recv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | /* ---------------------------------------------------- |
| 1167 | * the i2c address scanning function |
| 1168 | * Will not work for 10-bit addresses! |
| 1169 | * ---------------------------------------------------- |
| 1170 | */ |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 1171 | |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1172 | static int i2c_detect_address(struct i2c_client *temp_client, int kind, |
| 1173 | struct i2c_driver *driver) |
| 1174 | { |
| 1175 | struct i2c_board_info info; |
| 1176 | struct i2c_adapter *adapter = temp_client->adapter; |
| 1177 | int addr = temp_client->addr; |
| 1178 | int err; |
| 1179 | |
| 1180 | /* Make sure the address is valid */ |
| 1181 | if (addr < 0x03 || addr > 0x77) { |
| 1182 | dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", |
| 1183 | addr); |
| 1184 | return -EINVAL; |
| 1185 | } |
| 1186 | |
| 1187 | /* Skip if already in use */ |
| 1188 | if (i2c_check_addr(adapter, addr)) |
| 1189 | return 0; |
| 1190 | |
| 1191 | /* Make sure there is something at this address, unless forced */ |
| 1192 | if (kind < 0) { |
| 1193 | if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, |
| 1194 | I2C_SMBUS_QUICK, NULL) < 0) |
| 1195 | return 0; |
| 1196 | |
| 1197 | /* prevent 24RF08 corruption */ |
| 1198 | if ((addr & ~0x0f) == 0x50) |
| 1199 | i2c_smbus_xfer(adapter, addr, 0, 0, 0, |
| 1200 | I2C_SMBUS_QUICK, NULL); |
| 1201 | } |
| 1202 | |
| 1203 | /* Finally call the custom detection function */ |
| 1204 | memset(&info, 0, sizeof(struct i2c_board_info)); |
| 1205 | info.addr = addr; |
| 1206 | err = driver->detect(temp_client, kind, &info); |
| 1207 | if (err) { |
| 1208 | /* -ENODEV is returned if the detection fails. We catch it |
| 1209 | here as this isn't an error. */ |
| 1210 | return err == -ENODEV ? 0 : err; |
| 1211 | } |
| 1212 | |
| 1213 | /* Consistency check */ |
| 1214 | if (info.type[0] == '\0') { |
| 1215 | dev_err(&adapter->dev, "%s detection function provided " |
| 1216 | "no name for 0x%x\n", driver->driver.name, |
| 1217 | addr); |
| 1218 | } else { |
| 1219 | struct i2c_client *client; |
| 1220 | |
| 1221 | /* Detection succeeded, instantiate the device */ |
| 1222 | dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n", |
| 1223 | info.type, info.addr); |
| 1224 | client = i2c_new_device(adapter, &info); |
| 1225 | if (client) |
| 1226 | list_add_tail(&client->detected, &driver->clients); |
| 1227 | else |
| 1228 | dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n", |
| 1229 | info.type, info.addr); |
| 1230 | } |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver) |
| 1235 | { |
| 1236 | const struct i2c_client_address_data *address_data; |
| 1237 | struct i2c_client *temp_client; |
| 1238 | int i, err = 0; |
| 1239 | int adap_id = i2c_adapter_id(adapter); |
| 1240 | |
| 1241 | address_data = driver->address_data; |
| 1242 | if (!driver->detect || !address_data) |
| 1243 | return 0; |
| 1244 | |
| 1245 | /* Set up a temporary client to help detect callback */ |
| 1246 | temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); |
| 1247 | if (!temp_client) |
| 1248 | return -ENOMEM; |
| 1249 | temp_client->adapter = adapter; |
| 1250 | |
| 1251 | /* Force entries are done first, and are not affected by ignore |
| 1252 | entries */ |
| 1253 | if (address_data->forces) { |
| 1254 | const unsigned short * const *forces = address_data->forces; |
| 1255 | int kind; |
| 1256 | |
| 1257 | for (kind = 0; forces[kind]; kind++) { |
| 1258 | for (i = 0; forces[kind][i] != I2C_CLIENT_END; |
| 1259 | i += 2) { |
| 1260 | if (forces[kind][i] == adap_id |
| 1261 | || forces[kind][i] == ANY_I2C_BUS) { |
| 1262 | dev_dbg(&adapter->dev, "found force " |
| 1263 | "parameter for adapter %d, " |
| 1264 | "addr 0x%02x, kind %d\n", |
| 1265 | adap_id, forces[kind][i + 1], |
| 1266 | kind); |
| 1267 | temp_client->addr = forces[kind][i + 1]; |
| 1268 | err = i2c_detect_address(temp_client, |
| 1269 | kind, driver); |
| 1270 | if (err) |
| 1271 | goto exit_free; |
| 1272 | } |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | |
Jean Delvare | 4329cf8 | 2008-08-28 08:33:23 +0200 | [diff] [blame] | 1277 | /* Stop here if the classes do not match */ |
| 1278 | if (!(adapter->class & driver->class)) |
| 1279 | goto exit_free; |
| 1280 | |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1281 | /* Stop here if we can't use SMBUS_QUICK */ |
| 1282 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { |
| 1283 | if (address_data->probe[0] == I2C_CLIENT_END |
| 1284 | && address_data->normal_i2c[0] == I2C_CLIENT_END) |
| 1285 | goto exit_free; |
| 1286 | |
| 1287 | dev_warn(&adapter->dev, "SMBus Quick command not supported, " |
| 1288 | "can't probe for chips\n"); |
| 1289 | err = -EOPNOTSUPP; |
| 1290 | goto exit_free; |
| 1291 | } |
| 1292 | |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1293 | /* Probe entries are done second, and are not affected by ignore |
| 1294 | entries either */ |
| 1295 | for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) { |
| 1296 | if (address_data->probe[i] == adap_id |
| 1297 | || address_data->probe[i] == ANY_I2C_BUS) { |
| 1298 | dev_dbg(&adapter->dev, "found probe parameter for " |
| 1299 | "adapter %d, addr 0x%02x\n", adap_id, |
| 1300 | address_data->probe[i + 1]); |
| 1301 | temp_client->addr = address_data->probe[i + 1]; |
| 1302 | err = i2c_detect_address(temp_client, -1, driver); |
| 1303 | if (err) |
| 1304 | goto exit_free; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | /* Normal entries are done last, unless shadowed by an ignore entry */ |
| 1309 | for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) { |
| 1310 | int j, ignore; |
| 1311 | |
| 1312 | ignore = 0; |
| 1313 | for (j = 0; address_data->ignore[j] != I2C_CLIENT_END; |
| 1314 | j += 2) { |
| 1315 | if ((address_data->ignore[j] == adap_id || |
| 1316 | address_data->ignore[j] == ANY_I2C_BUS) |
| 1317 | && address_data->ignore[j + 1] |
| 1318 | == address_data->normal_i2c[i]) { |
| 1319 | dev_dbg(&adapter->dev, "found ignore " |
| 1320 | "parameter for adapter %d, " |
| 1321 | "addr 0x%02x\n", adap_id, |
| 1322 | address_data->ignore[j + 1]); |
| 1323 | ignore = 1; |
| 1324 | break; |
| 1325 | } |
| 1326 | } |
| 1327 | if (ignore) |
| 1328 | continue; |
| 1329 | |
| 1330 | dev_dbg(&adapter->dev, "found normal entry for adapter %d, " |
| 1331 | "addr 0x%02x\n", adap_id, |
| 1332 | address_data->normal_i2c[i]); |
| 1333 | temp_client->addr = address_data->normal_i2c[i]; |
| 1334 | err = i2c_detect_address(temp_client, -1, driver); |
| 1335 | if (err) |
| 1336 | goto exit_free; |
| 1337 | } |
| 1338 | |
| 1339 | exit_free: |
| 1340 | kfree(temp_client); |
| 1341 | return err; |
| 1342 | } |
| 1343 | |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1344 | struct i2c_client * |
| 1345 | i2c_new_probed_device(struct i2c_adapter *adap, |
| 1346 | struct i2c_board_info *info, |
| 1347 | unsigned short const *addr_list) |
| 1348 | { |
| 1349 | int i; |
| 1350 | |
| 1351 | /* Stop here if the bus doesn't support probing */ |
| 1352 | if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) { |
| 1353 | dev_err(&adap->dev, "Probing not supported\n"); |
| 1354 | return NULL; |
| 1355 | } |
| 1356 | |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1357 | for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { |
| 1358 | /* Check address validity */ |
| 1359 | if (addr_list[i] < 0x03 || addr_list[i] > 0x77) { |
| 1360 | dev_warn(&adap->dev, "Invalid 7-bit address " |
| 1361 | "0x%02x\n", addr_list[i]); |
| 1362 | continue; |
| 1363 | } |
| 1364 | |
| 1365 | /* Check address availability */ |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 1366 | if (i2c_check_addr(adap, addr_list[i])) { |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1367 | dev_dbg(&adap->dev, "Address 0x%02x already in " |
| 1368 | "use, not probing\n", addr_list[i]); |
| 1369 | continue; |
| 1370 | } |
| 1371 | |
| 1372 | /* Test address responsiveness |
| 1373 | The default probe method is a quick write, but it is known |
| 1374 | to corrupt the 24RF08 EEPROMs due to a state machine bug, |
| 1375 | and could also irreversibly write-protect some EEPROMs, so |
| 1376 | for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte |
| 1377 | read instead. Also, some bus drivers don't implement |
| 1378 | quick write, so we fallback to a byte read it that case |
| 1379 | too. */ |
| 1380 | if ((addr_list[i] & ~0x07) == 0x30 |
| 1381 | || (addr_list[i] & ~0x0f) == 0x50 |
| 1382 | || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) { |
Hans Verkuil | b25b791 | 2008-08-10 22:56:15 +0200 | [diff] [blame] | 1383 | union i2c_smbus_data data; |
| 1384 | |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1385 | if (i2c_smbus_xfer(adap, addr_list[i], 0, |
| 1386 | I2C_SMBUS_READ, 0, |
Hans Verkuil | b25b791 | 2008-08-10 22:56:15 +0200 | [diff] [blame] | 1387 | I2C_SMBUS_BYTE, &data) >= 0) |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1388 | break; |
| 1389 | } else { |
| 1390 | if (i2c_smbus_xfer(adap, addr_list[i], 0, |
| 1391 | I2C_SMBUS_WRITE, 0, |
| 1392 | I2C_SMBUS_QUICK, NULL) >= 0) |
| 1393 | break; |
| 1394 | } |
| 1395 | } |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1396 | |
| 1397 | if (addr_list[i] == I2C_CLIENT_END) { |
| 1398 | dev_dbg(&adap->dev, "Probing failed, no device found\n"); |
| 1399 | return NULL; |
| 1400 | } |
| 1401 | |
| 1402 | info->addr = addr_list[i]; |
| 1403 | return i2c_new_device(adap, info); |
| 1404 | } |
| 1405 | EXPORT_SYMBOL_GPL(i2c_new_probed_device); |
| 1406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | struct i2c_adapter* i2c_get_adapter(int id) |
| 1408 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | struct i2c_adapter *adapter; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1410 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1411 | mutex_lock(&core_lock); |
Jack Stone | 1cf92b4 | 2009-06-15 18:01:46 +0200 | [diff] [blame] | 1412 | adapter = idr_find(&i2c_adapter_idr, id); |
Mark M. Hoffman | a0920e1 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 1413 | if (adapter && !try_module_get(adapter->owner)) |
| 1414 | adapter = NULL; |
| 1415 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1416 | mutex_unlock(&core_lock); |
Mark M. Hoffman | a0920e1 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 1417 | return adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1419 | EXPORT_SYMBOL(i2c_get_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | |
| 1421 | void i2c_put_adapter(struct i2c_adapter *adap) |
| 1422 | { |
| 1423 | module_put(adap->owner); |
| 1424 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1425 | EXPORT_SYMBOL(i2c_put_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | |
| 1427 | /* The SMBus parts */ |
| 1428 | |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1429 | #define POLY (0x1070U << 3) |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 1430 | static u8 crc8(u16 data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | { |
| 1432 | int i; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | for(i = 0; i < 8; i++) { |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1435 | if (data & 0x8000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | data = data ^ POLY; |
| 1437 | data = data << 1; |
| 1438 | } |
| 1439 | return (u8)(data >> 8); |
| 1440 | } |
| 1441 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1442 | /* Incremental CRC8 over count bytes in the array pointed to by p */ |
| 1443 | static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | { |
| 1445 | int i; |
| 1446 | |
| 1447 | for(i = 0; i < count; i++) |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1448 | crc = crc8((crc ^ p[i]) << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | return crc; |
| 1450 | } |
| 1451 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1452 | /* Assume a 7-bit address, which is reasonable for SMBus */ |
| 1453 | static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1455 | /* The address will be sent first */ |
| 1456 | u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD); |
| 1457 | pec = i2c_smbus_pec(pec, &addr, 1); |
| 1458 | |
| 1459 | /* The data buffer follows */ |
| 1460 | return i2c_smbus_pec(pec, msg->buf, msg->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1463 | /* Used for write only transactions */ |
| 1464 | static inline void i2c_smbus_add_pec(struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1466 | msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); |
| 1467 | msg->len++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1470 | /* Return <0 on CRC error |
| 1471 | If there was a write before this read (most cases) we need to take the |
| 1472 | partial CRC from the write part into account. |
| 1473 | Note that this function does modify the message (we need to decrease the |
| 1474 | message length to hide the CRC byte from the caller). */ |
| 1475 | static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1477 | u8 rpec = msg->buf[--msg->len]; |
| 1478 | cpec = i2c_smbus_msg_pec(cpec, msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | if (rpec != cpec) { |
| 1481 | pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n", |
| 1482 | rpec, cpec); |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1483 | return -EBADMSG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | } |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1485 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1488 | /** |
| 1489 | * i2c_smbus_read_byte - SMBus "receive byte" protocol |
| 1490 | * @client: Handle to slave device |
| 1491 | * |
| 1492 | * This executes the SMBus "receive byte" protocol, returning negative errno |
| 1493 | * else the byte received from the device. |
| 1494 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | s32 i2c_smbus_read_byte(struct i2c_client *client) |
| 1496 | { |
| 1497 | union i2c_smbus_data data; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1498 | int status; |
| 1499 | |
| 1500 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 1501 | I2C_SMBUS_READ, 0, |
| 1502 | I2C_SMBUS_BYTE, &data); |
| 1503 | return (status < 0) ? status : data.byte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1505 | EXPORT_SYMBOL(i2c_smbus_read_byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1507 | /** |
| 1508 | * i2c_smbus_write_byte - SMBus "send byte" protocol |
| 1509 | * @client: Handle to slave device |
| 1510 | * @value: Byte to be sent |
| 1511 | * |
| 1512 | * This executes the SMBus "send byte" protocol, returning negative errno |
| 1513 | * else zero on success. |
| 1514 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value) |
| 1516 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1518 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1520 | EXPORT_SYMBOL(i2c_smbus_write_byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1522 | /** |
| 1523 | * i2c_smbus_read_byte_data - SMBus "read byte" protocol |
| 1524 | * @client: Handle to slave device |
| 1525 | * @command: Byte interpreted by slave |
| 1526 | * |
| 1527 | * This executes the SMBus "read byte" protocol, returning negative errno |
| 1528 | * else a data byte received from the device. |
| 1529 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command) |
| 1531 | { |
| 1532 | union i2c_smbus_data data; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1533 | int status; |
| 1534 | |
| 1535 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 1536 | I2C_SMBUS_READ, command, |
| 1537 | I2C_SMBUS_BYTE_DATA, &data); |
| 1538 | return (status < 0) ? status : data.byte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1540 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1542 | /** |
| 1543 | * i2c_smbus_write_byte_data - SMBus "write byte" protocol |
| 1544 | * @client: Handle to slave device |
| 1545 | * @command: Byte interpreted by slave |
| 1546 | * @value: Byte being written |
| 1547 | * |
| 1548 | * This executes the SMBus "write byte" protocol, returning negative errno |
| 1549 | * else zero on success. |
| 1550 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value) |
| 1552 | { |
| 1553 | union i2c_smbus_data data; |
| 1554 | data.byte = value; |
| 1555 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 1556 | I2C_SMBUS_WRITE,command, |
| 1557 | I2C_SMBUS_BYTE_DATA,&data); |
| 1558 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1559 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1561 | /** |
| 1562 | * i2c_smbus_read_word_data - SMBus "read word" protocol |
| 1563 | * @client: Handle to slave device |
| 1564 | * @command: Byte interpreted by slave |
| 1565 | * |
| 1566 | * This executes the SMBus "read word" protocol, returning negative errno |
| 1567 | * else a 16-bit unsigned "word" received from the device. |
| 1568 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command) |
| 1570 | { |
| 1571 | union i2c_smbus_data data; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1572 | int status; |
| 1573 | |
| 1574 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 1575 | I2C_SMBUS_READ, command, |
| 1576 | I2C_SMBUS_WORD_DATA, &data); |
| 1577 | return (status < 0) ? status : data.word; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1579 | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1581 | /** |
| 1582 | * i2c_smbus_write_word_data - SMBus "write word" protocol |
| 1583 | * @client: Handle to slave device |
| 1584 | * @command: Byte interpreted by slave |
| 1585 | * @value: 16-bit "word" being written |
| 1586 | * |
| 1587 | * This executes the SMBus "write word" protocol, returning negative errno |
| 1588 | * else zero on success. |
| 1589 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value) |
| 1591 | { |
| 1592 | union i2c_smbus_data data; |
| 1593 | data.word = value; |
| 1594 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 1595 | I2C_SMBUS_WRITE,command, |
| 1596 | I2C_SMBUS_WORD_DATA,&data); |
| 1597 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1598 | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | |
David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 1600 | /** |
Prakash Mortha | 596c88f | 2008-10-14 17:30:06 +0200 | [diff] [blame] | 1601 | * i2c_smbus_process_call - SMBus "process call" protocol |
| 1602 | * @client: Handle to slave device |
| 1603 | * @command: Byte interpreted by slave |
| 1604 | * @value: 16-bit "word" being written |
| 1605 | * |
| 1606 | * This executes the SMBus "process call" protocol, returning negative errno |
| 1607 | * else a 16-bit unsigned "word" received from the device. |
| 1608 | */ |
| 1609 | s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value) |
| 1610 | { |
| 1611 | union i2c_smbus_data data; |
| 1612 | int status; |
| 1613 | data.word = value; |
| 1614 | |
| 1615 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 1616 | I2C_SMBUS_WRITE, command, |
| 1617 | I2C_SMBUS_PROC_CALL, &data); |
| 1618 | return (status < 0) ? status : data.word; |
| 1619 | } |
| 1620 | EXPORT_SYMBOL(i2c_smbus_process_call); |
| 1621 | |
| 1622 | /** |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1623 | * i2c_smbus_read_block_data - SMBus "block read" protocol |
David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 1624 | * @client: Handle to slave device |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1625 | * @command: Byte interpreted by slave |
David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 1626 | * @values: Byte array into which data will be read; big enough to hold |
| 1627 | * the data returned by the slave. SMBus allows at most 32 bytes. |
| 1628 | * |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1629 | * This executes the SMBus "block read" protocol, returning negative errno |
| 1630 | * else the number of data bytes in the slave's response. |
David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 1631 | * |
| 1632 | * Note that using this function requires that the client's adapter support |
| 1633 | * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers |
| 1634 | * support this; its emulation through I2C messaging relies on a specific |
| 1635 | * mechanism (I2C_M_RECV_LEN) which may not be implemented. |
| 1636 | */ |
Jean Delvare | b86a1bc | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 1637 | s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command, |
| 1638 | u8 *values) |
| 1639 | { |
| 1640 | union i2c_smbus_data data; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1641 | int status; |
Jean Delvare | b86a1bc | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 1642 | |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1643 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 1644 | I2C_SMBUS_READ, command, |
| 1645 | I2C_SMBUS_BLOCK_DATA, &data); |
| 1646 | if (status) |
| 1647 | return status; |
Jean Delvare | b86a1bc | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 1648 | |
| 1649 | memcpy(values, &data.block[1], data.block[0]); |
| 1650 | return data.block[0]; |
| 1651 | } |
| 1652 | EXPORT_SYMBOL(i2c_smbus_read_block_data); |
| 1653 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1654 | /** |
| 1655 | * i2c_smbus_write_block_data - SMBus "block write" protocol |
| 1656 | * @client: Handle to slave device |
| 1657 | * @command: Byte interpreted by slave |
| 1658 | * @length: Size of data block; SMBus allows at most 32 bytes |
| 1659 | * @values: Byte array which will be written. |
| 1660 | * |
| 1661 | * This executes the SMBus "block write" protocol, returning negative errno |
| 1662 | * else zero on success. |
| 1663 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command, |
Krzysztof Halasa | 46f5ed7 | 2006-06-12 21:42:20 +0200 | [diff] [blame] | 1665 | u8 length, const u8 *values) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | { |
| 1667 | union i2c_smbus_data data; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 1668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 1670 | length = I2C_SMBUS_BLOCK_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | data.block[0] = length; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 1672 | memcpy(&data.block[1], values, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 1674 | I2C_SMBUS_WRITE,command, |
| 1675 | I2C_SMBUS_BLOCK_DATA,&data); |
| 1676 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1677 | EXPORT_SYMBOL(i2c_smbus_write_block_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | |
| 1679 | /* Returns the number of read bytes */ |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 1680 | s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, |
| 1681 | u8 length, u8 *values) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | { |
| 1683 | union i2c_smbus_data data; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1684 | int status; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 1685 | |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 1686 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 1687 | length = I2C_SMBUS_BLOCK_MAX; |
| 1688 | data.block[0] = length; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1689 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 1690 | I2C_SMBUS_READ, command, |
| 1691 | I2C_SMBUS_I2C_BLOCK_DATA, &data); |
| 1692 | if (status < 0) |
| 1693 | return status; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 1694 | |
| 1695 | memcpy(values, &data.block[1], data.block[0]); |
| 1696 | return data.block[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1698 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | |
Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 1700 | 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] | 1701 | u8 length, const u8 *values) |
Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 1702 | { |
| 1703 | union i2c_smbus_data data; |
| 1704 | |
| 1705 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 1706 | length = I2C_SMBUS_BLOCK_MAX; |
| 1707 | data.block[0] = length; |
| 1708 | memcpy(data.block + 1, values, length); |
| 1709 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 1710 | I2C_SMBUS_WRITE, command, |
| 1711 | I2C_SMBUS_I2C_BLOCK_DATA, &data); |
| 1712 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1713 | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); |
Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 1714 | |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1715 | /* Simulate a SMBus command using the i2c protocol |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | No checking of parameters is done! */ |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1717 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | unsigned short flags, |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1719 | char read_write, u8 command, int size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | union i2c_smbus_data * data) |
| 1721 | { |
| 1722 | /* So we need to generate a series of msgs. In the case of writing, we |
| 1723 | need to use only one message; when reading, we need two. We initialize |
| 1724 | most things with sane defaults, to keep the code below somewhat |
| 1725 | simpler. */ |
Hideki Iwamoto | 5c50d18 | 2005-09-25 17:01:11 +0200 | [diff] [blame] | 1726 | unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; |
| 1727 | unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | int num = read_write == I2C_SMBUS_READ?2:1; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1729 | struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | { addr, flags | I2C_M_RD, 0, msgbuf1 } |
| 1731 | }; |
| 1732 | int i; |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1733 | u8 partial_pec = 0; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1734 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | |
| 1736 | msgbuf0[0] = command; |
| 1737 | switch(size) { |
| 1738 | case I2C_SMBUS_QUICK: |
| 1739 | msg[0].len = 0; |
| 1740 | /* Special case: The read/write field is used as data */ |
Roel Kluin | f29d2e0 | 2009-02-24 19:19:48 +0100 | [diff] [blame] | 1741 | msg[0].flags = flags | (read_write == I2C_SMBUS_READ ? |
| 1742 | I2C_M_RD : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | num = 1; |
| 1744 | break; |
| 1745 | case I2C_SMBUS_BYTE: |
| 1746 | if (read_write == I2C_SMBUS_READ) { |
| 1747 | /* Special case: only a read! */ |
| 1748 | msg[0].flags = I2C_M_RD | flags; |
| 1749 | num = 1; |
| 1750 | } |
| 1751 | break; |
| 1752 | case I2C_SMBUS_BYTE_DATA: |
| 1753 | if (read_write == I2C_SMBUS_READ) |
| 1754 | msg[1].len = 1; |
| 1755 | else { |
| 1756 | msg[0].len = 2; |
| 1757 | msgbuf0[1] = data->byte; |
| 1758 | } |
| 1759 | break; |
| 1760 | case I2C_SMBUS_WORD_DATA: |
| 1761 | if (read_write == I2C_SMBUS_READ) |
| 1762 | msg[1].len = 2; |
| 1763 | else { |
| 1764 | msg[0].len=3; |
| 1765 | msgbuf0[1] = data->word & 0xff; |
Jean Delvare | 7eff82c | 2006-09-03 22:24:00 +0200 | [diff] [blame] | 1766 | msgbuf0[2] = data->word >> 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | } |
| 1768 | break; |
| 1769 | case I2C_SMBUS_PROC_CALL: |
| 1770 | num = 2; /* Special case */ |
| 1771 | read_write = I2C_SMBUS_READ; |
| 1772 | msg[0].len = 3; |
| 1773 | msg[1].len = 2; |
| 1774 | msgbuf0[1] = data->word & 0xff; |
Jean Delvare | 7eff82c | 2006-09-03 22:24:00 +0200 | [diff] [blame] | 1775 | msgbuf0[2] = data->word >> 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | break; |
| 1777 | case I2C_SMBUS_BLOCK_DATA: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | if (read_write == I2C_SMBUS_READ) { |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 1779 | msg[1].flags |= I2C_M_RECV_LEN; |
| 1780 | msg[1].len = 1; /* block length will be added by |
| 1781 | the underlying bus driver */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | } else { |
| 1783 | msg[0].len = data->block[0] + 2; |
| 1784 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1785 | dev_err(&adapter->dev, |
| 1786 | "Invalid block write size %d\n", |
| 1787 | data->block[0]); |
| 1788 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | } |
Hideki Iwamoto | 5c50d18 | 2005-09-25 17:01:11 +0200 | [diff] [blame] | 1790 | for (i = 1; i < msg[0].len; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | msgbuf0[i] = data->block[i-1]; |
| 1792 | } |
| 1793 | break; |
| 1794 | case I2C_SMBUS_BLOCK_PROC_CALL: |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 1795 | num = 2; /* Another special case */ |
| 1796 | read_write = I2C_SMBUS_READ; |
| 1797 | if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1798 | dev_err(&adapter->dev, |
| 1799 | "Invalid block write size %d\n", |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 1800 | data->block[0]); |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1801 | return -EINVAL; |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 1802 | } |
| 1803 | msg[0].len = data->block[0] + 2; |
| 1804 | for (i = 1; i < msg[0].len; i++) |
| 1805 | msgbuf0[i] = data->block[i-1]; |
| 1806 | msg[1].flags |= I2C_M_RECV_LEN; |
| 1807 | msg[1].len = 1; /* block length will be added by |
| 1808 | the underlying bus driver */ |
| 1809 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 1811 | if (read_write == I2C_SMBUS_READ) { |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 1812 | msg[1].len = data->block[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | } else { |
| 1814 | msg[0].len = data->block[0] + 1; |
Jean Delvare | 30dac74 | 2005-10-08 00:15:59 +0200 | [diff] [blame] | 1815 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1816 | dev_err(&adapter->dev, |
| 1817 | "Invalid block write size %d\n", |
| 1818 | data->block[0]); |
| 1819 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | } |
| 1821 | for (i = 1; i <= data->block[0]; i++) |
| 1822 | msgbuf0[i] = data->block[i]; |
| 1823 | } |
| 1824 | break; |
| 1825 | default: |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1826 | dev_err(&adapter->dev, "Unsupported transaction %d\n", size); |
| 1827 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1830 | i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK |
| 1831 | && size != I2C_SMBUS_I2C_BLOCK_DATA); |
| 1832 | if (i) { |
| 1833 | /* Compute PEC if first message is a write */ |
| 1834 | if (!(msg[0].flags & I2C_M_RD)) { |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1835 | if (num == 1) /* Write only */ |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1836 | i2c_smbus_add_pec(&msg[0]); |
| 1837 | else /* Write followed by read */ |
| 1838 | partial_pec = i2c_smbus_msg_pec(0, &msg[0]); |
| 1839 | } |
| 1840 | /* Ask for PEC if last message is a read */ |
| 1841 | if (msg[num-1].flags & I2C_M_RD) |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1842 | msg[num-1].len++; |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1843 | } |
| 1844 | |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1845 | status = i2c_transfer(adapter, msg, num); |
| 1846 | if (status < 0) |
| 1847 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1849 | /* Check PEC if last message is a read */ |
| 1850 | if (i && (msg[num-1].flags & I2C_M_RD)) { |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 1851 | status = i2c_smbus_check_pec(partial_pec, &msg[num-1]); |
| 1852 | if (status < 0) |
| 1853 | return status; |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1854 | } |
| 1855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | if (read_write == I2C_SMBUS_READ) |
| 1857 | switch(size) { |
| 1858 | case I2C_SMBUS_BYTE: |
| 1859 | data->byte = msgbuf0[0]; |
| 1860 | break; |
| 1861 | case I2C_SMBUS_BYTE_DATA: |
| 1862 | data->byte = msgbuf1[0]; |
| 1863 | break; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1864 | case I2C_SMBUS_WORD_DATA: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | case I2C_SMBUS_PROC_CALL: |
| 1866 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); |
| 1867 | break; |
| 1868 | case I2C_SMBUS_I2C_BLOCK_DATA: |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 1869 | for (i = 0; i < data->block[0]; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | data->block[i+1] = msgbuf1[i]; |
| 1871 | break; |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 1872 | case I2C_SMBUS_BLOCK_DATA: |
| 1873 | case I2C_SMBUS_BLOCK_PROC_CALL: |
| 1874 | for (i = 0; i < msgbuf1[0] + 1; i++) |
| 1875 | data->block[i] = msgbuf1[i]; |
| 1876 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | } |
| 1878 | return 0; |
| 1879 | } |
| 1880 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1881 | /** |
| 1882 | * i2c_smbus_xfer - execute SMBus protocol operations |
| 1883 | * @adapter: Handle to I2C bus |
| 1884 | * @addr: Address of SMBus slave on that bus |
| 1885 | * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC) |
| 1886 | * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE |
| 1887 | * @command: Byte interpreted by slave, for protocols which use such bytes |
| 1888 | * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL |
| 1889 | * @data: Data to be read or written |
| 1890 | * |
| 1891 | * This executes an SMBus protocol operation, and returns a negative |
| 1892 | * errno code else zero on success. |
| 1893 | */ |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 1894 | s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags, |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1895 | char read_write, u8 command, int protocol, |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 1896 | union i2c_smbus_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | { |
Clifford Wolf | 66b650f | 2009-06-15 18:01:46 +0200 | [diff] [blame] | 1898 | unsigned long orig_jiffies; |
| 1899 | int try; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | s32 res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | |
| 1902 | flags &= I2C_M_TEN | I2C_CLIENT_PEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | |
| 1904 | if (adapter->algo->smbus_xfer) { |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 1905 | mutex_lock(&adapter->bus_lock); |
Clifford Wolf | 66b650f | 2009-06-15 18:01:46 +0200 | [diff] [blame] | 1906 | |
| 1907 | /* Retry automatically on arbitration loss */ |
| 1908 | orig_jiffies = jiffies; |
| 1909 | for (res = 0, try = 0; try <= adapter->retries; try++) { |
| 1910 | res = adapter->algo->smbus_xfer(adapter, addr, flags, |
| 1911 | read_write, command, |
| 1912 | protocol, data); |
| 1913 | if (res != -EAGAIN) |
| 1914 | break; |
| 1915 | if (time_after(jiffies, |
| 1916 | orig_jiffies + adapter->timeout)) |
| 1917 | break; |
| 1918 | } |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 1919 | mutex_unlock(&adapter->bus_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | } else |
| 1921 | res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1922 | command, protocol, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | return res; |
| 1925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | EXPORT_SYMBOL(i2c_smbus_xfer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | |
| 1928 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); |
| 1929 | MODULE_DESCRIPTION("I2C-Bus main module"); |
| 1930 | MODULE_LICENSE("GPL"); |