blob: 0ac2f90ab840c8f1863013ba9551a3266247f738 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 Engelhardt96de0e22007-10-19 23:21:04 +020020/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020022 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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 Venb3585e42006-01-11 10:50:26 +010032#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010033#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010034#include <linux/hardirq.h>
35#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020036#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
David Brownell9c1600e2007-05-01 23:26:31 +020039#include "i2c-core.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jean Delvare99cd8e22009-06-19 16:58:20 +020042/* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020043 that device detection, deletion of detected devices, and attach_adapter
44 and detach_adapter calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010045static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static DEFINE_IDR(i2c_adapter_idr);
Jean Delvare99cd8e22009-06-19 16:58:20 +020047static LIST_HEAD(userspace_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Jean Delvare4f8cf822009-09-18 22:45:46 +020049static struct device_type i2c_client_type;
Jean Delvaref8a227e2009-06-19 16:58:18 +020050static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
Jean Delvare4735c982008-07-14 22:38:36 +020051static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010052
53/* ------------------------------------------------------------------------- */
54
Jean Delvared2653e92008-04-29 23:11:39 +020055static 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 Torvalds1da177e2005-04-16 15:20:36 -070066static int i2c_device_match(struct device *dev, struct device_driver *drv)
67{
Jean Delvare51298d12009-09-18 22:45:45 +020068 struct i2c_client *client = i2c_verify_client(dev);
69 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020070
Jean Delvare51298d12009-09-18 22:45:45 +020071 if (!client)
72 return 0;
73
74 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020075 /* 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 Delvareeb8a7902008-05-18 20:49:41 +020079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
David Brownell7b4fbc52007-05-01 23:26:30 +020082#ifdef CONFIG_HOTPLUG
83
84/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020085static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020086{
87 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020088
Jean Delvareeb8a7902008-05-18 20:49:41 +020089 if (add_uevent_var(env, "MODALIAS=%s%s",
90 I2C_MODULE_PREFIX, client->name))
91 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020092 dev_dbg(dev, "uevent\n");
93 return 0;
94}
95
96#else
97#define i2c_device_uevent NULL
98#endif /* CONFIG_HOTPLUG */
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static int i2c_device_probe(struct device *dev)
101{
Jean Delvare51298d12009-09-18 22:45:45 +0200102 struct i2c_client *client = i2c_verify_client(dev);
103 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100104 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200105
Jean Delvare51298d12009-09-18 22:45:45 +0200106 if (!client)
107 return 0;
108
109 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200110 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200111 return -ENODEV;
112 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200113 if (!device_can_wakeup(&client->dev))
114 device_init_wakeup(&client->dev,
115 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200116 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200117
Jean Delvaree0457442008-07-14 22:38:30 +0200118 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100119 if (status)
120 client->driver = NULL;
121 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static int i2c_device_remove(struct device *dev)
125{
Jean Delvare51298d12009-09-18 22:45:45 +0200126 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200127 struct i2c_driver *driver;
128 int status;
129
Jean Delvare51298d12009-09-18 22:45:45 +0200130 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200131 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 Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
David Brownellf37dd802007-02-13 22:09:00 +0100146static void i2c_device_shutdown(struct device *dev)
147{
Jean Delvare51298d12009-09-18 22:45:45 +0200148 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100149 struct i2c_driver *driver;
150
Jean Delvare51298d12009-09-18 22:45:45 +0200151 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100152 return;
153 driver = to_i2c_driver(dev->driver);
154 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200155 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100156}
157
sonic zhang54067ee2009-12-14 21:17:30 +0100158#ifdef CONFIG_SUSPEND
159static int i2c_device_pm_suspend(struct device *dev)
160{
161 const struct dev_pm_ops *pm;
162
163 if (!dev->driver)
164 return 0;
165 pm = dev->driver->pm;
166 if (!pm || !pm->suspend)
167 return 0;
168 return pm->suspend(dev);
169}
170
171static int i2c_device_pm_resume(struct device *dev)
172{
173 const struct dev_pm_ops *pm;
174
175 if (!dev->driver)
176 return 0;
177 pm = dev->driver->pm;
178 if (!pm || !pm->resume)
179 return 0;
180 return pm->resume(dev);
181}
182#else
183#define i2c_device_pm_suspend NULL
184#define i2c_device_pm_resume NULL
185#endif
186
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100187static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100188{
Jean Delvare51298d12009-09-18 22:45:45 +0200189 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100190 struct i2c_driver *driver;
191
Jean Delvare51298d12009-09-18 22:45:45 +0200192 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100193 return 0;
194 driver = to_i2c_driver(dev->driver);
195 if (!driver->suspend)
196 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200197 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100198}
199
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100200static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100201{
Jean Delvare51298d12009-09-18 22:45:45 +0200202 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100203 struct i2c_driver *driver;
204
Jean Delvare51298d12009-09-18 22:45:45 +0200205 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100206 return 0;
207 driver = to_i2c_driver(dev->driver);
208 if (!driver->resume)
209 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200210 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100211}
212
David Brownell9c1600e2007-05-01 23:26:31 +0200213static void i2c_client_dev_release(struct device *dev)
214{
215 kfree(to_i2c_client(dev));
216}
217
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100218static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200219show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200220{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200221 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
222 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200223}
224
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100225static ssize_t
226show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200227{
228 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200229 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200230}
231
Jean Delvare4f8cf822009-09-18 22:45:46 +0200232static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200233static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
234
235static struct attribute *i2c_dev_attrs[] = {
236 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200237 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200238 &dev_attr_modalias.attr,
239 NULL
240};
241
242static struct attribute_group i2c_dev_attr_group = {
243 .attrs = i2c_dev_attrs,
244};
245
246static const struct attribute_group *i2c_dev_attr_groups[] = {
247 &i2c_dev_attr_group,
248 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200249};
250
sonic zhang54067ee2009-12-14 21:17:30 +0100251const static struct dev_pm_ops i2c_device_pm_ops = {
252 .suspend = i2c_device_pm_suspend,
253 .resume = i2c_device_pm_resume,
254};
255
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200256struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100257 .name = "i2c",
258 .match = i2c_device_match,
259 .probe = i2c_device_probe,
260 .remove = i2c_device_remove,
261 .shutdown = i2c_device_shutdown,
262 .suspend = i2c_device_suspend,
263 .resume = i2c_device_resume,
sonic zhang54067ee2009-12-14 21:17:30 +0100264 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000265};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200266EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000267
Jean Delvare51298d12009-09-18 22:45:45 +0200268static struct device_type i2c_client_type = {
269 .groups = i2c_dev_attr_groups,
270 .uevent = i2c_device_uevent,
271 .release = i2c_client_dev_release,
272};
273
David Brownell9b766b82008-01-27 18:14:51 +0100274
275/**
276 * i2c_verify_client - return parameter as i2c_client, or NULL
277 * @dev: device, probably from some driver model iterator
278 *
279 * When traversing the driver model tree, perhaps using driver model
280 * iterators like @device_for_each_child(), you can't assume very much
281 * about the nodes you find. Use this function to avoid oopses caused
282 * by wrongly treating some non-I2C device as an i2c_client.
283 */
284struct i2c_client *i2c_verify_client(struct device *dev)
285{
Jean Delvare51298d12009-09-18 22:45:45 +0200286 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100287 ? to_i2c_client(dev)
288 : NULL;
289}
290EXPORT_SYMBOL(i2c_verify_client);
291
292
David Brownell9c1600e2007-05-01 23:26:31 +0200293/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200294 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200295 * @adap: the adapter managing the device
296 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200297 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200298 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200299 * Create an i2c device. Binding is handled through driver model
300 * probe()/remove() methods. A driver may be bound to this device when we
301 * return from this function, or any later moment (e.g. maybe hotplugging will
302 * load the driver module). This call is not appropriate for use by mainboard
303 * initialization logic, which usually runs during an arch_initcall() long
304 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200305 *
306 * This returns the new i2c client, which may be saved for later use with
307 * i2c_unregister_device(); or NULL to indicate an error.
308 */
309struct i2c_client *
310i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
311{
312 struct i2c_client *client;
313 int status;
314
315 client = kzalloc(sizeof *client, GFP_KERNEL);
316 if (!client)
317 return NULL;
318
319 client->adapter = adap;
320
321 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200322
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200323 if (info->archdata)
324 client->dev.archdata = *info->archdata;
325
Marc Pignatee354252008-08-28 08:33:22 +0200326 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200327 client->addr = info->addr;
328 client->irq = info->irq;
329
David Brownell9c1600e2007-05-01 23:26:31 +0200330 strlcpy(client->name, info->type, sizeof(client->name));
331
Jean Delvaref8a227e2009-06-19 16:58:18 +0200332 /* Check for address business */
333 status = i2c_check_addr(adap, client->addr);
334 if (status)
335 goto out_err;
336
337 client->dev.parent = &client->adapter->dev;
338 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200339 client->dev.type = &i2c_client_type;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200340
341 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
342 client->addr);
343 status = device_register(&client->dev);
344 if (status)
345 goto out_err;
346
Jean Delvaref8a227e2009-06-19 16:58:18 +0200347 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
348 client->name, dev_name(&client->dev));
349
David Brownell9c1600e2007-05-01 23:26:31 +0200350 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200351
352out_err:
353 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
354 "(%d)\n", client->name, client->addr, status);
355 kfree(client);
356 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200357}
358EXPORT_SYMBOL_GPL(i2c_new_device);
359
360
361/**
362 * i2c_unregister_device - reverse effect of i2c_new_device()
363 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200364 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200365 */
366void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200367{
David Brownella1d9e6e2007-05-01 23:26:30 +0200368 device_unregister(&client->dev);
369}
David Brownell9c1600e2007-05-01 23:26:31 +0200370EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200371
372
Jean Delvare60b129d2008-05-11 20:37:06 +0200373static const struct i2c_device_id dummy_id[] = {
374 { "dummy", 0 },
375 { },
376};
377
Jean Delvared2653e92008-04-29 23:11:39 +0200378static int dummy_probe(struct i2c_client *client,
379 const struct i2c_device_id *id)
380{
381 return 0;
382}
383
384static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100385{
386 return 0;
387}
388
389static struct i2c_driver dummy_driver = {
390 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200391 .probe = dummy_probe,
392 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200393 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100394};
395
396/**
397 * i2c_new_dummy - return a new i2c device bound to a dummy driver
398 * @adapter: the adapter managing the device
399 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100400 * Context: can sleep
401 *
402 * This returns an I2C client bound to the "dummy" driver, intended for use
403 * with devices that consume multiple addresses. Examples of such chips
404 * include various EEPROMS (like 24c04 and 24c08 models).
405 *
406 * These dummy devices have two main uses. First, most I2C and SMBus calls
407 * except i2c_transfer() need a client handle; the dummy will be that handle.
408 * And second, this prevents the specified address from being bound to a
409 * different driver.
410 *
411 * This returns the new i2c client, which should be saved for later use with
412 * i2c_unregister_device(); or NULL to indicate an error.
413 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100414struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100415{
416 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200417 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100418 };
419
David Brownelle9f13732008-01-27 18:14:52 +0100420 return i2c_new_device(adapter, &info);
421}
422EXPORT_SYMBOL_GPL(i2c_new_dummy);
423
David Brownellf37dd802007-02-13 22:09:00 +0100424/* ------------------------------------------------------------------------- */
425
David Brownell16ffadf2007-05-01 23:26:28 +0200426/* I2C bus adapters -- one roots each I2C or SMBUS segment */
427
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200428static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
David Brownellef2c83212007-05-01 23:26:28 +0200430 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 complete(&adap->dev_released);
432}
433
Jean Delvare99cd8e22009-06-19 16:58:20 +0200434/*
435 * Let users instantiate I2C devices through sysfs. This can be used when
436 * platform initialization code doesn't contain the proper data for
437 * whatever reason. Also useful for drivers that do device detection and
438 * detection fails, either because the device uses an unexpected address,
439 * or this is a compatible device with different ID register values.
440 *
441 * Parameter checking may look overzealous, but we really don't want
442 * the user to provide incorrect parameters.
443 */
444static ssize_t
445i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
446 const char *buf, size_t count)
447{
448 struct i2c_adapter *adap = to_i2c_adapter(dev);
449 struct i2c_board_info info;
450 struct i2c_client *client;
451 char *blank, end;
452 int res;
453
454 dev_warn(dev, "The new_device interface is still experimental "
455 "and may change in a near future\n");
456 memset(&info, 0, sizeof(struct i2c_board_info));
457
458 blank = strchr(buf, ' ');
459 if (!blank) {
460 dev_err(dev, "%s: Missing parameters\n", "new_device");
461 return -EINVAL;
462 }
463 if (blank - buf > I2C_NAME_SIZE - 1) {
464 dev_err(dev, "%s: Invalid device name\n", "new_device");
465 return -EINVAL;
466 }
467 memcpy(info.type, buf, blank - buf);
468
469 /* Parse remaining parameters, reject extra parameters */
470 res = sscanf(++blank, "%hi%c", &info.addr, &end);
471 if (res < 1) {
472 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
473 return -EINVAL;
474 }
475 if (res > 1 && end != '\n') {
476 dev_err(dev, "%s: Extra parameters\n", "new_device");
477 return -EINVAL;
478 }
479
480 if (info.addr < 0x03 || info.addr > 0x77) {
481 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
482 info.addr);
483 return -EINVAL;
484 }
485
486 client = i2c_new_device(adap, &info);
487 if (!client)
488 return -EEXIST;
489
490 /* Keep track of the added device */
491 mutex_lock(&core_lock);
492 list_add_tail(&client->detected, &userspace_devices);
493 mutex_unlock(&core_lock);
494 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
495 info.type, info.addr);
496
497 return count;
498}
499
500/*
501 * And of course let the users delete the devices they instantiated, if
502 * they got it wrong. This interface can only be used to delete devices
503 * instantiated by i2c_sysfs_new_device above. This guarantees that we
504 * don't delete devices to which some kernel code still has references.
505 *
506 * Parameter checking may look overzealous, but we really don't want
507 * the user to delete the wrong device.
508 */
509static ssize_t
510i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
511 const char *buf, size_t count)
512{
513 struct i2c_adapter *adap = to_i2c_adapter(dev);
514 struct i2c_client *client, *next;
515 unsigned short addr;
516 char end;
517 int res;
518
519 /* Parse parameters, reject extra parameters */
520 res = sscanf(buf, "%hi%c", &addr, &end);
521 if (res < 1) {
522 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
523 return -EINVAL;
524 }
525 if (res > 1 && end != '\n') {
526 dev_err(dev, "%s: Extra parameters\n", "delete_device");
527 return -EINVAL;
528 }
529
530 /* Make sure the device was added through sysfs */
531 res = -ENOENT;
532 mutex_lock(&core_lock);
533 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
534 if (client->addr == addr && client->adapter == adap) {
535 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
536 "delete_device", client->name, client->addr);
537
538 list_del(&client->detected);
539 i2c_unregister_device(client);
540 res = count;
541 break;
542 }
543 }
544 mutex_unlock(&core_lock);
545
546 if (res < 0)
547 dev_err(dev, "%s: Can't find device in list\n",
548 "delete_device");
549 return res;
550}
551
Jean Delvare4f8cf822009-09-18 22:45:46 +0200552static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
553static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
554
555static struct attribute *i2c_adapter_attrs[] = {
556 &dev_attr_name.attr,
557 &dev_attr_new_device.attr,
558 &dev_attr_delete_device.attr,
559 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200560};
561
Jean Delvare4f8cf822009-09-18 22:45:46 +0200562static struct attribute_group i2c_adapter_attr_group = {
563 .attrs = i2c_adapter_attrs,
564};
565
566static const struct attribute_group *i2c_adapter_attr_groups[] = {
567 &i2c_adapter_attr_group,
568 NULL
569};
570
571static struct device_type i2c_adapter_type = {
572 .groups = i2c_adapter_attr_groups,
573 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200574};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Jean Delvare2bb50952009-09-18 22:45:46 +0200576#ifdef CONFIG_I2C_COMPAT
577static struct class_compat *i2c_adapter_compat_class;
578#endif
579
David Brownell9c1600e2007-05-01 23:26:31 +0200580static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
581{
582 struct i2c_devinfo *devinfo;
583
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200584 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200585 list_for_each_entry(devinfo, &__i2c_board_list, list) {
586 if (devinfo->busnum == adapter->nr
587 && !i2c_new_device(adapter,
588 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100589 dev_err(&adapter->dev,
590 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200591 devinfo->board_info.addr);
592 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200593 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200594}
595
Jean Delvare69b00892009-12-06 17:06:27 +0100596static int i2c_do_add_adapter(struct i2c_driver *driver,
597 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100598{
Jean Delvare4735c982008-07-14 22:38:36 +0200599 /* Detect supported devices on that bus, and instantiate them */
600 i2c_detect(adap, driver);
601
602 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100603 if (driver->attach_adapter) {
604 /* We ignore the return code; if it fails, too bad */
605 driver->attach_adapter(adap);
606 }
607 return 0;
608}
609
Jean Delvare69b00892009-12-06 17:06:27 +0100610static int __process_new_adapter(struct device_driver *d, void *data)
611{
612 return i2c_do_add_adapter(to_i2c_driver(d), data);
613}
614
David Brownell6e13e642007-05-01 23:26:31 +0200615static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Jean Delvare026526f2008-01-27 18:14:49 +0100617 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
David Brownell1d0b19c2008-10-14 17:30:05 +0200619 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200620 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
621 res = -EAGAIN;
622 goto out_list;
623 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200624
Mika Kuoppala194684e2009-12-06 17:06:22 +0100625 rt_mutex_init(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Jean Delvare8fcfef62009-03-28 21:34:43 +0100627 /* Set default timeout to 1 second if not already set */
628 if (adap->timeout == 0)
629 adap->timeout = HZ;
630
Kay Sievers27d9c182009-01-07 14:29:16 +0100631 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200632 adap->dev.bus = &i2c_bus_type;
633 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200634 res = device_register(&adap->dev);
635 if (res)
636 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200638 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
639
Jean Delvare2bb50952009-09-18 22:45:46 +0200640#ifdef CONFIG_I2C_COMPAT
641 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
642 adap->dev.parent);
643 if (res)
644 dev_warn(&adap->dev,
645 "Failed to create compatibility class link\n");
646#endif
647
Jean Delvare729d6dd2009-06-19 16:58:18 +0200648 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200649 if (adap->nr < __i2c_first_dynamic_bus_num)
650 i2c_scan_static_board_info(adap);
651
Jean Delvare4735c982008-07-14 22:38:36 +0200652 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200653 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100654 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100655 __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100656 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200657
658 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200659
Jean Delvareb119c6c2006-08-15 18:26:30 +0200660out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200661 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200662 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200663 mutex_unlock(&core_lock);
664 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
David Brownell6e13e642007-05-01 23:26:31 +0200667/**
668 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
669 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200670 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200671 *
672 * This routine is used to declare an I2C adapter when its bus number
673 * doesn't matter. Examples: for I2C adapters dynamically added by
674 * USB links or PCI plugin cards.
675 *
676 * When this returns zero, a new bus number was allocated and stored
677 * in adap->nr, and the specified adapter became available for clients.
678 * Otherwise, a negative errno value is returned.
679 */
680int i2c_add_adapter(struct i2c_adapter *adapter)
681{
682 int id, res = 0;
683
684retry:
685 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
686 return -ENOMEM;
687
Jean Delvarecaada322008-01-27 18:14:49 +0100688 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200689 /* "above" here means "above or equal to", sigh */
690 res = idr_get_new_above(&i2c_adapter_idr, adapter,
691 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100692 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200693
694 if (res < 0) {
695 if (res == -EAGAIN)
696 goto retry;
697 return res;
698 }
699
700 adapter->nr = id;
701 return i2c_register_adapter(adapter);
702}
703EXPORT_SYMBOL(i2c_add_adapter);
704
705/**
706 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
707 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200708 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200709 *
710 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100711 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
712 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200713 * is used to properly configure I2C devices.
714 *
715 * If no devices have pre-been declared for this bus, then be sure to
716 * register the adapter before any dynamically allocated ones. Otherwise
717 * the required bus ID may not be available.
718 *
719 * When this returns zero, the specified adapter became available for
720 * clients using the bus number provided in adap->nr. Also, the table
721 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
722 * and the appropriate driver model device nodes are created. Otherwise, a
723 * negative errno value is returned.
724 */
725int i2c_add_numbered_adapter(struct i2c_adapter *adap)
726{
727 int id;
728 int status;
729
730 if (adap->nr & ~MAX_ID_MASK)
731 return -EINVAL;
732
733retry:
734 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
735 return -ENOMEM;
736
Jean Delvarecaada322008-01-27 18:14:49 +0100737 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200738 /* "above" here means "above or equal to", sigh;
739 * we need the "equal to" result to force the result
740 */
741 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
742 if (status == 0 && id != adap->nr) {
743 status = -EBUSY;
744 idr_remove(&i2c_adapter_idr, id);
745 }
Jean Delvarecaada322008-01-27 18:14:49 +0100746 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200747 if (status == -EAGAIN)
748 goto retry;
749
750 if (status == 0)
751 status = i2c_register_adapter(adap);
752 return status;
753}
754EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
755
Jean Delvare69b00892009-12-06 17:06:27 +0100756static int i2c_do_del_adapter(struct i2c_driver *driver,
757 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +0100758{
Jean Delvare4735c982008-07-14 22:38:36 +0200759 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100760 int res;
761
Jean Delvareacec2112009-03-28 21:34:40 +0100762 /* Remove the devices we created ourselves as the result of hardware
763 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200764 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
765 if (client->adapter == adapter) {
766 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
767 client->name, client->addr);
768 list_del(&client->detected);
769 i2c_unregister_device(client);
770 }
771 }
772
Jean Delvare026526f2008-01-27 18:14:49 +0100773 if (!driver->detach_adapter)
774 return 0;
775 res = driver->detach_adapter(adapter);
776 if (res)
777 dev_err(&adapter->dev, "detach_adapter failed (%d) "
778 "for driver [%s]\n", res, driver->driver.name);
779 return res;
780}
781
Jean Delvaree549c2b2009-06-19 16:58:19 +0200782static int __unregister_client(struct device *dev, void *dummy)
783{
784 struct i2c_client *client = i2c_verify_client(dev);
785 if (client)
786 i2c_unregister_device(client);
787 return 0;
788}
789
Jean Delvare69b00892009-12-06 17:06:27 +0100790static int __process_removed_adapter(struct device_driver *d, void *data)
791{
792 return i2c_do_del_adapter(to_i2c_driver(d), data);
793}
794
David Brownelld64f73b2007-07-12 14:12:28 +0200795/**
796 * i2c_del_adapter - unregister I2C adapter
797 * @adap: the adapter being unregistered
798 * Context: can sleep
799 *
800 * This unregisters an I2C adapter which was previously registered
801 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
802 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803int i2c_del_adapter(struct i2c_adapter *adap)
804{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200806 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100807 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200810 mutex_lock(&core_lock);
811 found = idr_find(&i2c_adapter_idr, adap->nr);
812 mutex_unlock(&core_lock);
813 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200814 pr_debug("i2c-core: attempting to delete unregistered "
815 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200816 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
Jean Delvare026526f2008-01-27 18:14:49 +0100819 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200820 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100821 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100822 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200823 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100824 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200825 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100827 /* Remove devices instantiated from sysfs */
828 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
829 if (client->adapter == adap) {
830 dev_dbg(&adap->dev, "Removing %s at 0x%x\n",
831 client->name, client->addr);
832 list_del(&client->detected);
833 i2c_unregister_device(client);
834 }
835 }
836
Jean Delvaree549c2b2009-06-19 16:58:19 +0200837 /* Detach any active clients. This can't fail, thus we do not
838 checking the returned value. */
839 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Jean Delvare2bb50952009-09-18 22:45:46 +0200841#ifdef CONFIG_I2C_COMPAT
842 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
843 adap->dev.parent);
844#endif
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* clean up the sysfs representation */
847 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /* wait for sysfs to drop all references */
851 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
David Brownell6e13e642007-05-01 23:26:31 +0200853 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200854 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200856 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200858 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200860 /* Clear the device structure in case this adapter is ever going to be
861 added again */
862 memset(&adap->dev, 0, sizeof(adap->dev));
863
Jean Delvare35fc37f2009-06-19 16:58:19 +0200864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
David Brownellc0564602007-05-01 23:26:31 +0200866EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868
David Brownell7b4fbc52007-05-01 23:26:30 +0200869/* ------------------------------------------------------------------------- */
870
Jean Delvare69b00892009-12-06 17:06:27 +0100871static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +0200872{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200873 if (dev->type != &i2c_adapter_type)
874 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +0100875 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +0200876}
877
David Brownell7b4fbc52007-05-01 23:26:30 +0200878/*
879 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200880 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 */
882
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800883int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100885 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
David Brownell1d0b19c2008-10-14 17:30:05 +0200887 /* Can't register until after driver model init */
888 if (unlikely(WARN_ON(!i2c_bus_type.p)))
889 return -EAGAIN;
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800892 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Jean Delvare729d6dd2009-06-19 16:58:18 +0200895 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200896 * will have called probe() for all matching-but-unbound devices.
897 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 res = driver_register(&driver->driver);
899 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100900 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100901
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100902 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Jean Delvare4735c982008-07-14 22:38:36 +0200904 INIT_LIST_HEAD(&driver->clients);
905 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200906 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +0100907 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
Jean Delvarecaada322008-01-27 18:14:49 +0100908 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200909
Jean Delvare7eebcb72006-02-05 23:28:21 +0100910 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800912EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Jean Delvare69b00892009-12-06 17:06:27 +0100914static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +0200915{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200916 if (dev->type != &i2c_adapter_type)
917 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +0100918 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +0200919}
920
David Brownella1d9e6e2007-05-01 23:26:30 +0200921/**
922 * i2c_del_driver - unregister I2C driver
923 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200924 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200925 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200926void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Jean Delvarecaada322008-01-27 18:14:49 +0100928 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +0100929 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200930 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +0200931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100933 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
David Brownellc0564602007-05-01 23:26:31 +0200935EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
David Brownell7b4fbc52007-05-01 23:26:30 +0200937/* ------------------------------------------------------------------------- */
938
David Brownell9b766b82008-01-27 18:14:51 +0100939static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
David Brownell9b766b82008-01-27 18:14:51 +0100941 struct i2c_client *client = i2c_verify_client(dev);
942 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
David Brownell9b766b82008-01-27 18:14:51 +0100944 if (client && client->addr == addr)
945 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 0;
947}
948
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100949static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
David Brownell9b766b82008-01-27 18:14:51 +0100951 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Jean Delvaree48d3312008-01-27 18:14:48 +0100954/**
955 * i2c_use_client - increments the reference count of the i2c client structure
956 * @client: the client being referenced
957 *
958 * Each live reference to a client should be refcounted. The driver model does
959 * that automatically as part of driver binding, so that most drivers don't
960 * need to do this explicitly: they hold a reference until they're unbound
961 * from the device.
962 *
963 * A pointer to the client with the incremented reference counter is returned.
964 */
965struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
David Brownell6ea438e2008-07-14 22:38:24 +0200967 if (client && get_device(&client->dev))
968 return client;
969 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
David Brownellc0564602007-05-01 23:26:31 +0200971EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Jean Delvaree48d3312008-01-27 18:14:48 +0100973/**
974 * i2c_release_client - release a use of the i2c client structure
975 * @client: the client being no longer referenced
976 *
977 * Must be called when a user of a client is finished with it.
978 */
979void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
David Brownell6ea438e2008-07-14 22:38:24 +0200981 if (client)
982 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
David Brownellc0564602007-05-01 23:26:31 +0200984EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
David Brownell9b766b82008-01-27 18:14:51 +0100986struct i2c_cmd_arg {
987 unsigned cmd;
988 void *arg;
989};
990
991static int i2c_cmd(struct device *dev, void *_arg)
992{
993 struct i2c_client *client = i2c_verify_client(dev);
994 struct i2c_cmd_arg *arg = _arg;
995
996 if (client && client->driver && client->driver->command)
997 client->driver->command(client, arg->cmd, arg->arg);
998 return 0;
999}
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1002{
David Brownell9b766b82008-01-27 18:14:51 +01001003 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
David Brownell9b766b82008-01-27 18:14:51 +01001005 cmd_arg.cmd = cmd;
1006 cmd_arg.arg = arg;
1007 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
David Brownellc0564602007-05-01 23:26:31 +02001009EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011static int __init i2c_init(void)
1012{
1013 int retval;
1014
1015 retval = bus_register(&i2c_bus_type);
1016 if (retval)
1017 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001018#ifdef CONFIG_I2C_COMPAT
1019 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1020 if (!i2c_adapter_compat_class) {
1021 retval = -ENOMEM;
1022 goto bus_err;
1023 }
1024#endif
David Brownelle9f13732008-01-27 18:14:52 +01001025 retval = i2c_add_driver(&dummy_driver);
1026 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001027 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001028 return 0;
1029
Jean Delvare2bb50952009-09-18 22:45:46 +02001030class_err:
1031#ifdef CONFIG_I2C_COMPAT
1032 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001033bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001034#endif
David Brownelle9f13732008-01-27 18:14:52 +01001035 bus_unregister(&i2c_bus_type);
1036 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039static void __exit i2c_exit(void)
1040{
David Brownelle9f13732008-01-27 18:14:52 +01001041 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001042#ifdef CONFIG_I2C_COMPAT
1043 class_compat_unregister(i2c_adapter_compat_class);
1044#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 bus_unregister(&i2c_bus_type);
1046}
1047
David Brownella10f9e72008-10-14 17:30:06 +02001048/* We must initialize early, because some subsystems register i2c drivers
1049 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1050 */
1051postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052module_exit(i2c_exit);
1053
1054/* ----------------------------------------------------
1055 * the functional interface to the i2c busses.
1056 * ----------------------------------------------------
1057 */
1058
David Brownella1cdeda2008-07-14 22:38:24 +02001059/**
1060 * i2c_transfer - execute a single or combined I2C message
1061 * @adap: Handle to I2C bus
1062 * @msgs: One or more messages to execute before STOP is issued to
1063 * terminate the operation; each message begins with a START.
1064 * @num: Number of messages to be executed.
1065 *
1066 * Returns negative errno, else the number of messages executed.
1067 *
1068 * Note that there is no requirement that each message be sent to
1069 * the same slave address, although that is the most common model.
1070 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001071int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001073 unsigned long orig_jiffies;
1074 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
David Brownella1cdeda2008-07-14 22:38:24 +02001076 /* REVISIT the fault reporting model here is weak:
1077 *
1078 * - When we get an error after receiving N bytes from a slave,
1079 * there is no way to report "N".
1080 *
1081 * - When we get a NAK after transmitting N bytes to a slave,
1082 * there is no way to report "N" ... or to let the master
1083 * continue executing the rest of this combined message, if
1084 * that's the appropriate response.
1085 *
1086 * - When for example "num" is two and we successfully complete
1087 * the first message but get an error part way through the
1088 * second, it's unclear whether that should be reported as
1089 * one (discarding status on the second message) or errno
1090 * (discarding status on the first one).
1091 */
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (adap->algo->master_xfer) {
1094#ifdef DEBUG
1095 for (ret = 0; ret < num; ret++) {
1096 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001097 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1098 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1099 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101#endif
1102
Mike Rapoportcea443a2008-01-27 18:14:50 +01001103 if (in_atomic() || irqs_disabled()) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001104 ret = rt_mutex_trylock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001105 if (!ret)
1106 /* I2C activity is ongoing. */
1107 return -EAGAIN;
1108 } else {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001109 rt_mutex_lock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001110 }
1111
Clifford Wolf66b650f2009-06-15 18:01:46 +02001112 /* Retry automatically on arbitration loss */
1113 orig_jiffies = jiffies;
1114 for (ret = 0, try = 0; try <= adap->retries; try++) {
1115 ret = adap->algo->master_xfer(adap, msgs, num);
1116 if (ret != -EAGAIN)
1117 break;
1118 if (time_after(jiffies, orig_jiffies + adap->timeout))
1119 break;
1120 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001121 rt_mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 return ret;
1124 } else {
1125 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001126 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128}
David Brownellc0564602007-05-01 23:26:31 +02001129EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
David Brownella1cdeda2008-07-14 22:38:24 +02001131/**
1132 * i2c_master_send - issue a single I2C message in master transmit mode
1133 * @client: Handle to slave device
1134 * @buf: Data that will be written to the slave
1135 * @count: How many bytes to write
1136 *
1137 * Returns negative errno, or else the number of bytes written.
1138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1140{
1141 int ret;
1142 struct i2c_adapter *adap=client->adapter;
1143 struct i2c_msg msg;
1144
Jean Delvare815f55f2005-05-07 22:58:46 +02001145 msg.addr = client->addr;
1146 msg.flags = client->flags & I2C_M_TEN;
1147 msg.len = count;
1148 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001149
Jean Delvare815f55f2005-05-07 22:58:46 +02001150 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Jean Delvare815f55f2005-05-07 22:58:46 +02001152 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1153 transmitted, else error code. */
1154 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
David Brownellc0564602007-05-01 23:26:31 +02001156EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
David Brownella1cdeda2008-07-14 22:38:24 +02001158/**
1159 * i2c_master_recv - issue a single I2C message in master receive mode
1160 * @client: Handle to slave device
1161 * @buf: Where to store data read from slave
1162 * @count: How many bytes to read
1163 *
1164 * Returns negative errno, or else the number of bytes read.
1165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1167{
1168 struct i2c_adapter *adap=client->adapter;
1169 struct i2c_msg msg;
1170 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Jean Delvare815f55f2005-05-07 22:58:46 +02001172 msg.addr = client->addr;
1173 msg.flags = client->flags & I2C_M_TEN;
1174 msg.flags |= I2C_M_RD;
1175 msg.len = count;
1176 msg.buf = buf;
1177
1178 ret = i2c_transfer(adap, &msg, 1);
1179
1180 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1181 transmitted, else error code. */
1182 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
David Brownellc0564602007-05-01 23:26:31 +02001184EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186/* ----------------------------------------------------
1187 * the i2c address scanning function
1188 * Will not work for 10-bit addresses!
1189 * ----------------------------------------------------
1190 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001191
Jean Delvareccfbbd02009-12-06 17:06:25 +01001192static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001193 struct i2c_driver *driver)
1194{
1195 struct i2c_board_info info;
1196 struct i2c_adapter *adapter = temp_client->adapter;
1197 int addr = temp_client->addr;
1198 int err;
1199
1200 /* Make sure the address is valid */
1201 if (addr < 0x03 || addr > 0x77) {
1202 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1203 addr);
1204 return -EINVAL;
1205 }
1206
1207 /* Skip if already in use */
1208 if (i2c_check_addr(adapter, addr))
1209 return 0;
1210
Jean Delvareccfbbd02009-12-06 17:06:25 +01001211 /* Make sure there is something at this address */
1212 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) < 0)
1213 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001214
Jean Delvareccfbbd02009-12-06 17:06:25 +01001215 /* Prevent 24RF08 corruption */
1216 if ((addr & ~0x0f) == 0x50)
1217 i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL);
Jean Delvare4735c982008-07-14 22:38:36 +02001218
1219 /* Finally call the custom detection function */
1220 memset(&info, 0, sizeof(struct i2c_board_info));
1221 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001222 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001223 if (err) {
1224 /* -ENODEV is returned if the detection fails. We catch it
1225 here as this isn't an error. */
1226 return err == -ENODEV ? 0 : err;
1227 }
1228
1229 /* Consistency check */
1230 if (info.type[0] == '\0') {
1231 dev_err(&adapter->dev, "%s detection function provided "
1232 "no name for 0x%x\n", driver->driver.name,
1233 addr);
1234 } else {
1235 struct i2c_client *client;
1236
1237 /* Detection succeeded, instantiate the device */
1238 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1239 info.type, info.addr);
1240 client = i2c_new_device(adapter, &info);
1241 if (client)
1242 list_add_tail(&client->detected, &driver->clients);
1243 else
1244 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1245 info.type, info.addr);
1246 }
1247 return 0;
1248}
1249
1250static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1251{
Jean Delvarec3813d62009-12-14 21:17:25 +01001252 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001253 struct i2c_client *temp_client;
1254 int i, err = 0;
1255 int adap_id = i2c_adapter_id(adapter);
1256
Jean Delvarec3813d62009-12-14 21:17:25 +01001257 address_list = driver->address_list;
1258 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001259 return 0;
1260
1261 /* Set up a temporary client to help detect callback */
1262 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1263 if (!temp_client)
1264 return -ENOMEM;
1265 temp_client->adapter = adapter;
1266
Jean Delvare4329cf82008-08-28 08:33:23 +02001267 /* Stop here if the classes do not match */
1268 if (!(adapter->class & driver->class))
1269 goto exit_free;
1270
Jean Delvare4735c982008-07-14 22:38:36 +02001271 /* Stop here if we can't use SMBUS_QUICK */
1272 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
Jean Delvarec3813d62009-12-14 21:17:25 +01001273 if (address_list[0] == I2C_CLIENT_END)
Jean Delvare4735c982008-07-14 22:38:36 +02001274 goto exit_free;
1275
1276 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1277 "can't probe for chips\n");
1278 err = -EOPNOTSUPP;
1279 goto exit_free;
1280 }
1281
Jean Delvarec3813d62009-12-14 21:17:25 +01001282 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001283 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001284 "addr 0x%02x\n", adap_id, address_list[i]);
1285 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001286 err = i2c_detect_address(temp_client, driver);
Jean Delvare4735c982008-07-14 22:38:36 +02001287 if (err)
1288 goto exit_free;
1289 }
1290
1291 exit_free:
1292 kfree(temp_client);
1293 return err;
1294}
1295
Jean Delvare12b5053a2007-05-01 23:26:31 +02001296struct i2c_client *
1297i2c_new_probed_device(struct i2c_adapter *adap,
1298 struct i2c_board_info *info,
1299 unsigned short const *addr_list)
1300{
1301 int i;
1302
1303 /* Stop here if the bus doesn't support probing */
1304 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1305 dev_err(&adap->dev, "Probing not supported\n");
1306 return NULL;
1307 }
1308
Jean Delvare12b5053a2007-05-01 23:26:31 +02001309 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1310 /* Check address validity */
1311 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1312 dev_warn(&adap->dev, "Invalid 7-bit address "
1313 "0x%02x\n", addr_list[i]);
1314 continue;
1315 }
1316
1317 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001318 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001319 dev_dbg(&adap->dev, "Address 0x%02x already in "
1320 "use, not probing\n", addr_list[i]);
1321 continue;
1322 }
1323
1324 /* Test address responsiveness
1325 The default probe method is a quick write, but it is known
1326 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1327 and could also irreversibly write-protect some EEPROMs, so
1328 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1329 read instead. Also, some bus drivers don't implement
1330 quick write, so we fallback to a byte read it that case
1331 too. */
1332 if ((addr_list[i] & ~0x07) == 0x30
1333 || (addr_list[i] & ~0x0f) == 0x50
1334 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001335 union i2c_smbus_data data;
1336
Jean Delvare12b5053a2007-05-01 23:26:31 +02001337 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1338 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001339 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001340 break;
1341 } else {
1342 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1343 I2C_SMBUS_WRITE, 0,
1344 I2C_SMBUS_QUICK, NULL) >= 0)
1345 break;
1346 }
1347 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001348
1349 if (addr_list[i] == I2C_CLIENT_END) {
1350 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1351 return NULL;
1352 }
1353
1354 info->addr = addr_list[i];
1355 return i2c_new_device(adap, info);
1356}
1357EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359struct i2c_adapter* i2c_get_adapter(int id)
1360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001362
Jean Delvarecaada322008-01-27 18:14:49 +01001363 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001364 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001365 if (adapter && !try_module_get(adapter->owner))
1366 adapter = NULL;
1367
Jean Delvarecaada322008-01-27 18:14:49 +01001368 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001369 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
David Brownellc0564602007-05-01 23:26:31 +02001371EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373void i2c_put_adapter(struct i2c_adapter *adap)
1374{
1375 module_put(adap->owner);
1376}
David Brownellc0564602007-05-01 23:26:31 +02001377EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379/* The SMBus parts */
1380
David Brownell438d6c22006-12-10 21:21:31 +01001381#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001382static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001387 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 data = data ^ POLY;
1389 data = data << 1;
1390 }
1391 return (u8)(data >> 8);
1392}
1393
Jean Delvare421ef472005-10-26 21:28:55 +02001394/* Incremental CRC8 over count bytes in the array pointed to by p */
1395static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 int i;
1398
1399 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001400 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return crc;
1402}
1403
Jean Delvare421ef472005-10-26 21:28:55 +02001404/* Assume a 7-bit address, which is reasonable for SMBus */
1405static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Jean Delvare421ef472005-10-26 21:28:55 +02001407 /* The address will be sent first */
1408 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1409 pec = i2c_smbus_pec(pec, &addr, 1);
1410
1411 /* The data buffer follows */
1412 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Jean Delvare421ef472005-10-26 21:28:55 +02001415/* Used for write only transactions */
1416static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Jean Delvare421ef472005-10-26 21:28:55 +02001418 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1419 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Jean Delvare421ef472005-10-26 21:28:55 +02001422/* Return <0 on CRC error
1423 If there was a write before this read (most cases) we need to take the
1424 partial CRC from the write part into account.
1425 Note that this function does modify the message (we need to decrease the
1426 message length to hide the CRC byte from the caller). */
1427static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Jean Delvare421ef472005-10-26 21:28:55 +02001429 u8 rpec = msg->buf[--msg->len];
1430 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (rpec != cpec) {
1433 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1434 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001435 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
David Brownell438d6c22006-12-10 21:21:31 +01001437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
David Brownella1cdeda2008-07-14 22:38:24 +02001440/**
1441 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1442 * @client: Handle to slave device
1443 *
1444 * This executes the SMBus "receive byte" protocol, returning negative errno
1445 * else the byte received from the device.
1446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447s32 i2c_smbus_read_byte(struct i2c_client *client)
1448{
1449 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001450 int status;
1451
1452 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1453 I2C_SMBUS_READ, 0,
1454 I2C_SMBUS_BYTE, &data);
1455 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
David Brownellc0564602007-05-01 23:26:31 +02001457EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
David Brownella1cdeda2008-07-14 22:38:24 +02001459/**
1460 * i2c_smbus_write_byte - SMBus "send byte" protocol
1461 * @client: Handle to slave device
1462 * @value: Byte to be sent
1463 *
1464 * This executes the SMBus "send byte" protocol, returning negative errno
1465 * else zero on success.
1466 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1468{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001470 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
David Brownellc0564602007-05-01 23:26:31 +02001472EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
David Brownella1cdeda2008-07-14 22:38:24 +02001474/**
1475 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1476 * @client: Handle to slave device
1477 * @command: Byte interpreted by slave
1478 *
1479 * This executes the SMBus "read byte" protocol, returning negative errno
1480 * else a data byte received from the device.
1481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1483{
1484 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001485 int status;
1486
1487 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1488 I2C_SMBUS_READ, command,
1489 I2C_SMBUS_BYTE_DATA, &data);
1490 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
David Brownellc0564602007-05-01 23:26:31 +02001492EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
David Brownella1cdeda2008-07-14 22:38:24 +02001494/**
1495 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1496 * @client: Handle to slave device
1497 * @command: Byte interpreted by slave
1498 * @value: Byte being written
1499 *
1500 * This executes the SMBus "write byte" protocol, returning negative errno
1501 * else zero on success.
1502 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1504{
1505 union i2c_smbus_data data;
1506 data.byte = value;
1507 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1508 I2C_SMBUS_WRITE,command,
1509 I2C_SMBUS_BYTE_DATA,&data);
1510}
David Brownellc0564602007-05-01 23:26:31 +02001511EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
David Brownella1cdeda2008-07-14 22:38:24 +02001513/**
1514 * i2c_smbus_read_word_data - SMBus "read word" protocol
1515 * @client: Handle to slave device
1516 * @command: Byte interpreted by slave
1517 *
1518 * This executes the SMBus "read word" protocol, returning negative errno
1519 * else a 16-bit unsigned "word" received from the device.
1520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1522{
1523 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001524 int status;
1525
1526 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1527 I2C_SMBUS_READ, command,
1528 I2C_SMBUS_WORD_DATA, &data);
1529 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
David Brownellc0564602007-05-01 23:26:31 +02001531EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
David Brownella1cdeda2008-07-14 22:38:24 +02001533/**
1534 * i2c_smbus_write_word_data - SMBus "write word" protocol
1535 * @client: Handle to slave device
1536 * @command: Byte interpreted by slave
1537 * @value: 16-bit "word" being written
1538 *
1539 * This executes the SMBus "write word" protocol, returning negative errno
1540 * else zero on success.
1541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1543{
1544 union i2c_smbus_data data;
1545 data.word = value;
1546 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1547 I2C_SMBUS_WRITE,command,
1548 I2C_SMBUS_WORD_DATA,&data);
1549}
David Brownellc0564602007-05-01 23:26:31 +02001550EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
David Brownella64ec072007-10-13 23:56:31 +02001552/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001553 * i2c_smbus_process_call - SMBus "process call" protocol
1554 * @client: Handle to slave device
1555 * @command: Byte interpreted by slave
1556 * @value: 16-bit "word" being written
1557 *
1558 * This executes the SMBus "process call" protocol, returning negative errno
1559 * else a 16-bit unsigned "word" received from the device.
1560 */
1561s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1562{
1563 union i2c_smbus_data data;
1564 int status;
1565 data.word = value;
1566
1567 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1568 I2C_SMBUS_WRITE, command,
1569 I2C_SMBUS_PROC_CALL, &data);
1570 return (status < 0) ? status : data.word;
1571}
1572EXPORT_SYMBOL(i2c_smbus_process_call);
1573
1574/**
David Brownella1cdeda2008-07-14 22:38:24 +02001575 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001576 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001577 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001578 * @values: Byte array into which data will be read; big enough to hold
1579 * the data returned by the slave. SMBus allows at most 32 bytes.
1580 *
David Brownella1cdeda2008-07-14 22:38:24 +02001581 * This executes the SMBus "block read" protocol, returning negative errno
1582 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001583 *
1584 * Note that using this function requires that the client's adapter support
1585 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1586 * support this; its emulation through I2C messaging relies on a specific
1587 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1588 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001589s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1590 u8 *values)
1591{
1592 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001593 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001594
David Brownell24a5bb72008-07-14 22:38:23 +02001595 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1596 I2C_SMBUS_READ, command,
1597 I2C_SMBUS_BLOCK_DATA, &data);
1598 if (status)
1599 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001600
1601 memcpy(values, &data.block[1], data.block[0]);
1602 return data.block[0];
1603}
1604EXPORT_SYMBOL(i2c_smbus_read_block_data);
1605
David Brownella1cdeda2008-07-14 22:38:24 +02001606/**
1607 * i2c_smbus_write_block_data - SMBus "block write" protocol
1608 * @client: Handle to slave device
1609 * @command: Byte interpreted by slave
1610 * @length: Size of data block; SMBus allows at most 32 bytes
1611 * @values: Byte array which will be written.
1612 *
1613 * This executes the SMBus "block write" protocol, returning negative errno
1614 * else zero on success.
1615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001617 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (length > I2C_SMBUS_BLOCK_MAX)
1622 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001624 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1626 I2C_SMBUS_WRITE,command,
1627 I2C_SMBUS_BLOCK_DATA,&data);
1628}
David Brownellc0564602007-05-01 23:26:31 +02001629EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001632s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1633 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001636 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001637
Jean Delvare4b2643d2007-07-12 14:12:29 +02001638 if (length > I2C_SMBUS_BLOCK_MAX)
1639 length = I2C_SMBUS_BLOCK_MAX;
1640 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001641 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1642 I2C_SMBUS_READ, command,
1643 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1644 if (status < 0)
1645 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001646
1647 memcpy(values, &data.block[1], data.block[0]);
1648 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
David Brownellc0564602007-05-01 23:26:31 +02001650EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Jean Delvare21bbd692006-01-09 15:19:18 +11001652s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001653 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001654{
1655 union i2c_smbus_data data;
1656
1657 if (length > I2C_SMBUS_BLOCK_MAX)
1658 length = I2C_SMBUS_BLOCK_MAX;
1659 data.block[0] = length;
1660 memcpy(data.block + 1, values, length);
1661 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1662 I2C_SMBUS_WRITE, command,
1663 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1664}
David Brownellc0564602007-05-01 23:26:31 +02001665EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001666
David Brownell438d6c22006-12-10 21:21:31 +01001667/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001669static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001671 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 union i2c_smbus_data * data)
1673{
1674 /* So we need to generate a series of msgs. In the case of writing, we
1675 need to use only one message; when reading, we need two. We initialize
1676 most things with sane defaults, to keep the code below somewhat
1677 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001678 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1679 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001681 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1683 };
1684 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001685 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001686 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 msgbuf0[0] = command;
1689 switch(size) {
1690 case I2C_SMBUS_QUICK:
1691 msg[0].len = 0;
1692 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001693 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1694 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 num = 1;
1696 break;
1697 case I2C_SMBUS_BYTE:
1698 if (read_write == I2C_SMBUS_READ) {
1699 /* Special case: only a read! */
1700 msg[0].flags = I2C_M_RD | flags;
1701 num = 1;
1702 }
1703 break;
1704 case I2C_SMBUS_BYTE_DATA:
1705 if (read_write == I2C_SMBUS_READ)
1706 msg[1].len = 1;
1707 else {
1708 msg[0].len = 2;
1709 msgbuf0[1] = data->byte;
1710 }
1711 break;
1712 case I2C_SMBUS_WORD_DATA:
1713 if (read_write == I2C_SMBUS_READ)
1714 msg[1].len = 2;
1715 else {
1716 msg[0].len=3;
1717 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001718 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720 break;
1721 case I2C_SMBUS_PROC_CALL:
1722 num = 2; /* Special case */
1723 read_write = I2C_SMBUS_READ;
1724 msg[0].len = 3;
1725 msg[1].len = 2;
1726 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001727 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 break;
1729 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001731 msg[1].flags |= I2C_M_RECV_LEN;
1732 msg[1].len = 1; /* block length will be added by
1733 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 } else {
1735 msg[0].len = data->block[0] + 2;
1736 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001737 dev_err(&adapter->dev,
1738 "Invalid block write size %d\n",
1739 data->block[0]);
1740 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001742 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 msgbuf0[i] = data->block[i-1];
1744 }
1745 break;
1746 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001747 num = 2; /* Another special case */
1748 read_write = I2C_SMBUS_READ;
1749 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001750 dev_err(&adapter->dev,
1751 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001752 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001753 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001754 }
1755 msg[0].len = data->block[0] + 2;
1756 for (i = 1; i < msg[0].len; i++)
1757 msgbuf0[i] = data->block[i-1];
1758 msg[1].flags |= I2C_M_RECV_LEN;
1759 msg[1].len = 1; /* block length will be added by
1760 the underlying bus driver */
1761 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 case I2C_SMBUS_I2C_BLOCK_DATA:
1763 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001764 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 } else {
1766 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001767 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001768 dev_err(&adapter->dev,
1769 "Invalid block write size %d\n",
1770 data->block[0]);
1771 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773 for (i = 1; i <= data->block[0]; i++)
1774 msgbuf0[i] = data->block[i];
1775 }
1776 break;
1777 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001778 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1779 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781
Jean Delvare421ef472005-10-26 21:28:55 +02001782 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1783 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1784 if (i) {
1785 /* Compute PEC if first message is a write */
1786 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001787 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001788 i2c_smbus_add_pec(&msg[0]);
1789 else /* Write followed by read */
1790 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1791 }
1792 /* Ask for PEC if last message is a read */
1793 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001794 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001795 }
1796
David Brownell24a5bb72008-07-14 22:38:23 +02001797 status = i2c_transfer(adapter, msg, num);
1798 if (status < 0)
1799 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Jean Delvare421ef472005-10-26 21:28:55 +02001801 /* Check PEC if last message is a read */
1802 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001803 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1804 if (status < 0)
1805 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001806 }
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (read_write == I2C_SMBUS_READ)
1809 switch(size) {
1810 case I2C_SMBUS_BYTE:
1811 data->byte = msgbuf0[0];
1812 break;
1813 case I2C_SMBUS_BYTE_DATA:
1814 data->byte = msgbuf1[0];
1815 break;
David Brownell438d6c22006-12-10 21:21:31 +01001816 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 case I2C_SMBUS_PROC_CALL:
1818 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1819 break;
1820 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001821 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 data->block[i+1] = msgbuf1[i];
1823 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001824 case I2C_SMBUS_BLOCK_DATA:
1825 case I2C_SMBUS_BLOCK_PROC_CALL:
1826 for (i = 0; i < msgbuf1[0] + 1; i++)
1827 data->block[i] = msgbuf1[i];
1828 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830 return 0;
1831}
1832
David Brownella1cdeda2008-07-14 22:38:24 +02001833/**
1834 * i2c_smbus_xfer - execute SMBus protocol operations
1835 * @adapter: Handle to I2C bus
1836 * @addr: Address of SMBus slave on that bus
1837 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1838 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1839 * @command: Byte interpreted by slave, for protocols which use such bytes
1840 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1841 * @data: Data to be read or written
1842 *
1843 * This executes an SMBus protocol operation, and returns a negative
1844 * errno code else zero on success.
1845 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001846s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001847 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001848 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001850 unsigned long orig_jiffies;
1851 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 if (adapter->algo->smbus_xfer) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001857 rt_mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001858
1859 /* Retry automatically on arbitration loss */
1860 orig_jiffies = jiffies;
1861 for (res = 0, try = 0; try <= adapter->retries; try++) {
1862 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1863 read_write, command,
1864 protocol, data);
1865 if (res != -EAGAIN)
1866 break;
1867 if (time_after(jiffies,
1868 orig_jiffies + adapter->timeout))
1869 break;
1870 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001871 rt_mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 } else
1873 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001874 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return res;
1877}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1881MODULE_DESCRIPTION("I2C-Bus main module");
1882MODULE_LICENSE("GPL");