blob: af5172486fab080e006ff9fc9411092ed05ed574 [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 Rapoportcea443a82008-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
Jean Delvare656b8762010-06-03 11:33:53 +0200390/* And this is a strict address validity check, used when probing. If a
391 * device uses a reserved address, then it shouldn't be probed. 7-bit
392 * addressing is assumed, 10-bit address devices are rare and should be
393 * explicitly enumerated. */
394static int i2c_check_addr_validity(unsigned short addr)
395{
396 /*
397 * Reserved addresses per I2C specification:
398 * 0x00 General call address / START byte
399 * 0x01 CBUS address
400 * 0x02 Reserved for different bus format
401 * 0x03 Reserved for future purposes
402 * 0x04-0x07 Hs-mode master code
403 * 0x78-0x7b 10-bit slave addressing
404 * 0x7c-0x7f Reserved for future purposes
405 */
406 if (addr < 0x08 || addr > 0x77)
407 return -EINVAL;
408 return 0;
409}
410
David Brownell9c1600e2007-05-01 23:26:31 +0200411/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200412 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200413 * @adap: the adapter managing the device
414 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200415 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200416 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200417 * Create an i2c device. Binding is handled through driver model
418 * probe()/remove() methods. A driver may be bound to this device when we
419 * return from this function, or any later moment (e.g. maybe hotplugging will
420 * load the driver module). This call is not appropriate for use by mainboard
421 * initialization logic, which usually runs during an arch_initcall() long
422 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200423 *
424 * This returns the new i2c client, which may be saved for later use with
425 * i2c_unregister_device(); or NULL to indicate an error.
426 */
427struct i2c_client *
428i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
429{
430 struct i2c_client *client;
431 int status;
432
433 client = kzalloc(sizeof *client, GFP_KERNEL);
434 if (!client)
435 return NULL;
436
437 client->adapter = adap;
438
439 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200440
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200441 if (info->archdata)
442 client->dev.archdata = *info->archdata;
443
Marc Pignatee354252008-08-28 08:33:22 +0200444 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200445 client->addr = info->addr;
446 client->irq = info->irq;
447
David Brownell9c1600e2007-05-01 23:26:31 +0200448 strlcpy(client->name, info->type, sizeof(client->name));
449
Jean Delvare3a89db52010-06-03 11:33:52 +0200450 /* Check for address validity */
451 status = i2c_check_client_addr_validity(client);
452 if (status) {
453 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
454 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
455 goto out_err_silent;
456 }
457
Jean Delvaref8a227e2009-06-19 16:58:18 +0200458 /* Check for address business */
459 status = i2c_check_addr(adap, client->addr);
460 if (status)
461 goto out_err;
462
463 client->dev.parent = &client->adapter->dev;
464 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200465 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700466#ifdef CONFIG_OF
467 client->dev.of_node = info->of_node;
468#endif
Jean Delvaref8a227e2009-06-19 16:58:18 +0200469
470 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
471 client->addr);
472 status = device_register(&client->dev);
473 if (status)
474 goto out_err;
475
Jean Delvaref8a227e2009-06-19 16:58:18 +0200476 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
477 client->name, dev_name(&client->dev));
478
David Brownell9c1600e2007-05-01 23:26:31 +0200479 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200480
481out_err:
482 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
483 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200484out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200485 kfree(client);
486 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200487}
488EXPORT_SYMBOL_GPL(i2c_new_device);
489
490
491/**
492 * i2c_unregister_device - reverse effect of i2c_new_device()
493 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200494 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200495 */
496void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200497{
David Brownella1d9e6e2007-05-01 23:26:30 +0200498 device_unregister(&client->dev);
499}
David Brownell9c1600e2007-05-01 23:26:31 +0200500EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200501
502
Jean Delvare60b129d2008-05-11 20:37:06 +0200503static const struct i2c_device_id dummy_id[] = {
504 { "dummy", 0 },
505 { },
506};
507
Jean Delvared2653e92008-04-29 23:11:39 +0200508static int dummy_probe(struct i2c_client *client,
509 const struct i2c_device_id *id)
510{
511 return 0;
512}
513
514static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100515{
516 return 0;
517}
518
519static struct i2c_driver dummy_driver = {
520 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200521 .probe = dummy_probe,
522 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200523 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100524};
525
526/**
527 * i2c_new_dummy - return a new i2c device bound to a dummy driver
528 * @adapter: the adapter managing the device
529 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100530 * Context: can sleep
531 *
532 * This returns an I2C client bound to the "dummy" driver, intended for use
533 * with devices that consume multiple addresses. Examples of such chips
534 * include various EEPROMS (like 24c04 and 24c08 models).
535 *
536 * These dummy devices have two main uses. First, most I2C and SMBus calls
537 * except i2c_transfer() need a client handle; the dummy will be that handle.
538 * And second, this prevents the specified address from being bound to a
539 * different driver.
540 *
541 * This returns the new i2c client, which should be saved for later use with
542 * i2c_unregister_device(); or NULL to indicate an error.
543 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100544struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100545{
546 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200547 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100548 };
549
David Brownelle9f13732008-01-27 18:14:52 +0100550 return i2c_new_device(adapter, &info);
551}
552EXPORT_SYMBOL_GPL(i2c_new_dummy);
553
David Brownellf37dd802007-02-13 22:09:00 +0100554/* ------------------------------------------------------------------------- */
555
David Brownell16ffadf2007-05-01 23:26:28 +0200556/* I2C bus adapters -- one roots each I2C or SMBUS segment */
557
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200558static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
David Brownellef2c83212007-05-01 23:26:28 +0200560 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 complete(&adap->dev_released);
562}
563
Jean Delvare99cd8e22009-06-19 16:58:20 +0200564/*
565 * Let users instantiate I2C devices through sysfs. This can be used when
566 * platform initialization code doesn't contain the proper data for
567 * whatever reason. Also useful for drivers that do device detection and
568 * detection fails, either because the device uses an unexpected address,
569 * or this is a compatible device with different ID register values.
570 *
571 * Parameter checking may look overzealous, but we really don't want
572 * the user to provide incorrect parameters.
573 */
574static ssize_t
575i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
576 const char *buf, size_t count)
577{
578 struct i2c_adapter *adap = to_i2c_adapter(dev);
579 struct i2c_board_info info;
580 struct i2c_client *client;
581 char *blank, end;
582 int res;
583
584 dev_warn(dev, "The new_device interface is still experimental "
585 "and may change in a near future\n");
586 memset(&info, 0, sizeof(struct i2c_board_info));
587
588 blank = strchr(buf, ' ');
589 if (!blank) {
590 dev_err(dev, "%s: Missing parameters\n", "new_device");
591 return -EINVAL;
592 }
593 if (blank - buf > I2C_NAME_SIZE - 1) {
594 dev_err(dev, "%s: Invalid device name\n", "new_device");
595 return -EINVAL;
596 }
597 memcpy(info.type, buf, blank - buf);
598
599 /* Parse remaining parameters, reject extra parameters */
600 res = sscanf(++blank, "%hi%c", &info.addr, &end);
601 if (res < 1) {
602 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
603 return -EINVAL;
604 }
605 if (res > 1 && end != '\n') {
606 dev_err(dev, "%s: Extra parameters\n", "new_device");
607 return -EINVAL;
608 }
609
Jean Delvare99cd8e22009-06-19 16:58:20 +0200610 client = i2c_new_device(adap, &info);
611 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200612 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200613
614 /* Keep track of the added device */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200615 i2c_lock_adapter(adap);
616 list_add_tail(&client->detected, &adap->userspace_clients);
617 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200618 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
619 info.type, info.addr);
620
621 return count;
622}
623
624/*
625 * And of course let the users delete the devices they instantiated, if
626 * they got it wrong. This interface can only be used to delete devices
627 * instantiated by i2c_sysfs_new_device above. This guarantees that we
628 * don't delete devices to which some kernel code still has references.
629 *
630 * Parameter checking may look overzealous, but we really don't want
631 * the user to delete the wrong device.
632 */
633static ssize_t
634i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
635 const char *buf, size_t count)
636{
637 struct i2c_adapter *adap = to_i2c_adapter(dev);
638 struct i2c_client *client, *next;
639 unsigned short addr;
640 char end;
641 int res;
642
643 /* Parse parameters, reject extra parameters */
644 res = sscanf(buf, "%hi%c", &addr, &end);
645 if (res < 1) {
646 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
647 return -EINVAL;
648 }
649 if (res > 1 && end != '\n') {
650 dev_err(dev, "%s: Extra parameters\n", "delete_device");
651 return -EINVAL;
652 }
653
654 /* Make sure the device was added through sysfs */
655 res = -ENOENT;
Jean Delvare6629dcf2010-05-04 11:09:28 +0200656 i2c_lock_adapter(adap);
657 list_for_each_entry_safe(client, next, &adap->userspace_clients,
658 detected) {
659 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200660 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
661 "delete_device", client->name, client->addr);
662
663 list_del(&client->detected);
664 i2c_unregister_device(client);
665 res = count;
666 break;
667 }
668 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200669 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200670
671 if (res < 0)
672 dev_err(dev, "%s: Can't find device in list\n",
673 "delete_device");
674 return res;
675}
676
Jean Delvare4f8cf822009-09-18 22:45:46 +0200677static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
678static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
679
680static struct attribute *i2c_adapter_attrs[] = {
681 &dev_attr_name.attr,
682 &dev_attr_new_device.attr,
683 &dev_attr_delete_device.attr,
684 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200685};
686
Jean Delvare4f8cf822009-09-18 22:45:46 +0200687static struct attribute_group i2c_adapter_attr_group = {
688 .attrs = i2c_adapter_attrs,
689};
690
691static const struct attribute_group *i2c_adapter_attr_groups[] = {
692 &i2c_adapter_attr_group,
693 NULL
694};
695
696static struct device_type i2c_adapter_type = {
697 .groups = i2c_adapter_attr_groups,
698 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200699};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Jean Delvare2bb50952009-09-18 22:45:46 +0200701#ifdef CONFIG_I2C_COMPAT
702static struct class_compat *i2c_adapter_compat_class;
703#endif
704
David Brownell9c1600e2007-05-01 23:26:31 +0200705static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
706{
707 struct i2c_devinfo *devinfo;
708
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200709 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200710 list_for_each_entry(devinfo, &__i2c_board_list, list) {
711 if (devinfo->busnum == adapter->nr
712 && !i2c_new_device(adapter,
713 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100714 dev_err(&adapter->dev,
715 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200716 devinfo->board_info.addr);
717 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200718 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200719}
720
Jean Delvare69b00892009-12-06 17:06:27 +0100721static int i2c_do_add_adapter(struct i2c_driver *driver,
722 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100723{
Jean Delvare4735c982008-07-14 22:38:36 +0200724 /* Detect supported devices on that bus, and instantiate them */
725 i2c_detect(adap, driver);
726
727 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100728 if (driver->attach_adapter) {
729 /* We ignore the return code; if it fails, too bad */
730 driver->attach_adapter(adap);
731 }
732 return 0;
733}
734
Jean Delvare69b00892009-12-06 17:06:27 +0100735static int __process_new_adapter(struct device_driver *d, void *data)
736{
737 return i2c_do_add_adapter(to_i2c_driver(d), data);
738}
739
David Brownell6e13e642007-05-01 23:26:31 +0200740static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Jean Delvare026526f2008-01-27 18:14:49 +0100742 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
David Brownell1d0b19c2008-10-14 17:30:05 +0200744 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200745 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
746 res = -EAGAIN;
747 goto out_list;
748 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200749
Mika Kuoppala194684e2009-12-06 17:06:22 +0100750 rt_mutex_init(&adap->bus_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200751 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Jean Delvare8fcfef62009-03-28 21:34:43 +0100753 /* Set default timeout to 1 second if not already set */
754 if (adap->timeout == 0)
755 adap->timeout = HZ;
756
Kay Sievers27d9c182009-01-07 14:29:16 +0100757 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200758 adap->dev.bus = &i2c_bus_type;
759 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200760 res = device_register(&adap->dev);
761 if (res)
762 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200764 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
765
Jean Delvare2bb50952009-09-18 22:45:46 +0200766#ifdef CONFIG_I2C_COMPAT
767 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
768 adap->dev.parent);
769 if (res)
770 dev_warn(&adap->dev,
771 "Failed to create compatibility class link\n");
772#endif
773
Jean Delvare729d6dd2009-06-19 16:58:18 +0200774 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200775 if (adap->nr < __i2c_first_dynamic_bus_num)
776 i2c_scan_static_board_info(adap);
777
Jean Delvare4735c982008-07-14 22:38:36 +0200778 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200779 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100780 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100781 __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100782 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200783
784 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200785
Jean Delvareb119c6c2006-08-15 18:26:30 +0200786out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200787 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200788 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200789 mutex_unlock(&core_lock);
790 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
David Brownell6e13e642007-05-01 23:26:31 +0200793/**
794 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
795 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200796 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200797 *
798 * This routine is used to declare an I2C adapter when its bus number
799 * doesn't matter. Examples: for I2C adapters dynamically added by
800 * USB links or PCI plugin cards.
801 *
802 * When this returns zero, a new bus number was allocated and stored
803 * in adap->nr, and the specified adapter became available for clients.
804 * Otherwise, a negative errno value is returned.
805 */
806int i2c_add_adapter(struct i2c_adapter *adapter)
807{
808 int id, res = 0;
809
810retry:
811 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
812 return -ENOMEM;
813
Jean Delvarecaada322008-01-27 18:14:49 +0100814 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200815 /* "above" here means "above or equal to", sigh */
816 res = idr_get_new_above(&i2c_adapter_idr, adapter,
817 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100818 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200819
820 if (res < 0) {
821 if (res == -EAGAIN)
822 goto retry;
823 return res;
824 }
825
826 adapter->nr = id;
827 return i2c_register_adapter(adapter);
828}
829EXPORT_SYMBOL(i2c_add_adapter);
830
831/**
832 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
833 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200834 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200835 *
836 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100837 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
838 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200839 * is used to properly configure I2C devices.
840 *
841 * If no devices have pre-been declared for this bus, then be sure to
842 * register the adapter before any dynamically allocated ones. Otherwise
843 * the required bus ID may not be available.
844 *
845 * When this returns zero, the specified adapter became available for
846 * clients using the bus number provided in adap->nr. Also, the table
847 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
848 * and the appropriate driver model device nodes are created. Otherwise, a
849 * negative errno value is returned.
850 */
851int i2c_add_numbered_adapter(struct i2c_adapter *adap)
852{
853 int id;
854 int status;
855
856 if (adap->nr & ~MAX_ID_MASK)
857 return -EINVAL;
858
859retry:
860 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
861 return -ENOMEM;
862
Jean Delvarecaada322008-01-27 18:14:49 +0100863 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200864 /* "above" here means "above or equal to", sigh;
865 * we need the "equal to" result to force the result
866 */
867 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
868 if (status == 0 && id != adap->nr) {
869 status = -EBUSY;
870 idr_remove(&i2c_adapter_idr, id);
871 }
Jean Delvarecaada322008-01-27 18:14:49 +0100872 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200873 if (status == -EAGAIN)
874 goto retry;
875
876 if (status == 0)
877 status = i2c_register_adapter(adap);
878 return status;
879}
880EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
881
Jean Delvare69b00892009-12-06 17:06:27 +0100882static int i2c_do_del_adapter(struct i2c_driver *driver,
883 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +0100884{
Jean Delvare4735c982008-07-14 22:38:36 +0200885 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100886 int res;
887
Jean Delvareacec2112009-03-28 21:34:40 +0100888 /* Remove the devices we created ourselves as the result of hardware
889 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200890 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
891 if (client->adapter == adapter) {
892 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
893 client->name, client->addr);
894 list_del(&client->detected);
895 i2c_unregister_device(client);
896 }
897 }
898
Jean Delvare026526f2008-01-27 18:14:49 +0100899 if (!driver->detach_adapter)
900 return 0;
901 res = driver->detach_adapter(adapter);
902 if (res)
903 dev_err(&adapter->dev, "detach_adapter failed (%d) "
904 "for driver [%s]\n", res, driver->driver.name);
905 return res;
906}
907
Jean Delvaree549c2b2009-06-19 16:58:19 +0200908static int __unregister_client(struct device *dev, void *dummy)
909{
910 struct i2c_client *client = i2c_verify_client(dev);
911 if (client)
912 i2c_unregister_device(client);
913 return 0;
914}
915
Jean Delvare69b00892009-12-06 17:06:27 +0100916static int __process_removed_adapter(struct device_driver *d, void *data)
917{
918 return i2c_do_del_adapter(to_i2c_driver(d), data);
919}
920
David Brownelld64f73b2007-07-12 14:12:28 +0200921/**
922 * i2c_del_adapter - unregister I2C adapter
923 * @adap: the adapter being unregistered
924 * Context: can sleep
925 *
926 * This unregisters an I2C adapter which was previously registered
927 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929int i2c_del_adapter(struct i2c_adapter *adap)
930{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200932 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100933 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200936 mutex_lock(&core_lock);
937 found = idr_find(&i2c_adapter_idr, adap->nr);
938 mutex_unlock(&core_lock);
939 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200940 pr_debug("i2c-core: attempting to delete unregistered "
941 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200942 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944
Jean Delvare026526f2008-01-27 18:14:49 +0100945 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200946 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100947 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100948 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200949 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100950 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200951 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100953 /* Remove devices instantiated from sysfs */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200954 i2c_lock_adapter(adap);
955 list_for_each_entry_safe(client, next, &adap->userspace_clients,
956 detected) {
957 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
958 client->addr);
959 list_del(&client->detected);
960 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100961 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200962 i2c_unlock_adapter(adap);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100963
Jean Delvaree549c2b2009-06-19 16:58:19 +0200964 /* Detach any active clients. This can't fail, thus we do not
965 checking the returned value. */
966 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Jean Delvare2bb50952009-09-18 22:45:46 +0200968#ifdef CONFIG_I2C_COMPAT
969 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
970 adap->dev.parent);
971#endif
972
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +0100973 /* device name is gone after device_unregister */
974 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* clean up the sysfs representation */
977 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 /* wait for sysfs to drop all references */
981 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
David Brownell6e13e642007-05-01 23:26:31 +0200983 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200984 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200986 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200988 /* Clear the device structure in case this adapter is ever going to be
989 added again */
990 memset(&adap->dev, 0, sizeof(adap->dev));
991
Jean Delvare35fc37f2009-06-19 16:58:19 +0200992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
David Brownellc0564602007-05-01 23:26:31 +0200994EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996
David Brownell7b4fbc52007-05-01 23:26:30 +0200997/* ------------------------------------------------------------------------- */
998
Jean Delvare69b00892009-12-06 17:06:27 +0100999static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001000{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001001 if (dev->type != &i2c_adapter_type)
1002 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001003 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001004}
1005
David Brownell7b4fbc52007-05-01 23:26:30 +02001006/*
1007 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001008 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 */
1010
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001011int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001013 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
David Brownell1d0b19c2008-10-14 17:30:05 +02001015 /* Can't register until after driver model init */
1016 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1017 return -EAGAIN;
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001020 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Jean Delvare729d6dd2009-06-19 16:58:18 +02001023 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001024 * will have called probe() for all matching-but-unbound devices.
1025 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 res = driver_register(&driver->driver);
1027 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001028 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001029
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001030 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Jean Delvare4735c982008-07-14 22:38:36 +02001032 INIT_LIST_HEAD(&driver->clients);
1033 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001034 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +01001035 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
Jean Delvarecaada322008-01-27 18:14:49 +01001036 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001037
Jean Delvare7eebcb72006-02-05 23:28:21 +01001038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001040EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Jean Delvare69b00892009-12-06 17:06:27 +01001042static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001043{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001044 if (dev->type != &i2c_adapter_type)
1045 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001046 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001047}
1048
David Brownella1d9e6e2007-05-01 23:26:30 +02001049/**
1050 * i2c_del_driver - unregister I2C driver
1051 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001052 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001053 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001054void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Jean Delvarecaada322008-01-27 18:14:49 +01001056 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +01001057 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001058 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +02001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001061 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
David Brownellc0564602007-05-01 23:26:31 +02001063EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
David Brownell7b4fbc52007-05-01 23:26:30 +02001065/* ------------------------------------------------------------------------- */
1066
David Brownell9b766b82008-01-27 18:14:51 +01001067static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
David Brownell9b766b82008-01-27 18:14:51 +01001069 struct i2c_client *client = i2c_verify_client(dev);
1070 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
David Brownell9b766b82008-01-27 18:14:51 +01001072 if (client && client->addr == addr)
1073 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return 0;
1075}
1076
Jean Delvare5e31c2b2007-11-15 19:24:02 +01001077static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
David Brownell9b766b82008-01-27 18:14:51 +01001079 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Jean Delvaree48d3312008-01-27 18:14:48 +01001082/**
1083 * i2c_use_client - increments the reference count of the i2c client structure
1084 * @client: the client being referenced
1085 *
1086 * Each live reference to a client should be refcounted. The driver model does
1087 * that automatically as part of driver binding, so that most drivers don't
1088 * need to do this explicitly: they hold a reference until they're unbound
1089 * from the device.
1090 *
1091 * A pointer to the client with the incremented reference counter is returned.
1092 */
1093struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
David Brownell6ea438e2008-07-14 22:38:24 +02001095 if (client && get_device(&client->dev))
1096 return client;
1097 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
David Brownellc0564602007-05-01 23:26:31 +02001099EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Jean Delvaree48d3312008-01-27 18:14:48 +01001101/**
1102 * i2c_release_client - release a use of the i2c client structure
1103 * @client: the client being no longer referenced
1104 *
1105 * Must be called when a user of a client is finished with it.
1106 */
1107void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
David Brownell6ea438e2008-07-14 22:38:24 +02001109 if (client)
1110 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
David Brownellc0564602007-05-01 23:26:31 +02001112EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
David Brownell9b766b82008-01-27 18:14:51 +01001114struct i2c_cmd_arg {
1115 unsigned cmd;
1116 void *arg;
1117};
1118
1119static int i2c_cmd(struct device *dev, void *_arg)
1120{
1121 struct i2c_client *client = i2c_verify_client(dev);
1122 struct i2c_cmd_arg *arg = _arg;
1123
1124 if (client && client->driver && client->driver->command)
1125 client->driver->command(client, arg->cmd, arg->arg);
1126 return 0;
1127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1130{
David Brownell9b766b82008-01-27 18:14:51 +01001131 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
David Brownell9b766b82008-01-27 18:14:51 +01001133 cmd_arg.cmd = cmd;
1134 cmd_arg.arg = arg;
1135 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
David Brownellc0564602007-05-01 23:26:31 +02001137EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139static int __init i2c_init(void)
1140{
1141 int retval;
1142
1143 retval = bus_register(&i2c_bus_type);
1144 if (retval)
1145 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001146#ifdef CONFIG_I2C_COMPAT
1147 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1148 if (!i2c_adapter_compat_class) {
1149 retval = -ENOMEM;
1150 goto bus_err;
1151 }
1152#endif
David Brownelle9f13732008-01-27 18:14:52 +01001153 retval = i2c_add_driver(&dummy_driver);
1154 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001155 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001156 return 0;
1157
Jean Delvare2bb50952009-09-18 22:45:46 +02001158class_err:
1159#ifdef CONFIG_I2C_COMPAT
1160 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001161bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001162#endif
David Brownelle9f13732008-01-27 18:14:52 +01001163 bus_unregister(&i2c_bus_type);
1164 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167static void __exit i2c_exit(void)
1168{
David Brownelle9f13732008-01-27 18:14:52 +01001169 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001170#ifdef CONFIG_I2C_COMPAT
1171 class_compat_unregister(i2c_adapter_compat_class);
1172#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 bus_unregister(&i2c_bus_type);
1174}
1175
David Brownella10f9e72008-10-14 17:30:06 +02001176/* We must initialize early, because some subsystems register i2c drivers
1177 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1178 */
1179postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180module_exit(i2c_exit);
1181
1182/* ----------------------------------------------------
1183 * the functional interface to the i2c busses.
1184 * ----------------------------------------------------
1185 */
1186
David Brownella1cdeda2008-07-14 22:38:24 +02001187/**
1188 * i2c_transfer - execute a single or combined I2C message
1189 * @adap: Handle to I2C bus
1190 * @msgs: One or more messages to execute before STOP is issued to
1191 * terminate the operation; each message begins with a START.
1192 * @num: Number of messages to be executed.
1193 *
1194 * Returns negative errno, else the number of messages executed.
1195 *
1196 * Note that there is no requirement that each message be sent to
1197 * the same slave address, although that is the most common model.
1198 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001199int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001201 unsigned long orig_jiffies;
1202 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
David Brownella1cdeda2008-07-14 22:38:24 +02001204 /* REVISIT the fault reporting model here is weak:
1205 *
1206 * - When we get an error after receiving N bytes from a slave,
1207 * there is no way to report "N".
1208 *
1209 * - When we get a NAK after transmitting N bytes to a slave,
1210 * there is no way to report "N" ... or to let the master
1211 * continue executing the rest of this combined message, if
1212 * that's the appropriate response.
1213 *
1214 * - When for example "num" is two and we successfully complete
1215 * the first message but get an error part way through the
1216 * second, it's unclear whether that should be reported as
1217 * one (discarding status on the second message) or errno
1218 * (discarding status on the first one).
1219 */
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (adap->algo->master_xfer) {
1222#ifdef DEBUG
1223 for (ret = 0; ret < num; ret++) {
1224 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001225 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1226 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1227 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229#endif
1230
Mike Rapoportcea443a82008-01-27 18:14:50 +01001231 if (in_atomic() || irqs_disabled()) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001232 ret = rt_mutex_trylock(&adap->bus_lock);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001233 if (!ret)
1234 /* I2C activity is ongoing. */
1235 return -EAGAIN;
1236 } else {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001237 rt_mutex_lock(&adap->bus_lock);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001238 }
1239
Clifford Wolf66b650f2009-06-15 18:01:46 +02001240 /* Retry automatically on arbitration loss */
1241 orig_jiffies = jiffies;
1242 for (ret = 0, try = 0; try <= adap->retries; try++) {
1243 ret = adap->algo->master_xfer(adap, msgs, num);
1244 if (ret != -EAGAIN)
1245 break;
1246 if (time_after(jiffies, orig_jiffies + adap->timeout))
1247 break;
1248 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001249 rt_mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 return ret;
1252 } else {
1253 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001254 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256}
David Brownellc0564602007-05-01 23:26:31 +02001257EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
David Brownella1cdeda2008-07-14 22:38:24 +02001259/**
1260 * i2c_master_send - issue a single I2C message in master transmit mode
1261 * @client: Handle to slave device
1262 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001263 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001264 *
1265 * Returns negative errno, or else the number of bytes written.
1266 */
Farid Hammane7225acf2010-05-21 18:40:58 +02001267int i2c_master_send(struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001270 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct i2c_msg msg;
1272
Jean Delvare815f55f2005-05-07 22:58:46 +02001273 msg.addr = client->addr;
1274 msg.flags = client->flags & I2C_M_TEN;
1275 msg.len = count;
1276 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001277
Jean Delvare815f55f2005-05-07 22:58:46 +02001278 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Jean Delvare815f55f2005-05-07 22:58:46 +02001280 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1281 transmitted, else error code. */
1282 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
David Brownellc0564602007-05-01 23:26:31 +02001284EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
David Brownella1cdeda2008-07-14 22:38:24 +02001286/**
1287 * i2c_master_recv - issue a single I2C message in master receive mode
1288 * @client: Handle to slave device
1289 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001290 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001291 *
1292 * Returns negative errno, or else the number of bytes read.
1293 */
Farid Hammane7225acf2010-05-21 18:40:58 +02001294int i2c_master_recv(struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Farid Hammane7225acf2010-05-21 18:40:58 +02001296 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct i2c_msg msg;
1298 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Jean Delvare815f55f2005-05-07 22:58:46 +02001300 msg.addr = client->addr;
1301 msg.flags = client->flags & I2C_M_TEN;
1302 msg.flags |= I2C_M_RD;
1303 msg.len = count;
1304 msg.buf = buf;
1305
1306 ret = i2c_transfer(adap, &msg, 1);
1307
1308 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1309 transmitted, else error code. */
1310 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
David Brownellc0564602007-05-01 23:26:31 +02001312EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314/* ----------------------------------------------------
1315 * the i2c address scanning function
1316 * Will not work for 10-bit addresses!
1317 * ----------------------------------------------------
1318 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001319
Jean Delvare63e4e802010-06-03 11:33:51 +02001320/*
1321 * Legacy default probe function, mostly relevant for SMBus. The default
1322 * probe method is a quick write, but it is known to corrupt the 24RF08
1323 * EEPROMs due to a state machine bug, and could also irreversibly
1324 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1325 * we use a short byte read instead. Also, some bus drivers don't implement
1326 * quick write, so we fallback to a byte read in that case too.
1327 * On x86, there is another special case for FSC hardware monitoring chips,
1328 * which want regular byte reads (address 0x73.) Fortunately, these are the
1329 * only known chips using this I2C address on PC hardware.
1330 * Returns 1 if probe succeeded, 0 if not.
1331 */
1332static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1333{
1334 int err;
1335 union i2c_smbus_data dummy;
1336
1337#ifdef CONFIG_X86
1338 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1339 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1340 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1341 I2C_SMBUS_BYTE_DATA, &dummy);
1342 else
1343#endif
1344 if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50
1345 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1346 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1347 I2C_SMBUS_BYTE, &dummy);
1348 else
1349 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1350 I2C_SMBUS_QUICK, NULL);
1351
1352 return err >= 0;
1353}
1354
Jean Delvareccfbbd02009-12-06 17:06:25 +01001355static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001356 struct i2c_driver *driver)
1357{
1358 struct i2c_board_info info;
1359 struct i2c_adapter *adapter = temp_client->adapter;
1360 int addr = temp_client->addr;
1361 int err;
1362
1363 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001364 err = i2c_check_addr_validity(addr);
1365 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001366 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1367 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001368 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001369 }
1370
1371 /* Skip if already in use */
1372 if (i2c_check_addr(adapter, addr))
1373 return 0;
1374
Jean Delvareccfbbd02009-12-06 17:06:25 +01001375 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001376 if (!i2c_default_probe(adapter, addr))
1377 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001378
1379 /* Finally call the custom detection function */
1380 memset(&info, 0, sizeof(struct i2c_board_info));
1381 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001382 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001383 if (err) {
1384 /* -ENODEV is returned if the detection fails. We catch it
1385 here as this isn't an error. */
1386 return err == -ENODEV ? 0 : err;
1387 }
1388
1389 /* Consistency check */
1390 if (info.type[0] == '\0') {
1391 dev_err(&adapter->dev, "%s detection function provided "
1392 "no name for 0x%x\n", driver->driver.name,
1393 addr);
1394 } else {
1395 struct i2c_client *client;
1396
1397 /* Detection succeeded, instantiate the device */
1398 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1399 info.type, info.addr);
1400 client = i2c_new_device(adapter, &info);
1401 if (client)
1402 list_add_tail(&client->detected, &driver->clients);
1403 else
1404 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1405 info.type, info.addr);
1406 }
1407 return 0;
1408}
1409
1410static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1411{
Jean Delvarec3813d62009-12-14 21:17:25 +01001412 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001413 struct i2c_client *temp_client;
1414 int i, err = 0;
1415 int adap_id = i2c_adapter_id(adapter);
1416
Jean Delvarec3813d62009-12-14 21:17:25 +01001417 address_list = driver->address_list;
1418 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001419 return 0;
1420
1421 /* Set up a temporary client to help detect callback */
1422 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1423 if (!temp_client)
1424 return -ENOMEM;
1425 temp_client->adapter = adapter;
1426
Jean Delvare4329cf82008-08-28 08:33:23 +02001427 /* Stop here if the classes do not match */
1428 if (!(adapter->class & driver->class))
1429 goto exit_free;
1430
Jean Delvare4735c982008-07-14 22:38:36 +02001431 /* Stop here if we can't use SMBUS_QUICK */
1432 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
Jean Delvarec3813d62009-12-14 21:17:25 +01001433 if (address_list[0] == I2C_CLIENT_END)
Jean Delvare4735c982008-07-14 22:38:36 +02001434 goto exit_free;
1435
1436 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1437 "can't probe for chips\n");
1438 err = -EOPNOTSUPP;
1439 goto exit_free;
1440 }
1441
Jean Delvarec3813d62009-12-14 21:17:25 +01001442 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001443 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001444 "addr 0x%02x\n", adap_id, address_list[i]);
1445 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001446 err = i2c_detect_address(temp_client, driver);
Jean Delvare4735c982008-07-14 22:38:36 +02001447 if (err)
1448 goto exit_free;
1449 }
1450
1451 exit_free:
1452 kfree(temp_client);
1453 return err;
1454}
1455
Jean Delvare12b5053a2007-05-01 23:26:31 +02001456struct i2c_client *
1457i2c_new_probed_device(struct i2c_adapter *adap,
1458 struct i2c_board_info *info,
1459 unsigned short const *addr_list)
1460{
1461 int i;
1462
1463 /* Stop here if the bus doesn't support probing */
1464 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1465 dev_err(&adap->dev, "Probing not supported\n");
1466 return NULL;
1467 }
1468
Jean Delvare12b5053a2007-05-01 23:26:31 +02001469 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1470 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001471 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001472 dev_warn(&adap->dev, "Invalid 7-bit address "
1473 "0x%02x\n", addr_list[i]);
1474 continue;
1475 }
1476
1477 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001478 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001479 dev_dbg(&adap->dev, "Address 0x%02x already in "
1480 "use, not probing\n", addr_list[i]);
1481 continue;
1482 }
1483
Jean Delvare63e4e802010-06-03 11:33:51 +02001484 /* Test address responsiveness */
1485 if (i2c_default_probe(adap, addr_list[i]))
1486 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001487 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001488
1489 if (addr_list[i] == I2C_CLIENT_END) {
1490 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1491 return NULL;
1492 }
1493
1494 info->addr = addr_list[i];
1495 return i2c_new_device(adap, info);
1496}
1497EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1498
Farid Hammane7225acf2010-05-21 18:40:58 +02001499struct i2c_adapter *i2c_get_adapter(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001502
Jean Delvarecaada322008-01-27 18:14:49 +01001503 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001504 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001505 if (adapter && !try_module_get(adapter->owner))
1506 adapter = NULL;
1507
Jean Delvarecaada322008-01-27 18:14:49 +01001508 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001509 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
David Brownellc0564602007-05-01 23:26:31 +02001511EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513void i2c_put_adapter(struct i2c_adapter *adap)
1514{
1515 module_put(adap->owner);
1516}
David Brownellc0564602007-05-01 23:26:31 +02001517EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519/* The SMBus parts */
1520
David Brownell438d6c22006-12-10 21:21:31 +01001521#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001522static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001525
Farid Hammane7225acf2010-05-21 18:40:58 +02001526 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001527 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 data = data ^ POLY;
1529 data = data << 1;
1530 }
1531 return (u8)(data >> 8);
1532}
1533
Jean Delvare421ef472005-10-26 21:28:55 +02001534/* Incremental CRC8 over count bytes in the array pointed to by p */
1535static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 int i;
1538
Farid Hammane7225acf2010-05-21 18:40:58 +02001539 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001540 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return crc;
1542}
1543
Jean Delvare421ef472005-10-26 21:28:55 +02001544/* Assume a 7-bit address, which is reasonable for SMBus */
1545static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Jean Delvare421ef472005-10-26 21:28:55 +02001547 /* The address will be sent first */
1548 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1549 pec = i2c_smbus_pec(pec, &addr, 1);
1550
1551 /* The data buffer follows */
1552 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
Jean Delvare421ef472005-10-26 21:28:55 +02001555/* Used for write only transactions */
1556static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Jean Delvare421ef472005-10-26 21:28:55 +02001558 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1559 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Jean Delvare421ef472005-10-26 21:28:55 +02001562/* Return <0 on CRC error
1563 If there was a write before this read (most cases) we need to take the
1564 partial CRC from the write part into account.
1565 Note that this function does modify the message (we need to decrease the
1566 message length to hide the CRC byte from the caller). */
1567static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Jean Delvare421ef472005-10-26 21:28:55 +02001569 u8 rpec = msg->buf[--msg->len];
1570 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (rpec != cpec) {
1573 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1574 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001575 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
David Brownell438d6c22006-12-10 21:21:31 +01001577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
David Brownella1cdeda2008-07-14 22:38:24 +02001580/**
1581 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1582 * @client: Handle to slave device
1583 *
1584 * This executes the SMBus "receive byte" protocol, returning negative errno
1585 * else the byte received from the device.
1586 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587s32 i2c_smbus_read_byte(struct i2c_client *client)
1588{
1589 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001590 int status;
1591
1592 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1593 I2C_SMBUS_READ, 0,
1594 I2C_SMBUS_BYTE, &data);
1595 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
David Brownellc0564602007-05-01 23:26:31 +02001597EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
David Brownella1cdeda2008-07-14 22:38:24 +02001599/**
1600 * i2c_smbus_write_byte - SMBus "send byte" protocol
1601 * @client: Handle to slave device
1602 * @value: Byte to be sent
1603 *
1604 * This executes the SMBus "send byte" protocol, returning negative errno
1605 * else zero on success.
1606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1608{
Farid Hammane7225acf2010-05-21 18:40:58 +02001609 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001610 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
David Brownellc0564602007-05-01 23:26:31 +02001612EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
David Brownella1cdeda2008-07-14 22:38:24 +02001614/**
1615 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1616 * @client: Handle to slave device
1617 * @command: Byte interpreted by slave
1618 *
1619 * This executes the SMBus "read byte" protocol, returning negative errno
1620 * else a data byte received from the device.
1621 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1623{
1624 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001625 int status;
1626
1627 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1628 I2C_SMBUS_READ, command,
1629 I2C_SMBUS_BYTE_DATA, &data);
1630 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
David Brownellc0564602007-05-01 23:26:31 +02001632EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
David Brownella1cdeda2008-07-14 22:38:24 +02001634/**
1635 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1636 * @client: Handle to slave device
1637 * @command: Byte interpreted by slave
1638 * @value: Byte being written
1639 *
1640 * This executes the SMBus "write byte" protocol, returning negative errno
1641 * else zero on success.
1642 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1644{
1645 union i2c_smbus_data data;
1646 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001647 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1648 I2C_SMBUS_WRITE, command,
1649 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
David Brownellc0564602007-05-01 23:26:31 +02001651EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
David Brownella1cdeda2008-07-14 22:38:24 +02001653/**
1654 * i2c_smbus_read_word_data - SMBus "read word" protocol
1655 * @client: Handle to slave device
1656 * @command: Byte interpreted by slave
1657 *
1658 * This executes the SMBus "read word" protocol, returning negative errno
1659 * else a 16-bit unsigned "word" received from the device.
1660 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1662{
1663 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001664 int status;
1665
1666 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1667 I2C_SMBUS_READ, command,
1668 I2C_SMBUS_WORD_DATA, &data);
1669 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
David Brownellc0564602007-05-01 23:26:31 +02001671EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
David Brownella1cdeda2008-07-14 22:38:24 +02001673/**
1674 * i2c_smbus_write_word_data - SMBus "write word" protocol
1675 * @client: Handle to slave device
1676 * @command: Byte interpreted by slave
1677 * @value: 16-bit "word" being written
1678 *
1679 * This executes the SMBus "write word" protocol, returning negative errno
1680 * else zero on success.
1681 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1683{
1684 union i2c_smbus_data data;
1685 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001686 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1687 I2C_SMBUS_WRITE, command,
1688 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
David Brownellc0564602007-05-01 23:26:31 +02001690EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
David Brownella64ec072007-10-13 23:56:31 +02001692/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001693 * i2c_smbus_process_call - SMBus "process call" protocol
1694 * @client: Handle to slave device
1695 * @command: Byte interpreted by slave
1696 * @value: 16-bit "word" being written
1697 *
1698 * This executes the SMBus "process call" protocol, returning negative errno
1699 * else a 16-bit unsigned "word" received from the device.
1700 */
1701s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1702{
1703 union i2c_smbus_data data;
1704 int status;
1705 data.word = value;
1706
1707 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1708 I2C_SMBUS_WRITE, command,
1709 I2C_SMBUS_PROC_CALL, &data);
1710 return (status < 0) ? status : data.word;
1711}
1712EXPORT_SYMBOL(i2c_smbus_process_call);
1713
1714/**
David Brownella1cdeda2008-07-14 22:38:24 +02001715 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001716 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001717 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001718 * @values: Byte array into which data will be read; big enough to hold
1719 * the data returned by the slave. SMBus allows at most 32 bytes.
1720 *
David Brownella1cdeda2008-07-14 22:38:24 +02001721 * This executes the SMBus "block read" protocol, returning negative errno
1722 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001723 *
1724 * Note that using this function requires that the client's adapter support
1725 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1726 * support this; its emulation through I2C messaging relies on a specific
1727 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1728 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001729s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1730 u8 *values)
1731{
1732 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001733 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001734
David Brownell24a5bb72008-07-14 22:38:23 +02001735 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1736 I2C_SMBUS_READ, command,
1737 I2C_SMBUS_BLOCK_DATA, &data);
1738 if (status)
1739 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001740
1741 memcpy(values, &data.block[1], data.block[0]);
1742 return data.block[0];
1743}
1744EXPORT_SYMBOL(i2c_smbus_read_block_data);
1745
David Brownella1cdeda2008-07-14 22:38:24 +02001746/**
1747 * i2c_smbus_write_block_data - SMBus "block write" protocol
1748 * @client: Handle to slave device
1749 * @command: Byte interpreted by slave
1750 * @length: Size of data block; SMBus allows at most 32 bytes
1751 * @values: Byte array which will be written.
1752 *
1753 * This executes the SMBus "block write" protocol, returning negative errno
1754 * else zero on success.
1755 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001757 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (length > I2C_SMBUS_BLOCK_MAX)
1762 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001764 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02001765 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1766 I2C_SMBUS_WRITE, command,
1767 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
David Brownellc0564602007-05-01 23:26:31 +02001769EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001772s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1773 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001776 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001777
Jean Delvare4b2643d2007-07-12 14:12:29 +02001778 if (length > I2C_SMBUS_BLOCK_MAX)
1779 length = I2C_SMBUS_BLOCK_MAX;
1780 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001781 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1782 I2C_SMBUS_READ, command,
1783 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1784 if (status < 0)
1785 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001786
1787 memcpy(values, &data.block[1], data.block[0]);
1788 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
David Brownellc0564602007-05-01 23:26:31 +02001790EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Jean Delvare21bbd692006-01-09 15:19:18 +11001792s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001793 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001794{
1795 union i2c_smbus_data data;
1796
1797 if (length > I2C_SMBUS_BLOCK_MAX)
1798 length = I2C_SMBUS_BLOCK_MAX;
1799 data.block[0] = length;
1800 memcpy(data.block + 1, values, length);
1801 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1802 I2C_SMBUS_WRITE, command,
1803 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1804}
David Brownellc0564602007-05-01 23:26:31 +02001805EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001806
David Brownell438d6c22006-12-10 21:21:31 +01001807/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02001809static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
1810 unsigned short flags,
1811 char read_write, u8 command, int size,
1812 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 /* So we need to generate a series of msgs. In the case of writing, we
1815 need to use only one message; when reading, we need two. We initialize
1816 most things with sane defaults, to keep the code below somewhat
1817 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001818 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1819 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02001820 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
David Brownell438d6c22006-12-10 21:21:31 +01001821 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1823 };
1824 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001825 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001826 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02001829 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 case I2C_SMBUS_QUICK:
1831 msg[0].len = 0;
1832 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001833 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1834 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 num = 1;
1836 break;
1837 case I2C_SMBUS_BYTE:
1838 if (read_write == I2C_SMBUS_READ) {
1839 /* Special case: only a read! */
1840 msg[0].flags = I2C_M_RD | flags;
1841 num = 1;
1842 }
1843 break;
1844 case I2C_SMBUS_BYTE_DATA:
1845 if (read_write == I2C_SMBUS_READ)
1846 msg[1].len = 1;
1847 else {
1848 msg[0].len = 2;
1849 msgbuf0[1] = data->byte;
1850 }
1851 break;
1852 case I2C_SMBUS_WORD_DATA:
1853 if (read_write == I2C_SMBUS_READ)
1854 msg[1].len = 2;
1855 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02001856 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001858 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860 break;
1861 case I2C_SMBUS_PROC_CALL:
1862 num = 2; /* Special case */
1863 read_write = I2C_SMBUS_READ;
1864 msg[0].len = 3;
1865 msg[1].len = 2;
1866 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001867 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 break;
1869 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001871 msg[1].flags |= I2C_M_RECV_LEN;
1872 msg[1].len = 1; /* block length will be added by
1873 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 } else {
1875 msg[0].len = data->block[0] + 2;
1876 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001877 dev_err(&adapter->dev,
1878 "Invalid block write size %d\n",
1879 data->block[0]);
1880 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001882 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 msgbuf0[i] = data->block[i-1];
1884 }
1885 break;
1886 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001887 num = 2; /* Another special case */
1888 read_write = I2C_SMBUS_READ;
1889 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001890 dev_err(&adapter->dev,
1891 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001892 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001893 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001894 }
1895 msg[0].len = data->block[0] + 2;
1896 for (i = 1; i < msg[0].len; i++)
1897 msgbuf0[i] = data->block[i-1];
1898 msg[1].flags |= I2C_M_RECV_LEN;
1899 msg[1].len = 1; /* block length will be added by
1900 the underlying bus driver */
1901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 case I2C_SMBUS_I2C_BLOCK_DATA:
1903 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001904 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 } else {
1906 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001907 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001908 dev_err(&adapter->dev,
1909 "Invalid block write size %d\n",
1910 data->block[0]);
1911 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913 for (i = 1; i <= data->block[0]; i++)
1914 msgbuf0[i] = data->block[i];
1915 }
1916 break;
1917 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001918 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1919 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921
Jean Delvare421ef472005-10-26 21:28:55 +02001922 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1923 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1924 if (i) {
1925 /* Compute PEC if first message is a write */
1926 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001927 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001928 i2c_smbus_add_pec(&msg[0]);
1929 else /* Write followed by read */
1930 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1931 }
1932 /* Ask for PEC if last message is a read */
1933 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001934 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001935 }
1936
David Brownell24a5bb72008-07-14 22:38:23 +02001937 status = i2c_transfer(adapter, msg, num);
1938 if (status < 0)
1939 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Jean Delvare421ef472005-10-26 21:28:55 +02001941 /* Check PEC if last message is a read */
1942 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001943 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1944 if (status < 0)
1945 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001946 }
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02001949 switch (size) {
1950 case I2C_SMBUS_BYTE:
1951 data->byte = msgbuf0[0];
1952 break;
1953 case I2C_SMBUS_BYTE_DATA:
1954 data->byte = msgbuf1[0];
1955 break;
1956 case I2C_SMBUS_WORD_DATA:
1957 case I2C_SMBUS_PROC_CALL:
1958 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1959 break;
1960 case I2C_SMBUS_I2C_BLOCK_DATA:
1961 for (i = 0; i < data->block[0]; i++)
1962 data->block[i+1] = msgbuf1[i];
1963 break;
1964 case I2C_SMBUS_BLOCK_DATA:
1965 case I2C_SMBUS_BLOCK_PROC_CALL:
1966 for (i = 0; i < msgbuf1[0] + 1; i++)
1967 data->block[i] = msgbuf1[i];
1968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970 return 0;
1971}
1972
David Brownella1cdeda2008-07-14 22:38:24 +02001973/**
1974 * i2c_smbus_xfer - execute SMBus protocol operations
1975 * @adapter: Handle to I2C bus
1976 * @addr: Address of SMBus slave on that bus
1977 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1978 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1979 * @command: Byte interpreted by slave, for protocols which use such bytes
1980 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1981 * @data: Data to be read or written
1982 *
1983 * This executes an SMBus protocol operation, and returns a negative
1984 * errno code else zero on success.
1985 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001986s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001987 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001988 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001990 unsigned long orig_jiffies;
1991 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 if (adapter->algo->smbus_xfer) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001997 rt_mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001998
1999 /* Retry automatically on arbitration loss */
2000 orig_jiffies = jiffies;
2001 for (res = 0, try = 0; try <= adapter->retries; try++) {
2002 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2003 read_write, command,
2004 protocol, data);
2005 if (res != -EAGAIN)
2006 break;
2007 if (time_after(jiffies,
2008 orig_jiffies + adapter->timeout))
2009 break;
2010 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01002011 rt_mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 } else
Farid Hammane7225acf2010-05-21 18:40:58 +02002013 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02002014 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return res;
2017}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2021MODULE_DESCRIPTION("I2C-Bus main module");
2022MODULE_LICENSE("GPL");