blob: b9306b1a6baa0e0f30d5777e3eb5782020aa0560 [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>
Mark Brown6de468a2010-03-02 12:23:46 +010037#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39
David Brownell9c1600e2007-05-01 23:26:31 +020040#include "i2c-core.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jean Delvare99cd8e22009-06-19 16:58:20 +020043/* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020044 that device detection, deletion of detected devices, and attach_adapter
45 and detach_adapter calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010046static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static DEFINE_IDR(i2c_adapter_idr);
Jean Delvare99cd8e22009-06-19 16:58:20 +020048static LIST_HEAD(userspace_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Jean Delvare4f8cf822009-09-18 22:45:46 +020050static struct device_type i2c_client_type;
Jean Delvaref8a227e2009-06-19 16:58:18 +020051static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
Jean Delvare4735c982008-07-14 22:38:36 +020052static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010053
54/* ------------------------------------------------------------------------- */
55
Jean Delvared2653e92008-04-29 23:11:39 +020056static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
57 const struct i2c_client *client)
58{
59 while (id->name[0]) {
60 if (strcmp(client->name, id->name) == 0)
61 return id;
62 id++;
63 }
64 return NULL;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int i2c_device_match(struct device *dev, struct device_driver *drv)
68{
Jean Delvare51298d12009-09-18 22:45:45 +020069 struct i2c_client *client = i2c_verify_client(dev);
70 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020071
Jean Delvare51298d12009-09-18 22:45:45 +020072 if (!client)
73 return 0;
74
75 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020076 /* match on an id table if there is one */
77 if (driver->id_table)
78 return i2c_match_id(driver->id_table, client) != NULL;
79
Jean Delvareeb8a7902008-05-18 20:49:41 +020080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
David Brownell7b4fbc52007-05-01 23:26:30 +020083#ifdef CONFIG_HOTPLUG
84
85/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020086static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020087{
88 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020089
Jean Delvareeb8a7902008-05-18 20:49:41 +020090 if (add_uevent_var(env, "MODALIAS=%s%s",
91 I2C_MODULE_PREFIX, client->name))
92 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020093 dev_dbg(dev, "uevent\n");
94 return 0;
95}
96
97#else
98#define i2c_device_uevent NULL
99#endif /* CONFIG_HOTPLUG */
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int i2c_device_probe(struct device *dev)
102{
Jean Delvare51298d12009-09-18 22:45:45 +0200103 struct i2c_client *client = i2c_verify_client(dev);
104 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100105 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200106
Jean Delvare51298d12009-09-18 22:45:45 +0200107 if (!client)
108 return 0;
109
110 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200111 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200112 return -ENODEV;
113 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200114 if (!device_can_wakeup(&client->dev))
115 device_init_wakeup(&client->dev,
116 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200117 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200118
Jean Delvaree0457442008-07-14 22:38:30 +0200119 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200120 if (status) {
Hans Verkuil50c33042008-03-12 14:15:00 +0100121 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200122 i2c_set_clientdata(client, NULL);
123 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100124 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127static int i2c_device_remove(struct device *dev)
128{
Jean Delvare51298d12009-09-18 22:45:45 +0200129 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200130 struct i2c_driver *driver;
131 int status;
132
Jean Delvare51298d12009-09-18 22:45:45 +0200133 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200134 return 0;
135
136 driver = to_i2c_driver(dev->driver);
137 if (driver->remove) {
138 dev_dbg(dev, "remove\n");
139 status = driver->remove(client);
140 } else {
141 dev->driver = NULL;
142 status = 0;
143 }
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200144 if (status == 0) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200145 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200146 i2c_set_clientdata(client, NULL);
147 }
David Brownella1d9e6e2007-05-01 23:26:30 +0200148 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
David Brownellf37dd802007-02-13 22:09:00 +0100151static void i2c_device_shutdown(struct device *dev)
152{
Jean Delvare51298d12009-09-18 22:45:45 +0200153 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100154 struct i2c_driver *driver;
155
Jean Delvare51298d12009-09-18 22:45:45 +0200156 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100157 return;
158 driver = to_i2c_driver(dev->driver);
159 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200160 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100161}
162
sonic zhang54067ee2009-12-14 21:17:30 +0100163#ifdef CONFIG_SUSPEND
164static int i2c_device_pm_suspend(struct device *dev)
165{
166 const struct dev_pm_ops *pm;
167
168 if (!dev->driver)
169 return 0;
170 pm = dev->driver->pm;
171 if (!pm || !pm->suspend)
172 return 0;
173 return pm->suspend(dev);
174}
175
176static int i2c_device_pm_resume(struct device *dev)
177{
178 const struct dev_pm_ops *pm;
179
180 if (!dev->driver)
181 return 0;
182 pm = dev->driver->pm;
183 if (!pm || !pm->resume)
184 return 0;
185 return pm->resume(dev);
186}
187#else
188#define i2c_device_pm_suspend NULL
189#define i2c_device_pm_resume NULL
190#endif
191
Mark Brown6de468a2010-03-02 12:23:46 +0100192#ifdef CONFIG_PM_RUNTIME
193static int i2c_device_runtime_suspend(struct device *dev)
194{
195 const struct dev_pm_ops *pm;
196
197 if (!dev->driver)
198 return 0;
199 pm = dev->driver->pm;
200 if (!pm || !pm->runtime_suspend)
201 return 0;
202 return pm->runtime_suspend(dev);
203}
204
205static int i2c_device_runtime_resume(struct device *dev)
206{
207 const struct dev_pm_ops *pm;
208
209 if (!dev->driver)
210 return 0;
211 pm = dev->driver->pm;
212 if (!pm || !pm->runtime_resume)
213 return 0;
214 return pm->runtime_resume(dev);
215}
216
217static int i2c_device_runtime_idle(struct device *dev)
218{
219 const struct dev_pm_ops *pm = NULL;
220 int ret;
221
222 if (dev->driver)
223 pm = dev->driver->pm;
224 if (pm && pm->runtime_idle) {
225 ret = pm->runtime_idle(dev);
226 if (ret)
227 return ret;
228 }
229
230 return pm_runtime_suspend(dev);
231}
232#else
233#define i2c_device_runtime_suspend NULL
234#define i2c_device_runtime_resume NULL
235#define i2c_device_runtime_idle NULL
236#endif
237
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100238static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100239{
Jean Delvare51298d12009-09-18 22:45:45 +0200240 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100241 struct i2c_driver *driver;
242
Jean Delvare51298d12009-09-18 22:45:45 +0200243 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100244 return 0;
245 driver = to_i2c_driver(dev->driver);
246 if (!driver->suspend)
247 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200248 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100249}
250
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100251static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100252{
Jean Delvare51298d12009-09-18 22:45:45 +0200253 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100254 struct i2c_driver *driver;
255
Jean Delvare51298d12009-09-18 22:45:45 +0200256 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100257 return 0;
258 driver = to_i2c_driver(dev->driver);
259 if (!driver->resume)
260 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200261 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100262}
263
David Brownell9c1600e2007-05-01 23:26:31 +0200264static void i2c_client_dev_release(struct device *dev)
265{
266 kfree(to_i2c_client(dev));
267}
268
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100269static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200270show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200271{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200272 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
273 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200274}
275
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100276static ssize_t
277show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200278{
279 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200280 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200281}
282
Jean Delvare4f8cf822009-09-18 22:45:46 +0200283static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200284static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
285
286static struct attribute *i2c_dev_attrs[] = {
287 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200288 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200289 &dev_attr_modalias.attr,
290 NULL
291};
292
293static struct attribute_group i2c_dev_attr_group = {
294 .attrs = i2c_dev_attrs,
295};
296
297static const struct attribute_group *i2c_dev_attr_groups[] = {
298 &i2c_dev_attr_group,
299 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200300};
301
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100302static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100303 .suspend = i2c_device_pm_suspend,
304 .resume = i2c_device_pm_resume,
Mark Brown6de468a2010-03-02 12:23:46 +0100305 .runtime_suspend = i2c_device_runtime_suspend,
306 .runtime_resume = i2c_device_runtime_resume,
307 .runtime_idle = i2c_device_runtime_idle,
sonic zhang54067ee2009-12-14 21:17:30 +0100308};
309
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200310struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100311 .name = "i2c",
312 .match = i2c_device_match,
313 .probe = i2c_device_probe,
314 .remove = i2c_device_remove,
315 .shutdown = i2c_device_shutdown,
316 .suspend = i2c_device_suspend,
317 .resume = i2c_device_resume,
sonic zhang54067ee2009-12-14 21:17:30 +0100318 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000319};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200320EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000321
Jean Delvare51298d12009-09-18 22:45:45 +0200322static struct device_type i2c_client_type = {
323 .groups = i2c_dev_attr_groups,
324 .uevent = i2c_device_uevent,
325 .release = i2c_client_dev_release,
326};
327
David Brownell9b766b82008-01-27 18:14:51 +0100328
329/**
330 * i2c_verify_client - return parameter as i2c_client, or NULL
331 * @dev: device, probably from some driver model iterator
332 *
333 * When traversing the driver model tree, perhaps using driver model
334 * iterators like @device_for_each_child(), you can't assume very much
335 * about the nodes you find. Use this function to avoid oopses caused
336 * by wrongly treating some non-I2C device as an i2c_client.
337 */
338struct i2c_client *i2c_verify_client(struct device *dev)
339{
Jean Delvare51298d12009-09-18 22:45:45 +0200340 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100341 ? to_i2c_client(dev)
342 : NULL;
343}
344EXPORT_SYMBOL(i2c_verify_client);
345
346
David Brownell9c1600e2007-05-01 23:26:31 +0200347/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200348 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200349 * @adap: the adapter managing the device
350 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200351 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200352 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200353 * Create an i2c device. Binding is handled through driver model
354 * probe()/remove() methods. A driver may be bound to this device when we
355 * return from this function, or any later moment (e.g. maybe hotplugging will
356 * load the driver module). This call is not appropriate for use by mainboard
357 * initialization logic, which usually runs during an arch_initcall() long
358 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200359 *
360 * This returns the new i2c client, which may be saved for later use with
361 * i2c_unregister_device(); or NULL to indicate an error.
362 */
363struct i2c_client *
364i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
365{
366 struct i2c_client *client;
367 int status;
368
369 client = kzalloc(sizeof *client, GFP_KERNEL);
370 if (!client)
371 return NULL;
372
373 client->adapter = adap;
374
375 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200376
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200377 if (info->archdata)
378 client->dev.archdata = *info->archdata;
379
Marc Pignatee354252008-08-28 08:33:22 +0200380 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200381 client->addr = info->addr;
382 client->irq = info->irq;
383
David Brownell9c1600e2007-05-01 23:26:31 +0200384 strlcpy(client->name, info->type, sizeof(client->name));
385
Jean Delvaref8a227e2009-06-19 16:58:18 +0200386 /* Check for address business */
387 status = i2c_check_addr(adap, client->addr);
388 if (status)
389 goto out_err;
390
391 client->dev.parent = &client->adapter->dev;
392 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200393 client->dev.type = &i2c_client_type;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200394
395 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
396 client->addr);
397 status = device_register(&client->dev);
398 if (status)
399 goto out_err;
400
Jean Delvaref8a227e2009-06-19 16:58:18 +0200401 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
402 client->name, dev_name(&client->dev));
403
David Brownell9c1600e2007-05-01 23:26:31 +0200404 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200405
406out_err:
407 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
408 "(%d)\n", client->name, client->addr, status);
409 kfree(client);
410 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200411}
412EXPORT_SYMBOL_GPL(i2c_new_device);
413
414
415/**
416 * i2c_unregister_device - reverse effect of i2c_new_device()
417 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200418 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200419 */
420void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200421{
David Brownella1d9e6e2007-05-01 23:26:30 +0200422 device_unregister(&client->dev);
423}
David Brownell9c1600e2007-05-01 23:26:31 +0200424EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200425
426
Jean Delvare60b129d2008-05-11 20:37:06 +0200427static const struct i2c_device_id dummy_id[] = {
428 { "dummy", 0 },
429 { },
430};
431
Jean Delvared2653e92008-04-29 23:11:39 +0200432static int dummy_probe(struct i2c_client *client,
433 const struct i2c_device_id *id)
434{
435 return 0;
436}
437
438static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100439{
440 return 0;
441}
442
443static struct i2c_driver dummy_driver = {
444 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200445 .probe = dummy_probe,
446 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200447 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100448};
449
450/**
451 * i2c_new_dummy - return a new i2c device bound to a dummy driver
452 * @adapter: the adapter managing the device
453 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100454 * Context: can sleep
455 *
456 * This returns an I2C client bound to the "dummy" driver, intended for use
457 * with devices that consume multiple addresses. Examples of such chips
458 * include various EEPROMS (like 24c04 and 24c08 models).
459 *
460 * These dummy devices have two main uses. First, most I2C and SMBus calls
461 * except i2c_transfer() need a client handle; the dummy will be that handle.
462 * And second, this prevents the specified address from being bound to a
463 * different driver.
464 *
465 * This returns the new i2c client, which should be saved for later use with
466 * i2c_unregister_device(); or NULL to indicate an error.
467 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100468struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100469{
470 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200471 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100472 };
473
David Brownelle9f13732008-01-27 18:14:52 +0100474 return i2c_new_device(adapter, &info);
475}
476EXPORT_SYMBOL_GPL(i2c_new_dummy);
477
David Brownellf37dd802007-02-13 22:09:00 +0100478/* ------------------------------------------------------------------------- */
479
David Brownell16ffadf2007-05-01 23:26:28 +0200480/* I2C bus adapters -- one roots each I2C or SMBUS segment */
481
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200482static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
David Brownellef2c83212007-05-01 23:26:28 +0200484 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 complete(&adap->dev_released);
486}
487
Jean Delvare99cd8e22009-06-19 16:58:20 +0200488/*
489 * Let users instantiate I2C devices through sysfs. This can be used when
490 * platform initialization code doesn't contain the proper data for
491 * whatever reason. Also useful for drivers that do device detection and
492 * detection fails, either because the device uses an unexpected address,
493 * or this is a compatible device with different ID register values.
494 *
495 * Parameter checking may look overzealous, but we really don't want
496 * the user to provide incorrect parameters.
497 */
498static ssize_t
499i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
500 const char *buf, size_t count)
501{
502 struct i2c_adapter *adap = to_i2c_adapter(dev);
503 struct i2c_board_info info;
504 struct i2c_client *client;
505 char *blank, end;
506 int res;
507
508 dev_warn(dev, "The new_device interface is still experimental "
509 "and may change in a near future\n");
510 memset(&info, 0, sizeof(struct i2c_board_info));
511
512 blank = strchr(buf, ' ');
513 if (!blank) {
514 dev_err(dev, "%s: Missing parameters\n", "new_device");
515 return -EINVAL;
516 }
517 if (blank - buf > I2C_NAME_SIZE - 1) {
518 dev_err(dev, "%s: Invalid device name\n", "new_device");
519 return -EINVAL;
520 }
521 memcpy(info.type, buf, blank - buf);
522
523 /* Parse remaining parameters, reject extra parameters */
524 res = sscanf(++blank, "%hi%c", &info.addr, &end);
525 if (res < 1) {
526 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
527 return -EINVAL;
528 }
529 if (res > 1 && end != '\n') {
530 dev_err(dev, "%s: Extra parameters\n", "new_device");
531 return -EINVAL;
532 }
533
534 if (info.addr < 0x03 || info.addr > 0x77) {
535 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
536 info.addr);
537 return -EINVAL;
538 }
539
540 client = i2c_new_device(adap, &info);
541 if (!client)
542 return -EEXIST;
543
544 /* Keep track of the added device */
545 mutex_lock(&core_lock);
546 list_add_tail(&client->detected, &userspace_devices);
547 mutex_unlock(&core_lock);
548 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
549 info.type, info.addr);
550
551 return count;
552}
553
554/*
555 * And of course let the users delete the devices they instantiated, if
556 * they got it wrong. This interface can only be used to delete devices
557 * instantiated by i2c_sysfs_new_device above. This guarantees that we
558 * don't delete devices to which some kernel code still has references.
559 *
560 * Parameter checking may look overzealous, but we really don't want
561 * the user to delete the wrong device.
562 */
563static ssize_t
564i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
565 const char *buf, size_t count)
566{
567 struct i2c_adapter *adap = to_i2c_adapter(dev);
568 struct i2c_client *client, *next;
569 unsigned short addr;
570 char end;
571 int res;
572
573 /* Parse parameters, reject extra parameters */
574 res = sscanf(buf, "%hi%c", &addr, &end);
575 if (res < 1) {
576 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
577 return -EINVAL;
578 }
579 if (res > 1 && end != '\n') {
580 dev_err(dev, "%s: Extra parameters\n", "delete_device");
581 return -EINVAL;
582 }
583
584 /* Make sure the device was added through sysfs */
585 res = -ENOENT;
586 mutex_lock(&core_lock);
587 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
588 if (client->addr == addr && client->adapter == adap) {
589 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
590 "delete_device", client->name, client->addr);
591
592 list_del(&client->detected);
593 i2c_unregister_device(client);
594 res = count;
595 break;
596 }
597 }
598 mutex_unlock(&core_lock);
599
600 if (res < 0)
601 dev_err(dev, "%s: Can't find device in list\n",
602 "delete_device");
603 return res;
604}
605
Jean Delvare4f8cf822009-09-18 22:45:46 +0200606static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
607static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
608
609static struct attribute *i2c_adapter_attrs[] = {
610 &dev_attr_name.attr,
611 &dev_attr_new_device.attr,
612 &dev_attr_delete_device.attr,
613 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200614};
615
Jean Delvare4f8cf822009-09-18 22:45:46 +0200616static struct attribute_group i2c_adapter_attr_group = {
617 .attrs = i2c_adapter_attrs,
618};
619
620static const struct attribute_group *i2c_adapter_attr_groups[] = {
621 &i2c_adapter_attr_group,
622 NULL
623};
624
625static struct device_type i2c_adapter_type = {
626 .groups = i2c_adapter_attr_groups,
627 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200628};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Jean Delvare2bb50952009-09-18 22:45:46 +0200630#ifdef CONFIG_I2C_COMPAT
631static struct class_compat *i2c_adapter_compat_class;
632#endif
633
David Brownell9c1600e2007-05-01 23:26:31 +0200634static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
635{
636 struct i2c_devinfo *devinfo;
637
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200638 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200639 list_for_each_entry(devinfo, &__i2c_board_list, list) {
640 if (devinfo->busnum == adapter->nr
641 && !i2c_new_device(adapter,
642 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100643 dev_err(&adapter->dev,
644 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200645 devinfo->board_info.addr);
646 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200647 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200648}
649
Jean Delvare69b00892009-12-06 17:06:27 +0100650static int i2c_do_add_adapter(struct i2c_driver *driver,
651 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100652{
Jean Delvare4735c982008-07-14 22:38:36 +0200653 /* Detect supported devices on that bus, and instantiate them */
654 i2c_detect(adap, driver);
655
656 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100657 if (driver->attach_adapter) {
658 /* We ignore the return code; if it fails, too bad */
659 driver->attach_adapter(adap);
660 }
661 return 0;
662}
663
Jean Delvare69b00892009-12-06 17:06:27 +0100664static int __process_new_adapter(struct device_driver *d, void *data)
665{
666 return i2c_do_add_adapter(to_i2c_driver(d), data);
667}
668
David Brownell6e13e642007-05-01 23:26:31 +0200669static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Jean Delvare026526f2008-01-27 18:14:49 +0100671 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
David Brownell1d0b19c2008-10-14 17:30:05 +0200673 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200674 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
675 res = -EAGAIN;
676 goto out_list;
677 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200678
Mika Kuoppala194684e2009-12-06 17:06:22 +0100679 rt_mutex_init(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Jean Delvare8fcfef62009-03-28 21:34:43 +0100681 /* Set default timeout to 1 second if not already set */
682 if (adap->timeout == 0)
683 adap->timeout = HZ;
684
Kay Sievers27d9c182009-01-07 14:29:16 +0100685 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200686 adap->dev.bus = &i2c_bus_type;
687 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200688 res = device_register(&adap->dev);
689 if (res)
690 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200692 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
693
Jean Delvare2bb50952009-09-18 22:45:46 +0200694#ifdef CONFIG_I2C_COMPAT
695 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
696 adap->dev.parent);
697 if (res)
698 dev_warn(&adap->dev,
699 "Failed to create compatibility class link\n");
700#endif
701
Jean Delvare729d6dd2009-06-19 16:58:18 +0200702 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200703 if (adap->nr < __i2c_first_dynamic_bus_num)
704 i2c_scan_static_board_info(adap);
705
Jean Delvare4735c982008-07-14 22:38:36 +0200706 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200707 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100708 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100709 __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100710 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200711
712 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200713
Jean Delvareb119c6c2006-08-15 18:26:30 +0200714out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200715 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200716 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200717 mutex_unlock(&core_lock);
718 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
David Brownell6e13e642007-05-01 23:26:31 +0200721/**
722 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
723 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200724 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200725 *
726 * This routine is used to declare an I2C adapter when its bus number
727 * doesn't matter. Examples: for I2C adapters dynamically added by
728 * USB links or PCI plugin cards.
729 *
730 * When this returns zero, a new bus number was allocated and stored
731 * in adap->nr, and the specified adapter became available for clients.
732 * Otherwise, a negative errno value is returned.
733 */
734int i2c_add_adapter(struct i2c_adapter *adapter)
735{
736 int id, res = 0;
737
738retry:
739 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
740 return -ENOMEM;
741
Jean Delvarecaada322008-01-27 18:14:49 +0100742 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200743 /* "above" here means "above or equal to", sigh */
744 res = idr_get_new_above(&i2c_adapter_idr, adapter,
745 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100746 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200747
748 if (res < 0) {
749 if (res == -EAGAIN)
750 goto retry;
751 return res;
752 }
753
754 adapter->nr = id;
755 return i2c_register_adapter(adapter);
756}
757EXPORT_SYMBOL(i2c_add_adapter);
758
759/**
760 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
761 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200762 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200763 *
764 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100765 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
766 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200767 * is used to properly configure I2C devices.
768 *
769 * If no devices have pre-been declared for this bus, then be sure to
770 * register the adapter before any dynamically allocated ones. Otherwise
771 * the required bus ID may not be available.
772 *
773 * When this returns zero, the specified adapter became available for
774 * clients using the bus number provided in adap->nr. Also, the table
775 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
776 * and the appropriate driver model device nodes are created. Otherwise, a
777 * negative errno value is returned.
778 */
779int i2c_add_numbered_adapter(struct i2c_adapter *adap)
780{
781 int id;
782 int status;
783
784 if (adap->nr & ~MAX_ID_MASK)
785 return -EINVAL;
786
787retry:
788 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
789 return -ENOMEM;
790
Jean Delvarecaada322008-01-27 18:14:49 +0100791 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200792 /* "above" here means "above or equal to", sigh;
793 * we need the "equal to" result to force the result
794 */
795 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
796 if (status == 0 && id != adap->nr) {
797 status = -EBUSY;
798 idr_remove(&i2c_adapter_idr, id);
799 }
Jean Delvarecaada322008-01-27 18:14:49 +0100800 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200801 if (status == -EAGAIN)
802 goto retry;
803
804 if (status == 0)
805 status = i2c_register_adapter(adap);
806 return status;
807}
808EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
809
Jean Delvare69b00892009-12-06 17:06:27 +0100810static int i2c_do_del_adapter(struct i2c_driver *driver,
811 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +0100812{
Jean Delvare4735c982008-07-14 22:38:36 +0200813 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100814 int res;
815
Jean Delvareacec2112009-03-28 21:34:40 +0100816 /* Remove the devices we created ourselves as the result of hardware
817 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200818 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
819 if (client->adapter == adapter) {
820 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
821 client->name, client->addr);
822 list_del(&client->detected);
823 i2c_unregister_device(client);
824 }
825 }
826
Jean Delvare026526f2008-01-27 18:14:49 +0100827 if (!driver->detach_adapter)
828 return 0;
829 res = driver->detach_adapter(adapter);
830 if (res)
831 dev_err(&adapter->dev, "detach_adapter failed (%d) "
832 "for driver [%s]\n", res, driver->driver.name);
833 return res;
834}
835
Jean Delvaree549c2b2009-06-19 16:58:19 +0200836static int __unregister_client(struct device *dev, void *dummy)
837{
838 struct i2c_client *client = i2c_verify_client(dev);
839 if (client)
840 i2c_unregister_device(client);
841 return 0;
842}
843
Jean Delvare69b00892009-12-06 17:06:27 +0100844static int __process_removed_adapter(struct device_driver *d, void *data)
845{
846 return i2c_do_del_adapter(to_i2c_driver(d), data);
847}
848
David Brownelld64f73b2007-07-12 14:12:28 +0200849/**
850 * i2c_del_adapter - unregister I2C adapter
851 * @adap: the adapter being unregistered
852 * Context: can sleep
853 *
854 * This unregisters an I2C adapter which was previously registered
855 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857int i2c_del_adapter(struct i2c_adapter *adap)
858{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200860 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100861 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200864 mutex_lock(&core_lock);
865 found = idr_find(&i2c_adapter_idr, adap->nr);
866 mutex_unlock(&core_lock);
867 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200868 pr_debug("i2c-core: attempting to delete unregistered "
869 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200870 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
Jean Delvare026526f2008-01-27 18:14:49 +0100873 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200874 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100875 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100876 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200877 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100878 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200879 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100881 /* Remove devices instantiated from sysfs */
882 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
883 if (client->adapter == adap) {
884 dev_dbg(&adap->dev, "Removing %s at 0x%x\n",
885 client->name, client->addr);
886 list_del(&client->detected);
887 i2c_unregister_device(client);
888 }
889 }
890
Jean Delvaree549c2b2009-06-19 16:58:19 +0200891 /* Detach any active clients. This can't fail, thus we do not
892 checking the returned value. */
893 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Jean Delvare2bb50952009-09-18 22:45:46 +0200895#ifdef CONFIG_I2C_COMPAT
896 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
897 adap->dev.parent);
898#endif
899
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +0100900 /* device name is gone after device_unregister */
901 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /* clean up the sysfs representation */
904 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 /* wait for sysfs to drop all references */
908 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
David Brownell6e13e642007-05-01 23:26:31 +0200910 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200911 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200913 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200915 /* Clear the device structure in case this adapter is ever going to be
916 added again */
917 memset(&adap->dev, 0, sizeof(adap->dev));
918
Jean Delvare35fc37f2009-06-19 16:58:19 +0200919 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
David Brownellc0564602007-05-01 23:26:31 +0200921EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923
David Brownell7b4fbc52007-05-01 23:26:30 +0200924/* ------------------------------------------------------------------------- */
925
Jean Delvare69b00892009-12-06 17:06:27 +0100926static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +0200927{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200928 if (dev->type != &i2c_adapter_type)
929 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +0100930 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +0200931}
932
David Brownell7b4fbc52007-05-01 23:26:30 +0200933/*
934 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200935 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 */
937
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800938int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100940 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
David Brownell1d0b19c2008-10-14 17:30:05 +0200942 /* Can't register until after driver model init */
943 if (unlikely(WARN_ON(!i2c_bus_type.p)))
944 return -EAGAIN;
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800947 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Jean Delvare729d6dd2009-06-19 16:58:18 +0200950 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200951 * will have called probe() for all matching-but-unbound devices.
952 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 res = driver_register(&driver->driver);
954 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100955 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100956
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100957 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Jean Delvare4735c982008-07-14 22:38:36 +0200959 INIT_LIST_HEAD(&driver->clients);
960 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200961 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +0100962 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
Jean Delvarecaada322008-01-27 18:14:49 +0100963 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200964
Jean Delvare7eebcb72006-02-05 23:28:21 +0100965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800967EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Jean Delvare69b00892009-12-06 17:06:27 +0100969static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +0200970{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200971 if (dev->type != &i2c_adapter_type)
972 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +0100973 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +0200974}
975
David Brownella1d9e6e2007-05-01 23:26:30 +0200976/**
977 * i2c_del_driver - unregister I2C driver
978 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200979 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200980 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200981void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Jean Delvarecaada322008-01-27 18:14:49 +0100983 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +0100984 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200985 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +0200986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100988 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
David Brownellc0564602007-05-01 23:26:31 +0200990EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
David Brownell7b4fbc52007-05-01 23:26:30 +0200992/* ------------------------------------------------------------------------- */
993
David Brownell9b766b82008-01-27 18:14:51 +0100994static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
David Brownell9b766b82008-01-27 18:14:51 +0100996 struct i2c_client *client = i2c_verify_client(dev);
997 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
David Brownell9b766b82008-01-27 18:14:51 +0100999 if (client && client->addr == addr)
1000 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return 0;
1002}
1003
Jean Delvare5e31c2b2007-11-15 19:24:02 +01001004static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
David Brownell9b766b82008-01-27 18:14:51 +01001006 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Jean Delvaree48d3312008-01-27 18:14:48 +01001009/**
1010 * i2c_use_client - increments the reference count of the i2c client structure
1011 * @client: the client being referenced
1012 *
1013 * Each live reference to a client should be refcounted. The driver model does
1014 * that automatically as part of driver binding, so that most drivers don't
1015 * need to do this explicitly: they hold a reference until they're unbound
1016 * from the device.
1017 *
1018 * A pointer to the client with the incremented reference counter is returned.
1019 */
1020struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
David Brownell6ea438e2008-07-14 22:38:24 +02001022 if (client && get_device(&client->dev))
1023 return client;
1024 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
David Brownellc0564602007-05-01 23:26:31 +02001026EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Jean Delvaree48d3312008-01-27 18:14:48 +01001028/**
1029 * i2c_release_client - release a use of the i2c client structure
1030 * @client: the client being no longer referenced
1031 *
1032 * Must be called when a user of a client is finished with it.
1033 */
1034void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
David Brownell6ea438e2008-07-14 22:38:24 +02001036 if (client)
1037 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
David Brownellc0564602007-05-01 23:26:31 +02001039EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
David Brownell9b766b82008-01-27 18:14:51 +01001041struct i2c_cmd_arg {
1042 unsigned cmd;
1043 void *arg;
1044};
1045
1046static int i2c_cmd(struct device *dev, void *_arg)
1047{
1048 struct i2c_client *client = i2c_verify_client(dev);
1049 struct i2c_cmd_arg *arg = _arg;
1050
1051 if (client && client->driver && client->driver->command)
1052 client->driver->command(client, arg->cmd, arg->arg);
1053 return 0;
1054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1057{
David Brownell9b766b82008-01-27 18:14:51 +01001058 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
David Brownell9b766b82008-01-27 18:14:51 +01001060 cmd_arg.cmd = cmd;
1061 cmd_arg.arg = arg;
1062 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
David Brownellc0564602007-05-01 23:26:31 +02001064EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066static int __init i2c_init(void)
1067{
1068 int retval;
1069
1070 retval = bus_register(&i2c_bus_type);
1071 if (retval)
1072 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001073#ifdef CONFIG_I2C_COMPAT
1074 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1075 if (!i2c_adapter_compat_class) {
1076 retval = -ENOMEM;
1077 goto bus_err;
1078 }
1079#endif
David Brownelle9f13732008-01-27 18:14:52 +01001080 retval = i2c_add_driver(&dummy_driver);
1081 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001082 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001083 return 0;
1084
Jean Delvare2bb50952009-09-18 22:45:46 +02001085class_err:
1086#ifdef CONFIG_I2C_COMPAT
1087 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001088bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001089#endif
David Brownelle9f13732008-01-27 18:14:52 +01001090 bus_unregister(&i2c_bus_type);
1091 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
1094static void __exit i2c_exit(void)
1095{
David Brownelle9f13732008-01-27 18:14:52 +01001096 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001097#ifdef CONFIG_I2C_COMPAT
1098 class_compat_unregister(i2c_adapter_compat_class);
1099#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 bus_unregister(&i2c_bus_type);
1101}
1102
David Brownella10f9e72008-10-14 17:30:06 +02001103/* We must initialize early, because some subsystems register i2c drivers
1104 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1105 */
1106postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107module_exit(i2c_exit);
1108
1109/* ----------------------------------------------------
1110 * the functional interface to the i2c busses.
1111 * ----------------------------------------------------
1112 */
1113
David Brownella1cdeda2008-07-14 22:38:24 +02001114/**
1115 * i2c_transfer - execute a single or combined I2C message
1116 * @adap: Handle to I2C bus
1117 * @msgs: One or more messages to execute before STOP is issued to
1118 * terminate the operation; each message begins with a START.
1119 * @num: Number of messages to be executed.
1120 *
1121 * Returns negative errno, else the number of messages executed.
1122 *
1123 * Note that there is no requirement that each message be sent to
1124 * the same slave address, although that is the most common model.
1125 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001126int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001128 unsigned long orig_jiffies;
1129 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
David Brownella1cdeda2008-07-14 22:38:24 +02001131 /* REVISIT the fault reporting model here is weak:
1132 *
1133 * - When we get an error after receiving N bytes from a slave,
1134 * there is no way to report "N".
1135 *
1136 * - When we get a NAK after transmitting N bytes to a slave,
1137 * there is no way to report "N" ... or to let the master
1138 * continue executing the rest of this combined message, if
1139 * that's the appropriate response.
1140 *
1141 * - When for example "num" is two and we successfully complete
1142 * the first message but get an error part way through the
1143 * second, it's unclear whether that should be reported as
1144 * one (discarding status on the second message) or errno
1145 * (discarding status on the first one).
1146 */
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (adap->algo->master_xfer) {
1149#ifdef DEBUG
1150 for (ret = 0; ret < num; ret++) {
1151 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001152 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1153 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1154 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156#endif
1157
Mike Rapoportcea443a2008-01-27 18:14:50 +01001158 if (in_atomic() || irqs_disabled()) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001159 ret = rt_mutex_trylock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001160 if (!ret)
1161 /* I2C activity is ongoing. */
1162 return -EAGAIN;
1163 } else {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001164 rt_mutex_lock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001165 }
1166
Clifford Wolf66b650f2009-06-15 18:01:46 +02001167 /* Retry automatically on arbitration loss */
1168 orig_jiffies = jiffies;
1169 for (ret = 0, try = 0; try <= adap->retries; try++) {
1170 ret = adap->algo->master_xfer(adap, msgs, num);
1171 if (ret != -EAGAIN)
1172 break;
1173 if (time_after(jiffies, orig_jiffies + adap->timeout))
1174 break;
1175 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001176 rt_mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 return ret;
1179 } else {
1180 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001181 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183}
David Brownellc0564602007-05-01 23:26:31 +02001184EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
David Brownella1cdeda2008-07-14 22:38:24 +02001186/**
1187 * i2c_master_send - issue a single I2C message in master transmit mode
1188 * @client: Handle to slave device
1189 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001190 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001191 *
1192 * Returns negative errno, or else the number of bytes written.
1193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1195{
1196 int ret;
1197 struct i2c_adapter *adap=client->adapter;
1198 struct i2c_msg msg;
1199
Jean Delvare815f55f2005-05-07 22:58:46 +02001200 msg.addr = client->addr;
1201 msg.flags = client->flags & I2C_M_TEN;
1202 msg.len = count;
1203 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001204
Jean Delvare815f55f2005-05-07 22:58:46 +02001205 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Jean Delvare815f55f2005-05-07 22:58:46 +02001207 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1208 transmitted, else error code. */
1209 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
David Brownellc0564602007-05-01 23:26:31 +02001211EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
David Brownella1cdeda2008-07-14 22:38:24 +02001213/**
1214 * i2c_master_recv - issue a single I2C message in master receive mode
1215 * @client: Handle to slave device
1216 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001217 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001218 *
1219 * Returns negative errno, or else the number of bytes read.
1220 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1222{
1223 struct i2c_adapter *adap=client->adapter;
1224 struct i2c_msg msg;
1225 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Jean Delvare815f55f2005-05-07 22:58:46 +02001227 msg.addr = client->addr;
1228 msg.flags = client->flags & I2C_M_TEN;
1229 msg.flags |= I2C_M_RD;
1230 msg.len = count;
1231 msg.buf = buf;
1232
1233 ret = i2c_transfer(adap, &msg, 1);
1234
1235 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1236 transmitted, else error code. */
1237 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
David Brownellc0564602007-05-01 23:26:31 +02001239EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/* ----------------------------------------------------
1242 * the i2c address scanning function
1243 * Will not work for 10-bit addresses!
1244 * ----------------------------------------------------
1245 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001246
Jean Delvareccfbbd02009-12-06 17:06:25 +01001247static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001248 struct i2c_driver *driver)
1249{
1250 struct i2c_board_info info;
1251 struct i2c_adapter *adapter = temp_client->adapter;
1252 int addr = temp_client->addr;
1253 int err;
1254
1255 /* Make sure the address is valid */
1256 if (addr < 0x03 || addr > 0x77) {
1257 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1258 addr);
1259 return -EINVAL;
1260 }
1261
1262 /* Skip if already in use */
1263 if (i2c_check_addr(adapter, addr))
1264 return 0;
1265
Jean Delvareccfbbd02009-12-06 17:06:25 +01001266 /* Make sure there is something at this address */
1267 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) < 0)
1268 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001269
Jean Delvareccfbbd02009-12-06 17:06:25 +01001270 /* Prevent 24RF08 corruption */
1271 if ((addr & ~0x0f) == 0x50)
1272 i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL);
Jean Delvare4735c982008-07-14 22:38:36 +02001273
1274 /* Finally call the custom detection function */
1275 memset(&info, 0, sizeof(struct i2c_board_info));
1276 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001277 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001278 if (err) {
1279 /* -ENODEV is returned if the detection fails. We catch it
1280 here as this isn't an error. */
1281 return err == -ENODEV ? 0 : err;
1282 }
1283
1284 /* Consistency check */
1285 if (info.type[0] == '\0') {
1286 dev_err(&adapter->dev, "%s detection function provided "
1287 "no name for 0x%x\n", driver->driver.name,
1288 addr);
1289 } else {
1290 struct i2c_client *client;
1291
1292 /* Detection succeeded, instantiate the device */
1293 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1294 info.type, info.addr);
1295 client = i2c_new_device(adapter, &info);
1296 if (client)
1297 list_add_tail(&client->detected, &driver->clients);
1298 else
1299 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1300 info.type, info.addr);
1301 }
1302 return 0;
1303}
1304
1305static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1306{
Jean Delvarec3813d62009-12-14 21:17:25 +01001307 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001308 struct i2c_client *temp_client;
1309 int i, err = 0;
1310 int adap_id = i2c_adapter_id(adapter);
1311
Jean Delvarec3813d62009-12-14 21:17:25 +01001312 address_list = driver->address_list;
1313 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001314 return 0;
1315
1316 /* Set up a temporary client to help detect callback */
1317 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1318 if (!temp_client)
1319 return -ENOMEM;
1320 temp_client->adapter = adapter;
1321
Jean Delvare4329cf82008-08-28 08:33:23 +02001322 /* Stop here if the classes do not match */
1323 if (!(adapter->class & driver->class))
1324 goto exit_free;
1325
Jean Delvare4735c982008-07-14 22:38:36 +02001326 /* Stop here if we can't use SMBUS_QUICK */
1327 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
Jean Delvarec3813d62009-12-14 21:17:25 +01001328 if (address_list[0] == I2C_CLIENT_END)
Jean Delvare4735c982008-07-14 22:38:36 +02001329 goto exit_free;
1330
1331 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1332 "can't probe for chips\n");
1333 err = -EOPNOTSUPP;
1334 goto exit_free;
1335 }
1336
Jean Delvarec3813d62009-12-14 21:17:25 +01001337 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001338 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001339 "addr 0x%02x\n", adap_id, address_list[i]);
1340 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001341 err = i2c_detect_address(temp_client, driver);
Jean Delvare4735c982008-07-14 22:38:36 +02001342 if (err)
1343 goto exit_free;
1344 }
1345
1346 exit_free:
1347 kfree(temp_client);
1348 return err;
1349}
1350
Jean Delvare12b5053a2007-05-01 23:26:31 +02001351struct i2c_client *
1352i2c_new_probed_device(struct i2c_adapter *adap,
1353 struct i2c_board_info *info,
1354 unsigned short const *addr_list)
1355{
1356 int i;
1357
1358 /* Stop here if the bus doesn't support probing */
1359 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1360 dev_err(&adap->dev, "Probing not supported\n");
1361 return NULL;
1362 }
1363
Jean Delvare12b5053a2007-05-01 23:26:31 +02001364 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1365 /* Check address validity */
1366 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1367 dev_warn(&adap->dev, "Invalid 7-bit address "
1368 "0x%02x\n", addr_list[i]);
1369 continue;
1370 }
1371
1372 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001373 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001374 dev_dbg(&adap->dev, "Address 0x%02x already in "
1375 "use, not probing\n", addr_list[i]);
1376 continue;
1377 }
1378
1379 /* Test address responsiveness
1380 The default probe method is a quick write, but it is known
1381 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1382 and could also irreversibly write-protect some EEPROMs, so
1383 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1384 read instead. Also, some bus drivers don't implement
1385 quick write, so we fallback to a byte read it that case
1386 too. */
1387 if ((addr_list[i] & ~0x07) == 0x30
1388 || (addr_list[i] & ~0x0f) == 0x50
1389 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001390 union i2c_smbus_data data;
1391
Jean Delvare12b5053a2007-05-01 23:26:31 +02001392 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1393 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001394 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001395 break;
1396 } else {
1397 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1398 I2C_SMBUS_WRITE, 0,
1399 I2C_SMBUS_QUICK, NULL) >= 0)
1400 break;
1401 }
1402 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001403
1404 if (addr_list[i] == I2C_CLIENT_END) {
1405 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1406 return NULL;
1407 }
1408
1409 info->addr = addr_list[i];
1410 return i2c_new_device(adap, info);
1411}
1412EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414struct i2c_adapter* i2c_get_adapter(int id)
1415{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001417
Jean Delvarecaada322008-01-27 18:14:49 +01001418 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001419 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001420 if (adapter && !try_module_get(adapter->owner))
1421 adapter = NULL;
1422
Jean Delvarecaada322008-01-27 18:14:49 +01001423 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001424 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
David Brownellc0564602007-05-01 23:26:31 +02001426EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428void i2c_put_adapter(struct i2c_adapter *adap)
1429{
1430 module_put(adap->owner);
1431}
David Brownellc0564602007-05-01 23:26:31 +02001432EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434/* The SMBus parts */
1435
David Brownell438d6c22006-12-10 21:21:31 +01001436#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001437static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001442 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 data = data ^ POLY;
1444 data = data << 1;
1445 }
1446 return (u8)(data >> 8);
1447}
1448
Jean Delvare421ef472005-10-26 21:28:55 +02001449/* Incremental CRC8 over count bytes in the array pointed to by p */
1450static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 int i;
1453
1454 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001455 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return crc;
1457}
1458
Jean Delvare421ef472005-10-26 21:28:55 +02001459/* Assume a 7-bit address, which is reasonable for SMBus */
1460static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Jean Delvare421ef472005-10-26 21:28:55 +02001462 /* The address will be sent first */
1463 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1464 pec = i2c_smbus_pec(pec, &addr, 1);
1465
1466 /* The data buffer follows */
1467 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Jean Delvare421ef472005-10-26 21:28:55 +02001470/* Used for write only transactions */
1471static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Jean Delvare421ef472005-10-26 21:28:55 +02001473 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1474 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Jean Delvare421ef472005-10-26 21:28:55 +02001477/* Return <0 on CRC error
1478 If there was a write before this read (most cases) we need to take the
1479 partial CRC from the write part into account.
1480 Note that this function does modify the message (we need to decrease the
1481 message length to hide the CRC byte from the caller). */
1482static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Jean Delvare421ef472005-10-26 21:28:55 +02001484 u8 rpec = msg->buf[--msg->len];
1485 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (rpec != cpec) {
1488 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1489 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001490 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
David Brownell438d6c22006-12-10 21:21:31 +01001492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
David Brownella1cdeda2008-07-14 22:38:24 +02001495/**
1496 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1497 * @client: Handle to slave device
1498 *
1499 * This executes the SMBus "receive byte" protocol, returning negative errno
1500 * else the byte received from the device.
1501 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502s32 i2c_smbus_read_byte(struct i2c_client *client)
1503{
1504 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001505 int status;
1506
1507 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1508 I2C_SMBUS_READ, 0,
1509 I2C_SMBUS_BYTE, &data);
1510 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
David Brownellc0564602007-05-01 23:26:31 +02001512EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
David Brownella1cdeda2008-07-14 22:38:24 +02001514/**
1515 * i2c_smbus_write_byte - SMBus "send byte" protocol
1516 * @client: Handle to slave device
1517 * @value: Byte to be sent
1518 *
1519 * This executes the SMBus "send byte" protocol, returning negative errno
1520 * else zero on success.
1521 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001525 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
David Brownellc0564602007-05-01 23:26:31 +02001527EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
David Brownella1cdeda2008-07-14 22:38:24 +02001529/**
1530 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1531 * @client: Handle to slave device
1532 * @command: Byte interpreted by slave
1533 *
1534 * This executes the SMBus "read byte" protocol, returning negative errno
1535 * else a data byte received from the device.
1536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1538{
1539 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001540 int status;
1541
1542 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1543 I2C_SMBUS_READ, command,
1544 I2C_SMBUS_BYTE_DATA, &data);
1545 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
David Brownellc0564602007-05-01 23:26:31 +02001547EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
David Brownella1cdeda2008-07-14 22:38:24 +02001549/**
1550 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1551 * @client: Handle to slave device
1552 * @command: Byte interpreted by slave
1553 * @value: Byte being written
1554 *
1555 * This executes the SMBus "write byte" protocol, returning negative errno
1556 * else zero on success.
1557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1559{
1560 union i2c_smbus_data data;
1561 data.byte = value;
1562 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1563 I2C_SMBUS_WRITE,command,
1564 I2C_SMBUS_BYTE_DATA,&data);
1565}
David Brownellc0564602007-05-01 23:26:31 +02001566EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
David Brownella1cdeda2008-07-14 22:38:24 +02001568/**
1569 * i2c_smbus_read_word_data - SMBus "read word" protocol
1570 * @client: Handle to slave device
1571 * @command: Byte interpreted by slave
1572 *
1573 * This executes the SMBus "read word" protocol, returning negative errno
1574 * else a 16-bit unsigned "word" received from the device.
1575 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1577{
1578 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001579 int status;
1580
1581 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1582 I2C_SMBUS_READ, command,
1583 I2C_SMBUS_WORD_DATA, &data);
1584 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
David Brownellc0564602007-05-01 23:26:31 +02001586EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
David Brownella1cdeda2008-07-14 22:38:24 +02001588/**
1589 * i2c_smbus_write_word_data - SMBus "write word" protocol
1590 * @client: Handle to slave device
1591 * @command: Byte interpreted by slave
1592 * @value: 16-bit "word" being written
1593 *
1594 * This executes the SMBus "write word" protocol, returning negative errno
1595 * else zero on success.
1596 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1598{
1599 union i2c_smbus_data data;
1600 data.word = value;
1601 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1602 I2C_SMBUS_WRITE,command,
1603 I2C_SMBUS_WORD_DATA,&data);
1604}
David Brownellc0564602007-05-01 23:26:31 +02001605EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
David Brownella64ec072007-10-13 23:56:31 +02001607/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001608 * i2c_smbus_process_call - SMBus "process call" protocol
1609 * @client: Handle to slave device
1610 * @command: Byte interpreted by slave
1611 * @value: 16-bit "word" being written
1612 *
1613 * This executes the SMBus "process call" protocol, returning negative errno
1614 * else a 16-bit unsigned "word" received from the device.
1615 */
1616s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1617{
1618 union i2c_smbus_data data;
1619 int status;
1620 data.word = value;
1621
1622 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1623 I2C_SMBUS_WRITE, command,
1624 I2C_SMBUS_PROC_CALL, &data);
1625 return (status < 0) ? status : data.word;
1626}
1627EXPORT_SYMBOL(i2c_smbus_process_call);
1628
1629/**
David Brownella1cdeda2008-07-14 22:38:24 +02001630 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001631 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001632 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001633 * @values: Byte array into which data will be read; big enough to hold
1634 * the data returned by the slave. SMBus allows at most 32 bytes.
1635 *
David Brownella1cdeda2008-07-14 22:38:24 +02001636 * This executes the SMBus "block read" protocol, returning negative errno
1637 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001638 *
1639 * Note that using this function requires that the client's adapter support
1640 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1641 * support this; its emulation through I2C messaging relies on a specific
1642 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1643 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001644s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1645 u8 *values)
1646{
1647 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001648 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001649
David Brownell24a5bb72008-07-14 22:38:23 +02001650 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1651 I2C_SMBUS_READ, command,
1652 I2C_SMBUS_BLOCK_DATA, &data);
1653 if (status)
1654 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001655
1656 memcpy(values, &data.block[1], data.block[0]);
1657 return data.block[0];
1658}
1659EXPORT_SYMBOL(i2c_smbus_read_block_data);
1660
David Brownella1cdeda2008-07-14 22:38:24 +02001661/**
1662 * i2c_smbus_write_block_data - SMBus "block write" protocol
1663 * @client: Handle to slave device
1664 * @command: Byte interpreted by slave
1665 * @length: Size of data block; SMBus allows at most 32 bytes
1666 * @values: Byte array which will be written.
1667 *
1668 * This executes the SMBus "block write" protocol, returning negative errno
1669 * else zero on success.
1670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001672 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (length > I2C_SMBUS_BLOCK_MAX)
1677 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001679 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1681 I2C_SMBUS_WRITE,command,
1682 I2C_SMBUS_BLOCK_DATA,&data);
1683}
David Brownellc0564602007-05-01 23:26:31 +02001684EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001687s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1688 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001691 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001692
Jean Delvare4b2643d2007-07-12 14:12:29 +02001693 if (length > I2C_SMBUS_BLOCK_MAX)
1694 length = I2C_SMBUS_BLOCK_MAX;
1695 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001696 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1697 I2C_SMBUS_READ, command,
1698 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1699 if (status < 0)
1700 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001701
1702 memcpy(values, &data.block[1], data.block[0]);
1703 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
David Brownellc0564602007-05-01 23:26:31 +02001705EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Jean Delvare21bbd692006-01-09 15:19:18 +11001707s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001708 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001709{
1710 union i2c_smbus_data data;
1711
1712 if (length > I2C_SMBUS_BLOCK_MAX)
1713 length = I2C_SMBUS_BLOCK_MAX;
1714 data.block[0] = length;
1715 memcpy(data.block + 1, values, length);
1716 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1717 I2C_SMBUS_WRITE, command,
1718 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1719}
David Brownellc0564602007-05-01 23:26:31 +02001720EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001721
David Brownell438d6c22006-12-10 21:21:31 +01001722/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001724static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001726 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 union i2c_smbus_data * data)
1728{
1729 /* So we need to generate a series of msgs. In the case of writing, we
1730 need to use only one message; when reading, we need two. We initialize
1731 most things with sane defaults, to keep the code below somewhat
1732 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001733 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1734 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001736 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1738 };
1739 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001740 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001741 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 msgbuf0[0] = command;
1744 switch(size) {
1745 case I2C_SMBUS_QUICK:
1746 msg[0].len = 0;
1747 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001748 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1749 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 num = 1;
1751 break;
1752 case I2C_SMBUS_BYTE:
1753 if (read_write == I2C_SMBUS_READ) {
1754 /* Special case: only a read! */
1755 msg[0].flags = I2C_M_RD | flags;
1756 num = 1;
1757 }
1758 break;
1759 case I2C_SMBUS_BYTE_DATA:
1760 if (read_write == I2C_SMBUS_READ)
1761 msg[1].len = 1;
1762 else {
1763 msg[0].len = 2;
1764 msgbuf0[1] = data->byte;
1765 }
1766 break;
1767 case I2C_SMBUS_WORD_DATA:
1768 if (read_write == I2C_SMBUS_READ)
1769 msg[1].len = 2;
1770 else {
1771 msg[0].len=3;
1772 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001773 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775 break;
1776 case I2C_SMBUS_PROC_CALL:
1777 num = 2; /* Special case */
1778 read_write = I2C_SMBUS_READ;
1779 msg[0].len = 3;
1780 msg[1].len = 2;
1781 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001782 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 break;
1784 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001786 msg[1].flags |= I2C_M_RECV_LEN;
1787 msg[1].len = 1; /* block length will be added by
1788 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 } else {
1790 msg[0].len = data->block[0] + 2;
1791 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001792 dev_err(&adapter->dev,
1793 "Invalid block write size %d\n",
1794 data->block[0]);
1795 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001797 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 msgbuf0[i] = data->block[i-1];
1799 }
1800 break;
1801 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001802 num = 2; /* Another special case */
1803 read_write = I2C_SMBUS_READ;
1804 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001805 dev_err(&adapter->dev,
1806 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001807 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001808 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001809 }
1810 msg[0].len = data->block[0] + 2;
1811 for (i = 1; i < msg[0].len; i++)
1812 msgbuf0[i] = data->block[i-1];
1813 msg[1].flags |= I2C_M_RECV_LEN;
1814 msg[1].len = 1; /* block length will be added by
1815 the underlying bus driver */
1816 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 case I2C_SMBUS_I2C_BLOCK_DATA:
1818 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001819 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 } else {
1821 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001822 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001823 dev_err(&adapter->dev,
1824 "Invalid block write size %d\n",
1825 data->block[0]);
1826 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
1828 for (i = 1; i <= data->block[0]; i++)
1829 msgbuf0[i] = data->block[i];
1830 }
1831 break;
1832 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001833 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1834 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
1836
Jean Delvare421ef472005-10-26 21:28:55 +02001837 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1838 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1839 if (i) {
1840 /* Compute PEC if first message is a write */
1841 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001842 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001843 i2c_smbus_add_pec(&msg[0]);
1844 else /* Write followed by read */
1845 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1846 }
1847 /* Ask for PEC if last message is a read */
1848 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001849 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001850 }
1851
David Brownell24a5bb72008-07-14 22:38:23 +02001852 status = i2c_transfer(adapter, msg, num);
1853 if (status < 0)
1854 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Jean Delvare421ef472005-10-26 21:28:55 +02001856 /* Check PEC if last message is a read */
1857 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001858 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1859 if (status < 0)
1860 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001861 }
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (read_write == I2C_SMBUS_READ)
1864 switch(size) {
1865 case I2C_SMBUS_BYTE:
1866 data->byte = msgbuf0[0];
1867 break;
1868 case I2C_SMBUS_BYTE_DATA:
1869 data->byte = msgbuf1[0];
1870 break;
David Brownell438d6c22006-12-10 21:21:31 +01001871 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 case I2C_SMBUS_PROC_CALL:
1873 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1874 break;
1875 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001876 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 data->block[i+1] = msgbuf1[i];
1878 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001879 case I2C_SMBUS_BLOCK_DATA:
1880 case I2C_SMBUS_BLOCK_PROC_CALL:
1881 for (i = 0; i < msgbuf1[0] + 1; i++)
1882 data->block[i] = msgbuf1[i];
1883 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885 return 0;
1886}
1887
David Brownella1cdeda2008-07-14 22:38:24 +02001888/**
1889 * i2c_smbus_xfer - execute SMBus protocol operations
1890 * @adapter: Handle to I2C bus
1891 * @addr: Address of SMBus slave on that bus
1892 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1893 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1894 * @command: Byte interpreted by slave, for protocols which use such bytes
1895 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1896 * @data: Data to be read or written
1897 *
1898 * This executes an SMBus protocol operation, and returns a negative
1899 * errno code else zero on success.
1900 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001901s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001902 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001903 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001905 unsigned long orig_jiffies;
1906 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 if (adapter->algo->smbus_xfer) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001912 rt_mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001913
1914 /* Retry automatically on arbitration loss */
1915 orig_jiffies = jiffies;
1916 for (res = 0, try = 0; try <= adapter->retries; try++) {
1917 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1918 read_write, command,
1919 protocol, data);
1920 if (res != -EAGAIN)
1921 break;
1922 if (time_after(jiffies,
1923 orig_jiffies + adapter->timeout))
1924 break;
1925 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001926 rt_mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 } else
1928 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001929 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return res;
1932}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1936MODULE_DESCRIPTION("I2C-Bus main module");
1937MODULE_LICENSE("GPL");