blob: a18a251dabf9abe3abbee44804009c9f9541f1a2 [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
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100158static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100159{
Jean Delvare51298d12009-09-18 22:45:45 +0200160 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100161 struct i2c_driver *driver;
162
Jean Delvare51298d12009-09-18 22:45:45 +0200163 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100164 return 0;
165 driver = to_i2c_driver(dev->driver);
166 if (!driver->suspend)
167 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200168 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100169}
170
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100171static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100172{
Jean Delvare51298d12009-09-18 22:45:45 +0200173 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100174 struct i2c_driver *driver;
175
Jean Delvare51298d12009-09-18 22:45:45 +0200176 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100177 return 0;
178 driver = to_i2c_driver(dev->driver);
179 if (!driver->resume)
180 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200181 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100182}
183
David Brownell9c1600e2007-05-01 23:26:31 +0200184static void i2c_client_dev_release(struct device *dev)
185{
186 kfree(to_i2c_client(dev));
187}
188
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100189static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200190show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200191{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200192 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
193 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200194}
195
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100196static ssize_t
197show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200198{
199 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200200 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200201}
202
Jean Delvare4f8cf822009-09-18 22:45:46 +0200203static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200204static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
205
206static struct attribute *i2c_dev_attrs[] = {
207 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200208 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200209 &dev_attr_modalias.attr,
210 NULL
211};
212
213static struct attribute_group i2c_dev_attr_group = {
214 .attrs = i2c_dev_attrs,
215};
216
217static const struct attribute_group *i2c_dev_attr_groups[] = {
218 &i2c_dev_attr_group,
219 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200220};
221
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200222struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100223 .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 Kingb864c7d2006-01-05 14:37:50 +0000230};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200231EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000232
Jean Delvare51298d12009-09-18 22:45:45 +0200233static 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 Brownell9b766b82008-01-27 18:14:51 +0100239
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 */
249struct i2c_client *i2c_verify_client(struct device *dev)
250{
Jean Delvare51298d12009-09-18 22:45:45 +0200251 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100252 ? to_i2c_client(dev)
253 : NULL;
254}
255EXPORT_SYMBOL(i2c_verify_client);
256
257
David Brownell9c1600e2007-05-01 23:26:31 +0200258/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200259 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200260 * @adap: the adapter managing the device
261 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200262 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200263 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200264 * 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 Brownell9c1600e2007-05-01 23:26:31 +0200270 *
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 */
274struct i2c_client *
275i2c_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 Brownell3bbb8352007-10-13 23:56:29 +0200287
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200288 if (info->archdata)
289 client->dev.archdata = *info->archdata;
290
Marc Pignatee354252008-08-28 08:33:22 +0200291 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200292 client->addr = info->addr;
293 client->irq = info->irq;
294
David Brownell9c1600e2007-05-01 23:26:31 +0200295 strlcpy(client->name, info->type, sizeof(client->name));
296
Jean Delvaref8a227e2009-06-19 16:58:18 +0200297 /* 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 Delvare51298d12009-09-18 22:45:45 +0200304 client->dev.type = &i2c_client_type;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200305
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 Delvaref8a227e2009-06-19 16:58:18 +0200312 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
313 client->name, dev_name(&client->dev));
314
David Brownell9c1600e2007-05-01 23:26:31 +0200315 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200316
317out_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 Brownell9c1600e2007-05-01 23:26:31 +0200322}
323EXPORT_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 Brownelld64f73b2007-07-12 14:12:28 +0200329 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200330 */
331void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200332{
David Brownella1d9e6e2007-05-01 23:26:30 +0200333 device_unregister(&client->dev);
334}
David Brownell9c1600e2007-05-01 23:26:31 +0200335EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200336
337
Jean Delvare60b129d2008-05-11 20:37:06 +0200338static const struct i2c_device_id dummy_id[] = {
339 { "dummy", 0 },
340 { },
341};
342
Jean Delvared2653e92008-04-29 23:11:39 +0200343static int dummy_probe(struct i2c_client *client,
344 const struct i2c_device_id *id)
345{
346 return 0;
347}
348
349static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100350{
351 return 0;
352}
353
354static struct i2c_driver dummy_driver = {
355 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200356 .probe = dummy_probe,
357 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200358 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100359};
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 Brownelle9f13732008-01-27 18:14:52 +0100365 * 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 Xu09b8ce02009-03-28 21:34:46 +0100379struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100380{
381 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200382 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100383 };
384
David Brownelle9f13732008-01-27 18:14:52 +0100385 return i2c_new_device(adapter, &info);
386}
387EXPORT_SYMBOL_GPL(i2c_new_dummy);
388
David Brownellf37dd802007-02-13 22:09:00 +0100389/* ------------------------------------------------------------------------- */
390
David Brownell16ffadf2007-05-01 23:26:28 +0200391/* I2C bus adapters -- one roots each I2C or SMBUS segment */
392
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200393static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
David Brownellef2c83212007-05-01 23:26:28 +0200395 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 complete(&adap->dev_released);
397}
398
Jean Delvare99cd8e22009-06-19 16:58:20 +0200399/*
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 */
409static ssize_t
410i2c_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 */
474static ssize_t
475i2c_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 Delvare4f8cf822009-09-18 22:45:46 +0200517static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
518static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
519
520static 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 Brownell16ffadf2007-05-01 23:26:28 +0200525};
526
Jean Delvare4f8cf822009-09-18 22:45:46 +0200527static struct attribute_group i2c_adapter_attr_group = {
528 .attrs = i2c_adapter_attrs,
529};
530
531static const struct attribute_group *i2c_adapter_attr_groups[] = {
532 &i2c_adapter_attr_group,
533 NULL
534};
535
536static struct device_type i2c_adapter_type = {
537 .groups = i2c_adapter_attr_groups,
538 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200539};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
David Brownell9c1600e2007-05-01 23:26:31 +0200541static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
542{
543 struct i2c_devinfo *devinfo;
544
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200545 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200546 list_for_each_entry(devinfo, &__i2c_board_list, list) {
547 if (devinfo->busnum == adapter->nr
548 && !i2c_new_device(adapter,
549 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100550 dev_err(&adapter->dev,
551 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200552 devinfo->board_info.addr);
553 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200554 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200555}
556
Jean Delvare026526f2008-01-27 18:14:49 +0100557static int i2c_do_add_adapter(struct device_driver *d, void *data)
558{
559 struct i2c_driver *driver = to_i2c_driver(d);
560 struct i2c_adapter *adap = data;
561
Jean Delvare4735c982008-07-14 22:38:36 +0200562 /* Detect supported devices on that bus, and instantiate them */
563 i2c_detect(adap, driver);
564
565 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100566 if (driver->attach_adapter) {
567 /* We ignore the return code; if it fails, too bad */
568 driver->attach_adapter(adap);
569 }
570 return 0;
571}
572
David Brownell6e13e642007-05-01 23:26:31 +0200573static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Jean Delvare026526f2008-01-27 18:14:49 +0100575 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
David Brownell1d0b19c2008-10-14 17:30:05 +0200577 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200578 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
579 res = -EAGAIN;
580 goto out_list;
581 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200582
Ingo Molnar5c085d32006-01-18 23:16:04 +0100583 mutex_init(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Jean Delvare8fcfef62009-03-28 21:34:43 +0100585 /* Set default timeout to 1 second if not already set */
586 if (adap->timeout == 0)
587 adap->timeout = HZ;
588
Kay Sievers27d9c182009-01-07 14:29:16 +0100589 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200590 adap->dev.bus = &i2c_bus_type;
591 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200592 res = device_register(&adap->dev);
593 if (res)
594 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200596 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
597
Jean Delvare729d6dd2009-06-19 16:58:18 +0200598 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200599 if (adap->nr < __i2c_first_dynamic_bus_num)
600 i2c_scan_static_board_info(adap);
601
Jean Delvare4735c982008-07-14 22:38:36 +0200602 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200603 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100604 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
605 i2c_do_add_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100606 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200607
608 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200609
Jean Delvareb119c6c2006-08-15 18:26:30 +0200610out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200611 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200612 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200613 mutex_unlock(&core_lock);
614 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
David Brownell6e13e642007-05-01 23:26:31 +0200617/**
618 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
619 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200620 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200621 *
622 * This routine is used to declare an I2C adapter when its bus number
623 * doesn't matter. Examples: for I2C adapters dynamically added by
624 * USB links or PCI plugin cards.
625 *
626 * When this returns zero, a new bus number was allocated and stored
627 * in adap->nr, and the specified adapter became available for clients.
628 * Otherwise, a negative errno value is returned.
629 */
630int i2c_add_adapter(struct i2c_adapter *adapter)
631{
632 int id, res = 0;
633
634retry:
635 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
636 return -ENOMEM;
637
Jean Delvarecaada322008-01-27 18:14:49 +0100638 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200639 /* "above" here means "above or equal to", sigh */
640 res = idr_get_new_above(&i2c_adapter_idr, adapter,
641 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100642 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200643
644 if (res < 0) {
645 if (res == -EAGAIN)
646 goto retry;
647 return res;
648 }
649
650 adapter->nr = id;
651 return i2c_register_adapter(adapter);
652}
653EXPORT_SYMBOL(i2c_add_adapter);
654
655/**
656 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
657 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200658 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200659 *
660 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100661 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
662 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200663 * is used to properly configure I2C devices.
664 *
665 * If no devices have pre-been declared for this bus, then be sure to
666 * register the adapter before any dynamically allocated ones. Otherwise
667 * the required bus ID may not be available.
668 *
669 * When this returns zero, the specified adapter became available for
670 * clients using the bus number provided in adap->nr. Also, the table
671 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
672 * and the appropriate driver model device nodes are created. Otherwise, a
673 * negative errno value is returned.
674 */
675int i2c_add_numbered_adapter(struct i2c_adapter *adap)
676{
677 int id;
678 int status;
679
680 if (adap->nr & ~MAX_ID_MASK)
681 return -EINVAL;
682
683retry:
684 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
685 return -ENOMEM;
686
Jean Delvarecaada322008-01-27 18:14:49 +0100687 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200688 /* "above" here means "above or equal to", sigh;
689 * we need the "equal to" result to force the result
690 */
691 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
692 if (status == 0 && id != adap->nr) {
693 status = -EBUSY;
694 idr_remove(&i2c_adapter_idr, id);
695 }
Jean Delvarecaada322008-01-27 18:14:49 +0100696 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200697 if (status == -EAGAIN)
698 goto retry;
699
700 if (status == 0)
701 status = i2c_register_adapter(adap);
702 return status;
703}
704EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
705
Jean Delvare026526f2008-01-27 18:14:49 +0100706static int i2c_do_del_adapter(struct device_driver *d, void *data)
707{
708 struct i2c_driver *driver = to_i2c_driver(d);
709 struct i2c_adapter *adapter = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200710 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100711 int res;
712
Jean Delvareacec2112009-03-28 21:34:40 +0100713 /* Remove the devices we created ourselves as the result of hardware
714 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200715 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
716 if (client->adapter == adapter) {
717 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
718 client->name, client->addr);
719 list_del(&client->detected);
720 i2c_unregister_device(client);
721 }
722 }
723
Jean Delvare026526f2008-01-27 18:14:49 +0100724 if (!driver->detach_adapter)
725 return 0;
726 res = driver->detach_adapter(adapter);
727 if (res)
728 dev_err(&adapter->dev, "detach_adapter failed (%d) "
729 "for driver [%s]\n", res, driver->driver.name);
730 return res;
731}
732
Jean Delvaree549c2b2009-06-19 16:58:19 +0200733static int __unregister_client(struct device *dev, void *dummy)
734{
735 struct i2c_client *client = i2c_verify_client(dev);
736 if (client)
737 i2c_unregister_device(client);
738 return 0;
739}
740
David Brownelld64f73b2007-07-12 14:12:28 +0200741/**
742 * i2c_del_adapter - unregister I2C adapter
743 * @adap: the adapter being unregistered
744 * Context: can sleep
745 *
746 * This unregisters an I2C adapter which was previously registered
747 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
748 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749int i2c_del_adapter(struct i2c_adapter *adap)
750{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200752 struct i2c_adapter *found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200755 mutex_lock(&core_lock);
756 found = idr_find(&i2c_adapter_idr, adap->nr);
757 mutex_unlock(&core_lock);
758 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200759 pr_debug("i2c-core: attempting to delete unregistered "
760 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200761 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763
Jean Delvare026526f2008-01-27 18:14:49 +0100764 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200765 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100766 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
767 i2c_do_del_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200768 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100769 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200770 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Jean Delvaree549c2b2009-06-19 16:58:19 +0200772 /* Detach any active clients. This can't fail, thus we do not
773 checking the returned value. */
774 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /* clean up the sysfs representation */
777 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* wait for sysfs to drop all references */
781 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
David Brownell6e13e642007-05-01 23:26:31 +0200783 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200784 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200786 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200788 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200790 /* Clear the device structure in case this adapter is ever going to be
791 added again */
792 memset(&adap->dev, 0, sizeof(adap->dev));
793
Jean Delvare35fc37f2009-06-19 16:58:19 +0200794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
David Brownellc0564602007-05-01 23:26:31 +0200796EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798
David Brownell7b4fbc52007-05-01 23:26:30 +0200799/* ------------------------------------------------------------------------- */
800
Dave Young7f101a92008-07-14 22:38:19 +0200801static int __attach_adapter(struct device *dev, void *data)
802{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200803 struct i2c_adapter *adapter;
Dave Young7f101a92008-07-14 22:38:19 +0200804 struct i2c_driver *driver = data;
805
Jean Delvare4f8cf822009-09-18 22:45:46 +0200806 if (dev->type != &i2c_adapter_type)
807 return 0;
808 adapter = to_i2c_adapter(dev);
809
Jean Delvare4735c982008-07-14 22:38:36 +0200810 i2c_detect(adapter, driver);
811
812 /* Legacy drivers scan i2c busses directly */
813 if (driver->attach_adapter)
814 driver->attach_adapter(adapter);
Dave Young7f101a92008-07-14 22:38:19 +0200815
816 return 0;
817}
818
David Brownell7b4fbc52007-05-01 23:26:30 +0200819/*
820 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200821 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
823
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800824int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100826 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
David Brownell1d0b19c2008-10-14 17:30:05 +0200828 /* Can't register until after driver model init */
829 if (unlikely(WARN_ON(!i2c_bus_type.p)))
830 return -EAGAIN;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800833 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Jean Delvare729d6dd2009-06-19 16:58:18 +0200836 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200837 * will have called probe() for all matching-but-unbound devices.
838 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 res = driver_register(&driver->driver);
840 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100841 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100842
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100843 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Jean Delvare4735c982008-07-14 22:38:36 +0200845 INIT_LIST_HEAD(&driver->clients);
846 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200847 mutex_lock(&core_lock);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200848 bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100849 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200850
Jean Delvare7eebcb72006-02-05 23:28:21 +0100851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800853EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Dave Young7f101a92008-07-14 22:38:19 +0200855static int __detach_adapter(struct device *dev, void *data)
856{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200857 struct i2c_adapter *adapter;
Dave Young7f101a92008-07-14 22:38:19 +0200858 struct i2c_driver *driver = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200859 struct i2c_client *client, *_n;
860
Jean Delvare4f8cf822009-09-18 22:45:46 +0200861 if (dev->type != &i2c_adapter_type)
862 return 0;
863 adapter = to_i2c_adapter(dev);
864
Jean Delvareacec2112009-03-28 21:34:40 +0100865 /* Remove the devices we created ourselves as the result of hardware
866 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200867 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
868 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
869 client->name, client->addr);
870 list_del(&client->detected);
871 i2c_unregister_device(client);
872 }
873
Dave Young7f101a92008-07-14 22:38:19 +0200874 if (driver->detach_adapter) {
875 if (driver->detach_adapter(adapter))
876 dev_err(&adapter->dev,
877 "detach_adapter failed for driver [%s]\n",
878 driver->driver.name);
Dave Young7f101a92008-07-14 22:38:19 +0200879 }
880
881 return 0;
882}
883
David Brownella1d9e6e2007-05-01 23:26:30 +0200884/**
885 * i2c_del_driver - unregister I2C driver
886 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200887 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200888 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200889void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Jean Delvarecaada322008-01-27 18:14:49 +0100891 mutex_lock(&core_lock);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200892 bus_for_each_dev(&i2c_bus_type, NULL, driver, __detach_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200893 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +0200894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100896 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
David Brownellc0564602007-05-01 23:26:31 +0200898EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
David Brownell7b4fbc52007-05-01 23:26:30 +0200900/* ------------------------------------------------------------------------- */
901
David Brownell9b766b82008-01-27 18:14:51 +0100902static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
David Brownell9b766b82008-01-27 18:14:51 +0100904 struct i2c_client *client = i2c_verify_client(dev);
905 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
David Brownell9b766b82008-01-27 18:14:51 +0100907 if (client && client->addr == addr)
908 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return 0;
910}
911
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100912static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
David Brownell9b766b82008-01-27 18:14:51 +0100914 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Jean Delvaree48d3312008-01-27 18:14:48 +0100917/**
918 * i2c_use_client - increments the reference count of the i2c client structure
919 * @client: the client being referenced
920 *
921 * Each live reference to a client should be refcounted. The driver model does
922 * that automatically as part of driver binding, so that most drivers don't
923 * need to do this explicitly: they hold a reference until they're unbound
924 * from the device.
925 *
926 * A pointer to the client with the incremented reference counter is returned.
927 */
928struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
David Brownell6ea438e2008-07-14 22:38:24 +0200930 if (client && get_device(&client->dev))
931 return client;
932 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
David Brownellc0564602007-05-01 23:26:31 +0200934EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Jean Delvaree48d3312008-01-27 18:14:48 +0100936/**
937 * i2c_release_client - release a use of the i2c client structure
938 * @client: the client being no longer referenced
939 *
940 * Must be called when a user of a client is finished with it.
941 */
942void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
David Brownell6ea438e2008-07-14 22:38:24 +0200944 if (client)
945 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
David Brownellc0564602007-05-01 23:26:31 +0200947EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
David Brownell9b766b82008-01-27 18:14:51 +0100949struct i2c_cmd_arg {
950 unsigned cmd;
951 void *arg;
952};
953
954static int i2c_cmd(struct device *dev, void *_arg)
955{
956 struct i2c_client *client = i2c_verify_client(dev);
957 struct i2c_cmd_arg *arg = _arg;
958
959 if (client && client->driver && client->driver->command)
960 client->driver->command(client, arg->cmd, arg->arg);
961 return 0;
962}
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
965{
David Brownell9b766b82008-01-27 18:14:51 +0100966 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
David Brownell9b766b82008-01-27 18:14:51 +0100968 cmd_arg.cmd = cmd;
969 cmd_arg.arg = arg;
970 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
David Brownellc0564602007-05-01 23:26:31 +0200972EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974static int __init i2c_init(void)
975{
976 int retval;
977
978 retval = bus_register(&i2c_bus_type);
979 if (retval)
980 return retval;
David Brownelle9f13732008-01-27 18:14:52 +0100981 retval = i2c_add_driver(&dummy_driver);
982 if (retval)
Jean Delvare4f8cf822009-09-18 22:45:46 +0200983 goto bus_err;
David Brownelle9f13732008-01-27 18:14:52 +0100984 return 0;
985
David Brownelle9f13732008-01-27 18:14:52 +0100986bus_err:
987 bus_unregister(&i2c_bus_type);
988 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991static void __exit i2c_exit(void)
992{
David Brownelle9f13732008-01-27 18:14:52 +0100993 i2c_del_driver(&dummy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 bus_unregister(&i2c_bus_type);
995}
996
David Brownella10f9e72008-10-14 17:30:06 +0200997/* We must initialize early, because some subsystems register i2c drivers
998 * in subsys_initcall() code, but are linked (and initialized) before i2c.
999 */
1000postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001module_exit(i2c_exit);
1002
1003/* ----------------------------------------------------
1004 * the functional interface to the i2c busses.
1005 * ----------------------------------------------------
1006 */
1007
David Brownella1cdeda2008-07-14 22:38:24 +02001008/**
1009 * i2c_transfer - execute a single or combined I2C message
1010 * @adap: Handle to I2C bus
1011 * @msgs: One or more messages to execute before STOP is issued to
1012 * terminate the operation; each message begins with a START.
1013 * @num: Number of messages to be executed.
1014 *
1015 * Returns negative errno, else the number of messages executed.
1016 *
1017 * Note that there is no requirement that each message be sent to
1018 * the same slave address, although that is the most common model.
1019 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001020int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001022 unsigned long orig_jiffies;
1023 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
David Brownella1cdeda2008-07-14 22:38:24 +02001025 /* REVISIT the fault reporting model here is weak:
1026 *
1027 * - When we get an error after receiving N bytes from a slave,
1028 * there is no way to report "N".
1029 *
1030 * - When we get a NAK after transmitting N bytes to a slave,
1031 * there is no way to report "N" ... or to let the master
1032 * continue executing the rest of this combined message, if
1033 * that's the appropriate response.
1034 *
1035 * - When for example "num" is two and we successfully complete
1036 * the first message but get an error part way through the
1037 * second, it's unclear whether that should be reported as
1038 * one (discarding status on the second message) or errno
1039 * (discarding status on the first one).
1040 */
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (adap->algo->master_xfer) {
1043#ifdef DEBUG
1044 for (ret = 0; ret < num; ret++) {
1045 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001046 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1047 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1048 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050#endif
1051
Mike Rapoportcea443a2008-01-27 18:14:50 +01001052 if (in_atomic() || irqs_disabled()) {
1053 ret = mutex_trylock(&adap->bus_lock);
1054 if (!ret)
1055 /* I2C activity is ongoing. */
1056 return -EAGAIN;
1057 } else {
1058 mutex_lock_nested(&adap->bus_lock, adap->level);
1059 }
1060
Clifford Wolf66b650f2009-06-15 18:01:46 +02001061 /* Retry automatically on arbitration loss */
1062 orig_jiffies = jiffies;
1063 for (ret = 0, try = 0; try <= adap->retries; try++) {
1064 ret = adap->algo->master_xfer(adap, msgs, num);
1065 if (ret != -EAGAIN)
1066 break;
1067 if (time_after(jiffies, orig_jiffies + adap->timeout))
1068 break;
1069 }
Ingo Molnar5c085d32006-01-18 23:16:04 +01001070 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 return ret;
1073 } else {
1074 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001075 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077}
David Brownellc0564602007-05-01 23:26:31 +02001078EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
David Brownella1cdeda2008-07-14 22:38:24 +02001080/**
1081 * i2c_master_send - issue a single I2C message in master transmit mode
1082 * @client: Handle to slave device
1083 * @buf: Data that will be written to the slave
1084 * @count: How many bytes to write
1085 *
1086 * Returns negative errno, or else the number of bytes written.
1087 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1089{
1090 int ret;
1091 struct i2c_adapter *adap=client->adapter;
1092 struct i2c_msg msg;
1093
Jean Delvare815f55f2005-05-07 22:58:46 +02001094 msg.addr = client->addr;
1095 msg.flags = client->flags & I2C_M_TEN;
1096 msg.len = count;
1097 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001098
Jean Delvare815f55f2005-05-07 22:58:46 +02001099 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Jean Delvare815f55f2005-05-07 22:58:46 +02001101 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1102 transmitted, else error code. */
1103 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
David Brownellc0564602007-05-01 23:26:31 +02001105EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
David Brownella1cdeda2008-07-14 22:38:24 +02001107/**
1108 * i2c_master_recv - issue a single I2C message in master receive mode
1109 * @client: Handle to slave device
1110 * @buf: Where to store data read from slave
1111 * @count: How many bytes to read
1112 *
1113 * Returns negative errno, or else the number of bytes read.
1114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1116{
1117 struct i2c_adapter *adap=client->adapter;
1118 struct i2c_msg msg;
1119 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Jean Delvare815f55f2005-05-07 22:58:46 +02001121 msg.addr = client->addr;
1122 msg.flags = client->flags & I2C_M_TEN;
1123 msg.flags |= I2C_M_RD;
1124 msg.len = count;
1125 msg.buf = buf;
1126
1127 ret = i2c_transfer(adap, &msg, 1);
1128
1129 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1130 transmitted, else error code. */
1131 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
David Brownellc0564602007-05-01 23:26:31 +02001133EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135/* ----------------------------------------------------
1136 * the i2c address scanning function
1137 * Will not work for 10-bit addresses!
1138 * ----------------------------------------------------
1139 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001140
Jean Delvare4735c982008-07-14 22:38:36 +02001141static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1142 struct i2c_driver *driver)
1143{
1144 struct i2c_board_info info;
1145 struct i2c_adapter *adapter = temp_client->adapter;
1146 int addr = temp_client->addr;
1147 int err;
1148
1149 /* Make sure the address is valid */
1150 if (addr < 0x03 || addr > 0x77) {
1151 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1152 addr);
1153 return -EINVAL;
1154 }
1155
1156 /* Skip if already in use */
1157 if (i2c_check_addr(adapter, addr))
1158 return 0;
1159
1160 /* Make sure there is something at this address, unless forced */
1161 if (kind < 0) {
1162 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1163 I2C_SMBUS_QUICK, NULL) < 0)
1164 return 0;
1165
1166 /* prevent 24RF08 corruption */
1167 if ((addr & ~0x0f) == 0x50)
1168 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1169 I2C_SMBUS_QUICK, NULL);
1170 }
1171
1172 /* Finally call the custom detection function */
1173 memset(&info, 0, sizeof(struct i2c_board_info));
1174 info.addr = addr;
1175 err = driver->detect(temp_client, kind, &info);
1176 if (err) {
1177 /* -ENODEV is returned if the detection fails. We catch it
1178 here as this isn't an error. */
1179 return err == -ENODEV ? 0 : err;
1180 }
1181
1182 /* Consistency check */
1183 if (info.type[0] == '\0') {
1184 dev_err(&adapter->dev, "%s detection function provided "
1185 "no name for 0x%x\n", driver->driver.name,
1186 addr);
1187 } else {
1188 struct i2c_client *client;
1189
1190 /* Detection succeeded, instantiate the device */
1191 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1192 info.type, info.addr);
1193 client = i2c_new_device(adapter, &info);
1194 if (client)
1195 list_add_tail(&client->detected, &driver->clients);
1196 else
1197 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1198 info.type, info.addr);
1199 }
1200 return 0;
1201}
1202
1203static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1204{
1205 const struct i2c_client_address_data *address_data;
1206 struct i2c_client *temp_client;
1207 int i, err = 0;
1208 int adap_id = i2c_adapter_id(adapter);
1209
1210 address_data = driver->address_data;
1211 if (!driver->detect || !address_data)
1212 return 0;
1213
1214 /* Set up a temporary client to help detect callback */
1215 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1216 if (!temp_client)
1217 return -ENOMEM;
1218 temp_client->adapter = adapter;
1219
1220 /* Force entries are done first, and are not affected by ignore
1221 entries */
1222 if (address_data->forces) {
1223 const unsigned short * const *forces = address_data->forces;
1224 int kind;
1225
1226 for (kind = 0; forces[kind]; kind++) {
1227 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1228 i += 2) {
1229 if (forces[kind][i] == adap_id
1230 || forces[kind][i] == ANY_I2C_BUS) {
1231 dev_dbg(&adapter->dev, "found force "
1232 "parameter for adapter %d, "
1233 "addr 0x%02x, kind %d\n",
1234 adap_id, forces[kind][i + 1],
1235 kind);
1236 temp_client->addr = forces[kind][i + 1];
1237 err = i2c_detect_address(temp_client,
1238 kind, driver);
1239 if (err)
1240 goto exit_free;
1241 }
1242 }
1243 }
1244 }
1245
Jean Delvare4329cf82008-08-28 08:33:23 +02001246 /* Stop here if the classes do not match */
1247 if (!(adapter->class & driver->class))
1248 goto exit_free;
1249
Jean Delvare4735c982008-07-14 22:38:36 +02001250 /* Stop here if we can't use SMBUS_QUICK */
1251 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1252 if (address_data->probe[0] == I2C_CLIENT_END
1253 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1254 goto exit_free;
1255
1256 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1257 "can't probe for chips\n");
1258 err = -EOPNOTSUPP;
1259 goto exit_free;
1260 }
1261
Jean Delvare4735c982008-07-14 22:38:36 +02001262 /* Probe entries are done second, and are not affected by ignore
1263 entries either */
1264 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1265 if (address_data->probe[i] == adap_id
1266 || address_data->probe[i] == ANY_I2C_BUS) {
1267 dev_dbg(&adapter->dev, "found probe parameter for "
1268 "adapter %d, addr 0x%02x\n", adap_id,
1269 address_data->probe[i + 1]);
1270 temp_client->addr = address_data->probe[i + 1];
1271 err = i2c_detect_address(temp_client, -1, driver);
1272 if (err)
1273 goto exit_free;
1274 }
1275 }
1276
1277 /* Normal entries are done last, unless shadowed by an ignore entry */
1278 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1279 int j, ignore;
1280
1281 ignore = 0;
1282 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1283 j += 2) {
1284 if ((address_data->ignore[j] == adap_id ||
1285 address_data->ignore[j] == ANY_I2C_BUS)
1286 && address_data->ignore[j + 1]
1287 == address_data->normal_i2c[i]) {
1288 dev_dbg(&adapter->dev, "found ignore "
1289 "parameter for adapter %d, "
1290 "addr 0x%02x\n", adap_id,
1291 address_data->ignore[j + 1]);
1292 ignore = 1;
1293 break;
1294 }
1295 }
1296 if (ignore)
1297 continue;
1298
1299 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1300 "addr 0x%02x\n", adap_id,
1301 address_data->normal_i2c[i]);
1302 temp_client->addr = address_data->normal_i2c[i];
1303 err = i2c_detect_address(temp_client, -1, driver);
1304 if (err)
1305 goto exit_free;
1306 }
1307
1308 exit_free:
1309 kfree(temp_client);
1310 return err;
1311}
1312
Jean Delvare12b5053a2007-05-01 23:26:31 +02001313struct i2c_client *
1314i2c_new_probed_device(struct i2c_adapter *adap,
1315 struct i2c_board_info *info,
1316 unsigned short const *addr_list)
1317{
1318 int i;
1319
1320 /* Stop here if the bus doesn't support probing */
1321 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1322 dev_err(&adap->dev, "Probing not supported\n");
1323 return NULL;
1324 }
1325
Jean Delvare12b5053a2007-05-01 23:26:31 +02001326 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1327 /* Check address validity */
1328 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1329 dev_warn(&adap->dev, "Invalid 7-bit address "
1330 "0x%02x\n", addr_list[i]);
1331 continue;
1332 }
1333
1334 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001335 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001336 dev_dbg(&adap->dev, "Address 0x%02x already in "
1337 "use, not probing\n", addr_list[i]);
1338 continue;
1339 }
1340
1341 /* Test address responsiveness
1342 The default probe method is a quick write, but it is known
1343 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1344 and could also irreversibly write-protect some EEPROMs, so
1345 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1346 read instead. Also, some bus drivers don't implement
1347 quick write, so we fallback to a byte read it that case
1348 too. */
1349 if ((addr_list[i] & ~0x07) == 0x30
1350 || (addr_list[i] & ~0x0f) == 0x50
1351 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001352 union i2c_smbus_data data;
1353
Jean Delvare12b5053a2007-05-01 23:26:31 +02001354 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1355 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001356 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001357 break;
1358 } else {
1359 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1360 I2C_SMBUS_WRITE, 0,
1361 I2C_SMBUS_QUICK, NULL) >= 0)
1362 break;
1363 }
1364 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001365
1366 if (addr_list[i] == I2C_CLIENT_END) {
1367 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1368 return NULL;
1369 }
1370
1371 info->addr = addr_list[i];
1372 return i2c_new_device(adap, info);
1373}
1374EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376struct i2c_adapter* i2c_get_adapter(int id)
1377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001379
Jean Delvarecaada322008-01-27 18:14:49 +01001380 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001381 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001382 if (adapter && !try_module_get(adapter->owner))
1383 adapter = NULL;
1384
Jean Delvarecaada322008-01-27 18:14:49 +01001385 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001386 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
David Brownellc0564602007-05-01 23:26:31 +02001388EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390void i2c_put_adapter(struct i2c_adapter *adap)
1391{
1392 module_put(adap->owner);
1393}
David Brownellc0564602007-05-01 23:26:31 +02001394EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396/* The SMBus parts */
1397
David Brownell438d6c22006-12-10 21:21:31 +01001398#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001399static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001404 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 data = data ^ POLY;
1406 data = data << 1;
1407 }
1408 return (u8)(data >> 8);
1409}
1410
Jean Delvare421ef472005-10-26 21:28:55 +02001411/* Incremental CRC8 over count bytes in the array pointed to by p */
1412static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 int i;
1415
1416 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001417 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return crc;
1419}
1420
Jean Delvare421ef472005-10-26 21:28:55 +02001421/* Assume a 7-bit address, which is reasonable for SMBus */
1422static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Jean Delvare421ef472005-10-26 21:28:55 +02001424 /* The address will be sent first */
1425 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1426 pec = i2c_smbus_pec(pec, &addr, 1);
1427
1428 /* The data buffer follows */
1429 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Jean Delvare421ef472005-10-26 21:28:55 +02001432/* Used for write only transactions */
1433static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Jean Delvare421ef472005-10-26 21:28:55 +02001435 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1436 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
Jean Delvare421ef472005-10-26 21:28:55 +02001439/* Return <0 on CRC error
1440 If there was a write before this read (most cases) we need to take the
1441 partial CRC from the write part into account.
1442 Note that this function does modify the message (we need to decrease the
1443 message length to hide the CRC byte from the caller). */
1444static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Jean Delvare421ef472005-10-26 21:28:55 +02001446 u8 rpec = msg->buf[--msg->len];
1447 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (rpec != cpec) {
1450 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1451 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001452 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
David Brownell438d6c22006-12-10 21:21:31 +01001454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
David Brownella1cdeda2008-07-14 22:38:24 +02001457/**
1458 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1459 * @client: Handle to slave device
1460 *
1461 * This executes the SMBus "receive byte" protocol, returning negative errno
1462 * else the byte received from the device.
1463 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464s32 i2c_smbus_read_byte(struct i2c_client *client)
1465{
1466 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001467 int status;
1468
1469 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1470 I2C_SMBUS_READ, 0,
1471 I2C_SMBUS_BYTE, &data);
1472 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
David Brownellc0564602007-05-01 23:26:31 +02001474EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
David Brownella1cdeda2008-07-14 22:38:24 +02001476/**
1477 * i2c_smbus_write_byte - SMBus "send byte" protocol
1478 * @client: Handle to slave device
1479 * @value: Byte to be sent
1480 *
1481 * This executes the SMBus "send byte" protocol, returning negative errno
1482 * else zero on success.
1483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1485{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001487 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
David Brownellc0564602007-05-01 23:26:31 +02001489EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
David Brownella1cdeda2008-07-14 22:38:24 +02001491/**
1492 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1493 * @client: Handle to slave device
1494 * @command: Byte interpreted by slave
1495 *
1496 * This executes the SMBus "read byte" protocol, returning negative errno
1497 * else a data byte received from the device.
1498 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1500{
1501 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001502 int status;
1503
1504 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1505 I2C_SMBUS_READ, command,
1506 I2C_SMBUS_BYTE_DATA, &data);
1507 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
David Brownellc0564602007-05-01 23:26:31 +02001509EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
David Brownella1cdeda2008-07-14 22:38:24 +02001511/**
1512 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1513 * @client: Handle to slave device
1514 * @command: Byte interpreted by slave
1515 * @value: Byte being written
1516 *
1517 * This executes the SMBus "write byte" protocol, returning negative errno
1518 * else zero on success.
1519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1521{
1522 union i2c_smbus_data data;
1523 data.byte = value;
1524 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1525 I2C_SMBUS_WRITE,command,
1526 I2C_SMBUS_BYTE_DATA,&data);
1527}
David Brownellc0564602007-05-01 23:26:31 +02001528EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
David Brownella1cdeda2008-07-14 22:38:24 +02001530/**
1531 * i2c_smbus_read_word_data - SMBus "read word" protocol
1532 * @client: Handle to slave device
1533 * @command: Byte interpreted by slave
1534 *
1535 * This executes the SMBus "read word" protocol, returning negative errno
1536 * else a 16-bit unsigned "word" received from the device.
1537 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1539{
1540 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001541 int status;
1542
1543 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1544 I2C_SMBUS_READ, command,
1545 I2C_SMBUS_WORD_DATA, &data);
1546 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
David Brownellc0564602007-05-01 23:26:31 +02001548EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
David Brownella1cdeda2008-07-14 22:38:24 +02001550/**
1551 * i2c_smbus_write_word_data - SMBus "write word" protocol
1552 * @client: Handle to slave device
1553 * @command: Byte interpreted by slave
1554 * @value: 16-bit "word" being written
1555 *
1556 * This executes the SMBus "write word" protocol, returning negative errno
1557 * else zero on success.
1558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1560{
1561 union i2c_smbus_data data;
1562 data.word = value;
1563 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1564 I2C_SMBUS_WRITE,command,
1565 I2C_SMBUS_WORD_DATA,&data);
1566}
David Brownellc0564602007-05-01 23:26:31 +02001567EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
David Brownella64ec072007-10-13 23:56:31 +02001569/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001570 * i2c_smbus_process_call - SMBus "process call" protocol
1571 * @client: Handle to slave device
1572 * @command: Byte interpreted by slave
1573 * @value: 16-bit "word" being written
1574 *
1575 * This executes the SMBus "process call" protocol, returning negative errno
1576 * else a 16-bit unsigned "word" received from the device.
1577 */
1578s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1579{
1580 union i2c_smbus_data data;
1581 int status;
1582 data.word = value;
1583
1584 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1585 I2C_SMBUS_WRITE, command,
1586 I2C_SMBUS_PROC_CALL, &data);
1587 return (status < 0) ? status : data.word;
1588}
1589EXPORT_SYMBOL(i2c_smbus_process_call);
1590
1591/**
David Brownella1cdeda2008-07-14 22:38:24 +02001592 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001593 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001594 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001595 * @values: Byte array into which data will be read; big enough to hold
1596 * the data returned by the slave. SMBus allows at most 32 bytes.
1597 *
David Brownella1cdeda2008-07-14 22:38:24 +02001598 * This executes the SMBus "block read" protocol, returning negative errno
1599 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001600 *
1601 * Note that using this function requires that the client's adapter support
1602 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1603 * support this; its emulation through I2C messaging relies on a specific
1604 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1605 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001606s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1607 u8 *values)
1608{
1609 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001610 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001611
David Brownell24a5bb72008-07-14 22:38:23 +02001612 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1613 I2C_SMBUS_READ, command,
1614 I2C_SMBUS_BLOCK_DATA, &data);
1615 if (status)
1616 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001617
1618 memcpy(values, &data.block[1], data.block[0]);
1619 return data.block[0];
1620}
1621EXPORT_SYMBOL(i2c_smbus_read_block_data);
1622
David Brownella1cdeda2008-07-14 22:38:24 +02001623/**
1624 * i2c_smbus_write_block_data - SMBus "block write" protocol
1625 * @client: Handle to slave device
1626 * @command: Byte interpreted by slave
1627 * @length: Size of data block; SMBus allows at most 32 bytes
1628 * @values: Byte array which will be written.
1629 *
1630 * This executes the SMBus "block write" protocol, returning negative errno
1631 * else zero on success.
1632 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001634 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (length > I2C_SMBUS_BLOCK_MAX)
1639 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001641 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1643 I2C_SMBUS_WRITE,command,
1644 I2C_SMBUS_BLOCK_DATA,&data);
1645}
David Brownellc0564602007-05-01 23:26:31 +02001646EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001649s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1650 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001653 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001654
Jean Delvare4b2643d2007-07-12 14:12:29 +02001655 if (length > I2C_SMBUS_BLOCK_MAX)
1656 length = I2C_SMBUS_BLOCK_MAX;
1657 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001658 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1659 I2C_SMBUS_READ, command,
1660 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1661 if (status < 0)
1662 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001663
1664 memcpy(values, &data.block[1], data.block[0]);
1665 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
David Brownellc0564602007-05-01 23:26:31 +02001667EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Jean Delvare21bbd692006-01-09 15:19:18 +11001669s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001670 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001671{
1672 union i2c_smbus_data data;
1673
1674 if (length > I2C_SMBUS_BLOCK_MAX)
1675 length = I2C_SMBUS_BLOCK_MAX;
1676 data.block[0] = length;
1677 memcpy(data.block + 1, values, length);
1678 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1679 I2C_SMBUS_WRITE, command,
1680 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1681}
David Brownellc0564602007-05-01 23:26:31 +02001682EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001683
David Brownell438d6c22006-12-10 21:21:31 +01001684/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001686static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001688 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 union i2c_smbus_data * data)
1690{
1691 /* So we need to generate a series of msgs. In the case of writing, we
1692 need to use only one message; when reading, we need two. We initialize
1693 most things with sane defaults, to keep the code below somewhat
1694 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001695 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1696 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001698 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1700 };
1701 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001702 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001703 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 msgbuf0[0] = command;
1706 switch(size) {
1707 case I2C_SMBUS_QUICK:
1708 msg[0].len = 0;
1709 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001710 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1711 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 num = 1;
1713 break;
1714 case I2C_SMBUS_BYTE:
1715 if (read_write == I2C_SMBUS_READ) {
1716 /* Special case: only a read! */
1717 msg[0].flags = I2C_M_RD | flags;
1718 num = 1;
1719 }
1720 break;
1721 case I2C_SMBUS_BYTE_DATA:
1722 if (read_write == I2C_SMBUS_READ)
1723 msg[1].len = 1;
1724 else {
1725 msg[0].len = 2;
1726 msgbuf0[1] = data->byte;
1727 }
1728 break;
1729 case I2C_SMBUS_WORD_DATA:
1730 if (read_write == I2C_SMBUS_READ)
1731 msg[1].len = 2;
1732 else {
1733 msg[0].len=3;
1734 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001735 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
1737 break;
1738 case I2C_SMBUS_PROC_CALL:
1739 num = 2; /* Special case */
1740 read_write = I2C_SMBUS_READ;
1741 msg[0].len = 3;
1742 msg[1].len = 2;
1743 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001744 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 break;
1746 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001748 msg[1].flags |= I2C_M_RECV_LEN;
1749 msg[1].len = 1; /* block length will be added by
1750 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 } else {
1752 msg[0].len = data->block[0] + 2;
1753 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001754 dev_err(&adapter->dev,
1755 "Invalid block write size %d\n",
1756 data->block[0]);
1757 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001759 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 msgbuf0[i] = data->block[i-1];
1761 }
1762 break;
1763 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001764 num = 2; /* Another special case */
1765 read_write = I2C_SMBUS_READ;
1766 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001767 dev_err(&adapter->dev,
1768 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001769 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001770 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001771 }
1772 msg[0].len = data->block[0] + 2;
1773 for (i = 1; i < msg[0].len; i++)
1774 msgbuf0[i] = data->block[i-1];
1775 msg[1].flags |= I2C_M_RECV_LEN;
1776 msg[1].len = 1; /* block length will be added by
1777 the underlying bus driver */
1778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 case I2C_SMBUS_I2C_BLOCK_DATA:
1780 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001781 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 } else {
1783 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001784 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001785 dev_err(&adapter->dev,
1786 "Invalid block write size %d\n",
1787 data->block[0]);
1788 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790 for (i = 1; i <= data->block[0]; i++)
1791 msgbuf0[i] = data->block[i];
1792 }
1793 break;
1794 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001795 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1796 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798
Jean Delvare421ef472005-10-26 21:28:55 +02001799 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1800 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1801 if (i) {
1802 /* Compute PEC if first message is a write */
1803 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001804 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001805 i2c_smbus_add_pec(&msg[0]);
1806 else /* Write followed by read */
1807 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1808 }
1809 /* Ask for PEC if last message is a read */
1810 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001811 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001812 }
1813
David Brownell24a5bb72008-07-14 22:38:23 +02001814 status = i2c_transfer(adapter, msg, num);
1815 if (status < 0)
1816 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Jean Delvare421ef472005-10-26 21:28:55 +02001818 /* Check PEC if last message is a read */
1819 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001820 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1821 if (status < 0)
1822 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001823 }
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (read_write == I2C_SMBUS_READ)
1826 switch(size) {
1827 case I2C_SMBUS_BYTE:
1828 data->byte = msgbuf0[0];
1829 break;
1830 case I2C_SMBUS_BYTE_DATA:
1831 data->byte = msgbuf1[0];
1832 break;
David Brownell438d6c22006-12-10 21:21:31 +01001833 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 case I2C_SMBUS_PROC_CALL:
1835 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1836 break;
1837 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001838 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 data->block[i+1] = msgbuf1[i];
1840 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001841 case I2C_SMBUS_BLOCK_DATA:
1842 case I2C_SMBUS_BLOCK_PROC_CALL:
1843 for (i = 0; i < msgbuf1[0] + 1; i++)
1844 data->block[i] = msgbuf1[i];
1845 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847 return 0;
1848}
1849
David Brownella1cdeda2008-07-14 22:38:24 +02001850/**
1851 * i2c_smbus_xfer - execute SMBus protocol operations
1852 * @adapter: Handle to I2C bus
1853 * @addr: Address of SMBus slave on that bus
1854 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1855 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1856 * @command: Byte interpreted by slave, for protocols which use such bytes
1857 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1858 * @data: Data to be read or written
1859 *
1860 * This executes an SMBus protocol operation, and returns a negative
1861 * errno code else zero on success.
1862 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001863s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001864 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001865 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001867 unsigned long orig_jiffies;
1868 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001874 mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001875
1876 /* Retry automatically on arbitration loss */
1877 orig_jiffies = jiffies;
1878 for (res = 0, try = 0; try <= adapter->retries; try++) {
1879 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1880 read_write, command,
1881 protocol, data);
1882 if (res != -EAGAIN)
1883 break;
1884 if (time_after(jiffies,
1885 orig_jiffies + adapter->timeout))
1886 break;
1887 }
Ingo Molnar5c085d32006-01-18 23:16:04 +01001888 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 } else
1890 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001891 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return res;
1894}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1898MODULE_DESCRIPTION("I2C-Bus main module");
1899MODULE_LICENSE("GPL");