blob: 96f01331544d8992a6c06ff86fa7cc340800b023 [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 Delvare6629dcf2010-05-04 11:09:28 +020043/* core_lock protects i2c_adapter_idr, 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);
48
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));
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200119 if (status) {
Hans Verkuil50c33042008-03-12 14:15:00 +0100120 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200121 i2c_set_clientdata(client, NULL);
122 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100123 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static int i2c_device_remove(struct device *dev)
127{
Jean Delvare51298d12009-09-18 22:45:45 +0200128 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200129 struct i2c_driver *driver;
130 int status;
131
Jean Delvare51298d12009-09-18 22:45:45 +0200132 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200133 return 0;
134
135 driver = to_i2c_driver(dev->driver);
136 if (driver->remove) {
137 dev_dbg(dev, "remove\n");
138 status = driver->remove(client);
139 } else {
140 dev->driver = NULL;
141 status = 0;
142 }
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200143 if (status == 0) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200144 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200145 i2c_set_clientdata(client, NULL);
146 }
David Brownella1d9e6e2007-05-01 23:26:30 +0200147 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
David Brownellf37dd802007-02-13 22:09:00 +0100150static void i2c_device_shutdown(struct device *dev)
151{
Jean Delvare51298d12009-09-18 22:45:45 +0200152 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100153 struct i2c_driver *driver;
154
Jean Delvare51298d12009-09-18 22:45:45 +0200155 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100156 return;
157 driver = to_i2c_driver(dev->driver);
158 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200159 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100160}
161
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200162#ifdef CONFIG_PM_SLEEP
163static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100164{
Jean Delvare51298d12009-09-18 22:45:45 +0200165 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100166 struct i2c_driver *driver;
167
Jean Delvare51298d12009-09-18 22:45:45 +0200168 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100169 return 0;
170 driver = to_i2c_driver(dev->driver);
171 if (!driver->suspend)
172 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200173 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100174}
175
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200176static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100177{
Jean Delvare51298d12009-09-18 22:45:45 +0200178 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100179 struct i2c_driver *driver;
180
Jean Delvare51298d12009-09-18 22:45:45 +0200181 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100182 return 0;
183 driver = to_i2c_driver(dev->driver);
184 if (!driver->resume)
185 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200186 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100187}
188
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200189static int i2c_device_pm_suspend(struct device *dev)
190{
191 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
192
193 if (pm_runtime_suspended(dev))
194 return 0;
195
196 if (pm)
197 return pm->suspend ? pm->suspend(dev) : 0;
198
199 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
200}
201
202static int i2c_device_pm_resume(struct device *dev)
203{
204 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
205 int ret;
206
207 if (pm)
208 ret = pm->resume ? pm->resume(dev) : 0;
209 else
210 ret = i2c_legacy_resume(dev);
211
212 if (!ret) {
213 pm_runtime_disable(dev);
214 pm_runtime_set_active(dev);
215 pm_runtime_enable(dev);
216 }
217
218 return ret;
219}
220
221static int i2c_device_pm_freeze(struct device *dev)
222{
223 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
224
225 if (pm_runtime_suspended(dev))
226 return 0;
227
228 if (pm)
229 return pm->freeze ? pm->freeze(dev) : 0;
230
231 return i2c_legacy_suspend(dev, PMSG_FREEZE);
232}
233
234static int i2c_device_pm_thaw(struct device *dev)
235{
236 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
237
238 if (pm_runtime_suspended(dev))
239 return 0;
240
241 if (pm)
242 return pm->thaw ? pm->thaw(dev) : 0;
243
244 return i2c_legacy_resume(dev);
245}
246
247static int i2c_device_pm_poweroff(struct device *dev)
248{
249 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
250
251 if (pm_runtime_suspended(dev))
252 return 0;
253
254 if (pm)
255 return pm->poweroff ? pm->poweroff(dev) : 0;
256
257 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
258}
259
260static int i2c_device_pm_restore(struct device *dev)
261{
262 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
263 int ret;
264
265 if (pm)
266 ret = pm->restore ? pm->restore(dev) : 0;
267 else
268 ret = i2c_legacy_resume(dev);
269
270 if (!ret) {
271 pm_runtime_disable(dev);
272 pm_runtime_set_active(dev);
273 pm_runtime_enable(dev);
274 }
275
276 return ret;
277}
278#else /* !CONFIG_PM_SLEEP */
279#define i2c_device_pm_suspend NULL
280#define i2c_device_pm_resume NULL
281#define i2c_device_pm_freeze NULL
282#define i2c_device_pm_thaw NULL
283#define i2c_device_pm_poweroff NULL
284#define i2c_device_pm_restore NULL
285#endif /* !CONFIG_PM_SLEEP */
286
David Brownell9c1600e2007-05-01 23:26:31 +0200287static void i2c_client_dev_release(struct device *dev)
288{
289 kfree(to_i2c_client(dev));
290}
291
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100292static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200293show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200294{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200295 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
296 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200297}
298
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100299static ssize_t
300show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200301{
302 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200303 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200304}
305
Jean Delvare4f8cf822009-09-18 22:45:46 +0200306static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200307static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
308
309static struct attribute *i2c_dev_attrs[] = {
310 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200311 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200312 &dev_attr_modalias.attr,
313 NULL
314};
315
316static struct attribute_group i2c_dev_attr_group = {
317 .attrs = i2c_dev_attrs,
318};
319
320static const struct attribute_group *i2c_dev_attr_groups[] = {
321 &i2c_dev_attr_group,
322 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200323};
324
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100325static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100326 .suspend = i2c_device_pm_suspend,
327 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200328 .freeze = i2c_device_pm_freeze,
329 .thaw = i2c_device_pm_thaw,
330 .poweroff = i2c_device_pm_poweroff,
331 .restore = i2c_device_pm_restore,
332 SET_RUNTIME_PM_OPS(
333 pm_generic_runtime_suspend,
334 pm_generic_runtime_resume,
335 pm_generic_runtime_idle
336 )
sonic zhang54067ee2009-12-14 21:17:30 +0100337};
338
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200339struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100340 .name = "i2c",
341 .match = i2c_device_match,
342 .probe = i2c_device_probe,
343 .remove = i2c_device_remove,
344 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100345 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000346};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200347EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000348
Jean Delvare51298d12009-09-18 22:45:45 +0200349static struct device_type i2c_client_type = {
350 .groups = i2c_dev_attr_groups,
351 .uevent = i2c_device_uevent,
352 .release = i2c_client_dev_release,
353};
354
David Brownell9b766b82008-01-27 18:14:51 +0100355
356/**
357 * i2c_verify_client - return parameter as i2c_client, or NULL
358 * @dev: device, probably from some driver model iterator
359 *
360 * When traversing the driver model tree, perhaps using driver model
361 * iterators like @device_for_each_child(), you can't assume very much
362 * about the nodes you find. Use this function to avoid oopses caused
363 * by wrongly treating some non-I2C device as an i2c_client.
364 */
365struct i2c_client *i2c_verify_client(struct device *dev)
366{
Jean Delvare51298d12009-09-18 22:45:45 +0200367 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100368 ? to_i2c_client(dev)
369 : NULL;
370}
371EXPORT_SYMBOL(i2c_verify_client);
372
373
Jean Delvare3a89db52010-06-03 11:33:52 +0200374/* This is a permissive address validity check, I2C address map constraints
375 * are purposedly not enforced, except for the general call address. */
376static int i2c_check_client_addr_validity(const struct i2c_client *client)
377{
378 if (client->flags & I2C_CLIENT_TEN) {
379 /* 10-bit address, all values are valid */
380 if (client->addr > 0x3ff)
381 return -EINVAL;
382 } else {
383 /* 7-bit address, reject the general call address */
384 if (client->addr == 0x00 || client->addr > 0x7f)
385 return -EINVAL;
386 }
387 return 0;
388}
389
David Brownell9c1600e2007-05-01 23:26:31 +0200390/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200391 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200392 * @adap: the adapter managing the device
393 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200394 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200395 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200396 * Create an i2c device. Binding is handled through driver model
397 * probe()/remove() methods. A driver may be bound to this device when we
398 * return from this function, or any later moment (e.g. maybe hotplugging will
399 * load the driver module). This call is not appropriate for use by mainboard
400 * initialization logic, which usually runs during an arch_initcall() long
401 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200402 *
403 * This returns the new i2c client, which may be saved for later use with
404 * i2c_unregister_device(); or NULL to indicate an error.
405 */
406struct i2c_client *
407i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
408{
409 struct i2c_client *client;
410 int status;
411
412 client = kzalloc(sizeof *client, GFP_KERNEL);
413 if (!client)
414 return NULL;
415
416 client->adapter = adap;
417
418 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200419
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200420 if (info->archdata)
421 client->dev.archdata = *info->archdata;
422
Marc Pignatee354252008-08-28 08:33:22 +0200423 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200424 client->addr = info->addr;
425 client->irq = info->irq;
426
David Brownell9c1600e2007-05-01 23:26:31 +0200427 strlcpy(client->name, info->type, sizeof(client->name));
428
Jean Delvare3a89db52010-06-03 11:33:52 +0200429 /* Check for address validity */
430 status = i2c_check_client_addr_validity(client);
431 if (status) {
432 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
433 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
434 goto out_err_silent;
435 }
436
Jean Delvaref8a227e2009-06-19 16:58:18 +0200437 /* Check for address business */
438 status = i2c_check_addr(adap, client->addr);
439 if (status)
440 goto out_err;
441
442 client->dev.parent = &client->adapter->dev;
443 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200444 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700445#ifdef CONFIG_OF
446 client->dev.of_node = info->of_node;
447#endif
Jean Delvaref8a227e2009-06-19 16:58:18 +0200448
449 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
450 client->addr);
451 status = device_register(&client->dev);
452 if (status)
453 goto out_err;
454
Jean Delvaref8a227e2009-06-19 16:58:18 +0200455 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
456 client->name, dev_name(&client->dev));
457
David Brownell9c1600e2007-05-01 23:26:31 +0200458 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200459
460out_err:
461 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
462 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200463out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200464 kfree(client);
465 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200466}
467EXPORT_SYMBOL_GPL(i2c_new_device);
468
469
470/**
471 * i2c_unregister_device - reverse effect of i2c_new_device()
472 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200473 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200474 */
475void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200476{
David Brownella1d9e6e2007-05-01 23:26:30 +0200477 device_unregister(&client->dev);
478}
David Brownell9c1600e2007-05-01 23:26:31 +0200479EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200480
481
Jean Delvare60b129d2008-05-11 20:37:06 +0200482static const struct i2c_device_id dummy_id[] = {
483 { "dummy", 0 },
484 { },
485};
486
Jean Delvared2653e92008-04-29 23:11:39 +0200487static int dummy_probe(struct i2c_client *client,
488 const struct i2c_device_id *id)
489{
490 return 0;
491}
492
493static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100494{
495 return 0;
496}
497
498static struct i2c_driver dummy_driver = {
499 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200500 .probe = dummy_probe,
501 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200502 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100503};
504
505/**
506 * i2c_new_dummy - return a new i2c device bound to a dummy driver
507 * @adapter: the adapter managing the device
508 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100509 * Context: can sleep
510 *
511 * This returns an I2C client bound to the "dummy" driver, intended for use
512 * with devices that consume multiple addresses. Examples of such chips
513 * include various EEPROMS (like 24c04 and 24c08 models).
514 *
515 * These dummy devices have two main uses. First, most I2C and SMBus calls
516 * except i2c_transfer() need a client handle; the dummy will be that handle.
517 * And second, this prevents the specified address from being bound to a
518 * different driver.
519 *
520 * This returns the new i2c client, which should be saved for later use with
521 * i2c_unregister_device(); or NULL to indicate an error.
522 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100523struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100524{
525 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200526 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100527 };
528
David Brownelle9f13732008-01-27 18:14:52 +0100529 return i2c_new_device(adapter, &info);
530}
531EXPORT_SYMBOL_GPL(i2c_new_dummy);
532
David Brownellf37dd802007-02-13 22:09:00 +0100533/* ------------------------------------------------------------------------- */
534
David Brownell16ffadf2007-05-01 23:26:28 +0200535/* I2C bus adapters -- one roots each I2C or SMBUS segment */
536
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200537static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
David Brownellef2c83212007-05-01 23:26:28 +0200539 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 complete(&adap->dev_released);
541}
542
Jean Delvare99cd8e22009-06-19 16:58:20 +0200543/*
544 * Let users instantiate I2C devices through sysfs. This can be used when
545 * platform initialization code doesn't contain the proper data for
546 * whatever reason. Also useful for drivers that do device detection and
547 * detection fails, either because the device uses an unexpected address,
548 * or this is a compatible device with different ID register values.
549 *
550 * Parameter checking may look overzealous, but we really don't want
551 * the user to provide incorrect parameters.
552 */
553static ssize_t
554i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
555 const char *buf, size_t count)
556{
557 struct i2c_adapter *adap = to_i2c_adapter(dev);
558 struct i2c_board_info info;
559 struct i2c_client *client;
560 char *blank, end;
561 int res;
562
563 dev_warn(dev, "The new_device interface is still experimental "
564 "and may change in a near future\n");
565 memset(&info, 0, sizeof(struct i2c_board_info));
566
567 blank = strchr(buf, ' ');
568 if (!blank) {
569 dev_err(dev, "%s: Missing parameters\n", "new_device");
570 return -EINVAL;
571 }
572 if (blank - buf > I2C_NAME_SIZE - 1) {
573 dev_err(dev, "%s: Invalid device name\n", "new_device");
574 return -EINVAL;
575 }
576 memcpy(info.type, buf, blank - buf);
577
578 /* Parse remaining parameters, reject extra parameters */
579 res = sscanf(++blank, "%hi%c", &info.addr, &end);
580 if (res < 1) {
581 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
582 return -EINVAL;
583 }
584 if (res > 1 && end != '\n') {
585 dev_err(dev, "%s: Extra parameters\n", "new_device");
586 return -EINVAL;
587 }
588
Jean Delvare99cd8e22009-06-19 16:58:20 +0200589 client = i2c_new_device(adap, &info);
590 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200591 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200592
593 /* Keep track of the added device */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200594 i2c_lock_adapter(adap);
595 list_add_tail(&client->detected, &adap->userspace_clients);
596 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200597 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
598 info.type, info.addr);
599
600 return count;
601}
602
603/*
604 * And of course let the users delete the devices they instantiated, if
605 * they got it wrong. This interface can only be used to delete devices
606 * instantiated by i2c_sysfs_new_device above. This guarantees that we
607 * don't delete devices to which some kernel code still has references.
608 *
609 * Parameter checking may look overzealous, but we really don't want
610 * the user to delete the wrong device.
611 */
612static ssize_t
613i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
614 const char *buf, size_t count)
615{
616 struct i2c_adapter *adap = to_i2c_adapter(dev);
617 struct i2c_client *client, *next;
618 unsigned short addr;
619 char end;
620 int res;
621
622 /* Parse parameters, reject extra parameters */
623 res = sscanf(buf, "%hi%c", &addr, &end);
624 if (res < 1) {
625 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
626 return -EINVAL;
627 }
628 if (res > 1 && end != '\n') {
629 dev_err(dev, "%s: Extra parameters\n", "delete_device");
630 return -EINVAL;
631 }
632
633 /* Make sure the device was added through sysfs */
634 res = -ENOENT;
Jean Delvare6629dcf2010-05-04 11:09:28 +0200635 i2c_lock_adapter(adap);
636 list_for_each_entry_safe(client, next, &adap->userspace_clients,
637 detected) {
638 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200639 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
640 "delete_device", client->name, client->addr);
641
642 list_del(&client->detected);
643 i2c_unregister_device(client);
644 res = count;
645 break;
646 }
647 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200648 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200649
650 if (res < 0)
651 dev_err(dev, "%s: Can't find device in list\n",
652 "delete_device");
653 return res;
654}
655
Jean Delvare4f8cf822009-09-18 22:45:46 +0200656static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
657static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
658
659static struct attribute *i2c_adapter_attrs[] = {
660 &dev_attr_name.attr,
661 &dev_attr_new_device.attr,
662 &dev_attr_delete_device.attr,
663 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200664};
665
Jean Delvare4f8cf822009-09-18 22:45:46 +0200666static struct attribute_group i2c_adapter_attr_group = {
667 .attrs = i2c_adapter_attrs,
668};
669
670static const struct attribute_group *i2c_adapter_attr_groups[] = {
671 &i2c_adapter_attr_group,
672 NULL
673};
674
675static struct device_type i2c_adapter_type = {
676 .groups = i2c_adapter_attr_groups,
677 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200678};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Jean Delvare2bb50952009-09-18 22:45:46 +0200680#ifdef CONFIG_I2C_COMPAT
681static struct class_compat *i2c_adapter_compat_class;
682#endif
683
David Brownell9c1600e2007-05-01 23:26:31 +0200684static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
685{
686 struct i2c_devinfo *devinfo;
687
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200688 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200689 list_for_each_entry(devinfo, &__i2c_board_list, list) {
690 if (devinfo->busnum == adapter->nr
691 && !i2c_new_device(adapter,
692 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100693 dev_err(&adapter->dev,
694 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200695 devinfo->board_info.addr);
696 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200697 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200698}
699
Jean Delvare69b00892009-12-06 17:06:27 +0100700static int i2c_do_add_adapter(struct i2c_driver *driver,
701 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100702{
Jean Delvare4735c982008-07-14 22:38:36 +0200703 /* Detect supported devices on that bus, and instantiate them */
704 i2c_detect(adap, driver);
705
706 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100707 if (driver->attach_adapter) {
708 /* We ignore the return code; if it fails, too bad */
709 driver->attach_adapter(adap);
710 }
711 return 0;
712}
713
Jean Delvare69b00892009-12-06 17:06:27 +0100714static int __process_new_adapter(struct device_driver *d, void *data)
715{
716 return i2c_do_add_adapter(to_i2c_driver(d), data);
717}
718
David Brownell6e13e642007-05-01 23:26:31 +0200719static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Jean Delvare026526f2008-01-27 18:14:49 +0100721 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
David Brownell1d0b19c2008-10-14 17:30:05 +0200723 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200724 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
725 res = -EAGAIN;
726 goto out_list;
727 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200728
Mika Kuoppala194684e2009-12-06 17:06:22 +0100729 rt_mutex_init(&adap->bus_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200730 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Jean Delvare8fcfef62009-03-28 21:34:43 +0100732 /* Set default timeout to 1 second if not already set */
733 if (adap->timeout == 0)
734 adap->timeout = HZ;
735
Kay Sievers27d9c182009-01-07 14:29:16 +0100736 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200737 adap->dev.bus = &i2c_bus_type;
738 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200739 res = device_register(&adap->dev);
740 if (res)
741 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200743 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
744
Jean Delvare2bb50952009-09-18 22:45:46 +0200745#ifdef CONFIG_I2C_COMPAT
746 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
747 adap->dev.parent);
748 if (res)
749 dev_warn(&adap->dev,
750 "Failed to create compatibility class link\n");
751#endif
752
Jean Delvare729d6dd2009-06-19 16:58:18 +0200753 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200754 if (adap->nr < __i2c_first_dynamic_bus_num)
755 i2c_scan_static_board_info(adap);
756
Jean Delvare4735c982008-07-14 22:38:36 +0200757 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200758 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100759 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100760 __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100761 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200762
763 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200764
Jean Delvareb119c6c2006-08-15 18:26:30 +0200765out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200766 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200767 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200768 mutex_unlock(&core_lock);
769 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
David Brownell6e13e642007-05-01 23:26:31 +0200772/**
773 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
774 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200775 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200776 *
777 * This routine is used to declare an I2C adapter when its bus number
778 * doesn't matter. Examples: for I2C adapters dynamically added by
779 * USB links or PCI plugin cards.
780 *
781 * When this returns zero, a new bus number was allocated and stored
782 * in adap->nr, and the specified adapter became available for clients.
783 * Otherwise, a negative errno value is returned.
784 */
785int i2c_add_adapter(struct i2c_adapter *adapter)
786{
787 int id, res = 0;
788
789retry:
790 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
791 return -ENOMEM;
792
Jean Delvarecaada322008-01-27 18:14:49 +0100793 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200794 /* "above" here means "above or equal to", sigh */
795 res = idr_get_new_above(&i2c_adapter_idr, adapter,
796 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100797 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200798
799 if (res < 0) {
800 if (res == -EAGAIN)
801 goto retry;
802 return res;
803 }
804
805 adapter->nr = id;
806 return i2c_register_adapter(adapter);
807}
808EXPORT_SYMBOL(i2c_add_adapter);
809
810/**
811 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
812 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200813 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200814 *
815 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100816 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
817 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200818 * is used to properly configure I2C devices.
819 *
820 * If no devices have pre-been declared for this bus, then be sure to
821 * register the adapter before any dynamically allocated ones. Otherwise
822 * the required bus ID may not be available.
823 *
824 * When this returns zero, the specified adapter became available for
825 * clients using the bus number provided in adap->nr. Also, the table
826 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
827 * and the appropriate driver model device nodes are created. Otherwise, a
828 * negative errno value is returned.
829 */
830int i2c_add_numbered_adapter(struct i2c_adapter *adap)
831{
832 int id;
833 int status;
834
835 if (adap->nr & ~MAX_ID_MASK)
836 return -EINVAL;
837
838retry:
839 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
840 return -ENOMEM;
841
Jean Delvarecaada322008-01-27 18:14:49 +0100842 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200843 /* "above" here means "above or equal to", sigh;
844 * we need the "equal to" result to force the result
845 */
846 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
847 if (status == 0 && id != adap->nr) {
848 status = -EBUSY;
849 idr_remove(&i2c_adapter_idr, id);
850 }
Jean Delvarecaada322008-01-27 18:14:49 +0100851 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200852 if (status == -EAGAIN)
853 goto retry;
854
855 if (status == 0)
856 status = i2c_register_adapter(adap);
857 return status;
858}
859EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
860
Jean Delvare69b00892009-12-06 17:06:27 +0100861static int i2c_do_del_adapter(struct i2c_driver *driver,
862 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +0100863{
Jean Delvare4735c982008-07-14 22:38:36 +0200864 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100865 int res;
866
Jean Delvareacec2112009-03-28 21:34:40 +0100867 /* Remove the devices we created ourselves as the result of hardware
868 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200869 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
870 if (client->adapter == adapter) {
871 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
872 client->name, client->addr);
873 list_del(&client->detected);
874 i2c_unregister_device(client);
875 }
876 }
877
Jean Delvare026526f2008-01-27 18:14:49 +0100878 if (!driver->detach_adapter)
879 return 0;
880 res = driver->detach_adapter(adapter);
881 if (res)
882 dev_err(&adapter->dev, "detach_adapter failed (%d) "
883 "for driver [%s]\n", res, driver->driver.name);
884 return res;
885}
886
Jean Delvaree549c2b2009-06-19 16:58:19 +0200887static int __unregister_client(struct device *dev, void *dummy)
888{
889 struct i2c_client *client = i2c_verify_client(dev);
890 if (client)
891 i2c_unregister_device(client);
892 return 0;
893}
894
Jean Delvare69b00892009-12-06 17:06:27 +0100895static int __process_removed_adapter(struct device_driver *d, void *data)
896{
897 return i2c_do_del_adapter(to_i2c_driver(d), data);
898}
899
David Brownelld64f73b2007-07-12 14:12:28 +0200900/**
901 * i2c_del_adapter - unregister I2C adapter
902 * @adap: the adapter being unregistered
903 * Context: can sleep
904 *
905 * This unregisters an I2C adapter which was previously registered
906 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
907 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908int i2c_del_adapter(struct i2c_adapter *adap)
909{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200911 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100912 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200915 mutex_lock(&core_lock);
916 found = idr_find(&i2c_adapter_idr, adap->nr);
917 mutex_unlock(&core_lock);
918 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200919 pr_debug("i2c-core: attempting to delete unregistered "
920 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200921 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
Jean Delvare026526f2008-01-27 18:14:49 +0100924 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200925 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100926 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100927 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200928 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100929 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200930 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100932 /* Remove devices instantiated from sysfs */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200933 i2c_lock_adapter(adap);
934 list_for_each_entry_safe(client, next, &adap->userspace_clients,
935 detected) {
936 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
937 client->addr);
938 list_del(&client->detected);
939 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100940 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200941 i2c_unlock_adapter(adap);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100942
Jean Delvaree549c2b2009-06-19 16:58:19 +0200943 /* Detach any active clients. This can't fail, thus we do not
944 checking the returned value. */
945 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Jean Delvare2bb50952009-09-18 22:45:46 +0200947#ifdef CONFIG_I2C_COMPAT
948 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
949 adap->dev.parent);
950#endif
951
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +0100952 /* device name is gone after device_unregister */
953 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* clean up the sysfs representation */
956 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* wait for sysfs to drop all references */
960 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
David Brownell6e13e642007-05-01 23:26:31 +0200962 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200963 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200965 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200967 /* Clear the device structure in case this adapter is ever going to be
968 added again */
969 memset(&adap->dev, 0, sizeof(adap->dev));
970
Jean Delvare35fc37f2009-06-19 16:58:19 +0200971 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
David Brownellc0564602007-05-01 23:26:31 +0200973EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975
David Brownell7b4fbc52007-05-01 23:26:30 +0200976/* ------------------------------------------------------------------------- */
977
Jean Delvare69b00892009-12-06 17:06:27 +0100978static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +0200979{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200980 if (dev->type != &i2c_adapter_type)
981 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +0100982 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +0200983}
984
David Brownell7b4fbc52007-05-01 23:26:30 +0200985/*
986 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200987 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 */
989
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800990int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100992 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
David Brownell1d0b19c2008-10-14 17:30:05 +0200994 /* Can't register until after driver model init */
995 if (unlikely(WARN_ON(!i2c_bus_type.p)))
996 return -EAGAIN;
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800999 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Jean Delvare729d6dd2009-06-19 16:58:18 +02001002 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001003 * will have called probe() for all matching-but-unbound devices.
1004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 res = driver_register(&driver->driver);
1006 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001007 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001008
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001009 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Jean Delvare4735c982008-07-14 22:38:36 +02001011 INIT_LIST_HEAD(&driver->clients);
1012 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001013 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +01001014 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
Jean Delvarecaada322008-01-27 18:14:49 +01001015 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001016
Jean Delvare7eebcb72006-02-05 23:28:21 +01001017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001019EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Jean Delvare69b00892009-12-06 17:06:27 +01001021static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001022{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001023 if (dev->type != &i2c_adapter_type)
1024 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001025 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001026}
1027
David Brownella1d9e6e2007-05-01 23:26:30 +02001028/**
1029 * i2c_del_driver - unregister I2C driver
1030 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001031 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001032 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001033void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Jean Delvarecaada322008-01-27 18:14:49 +01001035 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +01001036 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001037 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +02001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001040 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
David Brownellc0564602007-05-01 23:26:31 +02001042EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
David Brownell7b4fbc52007-05-01 23:26:30 +02001044/* ------------------------------------------------------------------------- */
1045
David Brownell9b766b82008-01-27 18:14:51 +01001046static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
David Brownell9b766b82008-01-27 18:14:51 +01001048 struct i2c_client *client = i2c_verify_client(dev);
1049 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
David Brownell9b766b82008-01-27 18:14:51 +01001051 if (client && client->addr == addr)
1052 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return 0;
1054}
1055
Jean Delvare5e31c2b2007-11-15 19:24:02 +01001056static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
David Brownell9b766b82008-01-27 18:14:51 +01001058 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Jean Delvaree48d3312008-01-27 18:14:48 +01001061/**
1062 * i2c_use_client - increments the reference count of the i2c client structure
1063 * @client: the client being referenced
1064 *
1065 * Each live reference to a client should be refcounted. The driver model does
1066 * that automatically as part of driver binding, so that most drivers don't
1067 * need to do this explicitly: they hold a reference until they're unbound
1068 * from the device.
1069 *
1070 * A pointer to the client with the incremented reference counter is returned.
1071 */
1072struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
David Brownell6ea438e2008-07-14 22:38:24 +02001074 if (client && get_device(&client->dev))
1075 return client;
1076 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
David Brownellc0564602007-05-01 23:26:31 +02001078EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Jean Delvaree48d3312008-01-27 18:14:48 +01001080/**
1081 * i2c_release_client - release a use of the i2c client structure
1082 * @client: the client being no longer referenced
1083 *
1084 * Must be called when a user of a client is finished with it.
1085 */
1086void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
David Brownell6ea438e2008-07-14 22:38:24 +02001088 if (client)
1089 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
David Brownellc0564602007-05-01 23:26:31 +02001091EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
David Brownell9b766b82008-01-27 18:14:51 +01001093struct i2c_cmd_arg {
1094 unsigned cmd;
1095 void *arg;
1096};
1097
1098static int i2c_cmd(struct device *dev, void *_arg)
1099{
1100 struct i2c_client *client = i2c_verify_client(dev);
1101 struct i2c_cmd_arg *arg = _arg;
1102
1103 if (client && client->driver && client->driver->command)
1104 client->driver->command(client, arg->cmd, arg->arg);
1105 return 0;
1106}
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1109{
David Brownell9b766b82008-01-27 18:14:51 +01001110 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
David Brownell9b766b82008-01-27 18:14:51 +01001112 cmd_arg.cmd = cmd;
1113 cmd_arg.arg = arg;
1114 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
David Brownellc0564602007-05-01 23:26:31 +02001116EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118static int __init i2c_init(void)
1119{
1120 int retval;
1121
1122 retval = bus_register(&i2c_bus_type);
1123 if (retval)
1124 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001125#ifdef CONFIG_I2C_COMPAT
1126 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1127 if (!i2c_adapter_compat_class) {
1128 retval = -ENOMEM;
1129 goto bus_err;
1130 }
1131#endif
David Brownelle9f13732008-01-27 18:14:52 +01001132 retval = i2c_add_driver(&dummy_driver);
1133 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001134 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001135 return 0;
1136
Jean Delvare2bb50952009-09-18 22:45:46 +02001137class_err:
1138#ifdef CONFIG_I2C_COMPAT
1139 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001140bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001141#endif
David Brownelle9f13732008-01-27 18:14:52 +01001142 bus_unregister(&i2c_bus_type);
1143 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146static void __exit i2c_exit(void)
1147{
David Brownelle9f13732008-01-27 18:14:52 +01001148 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001149#ifdef CONFIG_I2C_COMPAT
1150 class_compat_unregister(i2c_adapter_compat_class);
1151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 bus_unregister(&i2c_bus_type);
1153}
1154
David Brownella10f9e72008-10-14 17:30:06 +02001155/* We must initialize early, because some subsystems register i2c drivers
1156 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1157 */
1158postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159module_exit(i2c_exit);
1160
1161/* ----------------------------------------------------
1162 * the functional interface to the i2c busses.
1163 * ----------------------------------------------------
1164 */
1165
David Brownella1cdeda2008-07-14 22:38:24 +02001166/**
1167 * i2c_transfer - execute a single or combined I2C message
1168 * @adap: Handle to I2C bus
1169 * @msgs: One or more messages to execute before STOP is issued to
1170 * terminate the operation; each message begins with a START.
1171 * @num: Number of messages to be executed.
1172 *
1173 * Returns negative errno, else the number of messages executed.
1174 *
1175 * Note that there is no requirement that each message be sent to
1176 * the same slave address, although that is the most common model.
1177 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001178int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001180 unsigned long orig_jiffies;
1181 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
David Brownella1cdeda2008-07-14 22:38:24 +02001183 /* REVISIT the fault reporting model here is weak:
1184 *
1185 * - When we get an error after receiving N bytes from a slave,
1186 * there is no way to report "N".
1187 *
1188 * - When we get a NAK after transmitting N bytes to a slave,
1189 * there is no way to report "N" ... or to let the master
1190 * continue executing the rest of this combined message, if
1191 * that's the appropriate response.
1192 *
1193 * - When for example "num" is two and we successfully complete
1194 * the first message but get an error part way through the
1195 * second, it's unclear whether that should be reported as
1196 * one (discarding status on the second message) or errno
1197 * (discarding status on the first one).
1198 */
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (adap->algo->master_xfer) {
1201#ifdef DEBUG
1202 for (ret = 0; ret < num; ret++) {
1203 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001204 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1205 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1206 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208#endif
1209
Mike Rapoportcea443a2008-01-27 18:14:50 +01001210 if (in_atomic() || irqs_disabled()) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001211 ret = rt_mutex_trylock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001212 if (!ret)
1213 /* I2C activity is ongoing. */
1214 return -EAGAIN;
1215 } else {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001216 rt_mutex_lock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001217 }
1218
Clifford Wolf66b650f2009-06-15 18:01:46 +02001219 /* Retry automatically on arbitration loss */
1220 orig_jiffies = jiffies;
1221 for (ret = 0, try = 0; try <= adap->retries; try++) {
1222 ret = adap->algo->master_xfer(adap, msgs, num);
1223 if (ret != -EAGAIN)
1224 break;
1225 if (time_after(jiffies, orig_jiffies + adap->timeout))
1226 break;
1227 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001228 rt_mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 return ret;
1231 } else {
1232 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001233 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235}
David Brownellc0564602007-05-01 23:26:31 +02001236EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
David Brownella1cdeda2008-07-14 22:38:24 +02001238/**
1239 * i2c_master_send - issue a single I2C message in master transmit mode
1240 * @client: Handle to slave device
1241 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001242 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001243 *
1244 * Returns negative errno, or else the number of bytes written.
1245 */
Farid Hammane7225acf2010-05-21 18:40:58 +02001246int i2c_master_send(struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001249 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 struct i2c_msg msg;
1251
Jean Delvare815f55f2005-05-07 22:58:46 +02001252 msg.addr = client->addr;
1253 msg.flags = client->flags & I2C_M_TEN;
1254 msg.len = count;
1255 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001256
Jean Delvare815f55f2005-05-07 22:58:46 +02001257 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Jean Delvare815f55f2005-05-07 22:58:46 +02001259 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1260 transmitted, else error code. */
1261 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
David Brownellc0564602007-05-01 23:26:31 +02001263EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
David Brownella1cdeda2008-07-14 22:38:24 +02001265/**
1266 * i2c_master_recv - issue a single I2C message in master receive mode
1267 * @client: Handle to slave device
1268 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001269 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001270 *
1271 * Returns negative errno, or else the number of bytes read.
1272 */
Farid Hammane7225acf2010-05-21 18:40:58 +02001273int i2c_master_recv(struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Farid Hammane7225acf2010-05-21 18:40:58 +02001275 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 struct i2c_msg msg;
1277 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Jean Delvare815f55f2005-05-07 22:58:46 +02001279 msg.addr = client->addr;
1280 msg.flags = client->flags & I2C_M_TEN;
1281 msg.flags |= I2C_M_RD;
1282 msg.len = count;
1283 msg.buf = buf;
1284
1285 ret = i2c_transfer(adap, &msg, 1);
1286
1287 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1288 transmitted, else error code. */
1289 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
David Brownellc0564602007-05-01 23:26:31 +02001291EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293/* ----------------------------------------------------
1294 * the i2c address scanning function
1295 * Will not work for 10-bit addresses!
1296 * ----------------------------------------------------
1297 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001298
Jean Delvare63e4e802010-06-03 11:33:51 +02001299/*
1300 * Legacy default probe function, mostly relevant for SMBus. The default
1301 * probe method is a quick write, but it is known to corrupt the 24RF08
1302 * EEPROMs due to a state machine bug, and could also irreversibly
1303 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1304 * we use a short byte read instead. Also, some bus drivers don't implement
1305 * quick write, so we fallback to a byte read in that case too.
1306 * On x86, there is another special case for FSC hardware monitoring chips,
1307 * which want regular byte reads (address 0x73.) Fortunately, these are the
1308 * only known chips using this I2C address on PC hardware.
1309 * Returns 1 if probe succeeded, 0 if not.
1310 */
1311static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1312{
1313 int err;
1314 union i2c_smbus_data dummy;
1315
1316#ifdef CONFIG_X86
1317 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1318 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1319 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1320 I2C_SMBUS_BYTE_DATA, &dummy);
1321 else
1322#endif
1323 if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50
1324 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1325 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1326 I2C_SMBUS_BYTE, &dummy);
1327 else
1328 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1329 I2C_SMBUS_QUICK, NULL);
1330
1331 return err >= 0;
1332}
1333
Jean Delvareccfbbd02009-12-06 17:06:25 +01001334static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001335 struct i2c_driver *driver)
1336{
1337 struct i2c_board_info info;
1338 struct i2c_adapter *adapter = temp_client->adapter;
1339 int addr = temp_client->addr;
1340 int err;
1341
1342 /* Make sure the address is valid */
1343 if (addr < 0x03 || addr > 0x77) {
1344 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1345 addr);
1346 return -EINVAL;
1347 }
1348
1349 /* Skip if already in use */
1350 if (i2c_check_addr(adapter, addr))
1351 return 0;
1352
Jean Delvareccfbbd02009-12-06 17:06:25 +01001353 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001354 if (!i2c_default_probe(adapter, addr))
1355 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001356
1357 /* Finally call the custom detection function */
1358 memset(&info, 0, sizeof(struct i2c_board_info));
1359 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001360 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001361 if (err) {
1362 /* -ENODEV is returned if the detection fails. We catch it
1363 here as this isn't an error. */
1364 return err == -ENODEV ? 0 : err;
1365 }
1366
1367 /* Consistency check */
1368 if (info.type[0] == '\0') {
1369 dev_err(&adapter->dev, "%s detection function provided "
1370 "no name for 0x%x\n", driver->driver.name,
1371 addr);
1372 } else {
1373 struct i2c_client *client;
1374
1375 /* Detection succeeded, instantiate the device */
1376 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1377 info.type, info.addr);
1378 client = i2c_new_device(adapter, &info);
1379 if (client)
1380 list_add_tail(&client->detected, &driver->clients);
1381 else
1382 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1383 info.type, info.addr);
1384 }
1385 return 0;
1386}
1387
1388static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1389{
Jean Delvarec3813d62009-12-14 21:17:25 +01001390 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001391 struct i2c_client *temp_client;
1392 int i, err = 0;
1393 int adap_id = i2c_adapter_id(adapter);
1394
Jean Delvarec3813d62009-12-14 21:17:25 +01001395 address_list = driver->address_list;
1396 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001397 return 0;
1398
1399 /* Set up a temporary client to help detect callback */
1400 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1401 if (!temp_client)
1402 return -ENOMEM;
1403 temp_client->adapter = adapter;
1404
Jean Delvare4329cf82008-08-28 08:33:23 +02001405 /* Stop here if the classes do not match */
1406 if (!(adapter->class & driver->class))
1407 goto exit_free;
1408
Jean Delvare4735c982008-07-14 22:38:36 +02001409 /* Stop here if we can't use SMBUS_QUICK */
1410 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
Jean Delvarec3813d62009-12-14 21:17:25 +01001411 if (address_list[0] == I2C_CLIENT_END)
Jean Delvare4735c982008-07-14 22:38:36 +02001412 goto exit_free;
1413
1414 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1415 "can't probe for chips\n");
1416 err = -EOPNOTSUPP;
1417 goto exit_free;
1418 }
1419
Jean Delvarec3813d62009-12-14 21:17:25 +01001420 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001421 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001422 "addr 0x%02x\n", adap_id, address_list[i]);
1423 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001424 err = i2c_detect_address(temp_client, driver);
Jean Delvare4735c982008-07-14 22:38:36 +02001425 if (err)
1426 goto exit_free;
1427 }
1428
1429 exit_free:
1430 kfree(temp_client);
1431 return err;
1432}
1433
Jean Delvare12b5053a2007-05-01 23:26:31 +02001434struct i2c_client *
1435i2c_new_probed_device(struct i2c_adapter *adap,
1436 struct i2c_board_info *info,
1437 unsigned short const *addr_list)
1438{
1439 int i;
1440
1441 /* Stop here if the bus doesn't support probing */
1442 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1443 dev_err(&adap->dev, "Probing not supported\n");
1444 return NULL;
1445 }
1446
Jean Delvare12b5053a2007-05-01 23:26:31 +02001447 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1448 /* Check address validity */
1449 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1450 dev_warn(&adap->dev, "Invalid 7-bit address "
1451 "0x%02x\n", addr_list[i]);
1452 continue;
1453 }
1454
1455 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001456 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001457 dev_dbg(&adap->dev, "Address 0x%02x already in "
1458 "use, not probing\n", addr_list[i]);
1459 continue;
1460 }
1461
Jean Delvare63e4e802010-06-03 11:33:51 +02001462 /* Test address responsiveness */
1463 if (i2c_default_probe(adap, addr_list[i]))
1464 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001465 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001466
1467 if (addr_list[i] == I2C_CLIENT_END) {
1468 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1469 return NULL;
1470 }
1471
1472 info->addr = addr_list[i];
1473 return i2c_new_device(adap, info);
1474}
1475EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1476
Farid Hammane7225acf2010-05-21 18:40:58 +02001477struct i2c_adapter *i2c_get_adapter(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001480
Jean Delvarecaada322008-01-27 18:14:49 +01001481 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001482 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001483 if (adapter && !try_module_get(adapter->owner))
1484 adapter = NULL;
1485
Jean Delvarecaada322008-01-27 18:14:49 +01001486 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001487 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
David Brownellc0564602007-05-01 23:26:31 +02001489EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491void i2c_put_adapter(struct i2c_adapter *adap)
1492{
1493 module_put(adap->owner);
1494}
David Brownellc0564602007-05-01 23:26:31 +02001495EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497/* The SMBus parts */
1498
David Brownell438d6c22006-12-10 21:21:31 +01001499#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001500static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001503
Farid Hammane7225acf2010-05-21 18:40:58 +02001504 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001505 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 data = data ^ POLY;
1507 data = data << 1;
1508 }
1509 return (u8)(data >> 8);
1510}
1511
Jean Delvare421ef472005-10-26 21:28:55 +02001512/* Incremental CRC8 over count bytes in the array pointed to by p */
1513static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 int i;
1516
Farid Hammane7225acf2010-05-21 18:40:58 +02001517 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001518 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return crc;
1520}
1521
Jean Delvare421ef472005-10-26 21:28:55 +02001522/* Assume a 7-bit address, which is reasonable for SMBus */
1523static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Jean Delvare421ef472005-10-26 21:28:55 +02001525 /* The address will be sent first */
1526 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1527 pec = i2c_smbus_pec(pec, &addr, 1);
1528
1529 /* The data buffer follows */
1530 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Jean Delvare421ef472005-10-26 21:28:55 +02001533/* Used for write only transactions */
1534static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Jean Delvare421ef472005-10-26 21:28:55 +02001536 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1537 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
Jean Delvare421ef472005-10-26 21:28:55 +02001540/* Return <0 on CRC error
1541 If there was a write before this read (most cases) we need to take the
1542 partial CRC from the write part into account.
1543 Note that this function does modify the message (we need to decrease the
1544 message length to hide the CRC byte from the caller). */
1545static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Jean Delvare421ef472005-10-26 21:28:55 +02001547 u8 rpec = msg->buf[--msg->len];
1548 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (rpec != cpec) {
1551 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1552 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001553 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
David Brownell438d6c22006-12-10 21:21:31 +01001555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
David Brownella1cdeda2008-07-14 22:38:24 +02001558/**
1559 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1560 * @client: Handle to slave device
1561 *
1562 * This executes the SMBus "receive byte" protocol, returning negative errno
1563 * else the byte received from the device.
1564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565s32 i2c_smbus_read_byte(struct i2c_client *client)
1566{
1567 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001568 int status;
1569
1570 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1571 I2C_SMBUS_READ, 0,
1572 I2C_SMBUS_BYTE, &data);
1573 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
David Brownellc0564602007-05-01 23:26:31 +02001575EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
David Brownella1cdeda2008-07-14 22:38:24 +02001577/**
1578 * i2c_smbus_write_byte - SMBus "send byte" protocol
1579 * @client: Handle to slave device
1580 * @value: Byte to be sent
1581 *
1582 * This executes the SMBus "send byte" protocol, returning negative errno
1583 * else zero on success.
1584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1586{
Farid Hammane7225acf2010-05-21 18:40:58 +02001587 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001588 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
David Brownellc0564602007-05-01 23:26:31 +02001590EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
David Brownella1cdeda2008-07-14 22:38:24 +02001592/**
1593 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1594 * @client: Handle to slave device
1595 * @command: Byte interpreted by slave
1596 *
1597 * This executes the SMBus "read byte" protocol, returning negative errno
1598 * else a data byte received from the device.
1599 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1601{
1602 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001603 int status;
1604
1605 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1606 I2C_SMBUS_READ, command,
1607 I2C_SMBUS_BYTE_DATA, &data);
1608 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
David Brownellc0564602007-05-01 23:26:31 +02001610EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
David Brownella1cdeda2008-07-14 22:38:24 +02001612/**
1613 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1614 * @client: Handle to slave device
1615 * @command: Byte interpreted by slave
1616 * @value: Byte being written
1617 *
1618 * This executes the SMBus "write byte" protocol, returning negative errno
1619 * else zero on success.
1620 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1622{
1623 union i2c_smbus_data data;
1624 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001625 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1626 I2C_SMBUS_WRITE, command,
1627 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
David Brownellc0564602007-05-01 23:26:31 +02001629EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
David Brownella1cdeda2008-07-14 22:38:24 +02001631/**
1632 * i2c_smbus_read_word_data - SMBus "read word" protocol
1633 * @client: Handle to slave device
1634 * @command: Byte interpreted by slave
1635 *
1636 * This executes the SMBus "read word" protocol, returning negative errno
1637 * else a 16-bit unsigned "word" received from the device.
1638 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1640{
1641 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001642 int status;
1643
1644 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1645 I2C_SMBUS_READ, command,
1646 I2C_SMBUS_WORD_DATA, &data);
1647 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
David Brownellc0564602007-05-01 23:26:31 +02001649EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
David Brownella1cdeda2008-07-14 22:38:24 +02001651/**
1652 * i2c_smbus_write_word_data - SMBus "write word" protocol
1653 * @client: Handle to slave device
1654 * @command: Byte interpreted by slave
1655 * @value: 16-bit "word" being written
1656 *
1657 * This executes the SMBus "write word" protocol, returning negative errno
1658 * else zero on success.
1659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1661{
1662 union i2c_smbus_data data;
1663 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001664 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1665 I2C_SMBUS_WRITE, command,
1666 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
David Brownellc0564602007-05-01 23:26:31 +02001668EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
David Brownella64ec072007-10-13 23:56:31 +02001670/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001671 * i2c_smbus_process_call - SMBus "process call" protocol
1672 * @client: Handle to slave device
1673 * @command: Byte interpreted by slave
1674 * @value: 16-bit "word" being written
1675 *
1676 * This executes the SMBus "process call" protocol, returning negative errno
1677 * else a 16-bit unsigned "word" received from the device.
1678 */
1679s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1680{
1681 union i2c_smbus_data data;
1682 int status;
1683 data.word = value;
1684
1685 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1686 I2C_SMBUS_WRITE, command,
1687 I2C_SMBUS_PROC_CALL, &data);
1688 return (status < 0) ? status : data.word;
1689}
1690EXPORT_SYMBOL(i2c_smbus_process_call);
1691
1692/**
David Brownella1cdeda2008-07-14 22:38:24 +02001693 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001694 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001695 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001696 * @values: Byte array into which data will be read; big enough to hold
1697 * the data returned by the slave. SMBus allows at most 32 bytes.
1698 *
David Brownella1cdeda2008-07-14 22:38:24 +02001699 * This executes the SMBus "block read" protocol, returning negative errno
1700 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001701 *
1702 * Note that using this function requires that the client's adapter support
1703 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1704 * support this; its emulation through I2C messaging relies on a specific
1705 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1706 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001707s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1708 u8 *values)
1709{
1710 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001711 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001712
David Brownell24a5bb72008-07-14 22:38:23 +02001713 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1714 I2C_SMBUS_READ, command,
1715 I2C_SMBUS_BLOCK_DATA, &data);
1716 if (status)
1717 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001718
1719 memcpy(values, &data.block[1], data.block[0]);
1720 return data.block[0];
1721}
1722EXPORT_SYMBOL(i2c_smbus_read_block_data);
1723
David Brownella1cdeda2008-07-14 22:38:24 +02001724/**
1725 * i2c_smbus_write_block_data - SMBus "block write" protocol
1726 * @client: Handle to slave device
1727 * @command: Byte interpreted by slave
1728 * @length: Size of data block; SMBus allows at most 32 bytes
1729 * @values: Byte array which will be written.
1730 *
1731 * This executes the SMBus "block write" protocol, returning negative errno
1732 * else zero on success.
1733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001735 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (length > I2C_SMBUS_BLOCK_MAX)
1740 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001742 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02001743 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1744 I2C_SMBUS_WRITE, command,
1745 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
David Brownellc0564602007-05-01 23:26:31 +02001747EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001750s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1751 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001754 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001755
Jean Delvare4b2643d2007-07-12 14:12:29 +02001756 if (length > I2C_SMBUS_BLOCK_MAX)
1757 length = I2C_SMBUS_BLOCK_MAX;
1758 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001759 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1760 I2C_SMBUS_READ, command,
1761 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1762 if (status < 0)
1763 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001764
1765 memcpy(values, &data.block[1], data.block[0]);
1766 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
David Brownellc0564602007-05-01 23:26:31 +02001768EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Jean Delvare21bbd692006-01-09 15:19:18 +11001770s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001771 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001772{
1773 union i2c_smbus_data data;
1774
1775 if (length > I2C_SMBUS_BLOCK_MAX)
1776 length = I2C_SMBUS_BLOCK_MAX;
1777 data.block[0] = length;
1778 memcpy(data.block + 1, values, length);
1779 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1780 I2C_SMBUS_WRITE, command,
1781 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1782}
David Brownellc0564602007-05-01 23:26:31 +02001783EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001784
David Brownell438d6c22006-12-10 21:21:31 +01001785/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02001787static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
1788 unsigned short flags,
1789 char read_write, u8 command, int size,
1790 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 /* So we need to generate a series of msgs. In the case of writing, we
1793 need to use only one message; when reading, we need two. We initialize
1794 most things with sane defaults, to keep the code below somewhat
1795 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001796 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1797 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02001798 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
David Brownell438d6c22006-12-10 21:21:31 +01001799 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1801 };
1802 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001803 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001804 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02001807 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 case I2C_SMBUS_QUICK:
1809 msg[0].len = 0;
1810 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001811 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1812 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 num = 1;
1814 break;
1815 case I2C_SMBUS_BYTE:
1816 if (read_write == I2C_SMBUS_READ) {
1817 /* Special case: only a read! */
1818 msg[0].flags = I2C_M_RD | flags;
1819 num = 1;
1820 }
1821 break;
1822 case I2C_SMBUS_BYTE_DATA:
1823 if (read_write == I2C_SMBUS_READ)
1824 msg[1].len = 1;
1825 else {
1826 msg[0].len = 2;
1827 msgbuf0[1] = data->byte;
1828 }
1829 break;
1830 case I2C_SMBUS_WORD_DATA:
1831 if (read_write == I2C_SMBUS_READ)
1832 msg[1].len = 2;
1833 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02001834 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001836 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838 break;
1839 case I2C_SMBUS_PROC_CALL:
1840 num = 2; /* Special case */
1841 read_write = I2C_SMBUS_READ;
1842 msg[0].len = 3;
1843 msg[1].len = 2;
1844 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001845 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 break;
1847 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001849 msg[1].flags |= I2C_M_RECV_LEN;
1850 msg[1].len = 1; /* block length will be added by
1851 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 } else {
1853 msg[0].len = data->block[0] + 2;
1854 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001855 dev_err(&adapter->dev,
1856 "Invalid block write size %d\n",
1857 data->block[0]);
1858 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001860 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 msgbuf0[i] = data->block[i-1];
1862 }
1863 break;
1864 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001865 num = 2; /* Another special case */
1866 read_write = I2C_SMBUS_READ;
1867 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001868 dev_err(&adapter->dev,
1869 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001870 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001871 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001872 }
1873 msg[0].len = data->block[0] + 2;
1874 for (i = 1; i < msg[0].len; i++)
1875 msgbuf0[i] = data->block[i-1];
1876 msg[1].flags |= I2C_M_RECV_LEN;
1877 msg[1].len = 1; /* block length will be added by
1878 the underlying bus driver */
1879 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 case I2C_SMBUS_I2C_BLOCK_DATA:
1881 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001882 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 } else {
1884 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001885 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001886 dev_err(&adapter->dev,
1887 "Invalid block write size %d\n",
1888 data->block[0]);
1889 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891 for (i = 1; i <= data->block[0]; i++)
1892 msgbuf0[i] = data->block[i];
1893 }
1894 break;
1895 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001896 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1897 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899
Jean Delvare421ef472005-10-26 21:28:55 +02001900 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1901 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1902 if (i) {
1903 /* Compute PEC if first message is a write */
1904 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001905 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001906 i2c_smbus_add_pec(&msg[0]);
1907 else /* Write followed by read */
1908 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1909 }
1910 /* Ask for PEC if last message is a read */
1911 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001912 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001913 }
1914
David Brownell24a5bb72008-07-14 22:38:23 +02001915 status = i2c_transfer(adapter, msg, num);
1916 if (status < 0)
1917 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Jean Delvare421ef472005-10-26 21:28:55 +02001919 /* Check PEC if last message is a read */
1920 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001921 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1922 if (status < 0)
1923 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001924 }
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02001927 switch (size) {
1928 case I2C_SMBUS_BYTE:
1929 data->byte = msgbuf0[0];
1930 break;
1931 case I2C_SMBUS_BYTE_DATA:
1932 data->byte = msgbuf1[0];
1933 break;
1934 case I2C_SMBUS_WORD_DATA:
1935 case I2C_SMBUS_PROC_CALL:
1936 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1937 break;
1938 case I2C_SMBUS_I2C_BLOCK_DATA:
1939 for (i = 0; i < data->block[0]; i++)
1940 data->block[i+1] = msgbuf1[i];
1941 break;
1942 case I2C_SMBUS_BLOCK_DATA:
1943 case I2C_SMBUS_BLOCK_PROC_CALL:
1944 for (i = 0; i < msgbuf1[0] + 1; i++)
1945 data->block[i] = msgbuf1[i];
1946 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948 return 0;
1949}
1950
David Brownella1cdeda2008-07-14 22:38:24 +02001951/**
1952 * i2c_smbus_xfer - execute SMBus protocol operations
1953 * @adapter: Handle to I2C bus
1954 * @addr: Address of SMBus slave on that bus
1955 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1956 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1957 * @command: Byte interpreted by slave, for protocols which use such bytes
1958 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1959 * @data: Data to be read or written
1960 *
1961 * This executes an SMBus protocol operation, and returns a negative
1962 * errno code else zero on success.
1963 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001964s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001965 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001966 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001968 unsigned long orig_jiffies;
1969 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 if (adapter->algo->smbus_xfer) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001975 rt_mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001976
1977 /* Retry automatically on arbitration loss */
1978 orig_jiffies = jiffies;
1979 for (res = 0, try = 0; try <= adapter->retries; try++) {
1980 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1981 read_write, command,
1982 protocol, data);
1983 if (res != -EAGAIN)
1984 break;
1985 if (time_after(jiffies,
1986 orig_jiffies + adapter->timeout))
1987 break;
1988 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001989 rt_mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 } else
Farid Hammane7225acf2010-05-21 18:40:58 +02001991 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001992 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return res;
1995}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1999MODULE_DESCRIPTION("I2C-Bus main module");
2000MODULE_LICENSE("GPL");